Skip to content

Commit 01acb24

Browse files
committed
add test helper fns and execute_block case
1 parent fd36683 commit 01acb24

File tree

10 files changed

+2082
-55
lines changed

10 files changed

+2082
-55
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@
22
methods/guest/Cargo.lock
33
.idea/
44
target/
5+
ledger_db/
+56-55
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
use crate::types::Block;
21
use aptos_db::AptosDB;
32
use aptos_executor::block_executor::BlockExecutor;
43
use aptos_executor_types::state_checkpoint_output::StateCheckpointOutput;
@@ -9,8 +8,9 @@ use aptos_types::block_executor::config::BlockExecutorConfigFromOnchain;
98
use aptos_types::block_executor::partitioner::ExecutableBlock;
109
use aptos_types::validator_signer::ValidatorSigner;
1110
use aptos_vm::AptosVM;
12-
use std::sync::{Arc, RwLock};
11+
use std::sync::RwLock;
1312

13+
/// The name that appends the dir path of the rocksdb.
1414
const APTOS_DB_DIR: &str = ".aptosdb-block-executor";
1515

1616
/// The state of `movement-network` execution can exist in three states,
@@ -47,23 +47,23 @@ pub enum ExecutorState {
4747
/// against the `AptosVM`.
4848
pub struct Executor {
4949
/// The executing type.
50-
pub block_executor: Arc<RwLock<BlockExecutor<AptosVM>>>,
50+
pub block_executor: BlockExecutor<AptosVM>,
5151
/// The current state of the executor.
5252
pub status: ExecutorState,
5353
/// The access to db.
5454
pub db: DbReaderWriter,
5555
/// The signer of the executor's transactions.
56-
pub signer: Option<ValidatorSigner>,
56+
pub signer: ValidatorSigner,
5757
/// The access to the core mempool.
58-
pub mempool: Arc<RwLock<CoreMempool>>,
58+
pub mempool: CoreMempool,
5959
}
6060

6161
impl Executor {
6262
/// Create a new `Executor` instance.
6363
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,
6767
) -> Self {
6868
let path = format!("{}/{}", dirs::home_dir().unwrap().to_str().unwrap(), APTOS_DB_DIR);
6969
let (_aptos_db, reader_writer) = DbReaderWriter::wrap(AptosDB::new_for_test(path.as_str()));
@@ -79,10 +79,9 @@ impl Executor {
7979
if self.status != ExecutorState::Commit {
8080
return Err(anyhow::anyhow!("Executor is not in the Commit state"));
8181
}
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();
8483
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(
8685
block,
8786
parent_block_id,
8887
BlockExecutorConfigFromOnchain::new_no_block_limit(),
@@ -98,58 +97,60 @@ impl Executor {
9897
#[cfg(test)]
9998
mod tests {
10099
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,
106111
};
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+
}
110138

111139
#[test]
112140
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();
122142
assert_eq!(executor.status, ExecutorState::Idle);
123-
assert!(executor.signer.is_some());
124143
}
125144

126145
#[tokio::test]
127146
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();
154155
}
155156
}

protocol-units/executor/state_merkle_db/000004.log

Whitespace-only changes.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
MANIFEST-000005
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
38923803-103d-4739-81d3-671f6546930b

protocol-units/executor/state_merkle_db/LOCK

Whitespace-only changes.

0 commit comments

Comments
 (0)