-
Notifications
You must be signed in to change notification settings - Fork 45
/
beacon_block.go
243 lines (199 loc) · 7.13 KB
/
beacon_block.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
package wrapper
import (
"github.com/pkg/errors"
types "github.com/prysmaticlabs/eth2-types"
eth "github.com/prysmaticlabs/ethereumapis/eth/v1alpha1"
"github.com/prysmaticlabs/prysm/proto/interfaces"
prysmv2 "github.com/prysmaticlabs/prysm/proto/prysm/v2"
"github.com/prysmaticlabs/prysm/shared/copyutil"
"github.com/prysmaticlabs/prysm/shared/version"
"google.golang.org/protobuf/proto"
)
// Phase0SignedBeaconBlock is a convenience wrapper around a phase 0 beacon block
// object. This wrapper allows us to conform to a common interface so that beacon
// blocks for future forks can also be applied across prysm without issues.
type Phase0SignedBeaconBlock struct {
b *eth.SignedBeaconBlock
}
// WrappedPhase0SignedBeaconBlock is constructor which wraps a protobuf phase 0 block
// with the block wrapper.
func WrappedPhase0SignedBeaconBlock(b *eth.SignedBeaconBlock) interfaces.SignedBeaconBlock {
return Phase0SignedBeaconBlock{b: b}
}
// Signature returns the respective block signature.
func (w Phase0SignedBeaconBlock) Signature() []byte {
return w.b.Signature
}
// Block returns the underlying beacon block object.
func (w Phase0SignedBeaconBlock) Block() interfaces.BeaconBlock {
return WrappedPhase0BeaconBlock(w.b.Block)
}
// IsNil checks if the underlying beacon block is
// nil.
func (w Phase0SignedBeaconBlock) IsNil() bool {
return w.b == nil || w.Block().IsNil()
}
// Copy performs a deep copy of the signed beacon block
// object.
func (w Phase0SignedBeaconBlock) Copy() interfaces.SignedBeaconBlock {
return WrappedPhase0SignedBeaconBlock(copyutil.CopySignedBeaconBlock(w.b))
}
// MarshalSSZ marshals the signed beacon block to its relevant ssz
// form.
func (w Phase0SignedBeaconBlock) MarshalSSZ() ([]byte, error) {
return w.b.MarshalSSZ()
}
// MarshalSSZTo marshals the signed beacon block to its relevant ssz
// form to the provided byte buffer.
func (w Phase0SignedBeaconBlock) MarshalSSZTo(dst []byte) ([]byte, error) {
return w.b.MarshalSSZTo(dst)
}
// SizeSSZ returns the size of serialized signed block
func (w Phase0SignedBeaconBlock) SizeSSZ() int {
return w.b.SizeSSZ()
}
// UnmarshalSSZ unmarshalls the signed beacon block from its relevant ssz
// form.
func (w Phase0SignedBeaconBlock) UnmarshalSSZ(buf []byte) error {
return w.b.UnmarshalSSZ(buf)
}
// Proto returns the block in its underlying protobuf
// interface.
func (w Phase0SignedBeaconBlock) Proto() proto.Message {
return w.b
}
// PbPhase0Block returns the underlying protobuf object.
func (w Phase0SignedBeaconBlock) PbPhase0Block() (*eth.SignedBeaconBlock, error) {
return w.b, nil
}
// AltairBlock returns the underlying protobuf object.
func (w Phase0SignedBeaconBlock) PbAltairBlock() (*prysmv2.SignedBeaconBlockAltair, error) {
return nil, errors.New("unsupported altair block")
}
// Version of the underlying protobuf object.
func (w Phase0SignedBeaconBlock) Version() int {
return version.Phase0
}
// Phase0BeaconBlock is the wrapper for the actual block.
type Phase0BeaconBlock struct {
b *eth.BeaconBlock
}
// WrappedPhase0BeaconBlock is constructor which wraps a protobuf phase 0 object
// with the block wrapper.
func WrappedPhase0BeaconBlock(b *eth.BeaconBlock) interfaces.BeaconBlock {
return Phase0BeaconBlock{b: b}
}
// Slot returns the respective slot of the block.
func (w Phase0BeaconBlock) Slot() types.Slot {
return w.b.Slot
}
// ProposerIndex returns proposer index of the beacon block.
func (w Phase0BeaconBlock) ProposerIndex() types.ValidatorIndex {
return w.b.ProposerIndex
}
// ParentRoot returns the parent root of beacon block.
func (w Phase0BeaconBlock) ParentRoot() []byte {
return w.b.ParentRoot
}
// StateRoot returns the state root of the beacon block.
func (w Phase0BeaconBlock) StateRoot() []byte {
return w.b.StateRoot
}
// Body returns the underlying block body.
func (w Phase0BeaconBlock) Body() interfaces.BeaconBlockBody {
return WrappedPhase0BeaconBlockBody(w.b.Body)
}
// IsNil checks if the beacon block is nil.
func (w Phase0BeaconBlock) IsNil() bool {
return w.b == nil || w.Body().IsNil()
}
// HashTreeRoot returns the ssz root of the block.
func (w Phase0BeaconBlock) HashTreeRoot() ([32]byte, error) {
return w.b.HashTreeRoot()
}
// MarshalSSZ marshals the block into its respective
// ssz form.
func (w Phase0BeaconBlock) MarshalSSZ() ([]byte, error) {
return w.b.MarshalSSZ()
}
// MarshalSSZTo marshals the beacon block to its relevant ssz
// form to the provided byte buffer.
func (w Phase0BeaconBlock) MarshalSSZTo(dst []byte) ([]byte, error) {
return w.b.MarshalSSZTo(dst)
}
// SizeSSZ returns the size of serialized block.
func (w Phase0BeaconBlock) SizeSSZ() int {
return w.b.SizeSSZ()
}
// UnmarshalSSZ unmarshalls the beacon block from its relevant ssz
// form.
func (w Phase0BeaconBlock) UnmarshalSSZ(buf []byte) error {
return w.b.UnmarshalSSZ(buf)
}
// Proto returns the underlying block object in its
// proto form.
func (w Phase0BeaconBlock) Proto() proto.Message {
return w.b
}
// Version of the underlying protobuf object.
func (w Phase0BeaconBlock) Version() int {
return version.Phase0
}
// Phase0BeaconBlockBody is a wrapper of a beacon block body.
type Phase0BeaconBlockBody struct {
b *eth.BeaconBlockBody
}
// WrappedPhase0BeaconBlockBody is constructor which wraps a protobuf phase 0 object
// with the block wrapper.
func WrappedPhase0BeaconBlockBody(b *eth.BeaconBlockBody) interfaces.BeaconBlockBody {
return Phase0BeaconBlockBody{b: b}
}
// RandaoReveal returns the randao reveal from the block body.
func (w Phase0BeaconBlockBody) RandaoReveal() []byte {
return w.b.RandaoReveal
}
// Eth1Data returns the eth1 data in the block.
func (w Phase0BeaconBlockBody) Eth1Data() *eth.Eth1Data {
return w.b.Eth1Data
}
// Graffiti returns the graffiti in the block.
func (w Phase0BeaconBlockBody) Graffiti() []byte {
return w.b.Graffiti
}
// ProposerSlashings returns the proposer slashings in the block.
func (w Phase0BeaconBlockBody) ProposerSlashings() []*eth.ProposerSlashing {
return w.b.ProposerSlashings
}
// AttesterSlashings returns the attester slashings in the block.
func (w Phase0BeaconBlockBody) AttesterSlashings() []*eth.AttesterSlashing {
return w.b.AttesterSlashings
}
// Attestations returns the stored attestations in the block.
func (w Phase0BeaconBlockBody) Attestations() []*eth.Attestation {
return w.b.Attestations
}
// Deposits returns the stored deposits in the block.
func (w Phase0BeaconBlockBody) Deposits() []*eth.Deposit {
return w.b.Deposits
}
// VoluntaryExits returns the voluntary exits in the block.
func (w Phase0BeaconBlockBody) VoluntaryExits() []*eth.SignedVoluntaryExit {
return w.b.VoluntaryExits
}
// SyncAggregate returns the sync aggregate in the block.
func (w Phase0BeaconBlockBody) SyncAggregate() (*prysmv2.SyncAggregate, error) {
return nil, errors.New("Sync aggregate is not supported in phase 0 block")
}
// IsNil checks if the block body is nil.
func (w Phase0BeaconBlockBody) IsNil() bool {
return w.b == nil
}
// HashTreeRoot returns the ssz root of the block body.
func (w Phase0BeaconBlockBody) HashTreeRoot() ([32]byte, error) {
return w.b.HashTreeRoot()
}
// Proto returns the underlying proto form of the block
// body.
func (w Phase0BeaconBlockBody) Proto() proto.Message {
return w.b
}