1
- use std:: collections:: BTreeSet ;
2
1
use std:: path:: Path ;
3
2
use std:: time:: Duration ;
4
3
5
4
use affair:: AsyncWorker as WorkerTrait ;
6
5
use anyhow:: { Context , Result } ;
7
- use atomo:: { Atomo , AtomoBuilder , DefaultSerdeBackend , QueryPerm , StorageBackend , UpdatePerm } ;
6
+ use atomo:: { AtomoBuilder , DefaultSerdeBackend , SerdeBackend , StorageBackend } ;
8
7
use atomo_rocks:: { Cache as RocksCache , Env as RocksEnv , Options } ;
9
8
use fleek_crypto:: { ClientPublicKey , ConsensusPublicKey , EthAddress , NodePublicKey } ;
10
9
use hp_fixed:: unsigned:: HpUfixed ;
11
10
use lightning_interfaces:: prelude:: * ;
12
11
use lightning_interfaces:: types:: {
13
12
AccountInfo ,
14
- Blake3Hash ,
15
13
Block ,
16
14
BlockExecutionResponse ,
17
15
Committee ,
@@ -24,31 +22,28 @@ use lightning_interfaces::types::{
24
22
NodeInfo ,
25
23
NodeServed ,
26
24
ProtocolParams ,
27
- ReportedReputationMeasurements ,
28
25
Service ,
29
26
ServiceId ,
30
- ServiceRevenue ,
31
27
TotalServed ,
32
28
TransactionReceipt ,
33
29
TransactionResponse ,
34
- TxHash ,
35
30
Value ,
36
31
} ;
37
32
use lightning_metrics:: increment_counter;
38
33
use tracing:: warn;
39
34
40
35
use crate :: config:: { Config , StorageConfig } ;
41
36
use crate :: genesis:: GenesisPrices ;
42
- use crate :: query_runner:: QueryRunner ;
43
- use crate :: state:: State ;
37
+ use crate :: state:: { ApplicationState , QueryRunner } ;
44
38
use crate :: storage:: { AtomoStorage , AtomoStorageBuilder } ;
45
- use crate :: table:: StateTables ;
46
39
47
- pub struct Env < P , B : StorageBackend > {
48
- pub inner : Atomo < P , B > ,
40
+ pub struct Env < B : StorageBackend , S : SerdeBackend > {
41
+ pub inner : ApplicationState < B , S > ,
49
42
}
50
43
51
- impl Env < UpdatePerm , AtomoStorage > {
44
+ pub type ApplicationEnv = Env < AtomoStorage , DefaultSerdeBackend > ;
45
+
46
+ impl ApplicationEnv {
52
47
pub fn new ( config : & Config , checkpoint : Option < ( [ u8 ; 32 ] , & [ u8 ] ) > ) -> Result < Self > {
53
48
let storage = match config. storage {
54
49
StorageConfig :: RocksDb => {
@@ -83,60 +78,17 @@ impl Env<UpdatePerm, AtomoStorage> {
83
78
StorageConfig :: InMemory => AtomoStorageBuilder :: new :: < & Path > ( None ) ,
84
79
} ;
85
80
86
- let mut atomo = AtomoBuilder :: < AtomoStorageBuilder , DefaultSerdeBackend > :: new ( storage) ;
87
- atomo = atomo
88
- . with_table :: < Metadata , Value > ( "metadata" )
89
- . with_table :: < EthAddress , AccountInfo > ( "account" )
90
- . with_table :: < ClientPublicKey , EthAddress > ( "client_keys" )
91
- . with_table :: < NodeIndex , NodeInfo > ( "node" )
92
- . with_table :: < ConsensusPublicKey , NodeIndex > ( "consensus_key_to_index" )
93
- . with_table :: < NodePublicKey , NodeIndex > ( "pub_key_to_index" )
94
- . with_table :: < ( NodeIndex , NodeIndex ) , Duration > ( "latencies" )
95
- . with_table :: < Epoch , Committee > ( "committee" )
96
- . with_table :: < ServiceId , Service > ( "service" )
97
- . with_table :: < ProtocolParams , u128 > ( "parameter" )
98
- . with_table :: < NodeIndex , Vec < ReportedReputationMeasurements > > ( "rep_measurements" )
99
- . with_table :: < NodeIndex , u8 > ( "rep_scores" )
100
- . with_table :: < NodeIndex , u8 > ( "submitted_rep_measurements" )
101
- . with_table :: < NodeIndex , NodeServed > ( "current_epoch_served" )
102
- . with_table :: < NodeIndex , NodeServed > ( "last_epoch_served" )
103
- . with_table :: < Epoch , TotalServed > ( "total_served" )
104
- . with_table :: < CommodityTypes , HpUfixed < 6 > > ( "commodity_prices" )
105
- . with_table :: < ServiceId , ServiceRevenue > ( "service_revenue" )
106
- . with_table :: < TxHash , ( ) > ( "executed_digests" )
107
- . with_table :: < NodeIndex , u8 > ( "uptime" )
108
- . with_table :: < Blake3Hash , BTreeSet < NodeIndex > > ( "uri_to_node" )
109
- . with_table :: < NodeIndex , BTreeSet < Blake3Hash > > ( "node_to_uri" )
110
- . enable_iter ( "current_epoch_served" )
111
- . enable_iter ( "rep_measurements" )
112
- . enable_iter ( "submitted_rep_measurements" )
113
- . enable_iter ( "rep_scores" )
114
- . enable_iter ( "latencies" )
115
- . enable_iter ( "node" )
116
- . enable_iter ( "executed_digests" )
117
- . enable_iter ( "uptime" )
118
- . enable_iter ( "service_revenue" )
119
- . enable_iter ( "uri_to_node" )
120
- . enable_iter ( "node_to_uri" ) ;
121
-
122
- #[ cfg( debug_assertions) ]
123
- {
124
- atomo = atomo
125
- . enable_iter ( "consensus_key_to_index" )
126
- . enable_iter ( "pub_key_to_index" ) ;
127
- }
81
+ let atomo = AtomoBuilder :: < AtomoStorageBuilder , DefaultSerdeBackend > :: new ( storage) ;
128
82
129
83
Ok ( Self {
130
- inner : atomo . build ( ) ?,
84
+ inner : ApplicationState :: build ( atomo ) ?,
131
85
} )
132
86
}
133
87
134
88
pub fn query_runner ( & self ) -> QueryRunner {
135
- QueryRunner :: new ( self . inner . query ( ) )
89
+ self . inner . query ( )
136
90
}
137
- }
138
91
139
- impl < B : StorageBackend > Env < UpdatePerm , B > {
140
92
#[ autometrics:: autometrics]
141
93
async fn run < F , P > ( & mut self , mut block : Block , get_putter : F ) -> BlockExecutionResponse
142
94
where
@@ -145,10 +97,7 @@ impl<B: StorageBackend> Env<UpdatePerm, B> {
145
97
{
146
98
let response = self . inner . run ( move |ctx| {
147
99
// Create the app/execution environment
148
- let backend = StateTables {
149
- table_selector : ctx,
150
- } ;
151
- let app = State :: new ( backend) ;
100
+ let app = ApplicationState :: executor ( ctx) ;
152
101
let last_block_hash = app. get_block_hash ( ) ;
153
102
154
103
let block_number = app. get_block_number ( ) + 1 ;
@@ -199,14 +148,14 @@ impl<B: StorageBackend> Env<UpdatePerm, B> {
199
148
response. txn_receipts . push ( receipt) ;
200
149
}
201
150
151
+ // Set the last executed block hash and sub dag index
202
152
// if epoch changed a new committee starts and subdag starts back at 0
203
153
let ( new_sub_dag_index, new_sub_dag_round) = if response. change_epoch {
204
154
( 0 , 0 )
205
155
} else {
206
156
( block. sub_dag_index , block. sub_dag_round )
207
157
} ;
208
- // Set the last executed block hash and sub dag index
209
- app. set_last_block ( block. digest , new_sub_dag_index, new_sub_dag_round) ;
158
+ app. set_last_block ( response. block_hash , new_sub_dag_index, new_sub_dag_round) ;
210
159
211
160
// Return the response
212
161
response
@@ -243,13 +192,6 @@ impl<B: StorageBackend> Env<UpdatePerm, B> {
243
192
response
244
193
}
245
194
246
- /// Returns an identical environment but with query permissions
247
- pub fn query_socket ( & self ) -> Env < QueryPerm , B > {
248
- Env {
249
- inner : self . inner . query ( ) ,
250
- }
251
- }
252
-
253
195
/// Tries to seeds the application state with the genesis block
254
196
/// This function will panic if the genesis file cannot be decoded into the correct types
255
197
/// Will return true if database was empty and genesis needed to be loaded or false if there was
@@ -461,37 +403,34 @@ impl<B: StorageBackend> Env<UpdatePerm, B> {
461
403
}
462
404
}
463
405
464
- metadata_table. insert ( Metadata :: Epoch , Value :: Epoch ( 0 ) ) ;
465
- Ok ( true )
406
+ metadata_table. insert ( Metadata :: Epoch , Value :: Epoch ( 0 ) ) ;
407
+ Ok ( true )
466
408
} )
467
409
}
468
410
469
411
// Should only be called after saving or loading from an epoch checkpoint
470
412
pub fn update_last_epoch_hash ( & mut self , state_hash : [ u8 ; 32 ] ) {
471
413
self . inner . run ( move |ctx| {
472
- let backend = StateTables {
473
- table_selector : ctx,
474
- } ;
475
- let app = State :: new ( backend) ;
414
+ let app = ApplicationState :: executor ( ctx) ;
476
415
app. set_last_epoch_hash ( state_hash) ;
477
416
} )
478
417
}
479
418
}
480
419
481
- impl Default for Env < UpdatePerm , AtomoStorage > {
420
+ impl Default for ApplicationEnv {
482
421
fn default ( ) -> Self {
483
422
Self :: new ( & Config :: default ( ) , None ) . unwrap ( )
484
423
}
485
424
}
486
425
487
426
/// The socket that receives all update transactions
488
427
pub struct UpdateWorker < C : Collection > {
489
- env : Env < UpdatePerm , AtomoStorage > ,
428
+ env : ApplicationEnv ,
490
429
blockstore : C :: BlockstoreInterface ,
491
430
}
492
431
493
432
impl < C : Collection > UpdateWorker < C > {
494
- pub fn new ( env : Env < UpdatePerm , AtomoStorage > , blockstore : C :: BlockstoreInterface ) -> Self {
433
+ pub fn new ( env : ApplicationEnv , blockstore : C :: BlockstoreInterface ) -> Self {
495
434
Self { env, blockstore }
496
435
}
497
436
}
@@ -518,7 +457,7 @@ mod env_tests {
518
457
. write_to_dir ( temp_dir. path ( ) . to_path_buf ( ) . try_into ( ) . unwrap ( ) )
519
458
. unwrap ( ) ;
520
459
let config = Config :: test ( genesis_path) ;
521
- let mut env = Env :: < _ , AtomoStorage > :: new ( & config, None ) . unwrap ( ) ;
460
+ let mut env = ApplicationEnv :: new ( & config, None ) . unwrap ( ) ;
522
461
523
462
assert ! ( env. apply_genesis_block( & config) . unwrap( ) ) ;
524
463
0 commit comments