-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcapella.go
167 lines (154 loc) · 6.52 KB
/
capella.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
package relay_grpc
import (
"fmt"
"github.com/attestantio/go-builder-client/api/capella"
v1 "github.com/attestantio/go-builder-client/api/v1"
consensusspec "github.com/attestantio/go-eth2-client/spec"
"github.com/attestantio/go-eth2-client/spec/bellatrix"
consensus "github.com/attestantio/go-eth2-client/spec/capella"
"github.com/attestantio/go-eth2-client/spec/phase0"
"github.com/holiman/uint256"
)
func CapellaRequestToProtoRequest(block *capella.SubmitBlockRequest) *SubmitBlockRequest {
transactions := []*CompressTx{}
for _, tx := range block.ExecutionPayload.Transactions {
transactions = append(transactions, &CompressTx{
RawData: tx,
ShortID: 0,
})
}
withdrawals := []*Withdrawal{}
for _, withdrawal := range block.ExecutionPayload.Withdrawals {
withdrawals = append(withdrawals, &Withdrawal{
ValidatorIndex: uint64(withdrawal.ValidatorIndex),
Index: uint64(withdrawal.Index),
Amount: uint64(withdrawal.Amount),
Address: withdrawal.Address[:],
})
}
return &SubmitBlockRequest{
Version: uint64(consensusspec.DataVersionCapella),
BidTrace: &BidTrace{
Slot: block.Message.Slot,
ParentHash: block.Message.ParentHash[:],
BlockHash: block.Message.BlockHash[:],
BuilderPubkey: block.Message.BuilderPubkey[:],
ProposerPubkey: block.Message.ProposerPubkey[:],
ProposerFeeRecipient: block.Message.ProposerFeeRecipient[:],
GasLimit: block.Message.GasLimit,
GasUsed: block.Message.GasUsed,
Value: block.Message.Value.Hex(),
},
ExecutionPayload: &ExecutionPayload{
ParentHash: block.ExecutionPayload.ParentHash[:],
StateRoot: block.ExecutionPayload.StateRoot[:],
ReceiptsRoot: block.ExecutionPayload.ReceiptsRoot[:],
LogsBloom: block.ExecutionPayload.LogsBloom[:],
PrevRandao: block.ExecutionPayload.PrevRandao[:],
BaseFeePerGas: block.ExecutionPayload.BaseFeePerGas[:],
FeeRecipient: block.ExecutionPayload.FeeRecipient[:],
BlockHash: block.ExecutionPayload.BlockHash[:],
ExtraData: block.ExecutionPayload.ExtraData,
BlockNumber: block.ExecutionPayload.BlockNumber,
GasLimit: block.ExecutionPayload.GasLimit,
Timestamp: block.ExecutionPayload.Timestamp,
GasUsed: block.ExecutionPayload.GasUsed,
Transactions: transactions,
Withdrawals: withdrawals,
},
Signature: block.Signature[:],
}
}
func CapellaRequestToProtoRequestWithShortIDs(block *capella.SubmitBlockRequest, compressTxs []*CompressTx) *SubmitBlockRequest {
withdrawals := []*Withdrawal{}
for _, withdrawal := range block.ExecutionPayload.Withdrawals {
withdrawals = append(withdrawals, &Withdrawal{
ValidatorIndex: uint64(withdrawal.ValidatorIndex),
Index: uint64(withdrawal.Index),
Amount: uint64(withdrawal.Amount),
Address: withdrawal.Address[:],
})
}
return &SubmitBlockRequest{
Version: uint64(consensusspec.DataVersionCapella),
BidTrace: &BidTrace{
Slot: block.Message.Slot,
ParentHash: block.Message.ParentHash[:],
BlockHash: block.Message.BlockHash[:],
BuilderPubkey: block.Message.BuilderPubkey[:],
ProposerPubkey: block.Message.ProposerPubkey[:],
ProposerFeeRecipient: block.Message.ProposerFeeRecipient[:],
GasLimit: block.Message.GasLimit,
GasUsed: block.Message.GasUsed,
Value: block.Message.Value.Hex(),
},
ExecutionPayload: &ExecutionPayload{
ParentHash: block.ExecutionPayload.ParentHash[:],
StateRoot: block.ExecutionPayload.StateRoot[:],
ReceiptsRoot: block.ExecutionPayload.ReceiptsRoot[:],
LogsBloom: block.ExecutionPayload.LogsBloom[:],
PrevRandao: block.ExecutionPayload.PrevRandao[:],
BaseFeePerGas: block.ExecutionPayload.BaseFeePerGas[:],
FeeRecipient: block.ExecutionPayload.FeeRecipient[:],
BlockHash: block.ExecutionPayload.BlockHash[:],
ExtraData: block.ExecutionPayload.ExtraData,
BlockNumber: block.ExecutionPayload.BlockNumber,
GasLimit: block.ExecutionPayload.GasLimit,
Timestamp: block.ExecutionPayload.Timestamp,
GasUsed: block.ExecutionPayload.GasUsed,
Transactions: compressTxs,
Withdrawals: withdrawals,
},
Signature: block.Signature[:],
}
}
func ProtoRequestToCapellaRequest(block *SubmitBlockRequest) (*capella.SubmitBlockRequest, error) {
transactions := []bellatrix.Transaction{}
for _, tx := range block.ExecutionPayload.Transactions {
transactions = append(transactions, tx.RawData)
}
withdrawals := []*consensus.Withdrawal{}
for _, withdrawal := range block.ExecutionPayload.Withdrawals {
withdrawals = append(withdrawals, &consensus.Withdrawal{
ValidatorIndex: phase0.ValidatorIndex(withdrawal.ValidatorIndex),
Index: consensus.WithdrawalIndex(withdrawal.Index),
Amount: phase0.Gwei(withdrawal.Amount),
Address: b20(withdrawal.Address),
})
}
value, err := uint256.FromHex(block.BidTrace.Value)
if err != nil {
return nil, fmt.Errorf("failed to convert capella block value %s to uint256: %s", block.BidTrace.Value, err.Error())
}
return &capella.SubmitBlockRequest{
Message: &v1.BidTrace{
Slot: block.BidTrace.Slot,
ParentHash: b32(block.BidTrace.ParentHash),
BlockHash: b32(block.BidTrace.BlockHash),
BuilderPubkey: b48(block.BidTrace.BuilderPubkey),
ProposerPubkey: b48(block.BidTrace.ProposerPubkey),
ProposerFeeRecipient: b20(block.BidTrace.ProposerFeeRecipient),
GasLimit: block.BidTrace.GasLimit,
GasUsed: block.BidTrace.GasUsed,
Value: value,
},
ExecutionPayload: &consensus.ExecutionPayload{
ParentHash: b32(block.ExecutionPayload.ParentHash),
StateRoot: b32(block.ExecutionPayload.StateRoot),
ReceiptsRoot: b32(block.ExecutionPayload.ReceiptsRoot),
LogsBloom: b256(block.ExecutionPayload.LogsBloom),
PrevRandao: b32(block.ExecutionPayload.PrevRandao),
BaseFeePerGas: b32(block.ExecutionPayload.BaseFeePerGas),
FeeRecipient: b20(block.ExecutionPayload.FeeRecipient),
BlockHash: b32(block.ExecutionPayload.BlockHash),
ExtraData: block.ExecutionPayload.ExtraData,
BlockNumber: block.ExecutionPayload.BlockNumber,
GasLimit: block.ExecutionPayload.GasLimit,
Timestamp: block.ExecutionPayload.Timestamp,
GasUsed: block.ExecutionPayload.GasUsed,
Transactions: transactions,
Withdrawals: withdrawals,
},
Signature: b96(block.Signature),
}, nil
}