Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
252 changes: 252 additions & 0 deletions dash/tests/core_v24_devnet_vectors.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,252 @@
//! Transactions and blocks produced by Dash Core v24.
//!
//! The vectors in `data/core_v24_devnet/` were mined on a devnet where every node runs
//! Dash Core v24.0.0-rc.3 and V24 activated at height 15000, and fetched through DAPI
//! (`Core/getTransaction` and `Core/getBlock`). Each transaction file hashes (sha256d,
//! reversed) to the txid asserted below, so the bytes are exactly what Core relayed.

use std::net::SocketAddr;
use std::str::FromStr;

use dashcore::blockdata::transaction::special_transaction::TransactionPayload;
use dashcore::blockdata::transaction::special_transaction::coinbase::CoinbasePayload;
use dashcore::blockdata::transaction::special_transaction::provider_registration::{
MasternodePayoutShare, ProviderMasternodeType, ProviderRegistrationPayload, SharedCollateral,
};
use dashcore::consensus::{deserialize, serialize};
use dashcore::hash_types::MerkleRootAssetUnlocks;
use dashcore::hashes::Hash;
use dashcore::sml::masternode_list_entry::MasternodeNetInfo;
use dashcore::sml::masternode_list_entry::net_info::{ExtNetInfo, NetInfoEntry, NetInfoPurpose};
use dashcore::{Address, Block, Network, PlatformNodeId, ScriptBuf, Transaction, Txid, VarInt};
use test_case::test_case;

const PRO_REG_TX_ONE_PAYOUT: &str =
include_str!("data/core_v24_devnet/pro_reg_tx_v3_one_payout.hex");
const PRO_REG_TX_TWO_PAYOUTS: &str =
include_str!("data/core_v24_devnet/pro_reg_tx_v3_two_payouts.hex");
const PRO_REG_TX_P2SH_PAYOUT: &str =
include_str!("data/core_v24_devnet/pro_reg_tx_v3_p2sh_payout.hex");
const EVO_PRO_UP_SERV_TX: &str = include_str!("data/core_v24_devnet/evo_pro_up_serv_tx_v3.hex");
const PRO_UP_REG_TX: &str = include_str!("data/core_v24_devnet/pro_up_reg_tx_v3.hex");

const PRO_REG_TX_ONE_PAYOUT_TXID: &str =
"20814c480e94dce7f3fe3f3370ec6bacd1fdc4f446f17aa7de7886be0b5d07c2";
const PRO_REG_TX_TWO_PAYOUTS_TXID: &str =
"f7e6fad0084ae34e410a8b710e5d74c0af1b95a29c1f3ae5507bed8e08b38adb";
const PRO_REG_TX_P2SH_PAYOUT_TXID: &str =
"1bff9d395c2647e370202e87c80fa3942930567d217238392363cd2542fb496f";
const EVO_PRO_UP_SERV_TX_TXID: &str =
"7c58d9776ef31869b8cae3f94366e9fedb914e41a5b4c26d048c12f30f5e51ac";
const PRO_UP_REG_TX_TXID: &str = "16b69f49a4aba21954e199efe7209478763133f7c473fab6668d5d2b79e55775";

/// Payout shares are in basis points: a single payee receives the whole owner reward.
const WHOLE_OWNER_REWARD: u16 = 10_000;

fn hex_bytes(hex: &str) -> Vec<u8> {
hex_conservative::decode_to_vec(hex.trim()).expect("vector is hex")
}

/// Decodes a transaction Core produced and checks that nothing is lost on the way back: it
/// re-encodes to the same bytes and hashes to the txid Core reported. The payload's `size()`
/// must also match its encoding, since fees are estimated from it.
fn decode_core_tx(hex: &str, txid: &str) -> TransactionPayload {
let bytes = hex_bytes(hex);
let tx: Transaction = deserialize(&bytes).expect("Core v24 transaction decodes");
assert_eq!(serialize(&tx), bytes, "re-encoding changes the transaction bytes");
assert_eq!(tx.txid(), Txid::from_str(txid).unwrap(), "txid differs from Core's");
let payload = tx.special_transaction_payload.clone().expect("special transaction payload");
assert_eq!(payload.size(), serialize(&payload).len(), "payload size() is off");
payload
}

