1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
//! Data access layer (DAL) for ZKsync Era.

// Linter settings.
#![warn(clippy::cast_lossless)]

pub use sqlx::{types::BigDecimal, Error as SqlxError};
use zksync_db_connection::connection::DbMarker;
pub use zksync_db_connection::{
    connection::{Connection, IsolationLevel},
    connection_pool::{ConnectionPool, ConnectionPoolBuilder},
    error::{DalError, DalResult},
};

use crate::{
    base_token_dal::BaseTokenDal, blocks_dal::BlocksDal, blocks_web3_dal::BlocksWeb3Dal,
    consensus_dal::ConsensusDal, contract_verification_dal::ContractVerificationDal,
    data_availability_dal::DataAvailabilityDal, eth_sender_dal::EthSenderDal,
    events_dal::EventsDal, events_web3_dal::EventsWeb3Dal, factory_deps_dal::FactoryDepsDal,
    proof_generation_dal::ProofGenerationDal, protocol_versions_dal::ProtocolVersionsDal,
    protocol_versions_web3_dal::ProtocolVersionsWeb3Dal, pruning_dal::PruningDal,
    snapshot_recovery_dal::SnapshotRecoveryDal, snapshots_creator_dal::SnapshotsCreatorDal,
    snapshots_dal::SnapshotsDal, storage_logs_dal::StorageLogsDal,
    storage_logs_dedup_dal::StorageLogsDedupDal, storage_web3_dal::StorageWeb3Dal,
    sync_dal::SyncDal, system_dal::SystemDal, tee_proof_generation_dal::TeeProofGenerationDal,
    tee_verifier_input_producer_dal::TeeVerifierInputProducerDal, tokens_dal::TokensDal,
    tokens_web3_dal::TokensWeb3Dal, transactions_dal::TransactionsDal,
    transactions_web3_dal::TransactionsWeb3Dal, vm_runner_dal::VmRunnerDal,
};

pub mod base_token_dal;
pub mod blocks_dal;
pub mod blocks_web3_dal;
pub mod consensus;
pub mod consensus_dal;
pub mod contract_verification_dal;
mod data_availability_dal;
pub mod eth_sender_dal;
pub mod events_dal;
pub mod events_web3_dal;
pub mod factory_deps_dal;
pub mod helpers;
pub mod metrics;
mod models;
pub mod proof_generation_dal;
pub mod protocol_versions_dal;
pub mod protocol_versions_web3_dal;
pub mod pruning_dal;
pub mod snapshot_recovery_dal;
pub mod snapshots_creator_dal;
pub mod snapshots_dal;
pub mod storage_logs_dal;
pub mod storage_logs_dedup_dal;
pub mod storage_web3_dal;
pub mod sync_dal;
pub mod system_dal;
pub mod tee_proof_generation_dal;
pub mod tee_verifier_input_producer_dal;
pub mod tokens_dal;
pub mod tokens_web3_dal;
pub mod transactions_dal;
pub mod transactions_web3_dal;
pub mod vm_runner_dal;

#[cfg(test)]
mod tests;

// This module is private and serves as a way to seal the trait.
mod private {
    pub trait Sealed {}
}

// Here we are making the trait sealed, because it should be public to function correctly, but we don't
// want to allow any other downstream implementations of this trait.
pub trait CoreDal<'a>: private::Sealed
where
    Self: 'a,
{
    fn transactions_dal(&mut self) -> TransactionsDal<'_, 'a>;

    fn transactions_web3_dal(&mut self) -> TransactionsWeb3Dal<'_, 'a>;

    fn tee_verifier_input_producer_dal(&mut self) -> TeeVerifierInputProducerDal<'_, 'a>;

    fn blocks_dal(&mut self) -> BlocksDal<'_, 'a>;

    fn blocks_web3_dal(&mut self) -> BlocksWeb3Dal<'_, 'a>;

    fn consensus_dal(&mut self) -> ConsensusDal<'_, 'a>;

    fn eth_sender_dal(&mut self) -> EthSenderDal<'_, 'a>;

    fn events_dal(&mut self) -> EventsDal<'_, 'a>;

    fn events_web3_dal(&mut self) -> EventsWeb3Dal<'_, 'a>;

    fn factory_deps_dal(&mut self) -> FactoryDepsDal<'_, 'a>;

    fn storage_web3_dal(&mut self) -> StorageWeb3Dal<'_, 'a>;

    fn storage_logs_dal(&mut self) -> StorageLogsDal<'_, 'a>;

    fn storage_logs_dedup_dal(&mut self) -> StorageLogsDedupDal<'_, 'a>;

    fn tokens_dal(&mut self) -> TokensDal<'_, 'a>;

    fn tokens_web3_dal(&mut self) -> TokensWeb3Dal<'_, 'a>;

    fn contract_verification_dal(&mut self) -> ContractVerificationDal<'_, 'a>;

    fn protocol_versions_dal(&mut self) -> ProtocolVersionsDal<'_, 'a>;

    fn protocol_versions_web3_dal(&mut self) -> ProtocolVersionsWeb3Dal<'_, 'a>;

    fn sync_dal(&mut self) -> SyncDal<'_, 'a>;

    fn proof_generation_dal(&mut self) -> ProofGenerationDal<'_, 'a>;

    fn tee_proof_generation_dal(&mut self) -> TeeProofGenerationDal<'_, 'a>;

    fn system_dal(&mut self) -> SystemDal<'_, 'a>;

    fn snapshots_dal(&mut self) -> SnapshotsDal<'_, 'a>;

    fn snapshots_creator_dal(&mut self) -> SnapshotsCreatorDal<'_, 'a>;

    fn snapshot_recovery_dal(&mut self) -> SnapshotRecoveryDal<'_, 'a>;

    fn pruning_dal(&mut self) -> PruningDal<'_, 'a>;

    fn data_availability_dal(&mut self) -> DataAvailabilityDal<'_, 'a>;

    fn vm_runner_dal(&mut self) -> VmRunnerDal<'_, 'a>;

    fn base_token_dal(&mut self) -> BaseTokenDal<'_, 'a>;
}

