Skip to content

Commit

Permalink
feat: add lightclient type for deneb
Browse files Browse the repository at this point in the history
  • Loading branch information
fearlessfe committed Apr 15, 2024
1 parent 89802fe commit d9dbf06
Show file tree
Hide file tree
Showing 2 changed files with 254 additions and 0 deletions.
243 changes: 243 additions & 0 deletions eth2/beacon/deneb/lightclient.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,243 @@
package deneb

import (
"github.com/protolambda/zrnt/eth2/beacon/altair"
"github.com/protolambda/zrnt/eth2/beacon/capella"
"github.com/protolambda/zrnt/eth2/beacon/common"
"github.com/protolambda/ztyp/codec"
"github.com/protolambda/ztyp/tree"
"github.com/protolambda/ztyp/view"
)

type LightClientHeader struct {
Beacon common.BeaconBlockHeader `yaml:"beacon" json:"beacon"`
Execution ExecutionPayloadHeader `yaml:"execution" json:"execution"`
ExecutionBranch capella.ExecutionBranch `yaml:"execution_branch" json:"execution_branch"`
}

var LightClientHeaderType = view.ContainerType("LightClientHeader", []view.FieldDef{
{Name: "beacon", Type: common.BeaconBlockHeaderType},
{Name: "execution", Type: ExecutionPayloadHeaderType},
{Name: "execution_branch", Type: capella.ExecutionBranchType},
})

func (l *LightClientHeader) Deserialize(dr *codec.DecodingReader) error {
return dr.Container(&l.Beacon, &l.Execution, &l.ExecutionBranch)
}

func (l *LightClientHeader) FixedLength() uint64 {
return LightClientHeaderType.TypeByteLength()
}

func (l *LightClientHeader) Serialize(w *codec.EncodingWriter) error {
return w.Container(&l.Beacon, &l.Execution, &l.ExecutionBranch)
}

func (l *LightClientHeader) ByteLength() (out uint64) {
return codec.ContainerLength(&l.Beacon, &l.Execution, &l.ExecutionBranch)
}

func (l *LightClientHeader) HashTreeRoot(h tree.HashFn) common.Root {
return h.HashTreeRoot(&l.Beacon, &l.Execution, &l.ExecutionBranch)
}

type LightClientBootstrap struct {
Header LightClientHeader `yaml:"header" json:"header"`
CurrentSyncCommittee common.SyncCommittee `yaml:"current_sync_committee" json:"current_sync_committee"`
CurrentSyncCommitteeBranch altair.SyncCommitteeProofBranch `yaml:"current_sync_committee_branch" json:"current_sync_committee_branch"`
}

func NewLightClientBootstrapType(spec *common.Spec) *view.ContainerTypeDef {
return view.ContainerType("LightClientHeader", []view.FieldDef{
{Name: "header", Type: LightClientHeaderType},
{Name: "next_sync_committee", Type: common.SyncCommitteeType(spec)},
{Name: "next_sync_committee_branch", Type: altair.SyncCommitteeProofBranchType},
})
}

func (lcb *LightClientBootstrap) FixedLength(spec *common.Spec) uint64 {
return 0
}

func (lcb *LightClientBootstrap) Deserialize(spec *common.Spec, dr *codec.DecodingReader) error {
return dr.Container(&lcb.Header, spec.Wrap(&lcb.CurrentSyncCommittee), &lcb.CurrentSyncCommitteeBranch)
}

func (lcb *LightClientBootstrap) Serialize(spec *common.Spec, w *codec.EncodingWriter) error {
return w.Container(&lcb.Header, spec.Wrap(&lcb.CurrentSyncCommittee), &lcb.CurrentSyncCommitteeBranch)
}

func (lcb *LightClientBootstrap) ByteLength(spec *common.Spec) uint64 {
return codec.ContainerLength(&lcb.Header, spec.Wrap(&lcb.CurrentSyncCommittee), &lcb.CurrentSyncCommitteeBranch)
}

func (lcb *LightClientBootstrap) HashTreeRoot(spec *common.Spec, hFn tree.HashFn) common.Root {
return hFn.HashTreeRoot(
&lcb.Header,
spec.Wrap(&lcb.CurrentSyncCommittee),
&lcb.CurrentSyncCommitteeBranch,
)
}

func LightClientUpdateType(spec *common.Spec) *view.ContainerTypeDef {
return view.ContainerType("SyncCommittee", []view.FieldDef{
{Name: "attested_header", Type: common.BeaconBlockHeaderType},
{Name: "next_sync_committee", Type: common.SyncCommitteeType(spec)},
{Name: "next_sync_committee_branch", Type: altair.SyncCommitteeProofBranchType},
{Name: "finalized_header", Type: LightClientHeaderType},
{Name: "finality_branch", Type: altair.FinalizedRootProofBranchType},
{Name: "sync_aggregate", Type: altair.SyncAggregateType(spec)},
{Name: "signature_slot", Type: common.SlotType},
})
}

