Skip to content

Commit d84c1ad

Browse files
fearlessfepengzhen
authored andcommitted
feat: add lightclient types
1. LightClientFinalityUpdate and LightClientOptimisticUpdate 2. upgrade the testcases
1 parent 188558b commit d84c1ad

File tree

3 files changed

+297
-7
lines changed

3 files changed

+297
-7
lines changed

eth2/beacon/altair/lightclient.go

Lines changed: 91 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ func LightClientUpdateType(spec *common.Spec) *ContainerTypeDef {
148148

149149
type LightClientUpdate struct {
150150
// Update beacon block header
151-
AttestedHeader common.BeaconBlockHeader `yaml:"attested_header" json:"attested_header"`
151+
AttestedHeader LightClientHeader `yaml:"attested_header" json:"attested_header"`
152152
// Next sync committee corresponding to the header
153153
NextSyncCommittee common.SyncCommittee `yaml:"next_sync_committee" json:"next_sync_committee"`
154154
NextSyncCommitteeBranch SyncCommitteeProofBranch `yaml:"next_sync_committee_branch" json:"next_sync_committee_branch"`
@@ -222,7 +222,7 @@ func (lcu *LightClientUpdate) HashTreeRoot(spec *common.Spec, hFn tree.HashFn) c
222222
}
223223

224224
var LightClientHeaderType = ContainerType("LightClientHeaderType", []FieldDef{
225-
{"beacon", common.BeaconBlockHeaderType},
225+
{Name: "beacon", Type: common.BeaconBlockHeaderType},
226226
})
227227

228228
type LightClientHeader struct {
@@ -260,16 +260,16 @@ func (lch *LightClientHeader) HashTreeRoot(hFn tree.HashFn) common.Root {
260260
}
261261

262262
type LightClientBootstrap struct {
263-
Header LightClientHeader
264-
CurrentSyncCommittee common.SyncCommittee
265-
CurrentSyncCommitteeBranch SyncCommitteeProofBranch
263+
Header LightClientHeader `yaml:"header" json:"header"`
264+
CurrentSyncCommittee common.SyncCommittee `yaml:"current_sync_committee" json:"current_sync_committee"`
265+
CurrentSyncCommitteeBranch SyncCommitteeProofBranch `yaml:"current_sync_committee_branch" json:"current_sync_committee_branch"`
266266
}
267267

268268
func NewLightClientBootstrapType(spec *common.Spec) *ContainerTypeDef {
269269
return ContainerType("LightClientHeader", []FieldDef{
270270
{Name: "header", Type: LightClientHeaderType},
271-
{Name: "next_sync_committee", Type: common.SyncCommitteeType(spec)},
272-
{Name: "next_sync_committee_branch", Type: SyncCommitteeProofBranchType},
271+
{Name: "current_sync_committee", Type: common.SyncCommitteeType(spec)},
272+
{Name: "current_sync_committee_branch", Type: SyncCommitteeProofBranchType},
273273
})
274274
}
275275

@@ -296,3 +296,87 @@ func (lcb *LightClientBootstrap) HashTreeRoot(spec *common.Spec, hFn tree.HashFn
296296
&lcb.CurrentSyncCommitteeBranch,
297297
)
298298
}
299+
300+
type LightClientFinalityUpdate struct {
301+
AttestedHeader LightClientHeader `yaml:"attested_header" json:"attested_header"`
302+
FinalizedHeader common.BeaconBlockHeader `yaml:"finalized_header" json:"finalized_header"`
303+
FinalityBranch FinalizedRootProofBranch `yaml:"finality_branch" json:"finality_branch"`
304+
SyncAggregate SyncAggregate `yaml:"sync_aggregate" json:"sync_aggregate"`
305+
SignatureSlot common.Slot `yaml:"signature_slot" json:"signature_slot"`
306+
}
307+
308+
func LightClientFinalityUpdateType(spec *common.Spec) *ContainerTypeDef {
309+
return ContainerType("SyncCommittee", []FieldDef{
310+
{Name: "attested_header", Type: common.BeaconBlockHeaderType},
311+
{Name: "finalized_header", Type: common.BeaconBlockHeaderType},
312+
{Name: "finality_branch", Type: FinalizedRootProofBranchType},
313+
{Name: "sync_aggregate", Type: SyncAggregateType(spec)},
314+
{Name: "signature_slot", Type: common.SlotType},
315+
})
316+
}
317+
318+
func (lcfu *LightClientFinalityUpdate) FixedLength(spec *common.Spec) uint64 {
319+
return codec.ContainerLength(&lcfu.AttestedHeader, &lcfu.FinalizedHeader, &lcfu.FinalityBranch, spec.Wrap(&lcfu.SyncAggregate), &lcfu.SignatureSlot)
320+
}
321+
322+
func (lcfu *LightClientFinalityUpdate) Deserialize(spec *common.Spec, dr *codec.DecodingReader) error {
323+
return dr.Container(&lcfu.AttestedHeader, &lcfu.FinalizedHeader, &lcfu.FinalityBranch, spec.Wrap(&lcfu.SyncAggregate), &lcfu.SignatureSlot)
324+
}
325+
326+
func (lcfu *LightClientFinalityUpdate) Serialize(spec *common.Spec, w *codec.EncodingWriter) error {
327+
return w.Container(&lcfu.AttestedHeader, &lcfu.FinalizedHeader, &lcfu.FinalityBranch, spec.Wrap(&lcfu.SyncAggregate), &lcfu.SignatureSlot)
328+
}
329+
330+
func (lcfu *LightClientFinalityUpdate) ByteLength(spec *common.Spec) uint64 {
331+
return codec.ContainerLength(&lcfu.AttestedHeader, &lcfu.FinalizedHeader, &lcfu.FinalityBranch, spec.Wrap(&lcfu.SyncAggregate), &lcfu.SignatureSlot)
332+
}
333+
334+
func (lcfu *LightClientFinalityUpdate) HashTreeRoot(spec *common.Spec, hFn tree.HashFn) common.Root {
335+
return hFn.HashTreeRoot(
336+
&lcfu.AttestedHeader,
337+
&lcfu.FinalizedHeader,
338+
&lcfu.FinalityBranch,
339+
spec.Wrap(&lcfu.SyncAggregate),
340+
&lcfu.SignatureSlot,
341+
)
342+
}
343+
344+
type LightClientOptimisticUpdate struct {
345+
AttestedHeader LightClientHeader `yaml:"attested_header" json:"attested_header"`
346+
SyncAggregate SyncAggregate `yaml:"sync_aggregate" json:"sync_aggregate"`
347+
SignatureSlot common.Slot `yaml:"signature_slot" json:"signature_slot"`
348+
}
349+
350+
func LightClientOptimisticUpdateType(spec *common.Spec) *ContainerTypeDef {
351+
return ContainerType("SyncCommittee", []FieldDef{
352+
{Name: "attested_header", Type: common.BeaconBlockHeaderType},
353+
{Name: "finalized_header", Type: common.BeaconBlockHeaderType},
354+
{Name: "finality_branch", Type: FinalizedRootProofBranchType},
355+
{Name: "sync_aggregate", Type: SyncAggregateType(spec)},
356+
{Name: "signature_slot", Type: common.SlotType},
357+
})
358+
}
359+
360+
func (lcou *LightClientOptimisticUpdate) FixedLength(spec *common.Spec) uint64 {
361+
return codec.ContainerLength(&lcou.AttestedHeader, spec.Wrap(&lcou.SyncAggregate), &lcou.SignatureSlot)
362+
}
363+
364+
func (lcou *LightClientOptimisticUpdate) Deserialize(spec *common.Spec, dr *codec.DecodingReader) error {
365+
return dr.Container(&lcou.AttestedHeader, spec.Wrap(&lcou.SyncAggregate), &lcou.SignatureSlot)
366+
}
367+
368+
func (lcou *LightClientOptimisticUpdate) Serialize(spec *common.Spec, w *codec.EncodingWriter) error {
369+
return w.Container(&lcou.AttestedHeader, spec.Wrap(&lcou.SyncAggregate), &lcou.SignatureSlot)
370+
}
371+
372+
func (lcou *LightClientOptimisticUpdate) ByteLength(spec *common.Spec) uint64 {
373+
return codec.ContainerLength(&lcou.AttestedHeader, spec.Wrap(&lcou.SyncAggregate), &lcou.SignatureSlot)
374+
}
375+
376+
func (lcou *LightClientOptimisticUpdate) HashTreeRoot(spec *common.Spec, hFn tree.HashFn) common.Root {
377+
return hFn.HashTreeRoot(
378+
&lcou.AttestedHeader,
379+
spec.Wrap(&lcou.SyncAggregate),
380+
&lcou.SignatureSlot,
381+
)
382+
}

eth2/beacon/capella/lightclient.go

Lines changed: 171 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,3 +110,174 @@ func (lcb *LightClientBootstrap) HashTreeRoot(spec *common.Spec, hFn tree.HashFn
110110
&lcb.CurrentSyncCommitteeBranch,
111111
)
112112
}
113+
114+
func LightClientUpdateType(spec *common.Spec) *view.ContainerTypeDef {
115+
return view.ContainerType("SyncCommittee", []view.FieldDef{
116+
{Name: "attested_header", Type: common.BeaconBlockHeaderType},
117+
{Name: "next_sync_committee", Type: common.SyncCommitteeType(spec)},
118+
{Name: "next_sync_committee_branch", Type: altair.SyncCommitteeProofBranchType},
119+
{Name: "finalized_header", Type: common.BeaconBlockHeaderType},
120+
{Name: "finality_branch", Type: altair.FinalizedRootProofBranchType},
121+
{Name: "sync_aggregate", Type: altair.SyncAggregateType(spec)},
122+
{Name: "signature_slot", Type: common.SlotType},
123+
})
124+
}
125+
126+
type LightClientUpdate struct {
127+
// Update beacon block header
128+
AttestedHeader LightClientHeader `yaml:"attested_header" json:"attested_header"`
129+
// Next sync committee corresponding to the header
130+
NextSyncCommittee common.SyncCommittee `yaml:"next_sync_committee" json:"next_sync_committee"`
131+
NextSyncCommitteeBranch altair.SyncCommitteeProofBranch `yaml:"next_sync_committee_branch" json:"next_sync_committee_branch"`
132+
// Finality proof for the update header
133+
FinalizedHeader common.BeaconBlockHeader `yaml:"finalized_header" json:"finalized_header"`
134+
FinalityBranch altair.FinalizedRootProofBranch `yaml:"finality_branch" json:"finality_branch"`
135+
// Sync committee aggregate signature
136+
SyncAggregate altair.SyncAggregate `yaml:"sync_aggregate" json:"sync_aggregate"`
137+
// Slot at which the aggregate signature was created (untrusted)
138+
SignatureSlot common.Slot `yaml:"signature_slot" json:"signature_slot"`
139+
}
140+
141+
func (lcu *LightClientUpdate) Deserialize(spec *common.Spec, dr *codec.DecodingReader) error {
142+
return dr.FixedLenContainer(
143+
&lcu.AttestedHeader,
144+
spec.Wrap(&lcu.NextSyncCommittee),
145+
&lcu.NextSyncCommitteeBranch,
146+
&lcu.FinalizedHeader,
147+
&lcu.FinalityBranch,
148+
spec.Wrap(&lcu.SyncAggregate),
149+
&lcu.SignatureSlot,
150+
)
151+
}
152+
153+
func (lcu *LightClientUpdate) Serialize(spec *common.Spec, w *codec.EncodingWriter) error {
154+
return w.FixedLenContainer(
155+
&lcu.AttestedHeader,
156+
spec.Wrap(&lcu.NextSyncCommittee),
157+
&lcu.NextSyncCommitteeBranch,
158+
&lcu.FinalizedHeader,
159+
&lcu.FinalityBranch,
160+
spec.Wrap(&lcu.SyncAggregate),
161+
&lcu.SignatureSlot,
162+
)
163+
}
164+
165+
func (lcu *LightClientUpdate) ByteLength(spec *common.Spec) uint64 {
166+
return codec.ContainerLength(
167+
&lcu.AttestedHeader,
168+
spec.Wrap(&lcu.NextSyncCommittee),
169+
&lcu.NextSyncCommitteeBranch,
170+
&lcu.FinalizedHeader,
171+
&lcu.FinalityBranch,
172+
spec.Wrap(&lcu.SyncAggregate),
173+
&lcu.SignatureSlot,
174+
)
175+
}
176+
177+
func (lcu *LightClientUpdate) FixedLength(spec *common.Spec) uint64 {
178+
return codec.ContainerLength(
179+
&lcu.AttestedHeader,
180+
spec.Wrap(&lcu.NextSyncCommittee),
181+
&lcu.NextSyncCommitteeBranch,
182+
&lcu.FinalizedHeader,
183+
&lcu.FinalityBranch,
184+
spec.Wrap(&lcu.SyncAggregate),
185+
&lcu.SignatureSlot,
186+
)
187+
}
188+
189+
func (lcu *LightClientUpdate) HashTreeRoot(spec *common.Spec, hFn tree.HashFn) common.Root {
190+
return hFn.HashTreeRoot(
191+
&lcu.AttestedHeader,
192+
spec.Wrap(&lcu.NextSyncCommittee),
193+
&lcu.NextSyncCommitteeBranch,
194+
&lcu.FinalizedHeader,
195+
&lcu.FinalityBranch,
196+
spec.Wrap(&lcu.SyncAggregate),
197+
&lcu.SignatureSlot,
198+
)
199+
}
200+
201+
type LightClientFinalityUpdate struct {
202+
AttestedHeader LightClientHeader `yaml:"attested_header" json:"attested_header"`
203+
FinalizedHeader common.BeaconBlockHeader `yaml:"finalized_header" json:"finalized_header"`
204+
FinalityBranch altair.FinalizedRootProofBranch `yaml:"finality_branch" json:"finality_branch"`
205+
SyncAggregate altair.SyncAggregate `yaml:"sync_aggregate" json:"sync_aggregate"`
206+
SignatureSlot common.Slot `yaml:"signature_slot" json:"signature_slot"`
207+
}
208+
209+
func LightClientFinalityUpdateType(spec *common.Spec) *view.ContainerTypeDef {
210+
return view.ContainerType("SyncCommittee", []view.FieldDef{
211+
{Name: "attested_header", Type: LightClientHeaderType},
212+
{Name: "finalized_header", Type: common.BeaconBlockHeaderType},
213+
{Name: "finality_branch", Type: altair.FinalizedRootProofBranchType},
214+
{Name: "sync_aggregate", Type: altair.SyncAggregateType(spec)},
215+
{Name: "signature_slot", Type: common.SlotType},
216+
})
217+
}
218+
219+
func (lcfu *LightClientFinalityUpdate) FixedLength(spec *common.Spec) uint64 {
220+
return codec.ContainerLength(&lcfu.AttestedHeader, &lcfu.FinalizedHeader, &lcfu.FinalityBranch, spec.Wrap(&lcfu.SyncAggregate), &lcfu.SignatureSlot)
221+
}
222+
223+
func (lcfu *LightClientFinalityUpdate) Deserialize(spec *common.Spec, dr *codec.DecodingReader) error {
224+
return dr.Container(&lcfu.AttestedHeader, &lcfu.FinalizedHeader, &lcfu.FinalityBranch, spec.Wrap(&lcfu.SyncAggregate), &lcfu.SignatureSlot)
225+
}
226+
227+
func (lcfu *LightClientFinalityUpdate) Serialize(spec *common.Spec, w *codec.EncodingWriter) error {
228+
return w.Container(&lcfu.AttestedHeader, &lcfu.FinalizedHeader, &lcfu.FinalityBranch, spec.Wrap(&lcfu.SyncAggregate), &lcfu.SignatureSlot)
229+
}
230+
231+
func (lcfu *LightClientFinalityUpdate) ByteLength(spec *common.Spec) uint64 {
232+
return codec.ContainerLength(&lcfu.AttestedHeader, &lcfu.FinalizedHeader, &lcfu.FinalityBranch, spec.Wrap(&lcfu.SyncAggregate), &lcfu.SignatureSlot)
233+
}
234+
235+
func (lcfu *LightClientFinalityUpdate) HashTreeRoot(spec *common.Spec, hFn tree.HashFn) common.Root {
236+
return hFn.HashTreeRoot(
237+
&lcfu.AttestedHeader,
238+
&lcfu.FinalizedHeader,
239+
&lcfu.FinalityBranch,
240+
spec.Wrap(&lcfu.SyncAggregate),
241+
&lcfu.SignatureSlot,
242+
)
243+
}
244+
245+
type LightClientOptimisticUpdate struct {
246+
AttestedHeader LightClientHeader `yaml:"attested_header" json:"attested_header"`
247+
SyncAggregate altair.SyncAggregate `yaml:"sync_aggregate" json:"sync_aggregate"`
248+
SignatureSlot common.Slot `yaml:"signature_slot" json:"signature_slot"`
249+
}
250+
251+
func LightClientOptimisticUpdateType(spec *common.Spec) *view.ContainerTypeDef {
252+
return view.ContainerType("SyncCommittee", []view.FieldDef{
253+
{Name: "attested_header", Type: LightClientHeaderType},
254+
{Name: "finalized_header", Type: common.BeaconBlockHeaderType},
255+
{Name: "finality_branch", Type: altair.FinalizedRootProofBranchType},
256+
{Name: "sync_aggregate", Type: altair.SyncAggregateType(spec)},
257+
{Name: "signature_slot", Type: common.SlotType},
258+
})
259+
}
260+
261+
func (lcou *LightClientOptimisticUpdate) FixedLength(spec *common.Spec) uint64 {
262+
return codec.ContainerLength(&lcou.AttestedHeader, spec.Wrap(&lcou.SyncAggregate), &lcou.SignatureSlot)
263+
}
264+
265+
func (lcou *LightClientOptimisticUpdate) Deserialize(spec *common.Spec, dr *codec.DecodingReader) error {
266+
return dr.Container(&lcou.AttestedHeader, spec.Wrap(&lcou.SyncAggregate), &lcou.SignatureSlot)
267+
}
268+
269+
func (lcou *LightClientOptimisticUpdate) Serialize(spec *common.Spec, w *codec.EncodingWriter) error {
270+
return w.Container(&lcou.AttestedHeader, spec.Wrap(&lcou.SyncAggregate), &lcou.SignatureSlot)
271+
}
272+
273+
func (lcou *LightClientOptimisticUpdate) ByteLength(spec *common.Spec) uint64 {
274+
return codec.ContainerLength(&lcou.AttestedHeader, spec.Wrap(&lcou.SyncAggregate), &lcou.SignatureSlot)
275+
}
276+
277+
func (lcou *LightClientOptimisticUpdate) HashTreeRoot(spec *common.Spec, hFn tree.HashFn) common.Root {
278+
return hFn.HashTreeRoot(
279+
&lcou.AttestedHeader,
280+
spec.Wrap(&lcou.SyncAggregate),
281+
&lcou.SignatureSlot,
282+
)
283+
}

