From 6fb83ddd76579be6f9080cbc998edce589b11223 Mon Sep 17 00:00:00 2001 From: jolestar Date: Tue, 4 Jun 2024 09:58:20 +0800 Subject: [PATCH] [bitcoin]refactor Bitcoin tx progress --- crates/rooch-types/src/bitcoin/types.rs | 29 +++++++++++++++++++- frameworks/bitcoin-move/sources/bitcoin.move | 6 ++-- 2 files changed, 31 insertions(+), 4 deletions(-) diff --git a/crates/rooch-types/src/bitcoin/types.rs b/crates/rooch-types/src/bitcoin/types.rs index 308fd26ceb..5e304502d0 100644 --- a/crates/rooch-types/src/bitcoin/types.rs +++ b/crates/rooch-types/src/bitcoin/types.rs @@ -172,6 +172,13 @@ impl MoveStructState for Transaction { } } +impl Transaction { + + pub fn is_coinbase(&self) -> bool { + self.input.len() == 1 && self.input[0].previous_output.is_null() + } +} + #[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)] pub struct TxIn { /// The reference to the previous output that is being used as an input. @@ -259,6 +266,16 @@ impl OutPoint { pub fn new(txid: AccountAddress, vout: u32) -> Self { Self { txid, vout } } + + pub fn null() -> Self { + Self { + txid: AccountAddress::ZERO, + vout: u32::MAX, + } + } + + #[inline] + pub fn is_null(&self) -> bool { *self == OutPoint::null() } } impl From for OutPoint { @@ -359,7 +376,7 @@ impl MoveStructState for TxOut { #[cfg(test)] mod tests { use super::*; - use bitcoin::{consensus::deserialize, Block}; + use bitcoin::{consensus::{deserialize, Decodable}, Block}; use hex::FromHex; #[test] @@ -388,4 +405,14 @@ mod tests { assert_eq!(block_header.bits, 486604799); assert_eq!(block_header.nonce, 2067413810); } + + #[test] + fn test_coin_base_tx(){ + //https://mempool.space/api/tx/3ea07d9966895a8a73a5580d34713b8ff302a8413215af156e2ad484e50ccc5c/hex + let tx_bytes = Vec::::from_hex("010000000001010000000000000000000000000000000000000000000000000000000000000000ffffffff56035cea0c194d696e656420627920416e74506f6f6c20b9004206d7a9abb4fabe6d6dbbd991d69c05a27bd76b9bc7ad80763da6d836be289c7a53e12612625d5d1fec100000000000000000003d64a66d000000000000ffffffff05220200000000000017a91442402a28dd61f2718a4b27ae72a4791d5bbdade7872d04b0130000000017a9145249bdf2c131d43995cff42e8feee293f79297a8870000000000000000266a24aa21a9ede27dc3f39ba542af6f3b7b10d1b36d123910d46438a360e718ffcdd550d3c37e00000000000000002f6a2d434f52450142fdeae88682a965939fee9b7b2bd5b99694ff644e3ecda72cb7961caa4b541b1e322bcfe0b5a03000000000000000002b6a2952534b424c4f434b3a920ea155edd52e4efb952d4cec821261746fb0aa72b2c1552c1cce2b0061b56e0120000000000000000000000000000000000000000000000000000000000000000000000000").unwrap(); + let bitcoin_tx: bitcoin::Transaction = deserialize(&tx_bytes).unwrap(); + assert!(bitcoin_tx.is_coinbase()); + let tx: Transaction = bitcoin_tx.into(); + assert!(tx.is_coinbase()); + } } diff --git a/frameworks/bitcoin-move/sources/bitcoin.move b/frameworks/bitcoin-move/sources/bitcoin.move index 16140afee9..622a1eb8be 100644 --- a/frameworks/bitcoin-move/sources/bitcoin.move +++ b/frameworks/bitcoin-move/sources/bitcoin.move @@ -88,7 +88,7 @@ module bitcoin_move::bitcoin{ assert!(!table::contains(&btc_block_store.hash_to_height, block_hash), ErrorBlockAlreadyProcessed); let block = bcs::from_bytes(block_bytes); - process_txs(btc_block_store, &block, block_height); + //process_txs(btc_block_store, &block, block_height); let block_header = types::header(&block); if(table::contains(&btc_block_store.height_to_hash, block_height)){ @@ -129,7 +129,7 @@ module bitcoin_move::bitcoin{ fun is_coinbase_tx(tx: &Transaction): bool { let txinput = types::tx_input(tx); - let is_coinbase = if(vector::length(txinput) > 0) { + let is_coinbase = if(vector::length(txinput) == 1) { let first_input = vector::borrow(txinput, 0); let previous_output = types::txin_previous_output(first_input); types::is_null_outpoint(previous_output) @@ -292,7 +292,7 @@ module bitcoin_move::bitcoin{ } - /// The relay server submit a new Bitcoin block to the light client. + /// The the sequencer submit a new Bitcoin block fun submit_new_block(block_height: u64, block_hash: address, block_bytes: vector){ let btc_block_store_obj = borrow_block_store_mut(); let time = process_block(btc_block_store_obj, block_height, block_hash, block_bytes);