type LightClientUpdate struct {
// Update beacon block header
AttestedHeader LightClientHeader `yaml:"attested_header" json:"attested_header"`
// Next sync committee corresponding to the header
NextSyncCommittee common.SyncCommittee `yaml:"next_sync_committee" json:"next_sync_committee"`
NextSyncCommitteeBranch altair.SyncCommitteeProofBranch `yaml:"next_sync_committee_branch" json:"next_sync_committee_branch"`
// Finality proof for the update header
FinalizedHeader LightClientHeader `yaml:"finalized_header" json:"finalized_header"`
FinalityBranch altair.FinalizedRootProofBranch `yaml:"finality_branch" json:"finality_branch"`
// Sync committee aggregate signature
SyncAggregate altair.SyncAggregate `yaml:"sync_aggregate" json:"sync_aggregate"`
// Slot at which the aggregate signature was created (untrusted)
SignatureSlot common.Slot `yaml:"signature_slot" json:"signature_slot"`
}

func (lcu *LightClientUpdate) Deserialize(spec *common.Spec, dr *codec.DecodingReader) error {
return dr.Container(
&lcu.AttestedHeader,
spec.Wrap(&lcu.NextSyncCommittee),
&lcu.NextSyncCommitteeBranch,
&lcu.FinalizedHeader,
&lcu.FinalityBranch,
spec.Wrap(&lcu.SyncAggregate),
&lcu.SignatureSlot,
)
}

func (lcu *LightClientUpdate) Serialize(spec *common.Spec, w *codec.EncodingWriter) error {
return w.Container(
&lcu.AttestedHeader,
spec.Wrap(&lcu.NextSyncCommittee),
&lcu.NextSyncCommitteeBranch,
&lcu.FinalizedHeader,
&lcu.FinalityBranch,
spec.Wrap(&lcu.SyncAggregate),
&lcu.SignatureSlot,
)
}

func (lcu *LightClientUpdate) ByteLength(spec *common.Spec) uint64 {
return codec.ContainerLength(
&lcu.AttestedHeader,
spec.Wrap(&lcu.NextSyncCommittee),
&lcu.NextSyncCommitteeBranch,
&lcu.FinalizedHeader,
&lcu.FinalityBranch,
spec.Wrap(&lcu.SyncAggregate),
&lcu.SignatureSlot,
)
}

func (lcu *LightClientUpdate) FixedLength(spec *common.Spec) uint64 {
return 0
}

func (lcu *LightClientUpdate) HashTreeRoot(spec *common.Spec, hFn tree.HashFn) common.Root {
return hFn.HashTreeRoot(
&lcu.AttestedHeader,
spec.Wrap(&lcu.NextSyncCommittee),
&lcu.NextSyncCommitteeBranch,
&lcu.FinalizedHeader,
&lcu.FinalityBranch,
spec.Wrap(&lcu.SyncAggregate),
&lcu.SignatureSlot,
)
}

type LightClientFinalityUpdate struct {
AttestedHeader LightClientHeader `yaml:"attested_header" json:"attested_header"`
FinalizedHeader LightClientHeader `yaml:"finalized_header" json:"finalized_header"`
FinalityBranch altair.FinalizedRootProofBranch `yaml:"finality_branch" json:"finality_branch"`
SyncAggregate altair.SyncAggregate `yaml:"sync_aggregate" json:"sync_aggregate"`
SignatureSlot common.Slot `yaml:"signature_slot" json:"signature_slot"`
}

func LightClientFinalityUpdateType(spec *common.Spec) *view.ContainerTypeDef {
return view.ContainerType("SyncCommittee", []view.FieldDef{
{Name: "attested_header", Type: LightClientHeaderType},
{Name: "finalized_header", Type: LightClientHeaderType},
{Name: "finality_branch", Type: altair.FinalizedRootProofBranchType},
{Name: "sync_aggregate", Type: altair.SyncAggregateType(spec)},
{Name: "signature_slot", Type: common.SlotType},
})
}

func (lcfu *LightClientFinalityUpdate) FixedLength(spec *common.Spec) uint64 {
return 0
}

func (lcfu *LightClientFinalityUpdate) Deserialize(spec *common.Spec, dr *codec.DecodingReader) error {
return dr.Container(&lcfu.AttestedHeader, &lcfu.FinalizedHeader, &lcfu.FinalityBranch, spec.Wrap(&lcfu.SyncAggregate), &lcfu.SignatureSlot)
}

func (lcfu *LightClientFinalityUpdate) Serialize(spec *common.Spec, w *codec.EncodingWriter) error {
return w.Container(&lcfu.AttestedHeader, &lcfu.FinalizedHeader, &lcfu.FinalityBranch, spec.Wrap(&lcfu.SyncAggregate), &lcfu.SignatureSlot)
}

