Skip to content

Commit

Permalink
[bitcoin-move] Test Babylon Stake with Bitcoin Block Tester (#2766)
Browse files Browse the repository at this point in the history
  • Loading branch information
jolestar authored Oct 15, 2024
1 parent 61338c5 commit af1a006
Show file tree
Hide file tree
Showing 14 changed files with 784 additions and 191 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions crates/rooch-framework-tests/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ coerce = { workspace = true }
tokio = { workspace = true }
clap = { features = ["derive", ], workspace = true }
rand = { workspace = true }
csv = { workspace = true }

move-core-types = { workspace = true }
moveos-types = { workspace = true }
Expand Down
61 changes: 61 additions & 0 deletions crates/rooch-framework-tests/src/bbn_tx_loader.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
// Copyright (c) RoochNetwork
// SPDX-License-Identifier: Apache-2.0

use std::{path::Path, str::FromStr};

use anyhow::Result;
use bitcoin::Txid;

// Load the babylon staking transactions exported file

// https://github.com/babylonlabs-io/staking-indexer

// Transaction Hash,Staking Output Index,Inclusion Height,Staker Public Key,Staking Time,Finality Provider Public Key,Is Overflow,Staking Value
// 8440304144a4585d80b60888ba58944f3c626d5c2a813b8955052b2daac20b00,0,864791,04bd117663e6970dad57769a9105bf72f8f7ec162b8e44bf597f41babe5cf8a3,64000,fc8a5b9930c3383e94bd940890e93cfcf95b2571ad50df8063b7011f120b918a,true,4800000
// ffaae2983630d3d51fac15180e2f89c1ae237e3648e11c5ec506113e78216e00,0,864791,3f1713f12f5ce2269c3360454fd552c77994f287f006b8f7e4c215b5f57a47ed,64000,db9160428e401753dc1a9952ffd4fa3386c7609cf8411d2b6d79c42323ca9923,true,1345800
// a1fa47d149457a994d2199ceffc43793eb18287864a6b7314c14ba3649f07000,0,864791,ef548602c263dc77b3c75ebb82edae9f1f57c16b6551c40179e9eb942b454be6,64000,742f1eb3c7fdbd327fa44fcdddf17645d9c6b1287ea97463e046508234fa7537,true,600000
// 7487946cb0598179b805ce73575bb22f99b2ca49d213bf57047ea864dc2f7800,0,864791,c749e4aa8436dc738373f1ccc9570ce9fe8a1d70bae1c25dac71f8e6e0c699ed,64000,0f5c19935a08f661a1c4dfeb5e51ce7f0cfcf4d2eeb405fe4c7d7bd668fc85e4,true,564500

#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
pub struct BBNStakingTxRecord {
pub transaction_hash: String,
pub staking_output_index: u32,
pub inclusion_height: u64,
pub staker_public_key: String,
pub staking_time: u16,
pub finality_provider_public_key: String,
pub is_overflow: bool,
pub staking_value: u64,
}
impl BBNStakingTxRecord {
pub fn load_bbn_staking_txs<P: AsRef<Path>>(
file_path: P,
block_height: u64,
) -> Result<Vec<BBNStakingTxRecord>> {
let mut rdr = csv::ReaderBuilder::new()
.has_headers(true)
.from_path(file_path.as_ref())?;

let mut txs = vec![];
for result in rdr.records() {
let record = result?;
let tx: BBNStakingTxRecord = record.deserialize(None)?;
if tx.inclusion_height == block_height {
txs.push(tx);
}
}
Ok(txs)
}

pub fn txid(&self) -> Txid {
Txid::from_str(&self.transaction_hash).unwrap()
}

pub fn staker_public_key(&self) -> Vec<u8> {
hex::decode(&self.staker_public_key).unwrap()
}

pub fn finality_provider_public_key(&self) -> Vec<u8> {
hex::decode(&self.finality_provider_public_key).unwrap()
}
}
60 changes: 57 additions & 3 deletions crates/rooch-framework-tests/src/binding_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,29 +4,36 @@
use anyhow::{bail, Result};
use metrics::RegistryService;
use move_core_types::account_address::AccountAddress;
use move_core_types::u256::U256;
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::gas_schedule::GasScheduleConfig;
use moveos_types::moveos_std::object::ObjectMeta;
use moveos_types::moveos_std::tx_context::TxContext;
use moveos_types::state::{FieldKey, ObjectChange, ObjectState, StateChangeSet};
use moveos_types::state::{FieldKey, MoveStructType, ObjectChange, ObjectState, StateChangeSet};
use moveos_types::state_resolver::{
RootObjectResolver, StateKV, StateReaderExt, StateResolver, StatelessResolver,
};
use moveos_types::transaction::{FunctionCall, VerifiedMoveOSTransaction};
use moveos_types::transaction::{FunctionCall, MoveAction, VerifiedMoveOSTransaction};
use rooch_config::RoochOpt;
use rooch_db::RoochDB;
use rooch_executor::actor::reader_executor::ReaderExecutorActor;
use rooch_executor::actor::{executor::ExecutorActor, messages::ExecuteTransactionResult};
use rooch_genesis::RoochGenesis;
use rooch_types::address::BitcoinAddress;
use rooch_types::crypto::RoochKeyPair;
use rooch_types::framework::gas_coin::RGas;
use rooch_types::framework::transfer::TransferModule;
use rooch_types::rooch_network::{BuiltinChainID, RoochNetwork};
use rooch_types::transaction::{L1BlockWithBody, L1Transaction, RoochTransaction};
use rooch_types::transaction::authenticator::BitcoinAuthenticator;
use rooch_types::transaction::{
Authenticator, L1BlockWithBody, L1Transaction, RoochTransaction, RoochTransactionData,
};
use std::collections::VecDeque;
use std::path::Path;
use std::sync::Arc;
Expand All @@ -47,6 +54,7 @@ pub fn get_data_dir() -> DataDirPath {
}

pub struct RustBindingTest {
network: RoochNetwork,
//we keep the opt to ensure the temp dir is not be deleted before the test end
opt: RoochOpt,
pub sequencer: AccountAddress,
Expand Down Expand Up @@ -102,6 +110,7 @@ impl RustBindingTest {
None,
)?;
Ok(Self {
network,
opt,
root,
sequencer: sequencer.to_rooch_address().into(),
Expand Down Expand Up @@ -139,6 +148,33 @@ impl RustBindingTest {
&self.rooch_db
}

pub fn get_rgas(&mut self, addr: AccountAddress, amount: U256) -> Result<()> {
// transfer RGas from rooch dao account to addr
let function_call =
TransferModule::create_transfer_coin_action(RGas::struct_tag(), addr, amount);
let sender = self
.network
.genesis_config
.rooch_dao
.multisign_bitcoin_address
.to_rooch_address();
let sequence_number = self.get_account_sequence_number(sender.into())?;
let tx_data = RoochTransactionData::new(
sender,
sequence_number,
self.network.chain_id.id,
GasScheduleConfig::CLI_DEFAULT_MAX_GAS_AMOUNT,
function_call,
);
//RoochDao is a multisign account, so we need to sign the tx with the multisign account
//In test env, it is a 1-of-1 multisign account, so we can sign with the only key
let first_signature = BitcoinAuthenticator::sign(&self.kp, &tx_data);
let authenticator = Authenticator::bitcoin_multisign(vec![first_signature])?;
let tx = RoochTransaction::new(tx_data, authenticator);
self.execute(tx)?;
Ok(())
}

//TODO let the module bundle to execute the function
pub fn execute(&mut self, tx: RoochTransaction) -> Result<ExecuteTransactionResult> {
let execute_result = self.execute_as_result(tx)?;
Expand All @@ -151,6 +187,24 @@ impl RustBindingTest {
Ok(execute_result)
}

pub fn execute_function_call_via_sequencer(
&mut self,
function_call: FunctionCall,
) -> Result<ExecuteTransactionResult> {
let action = MoveAction::Function(function_call);
let sequence_number = self.get_account_sequence_number(self.sequencer)?;
let tx_data = RoochTransactionData::new(
self.sequencer.into(),
sequence_number,
self.network.chain_id.id,
GasScheduleConfig::CLI_DEFAULT_MAX_GAS_AMOUNT,
action,
);
let tx = tx_data.sign(&self.kp);
let result = self.execute_as_result(tx)?;
Ok(result)
}

pub fn execute_l1_block_and_tx(
&mut self,
l1_block: L1BlockWithBody,
Expand Down
Loading

0 comments on commit af1a006

Please sign in to comment.