forked from mapprotocol/atlas
-
Notifications
You must be signed in to change notification settings - Fork 0
/
types.go
357 lines (300 loc) · 9.19 KB
/
types.go
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
package eth2
import (
"bytes"
"github.com/ethereum/go-ethereum/common"
fssz "github.com/prysmaticlabs/fastssz"
ssz "github.com/prysmaticlabs/fastssz"
"github.com/prysmaticlabs/go-bitfield"
"google.golang.org/protobuf/runtime/protoimpl"
"math/big"
)
const BLSPubkeyLength = 48
type ForkVersion [4]byte
type DomainType [4]byte
type Root []byte
type ValidatorIndex uint64
type ILightClientUpdate interface {
GetAttestedHeader() *BeaconBlockHeader
GetNextSyncCommittee() *SyncCommittee
GetNextSyncCommitteeBranch() [][]byte
GetFinalizedHeader() *BeaconBlockHeader
GetFinalityBranch() [][]byte
GetSyncAggregate() *SyncAggregate
GetSignatureSlot() uint64
}
type LightClientState struct {
// Beacon block header that is finalized
finalizedHeader *BeaconBlockHeader
// Sync committees corresponding to the header
currentSyncCommittee *SyncCommittee
nextSyncCommittee *SyncCommittee
chainID uint64
}
type LightClientVerify struct {
update ILightClientUpdate
state *LightClientState
}
type BeaconBlockHeader struct {
Slot uint64
ProposerIndex ValidatorIndex
ParentRoot []byte
StateRoot []byte
BodyRoot []byte
}
type ExecutionPayload struct {
ParentHash common.Hash
FeeRecipient common.Address
StateRoot common.Hash
ReceiptsRoot common.Hash
LogsBloom []byte
PrevRandao common.Hash
BlockNumber *big.Int
GasLimit uint64
GasUsed uint64
Timestamp uint64
ExtraData []byte
BaseFeePerGas *big.Int
BlockHash common.Hash
TransactionsRoot common.Hash
WithdrawalsRoot common.Hash
}
// HashTreeRoot ssz hashes the BeaconBlockHeader object
func (b *BeaconBlockHeader) HashTreeRoot() ([32]byte, error) {
return ssz.HashWithDefaultHasher(b)
}
// HashTreeRootWith ssz hashes the BeaconBlockHeader object with a hasher
func (b *BeaconBlockHeader) HashTreeRootWith(hh *ssz.Hasher) (err error) {
indx := hh.Index()
// Field (0) 'Slot'
hh.PutUint64(b.Slot)
// Field (1) 'ProposerIndex'
hh.PutUint64(uint64(b.ProposerIndex))
// Field (2) 'ParentRoot'
if size := len(b.ParentRoot); size != 32 {
err = ssz.ErrBytesLengthFn("--.ParentRoot", size, 32)
return
}
hh.PutBytes(b.ParentRoot)
// Field (3) 'StateRoot'
if size := len(b.StateRoot); size != 32 {
err = ssz.ErrBytesLengthFn("--.StateRoot", size, 32)
return
}
hh.PutBytes(b.StateRoot)
// Field (4) 'BodyRoot'
if size := len(b.BodyRoot); size != 32 {
err = ssz.ErrBytesLengthFn("--.BodyRoot", size, 32)
return
}
hh.PutBytes(b.BodyRoot)
if ssz.EnableVectorizedHTR {
hh.MerkleizeVectorizedHTR(indx)
} else {
hh.Merkleize(indx)
}
return
}
type SyncCommittee struct {
Pubkeys [][]byte
AggregatePubkey []byte
}
type SyncAggregate struct {
SyncCommitteeBits bitfield.Bitvector512
SyncCommitteeSignature []byte
}
// HashTreeRoot ssz hashes the ExecutionPayload object
func (e *ExecutionPayload) HashTreeRoot() ([32]byte, error) {
return fssz.HashWithDefaultHasher(e)
}
// HashTreeRootWith ssz hashes the ExecutionPayload object with a hasher
func (e *ExecutionPayload) HashTreeRootWith(hh *fssz.Hasher) (err error) {
indx := hh.Index()
// Field (0) 'ParentHash'
hh.PutBytes(e.ParentHash.Bytes())
// Field (1) 'FeeRecipient'
hh.PutBytes(e.FeeRecipient.Bytes())
// Field (2) 'StateRoot'
hh.PutBytes(e.StateRoot.Bytes())
// Field (3) 'ReceiptsRoot'
hh.PutBytes(e.ReceiptsRoot.Bytes())
// Field (4) 'LogsBloom'
if size := len(e.LogsBloom); size != 256 {
err = fssz.ErrBytesLengthFn("--.LogsBloom", size, 256)
return
}
hh.PutBytes(e.LogsBloom)
// Field (5) 'PrevRandao'
hh.PutBytes(e.PrevRandao.Bytes())
// Field (6) 'BlockNumber'
hh.PutUint64(e.BlockNumber.Uint64())
// Field (7) 'GasLimit'
hh.PutUint64(e.GasLimit)
// Field (8) 'GasUsed'
hh.PutUint64(e.GasUsed)
// Field (9) 'Timestamp'
hh.PutUint64(e.Timestamp)
// Field (10) 'ExtraData'
{
elemIndx := hh.Index()
byteLen := uint64(len(e.ExtraData))
if byteLen > 32 {
err = fssz.ErrIncorrectListSize
return
}
hh.PutBytes(e.ExtraData)
if fssz.EnableVectorizedHTR {
hh.MerkleizeWithMixinVectorizedHTR(elemIndx, byteLen, (32+31)/32)
} else {
hh.MerkleizeWithMixin(elemIndx, byteLen, (32+31)/32)
}
}
// Field (11) 'BaseFeePerGas'
hh.PutBytes(PadTo(ReverseByteOrder(e.BaseFeePerGas.Bytes()), 32))
// Field (12) 'BlockHash'
hh.PutBytes(e.BlockHash.Bytes())
// Field (13) 'Transactions'
hh.PutBytes(e.TransactionsRoot.Bytes())
// Field (14) 'WithdrawalsRoot'
if !bytes.Equal(e.WithdrawalsRoot[:], make([]byte, 32)) {
hh.PutBytes(e.WithdrawalsRoot.Bytes())
}
if fssz.EnableVectorizedHTR {
hh.MerkleizeVectorizedHTR(indx)
} else {
hh.Merkleize(indx)
}
return
}
type ForkData struct {
CurrentVersion []byte
GenesisValidatorsRoot []byte
}
func (f *ForkData) HashTreeRootWith(hh *ssz.Hasher) (err error) {
indx := hh.Index()
// Field (0) 'CurrentVersion'
if size := len(f.CurrentVersion); size != 4 {
err = ssz.ErrBytesLengthFn("--.CurrentVersion", size, 4)
return
}
hh.PutBytes(f.CurrentVersion)
// Field (1) 'GenesisValidatorsRoot'
if size := len(f.GenesisValidatorsRoot); size != 32 {
err = ssz.ErrBytesLengthFn("--.GenesisValidatorsRoot", size, 32)
return
}
hh.PutBytes(f.GenesisValidatorsRoot)
if ssz.EnableVectorizedHTR {
hh.MerkleizeVectorizedHTR(indx)
} else {
hh.Merkleize(indx)
}
return
}
// HashTreeRoot ssz hashes the ForkData object
func (f *ForkData) HashTreeRoot() ([32]byte, error) {
return ssz.HashWithDefaultHasher(f)
}
type SigningData struct {
state protoimpl.MessageState
sizeCache protoimpl.SizeCache
unknownFields protoimpl.UnknownFields
ObjectRoot []byte `protobuf:"bytes,1,opt,name=object_root,json=objectRoot,proto3" json:"object_root,omitempty" ssz-size:"32"`
Domain []byte `protobuf:"bytes,2,opt,name=domain,proto3" json:"domain,omitempty" ssz-size:"32"`
}
// HashTreeRoot ssz hashes the SigningData object
func (s *SigningData) HashTreeRoot() ([32]byte, error) {
return ssz.HashWithDefaultHasher(s)
}
// HashTreeRootWith ssz hashes the SigningData object with a hasher
func (s *SigningData) HashTreeRootWith(hh *ssz.Hasher) (err error) {
indx := hh.Index()
// Field (0) 'ObjectRoot'
if size := len(s.ObjectRoot); size != 32 {
err = ssz.ErrBytesLengthFn("--.ObjectRoot", size, 32)
return
}
hh.PutBytes(s.ObjectRoot)
// Field (1) 'Domain'
if size := len(s.Domain); size != 32 {
err = ssz.ErrBytesLengthFn("--.Domain", size, 32)
return
}
hh.PutBytes(s.Domain)
if ssz.EnableVectorizedHTR {
hh.MerkleizeVectorizedHTR(indx)
} else {
hh.Merkleize(indx)
}
return
}
// ILightNodeBeaconBlockHeader is an auto generated low-level Go binding around an user-defined struct.
type ILightNodeBeaconBlockHeader struct {
Slot uint64
ProposerIndex uint64
ParentRoot [32]byte
StateRoot [32]byte
BodyRoot [32]byte
}
// ILightNodeSyncAggregate is an auto generated low-level Go binding around an user-defined struct.
type ILightNodeSyncAggregate struct {
SyncCommitteeBits []byte
SyncCommitteeSignature []byte
}
// ILightNodeSyncCommittee is an auto generated low-level Go binding around an user-defined struct.
type ILightNodeSyncCommittee struct {
Pubkeys []byte
AggregatePubkey []byte
}
func (header *ILightNodeBeaconBlockHeader) toBeaconBlockHeader() *BeaconBlockHeader {
return &BeaconBlockHeader{
Slot: header.Slot,
ProposerIndex: ValidatorIndex(header.ProposerIndex),
ParentRoot: header.ParentRoot[:],
StateRoot: header.StateRoot[:],
BodyRoot: header.BodyRoot[:],
}
}
func (execution *ILightNodeExecution) toExecutionPayload() *ExecutionPayload {
return &ExecutionPayload{
ParentHash: common.BytesToHash(execution.ParentHash[:]),
FeeRecipient: common.BytesToAddress(execution.FeeRecipient[:]),
StateRoot: common.BytesToHash(execution.StateRoot[:]),
ReceiptsRoot: common.BytesToHash(execution.ReceiptsRoot[:]),
LogsBloom: execution.LogsBloom,
PrevRandao: common.BytesToHash(execution.PrevRandao[:]),
BlockNumber: execution.BlockNumber,
GasLimit: execution.GasLimit.Uint64(),
GasUsed: execution.GasUsed.Uint64(),
Timestamp: execution.Timestamp.Uint64(),
ExtraData: execution.ExtraData,
BaseFeePerGas: execution.BaseFeePerGas,
BlockHash: common.BytesToHash(execution.BlockHash[:]),
TransactionsRoot: common.BytesToHash(execution.TransactionsRoot[:]),
WithdrawalsRoot: common.BytesToHash(execution.WithdrawalsRoot[:]),
}
}
func (syncCommittee *ILightNodeSyncCommittee) toSyncCommittee() *SyncCommittee {
var pubkeys [][]byte
count := len(syncCommittee.Pubkeys) / BLSPubkeyLength
for i := 0; i < count; i++ {
pubkeys = append(pubkeys, syncCommittee.Pubkeys[i*BLSPubkeyLength:(i+1)*BLSPubkeyLength])
}
return &SyncCommittee{
Pubkeys: pubkeys,
AggregatePubkey: syncCommittee.AggregatePubkey,
}
}
func (syncAggregate *ILightNodeSyncAggregate) toSyncAggregate() *SyncAggregate {
return &SyncAggregate{
SyncCommitteeBits: syncAggregate.SyncCommitteeBits,
SyncCommitteeSignature: syncAggregate.SyncCommitteeSignature,
}
}
func bytes32ArrayToBytesArray(input [][32]byte) [][]byte {
var output [][]byte
for _, item := range input {
itemCopy := item
output = append(output, itemCopy[:])
}
return output
}