Skip to content

Commit 4543b5a

Browse files
authored
data sim 3/x: make test adapter executor parametric (#14208)
## Description This makes the executor in adapter test runner parametric. Subseq PR, will enable choosing the executor in the test file. Executors include: validator (current approach) and an upcoming simulator ## Test Plan Existing tests --- If your changes are not user-facing and not a breaking change, you can skip the following section. Otherwise, please indicate what changed, and then add to the Release Notes section as highlighted during the release process. ### Type of Change (Check all that apply) - [ ] protocol change - [ ] user-visible impact - [ ] breaking change for a client SDKs - [ ] breaking change for FNs (FN binary must upgrade) - [ ] breaking change for validators or node operators (must upgrade binaries) - [ ] breaking change for on-chain data layout - [ ] necessitate either a data wipe or data migration ### Release notes
1 parent 108ce43 commit 4543b5a

File tree

3 files changed

+174
-28
lines changed

3 files changed

+174
-28
lines changed

crates/sui-transactional-test-runner/src/args.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ use sui_types::base_types::{SequenceNumber, SuiAddress};
1515
use sui_types::move_package::UpgradePolicy;
1616
use sui_types::object::{Object, Owner};
1717
use sui_types::programmable_transaction_builder::ProgrammableTransactionBuilder;
18-
use sui_types::storage::ObjectStore;
1918
use sui_types::transaction::{Argument, CallArg, ObjectArg};
2019

2120
use crate::test_adapter::{FakeID, SuiTestAdapter};
@@ -254,9 +253,9 @@ impl SuiValue {
254253
None => bail!("INVALID TEST. Unknown object, object({})", fake_id),
255254
};
256255
let obj_res = if let Some(v) = version {
257-
test_adapter.validator.database.get_object_by_key(&id, v)
256+
test_adapter.executor.get_object_by_key(&id, v)
258257
} else {
259-
test_adapter.validator.database.get_object(&id)
258+
test_adapter.executor.get_object(&id)
260259
};
261260
let obj = match obj_res {
262261
Ok(Some(obj)) => obj,

crates/sui-transactional-test-runner/src/lib.rs

Lines changed: 154 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,165 @@ pub mod test_adapter;
99

1010
use move_transactional_test_runner::framework::run_test_impl;
1111
use std::path::Path;
12+
use sui_types::storage::ObjectStore;
1213
use test_adapter::{SuiTestAdapter, PRE_COMPILED};
1314

15+
use std::sync::Arc;
16+
use sui_core::authority::authority_test_utils::send_and_confirm_transaction_with_execution_error;
17+
use sui_core::authority::AuthorityState;
18+
use sui_json_rpc_types::DevInspectResults;
19+
use sui_json_rpc_types::EventFilter;
20+
use sui_json_rpc_types::SuiEvent;
21+
use sui_storage::key_value_store::TransactionKeyValueStore;
22+
use sui_types::base_types::ObjectID;
23+
use sui_types::base_types::SuiAddress;
24+
use sui_types::base_types::VersionNumber;
25+
use sui_types::effects::TransactionEffects;
26+
use sui_types::error::ExecutionError;
27+
use sui_types::error::SuiError;
28+
use sui_types::error::SuiResult;
29+
use sui_types::event::EventID;
30+
use sui_types::messages_checkpoint::VerifiedCheckpoint;
31+
use sui_types::object::Object;
32+
use sui_types::transaction::Transaction;
33+
use sui_types::transaction::TransactionDataAPI;
34+
use sui_types::transaction::TransactionKind;
35+
1436
#[cfg_attr(not(msim), tokio::main)]
1537
#[cfg_attr(msim, msim::main)]
1638
pub async fn run_test(path: &Path) -> Result<(), Box<dyn std::error::Error>> {
1739
run_test_impl::<SuiTestAdapter>(path, Some(&*PRE_COMPILED)).await?;
1840
Ok(())
1941
}
42+
43+
pub struct ValidatorWithFullnode {
44+
pub validator: Arc<AuthorityState>,
45+
pub fullnode: Arc<AuthorityState>,
46+
pub kv_store: Arc<TransactionKeyValueStore>,
47+
}
48+
49+
#[allow(unused_variables)]
50+
/// TODO: better name?
51+
#[async_trait::async_trait]
52+
pub trait TransactionalAdapter: Send + Sync + ObjectStore {
53+
async fn execute_txn(
54+
&mut self,
55+
transaction: Transaction,
56+
) -> anyhow::Result<(TransactionEffects, Option<ExecutionError>)>;
57+
58+
async fn create_checkpoint(&mut self) -> anyhow::Result<VerifiedCheckpoint>;
59+
60+
async fn advance_clock(
61+
&mut self,
62+
duration: std::time::Duration,
63+
) -> anyhow::Result<TransactionEffects>;
64+
65+
async fn advance_epoch(&mut self) -> anyhow::Result<()>;
66+
67+
async fn request_gas(
68+
&mut self,
69+
address: SuiAddress,
70+
amount: u64,
71+
) -> anyhow::Result<TransactionEffects>;
72+
73+
async fn dev_inspect_transaction_block(
74+
&self,
75+
sender: SuiAddress,
76+
transaction_kind: TransactionKind,
77+
gas_price: Option<u64>,
78+
) -> SuiResult<DevInspectResults>;
79+
80+
async fn query_events(
81+
&self,
82+
query: EventFilter,
83+
// If `Some`, the query will start from the next item after the specified cursor
84+
cursor: Option<EventID>,
85+
limit: usize,
86+
descending: bool,
87+
) -> SuiResult<Vec<SuiEvent>>;
88+
}
89+
90+
#[async_trait::async_trait]
91+
impl TransactionalAdapter for ValidatorWithFullnode {
92+
async fn execute_txn(
93+
&mut self,
94+
transaction: Transaction,
95+
) -> anyhow::Result<(TransactionEffects, Option<ExecutionError>)> {
96+
let with_shared = transaction
97+
.data()
98+
.intent_message()
99+
.value
100+
.contains_shared_object();
101+
let (_, effects, execution_error) = send_and_confirm_transaction_with_execution_error(
102+
&self.validator,
103+
Some(&self.fullnode),
104+
transaction,
105+
with_shared,
106+
)
107+
.await?;
108+
Ok((effects.into_data(), execution_error))
109+
}
110+
111+
async fn dev_inspect_transaction_block(
112+
&self,
113+
sender: SuiAddress,
114+
transaction_kind: TransactionKind,
115+
gas_price: Option<u64>,
116+
) -> SuiResult<DevInspectResults> {
117+
self.fullnode
118+
.dev_inspect_transaction_block(sender, transaction_kind, gas_price)
119+
.await
120+
}
121+
122+
async fn query_events(
123+
&self,
124+
query: EventFilter,
125+
// If `Some`, the query will start from the next item after the specified cursor
126+
cursor: Option<EventID>,
127+
limit: usize,
128+
descending: bool,
129+
) -> SuiResult<Vec<SuiEvent>> {
130+
self.validator
131+
.query_events(&self.kv_store, query, cursor, limit, descending)
132+
.await
133+
}
134+
135+
async fn create_checkpoint(&mut self) -> anyhow::Result<VerifiedCheckpoint> {
136+
unimplemented!("create_checkpoint not supported")
137+
}
138+
139+
async fn advance_clock(
140+
&mut self,
141+
_duration: std::time::Duration,
142+
) -> anyhow::Result<TransactionEffects> {
143+
unimplemented!("advance_clock not supported")
144+
}
145+
146+
async fn advance_epoch(&mut self) -> anyhow::Result<()> {
147+
unimplemented!("advance_epoch not supported")
148+
}
149+
150+
async fn request_gas(
151+
&mut self,
152+
_address: SuiAddress,
153+
_amount: u64,
154+
) -> anyhow::Result<TransactionEffects> {
155+
unimplemented!("request_gas not supported")
156+
}
157+
}
158+
159+
impl ObjectStore for ValidatorWithFullnode {
160+
fn get_object(&self, object_id: &ObjectID) -> Result<Option<Object>, SuiError> {
161+
self.validator.database.get_object(object_id)
162+
}
163+
164+
fn get_object_by_key(
165+
&self,
166+
object_id: &ObjectID,
167+
version: VersionNumber,
168+
) -> Result<Option<Object>, SuiError> {
169+
self.validator
170+
.database
171+
.get_object_by_key(object_id, version)
172+
}
173+
}

crates/sui-transactional-test-runner/src/test_adapter.rs

Lines changed: 18 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
//! This module contains the transactional test runner instantiation for the Sui adapter
55
66
use crate::{args::*, programmable_transaction_test_parser::parser::ParsedCommand};
7+
use crate::{TransactionalAdapter, ValidatorWithFullnode};
78
use anyhow::{anyhow, bail};
89
use async_trait::async_trait;
910
use bimap::btree::BiBTreeMap;
@@ -38,10 +39,7 @@ use std::{
3839
path::Path,
3940
sync::Arc,
4041
};
41-
use sui_core::authority::{
42-
authority_test_utils::send_and_confirm_transaction_with_execution_error,
43-
test_authority_builder::TestAuthorityBuilder, AuthorityState,
44-
};
42+
use sui_core::authority::test_authority_builder::TestAuthorityBuilder;
4543
use sui_framework::BuiltInFramework;
4644
use sui_framework::DEFAULT_FRAMEWORK_PATH;
4745
use sui_json_rpc::api::QUERY_MAX_RESULT_LIMIT;
@@ -52,11 +50,13 @@ use sui_protocol_config::{Chain, ProtocolConfig, ProtocolVersion};
5250
use sui_storage::{
5351
key_value_store::TransactionKeyValueStore, key_value_store_metrics::KeyValueStoreMetrics,
5452
};
53+
use sui_types::base_types::SequenceNumber;
54+
use sui_types::crypto::get_authority_key_pair;
55+
use sui_types::effects::TransactionEffectsAPI;
5556
use sui_types::transaction::Command;
5657
use sui_types::transaction::ProgrammableTransaction;
5758
use sui_types::DEEPBOOK_PACKAGE_ID;
5859
use sui_types::MOVE_STDLIB_PACKAGE_ID;
59-
use sui_types::{base_types::SequenceNumber, effects::TransactionEffectsAPI};
6060
use sui_types::{
6161
base_types::{ObjectID, ObjectRef, SuiAddress, TransactionDigest, SUI_ADDRESS_LENGTH},
6262
crypto::{get_key_pair_from_rng, AccountKeyPair},
@@ -68,7 +68,6 @@ use sui_types::{
6868
SUI_FRAMEWORK_ADDRESS, SUI_SYSTEM_STATE_OBJECT_ID,
6969
};
7070
use sui_types::{clock::Clock, SUI_SYSTEM_ADDRESS};
71-
use sui_types::{crypto::get_authority_key_pair, storage::ObjectStore};
7271
use sui_types::{execution_status::ExecutionStatus, transaction::TransactionKind};
7372
use sui_types::{gas::GasCostSummary, object::GAS_VALUE_FOR_TESTING};
7473
use sui_types::{id::UID, DEEPBOOK_ADDRESS};
@@ -106,9 +105,6 @@ const DEFAULT_GAS_BUDGET: u64 = 5_000_000_000;
106105
const GAS_FOR_TESTING: u64 = GAS_VALUE_FOR_TESTING;
107106

108107
pub struct SuiTestAdapter<'a> {
109-
pub(crate) validator: Arc<AuthorityState>,
110-
pub(crate) kv_store: Arc<TransactionKeyValueStore>,
111-
pub(crate) fullnode: Arc<AuthorityState>,
112108
pub(crate) compiled_state: CompiledState<'a>,
113109
/// For upgrades: maps an upgraded package name to the original package name.
114110
package_upgrade_mapping: BTreeMap<Symbol, Symbol>,
@@ -119,6 +115,7 @@ pub struct SuiTestAdapter<'a> {
119115
next_fake: (u64, u64),
120116
gas_price: u64,
121117
pub(crate) staged_modules: BTreeMap<Symbol, StagedPackage>,
118+
pub(crate) executor: Box<dyn TransactionalAdapter>,
122119
}
123120

124121
pub(crate) struct StagedPackage {
@@ -356,9 +353,11 @@ impl<'a> MoveTestAdapter<'a> for SuiTestAdapter<'a> {
356353
));
357354

358355
let mut test_adapter = Self {
359-
validator,
360-
kv_store,
361-
fullnode,
356+
executor: Box::new(ValidatorWithFullnode {
357+
validator,
358+
fullnode,
359+
kv_store,
360+
}),
362361
compiled_state: CompiledState::new(
363362
named_address_mapping,
364363
pre_compiled_deps,
@@ -1106,13 +1105,8 @@ impl<'a> SuiTestAdapter<'a> {
11061105
.intent_message()
11071106
.value
11081107
.contains_shared_object();
1109-
let (txn, effects, error_opt) = send_and_confirm_transaction_with_execution_error(
1110-
&self.validator,
1111-
Some(&self.fullnode),
1112-
transaction,
1113-
with_shared,
1114-
)
1115-
.await?;
1108+
let (effects, error_opt) = self.executor.execute_txn(transaction).await?;
1109+
let digest = effects.transaction_digest();
11161110
let mut created_ids: Vec<_> = effects
11171111
.created()
11181112
.iter()
@@ -1164,10 +1158,9 @@ impl<'a> SuiTestAdapter<'a> {
11641158
match effects.status() {
11651159
ExecutionStatus::Success { .. } => {
11661160
let events = self
1167-
.validator
1161+
.executor
11681162
.query_events(
1169-
&self.kv_store,
1170-
EventFilter::Transaction(*txn.digest()),
1163+
EventFilter::Transaction(*digest),
11711164
None,
11721165
*QUERY_MAX_RESULT_LIMIT,
11731166
/* descending */ false,
@@ -1211,7 +1204,7 @@ impl<'a> SuiTestAdapter<'a> {
12111204
gas_price: Option<u64>,
12121205
) -> anyhow::Result<TxnSummary> {
12131206
let results = self
1214-
.fullnode
1207+
.executor
12151208
.dev_inspect_transaction_block(sender, transaction_kind, gas_price)
12161209
.await?;
12171210
let DevInspectResults {
@@ -1279,9 +1272,9 @@ impl<'a> SuiTestAdapter<'a> {
12791272

12801273
fn get_object(&self, id: &ObjectID, version: Option<SequenceNumber>) -> anyhow::Result<Object> {
12811274
let obj_res = if let Some(v) = version {
1282-
self.validator.database.get_object_by_key(id, v)
1275+
self.executor.get_object_by_key(id, v)
12831276
} else {
1284-
self.validator.database.get_object(id)
1277+
self.executor.get_object(id)
12851278
};
12861279
match obj_res {
12871280
Ok(Some(obj)) => Ok(obj),

0 commit comments

Comments
 (0)