#[derive(Clone, Debug)]
pub struct Core;

// Implement the marker trait for the Core to be able to use it in Connection.
impl DbMarker for Core {}
// Implement the sealed trait for the struct itself.
impl private::Sealed for Connection<'_, Core> {}

impl<'a> CoreDal<'a> for Connection<'a, Core> {
    fn transactions_dal(&mut self) -> TransactionsDal<'_, 'a> {
        TransactionsDal { storage: self }
    }

    fn transactions_web3_dal(&mut self) -> TransactionsWeb3Dal<'_, 'a> {
        TransactionsWeb3Dal { storage: self }
    }

    fn tee_verifier_input_producer_dal(&mut self) -> TeeVerifierInputProducerDal<'_, 'a> {
        TeeVerifierInputProducerDal { storage: self }
    }

    fn blocks_dal(&mut self) -> BlocksDal<'_, 'a> {
        BlocksDal { storage: self }
    }

    fn blocks_web3_dal(&mut self) -> BlocksWeb3Dal<'_, 'a> {
        BlocksWeb3Dal { storage: self }
    }

    fn consensus_dal(&mut self) -> ConsensusDal<'_, 'a> {
        ConsensusDal { storage: self }
    }

    fn eth_sender_dal(&mut self) -> EthSenderDal<'_, 'a> {
        EthSenderDal { storage: self }
    }

    fn events_dal(&mut self) -> EventsDal<'_, 'a> {
        EventsDal { storage: self }
    }

    fn events_web3_dal(&mut self) -> EventsWeb3Dal<'_, 'a> {
        EventsWeb3Dal { storage: self }
    }

    fn factory_deps_dal(&mut self) -> FactoryDepsDal<'_, 'a> {
        FactoryDepsDal { storage: self }
    }

    fn storage_web3_dal(&mut self) -> StorageWeb3Dal<'_, 'a> {
        StorageWeb3Dal { storage: self }
    }

    fn storage_logs_dal(&mut self) -> StorageLogsDal<'_, 'a> {
        StorageLogsDal { storage: self }
    }

    fn storage_logs_dedup_dal(&mut self) -> StorageLogsDedupDal<'_, 'a> {
        StorageLogsDedupDal { storage: self }
    }

    fn tokens_dal(&mut self) -> TokensDal<'_, 'a> {
        TokensDal { storage: self }
    }

    fn tokens_web3_dal(&mut self) -> TokensWeb3Dal<'_, 'a> {
        TokensWeb3Dal { storage: self }
    }

    fn contract_verification_dal(&mut self) -> ContractVerificationDal<'_, 'a> {
        ContractVerificationDal { storage: self }
    }

    fn protocol_versions_dal(&mut self) -> ProtocolVersionsDal<'_, 'a> {
        ProtocolVersionsDal { storage: self }
    }

    fn protocol_versions_web3_dal(&mut self) -> ProtocolVersionsWeb3Dal<'_, 'a> {
        ProtocolVersionsWeb3Dal { storage: self }
    }

    fn sync_dal(&mut self) -> SyncDal<'_, 'a> {
        SyncDal { storage: self }
    }

    fn proof_generation_dal(&mut self) -> ProofGenerationDal<'_, 'a> {
        ProofGenerationDal { storage: self }
    }

    fn tee_proof_generation_dal(&mut self) -> TeeProofGenerationDal<'_, 'a> {
        TeeProofGenerationDal { storage: self }
    }

    fn system_dal(&mut self) -> SystemDal<'_, 'a> {
        SystemDal { storage: self }
    }

    fn snapshots_dal(&mut self) -> SnapshotsDal<'_, 'a> {
        SnapshotsDal { storage: self }
    }

    fn snapshots_creator_dal(&mut self) -> SnapshotsCreatorDal<'_, 'a> {
        SnapshotsCreatorDal { storage: self }
    }

    fn snapshot_recovery_dal(&mut self) -> SnapshotRecoveryDal<'_, 'a> {
        SnapshotRecoveryDal { storage: self }
    }

    fn pruning_dal(&mut self) -> PruningDal<'_, 'a> {
        PruningDal { storage: self }
    }

    fn data_availability_dal(&mut self) -> DataAvailabilityDal<'_, 'a> {
        DataAvailabilityDal { storage: self }
    }

    fn vm_runner_dal(&mut self) -> VmRunnerDal<'_, 'a> {
        VmRunnerDal { storage: self }
    }

    fn base_token_dal(&mut self) -> BaseTokenDal<'_, 'a> {
        BaseTokenDal { storage: self }
    }
}