tests/spec/test_runners/ssz_static/ssz_static_test.go

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,8 @@ func init() {
150150
objs["altair"]["LightClientUpdate"] = func() interface{} { return new(altair.LightClientUpdate) }
151151
objs["altair"]["LightClientHeader"] = func() interface{} { return new(altair.LightClientHeader) }
152152
objs["altair"]["LightClientBootstrap"] = func() interface{} { return new(altair.LightClientBootstrap) }
153+
objs["altair"]["LightClientFinalityUpdate"] = func() interface{} { return new(altair.LightClientFinalityUpdate) }
154+
objs["altair"]["LightClientOptimisticUpdate"] = func() interface{} { return new(altair.LightClientOptimisticUpdate) }
153155
objs["altair"]["SyncAggregatorSelectionData"] = func() interface{} { return new(altair.SyncAggregatorSelectionData) }
154156
objs["altair"]["SyncCommitteeContribution"] = func() interface{} { return new(altair.SyncCommitteeContribution) }
155157
objs["altair"]["ContributionAndProof"] = func() interface{} { return new(altair.ContributionAndProof) }
@@ -176,6 +178,9 @@ func init() {
176178
objs["capella"]["SignedBLSToExecutionChange"] = func() interface{} { return new(common.SignedBLSToExecutionChange) }
177179
objs["capella"]["LightClientHeader"] = func() interface{} { return new(capella.LightClientHeader) }
178180
objs["capella"]["LightClientBootstrap"] = func() interface{} { return new(capella.LightClientBootstrap) }
181+
objs["capella"]["LightClientUpdate"] = func() interface{} { return new(capella.LightClientUpdate) }
182+
objs["capella"]["LightClientFinalityUpdate"] = func() interface{} { return new(capella.LightClientFinalityUpdate) }
183+
objs["capella"]["LightClientOptimisticUpdate"] = func() interface{} { return new(capella.LightClientOptimisticUpdate) }
179184
}
180185

181186
type RootsYAML struct {
@@ -246,3 +251,33 @@ func TestSSZStatic(t *testing.T) {
246251
}
247252
})
248253
}
254+
255+
func TestSSZStatic2(t *testing.T) {
256+
var objs = map[test_util.ForkName]map[string]ObjAllocator{
257+
"phase0": {},
258+
"altair": {},
259+
"bellatrix": {},
260+
"capella": {},
261+
}
262+
//objs["capella"]["LightClientUpdate"] = func() interface{} { return new(capella.LightClientUpdate) }
263+
objs["capella"]["LightClientFinalityUpdate"] = func() interface{} { return new(capella.LightClientFinalityUpdate) }
264+
//objs["capella"]["LightClientOptimisticUpdate"] = func() interface{} { return new(capella.LightClientOptimisticUpdate) }
265+
t.Run("minimal", func(t *testing.T) {
266+
for fork, objByName := range objs {
267+
t.Run(string(fork), func(t *testing.T) {
268+
for k, v := range objByName {
269+
t.Run(k, runSSZStaticTest(fork, k, v, configs.Minimal))
270+
}
271+
})
272+
}
273+
})
274+
t.Run("mainnet", func(t *testing.T) {
275+
for fork, objByName := range objs {
276+
t.Run(string(fork), func(t *testing.T) {
277+
for k, v := range objByName {
278+
t.Run(k, runSSZStaticTest(fork, k, v, configs.Mainnet))
279+
}
280+
})
281+
}
282+
})
283+
}

0 commit comments

Comments
 (0)