Skip to content

Commit

Permalink
Add independent test-vector for merkle root calculation
Browse files Browse the repository at this point in the history
  • Loading branch information
jakubtrnka authored and plebhash committed Jan 29, 2025
1 parent fce7fcf commit e8e8a14
Showing 1 changed file with 78 additions and 0 deletions.
78 changes: 78 additions & 0 deletions protocols/v2/roles-logic-sv2/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -607,6 +607,84 @@ fn test_group_id_new_into_group_id() {
assert!(channel_id == channel_from_complete);
}

#[test]
fn test_merkle_root_independent_vector() {
const REFERENCE_MERKLE_ROOT: [u8; 32] = [
28, 204, 213, 73, 250, 160, 146, 15, 5, 127, 9, 214, 204, 20, 164, 199, 20, 181, 26, 190,
236, 91, 40, 225, 128, 239, 213, 148, 232, 77, 4, 36,
];
const BRANCH: &[[u8; 32]] = &[
[
224, 195, 140, 86, 17, 172, 9, 61, 54, 73, 215, 202, 109, 83, 124, 163, 215, 78, 143,
204, 44, 242, 242, 122, 37, 106, 55, 81, 58, 234, 27, 210,
],
[
35, 10, 232, 246, 235, 117, 56, 190, 87, 77, 81, 11, 159, 79, 90, 62, 91, 52, 41, 49,
57, 245, 219, 122, 115, 223, 199, 229, 238, 60, 47, 144,
],
[
95, 18, 132, 87, 213, 76, 188, 74, 245, 106, 18, 149, 106, 32, 209, 158, 239, 3, 17,
26, 207, 230, 118, 149, 120, 48, 96, 66, 214, 150, 137, 220,
],
[
205, 167, 106, 179, 82, 50, 157, 76, 91, 36, 54, 226, 34, 183, 162, 179, 109, 64, 185,
207, 103, 192, 63, 31, 141, 126, 34, 30, 68, 69, 154, 176,
],
[
251, 236, 76, 1, 218, 98, 98, 236, 144, 52, 151, 246, 95, 13, 109, 240, 240, 195, 64,
157, 7, 142, 28, 242, 29, 123, 51, 93, 51, 36, 143, 148,
],
[
35, 146, 105, 130, 188, 39, 97, 252, 75, 229, 185, 148, 242, 106, 164, 112, 123, 66,
34, 95, 218, 203, 50, 203, 129, 208, 109, 220, 112, 228, 121, 160,
],
[
44, 55, 125, 47, 249, 213, 175, 143, 140, 50, 219, 72, 111, 71, 125, 54, 85, 70, 4, 85,
60, 92, 208, 35, 113, 245, 128, 139, 228, 4, 230, 177,
],
[
169, 119, 48, 178, 205, 188, 19, 220, 85, 29, 174, 45, 158, 172, 222, 238, 170, 144,
79, 140, 56, 90, 105, 187, 204, 145, 241, 96, 75, 88, 6, 133,
],
[
72, 202, 11, 90, 167, 140, 253, 12, 58, 85, 223, 17, 82, 112, 24, 129, 186, 39, 224,
171, 227, 192, 14, 167, 154, 248, 150, 55, 114, 169, 43, 17,
],
];
const CB_PREFIX: &[u8] = &[
1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 255, 255, 255, 255, 75, 3, 139, 133, 11, 250, 190, 109, 109, 43, 220,
215, 96, 154, 211, 18, 14, 53, 53, 0, 95, 132, 159, 127, 54, 197, 70, 135, 74, 17, 149, 12,
104, 133, 16, 182, 152, 109, 207, 13, 9, 1, 0, 0, 0, 0, 0, 0, 0,
];
const CB_SUFFIX: &[u8] = &[
89, 236, 29, 54, 20, 47, 115, 108, 117, 115, 104, 47, 0, 0, 0, 0, 3, 236, 42, 86, 37, 0, 0,
0, 0, 25, 118, 169, 20, 124, 21, 78, 209, 220, 89, 96, 158, 61, 38, 171, 178, 223, 46, 163,
213, 135, 205, 140, 65, 136, 172, 0, 0, 0, 0, 0, 0, 0, 0, 44, 106, 76, 41, 82, 83, 75, 66,
76, 79, 67, 75, 58, 155, 83, 3, 23, 69, 4, 30, 18, 212, 34, 33, 76, 167, 101, 132, 91, 1,
127, 124, 85, 238, 57, 118, 135, 107, 35, 25, 33, 0, 71, 6, 88, 0, 0, 0, 0, 0, 0, 0, 0, 38,
106, 36, 170, 33, 169, 237, 123, 170, 130, 253, 191, 130, 150, 16, 0, 18, 157, 2, 231, 33,
177, 230, 137, 182, 134, 51, 32, 216, 181, 6, 73, 60, 103, 211, 194, 61, 77, 64, 0, 0, 0,
0,
];
const EXTRANONCE_PREFIX: &[u8] = &[41, 101, 8, 3, 39, 21, 251];
const EXTRANONCE: &[u8] = &[165, 6, 238, 7, 139, 252, 22, 7];

let full_extranonce = {
let mut xn = EXTRANONCE_PREFIX.to_vec();
xn.extend_from_slice(EXTRANONCE);
xn
};

let calculated_merkle_root =
merkle_root_from_path(CB_PREFIX, CB_SUFFIX, &full_extranonce, BRANCH)
.expect("Ultimate failure. Merkle root calculator returned None");
assert_eq!(
calculated_merkle_root, REFERENCE_MERKLE_ROOT,
"Merkle root does not match reference"
)
}

#[test]
fn test_merkle_root_from_path() {
let coinbase_bytes = vec![
Expand Down

0 comments on commit e8e8a14

Please sign in to comment.