Skip to content

Commit

Permalink
fix: block deserialization (#45)
Browse files Browse the repository at this point in the history
* added temp test

* fix: CoinbasePayload.best_cl_signature as BLSSignature

* fix: CoinbasePayload.best_cl_height as compactsize

* small fix

---------

Co-authored-by: Odysseas Gabrielides <[email protected]>
  • Loading branch information
QuantumExplorer and ogabrielides authored Oct 30, 2024
1 parent eb700ae commit 078ae44
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 8 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dash/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "dashcore"
version = "0.33.1"
version = "0.33.2"
authors = [
"Samuel Westrich <[email protected]>",
"Anton Suprunchuk <[email protected]>",
Expand Down
52 changes: 52 additions & 0 deletions dash/src/blockdata/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -466,6 +466,58 @@ mod tests {
assert_eq!(serialize(&real_decode), some_block);
}

#[test]
fn block_test_2() {

let some_block = hex!(
"000000200ec684405b58b3a0f0144c9a92c7d4296587ba6fc71041fff2130a038a00000078e259b490cfc8a8e50e1933afde3f777a47bdac8b61d504a51b68dede2ac181cd0822679008011e2e0506000103000500010000000000000000000000000000000000000000000000000000000000000000ffffffff06031e42110101ffffffff037a681d04000000001976a914c69a0bda7daaae481be8def95e5f347a1d00a4b488ac8815a10400000000016ae523b707000000001976a91464f2b2b84f62d68a2cd7f7f5fb2b5aa75ef716d788ac00000000af03001e421100032459005e51c0c2180b5a06001eacee43e1ab9a169dc0c9fb8ca40506258fed68c5aa969c10a3015bde2cfbe1700c795f97a4fa3946e9de4decc14d3a2117be00b695cd60fa9339a8318b65dab7a8a45655ffc6daf0ff1b0b19958903460613f92ac911f3b80bf71bfaeff3968aff87dd0750233a73145f913a3a2a9b1ddaca1ef8f14738870767a80d539ad639837dff1140f78b8f4d65eb4216b7128b736a2453afd99ce4040000"
);
let cutoff_block = hex!(
"010000004ddccd549d28f385ab457e98d1b11ce80bfea2c5ab93015ade4973e400000000bf4473e53794beae34e64fccc471dace6ae544180816f89591894e0f417a914cd74d6e49ffff001d323b3a7b0201000000010000000000000000000000000000000000000000000000000000000000000000ffffffff0804ffff001d026e04ffffffff0100f2052a0100000043410446ef0102d1ec5240f0d061a4246c1bdef63fc3dbab7733052fbbf0ecd8f41fc26bf049ebb4f9527f374280259e7cfa99c48b0e3f39c51347a19a5819651503a5ac00000000010000000321f75f3139a013f50f315b23b0c9a2b6eac31e2bec98e5891c924664889942260000000049483045022100cb2c6b346a978ab8c61b18b5e9397755cbd17d6eb2fe0083ef32e067fa6c785a02206ce44e613f31d9a6b0517e46f3db1576e9812cc98d159bfdaf759a5014081b5c01ffffffff79cda0945903627c3da1f85fc95d0b8ee3e76ae0cfdc9a65d09744b1f8fc85430000000049483045022047957cdd957cfd0becd642f6b84d82f49b6cb4c51a91f49246908af7c3cfdf4a022100e96b46621f1bffcf5ea5982f88cef651e9354f5791602369bf5a82a6cd61a62501fffffffffe09f5fe3ffbf5ee97a54eb5e5069e9da6b4856ee86fc52938c2f979b0f38e82000000004847304402204165be9a4cbab8049e1af9723b96199bfd3e85f44c6b4c0177e3962686b26073022028f638da23fc003760861ad481ead4099312c60030d4cb57820ce4d33812a5ce01ffffffff01009d966b01000000434104ea1feff861b51fe3f5f8a3b12d0f4712db80e919548a80839fc47c6a21e66d957e9c5d8cd108c7a2d2324bad71f9904ac0ae7336507d785b17a2c115e427a32fac"
);

let prevhash = hex!("85691f6a060e65346c281ed25b99dbd18c139053562ccd001d00000000000000");
let merkle = hex!("377b6aa24658b7a0ae7b73f0673d047a291de5cbc06907038b288b2ebf491c2c");

let work_bytes: [u8; 32] =
hex!("000000000000000000000000000000000000000000000000050ec30af44bf25e")
.try_into()
.unwrap();
let work = Work::from_be_bytes(work_bytes);

let real_decode: Block = deserialize(&some_block).expect("issue here");
let bad_decode: Result<Block, _> = deserialize(&cutoff_block);

assert!(bad_decode.is_err());
assert_eq!(real_decode.header.version, Version(536870912));
assert_eq!(serialize(&real_decode.header.prev_blockhash), prevhash);
assert_eq!(real_decode.header.merkle_root, real_decode.compute_merkle_root().unwrap());
assert_eq!(serialize(&real_decode.header.merkle_root), merkle);
assert_eq!(real_decode.header.time, 1685447065);
assert_eq!(real_decode.header.bits, CompactTarget::from_consensus(422747587));
assert_eq!(real_decode.header.nonce, 2456102546);
assert_eq!(real_decode.header.work(), work);
assert_eq!(
real_decode.header.validate_pow(real_decode.header.target()).unwrap(),
real_decode.block_hash()
);
assert_eq!(real_decode.header.difficulty(), 84852220);
assert_eq!(real_decode.header.difficulty_float(), 84852220.19239795);
// [test] TODO: check the transaction data

assert_eq!(real_decode.size(), some_block.len());
assert_eq!(real_decode.strippedsize(), some_block.len());
assert_eq!(
real_decode.weight(),
Weight::from_non_witness_data_size(some_block.len() as u64)
);

// should be also ok for a non-witness block as commitment is optional in that case
assert!(real_decode.check_witness_commitment());

assert_eq!(serialize(&real_decode), some_block);
}

// Check testnet block 000000000000045e0b1660b6445b5e5c5ab63c9a4f956be7e1e69be04fa4497b
#[ignore]
#[test]
Expand Down
19 changes: 13 additions & 6 deletions dash/src/blockdata/transaction/special_transaction/coinbase.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@
use crate::consensus::{Decodable, Encodable, encode};
use crate::hash_types::{MerkleRootMasternodeList, MerkleRootQuorums};
use crate::io::{Error, ErrorKind};
use crate::prelude::Vec;
use crate::{VarInt, io};
use crate::bls_sig_utils::BLSSignature;

/// A Coinbase payload. This is contained as the payload of a coinbase special transaction.
/// The Coinbase payload is described in DIP4.
Expand All @@ -36,7 +36,7 @@ pub struct CoinbasePayload {
pub merkle_root_masternode_list: MerkleRootMasternodeList,
pub merkle_root_quorums: MerkleRootQuorums,
pub best_cl_height: Option<u32>,
pub best_cl_signature: Option<Vec<u8>>,
pub best_cl_signature: Option<BLSSignature>,
pub asset_locked_amount: Option<u64>,
}

Expand Down Expand Up @@ -93,9 +93,16 @@ impl Decodable for CoinbasePayload {
let height = u32::consensus_decode(r)?;
let merkle_root_masternode_list = MerkleRootMasternodeList::consensus_decode(r)?;
let merkle_root_quorums = MerkleRootQuorums::consensus_decode(r)?;
let best_cl_height = if version >= 3 { Some(u32::consensus_decode(r)?) } else { None };
let best_cl_height = if version >= 3 {
let value = u8::consensus_decode(r)?;
match value {
253 => Some(u16::consensus_decode(r)? as u32),
254 => Some(u32::consensus_decode(r)?),
_ => Some(value as u32)
}
} else { None };
let best_cl_signature =
if version >= 3 { Some(Vec::<u8>::consensus_decode(r)?) } else { None };
if version >= 3 { Some(BLSSignature::consensus_decode(r)?) } else { None };
let asset_locked_amount = if version >= 3 { Some(u64::consensus_decode(r)?) } else { None };
Ok(CoinbasePayload {
version,
Expand All @@ -112,7 +119,7 @@ impl Decodable for CoinbasePayload {
#[cfg(test)]
mod tests {
use hashes::Hash;

use crate::bls_sig_utils::BLSSignature;
use crate::consensus::Encodable;
use crate::hash_types::{MerkleRootMasternodeList, MerkleRootQuorums};
use crate::transaction::special_transaction::coinbase::CoinbasePayload;
Expand All @@ -127,7 +134,7 @@ mod tests {
merkle_root_masternode_list: MerkleRootMasternodeList::all_zeros(),
merkle_root_quorums: MerkleRootQuorums::all_zeros(),
best_cl_height: Some(900),
best_cl_signature: Some(vec![0; 96]),
best_cl_signature: Some(BLSSignature::from([0; 96])),
asset_locked_amount: Some(10000),
};
assert_eq!(payload.size(), *want);
Expand Down

0 comments on commit 078ae44

Please sign in to comment.