Skip to content

Commit

Permalink
[test] Bitcoin block tester (#2480)
Browse files Browse the repository at this point in the history
* [test] bitcoin block executor

* [cli] Provide util to reverse the hex string

* [types]Unify the InscriptionId with BitcoinInscriptionID

* [test] Improve bitcoin block tester
  • Loading branch information
jolestar authored Aug 22, 2024
1 parent e13f6d3 commit 1158cc0
Show file tree
Hide file tree
Showing 50 changed files with 963 additions and 195 deletions.
3 changes: 3 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions crates/rooch-executor/src/actor/executor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -414,6 +414,11 @@ impl ExecutorActor {
verified_action,
))
}

pub fn refresh_state(&mut self, root: ObjectMeta, is_upgrade: bool) -> Result<()> {
self.root = root;
self.moveos.flush_module_cache(is_upgrade)
}
}

#[async_trait]
Expand Down
4 changes: 3 additions & 1 deletion crates/rooch-framework-tests/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@ musig2 = { workspace = true }
miniscript = { workspace = true }
coerce = { workspace = true }
tokio = { workspace = true }
clap = { features = ["derive", ], workspace = true }

move-core-types = { workspace = true }

moveos-types = { workspace = true }
moveos-store = { workspace = true }
moveos-config = { workspace = true }
Expand All @@ -45,8 +45,10 @@ rooch-executor = { workspace = true }
rooch-config = { workspace = true }
rooch-db = { workspace = true }
rooch-event = { workspace = true }
rooch-relayer = { workspace = true }

bitcoin-move = { workspace = true }
framework-builder = { workspace = true }

[dev-dependencies]
rooch-integration-test-runner = { workspace = true }
Expand Down
8 changes: 8 additions & 0 deletions crates/rooch-framework-tests/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# Rooch framework tests


## How to build a bitcoin block tester genesis?

```bash
cargo run -p rooch-framework-tests -- --btc-rpc-url http://localhost:9332 --btc-rpc-username your_username --btc-rpc-password your_pwd --block-hash 000000000003ba27aa200b1cecaad478d2b00432346c3f1f3986da1afd33e506
```
70 changes: 60 additions & 10 deletions crates/rooch-framework-tests/src/binding_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,15 @@ use move_core_types::vm_status::KeptVMStatus;
use moveos_config::DataDirPath;
use moveos_store::MoveOSStore;
use moveos_types::function_return_value::FunctionResult;
use moveos_types::h256::H256;
use moveos_types::module_binding::MoveFunctionCaller;
use moveos_types::moveos_std::event::Event;
use moveos_types::moveos_std::object::ObjectMeta;
use moveos_types::moveos_std::tx_context::TxContext;
use moveos_types::state_resolver::{RootObjectResolver, StateReaderExt};
use moveos_types::state::{FieldKey, ObjectChange, ObjectState, StateChangeSet};
use moveos_types::state_resolver::{
RootObjectResolver, StateKV, StateReaderExt, StateResolver, StatelessResolver,
};
use moveos_types::transaction::{FunctionCall, VerifiedMoveOSTransaction};
use rooch_config::RoochOpt;
use rooch_db::RoochDB;
Expand Down Expand Up @@ -51,6 +56,7 @@ pub struct RustBindingTest {
root: ObjectMeta,
rooch_db: RoochDB,
pub registry_service: RegistryService,
events: Vec<Event>,
}

impl RustBindingTest {
Expand All @@ -64,20 +70,21 @@ impl RustBindingTest {
}

pub fn new() -> Result<Self> {
let opt = RoochOpt::new_with_temp_store()?;
let store_config = opt.store_config();
let registry_service = metrics::RegistryService::default();
let rooch_db = RoochDB::init(store_config, &registry_service.default_registry())?;

let mut network: RoochNetwork = BuiltinChainID::Local.into();
Self::new_with_network(BuiltinChainID::Local.into())
}

pub fn new_with_network(mut network: RoochNetwork) -> Result<Self> {
let kp = RoochKeyPair::generate_secp256k1();
let sequencer = kp.public().bitcoin_address()?;

network.set_sequencer_account(sequencer.clone());

let genesis = RoochGenesis::load_or_init(network, &rooch_db)?;
let root = genesis.genesis_root().clone();
let genesis = RoochGenesis::build(network.clone())?;
let opt = RoochOpt::new_with_temp_store()?;
let store_config = opt.store_config();
let registry_service = metrics::RegistryService::default();
let rooch_db = RoochDB::init(store_config, &registry_service.default_registry())?;
let root = genesis.init_genesis(&rooch_db)?;

let executor = ExecutorActor::new(
root.clone(),
Expand All @@ -103,6 +110,7 @@ impl RustBindingTest {
reader_executor,
rooch_db,
registry_service,
events: vec![],
})
}

Expand Down Expand Up @@ -205,19 +213,40 @@ impl RustBindingTest {
) -> Result<ExecuteTransactionResult> {
let result = self.executor.execute(tx)?;
self.root = result.transaction_info.root_metadata();

self.events.extend(result.output.events.clone());
self.reader_executor
.refresh_state(self.root.clone(), false)?;
Ok(result)
}

/// Directly apply a change set to the state and update root
pub fn apply_changes(&mut self, changes: Vec<ObjectChange>) -> Result<()> {
let mut change_set = StateChangeSet::new(self.root.state_root(), self.root.size);
for change in changes {
change_set.add_change(change)?;
}
self.rooch_db
.moveos_store
.state_store
.apply_change_set(&mut change_set)?;
self.root = change_set.root_metadata();
self.reader_executor
.refresh_state(self.root.clone(), false)?;
self.executor.refresh_state(self.root.clone(), false)?;
Ok(())
}

pub fn get_account_sequence_number(&self, address: AccountAddress) -> Result<u64> {
Ok(self
.resolver()
.get_account(address)?
.map(|account| account.value.sequence_number)
.unwrap_or(0))
}

pub fn events(&self) -> &Vec<Event> {
&self.events
}
}

impl MoveFunctionCaller for RustBindingTest {
Expand All @@ -234,3 +263,24 @@ impl MoveFunctionCaller for RustBindingTest {
Ok(result)
}
}

impl StateResolver for RustBindingTest {
fn root(&self) -> &ObjectMeta {
&self.root
}
}

impl StatelessResolver for RustBindingTest {
fn get_field_at(&self, state_root: H256, key: &FieldKey) -> Result<Option<ObjectState>> {
self.resolver().get_field_at(state_root, key)
}

fn list_fields_at(
&self,
state_root: H256,
cursor: Option<FieldKey>,
limit: usize,
) -> Result<Vec<StateKV>> {
self.resolver().list_fields_at(state_root, cursor, limit)
}
}
Loading

0 comments on commit 1158cc0

Please sign in to comment.