This repository was archived by the owner on Mar 3, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathBeefyCommitmentScheme.sol
130 lines (119 loc) · 3.89 KB
/
BeefyCommitmentScheme.sol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
// SPDX-License-Identifier: MIT
pragma solidity >=0.6.0 <0.7.0;
pragma experimental ABIEncoderV2;
contract BeefyCommitmentScheme {
/**
* Next BEEFY authority set
* @param id ID of the next set
* @param len Number of validators in the set
* @param root Merkle Root Hash build from BEEFY AuthorityIds
*/
struct NextValidatorSet {
uint64 id;
uint32 len;
bytes32 root;
}
/**
* The payload being signed
* @param network Source chain network identifier
* @param mmr MMR root hash
* @param messageRoot Darwnia message root commitment hash
* @param nextValidatorSet Next BEEFY authority set
*/
struct Payload {
bytes32 network;
bytes32 mmr;
bytes32 messageRoot;
NextValidatorSet nextValidatorSet;
}
/**
* The Commitment, with its payload, is the core thing we are trying to verify with this contract.
* It contains a next validator set or not and a MMR root that commits to the darwinia history,
* including past blocks and can be used to verify darwinia blocks.
* @param payload the payload of the new commitment in beefy justifications (in
* our case, this is a next validator set and a new MMR root for all past darwinia blocks)
* @param blockNumber block number for the given commitment
* @param validatorSetId validator set id that signed the given commitment
*/
struct Commitment {
Payload payload;
uint32 blockNumber;
uint64 validatorSetId;
}
/**
* Hash of the NextValidatorSet Schema
* keccak256("NextValidatorSet(uint64 id,uint32 len,bytes32 root)")
*/
bytes32 internal constant NEXTVALIDATORSET_TYPEHASH = 0x599882aa3cf9166c2c8867b0e7c41899bd7c26ee7898f261a5f495738da7dbd0;
/**
* Hash of the Payload Schema
* keccak256(abi.encodePacked(
* "Payload(bytes32 network,bytes32 mmr,bytes32 messageRoot,NextValidatorSet nextValidatorSet)",
* "NextValidatorSet(uint64 id,uint32 len,bytes32 root)",
* ")"
* )
*/
bytes32 internal constant PAYLOAD_TYPEHASH = 0x62bbbb2624ffde1ec395c5f7f00ec3bec6217d975467b8deaf45d8dc276236a5;
/**
* Hash of the Commitment Schema
* keccak256(abi.encodePacked(
* "Commitment(Payload payload,uint32 blockNumber,uint64 validatorSetId)",
* "Payload(bytes32 network,bytes32 mmr,bytes32 messageRoot,NextValidatorSet nextValidatorSet)",
* "NextValidatorSet(uint64 id,uint32 len,bytes32 root)",
* ")"
* )
*/
bytes32 internal constant COMMITMENT_TYPEHASH = 0xb962b25b1a6ae67dc9886e336d7136273db7f78be39c3b3a86664187b2807317;
function hash(Commitment memory commitment)
public
pure
returns (bytes32)
{
/**
* Encode and hash the Commitment
*/
return keccak256(
abi.encode(
COMMITMENT_TYPEHASH,
hash(commitment.payload),
commitment.blockNumber,
commitment.validatorSetId
)
);
}
function hash(Payload memory payload)
internal
pure
returns (bytes32)
{
/**
* Encode and hash the Payload
*/
return keccak256(
abi.encode(
PAYLOAD_TYPEHASH,
payload.network,
payload.mmr,
payload.messageRoot,
hash(payload.nextValidatorSet)
)
);
}
function hash(NextValidatorSet memory nextValidatorSet)
internal
pure
returns (bytes32)
{
/**
* Encode and hash the NextValidatorSet
*/
return keccak256(
abi.encode(
NEXTVALIDATORSET_TYPEHASH,
nextValidatorSet.id,
nextValidatorSet.len,
nextValidatorSet.root
)
);
}
}