fn decode_pro_reg_tx(hex: &str, txid: &str) -> ProviderRegistrationPayload {
let payload = decode_core_tx(hex, txid);
let payload =
payload.to_provider_registration_payload().expect("payload decodes as a typed ProRegTx");
assert_eq!(payload.version, 3);
assert_eq!(payload.masternode_type, ProviderMasternodeType::Regular);
// From version 3 the payees live in `payouts` and the legacy payout script stays empty.
assert!(payload.script_payout.is_empty());
assert_eq!(payload.platform_node_id, None);
// These masternodes register without announcing an address and do not share their
// collateral, so both fields are present but empty.
assert_eq!(
payload.service_address,
MasternodeNetInfo::Extended(ExtNetInfo {
version: 1,
purposes: vec![],
})
);
assert_eq!(payload.shared_collateral, Some(SharedCollateral::default()));
payload
}

fn p2pkh_payout(payout: &MasternodePayoutShare) -> &ScriptBuf {
assert!(payout.script_payout.is_p2pkh(), "not P2PKH: {:?}", payout.script_payout);
&payout.script_payout
}

fn devnet_address(script: &ScriptBuf) -> String {
Address::from_script(script, Network::Devnet).expect("standard script").to_string()
}

#[test]
fn a_pro_reg_tx_v3_pays_the_whole_owner_reward_to_one_payout() {
let payload = decode_pro_reg_tx(PRO_REG_TX_ONE_PAYOUT, PRO_REG_TX_ONE_PAYOUT_TXID);

let payouts = payload.payouts.expect("version 3 has payouts");
assert_eq!(payouts.len(), 1);
assert_eq!(devnet_address(p2pkh_payout(&payouts[0])), "yQohnoFhM5avKnzVzAX79xT8uCKviaPxj4");
assert_eq!(payouts[0].reward, WHOLE_OWNER_REWARD);
}

#[test]
fn a_pro_reg_tx_v3_splits_the_owner_reward_between_two_payouts() {
let payload = decode_pro_reg_tx(PRO_REG_TX_TWO_PAYOUTS, PRO_REG_TX_TWO_PAYOUTS_TXID);

let payouts = payload.payouts.expect("version 3 has payouts");
let rewards: Vec<u16> = payouts.iter().map(|payout| payout.reward).collect();
assert_eq!(rewards, [6000, 4000]);
assert_ne!(p2pkh_payout(&payouts[0]), p2pkh_payout(&payouts[1]));
}

#[test]
fn a_pro_reg_tx_v3_pays_the_owner_reward_to_a_p2sh_script() {
let payload = decode_pro_reg_tx(PRO_REG_TX_P2SH_PAYOUT, PRO_REG_TX_P2SH_PAYOUT_TXID);

let payouts = payload.payouts.expect("version 3 has payouts");
assert_eq!(payouts.len(), 1);
assert_eq!(payouts[0].reward, WHOLE_OWNER_REWARD);
let script = &payouts[0].script_payout;
assert!(script.is_p2sh(), "not P2SH: {script:?}");
assert_eq!(devnet_address(script), "8yyRcXHGCXypDWTCNZMHUYZJJEC87GqLfK");
}

#[test]
fn an_evo_pro_up_serv_tx_v3_keeps_its_platform_node_id_and_carries_platform_ports_in_net_info() {
let payload = decode_core_tx(EVO_PRO_UP_SERV_TX, EVO_PRO_UP_SERV_TX_TXID);
let payload =
payload.to_update_service_payload().expect("payload decodes as a typed ProUpServTx");

assert_eq!(payload.version, 3);
assert_eq!(payload.mn_type, Some(ProviderMasternodeType::HighPerformance as u16));
assert_eq!(
payload.platform_node_id,
Some(PlatformNodeId::from_str("f958365178aaf234a101db3a34025bdf5f6ed315").unwrap())
);
// From version 3 the platform ports are entries of the extended addresses, not fields.
assert_eq!(payload.platform_p2p_port, None);
assert_eq!(payload.platform_http_port, None);

let MasternodeNetInfo::Extended(net_info) = &payload.service_address else {
panic!("version 3 carries extended addresses, got {:?}", payload.service_address);
};
assert_eq!(
purpose_ports(net_info),
[
(NetInfoPurpose::CoreP2P, 20001),
(NetInfoPurpose::PlatformP2P, 26656),
(NetInfoPurpose::PlatformHttps, 1443),
]
);
assert_eq!(
payload.service_address.primary_service_address(),
Some(SocketAddr::from(([68, 67, 122, 237], 20001)))
);
assert_eq!(net_info.platform_https_port(), Some(1443));
}

/// The port of every service entry in `net_info` with its purpose, in wire order.
fn purpose_ports(net_info: &ExtNetInfo) -> Vec<(NetInfoPurpose, u16)> {
net_info
.purposes
.iter()
.flat_map(|(purpose, entries)| {
entries.iter().map(move |entry| match entry {
NetInfoEntry::Service {
port,
..
} => (*purpose, *port),
other => panic!("expected a service entry, got {other:?}"),
})
})
.collect()
}

