@@ -20,7 +20,7 @@ pub fn computeDomain(domain_type: DomainType, fork_version: Version, genesis_val
2020
2121/// Return the ForkVersion at an epoch from a Fork type
2222pub fn forkVersion (fork : Fork , epoch : Epoch ) Version {
23- return if (epoch < fork .epoch ) fork .previousVersion else fork .currentVersion ;
23+ return if (epoch < fork .epoch ) fork .previous_version else fork .current_version ;
2424}
2525
2626/// Used primarily in signature domains to avoid collisions across forks/chains.
@@ -31,3 +31,71 @@ pub fn computeForkDataRoot(current_version: Version, genesis_validators_root: Ro
3131 };
3232 try types .phase0 .ForkData .hashTreeRoot (& fork_data , out );
3333}
34+
35+ const testing = std .testing ;
36+ const constants = @import ("constants" );
37+ const DOMAIN_BEACON_PROPOSER = constants .DOMAIN_BEACON_PROPOSER ;
38+ const DOMAIN_BEACON_ATTESTER = constants .DOMAIN_BEACON_ATTESTER ;
39+ const DOMAIN_VOLUNTARY_EXIT = constants .DOMAIN_VOLUNTARY_EXIT ;
40+
41+ test "computeDomain - domain type is first 4 bytes" {
42+ const fork_version = [4 ]u8 { 0x01 , 0x00 , 0x00 , 0x00 };
43+ const genesis_root = [_ ]u8 {0 } ** 32 ;
44+ var domain : Domain = undefined ;
45+
46+ try computeDomain (DOMAIN_VOLUNTARY_EXIT , fork_version , genesis_root , & domain );
47+
48+ try testing .expectEqualSlices (u8 , & DOMAIN_VOLUNTARY_EXIT , domain [0.. 4]);
49+ }
50+
51+ test "computeDomain - different domain types produce different domains" {
52+ const fork_version = [4 ]u8 { 0x01 , 0x00 , 0x00 , 0x00 };
53+ const genesis_root = [_ ]u8 {0xAA } ** 32 ;
54+
55+ var domain_a : Domain = undefined ;
56+ var domain_b : Domain = undefined ;
57+
58+ try computeDomain (DOMAIN_BEACON_PROPOSER , fork_version , genesis_root , & domain_a );
59+ try computeDomain (DOMAIN_BEACON_ATTESTER , fork_version , genesis_root , & domain_b );
60+
61+ try testing .expect (! std .mem .eql (u8 , & domain_a , & domain_b ));
62+ }
63+
64+ test "computeDomain - different fork versions produce different domains" {
65+ const genesis_root = [_ ]u8 {0xBB } ** 32 ;
66+
67+ var domain_a : Domain = undefined ;
68+ var domain_b : Domain = undefined ;
69+ const version_a = [4 ]u8 { 0x01 , 0x00 , 0x00 , 0x00 };
70+ const version_b = [4 ]u8 { 0x02 , 0x00 , 0x00 , 0x00 };
71+
72+ try computeDomain (DOMAIN_BEACON_PROPOSER , version_a , genesis_root , & domain_a );
73+ try computeDomain (DOMAIN_BEACON_PROPOSER , version_b , genesis_root , & domain_b );
74+
75+ // First 4 bytes (domain type) are the same
76+ try testing .expectEqualSlices (u8 , domain_a [0.. 4], domain_b [0.. 4]);
77+ // But the fork data root portion differs
78+ try testing .expect (! std .mem .eql (u8 , domain_a [4.. 32], domain_b [4.. 32]));
79+ }
80+
81+ test "forkVersion - given epoch returns correct version" {
82+ const fork : Fork = .{
83+ .previous_version = [4 ]u8 { 0x01 , 0x00 , 0x00 , 0x00 },
84+ .current_version = [4 ]u8 { 0x02 , 0x00 , 0x00 , 0x00 },
85+ .epoch = 100 ,
86+ };
87+ try testing .expectEqualSlices (u8 , & fork .previous_version , & forkVersion (fork , 99 ));
88+ try testing .expectEqualSlices (u8 , & fork .current_version , & forkVersion (fork , 100 ));
89+ try testing .expectEqualSlices (u8 , & fork .current_version , & forkVersion (fork , 200 ));
90+ }
91+
92+ test "computeForkDataRoot - different inputs produce different roots" {
93+ const genesis_root = [_ ]u8 {0xDD } ** 32 ;
94+ var root_a : Root = undefined ;
95+ var root_b : Root = undefined ;
96+
97+ try computeForkDataRoot ([4 ]u8 { 0x01 , 0x00 , 0x00 , 0x00 }, genesis_root , & root_a );
98+ try computeForkDataRoot ([4 ]u8 { 0x02 , 0x00 , 0x00 , 0x00 }, genesis_root , & root_b );
99+
100+ try testing .expect (! std .mem .eql (u8 , & root_a , & root_b ));
101+ }
0 commit comments