1
- use crate :: types:: Block ;
2
1
use aptos_db:: AptosDB ;
3
2
use aptos_executor:: block_executor:: BlockExecutor ;
4
3
use aptos_executor_types:: state_checkpoint_output:: StateCheckpointOutput ;
@@ -9,8 +8,9 @@ use aptos_types::block_executor::config::BlockExecutorConfigFromOnchain;
9
8
use aptos_types:: block_executor:: partitioner:: ExecutableBlock ;
10
9
use aptos_types:: validator_signer:: ValidatorSigner ;
11
10
use aptos_vm:: AptosVM ;
12
- use std:: sync:: { Arc , RwLock } ;
11
+ use std:: sync:: RwLock ;
13
12
13
+ /// The name that appends the dir path of the rocksdb.
14
14
const APTOS_DB_DIR : & str = ".aptosdb-block-executor" ;
15
15
16
16
/// The state of `movement-network` execution can exist in three states,
@@ -47,23 +47,23 @@ pub enum ExecutorState {
47
47
/// against the `AptosVM`.
48
48
pub struct Executor {
49
49
/// The executing type.
50
- pub block_executor : Arc < RwLock < BlockExecutor < AptosVM > > > ,
50
+ pub block_executor : BlockExecutor < AptosVM > ,
51
51
/// The current state of the executor.
52
52
pub status : ExecutorState ,
53
53
/// The access to db.
54
54
pub db : DbReaderWriter ,
55
55
/// The signer of the executor's transactions.
56
- pub signer : Option < ValidatorSigner > ,
56
+ pub signer : ValidatorSigner ,
57
57
/// The access to the core mempool.
58
- pub mempool : Arc < RwLock < CoreMempool > > ,
58
+ pub mempool : CoreMempool ,
59
59
}
60
60
61
61
impl Executor {
62
62
/// Create a new `Executor` instance.
63
63
pub fn new (
64
- block_executor : Arc < RwLock < BlockExecutor < AptosVM > > > ,
65
- signer : Option < ValidatorSigner > ,
66
- mempool : Arc < RwLock < CoreMempool > > ,
64
+ block_executor : BlockExecutor < AptosVM > ,
65
+ signer : ValidatorSigner ,
66
+ mempool : CoreMempool ,
67
67
) -> Self {
68
68
let path = format ! ( "{}/{}" , dirs:: home_dir( ) . unwrap( ) . to_str( ) . unwrap( ) , APTOS_DB_DIR ) ;
69
69
let ( _aptos_db, reader_writer) = DbReaderWriter :: wrap ( AptosDB :: new_for_test ( path. as_str ( ) ) ) ;
@@ -79,10 +79,9 @@ impl Executor {
79
79
if self . status != ExecutorState :: Commit {
80
80
return Err ( anyhow:: anyhow!( "Executor is not in the Commit state" ) ) ;
81
81
}
82
- let executor = self . block_executor . write ( ) . unwrap ( ) ;
83
- let parent_block_id = executor. committed_block_id ( ) ;
82
+ let parent_block_id = self . block_executor . committed_block_id ( ) ;
84
83
log:: info!( "Executing block: {:?}" , block. block_id) ;
85
- let state_checkpoint = executor . execute_and_state_checkpoint (
84
+ let state_checkpoint = self . block_executor . execute_and_state_checkpoint (
86
85
block,
87
86
parent_block_id,
88
87
BlockExecutorConfigFromOnchain :: new_no_block_limit ( ) ,
@@ -98,58 +97,60 @@ impl Executor {
98
97
#[ cfg( test) ]
99
98
mod tests {
100
99
use super :: * ;
101
- use aptos_crypto:: ed25519:: Ed25519PrivateKey ;
102
- use aptos_types:: {
103
- block_info:: BlockInfo ,
104
- ledger_info:: { LedgerInfo , LedgerInfoWithSignatures } ,
105
- validator_signer:: ValidatorSigner ,
100
+ use aptos_config:: config:: NodeConfig ;
101
+ use aptos_crypto:: ed25519:: { Ed25519PrivateKey , Ed25519Signature } ;
102
+ use aptos_crypto:: { HashValue , PrivateKey , Uniform } ;
103
+ use aptos_executor:: block_executor:: BlockExecutor ;
104
+ use aptos_storage_interface:: DbReaderWriter ;
105
+ use aptos_types:: account_address:: AccountAddress ;
106
+ use aptos_types:: block_executor:: partitioner:: ExecutableTransactions ;
107
+ use aptos_types:: chain_id:: ChainId ;
108
+ use aptos_types:: transaction:: signature_verified_transaction:: SignatureVerifiedTransaction ;
109
+ use aptos_types:: transaction:: {
110
+ RawTransaction , Script , SignedTransaction , Transaction , TransactionPayload ,
106
111
} ;
107
- use aptos_vm:: AptosVM ;
108
- use executor:: block_executor:: BlockExecutor ;
109
- use std:: sync:: Arc ;
112
+ use aptos_types:: validator_signer:: ValidatorSigner ;
113
+
114
+ fn init_executor ( ) -> Executor {
115
+ let ( _, reader_writer) = DbReaderWriter :: wrap ( AptosDB :: new_for_test ( "" ) ) ;
116
+ let block_executor = BlockExecutor :: new ( reader_writer) ;
117
+ let signer = ValidatorSigner :: random ( None ) ;
118
+ let mempool = CoreMempool :: new ( & NodeConfig :: default ( ) ) ;
119
+ Executor :: new ( block_executor, signer, mempool)
120
+ }
121
+
122
+ fn create_signed_transaction ( gas_unit_price : u64 ) -> SignedTransaction {
123
+ let private_key = Ed25519PrivateKey :: generate_for_testing ( ) ;
124
+ let public_key = private_key. public_key ( ) ;
125
+
126
+ let transaction_payload = TransactionPayload :: Script ( Script :: new ( vec ! [ ] , vec ! [ ] , vec ! [ ] ) ) ;
127
+ let raw_transaction = RawTransaction :: new (
128
+ AccountAddress :: random ( ) ,
129
+ 0 ,
130
+ transaction_payload,
131
+ 0 ,
132
+ gas_unit_price,
133
+ 0 ,
134
+ ChainId :: new ( 10 ) , // This is the value used in aptos testing code.
135
+ ) ;
136
+ SignedTransaction :: new ( raw_transaction, public_key, Ed25519Signature :: dummy_signature ( ) )
137
+ }
110
138
111
139
#[ test]
112
140
fn test_executor_new ( ) {
113
- let block_executor = Arc :: new ( RwLock :: new ( BlockExecutor :: < AptosVM > :: new ( Arc :: new (
114
- DbReaderWriter :: new ( DbReader :: new ( Arc :: new ( MockTreeStore :: default ( ) ) ) , None ) ,
115
- ) ) ) ) ;
116
- let signer =
117
- Some ( ValidatorSigner :: new ( Vec :: new ( ) , Ed25519PrivateKey :: generate_for_testing ( ) ) ) ;
118
- let mempool = Arc :: new ( RwLock :: new ( CoreMempool :: new ( Arc :: new ( MockDB :: default ( ) ) ) ) ) ;
119
-
120
- let executor = Executor :: new ( block_executor, signer, mempool) ;
121
-
141
+ let executor = init_executor ( ) ;
122
142
assert_eq ! ( executor. status, ExecutorState :: Idle ) ;
123
- assert ! ( executor. signer. is_some( ) ) ;
124
143
}
125
144
126
145
#[ tokio:: test]
127
146
async fn test_execute_block ( ) {
128
- let block_executor = Arc :: new ( RwLock :: new ( BlockExecutor :: < AptosVM > :: new ( Arc :: new (
129
- DbReaderWriter :: new ( DbReader :: new ( Arc :: new ( MockTreeStore :: default ( ) ) ) , None ) ,
130
- ) ) ) ) ;
131
- let signer =
132
- Some ( ValidatorSigner :: new ( Vec :: new ( ) , Ed25519PrivateKey :: generate_for_testing ( ) ) ) ;
133
- let mempool = Arc :: new ( RwLock :: new ( CoreMempool :: new ( Arc :: new ( MockDB :: default ( ) ) ) ) ) ;
134
-
135
- let mut executor = Executor :: new ( block_executor, signer, mempool) ;
136
-
137
- // Create a sample executable block
138
- let block = ExecutableBlock { block : BlockInfo :: random ( ) , txns : vec ! [ ] } ;
139
-
140
- // Try executing the block when executor is in Idle state
141
- let result = executor. execute_block ( block. clone ( ) ) . await ;
142
- assert ! ( result. is_err( ) ) ;
143
- assert_eq ! ( result. unwrap_err( ) . to_string( ) , "Executor is not in the Commit state" ) ;
144
-
145
- // Set the executor state to Commit
146
- executor. status = ExecutorState :: Commit ;
147
-
148
- // Execute the block
149
- let result = executor. execute_block ( block) . await ;
150
- assert ! ( result. is_ok( ) ) ;
151
-
152
- // Check if the executor state is updated to Idle
153
- assert_eq ! ( executor. status, ExecutorState :: Idle ) ;
147
+ let mut executor = init_executor ( ) ;
148
+ let block_id = HashValue :: random ( ) ;
149
+ let tx = SignatureVerifiedTransaction :: Valid ( Transaction :: UserTransaction (
150
+ create_signed_transaction ( 0 ) ,
151
+ ) ) ;
152
+ let txs = ExecutableTransactions :: Unsharded ( vec ! [ tx] ) ;
153
+ let block = ExecutableBlock :: new ( block_id. clone ( ) , txs) ;
154
+ executor. execute_block ( block) . await . unwrap ( ) ;
154
155
}
155
156
}
0 commit comments