Skip to content

Commit

Permalink
Fix runtime coinbase tx conversion (#65)
Browse files Browse the repository at this point in the history
* Refactor NetworkMessage

* Use original coin sequence

* Fix clippy
  • Loading branch information
liuchengxu authored Oct 19, 2024
1 parent 7a31c1b commit e623261
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 19 deletions.
2 changes: 1 addition & 1 deletion crates/pallet-bitcoin/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ pub mod pallet {
script_pubkey: txout.script_pubkey.clone().into_bytes(),
height: 0u32,
};
Coins::<T>::insert(txid.clone(), index as u32, coin);
Coins::<T>::insert(txid, index as u32, coin);
});
}
}
Expand Down
17 changes: 11 additions & 6 deletions crates/pallet-bitcoin/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ impl From<LockTime> for absolute::LockTime {
}

/// Wrapper type for representing [`bitcoin::Txid`] in runtime, stored in reversed byte order.
#[derive(Clone, TypeInfo, Encode, Decode, MaxEncodedLen, PartialEq)]
#[derive(Clone, Copy, TypeInfo, Encode, Decode, MaxEncodedLen, PartialEq)]
pub struct Txid(H256);

impl Txid {
Expand Down Expand Up @@ -159,7 +159,11 @@ pub enum TxIn {
/// Input from a coinbase transaction, which does not reference any previous output.
Coinbase {
/// Arbitrary data used in the coinbase transaction, such as extra nonce or miner-specific information.
coinbase_data: Vec<u8>,
data: Vec<u8>,
/// Sequence.
///
/// Note that the value of sequence is not always Sequence::MAX, see https://www.blockchain.com/explorer/transactions/btc/0961c660358478829505e16a1f028757e54b5bbf9758341a7546573738f31429
sequence: u32,
},
/// Input from a regular transaction, which references a previous output (`OutPoint`).
Regular(RegularTxIn),
Expand All @@ -169,7 +173,8 @@ impl From<bitcoin::TxIn> for TxIn {
fn from(txin: bitcoin::TxIn) -> Self {
if txin.previous_output.is_null() {
Self::Coinbase {
coinbase_data: txin.script_sig.into_bytes(),
data: txin.script_sig.into_bytes(),
sequence: txin.sequence.0,
}
} else {
Self::Regular(RegularTxIn {
Expand All @@ -185,10 +190,10 @@ impl From<bitcoin::TxIn> for TxIn {
impl From<TxIn> for bitcoin::TxIn {
fn from(val: TxIn) -> Self {
match val {
TxIn::Coinbase { coinbase_data } => bitcoin::TxIn {
TxIn::Coinbase { data, sequence } => bitcoin::TxIn {
previous_output: bitcoin::OutPoint::null(),
script_sig: bitcoin::ScriptBuf::from_bytes(coinbase_data),
sequence: bitcoin::Sequence::MAX,
script_sig: bitcoin::ScriptBuf::from_bytes(data),
sequence: bitcoin::Sequence(sequence),
witness: bitcoin::Witness::new(),
},
TxIn::Regular(RegularTxIn {
Expand Down
2 changes: 2 additions & 0 deletions crates/sc-consensus-nakamoto/src/verification.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ pub enum Error {
#[error("UTXO not found (#{block_number}:{txid}: {utxo:?})")]
UtxoNotFound {
block_number: u32,
tx_index: usize,
txid: Txid,
utxo: OutPoint,
},
Expand Down Expand Up @@ -393,6 +394,7 @@ where
let (spent_output, is_coinbase, coin_height) =
access_coin(coin).ok_or_else(|| Error::UtxoNotFound {
block_number,
tx_index,
txid: get_txid(tx_index),
utxo: coin,
})?;
Expand Down
2 changes: 1 addition & 1 deletion crates/subcoin-network/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -397,7 +397,7 @@ where
let (sender, receiver) = oneshot::channel();

if worker_msg_sender
.unbounded_send(NetworkWorkerMessage::InboundPeersCount(sender))
.unbounded_send(NetworkWorkerMessage::RequestInboundPeersCount(sender))
.is_err()
{
return;
Expand Down
14 changes: 7 additions & 7 deletions crates/subcoin-network/src/network.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,13 +59,13 @@ pub(crate) struct IncomingTransaction {
#[derive(Debug)]
pub(crate) enum NetworkWorkerMessage {
/// Request the current network status.
NetworkStatus(oneshot::Sender<NetworkStatus>),
RequestNetworkStatus(oneshot::Sender<NetworkStatus>),
/// Request the sync peers.
SyncPeers(oneshot::Sender<Vec<PeerSync>>),
RequestSyncPeers(oneshot::Sender<Vec<PeerSync>>),
/// Request the number of inbound connected peers.
InboundPeersCount(oneshot::Sender<usize>),
RequestInboundPeersCount(oneshot::Sender<usize>),
/// Request a specific transaction by its Txid.
GetTransaction((Txid, oneshot::Sender<Option<Transaction>>)),
RequestTransaction((Txid, oneshot::Sender<Option<Transaction>>)),
/// Submit a transaction to the transaction manager.
SendTransaction((IncomingTransaction, oneshot::Sender<SendTransactionResult>)),
/// Enable the block sync within the chain sync component.
Expand All @@ -91,7 +91,7 @@ impl NetworkHandle {
let (sender, receiver) = oneshot::channel();

self.worker_msg_sender
.unbounded_send(NetworkWorkerMessage::NetworkStatus(sender))
.unbounded_send(NetworkWorkerMessage::RequestNetworkStatus(sender))
.ok();

receiver.await.ok()
Expand All @@ -103,7 +103,7 @@ impl NetworkHandle {

if self
.worker_msg_sender
.unbounded_send(NetworkWorkerMessage::SyncPeers(sender))
.unbounded_send(NetworkWorkerMessage::RequestSyncPeers(sender))
.is_err()
{
return Vec::new();
Expand All @@ -118,7 +118,7 @@ impl NetworkHandle {

if self
.worker_msg_sender
.unbounded_send(NetworkWorkerMessage::GetTransaction((txid, sender)))
.unbounded_send(NetworkWorkerMessage::RequestTransaction((txid, sender)))
.is_err()
{
return None;
Expand Down
8 changes: 4 additions & 4 deletions crates/subcoin-network/src/worker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ where

fn process_worker_message(&mut self, worker_msg: NetworkWorkerMessage, bandwidth: &Bandwidth) {
match worker_msg {
NetworkWorkerMessage::NetworkStatus(result_sender) => {
NetworkWorkerMessage::RequestNetworkStatus(result_sender) => {
let net_status = NetworkStatus {
num_connected_peers: self.peer_manager.connected_peers_count(),
total_bytes_inbound: bandwidth.total_bytes_inbound.load(Ordering::Relaxed),
Expand All @@ -252,14 +252,14 @@ where
};
let _ = result_sender.send(net_status);
}
NetworkWorkerMessage::SyncPeers(result_sender) => {
NetworkWorkerMessage::RequestSyncPeers(result_sender) => {
let sync_peers = self.chain_sync.peers.values().cloned().collect::<Vec<_>>();
let _ = result_sender.send(sync_peers);
}
NetworkWorkerMessage::InboundPeersCount(result_sender) => {
NetworkWorkerMessage::RequestInboundPeersCount(result_sender) => {
let _ = result_sender.send(self.peer_manager.inbound_peers_count());
}
NetworkWorkerMessage::GetTransaction((txid, result_sender)) => {
NetworkWorkerMessage::RequestTransaction((txid, result_sender)) => {
let _ = result_sender.send(self.transaction_manager.get_transaction(&txid));
}
NetworkWorkerMessage::SendTransaction((incoming_transaction, result_sender)) => {
Expand Down

0 comments on commit e623261

Please sign in to comment.