Skip to content

Commit

Permalink
[bitcoin-move] Refacor ord::from_transation make it is a readonly fun…
Browse files Browse the repository at this point in the history
…ction for testing. (#2459)
  • Loading branch information
jolestar authored Aug 19, 2024
1 parent eab2971 commit 8307502
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 43 deletions.
74 changes: 45 additions & 29 deletions crates/rooch-framework-tests/src/tests/ord_test.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,18 @@
// Copyright (c) RoochNetwork
// SPDX-License-Identifier: Apache-2.0

use crate::binding_test;
use bitcoin::consensus::Decodable;
use hex::FromHex;
use moveos_types::module_binding::MoveFunctionCaller;
use tracing::debug;

fn decode_inscription(btx_tx_hex: &str) {
fn decode_inscription(
btx_tx_hex: &str,
input_utxo_values: Vec<u64>,
next_inscription_number: u32,
next_sequence_number: u32,
) {
let btc_tx_bytes = Vec::from_hex(btx_tx_hex).unwrap();
let btc_tx: bitcoin::Transaction =
Decodable::consensus_decode(&mut btc_tx_bytes.as_slice()).unwrap();
Expand All @@ -21,55 +28,64 @@ fn decode_inscription(btx_tx_hex: &str) {
output.script_pubkey.p2wpkh_script_code()
);
}
let _inscriptions = bitcoin_move::natives::ord::from_transaction(&btc_tx);
let inscriptions = bitcoin_move::natives::ord::from_transaction(&btc_tx);

//TODO fixme the from_transaction function is not a read only function now.
//let binding_test = binding_test::RustBindingTest::new().unwrap();
// let ord_module = binding_test.as_module_binding::<rooch_types::bitcoin::ord::OrdModule>();
// let move_btc_tx: rooch_types::bitcoin::types::Transaction =
// rooch_types::bitcoin::types::Transaction::from(btc_tx);
let binding_test = binding_test::RustBindingTest::new().unwrap();
let ord_module = binding_test.as_module_binding::<rooch_types::bitcoin::ord::OrdModule>();
let move_btc_tx: rooch_types::bitcoin::types::Transaction =
rooch_types::bitcoin::types::Transaction::from(btc_tx);

// let inscriptions_from_move = ord_module.from_transaction(&move_btc_tx).unwrap();
let inscriptions_from_move = ord_module
.from_transaction(
&move_btc_tx,
input_utxo_values,
next_inscription_number,
next_sequence_number,
)
.unwrap();

// for (i, (inscription, inscription_from_move)) in inscriptions
// .into_iter()
// .zip(inscriptions_from_move)
// .enumerate()
// {
// debug!("{}. inscription: {:?}", i, inscription);
// assert_eq!(
// inscription.payload.body.unwrap_or_default(),
// inscription_from_move.body
// );
// }
for (i, (inscription, inscription_from_move)) in inscriptions
.into_iter()
.zip(inscriptions_from_move)
.enumerate()
{
debug!(
"{}. inscription: {:?}, inscription_from_move:{:?}",
i, inscription, inscription_from_move
);
assert_eq!(
inscription.payload.body.unwrap_or_default(),
inscription_from_move.body
);
}
}

#[test]
fn test_from_transaction() {
#[tokio::test]
async fn test_from_transaction() {
let _ = tracing_subscriber::fmt::try_init();

//https://mempool.space/api/tx/69d52ccb5eb80372b7fc6c4fc3feb17038dd2f58313c5d16302d70f7ef0fff7f/hex
let btc_tx_hex = "02000000000101c5d8fc62b7512401b6fc31911dbb763a473d6e1cfd63966f348d617c2f7b721c0100000000fffffffd02260100000000000016001480f177474e4e9caba5eb4d58f6d071264401d072ab6919020000000022512074a7bcc1ed5fc5b28680a838ccf8e745fd8afdcb171a95e4e1bcf0100792c3e103408160d443c29618f76a7498f952c8b42309489566f482cd9564796720056d948b86b84bcf44c1068ca6146a611a592f3f6799b55dd4682859b992d6a11d48d28d452088225c0158a85208c9f0a93d3b724953f164a056121b81a87f88ab0a666cbff1ac0063036f726401010a746578742f706c61696e000d3832303936302e6269746d61706821c188225c0158a85208c9f0a93d3b724953f164a056121b81a87f88ab0a666cbff100000000";
decode_inscription(btc_tx_hex);
decode_inscription(btc_tx_hex, vec![], 0, 0);
}

//RUST_LOG=debug cargo test test_from_tx -- --nocapture
#[test]
fn test_from_tx() {
#[tokio::test]
async fn test_from_tx() {
let _ = tracing_subscriber::fmt::try_init();

//https://mempool.space/api/tx/69d52ccb5eb80372b7fc6c4fc3feb17038dd2f58313c5d16302d70f7ef0fff7f/hex
let btc_tx_hex = "02000000000108c3e10bb05fbe2a616ed68f0e90f3785d003a278544eabcd89c87cb34b67e91a70000000000fdffffff3aa924735200623c20fb144891134e90e6c53da9d58fc5a1c52b3ecab41b134a0000000000fdffffff7475995bd9675266bf790621ca0420c49bf5a2cfec4e73f0aedce605a0c7d9b30d00000000fdffffff7793ffc373d0a20765acdfe6ebc527c5476d71c7ac2cbff9b37fe68870be49340000000000fdffffff7db8e570d8190dd79c0caefd37109308c45d355be8e6641741b905ac1944b3c00000000000fdffffff5b016423949dd88656575639d7e687a26b94397e17ffc1dd226dbb5e81d6d2130000000000c4020000b620af411320278a3d6e854b7dfc49ad7e65ed379e2081f749dfd3bae8b83d9a0000000000fdffffff7793ffc373d0a20765acdfe6ebc527c5476d71c7ac2cbff9b37fe68870be49340100000000fdffffff6f6aea00000000000017a91466cb521c26e8261284a9370f1b8fd192aed2c4e1878f0201000000000017a914da9886b43e6f2e8498c9d4a52da5d739c7ced4058700dc00000000000022512059c8a819efee585fb5a99c7437ea4e8d0d1ab8b7dbcc808ca5340ad40743f67195eb00000000000017a914f7b14923e7bd32dceb313b4bafb6d088d701a72987da080100000000002251208d34c01d98b9975ba64ff6e7ef80191c6cca14b8fbf87b868a8e9157967fd3ba0af800000000000017a9147a3005627731e481b6fe77ab89d6d69dbb75399987d2ea000000000000160014321565d58e863f528c53fcc655b948dffa1ac29d4ff700000000000017a914da9886b43e6f2e8498c9d4a52da5d739c7ced405876eff000000000000225120e2161fdf3f80fd03394d451f347bf5d6f983be87ab349e08b2b1d856cc8104d9102700000000000022512070d46f93e88186f6a277bd36c1e98e6684d9e9f244df437ebae37e201338c2a6720401000000000017a9145891991b9fb61b2cddfd8381fb7f579758a4c320878001010000000000225120e2161fdf3f80fd03394d451f347bf5d6f983be87ab349e08b2b1d856cc8104d9610a01000000000022512091cbd6674d682db52fe0efa4b1451c844a26d7ea2c6715e6ea4a6b74c86a208d24ec00000000000017a914076c270aa4adfdd87976968a0f286c97e974b8e587b80001000000000022512097461ccc9766518e9b8290be0aae51c54d22ffdaf996641ed9fbaaa078d5695170f50000000000002251204cf5322e4ff2118bd0d3b0b7fc6f04c0f0f334579f38df29c5bd07c17b9e1d86f0f40000000000002251207cb864badadab824fda368055de23853eb545609bbe8c599ee9315456432381ab800010000000000225120f7739b2a67920f69c8d5cf0512bd5fd6bf02955e1dca9d9eab0cf1c204a8606ece0201000000000017a9142b6712bc7071b068b49a13fdb22e581942243f008795080100000000002251208d34c01d98b9975ba64ff6e7ef80191c6cca14b8fbf87b868a8e9157967fd3bad8f600000000000022512077b9c4ca0e23ff630bff1c13b183669f8ae3e3ca95cc3abecb7c131ebfdf7dc3e8f7000000000000225120cf04a33b39bb54ab07df8268b995bf1bb3fc9b0cce26c779e0fc28afdf1cfd3acfdb000000000000225120f7739b2a67920f69c8d5cf0512bd5fd6bf02955e1dca9d9eab0cf1c204a8606e04fa0000000000002251204cf5322e4ff2118bd0d3b0b7fc6f04c0f0f334579f38df29c5bd07c17b9e1d86f40301000000000017a9147a3005627731e481b6fe77ab89d6d69dbb75399987720401000000000017a9149af3e5434d68e4cb9b12035d359050fd51c62e608788eb00000000000017a9140cc19097f9a57bf5a943a1d16dd2c1045313be1f8758e800000000000017a914b6196d7351f25d7efcb6e5c11dc4a611026a76be87b8db000000000000225120f7739b2a67920f69c8d5cf0512bd5fd6bf02955e1dca9d9eab0cf1c204a8606e09f300000000000017a914e049fd483cb209a7e173826c983ff7d47e09e3328794e800000000000017a9145335082c985776dc08688a354350aa12cf31e3e687c6f200000000000022512073e4bc8e10260e3ac8f27ada68670e2abd838f139eecaa28d36c5bbece80a554520801000000000017a914f3546c7a8af1ee6f3fddc0b1479b869347f52ca88734e80000000000001600147d55169a15439f59b9409e72fcff1c49637c2402bc0101000000000017a914d8b43648ef4a33bdcb0608ca86700cc4c18e00bb87d5eb0000000000002251200512fc29b28e3348517367f4b6ccfe4871ae2bea657a772e39a4e30036af789ee40701000000000017a9147a3005627731e481b6fe77ab89d6d69dbb7539998784ea00000000000017a914c03673b02dabbae673fbda527745efe5f8937f1b87b9fd00000000000022512011bec12dcc2ac9125fd4595d184293eeb4f23ba3bd21b21807bc428b30ce17c7b6f600000000000022512011bec12dcc2ac9125fd4595d184293eeb4f23ba3bd21b21807bc428b30ce17c7900001000000000017a914da9886b43e6f2e8498c9d4a52da5d739c7ced40587ff0b01000000000017a9147a3005627731e481b6fe77ab89d6d69dbb7539998782df00000000000022512078a341a850d45c3fa66a6fe0c4e781f3ae2f929f54a749486821e891896392ffa9f10000000000002251208f5c1670a45a4887d8d70202a693a2448131347076b4c39d8225e0a458c9dcb5ba0b01000000000017a9145891991b9fb61b2cddfd8381fb7f579758a4c32087e9fe00000000000017a914da9886b43e6f2e8498c9d4a52da5d739c7ced405870ddf00000000000017a914dcf6297b18bf608931c6801d0a452ad2ff0f285d87d1e500000000000022512097461ccc9766518e9b8290be0aae51c54d22ffdaf996641ed9fbaaa078d56951c7d800000000000022512059c8a819efee585fb5a99c7437ea4e8d0d1ab8b7dbcc808ca5340ad40743f6718bdf00000000000017a914dcf6297b18bf608931c6801d0a452ad2ff0f285d8759fd00000000000017a9147a3005627731e481b6fe77ab89d6d69dbb7539998720f500000000000017a914076c270aa4adfdd87976968a0f286c97e974b8e587e8fc00000000000017a9142316b49e9a04f073c65f6258f77f761e14fa62de874ae200000000000017a91435f9e49eb80b774639e3ccb7166156e8bb5c497b8760f400000000000017a91435f9e49eb80b774639e3ccb7166156e8bb5c497b87f00401000000000017a914bb8a1d9298ea86ca1d36be2fb287709672076e0287102700000000000022512070d46f93e88186f6a277bd36c1e98e6684d9e9f244df437ebae37e201338c2a68afe00000000000022512059c8a819efee585fb5a99c7437ea4e8d0d1ab8b7dbcc808ca5340ad40743f671dbd800000000000017a914dcf6297b18bf608931c6801d0a452ad2ff0f285d87c3080100000000002251208d34c01d98b9975ba64ff6e7ef80191c6cca14b8fbf87b868a8e9157967fd3bac8e700000000000022512078a341a850d45c3fa66a6fe0c4e781f3ae2f929f54a749486821e891896392ff06ee00000000000017a9143e08905e7223c5cb5e9de5753fd6836d8d02b7938764fe00000000000017a914da9886b43e6f2e8498c9d4a52da5d739c7ced40587c5ea00000000000017a9140e1f82b1090f3afb7ebacfb3794b648fe1c2f3928737d900000000000022512059c8a819efee585fb5a99c7437ea4e8d0d1ab8b7dbcc808ca5340ad40743f671241301000000000017a91466cb521c26e8261284a9370f1b8fd192aed2c4e187c9eb00000000000017a914dcf6297b18bf608931c6801d0a452ad2ff0f285d8700f700000000000017a91449659866eebc5f01b555e1668f38bb85ebab851e8790e20000000000002251201509811da8b879c2cfb0c86a8a8c91c0390da4a28fc99275e1afe2a5f887ad4461eb00000000000017a9142c9bc85322d770ff8da89f84fd27a086b4cbdd578707e600000000000017a914db83b44f700e97b960d4c921aa14b29dd96a4e4e8727fa00000000000017a91466cb521c26e8261284a9370f1b8fd192aed2c4e18728dc00000000000017a914db83b44f700e97b960d4c921aa14b29dd96a4e4e8788d8000000000000225120fa7f88cb8fb54b2c7c2e0af1917dd3a778f4865233e882ed87659988993fd9d7ce0c01000000000017a914d8b43648ef4a33bdcb0608ca86700cc4c18e00bb87c7e100000000000017a91466cb521c26e8261284a9370f1b8fd192aed2c4e18765ec0000000000002251200afc75fd6affdae5fa6e3b6cb0e670a3ee1df518376da52bb12d5c865a9cd9e763fc000000000000225120219199147fba6b2265887961b3a1698fb363875b5cf2934df42a557b0c048c6ad8fb000000000000225120cfba6b0994b84bab5402a2f443f1e4fe9b6d1858d463b0c34ebd97208d94fdee480d01000000000017a9143e926f569d356dfc117a079556849e66d1eadb628710f4000000000000225120f7739b2a67920f69c8d5cf0512bd5fd6bf02955e1dca9d9eab0cf1c204a8606e4a0f01000000000017a9144ed4760f0ce3437cd225613f6ae60146362e52c2870efd00000000000017a914bb8a1d9298ea86ca1d36be2fb287709672076e0287c5f800000000000022512059c8a819efee585fb5a99c7437ea4e8d0d1ab8b7dbcc808ca5340ad40743f671c0f400000000000017a914dcf6297b18bf608931c6801d0a452ad2ff0f285d87d8db00000000000022512078a341a850d45c3fa66a6fe0c4e781f3ae2f929f54a749486821e891896392ff15df00000000000017a914a4ef0fedabeeb2943899e3aed4c18c9db7245d6e871a05010000000000160014ea89a8be81547ed6508d007d23f90124d8849f31f2f900000000000017a914ed7d56e3d9c5158fa9f4498a734da782cb9743af87d6f100000000000017a914f4c36198781dd858606963adceeb0c31707f1a6f87e3e4000000000000160014321565d58e863f528c53fcc655b948dffa1ac29d97de00000000000017a9140e1f82b1090f3afb7ebacfb3794b648fe1c2f39287500e01000000000017a9149af3e5434d68e4cb9b12035d359050fd51c62e6087862b00000000000022512070d46f93e88186f6a277bd36c1e98e6684d9e9f244df437ebae37e201338c2a6160c01000000000017a914fe536c7c03806b2429ffba561167cb1783ce668c877dde00000000000022512073e4bc8e10260e3ac8f27ada68670e2abd838f139eecaa28d36c5bbece80a5549bef00000000000022512059c8a819efee585fb5a99c7437ea4e8d0d1ab8b7dbcc808ca5340ad40743f671e9de0000000000002251206c822bb7a28bfd8cfd2da8102b9e04db4c15760de50c47cb6aa5f487673a7225f2fd00000000000017a914e0b4065806559309a467db9dd04268225829c3c187a5fa0000000000002251201d24953a655d04f32a34b8981aad555081f946e9b36ce7a5f3488029db7b2dcb34fd0000000000002251209bb98275e37cb381dc07e521bb30a8a7f74376e187b99e3e0df2b049d5e4a39f10e800000000000022512078a341a850d45c3fa66a6fe0c4e781f3ae2f929f54a749486821e891896392ffced80000000000002251207cb864badadab824fda368055de23853eb545609bbe8c599ee9315456432381ac6f100000000000017a91453e3d7b042450d5691c2012f13d7cfbb916db43287c5f80000000000002251209bb98275e37cb381dc07e521bb30a8a7f74376e187b99e3e0df2b049d5e4a39ffce500000000000017a914076c270aa4adfdd87976968a0f286c97e974b8e587c5de00000000000017a914f4c36198781dd858606963adceeb0c31707f1a6f87610a0100000000002251208d34c01d98b9975ba64ff6e7ef80191c6cca14b8fbf87b868a8e9157967fd3ba98f0000000000000225120640dd9344646268c0736fce8850478e7387ff6757a8f5ef3ff533957afcced48eae400000000000017a91422e448fa46f6643081b19703dac50afba6e671eb87d8db00000000000022512097461ccc9766518e9b8290be0aae51c54d22ffdaf996641ed9fbaaa078d5695101409f45874b8abf33d46d8ee3ad9daade2d640e259e3cba0feef53ecf923ab99bc5019fe2209ed3db4d56c8afb5b2233e053f0c55b16b541c6199a842a5e352e15101402c161b7ce701794d91981702947fa09208f02d873beb461d09f679f62d50201b1a0da12b02d9a5c07aca8027cccc9bca55d1bc40b13e7de1aa9cdb59e233d49a01403cca9fce16741c3e21033052b5858784362b96b2f7d15c15af734c384cced6df3c2812be99b3c6d33fb35494e7a854b80dca2bacef66154a0f0fdff219d4c3770140068e0e1f6376e674013fda345d229acc47272bd173e9c9ec2fe1500cdab64f492a13083821ec51facf1d5a57a47f5cffb3c8d2a5317f8f67b26f4240a00fe63701404e730262b00db14596bad043aee9727f5ac0a688aa4f7b31e02bcc6066109646b6805a1774d744c73c2da4ded5dee679c6ca8bb12f8d9b9bd883063cd2a5d876014098fe55ec4456676c72c02af2a1cd7f46f1aa0ffd56b5dc53a6d528eb29779806363ebf44a9d5df4f5ec5cb0875d2ad65e42f14c5f21a1c21346a0a0ec60809bc014015981c47a9eb2dd19bc9e18a2ada10b437eb77dab0769346b5bc444b787d365f7d57218b0e4cc8285b835cc8f9f0919e4051b44c847cb14db365984601594f8d0140f7cc635d30a9fcff53a887647ceb0b296dcbe2e4318abb040d88d98c35f05d757aa58f22d0eac1c44eccc11bcc91ca6a11c4d1b40b2fa87528851b182df760db00000000";
decode_inscription(btc_tx_hex);
decode_inscription(btc_tx_hex, vec![], 0, 0);
}

#[test]
fn test_local_tx() {
#[tokio::test]
async fn test_local_tx() {
let _ = tracing_subscriber::fmt::try_init();
//commit tx
let btx_tx_hex = "01000000000101303a8b5191266d37103f6c4c6033019b59d98ac468e07f61ddbc6f50b204a7eb0000000000fdffffff029b270000000000002251201d44f728e28f6ffa0b0094edabefb466a348e1e7adbf2ff0e7e70abd2ed8871bcbd0029500000000225120ae68b97d450930db183dede9c33fbb1c147a69672ecc3100a75f9be25c50f0cf014064cf4601628581d59a8dc482f10c67106ffad818869e733d9cabbdab73240da556aa4d04afd5fa1e72d5ca1ea4fa151385e3e8a7596ebd3959212c2a57696c8400000000";
decode_inscription(btx_tx_hex);
decode_inscription(btx_tx_hex, vec![], 0, 0);
//reveal tx
let btc_tx_hex = "010000000001019cea25cbdacc895f9dbb85e4bfb7aa51d04cc69cc7f75ed49da3ff3f442f2e7f0000000000fdffffff01102700000000000022512036646c76dd6505025341c7cc1cf6c22fcc638c47454945da1948a4637a86f9200340e7f99517f921be44b83854b05d7eb98f7c1a9a0cd373ade7e29c1bcc321190bef1766976726641b69ccc61f9ea62ea55b97e644ec47a15481d3b70b0c44d008f4c207f6ef96528b25ace707fe33f4a23113c824da971dab921a2ad311c309edf0944ac0063036f7264010118746578742f706c61696e3b636861727365743d7574662d38000668656c6c6f0a6821c17f6ef96528b25ace707fe33f4a23113c824da971dab921a2ad311c309edf094400000000";
decode_inscription(btc_tx_hex);
decode_inscription(btc_tx_hex, vec![], 0, 0);
}
15 changes: 13 additions & 2 deletions crates/rooch-types/src/bitcoin/ord.rs
Original file line number Diff line number Diff line change
Expand Up @@ -243,11 +243,22 @@ impl<'a> OrdModule<'a> {
pub const FROM_TRANSACTION_FUNCTION_NAME: &'static IdentStr =
ident_str!("from_transaction_bytes");

pub fn from_transaction(&self, tx: &Transaction) -> Result<Vec<Inscription>> {
pub fn from_transaction(
&self,
tx: &Transaction,
input_utxo_values: Vec<u64>,
next_inscription_number: u32,
next_sequence_number: u32,
) -> Result<Vec<Inscription>> {
let call = Self::create_function_call(
Self::FROM_TRANSACTION_FUNCTION_NAME,
vec![],
vec![MoveValue::vector_u8(tx.to_bytes())],
vec![
MoveValue::vector_u8(tx.to_bytes()),
input_utxo_values.to_move_value(),
next_inscription_number.to_move_value(),
next_sequence_number.to_move_value(),
],
);
let ctx = TxContext::new_readonly_ctx(AccountAddress::ONE);
let inscriptions =
Expand Down
Loading

0 comments on commit 8307502

Please sign in to comment.