#[test]
fn a_pro_up_reg_tx_v3_replaces_a_masternode_payout_through_the_payouts_list() {
let payload = decode_core_tx(PRO_UP_REG_TX, PRO_UP_REG_TX_TXID);
let payload =
payload.to_update_registrar_payload().expect("payload decodes as a typed ProUpRegTx");

assert_eq!(payload.version, 3);
assert!(payload.script_payout.is_empty());
// It updates the masternode the one-payout ProRegTx vector registered.
assert_eq!(payload.pro_tx_hash, Txid::from_str(PRO_REG_TX_ONE_PAYOUT_TXID).unwrap());

// The registration paid yQohnoFhM5avKnzVzAX79xT8uCKviaPxj4; the update moves the whole owner
// reward to another payee.
let payouts = payload.payouts.expect("version 3 has payouts");
assert_eq!(payouts.len(), 1);
assert_eq!(devnet_address(p2pkh_payout(&payouts[0])), "yT7ShTqEqPBskpTAiHzWaAggFmD4j1MCT3");
assert_eq!(payouts[0].reward, WHOLE_OWNER_REWARD);
}

/// Every block decodes with no special payload left raw, and re-encodes transaction by
/// transaction to the bytes Core mined, so the merkle root built from the decoded txids matches
/// the header. Its coinbase carries a version 4 CbTx at the block's height, and every block
/// but the activation block carries the transaction vector mined in it, byte for byte.
#[test_case(include_str!("data/core_v24_devnet/block_15000.hex"), 15000, None; "the V24 activation block")]
#[test_case(include_str!("data/core_v24_devnet/block_15146.hex"), 15146, Some(PRO_REG_TX_ONE_PAYOUT); "the block with the one-payout ProRegTx")]
#[test_case(include_str!("data/core_v24_devnet/block_15153.hex"), 15153, Some(PRO_REG_TX_TWO_PAYOUTS); "the block with the two-payout ProRegTx")]
#[test_case(include_str!("data/core_v24_devnet/block_15166.hex"), 15166, Some(PRO_REG_TX_P2SH_PAYOUT); "the block with the P2SH-payout ProRegTx")]
#[test_case(include_str!("data/core_v24_devnet/block_15182.hex"), 15182, Some(EVO_PRO_UP_SERV_TX); "the block with the Evo ProUpServTx")]
#[test_case(include_str!("data/core_v24_devnet/block_15190.hex"), 15190, Some(PRO_UP_REG_TX); "the block with the ProUpRegTx")]
fn a_core_v24_block_decodes_with_every_transaction_byte_identical(
block_hex: &str,
height: u32,
mined_tx_hex: Option<&str>,
) {
let bytes = hex_bytes(block_hex);
let block: Block = deserialize(&bytes).expect("Core v24 block decodes");

let mut offset = serialize(&block.header).len() + VarInt(block.txdata.len() as u64).len();
for tx in &block.txdata {
let encoded = serialize(tx);
assert_eq!(
bytes.get(offset..offset + encoded.len()),
Some(encoded.as_slice()),
"transaction {} re-encodes differently",
tx.txid()
);
offset += encoded.len();
if let Some(TransactionPayload::RawPayloadType(tx_type, _)) =
&tx.special_transaction_payload
{
panic!("transaction {} kept its type {tx_type} payload raw", tx.txid());
}
}
assert_eq!(offset, bytes.len(), "transactions do not cover the whole block");
assert_eq!(serialize(&block), bytes);
assert!(block.check_merkle_root(), "merkle root does not match the decoded txids");

let coinbase: CoinbasePayload = block
.coinbase()
.and_then(|coinbase| coinbase.special_transaction_payload.clone())
.expect("coinbase has a CbTx payload")
.to_coinbase_payload()
.expect("payload decodes as a typed CbTx");
assert_eq!(coinbase.version, 4);
assert_eq!(coinbase.height, height);
// None of these blocks carries an asset unlock, so the root over them is all-zero.
assert_eq!(coinbase.merkle_root_asset_unlocks, Some(MerkleRootAssetUnlocks::all_zeros()));

if let Some(mined_tx_hex) = mined_tx_hex {
let mined_tx = hex_bytes(mined_tx_hex);
assert!(
block.txdata.iter().any(|tx| serialize(tx) == mined_tx),
"block {height} does not carry the transaction vector mined in it"
);
}
}
1 change: 1 addition & 0 deletions dash/tests/data/core_v24_devnet/block_15000.hex
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
0000002012514e30f56867fb04b03c95841015625ddddbc7012a4e7bdb5fc18dcfdb0909cf04eeeb7aedeeac1f291036d42745c7c2db752752e9c76cbe078793365d76261ea1c76affff7f20010000000303000500010000000000000000000000000000000000000000000000000000000000000000ffffffff016affffffff0300e1f505000000001976a91456208a8adff9433262bffee3c4894d2a160c21bd88ac209db40600000000016ae0052d0b000000001976a91456208a8adff9433262bffee3c4894d2a160c21bd88ac00000000cf0400983a0000bef20047dee38171f51b84fa9913bbe999b8734edb7dbce44b205061102899d1664e5292252f84e74368a648d608ef39c48d22ddc87f08142ef8c87790b5c76f00961d3f3df7fc6acf320236d2e8d9c34f6ecd8ac46adc6df0c18eef92dbd89b4dc0c23251ec18480c1fb56792524cebd311e43baaaa8086dab1a76ce9a910d42bd0e59560bf7ea1d8c28ff25fee66382b3fcf5f0b641c0163eeda91b0598ab3bd2e93a684c0070000000000000000000000000000000000000000000000000000000000000000000003000600000000000000fda3010100983a0000030002f5ec6011210fb9ecc2fa58d73bed67414792c10ddf793f45c1901e72f1db7465fd90010000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000fd90010000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000003000600000000000000fda3010100983a0000030003f5ec6011210fb9ecc2fa58d73bed67414792c10ddf793f45c1901e72f1db7465fd90010000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000fd900100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
1 change: 1 addition & 0 deletions dash/tests/data/core_v24_devnet/block_15146.hex
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
00000020624025135b0936675af8abbe364e7d21c8e1e42eab3f2b6f6528d6ea875dad51e2664e33e00b3bf6b05a67603cddadf9daa4149e9130c7e5180e38426d20a695d5a6c76affff7f20010000000203000500010000000000000000000000000000000000000000000000000000000000000000ffffffff016affffffff036de1f505000000001976a91456208a8adff9433262bffee3c4894d2a160c21bd88ac209db40600000000016a26072d0b000000001976a91456208a8adff9433262bffee3c4894d2a160c21bd88ac00000000cf04002a3b0000db318d0bad75d80a6e14088f71657af817056edee711360d25c729905f33ed502692b21d078be4bc3103156612ba6bdcf387efdd6320a50a97985f5cdce4ead8008df9d40906f1c7c36c545b06daec45a84ffd43b5caf4045c9c9a39a146a7771de6cbdd11f5d16b22e9e358f5c483e35b12a5f3cab190026c5445dec34e632c36a0ba36a1f87bb411d7d292c5fd55cd36128877c707747286e40abec1aee7e4812eb72a56c407000000000000000000000000000000000000000000000000000000000000000000000300010001d02a36181466f1f3ec0f61a73114bdf31fa7f137fa880441d7e5e7b672493c04010000006a473044022004f9bb946ee7fab7d823a812cfb27b576f2c0cda4e597a8c92be26d8af33f155022034d05190a9b7a9aaae89fc89148639af1eb7f0601d9c959232b7a159cb2ca991012102c861a156ca6c6abecdeea95eab3ec7c93c6a37d63a5a6f62368ac94a424af15ffeffffff0200e87648170000001976a914fc52fb4219926f353f5475da7885b213c384a24188ac4d4fbfb88b0000001976a914c39aa109a7f20cefa69079374c7324e01998a12488ac00000000d10300000000000000000000000000000000000000000000000000000000000000000000000000000000000100c1dad84fdab5738de910b64d3a9ae7baf6afbd69806b8889b4ed6ccecefe7861fc3d69b7598f6ab9621fe2568403f94a1c5df398891433cdf301c67fda82f80bfe7f321465e088b8b1360c62946afcbea403cf927d5bfe230000011976a91431389f57b2f32565dcf713d2b2420914885bedcd88ac1027000000000000000000000000004f8f8fe9fd4ad5565cd6c4887bc54e2910a340bb7583c0d8d6a92d52b324095700
1 change: 1 addition & 0 deletions dash/tests/data/core_v24_devnet/block_15153.hex
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
00000020aecd473bc731735ee4f65189ed998c2cdf7af153685380a12e56158bfb5258302fbedf46cb9369460ecef6d630d28ae3115038e402486b14f53c75166e2efa311ba7c76affff7f20040000000203000500010000000000000000000000000000000000000000000000000000000000000000ffffffff016affffffff0374e1f505000000001976a91456208a8adff9433262bffee3c4894d2a160c21bd88ac209db40600000000016a3b072d0b000000001976a91456208a8adff9433262bffee3c4894d2a160c21bd88ac00000000cf0400313b00001beefdffbdc8ba4c53159dee72569b140244965f6e51038b4f5cc78f03926ecd2692b21d078be4bc3103156612ba6bdcf387efdd6320a50a97985f5cdce4ead80090c1f563c2ad4b04f16204c812f14399a37d2868f00c90ef293425f95d4af691e2bdf0d208aaa461579a497c8ffa7e3b128ddfb6900b17b6ed1321b33f51185d9767dab1e0b43cce6f391634e7d2b9baed2e2729d01c35c118e049963852e24c0e031b85c407000000000000000000000000000000000000000000000000000000000000000000000300010001c2075d0bbe8678dea77af146f4c4fdd1ac6bec70333ffef3e7dc940e484c8120010000006a473044022065a0067b55484e06109b4282a8bb14a3392ed76ed0a1195a91a3e3b531098c7f022002aafa428439c858e721664c6b4a71aae2f5bc4ad77bdfc1651a9e8e35428c40012102c861a156ca6c6abecdeea95eab3ec7c93c6a37d63a5a6f62368ac94a424af15ffeffffff0200e87648170000001976a9140b80dfe15263ad7abd2421b82a1291e07d30571c88ac7e654870740000001976a914c39aa109a7f20cefa69079374c7324e01998a12488ac00000000ed03000000000000000000000000000000000000000000000000000000000000000000000000000000000001000c707b9fc7af33ddf9a9cf50ff45fc587f590f5693bec56a034e3bdb3ce1e978d5487d03129c507bd15e73fcb7dac95c7333a68f561450702cd1aac511e40275a501ea07d5b43701d895d09a68803bd67d9c23ab94d4428d0000021976a91494a6df7e4fc8f474b28c634c748d5ddf88b7ee3788ac70171976a91417340a777ca7c690d65d19d2f7aca4b1a0387fa388aca00f000000000000000000000000008576d28cdc5f9022cd2988a05001e5ed2c318854bb3c9d19ce3dcb38ad3ce52e00
1 change: 1 addition & 0 deletions dash/tests/data/core_v24_devnet/block_15166.hex
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
00000020bb3c1469798a37b5b02d4bee8e64d0097ac3a3c67d781a1b47f5783f6b6a1f208a9bbe8d9059a1f3d13ff03b264d22095a081bd7dd7935e74ef280f9dfe2eaaf9da7c76affff7f20010000000203000500010000000000000000000000000000000000000000000000000000000000000000ffffffff016affffffff036de1f505000000001976a91456208a8adff9433262bffee3c4894d2a160c21bd88ac209db40600000000016a24072d0b000000001976a91456208a8adff9433262bffee3c4894d2a160c21bd88ac00000000cf04003e3b0000906748b92fbd1eedde34683b6d0de5a07f2ef17f2e360010bb494f1b97c4e08adb67a987dca290d49310f2e30d411e7302d605505d7b9679199b3966f2da8d74008280e8ad04186510b99bc596dd191a5114dfc47f6520ec0d14f3bc829bc6e6a2e5d54511780ae8f690822ca97b8f4be8056a464d76ea5617b396f44723080575f28f629fe27eec0994c8dfe71bd51c05976438b551008fa1f7f2944473fc221caefd46dcc407000000000000000000000000000000000000000000000000000000000000000000000300010001db8ab3088eed7b50e53a1f9ca2951bafc0745d0e718b0a414ee34a08d0fae6f7010000006a4730440220433c544395783b738a418c8b16f0368f13d56e9c56af3d7595e8eb61d41b0a650220771f2132348b28be1b853e7000ea6fa5d5f0d5913dac7bfc07b8f5c8a20d19f5012102c861a156ca6c6abecdeea95eab3ec7c93c6a37d63a5a6f62368ac94a424af15ffeffffff0200e87648170000001976a9140d1b303302b3a691a912fc9b0f2bd4f33f70816488accd7bd1275d0000001976a914c39aa109a7f20cefa69079374c7324e01998a12488ac00000000cf03000000000000000000000000000000000000000000000000000000000000000000000000000000000001003a02101abc24108438ebe3af704fd64a658fef6bb96d7c328cb02753c2487a780ef8401d621ad5c505beeba3ba5958984fab293bfe411fe9cfeba617ae9b7ada2002c32d9dd47e671a9f43fec688085b7cc462a0e594a26200000117a914d675f38f31792b691cc7c724d8dc44134c710e7e8710270000000000000000000000000070dc754d77ae0cb2034f079c87a764f47f1aaa16afa64baa6aa6df335cb379a800
Loading
Loading