-
Notifications
You must be signed in to change notification settings - Fork 153
Expand file tree
/
Copy pathapi_electra.go
More file actions
128 lines (107 loc) · 3.52 KB
/
api_electra.go
File metadata and controls
128 lines (107 loc) · 3.52 KB
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
package api
import (
"math/big"
"github.com/snowfork/go-substrate-rpc-client/v4/types"
"github.com/snowfork/snowbridge/relayer/relays/beacon/header/syncer/scale"
"github.com/snowfork/snowbridge/relayer/relays/beacon/state"
"github.com/snowfork/snowbridge/relayer/relays/util"
)
func ElectraExecutionPayloadToScale(e *state.ExecutionPayloadElectra) (scale.ExecutionPayloadHeaderDeneb, error) {
var payloadHeader scale.ExecutionPayloadHeaderDeneb
transactionsContainer := state.TransactionsRootContainer{}
transactionsContainer.Transactions = e.Transactions
transactionsRoot, err := transactionsContainer.HashTreeRoot()
if err != nil {
return payloadHeader, err
}
var withdrawalRoot types.H256
withdrawalContainer := state.WithdrawalsRootContainerMainnet{}
withdrawalContainer.Withdrawals = e.Withdrawals
withdrawalRoot, err = withdrawalContainer.HashTreeRoot()
if err != nil {
return payloadHeader, err
}
baseFeePerGas := big.Int{}
// Change BaseFeePerGas back from little-endian to big-endian
baseFeePerGas.SetBytes(util.ChangeByteOrder(e.BaseFeePerGas[:]))
return scale.ExecutionPayloadHeaderDeneb{
ParentHash: types.NewH256(e.ParentHash[:]),
FeeRecipient: e.FeeRecipient,
StateRoot: types.NewH256(e.StateRoot[:]),
ReceiptsRoot: types.NewH256(e.ReceiptsRoot[:]),
LogsBloom: e.LogsBloom[:],
PrevRandao: types.NewH256(e.PrevRandao[:]),
BlockNumber: types.NewU64(e.BlockNumber),
GasLimit: types.NewU64(e.GasLimit),
GasUsed: types.NewU64(e.GasUsed),
Timestamp: types.NewU64(e.Timestamp),
ExtraData: e.ExtraData,
BaseFeePerGas: types.NewU256(baseFeePerGas),
BlockHash: types.NewH256(e.BlockHash[:]),
TransactionsRoot: transactionsRoot,
WithdrawalsRoot: withdrawalRoot,
BlobGasUsed: types.NewU64(e.BlobGasUsed),
ExcessBlobGas: types.NewU64(e.ExcessBlobGas),
}, nil
}
func (a AttesterSlashingResponse) ToFastSSZElectra() (*state.AttesterSlashingElectra, error) {
attestation1, err := a.Attestation1.ToFastSSZElectra()
if err != nil {
return nil, err
}
attestation2, err := a.Attestation2.ToFastSSZElectra()
if err != nil {
return nil, err
}
return &state.AttesterSlashingElectra{
Attestation1: attestation1,
Attestation2: attestation2,
}, nil
}
func (i IndexedAttestationResponse) ToFastSSZElectra() (*state.IndexedAttestationElectra, error) {
data, err := i.Data.ToFastSSZ()
if err != nil {
return nil, err
}
attestationIndexes := []uint64{}
for _, index := range i.AttestingIndices {
indexInt, err := util.ToUint64(index)
if err != nil {
return nil, err
}
attestationIndexes = append(attestationIndexes, indexInt)
}
signature, err := util.HexStringToByteArray(i.Signature)
if err != nil {
return nil, err
}
return &state.IndexedAttestationElectra{
AttestationIndices: attestationIndexes,
Data: data,
Signature: signature,
}, nil
}
func (a AttestationResponse) ToFastSSZElectra() (*state.AttestationElectra, error) {
data, err := a.Data.ToFastSSZ()
if err != nil {
return nil, err
}
aggregationBits, err := util.HexStringToByteArray(a.AggregationBits)
if err != nil {
return nil, err
}
signature, err := util.HexStringTo96Bytes(a.Signature)
if err != nil {
return nil, err
}
committeeBits, err := util.HexStringToByteArray(a.CommitteeBits)
if err != nil {
return nil, err
}
return &state.AttestationElectra{
AggregationBits: aggregationBits,
Data: data,
Signature: signature,
CommitteeBits: committeeBits,
}, nil
}