Skip to content

Commit

Permalink
[bitcoin-move] Add more ord tests (#2466)
Browse files Browse the repository at this point in the history
* [bitcoin-move] Ord tests

* fixup
  • Loading branch information
jolestar authored Aug 20, 2024
1 parent 8307502 commit 582324c
Show file tree
Hide file tree
Showing 10 changed files with 9,810 additions and 47 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 @@ -17,6 +17,7 @@ rust-version = { workspace = true }
anyhow = { workspace = true }
bitcoin = { workspace = true }
ethers = { workspace = true }
serde = { workspace = true }
serde_json = { workspace = true }
hex = { workspace = true }
bcs = { workspace = true }
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
020000000001013aa223b77dccee5af627b1895ce5b2a352e482ae131b664ff0f2685d01e618df0700000000fffffffd012202000000000000160014eb501bd60d2d1dc04a30a75d0ccd1e5ca42a758f0340dc173f55f63045cdc9f36ca578a16b8a712d2ed4cae1eb1ad813d1aa8ef2fb1b2f4776af686c5c7caa835f401a224709e55a4f15c105909ffb5dceaab99d3e718020c8a3d49a332c37d564d94b93d23f7df03eff030e8e2ac03912d963ad0bbf76c9ac0063036f7264010118746578742f706c61696e3b636861727365743d7574662d38003a7b2270223a226272632d3230222c226f70223a226d696e74222c227469636b223a2273617473222c22616d74223a22313030303030303030227d6821c0c8a3d49a332c37d564d94b93d23f7df03eff030e8e2ac03912d963ad0bbf76c900000000
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
02000000000101c5d8fc62b7512401b6fc31911dbb763a473d6e1cfd63966f348d617c2f7b721c0100000000fffffffd02260100000000000016001480f177474e4e9caba5eb4d58f6d071264401d072ab6919020000000022512074a7bcc1ed5fc5b28680a838ccf8e745fd8afdcb171a95e4e1bcf0100792c3e103408160d443c29618f76a7498f952c8b42309489566f482cd9564796720056d948b86b84bcf44c1068ca6146a611a592f3f6799b55dd4682859b992d6a11d48d28d452088225c0158a85208c9f0a93d3b724953f164a056121b81a87f88ab0a666cbff1ac0063036f726401010a746578742f706c61696e000d3832303936302e6269746d61706821c188225c0158a85208c9f0a93d3b724953f164a056121b81a87f88ab0a666cbff100000000

Large diffs are not rendered by default.

126 changes: 126 additions & 0 deletions crates/rooch-framework-tests/src/tests/bitcoin_data.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
// Copyright (c) RoochNetwork
// SPDX-License-Identifier: Apache-2.0

use bitcoin::{
absolute::LockTime, consensus::deserialize, hex::FromHex, transaction::Version, Amount, Block,
Sequence, Transaction, Txid, Witness,
};
use include_dir::{include_dir, Dir};
use rooch_types::bitcoin::network::Network;
use serde::{Deserialize, Serialize};
use std::path::PathBuf;

// Download the bitcoin block via the following command:
// curl -sSL "https://mempool.space/api/block/00000000000af0aed4792b1acee3d966af36cf5def14935db8de83d6f9306f2f/raw" > crates/rooch-framework-tests/blocks/bitcoin/91812.blob
// curl -sSL "https://mempool.space/api/block/00000000000a4d0a398161ffc163c503763b1f4360639393e0e4c8e300e0caec/raw" > crates/rooch-framework-tests/blocks/bitcoin/91842.blob
// curl -sSL "https://mempool.space/api/block/000000000000000000020750f322f4e72e99c2f0b9738fb4f46607860bd18c13/raw" > crates/rooch-framework-tests/blocks/bitcoin/818677.blob
// curl -sSL "https://mempool.space/testnet/api/block/0000000016412abe1778a347da773ff8bc087ad1a91ae5daad349bc268285c2d/raw" > crates/rooch-framework-tests/blocks/testnet/2821527.blob
pub(crate) const STATIC_BLOCK_DIR: Dir = include_dir!("blocks");

pub(crate) fn load_block(network: Network, height: u64) -> Block {
let block_file = PathBuf::from(network.to_string()).join(format!("{}.blob", height));
let btc_block_bytes = STATIC_BLOCK_DIR
.get_file(block_file.as_path())
.unwrap()
.contents();
let block: Block = deserialize(btc_block_bytes).unwrap();
block
}

pub(crate) fn load_tx(network: Network, txid: &str) -> Transaction {
let tx_file = PathBuf::from(network.to_string()).join(format!("{}.txt", txid));
let btc_tx_hex = STATIC_BLOCK_DIR
.get_file(tx_file.as_path())
.unwrap()
.contents_utf8()
.unwrap();
let tx: Transaction = bitcoin_tx_from_hex(btc_tx_hex);
tx
}

pub(crate) fn load_tx_info(network: Network, txid: &str) -> TxInfo {
let tx_file = PathBuf::from(network.to_string()).join(format!("{}.json", txid));
let btc_tx_json = STATIC_BLOCK_DIR
.get_file(tx_file.as_path())
.unwrap()
.contents_utf8()
.unwrap();
let tx: TxInfo = serde_json::from_str(btc_tx_json).unwrap();
tx
}

pub(crate) fn bitcoin_tx_from_hex(hex: &str) -> Transaction {
let btc_tx_bytes = Vec::from_hex(hex).unwrap();
let btc_tx: Transaction = deserialize(btc_tx_bytes.as_slice()).unwrap();
btc_tx
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct OutputInfo {
pub scriptpubkey: String,
pub scriptpubkey_asm: String,
pub scriptpubkey_type: String,
pub scriptpubkey_address: String,
pub value: Amount,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TxInputInfo {
pub txid: Txid,
pub vout: u32,
pub prevout: OutputInfo,
pub scriptsig: String,
pub scriptsig_asm: String,
pub witness: Vec<String>,
pub is_coinbase: bool,
pub sequence: u32,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TxInfo {
pub txid: Txid,
pub version: i32,
pub locktime: u32,
pub vin: Vec<TxInputInfo>,
pub vout: Vec<OutputInfo>,
}

impl From<TxInfo> for Transaction {
fn from(tx_info: TxInfo) -> Self {
let mut tx = Transaction {
version: Version::non_standard(tx_info.version),
lock_time: LockTime::from_consensus(tx_info.locktime),
input: vec![],
output: vec![],
};
for input in tx_info.vin {
let txid = input.txid;
let vout = input.vout;

let script_sig = input.scriptsig;
let sequence = input.sequence;
let witness = input
.witness
.into_iter()
.map(|w| Vec::from_hex(&w).unwrap())
.collect::<Vec<Vec<u8>>>();
let tx_input = bitcoin::TxIn {
previous_output: bitcoin::OutPoint { txid, vout },
script_sig: bitcoin::ScriptBuf::from_hex(&script_sig).unwrap(),
sequence: Sequence::from_consensus(sequence),
witness: Witness::from_slice(&witness),
};
tx.input.push(tx_input);
}
for output in tx_info.vout {
let script_pubkey = output.scriptpubkey;
let value = output.value;
let tx_output = bitcoin::TxOut {
script_pubkey: bitcoin::ScriptBuf::from_hex(&script_pubkey).unwrap(),
value,
};
tx.output.push(tx_output);
}
tx
}
}
19 changes: 1 addition & 18 deletions crates/rooch-framework-tests/src/tests/bitcoin_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
// SPDX-License-Identifier: Apache-2.0

use crate::binding_test;
use crate::tests::bitcoin_data::load_block;
use bitcoin::consensus::deserialize;
use bitcoin::hashes::Hash;
use bitcoin::{Block, OutPoint, Transaction, TxOut};
use hex::FromHex;
use include_dir::{include_dir, Dir};
use moveos_types::access_path::AccessPath;
use moveos_types::module_binding::MoveFunctionCaller;
use moveos_types::state_resolver::StateReader;
Expand All @@ -19,7 +19,6 @@ use rooch_types::into_address::IntoAddress;
use rooch_types::multichain_id::RoochMultiChainID;
use rooch_types::transaction::L1BlockWithBody;
use std::collections::HashMap;
use std::path::PathBuf;
use tracing::{debug, info};

#[tokio::test]
Expand Down Expand Up @@ -207,19 +206,3 @@ async fn test_real_bocks() {
test_block_process(blocks);
}
}
// Download the bitcoin block via the following command:
// curl -sSL "https://mempool.space/api/block/00000000000af0aed4792b1acee3d966af36cf5def14935db8de83d6f9306f2f/raw" > crates/rooch-framework-tests/blocks/bitcoin/91812.blob
// curl -sSL "https://mempool.space/api/block/00000000000a4d0a398161ffc163c503763b1f4360639393e0e4c8e300e0caec/raw" > crates/rooch-framework-tests/blocks/bitcoin/91842.blob
// curl -sSL "https://mempool.space/api/block/000000000000000000020750f322f4e72e99c2f0b9738fb4f46607860bd18c13/raw" > crates/rooch-framework-tests/blocks/bitcoin/818677.blob
// curl -sSL "https://mempool.space/testnet/api/block/0000000016412abe1778a347da773ff8bc087ad1a91ae5daad349bc268285c2d/raw" > crates/rooch-framework-tests/blocks/testnet/2821527.blob
pub(crate) const STATIC_BLOCK_DIR: Dir = include_dir!("blocks");

fn load_block(network: Network, height: u64) -> Block {
let block_file = PathBuf::from(network.to_string()).join(format!("{}.blob", height));
let btc_block_bytes = STATIC_BLOCK_DIR
.get_file(block_file.as_path())
.unwrap()
.contents();
let block: Block = deserialize(btc_block_bytes).unwrap();
block
}
1 change: 1 addition & 0 deletions crates/rooch-framework-tests/src/tests/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Copyright (c) RoochNetwork
// SPDX-License-Identifier: Apache-2.0

mod bitcoin_data;
mod bitcoin_multisign_validator_tests;
mod bitcoin_test;
mod bitcoin_validator_tests;
Expand Down
Loading

0 comments on commit 582324c

Please sign in to comment.