func (lcfu *LightClientFinalityUpdate) ByteLength(spec *common.Spec) uint64 {
return codec.ContainerLength(&lcfu.AttestedHeader, &lcfu.FinalizedHeader, &lcfu.FinalityBranch, spec.Wrap(&lcfu.SyncAggregate), &lcfu.SignatureSlot)
}

func (lcfu *LightClientFinalityUpdate) HashTreeRoot(spec *common.Spec, hFn tree.HashFn) common.Root {
return hFn.HashTreeRoot(
&lcfu.AttestedHeader,
&lcfu.FinalizedHeader,
&lcfu.FinalityBranch,
spec.Wrap(&lcfu.SyncAggregate),
&lcfu.SignatureSlot,
)
}

type LightClientOptimisticUpdate struct {
AttestedHeader LightClientHeader `yaml:"attested_header" json:"attested_header"`
SyncAggregate altair.SyncAggregate `yaml:"sync_aggregate" json:"sync_aggregate"`
SignatureSlot common.Slot `yaml:"signature_slot" json:"signature_slot"`
}

func LightClientOptimisticUpdateType(spec *common.Spec) *view.ContainerTypeDef {
return view.ContainerType("SyncCommittee", []view.FieldDef{
{Name: "attested_header", Type: LightClientHeaderType},
{Name: "finalized_header", Type: common.BeaconBlockHeaderType},
{Name: "finality_branch", Type: altair.FinalizedRootProofBranchType},
{Name: "sync_aggregate", Type: altair.SyncAggregateType(spec)},
{Name: "signature_slot", Type: common.SlotType},
})
}

func (lcou *LightClientOptimisticUpdate) FixedLength(spec *common.Spec) uint64 {
return 0
}

func (lcou *LightClientOptimisticUpdate) Deserialize(spec *common.Spec, dr *codec.DecodingReader) error {
return dr.Container(&lcou.AttestedHeader, spec.Wrap(&lcou.SyncAggregate), &lcou.SignatureSlot)
}

func (lcou *LightClientOptimisticUpdate) Serialize(spec *common.Spec, w *codec.EncodingWriter) error {
return w.Container(&lcou.AttestedHeader, spec.Wrap(&lcou.SyncAggregate), &lcou.SignatureSlot)
}

func (lcou *LightClientOptimisticUpdate) ByteLength(spec *common.Spec) uint64 {
return codec.ContainerLength(&lcou.AttestedHeader, spec.Wrap(&lcou.SyncAggregate), &lcou.SignatureSlot)
}

func (lcou *LightClientOptimisticUpdate) HashTreeRoot(spec *common.Spec, hFn tree.HashFn) common.Root {
return hFn.HashTreeRoot(
&lcou.AttestedHeader,
spec.Wrap(&lcou.SyncAggregate),
&lcou.SignatureSlot,
)
}
11 changes: 11 additions & 0 deletions tests/spec/test_runners/ssz_static/ssz_static_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"testing"

"github.com/protolambda/zrnt/eth2/beacon/capella"
"github.com/protolambda/zrnt/eth2/beacon/deneb"

"github.com/golang/snappy"
"github.com/protolambda/ztyp/codec"
Expand Down Expand Up @@ -102,6 +103,7 @@ var objs = map[test_util.ForkName]map[string]ObjAllocator{
"altair": {},
"bellatrix": {},
"capella": {},
"deneb": {},
}

func init() {
Expand Down Expand Up @@ -181,6 +183,15 @@ func init() {
objs["capella"]["LightClientUpdate"] = func() interface{} { return new(capella.LightClientUpdate) }
objs["capella"]["LightClientFinalityUpdate"] = func() interface{} { return new(capella.LightClientFinalityUpdate) }
objs["capella"]["LightClientOptimisticUpdate"] = func() interface{} { return new(capella.LightClientOptimisticUpdate) }

objs["deneb"]["SignedBeaconBlock"] = func() interface{} { return new(deneb.SignedBeaconBlock) }
objs["deneb"]["ExecutionPayload"] = func() interface{} { return new(deneb.ExecutionPayload) }
objs["deneb"]["ExecutionPayloadHeader"] = func() interface{} { return new(deneb.ExecutionPayloadHeader) }
objs["deneb"]["LightClientHeader"] = func() interface{} { return new(deneb.LightClientHeader) }
objs["deneb"]["LightClientBootstrap"] = func() interface{} { return new(deneb.LightClientBootstrap) }
objs["deneb"]["LightClientUpdate"] = func() interface{} { return new(deneb.LightClientUpdate) }
objs["deneb"]["LightClientFinalityUpdate"] = func() interface{} { return new(deneb.LightClientFinalityUpdate) }
objs["deneb"]["LightClientOptimisticUpdate"] = func() interface{} { return new(deneb.LightClientOptimisticUpdate) }
}

type RootsYAML struct {
Expand Down

0 comments on commit d9dbf06

Please sign in to comment.