Skip to content

Commit

Permalink
[bitcoin]refactor Bitcoin tx progress
Browse files Browse the repository at this point in the history
  • Loading branch information
jolestar committed Jun 4, 2024
1 parent 241c122 commit 6fb83dd
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 4 deletions.
29 changes: 28 additions & 1 deletion crates/rooch-types/src/bitcoin/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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<bitcoin::OutPoint> for OutPoint {
Expand Down Expand Up @@ -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]
Expand Down Expand Up @@ -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::<u8>::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());
}
}
6 changes: 3 additions & 3 deletions frameworks/bitcoin-move/sources/bitcoin.move
Original file line number Diff line number Diff line change
Expand Up @@ -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>(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)){
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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<u8>){
let btc_block_store_obj = borrow_block_store_mut();
let time = process_block(btc_block_store_obj, block_height, block_hash, block_bytes);
Expand Down

0 comments on commit 6fb83dd

Please sign in to comment.