Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
plebhash committed Feb 20, 2025
1 parent c41c445 commit 779b3b1
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 22 deletions.
1 change: 1 addition & 0 deletions protocols/v2/roles-logic-sv2/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ quickcheck_macros = "1"
rand = "0.8.5"
toml = {git = "https://github.com/diondokter/toml-rs", default-features = false, rev="c4161aa"}
serde = { version = "1.0.89", features = ["derive", "alloc"], default-features = false}
hex = "0.4.3"

[features]
prop_test = ["template_distribution_sv2/prop_test"]
Expand Down
27 changes: 17 additions & 10 deletions protocols/v2/roles-logic-sv2/src/job_creator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -532,15 +532,22 @@ pub mod tests {
let mut encoded_clone = encoded.clone();
encoded_clone.extend_from_slice(&extranonce[..]);
encoded_clone.extend_from_slice(coinbase_suffix);
// let mut i = 1;
// while let Err(_) = Transaction::deserialize(&encoded_clone) {
// encoded_clone = encoded.clone();
// extranonce.push(0);
// encoded_clone.extend_from_slice(&extranonce[..]);
// encoded_clone.extend_from_slice(coinbase_suffix);
// i+=1;
// }
// println!("SIZE: {:?}", i);
Transaction::deserialize(&encoded_clone).unwrap();

let parsed_tx = Transaction::deserialize(&encoded_clone).unwrap();

let coinbase = Coinbase {
tx: parsed_tx.clone(),
script_sig_prefix_len: 60,
};

let prefix = coinbase.clone().coinbase_tx_prefix().unwrap().to_vec();
let suffix = coinbase.clone().coinbase_tx_suffix().unwrap().to_vec();

let mut encoded_b = vec![];
encoded_b.extend_from_slice(&prefix);
encoded_b.extend_from_slice(&extranonce[..]);
encoded_b.extend_from_slice(&suffix);

let parsed_tx_b = Transaction::deserialize(&encoded_b).unwrap();
}
}
20 changes: 8 additions & 12 deletions protocols/v2/roles-logic-sv2/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ use stratum_common::{
transaction::{OutPoint, TxIn, TxOut},
witness::Witness,
},
consensus::Encodable,
hash_types::{BlockHash, TxMerkleNode},
hashes::{sha256, sha256d::Hash as DHash, Hash},
secp256k1::{All, Secp256k1},
Expand Down Expand Up @@ -207,17 +208,10 @@ impl Coinbase {
}

// serialize input from Transaction object
fn serialized_input(&self) -> Vec<u8> {
let input = self.tx.input[0].clone(); // coinbase only has one input

let mut serialized_input = Vec::new();
serialized_input.extend_from_slice(&input.previous_output.txid);
serialized_input.extend_from_slice(&input.previous_output.vout.to_le_bytes());
serialized_input.push(input.script_sig.len() as u8);
serialized_input.extend_from_slice(input.script_sig.as_bytes());
serialized_input.extend_from_slice(&input.sequence.0.to_le_bytes());

serialized_input
pub fn serialized_input(&self) -> Vec<u8> {
let mut buffer = Vec::new();
self.tx.input[0].consensus_encode(&mut buffer).unwrap();
buffer
}

// serialize outputs from Transaction object
Expand Down Expand Up @@ -265,7 +259,7 @@ impl Coinbase {
// while the legacy `txid` is what is used for computing the merkle root
pub fn coinbase_tx_suffix(&self) -> Result<B064K<'static>, Error> {
let serialized_input = self.serialized_input();
let input_sequence = &serialized_input[serialized_input.len() - 4..];
let input_sequence: &[u8] = &serialized_input[serialized_input.len() - 4..];

let serialized_outputs = self.serialized_outputs();
let lock_time_u32: u32 = self.tx.lock_time.into();
Expand Down Expand Up @@ -317,6 +311,8 @@ pub fn merkle_root_from_path<T: AsRef<[u8]>>(
debug!("- total coinbase len: {}", coinbase.len());
debug!("- full coinbase: {:?}", coinbase);

let coinbase_tx = Transaction::deserialize(&coinbase).unwrap();

let coinbase_id = Sha256dHash::hash(&coinbase).into_inner();

debug!("- coinbase_id: {:?}", coinbase_id);
Expand Down

0 comments on commit 779b3b1

Please sign in to comment.