From ce1810fe44a4f4e7122bc343b1b529fde4bf81b8 Mon Sep 17 00:00:00 2001 From: fearlessfe <505380967@qq.com> Date: Mon, 1 Apr 2024 23:03:02 +0800 Subject: [PATCH 1/7] feat: add lightClientHeader type --- eth2/beacon/altair/lightclient.go | 38 ++++++++++ eth2/beacon/capella/lightclient.go | 73 +++++++++++++++++++ .../ssz_static/ssz_static_test.go | 2 + 3 files changed, 113 insertions(+) create mode 100644 eth2/beacon/capella/lightclient.go diff --git a/eth2/beacon/altair/lightclient.go b/eth2/beacon/altair/lightclient.go index 26a5100..710daa7 100644 --- a/eth2/beacon/altair/lightclient.go +++ b/eth2/beacon/altair/lightclient.go @@ -220,3 +220,41 @@ func (lcu *LightClientUpdate) HashTreeRoot(spec *common.Spec, hFn tree.HashFn) c &lcu.SignatureSlot, ) } + +var LightClientHeaderType = ContainerType("LightClientHeaderType", []FieldDef{ + {"beacon", common.BeaconBlockHeaderType}, +}) + +type LightClientHeader struct { + Beacon common.BeaconBlockHeader `yaml:"beacon" json:"beacon"` +} + +func (lch *LightClientHeader) Deserialize(dr *codec.DecodingReader) error { + return dr.FixedLenContainer( + &lch.Beacon, + ) +} + +func (lch *LightClientHeader) Serialize(w *codec.EncodingWriter) error { + return w.FixedLenContainer( + &lch.Beacon, + ) +} + +func (lch *LightClientHeader) ByteLength() uint64 { + return codec.ContainerLength( + &lch.Beacon, + ) +} + +func (lch *LightClientHeader) FixedLength() uint64 { + return codec.ContainerLength( + &lch.Beacon, + ) +} + +func (lch *LightClientHeader) HashTreeRoot(hFn tree.HashFn) common.Root { + return hFn.HashTreeRoot( + &lch.Beacon, + ) +} diff --git a/eth2/beacon/capella/lightclient.go b/eth2/beacon/capella/lightclient.go new file mode 100644 index 0000000..4f58b30 --- /dev/null +++ b/eth2/beacon/capella/lightclient.go @@ -0,0 +1,73 @@ +package capella + +import ( + "github.com/protolambda/zrnt/eth2/beacon/common" + "github.com/protolambda/ztyp/codec" + "github.com/protolambda/ztyp/tree" + "github.com/protolambda/ztyp/view" +) + +// https://github.com/ethereum/consensus-specs/blob/dev/specs/capella/light-client/sync-protocol.md +const ExecutionBranchLength = 4 + +var ExecutionBranchType = view.VectorType(common.Bytes32Type, ExecutionBranchLength) + +type ExecutionBranch [ExecutionBranchLength]common.Bytes32 + +func (eb *ExecutionBranch) Deserialize(dr *codec.DecodingReader) error { + roots := eb[:] + return tree.ReadRoots(dr, &roots, 4) +} + +func (eb *ExecutionBranch) FixedLength() uint64 { + return ExecutionBranchType.TypeByteLength() +} + +func (eb *ExecutionBranch) Serialize(w *codec.EncodingWriter) error { + return tree.WriteRoots(w, eb[:]) +} + +func (eb *ExecutionBranch) ByteLength() (out uint64) { + return ExecutionBranchType.TypeByteLength() +} + +func (eb *ExecutionBranch) HashTreeRoot(hFn tree.HashFn) common.Root { + return hFn.ComplexVectorHTR(func(i uint64) tree.HTR { + if i < ExecutionBranchLength { + return &eb[i] + } + return nil + }, ExecutionBranchLength) +} + +type LightClientHeader struct { + Beacon common.BeaconBlockHeader + Execution ExecutionPayloadHeader + ExecutionBranch ExecutionBranch +} + +var LightClientHeaderType = view.ContainerType("LightClientHeader", []view.FieldDef{ + {Name: "beacon", Type: common.BeaconBlockHeaderType}, + {Name: "execution", Type: ExecutionPayloadHeaderType}, + {Name: "execution_branch", Type: 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 LightClientHeaderType.TypeByteLength() +} + +func (l *LightClientHeader) HashTreeRoot(h tree.HashFn) common.Root { + return h.HashTreeRoot(&l.Beacon, &l.Execution, &l.ExecutionBranch) +} diff --git a/tests/spec/test_runners/ssz_static/ssz_static_test.go b/tests/spec/test_runners/ssz_static/ssz_static_test.go index 6a9819e..605039f 100644 --- a/tests/spec/test_runners/ssz_static/ssz_static_test.go +++ b/tests/spec/test_runners/ssz_static/ssz_static_test.go @@ -148,6 +148,7 @@ func init() { objs["altair"]["LightClientSnapshot"] = func() interface{} { return new(altair.LightClientSnapshot) } objs["altair"]["LightClientUpdate"] = func() interface{} { return new(altair.LightClientUpdate) } + objs["altair"]["LightClientHeader"] = func() interface{} { return new(altair.LightClientHeader) } objs["altair"]["SyncAggregatorSelectionData"] = func() interface{} { return new(altair.SyncAggregatorSelectionData) } objs["altair"]["SyncCommitteeContribution"] = func() interface{} { return new(altair.SyncCommitteeContribution) } objs["altair"]["ContributionAndProof"] = func() interface{} { return new(altair.ContributionAndProof) } @@ -172,6 +173,7 @@ func init() { objs["capella"]["Withdrawal"] = func() interface{} { return new(common.Withdrawal) } objs["capella"]["BLSToExecutionChange"] = func() interface{} { return new(common.BLSToExecutionChange) } objs["capella"]["SignedBLSToExecutionChange"] = func() interface{} { return new(common.SignedBLSToExecutionChange) } + objs["capella"]["LightClientHeader"] = func() interface{} { return new(capella.LightClientHeader) } } type RootsYAML struct { From fdfcb72b8376066db06ef0c8874ff9b9e80b9cac Mon Sep 17 00:00:00 2001 From: pengzhen Date: Tue, 2 Apr 2024 18:14:03 +0800 Subject: [PATCH 2/7] feat: add LightClientBootstrap type --- eth2/beacon/altair/lightclient.go | 38 ++++++++++++++++++ eth2/beacon/capella/lightclient.go | 39 +++++++++++++++++++ .../ssz_static/ssz_static_test.go | 2 + 3 files changed, 79 insertions(+) diff --git a/eth2/beacon/altair/lightclient.go b/eth2/beacon/altair/lightclient.go index 710daa7..4b5be43 100644 --- a/eth2/beacon/altair/lightclient.go +++ b/eth2/beacon/altair/lightclient.go @@ -258,3 +258,41 @@ func (lch *LightClientHeader) HashTreeRoot(hFn tree.HashFn) common.Root { &lch.Beacon, ) } + +type LightClientBootstrap struct { + Header LightClientHeader + CurrentSyncCommittee common.SyncCommittee + CurrentSyncCommitteeBranch SyncCommitteeProofBranch +} + +func NewLightClientBootstrapType(spec *common.Spec) *ContainerTypeDef { + return ContainerType("LightClientHeader", []FieldDef{ + {Name: "header", Type: LightClientHeaderType}, + {Name: "next_sync_committee", Type: common.SyncCommitteeType(spec)}, + {Name: "next_sync_committee_branch", Type: SyncCommitteeProofBranchType}, + }) +} + +func (lcb *LightClientBootstrap) FixedLength(spec *common.Spec) uint64 { + return codec.ContainerLength(&lcb.Header, spec.Wrap(&lcb.CurrentSyncCommittee), &lcb.CurrentSyncCommitteeBranch) +} + +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, + ) +} diff --git a/eth2/beacon/capella/lightclient.go b/eth2/beacon/capella/lightclient.go index 4f58b30..49bb8fb 100644 --- a/eth2/beacon/capella/lightclient.go +++ b/eth2/beacon/capella/lightclient.go @@ -1,6 +1,7 @@ package capella import ( + "github.com/protolambda/zrnt/eth2/beacon/altair" "github.com/protolambda/zrnt/eth2/beacon/common" "github.com/protolambda/ztyp/codec" "github.com/protolambda/ztyp/tree" @@ -71,3 +72,41 @@ func (l *LightClientHeader) ByteLength() (out uint64) { func (l *LightClientHeader) HashTreeRoot(h tree.HashFn) common.Root { return h.HashTreeRoot(&l.Beacon, &l.Execution, &l.ExecutionBranch) } + +type LightClientBootstrap struct { + Header LightClientHeader + CurrentSyncCommittee common.SyncCommittee + CurrentSyncCommitteeBranch altair.SyncCommitteeProofBranch +} + +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 codec.ContainerLength(&lcb.Header, spec.Wrap(&lcb.CurrentSyncCommittee), &lcb.CurrentSyncCommitteeBranch) +} + +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, + ) +} diff --git a/tests/spec/test_runners/ssz_static/ssz_static_test.go b/tests/spec/test_runners/ssz_static/ssz_static_test.go index 605039f..83692b6 100644 --- a/tests/spec/test_runners/ssz_static/ssz_static_test.go +++ b/tests/spec/test_runners/ssz_static/ssz_static_test.go @@ -149,6 +149,7 @@ func init() { objs["altair"]["LightClientSnapshot"] = func() interface{} { return new(altair.LightClientSnapshot) } objs["altair"]["LightClientUpdate"] = func() interface{} { return new(altair.LightClientUpdate) } objs["altair"]["LightClientHeader"] = func() interface{} { return new(altair.LightClientHeader) } + objs["altair"]["LightClientBootstrap"] = func() interface{} { return new(altair.LightClientBootstrap) } objs["altair"]["SyncAggregatorSelectionData"] = func() interface{} { return new(altair.SyncAggregatorSelectionData) } objs["altair"]["SyncCommitteeContribution"] = func() interface{} { return new(altair.SyncCommitteeContribution) } objs["altair"]["ContributionAndProof"] = func() interface{} { return new(altair.ContributionAndProof) } @@ -174,6 +175,7 @@ func init() { objs["capella"]["BLSToExecutionChange"] = func() interface{} { return new(common.BLSToExecutionChange) } objs["capella"]["SignedBLSToExecutionChange"] = func() interface{} { return new(common.SignedBLSToExecutionChange) } objs["capella"]["LightClientHeader"] = func() interface{} { return new(capella.LightClientHeader) } + objs["capella"]["LightClientBootstrap"] = func() interface{} { return new(capella.LightClientBootstrap) } } type RootsYAML struct { From d84c1adff5bb9ee79a86668330414eab49ac6015 Mon Sep 17 00:00:00 2001 From: fearlessfe <505380967@qq.com> Date: Wed, 3 Apr 2024 08:27:52 +0800 Subject: [PATCH 3/7] feat: add lightclient types 1. LightClientFinalityUpdate and LightClientOptimisticUpdate 2. upgrade the testcases --- eth2/beacon/altair/lightclient.go | 98 +++++++++- eth2/beacon/capella/lightclient.go | 171 ++++++++++++++++++ .../ssz_static/ssz_static_test.go | 35 ++++ 3 files changed, 297 insertions(+), 7 deletions(-) diff --git a/eth2/beacon/altair/lightclient.go b/eth2/beacon/altair/lightclient.go index 4b5be43..32a390c 100644 --- a/eth2/beacon/altair/lightclient.go +++ b/eth2/beacon/altair/lightclient.go @@ -148,7 +148,7 @@ func LightClientUpdateType(spec *common.Spec) *ContainerTypeDef { type LightClientUpdate struct { // Update beacon block header - AttestedHeader common.BeaconBlockHeader `yaml:"attested_header" json:"attested_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 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 } var LightClientHeaderType = ContainerType("LightClientHeaderType", []FieldDef{ - {"beacon", common.BeaconBlockHeaderType}, + {Name: "beacon", Type: common.BeaconBlockHeaderType}, }) type LightClientHeader struct { @@ -260,16 +260,16 @@ func (lch *LightClientHeader) HashTreeRoot(hFn tree.HashFn) common.Root { } type LightClientBootstrap struct { - Header LightClientHeader - CurrentSyncCommittee common.SyncCommittee - CurrentSyncCommitteeBranch SyncCommitteeProofBranch + Header LightClientHeader `yaml:"header" json:"header"` + CurrentSyncCommittee common.SyncCommittee `yaml:"current_sync_committee" json:"current_sync_committee"` + CurrentSyncCommitteeBranch SyncCommitteeProofBranch `yaml:"current_sync_committee_branch" json:"current_sync_committee_branch"` } func NewLightClientBootstrapType(spec *common.Spec) *ContainerTypeDef { return ContainerType("LightClientHeader", []FieldDef{ {Name: "header", Type: LightClientHeaderType}, - {Name: "next_sync_committee", Type: common.SyncCommitteeType(spec)}, - {Name: "next_sync_committee_branch", Type: SyncCommitteeProofBranchType}, + {Name: "current_sync_committee", Type: common.SyncCommitteeType(spec)}, + {Name: "current_sync_committee_branch", Type: SyncCommitteeProofBranchType}, }) } @@ -296,3 +296,87 @@ func (lcb *LightClientBootstrap) HashTreeRoot(spec *common.Spec, hFn tree.HashFn &lcb.CurrentSyncCommitteeBranch, ) } + +type LightClientFinalityUpdate struct { + AttestedHeader LightClientHeader `yaml:"attested_header" json:"attested_header"` + FinalizedHeader common.BeaconBlockHeader `yaml:"finalized_header" json:"finalized_header"` + FinalityBranch FinalizedRootProofBranch `yaml:"finality_branch" json:"finality_branch"` + SyncAggregate SyncAggregate `yaml:"sync_aggregate" json:"sync_aggregate"` + SignatureSlot common.Slot `yaml:"signature_slot" json:"signature_slot"` +} + +func LightClientFinalityUpdateType(spec *common.Spec) *ContainerTypeDef { + return ContainerType("SyncCommittee", []FieldDef{ + {Name: "attested_header", Type: common.BeaconBlockHeaderType}, + {Name: "finalized_header", Type: common.BeaconBlockHeaderType}, + {Name: "finality_branch", Type: FinalizedRootProofBranchType}, + {Name: "sync_aggregate", Type: SyncAggregateType(spec)}, + {Name: "signature_slot", Type: common.SlotType}, + }) +} + +func (lcfu *LightClientFinalityUpdate) FixedLength(spec *common.Spec) uint64 { + return codec.ContainerLength(&lcfu.AttestedHeader, &lcfu.FinalizedHeader, &lcfu.FinalityBranch, spec.Wrap(&lcfu.SyncAggregate), &lcfu.SignatureSlot) +} + +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 SyncAggregate `yaml:"sync_aggregate" json:"sync_aggregate"` + SignatureSlot common.Slot `yaml:"signature_slot" json:"signature_slot"` +} + +func LightClientOptimisticUpdateType(spec *common.Spec) *ContainerTypeDef { + return ContainerType("SyncCommittee", []FieldDef{ + {Name: "attested_header", Type: common.BeaconBlockHeaderType}, + {Name: "finalized_header", Type: common.BeaconBlockHeaderType}, + {Name: "finality_branch", Type: FinalizedRootProofBranchType}, + {Name: "sync_aggregate", Type: SyncAggregateType(spec)}, + {Name: "signature_slot", Type: common.SlotType}, + }) +} + +func (lcou *LightClientOptimisticUpdate) FixedLength(spec *common.Spec) uint64 { + return codec.ContainerLength(&lcou.AttestedHeader, spec.Wrap(&lcou.SyncAggregate), &lcou.SignatureSlot) +} + +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, + ) +} diff --git a/eth2/beacon/capella/lightclient.go b/eth2/beacon/capella/lightclient.go index 49bb8fb..65115b0 100644 --- a/eth2/beacon/capella/lightclient.go +++ b/eth2/beacon/capella/lightclient.go @@ -110,3 +110,174 @@ func (lcb *LightClientBootstrap) HashTreeRoot(spec *common.Spec, hFn tree.HashFn &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: common.BeaconBlockHeaderType}, + {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 common.BeaconBlockHeader `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.FixedLenContainer( + &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.FixedLenContainer( + &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 codec.ContainerLength( + &lcu.AttestedHeader, + spec.Wrap(&lcu.NextSyncCommittee), + &lcu.NextSyncCommitteeBranch, + &lcu.FinalizedHeader, + &lcu.FinalityBranch, + spec.Wrap(&lcu.SyncAggregate), + &lcu.SignatureSlot, + ) +} + +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 common.BeaconBlockHeader `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: common.BeaconBlockHeaderType}, + {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 codec.ContainerLength(&lcfu.AttestedHeader, &lcfu.FinalizedHeader, &lcfu.FinalityBranch, spec.Wrap(&lcfu.SyncAggregate), &lcfu.SignatureSlot) +} + +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 codec.ContainerLength(&lcou.AttestedHeader, spec.Wrap(&lcou.SyncAggregate), &lcou.SignatureSlot) +} + +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, + ) +} diff --git a/tests/spec/test_runners/ssz_static/ssz_static_test.go b/tests/spec/test_runners/ssz_static/ssz_static_test.go index 83692b6..96493ce 100644 --- a/tests/spec/test_runners/ssz_static/ssz_static_test.go +++ b/tests/spec/test_runners/ssz_static/ssz_static_test.go @@ -150,6 +150,8 @@ func init() { objs["altair"]["LightClientUpdate"] = func() interface{} { return new(altair.LightClientUpdate) } objs["altair"]["LightClientHeader"] = func() interface{} { return new(altair.LightClientHeader) } objs["altair"]["LightClientBootstrap"] = func() interface{} { return new(altair.LightClientBootstrap) } + objs["altair"]["LightClientFinalityUpdate"] = func() interface{} { return new(altair.LightClientFinalityUpdate) } + objs["altair"]["LightClientOptimisticUpdate"] = func() interface{} { return new(altair.LightClientOptimisticUpdate) } objs["altair"]["SyncAggregatorSelectionData"] = func() interface{} { return new(altair.SyncAggregatorSelectionData) } objs["altair"]["SyncCommitteeContribution"] = func() interface{} { return new(altair.SyncCommitteeContribution) } objs["altair"]["ContributionAndProof"] = func() interface{} { return new(altair.ContributionAndProof) } @@ -176,6 +178,9 @@ func init() { objs["capella"]["SignedBLSToExecutionChange"] = func() interface{} { return new(common.SignedBLSToExecutionChange) } objs["capella"]["LightClientHeader"] = func() interface{} { return new(capella.LightClientHeader) } objs["capella"]["LightClientBootstrap"] = func() interface{} { return new(capella.LightClientBootstrap) } + 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) } } type RootsYAML struct { @@ -246,3 +251,33 @@ func TestSSZStatic(t *testing.T) { } }) } + +func TestSSZStatic2(t *testing.T) { + var objs = map[test_util.ForkName]map[string]ObjAllocator{ + "phase0": {}, + "altair": {}, + "bellatrix": {}, + "capella": {}, + } + //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) } + t.Run("minimal", func(t *testing.T) { + for fork, objByName := range objs { + t.Run(string(fork), func(t *testing.T) { + for k, v := range objByName { + t.Run(k, runSSZStaticTest(fork, k, v, configs.Minimal)) + } + }) + } + }) + t.Run("mainnet", func(t *testing.T) { + for fork, objByName := range objs { + t.Run(string(fork), func(t *testing.T) { + for k, v := range objByName { + t.Run(k, runSSZStaticTest(fork, k, v, configs.Mainnet)) + } + }) + } + }) +} From 235b70f0f7bdfa20f6c3ba9878436fbfc1add835 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BD=AD=E6=8C=AF?= <505380967@qq.com> Date: Wed, 3 Apr 2024 21:19:25 +0800 Subject: [PATCH 4/7] Revert "feat: add lightclient types" --- eth2/beacon/altair/lightclient.go | 98 +--------- eth2/beacon/capella/lightclient.go | 171 ------------------ .../ssz_static/ssz_static_test.go | 35 ---- 3 files changed, 7 insertions(+), 297 deletions(-) diff --git a/eth2/beacon/altair/lightclient.go b/eth2/beacon/altair/lightclient.go index 32a390c..4b5be43 100644 --- a/eth2/beacon/altair/lightclient.go +++ b/eth2/beacon/altair/lightclient.go @@ -148,7 +148,7 @@ func LightClientUpdateType(spec *common.Spec) *ContainerTypeDef { type LightClientUpdate struct { // Update beacon block header - AttestedHeader LightClientHeader `yaml:"attested_header" json:"attested_header"` + AttestedHeader common.BeaconBlockHeader `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 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 } var LightClientHeaderType = ContainerType("LightClientHeaderType", []FieldDef{ - {Name: "beacon", Type: common.BeaconBlockHeaderType}, + {"beacon", common.BeaconBlockHeaderType}, }) type LightClientHeader struct { @@ -260,16 +260,16 @@ func (lch *LightClientHeader) HashTreeRoot(hFn tree.HashFn) common.Root { } type LightClientBootstrap struct { - Header LightClientHeader `yaml:"header" json:"header"` - CurrentSyncCommittee common.SyncCommittee `yaml:"current_sync_committee" json:"current_sync_committee"` - CurrentSyncCommitteeBranch SyncCommitteeProofBranch `yaml:"current_sync_committee_branch" json:"current_sync_committee_branch"` + Header LightClientHeader + CurrentSyncCommittee common.SyncCommittee + CurrentSyncCommitteeBranch SyncCommitteeProofBranch } func NewLightClientBootstrapType(spec *common.Spec) *ContainerTypeDef { return ContainerType("LightClientHeader", []FieldDef{ {Name: "header", Type: LightClientHeaderType}, - {Name: "current_sync_committee", Type: common.SyncCommitteeType(spec)}, - {Name: "current_sync_committee_branch", Type: SyncCommitteeProofBranchType}, + {Name: "next_sync_committee", Type: common.SyncCommitteeType(spec)}, + {Name: "next_sync_committee_branch", Type: SyncCommitteeProofBranchType}, }) } @@ -296,87 +296,3 @@ func (lcb *LightClientBootstrap) HashTreeRoot(spec *common.Spec, hFn tree.HashFn &lcb.CurrentSyncCommitteeBranch, ) } - -type LightClientFinalityUpdate struct { - AttestedHeader LightClientHeader `yaml:"attested_header" json:"attested_header"` - FinalizedHeader common.BeaconBlockHeader `yaml:"finalized_header" json:"finalized_header"` - FinalityBranch FinalizedRootProofBranch `yaml:"finality_branch" json:"finality_branch"` - SyncAggregate SyncAggregate `yaml:"sync_aggregate" json:"sync_aggregate"` - SignatureSlot common.Slot `yaml:"signature_slot" json:"signature_slot"` -} - -func LightClientFinalityUpdateType(spec *common.Spec) *ContainerTypeDef { - return ContainerType("SyncCommittee", []FieldDef{ - {Name: "attested_header", Type: common.BeaconBlockHeaderType}, - {Name: "finalized_header", Type: common.BeaconBlockHeaderType}, - {Name: "finality_branch", Type: FinalizedRootProofBranchType}, - {Name: "sync_aggregate", Type: SyncAggregateType(spec)}, - {Name: "signature_slot", Type: common.SlotType}, - }) -} - -func (lcfu *LightClientFinalityUpdate) FixedLength(spec *common.Spec) uint64 { - return codec.ContainerLength(&lcfu.AttestedHeader, &lcfu.FinalizedHeader, &lcfu.FinalityBranch, spec.Wrap(&lcfu.SyncAggregate), &lcfu.SignatureSlot) -} - -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 SyncAggregate `yaml:"sync_aggregate" json:"sync_aggregate"` - SignatureSlot common.Slot `yaml:"signature_slot" json:"signature_slot"` -} - -func LightClientOptimisticUpdateType(spec *common.Spec) *ContainerTypeDef { - return ContainerType("SyncCommittee", []FieldDef{ - {Name: "attested_header", Type: common.BeaconBlockHeaderType}, - {Name: "finalized_header", Type: common.BeaconBlockHeaderType}, - {Name: "finality_branch", Type: FinalizedRootProofBranchType}, - {Name: "sync_aggregate", Type: SyncAggregateType(spec)}, - {Name: "signature_slot", Type: common.SlotType}, - }) -} - -func (lcou *LightClientOptimisticUpdate) FixedLength(spec *common.Spec) uint64 { - return codec.ContainerLength(&lcou.AttestedHeader, spec.Wrap(&lcou.SyncAggregate), &lcou.SignatureSlot) -} - -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, - ) -} diff --git a/eth2/beacon/capella/lightclient.go b/eth2/beacon/capella/lightclient.go index 65115b0..49bb8fb 100644 --- a/eth2/beacon/capella/lightclient.go +++ b/eth2/beacon/capella/lightclient.go @@ -110,174 +110,3 @@ func (lcb *LightClientBootstrap) HashTreeRoot(spec *common.Spec, hFn tree.HashFn &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: common.BeaconBlockHeaderType}, - {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 common.BeaconBlockHeader `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.FixedLenContainer( - &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.FixedLenContainer( - &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 codec.ContainerLength( - &lcu.AttestedHeader, - spec.Wrap(&lcu.NextSyncCommittee), - &lcu.NextSyncCommitteeBranch, - &lcu.FinalizedHeader, - &lcu.FinalityBranch, - spec.Wrap(&lcu.SyncAggregate), - &lcu.SignatureSlot, - ) -} - -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 common.BeaconBlockHeader `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: common.BeaconBlockHeaderType}, - {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 codec.ContainerLength(&lcfu.AttestedHeader, &lcfu.FinalizedHeader, &lcfu.FinalityBranch, spec.Wrap(&lcfu.SyncAggregate), &lcfu.SignatureSlot) -} - -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 codec.ContainerLength(&lcou.AttestedHeader, spec.Wrap(&lcou.SyncAggregate), &lcou.SignatureSlot) -} - -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, - ) -} diff --git a/tests/spec/test_runners/ssz_static/ssz_static_test.go b/tests/spec/test_runners/ssz_static/ssz_static_test.go index 96493ce..83692b6 100644 --- a/tests/spec/test_runners/ssz_static/ssz_static_test.go +++ b/tests/spec/test_runners/ssz_static/ssz_static_test.go @@ -150,8 +150,6 @@ func init() { objs["altair"]["LightClientUpdate"] = func() interface{} { return new(altair.LightClientUpdate) } objs["altair"]["LightClientHeader"] = func() interface{} { return new(altair.LightClientHeader) } objs["altair"]["LightClientBootstrap"] = func() interface{} { return new(altair.LightClientBootstrap) } - objs["altair"]["LightClientFinalityUpdate"] = func() interface{} { return new(altair.LightClientFinalityUpdate) } - objs["altair"]["LightClientOptimisticUpdate"] = func() interface{} { return new(altair.LightClientOptimisticUpdate) } objs["altair"]["SyncAggregatorSelectionData"] = func() interface{} { return new(altair.SyncAggregatorSelectionData) } objs["altair"]["SyncCommitteeContribution"] = func() interface{} { return new(altair.SyncCommitteeContribution) } objs["altair"]["ContributionAndProof"] = func() interface{} { return new(altair.ContributionAndProof) } @@ -178,9 +176,6 @@ func init() { objs["capella"]["SignedBLSToExecutionChange"] = func() interface{} { return new(common.SignedBLSToExecutionChange) } objs["capella"]["LightClientHeader"] = func() interface{} { return new(capella.LightClientHeader) } objs["capella"]["LightClientBootstrap"] = func() interface{} { return new(capella.LightClientBootstrap) } - 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) } } type RootsYAML struct { @@ -251,33 +246,3 @@ func TestSSZStatic(t *testing.T) { } }) } - -func TestSSZStatic2(t *testing.T) { - var objs = map[test_util.ForkName]map[string]ObjAllocator{ - "phase0": {}, - "altair": {}, - "bellatrix": {}, - "capella": {}, - } - //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) } - t.Run("minimal", func(t *testing.T) { - for fork, objByName := range objs { - t.Run(string(fork), func(t *testing.T) { - for k, v := range objByName { - t.Run(k, runSSZStaticTest(fork, k, v, configs.Minimal)) - } - }) - } - }) - t.Run("mainnet", func(t *testing.T) { - for fork, objByName := range objs { - t.Run(string(fork), func(t *testing.T) { - for k, v := range objByName { - t.Run(k, runSSZStaticTest(fork, k, v, configs.Mainnet)) - } - }) - } - }) -} From 04d1d446b0dad1331291575f808e5e979d214a57 Mon Sep 17 00:00:00 2001 From: fearlessfe <505380967@qq.com> Date: Wed, 3 Apr 2024 08:27:52 +0800 Subject: [PATCH 5/7] feat: add lightclient types 1. LightClientFinalityUpdate and LightClientOptimisticUpdate 2. upgrade the testcases --- Makefile | 2 +- eth2/beacon/altair/lightclient.go | 104 +++++++++- eth2/beacon/capella/lightclient.go | 185 +++++++++++++++++- .../ssz_static/ssz_static_test.go | 5 + 4 files changed, 278 insertions(+), 18 deletions(-) diff --git a/Makefile b/Makefile index e87459a..15078f8 100644 --- a/Makefile +++ b/Makefile @@ -8,7 +8,7 @@ clean: create-test-dir: mkdir -p $(TEST_OUT_DIR) -SPEC_VERSION ?= v1.4.0-beta.7-hotfix +SPEC_VERSION ?= v1.4.0 clear-tests: rm -rf tests/spec/eth2.0-spec-tests diff --git a/eth2/beacon/altair/lightclient.go b/eth2/beacon/altair/lightclient.go index 4b5be43..39cf018 100644 --- a/eth2/beacon/altair/lightclient.go +++ b/eth2/beacon/altair/lightclient.go @@ -136,10 +136,10 @@ func (fb FinalizedRootProofBranch) HashTreeRoot(hFn tree.HashFn) common.Root { func LightClientUpdateType(spec *common.Spec) *ContainerTypeDef { return ContainerType("SyncCommittee", []FieldDef{ - {"attested_header", common.BeaconBlockHeaderType}, + {"attested_header", LightClientHeaderType}, {"next_sync_committee", common.SyncCommitteeType(spec)}, {"next_sync_committee_branch", SyncCommitteeProofBranchType}, - {"finalized_header", common.BeaconBlockHeaderType}, + {"finalized_header", LightClientHeaderType}, {"finality_branch", FinalizedRootProofBranchType}, {"sync_aggregate", SyncAggregateType(spec)}, {"signature_slot", common.SlotType}, @@ -148,12 +148,12 @@ func LightClientUpdateType(spec *common.Spec) *ContainerTypeDef { type LightClientUpdate struct { // Update beacon block header - AttestedHeader common.BeaconBlockHeader `yaml:"attested_header" json:"attested_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 SyncCommitteeProofBranch `yaml:"next_sync_committee_branch" json:"next_sync_committee_branch"` // Finality proof for the update header - FinalizedHeader common.BeaconBlockHeader `yaml:"finalized_header" json:"finalized_header"` + FinalizedHeader LightClientHeader `yaml:"finalized_header" json:"finalized_header"` FinalityBranch FinalizedRootProofBranch `yaml:"finality_branch" json:"finality_branch"` // Sync committee aggregate signature SyncAggregate SyncAggregate `yaml:"sync_aggregate" json:"sync_aggregate"` @@ -222,7 +222,7 @@ func (lcu *LightClientUpdate) HashTreeRoot(spec *common.Spec, hFn tree.HashFn) c } var LightClientHeaderType = ContainerType("LightClientHeaderType", []FieldDef{ - {"beacon", common.BeaconBlockHeaderType}, + {Name: "beacon", Type: common.BeaconBlockHeaderType}, }) type LightClientHeader struct { @@ -260,16 +260,16 @@ func (lch *LightClientHeader) HashTreeRoot(hFn tree.HashFn) common.Root { } type LightClientBootstrap struct { - Header LightClientHeader - CurrentSyncCommittee common.SyncCommittee - CurrentSyncCommitteeBranch SyncCommitteeProofBranch + Header LightClientHeader `yaml:"header" json:"header"` + CurrentSyncCommittee common.SyncCommittee `yaml:"current_sync_committee" json:"current_sync_committee"` + CurrentSyncCommitteeBranch SyncCommitteeProofBranch `yaml:"current_sync_committee_branch" json:"current_sync_committee_branch"` } func NewLightClientBootstrapType(spec *common.Spec) *ContainerTypeDef { return ContainerType("LightClientHeader", []FieldDef{ {Name: "header", Type: LightClientHeaderType}, - {Name: "next_sync_committee", Type: common.SyncCommitteeType(spec)}, - {Name: "next_sync_committee_branch", Type: SyncCommitteeProofBranchType}, + {Name: "current_sync_committee", Type: common.SyncCommitteeType(spec)}, + {Name: "current_sync_committee_branch", Type: SyncCommitteeProofBranchType}, }) } @@ -296,3 +296,87 @@ func (lcb *LightClientBootstrap) HashTreeRoot(spec *common.Spec, hFn tree.HashFn &lcb.CurrentSyncCommitteeBranch, ) } + +type LightClientFinalityUpdate struct { + AttestedHeader LightClientHeader `yaml:"attested_header" json:"attested_header"` + FinalizedHeader common.BeaconBlockHeader `yaml:"finalized_header" json:"finalized_header"` + FinalityBranch FinalizedRootProofBranch `yaml:"finality_branch" json:"finality_branch"` + SyncAggregate SyncAggregate `yaml:"sync_aggregate" json:"sync_aggregate"` + SignatureSlot common.Slot `yaml:"signature_slot" json:"signature_slot"` +} + +func LightClientFinalityUpdateType(spec *common.Spec) *ContainerTypeDef { + return ContainerType("SyncCommittee", []FieldDef{ + {Name: "attested_header", Type: common.BeaconBlockHeaderType}, + {Name: "finalized_header", Type: common.BeaconBlockHeaderType}, + {Name: "finality_branch", Type: FinalizedRootProofBranchType}, + {Name: "sync_aggregate", Type: SyncAggregateType(spec)}, + {Name: "signature_slot", Type: common.SlotType}, + }) +} + +func (lcfu *LightClientFinalityUpdate) FixedLength(spec *common.Spec) uint64 { + return codec.ContainerLength(&lcfu.AttestedHeader, &lcfu.FinalizedHeader, &lcfu.FinalityBranch, spec.Wrap(&lcfu.SyncAggregate), &lcfu.SignatureSlot) +} + +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 SyncAggregate `yaml:"sync_aggregate" json:"sync_aggregate"` + SignatureSlot common.Slot `yaml:"signature_slot" json:"signature_slot"` +} + +func LightClientOptimisticUpdateType(spec *common.Spec) *ContainerTypeDef { + return ContainerType("SyncCommittee", []FieldDef{ + {Name: "attested_header", Type: common.BeaconBlockHeaderType}, + {Name: "finalized_header", Type: common.BeaconBlockHeaderType}, + {Name: "finality_branch", Type: FinalizedRootProofBranchType}, + {Name: "sync_aggregate", Type: SyncAggregateType(spec)}, + {Name: "signature_slot", Type: common.SlotType}, + }) +} + +func (lcou *LightClientOptimisticUpdate) FixedLength(spec *common.Spec) uint64 { + return codec.ContainerLength(&lcou.AttestedHeader, spec.Wrap(&lcou.SyncAggregate), &lcou.SignatureSlot) +} + +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, + ) +} diff --git a/eth2/beacon/capella/lightclient.go b/eth2/beacon/capella/lightclient.go index 49bb8fb..deb41d7 100644 --- a/eth2/beacon/capella/lightclient.go +++ b/eth2/beacon/capella/lightclient.go @@ -42,9 +42,9 @@ func (eb *ExecutionBranch) HashTreeRoot(hFn tree.HashFn) common.Root { } type LightClientHeader struct { - Beacon common.BeaconBlockHeader - Execution ExecutionPayloadHeader - ExecutionBranch ExecutionBranch + Beacon common.BeaconBlockHeader `yaml:"beacon" json:"beacon"` + Execution ExecutionPayloadHeader `yaml:"execution" json:"execution"` + ExecutionBranch ExecutionBranch `yaml:"execution_branch" json:"execution_branch"` } var LightClientHeaderType = view.ContainerType("LightClientHeader", []view.FieldDef{ @@ -66,7 +66,7 @@ func (l *LightClientHeader) Serialize(w *codec.EncodingWriter) error { } func (l *LightClientHeader) ByteLength() (out uint64) { - return LightClientHeaderType.TypeByteLength() + return codec.ContainerLength(&l.Beacon, &l.Execution, &l.ExecutionBranch) } func (l *LightClientHeader) HashTreeRoot(h tree.HashFn) common.Root { @@ -74,9 +74,9 @@ func (l *LightClientHeader) HashTreeRoot(h tree.HashFn) common.Root { } type LightClientBootstrap struct { - Header LightClientHeader - CurrentSyncCommittee common.SyncCommittee - CurrentSyncCommitteeBranch altair.SyncCommitteeProofBranch + 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 { @@ -110,3 +110,174 @@ func (lcb *LightClientBootstrap) HashTreeRoot(spec *common.Spec, hFn tree.HashFn &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 codec.ContainerLength( + &lcu.AttestedHeader, + spec.Wrap(&lcu.NextSyncCommittee), + &lcu.NextSyncCommitteeBranch, + &lcu.FinalizedHeader, + &lcu.FinalityBranch, + spec.Wrap(&lcu.SyncAggregate), + &lcu.SignatureSlot, + ) +} + +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 codec.ContainerLength(&lcfu.AttestedHeader, &lcfu.FinalizedHeader, &lcfu.FinalityBranch, spec.Wrap(&lcfu.SyncAggregate), &lcfu.SignatureSlot) +} + +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 codec.ContainerLength(&lcou.AttestedHeader, spec.Wrap(&lcou.SyncAggregate), &lcou.SignatureSlot) +} + +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, + ) +} diff --git a/tests/spec/test_runners/ssz_static/ssz_static_test.go b/tests/spec/test_runners/ssz_static/ssz_static_test.go index 83692b6..3441b12 100644 --- a/tests/spec/test_runners/ssz_static/ssz_static_test.go +++ b/tests/spec/test_runners/ssz_static/ssz_static_test.go @@ -150,6 +150,8 @@ func init() { objs["altair"]["LightClientUpdate"] = func() interface{} { return new(altair.LightClientUpdate) } objs["altair"]["LightClientHeader"] = func() interface{} { return new(altair.LightClientHeader) } objs["altair"]["LightClientBootstrap"] = func() interface{} { return new(altair.LightClientBootstrap) } + objs["altair"]["LightClientFinalityUpdate"] = func() interface{} { return new(altair.LightClientFinalityUpdate) } + objs["altair"]["LightClientOptimisticUpdate"] = func() interface{} { return new(altair.LightClientOptimisticUpdate) } objs["altair"]["SyncAggregatorSelectionData"] = func() interface{} { return new(altair.SyncAggregatorSelectionData) } objs["altair"]["SyncCommitteeContribution"] = func() interface{} { return new(altair.SyncCommitteeContribution) } objs["altair"]["ContributionAndProof"] = func() interface{} { return new(altair.ContributionAndProof) } @@ -176,6 +178,9 @@ func init() { objs["capella"]["SignedBLSToExecutionChange"] = func() interface{} { return new(common.SignedBLSToExecutionChange) } objs["capella"]["LightClientHeader"] = func() interface{} { return new(capella.LightClientHeader) } objs["capella"]["LightClientBootstrap"] = func() interface{} { return new(capella.LightClientBootstrap) } + 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) } } type RootsYAML struct { From 89802fe410f59e35cefc2422b17f8de391fcaf6a Mon Sep 17 00:00:00 2001 From: pengzhen Date: Mon, 8 Apr 2024 13:20:07 +0800 Subject: [PATCH 6/7] fix: FixedLength method err --- eth2/beacon/capella/lightclient.go | 16 ++++------------ 1 file changed, 4 insertions(+), 12 deletions(-) diff --git a/eth2/beacon/capella/lightclient.go b/eth2/beacon/capella/lightclient.go index deb41d7..7f75f70 100644 --- a/eth2/beacon/capella/lightclient.go +++ b/eth2/beacon/capella/lightclient.go @@ -88,7 +88,7 @@ func NewLightClientBootstrapType(spec *common.Spec) *view.ContainerTypeDef { } func (lcb *LightClientBootstrap) FixedLength(spec *common.Spec) uint64 { - return codec.ContainerLength(&lcb.Header, spec.Wrap(&lcb.CurrentSyncCommittee), &lcb.CurrentSyncCommitteeBranch) + return 0 } func (lcb *LightClientBootstrap) Deserialize(spec *common.Spec, dr *codec.DecodingReader) error { @@ -175,15 +175,7 @@ func (lcu *LightClientUpdate) ByteLength(spec *common.Spec) uint64 { } func (lcu *LightClientUpdate) FixedLength(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, - ) + return 0 } func (lcu *LightClientUpdate) HashTreeRoot(spec *common.Spec, hFn tree.HashFn) common.Root { @@ -217,7 +209,7 @@ func LightClientFinalityUpdateType(spec *common.Spec) *view.ContainerTypeDef { } func (lcfu *LightClientFinalityUpdate) FixedLength(spec *common.Spec) uint64 { - return codec.ContainerLength(&lcfu.AttestedHeader, &lcfu.FinalizedHeader, &lcfu.FinalityBranch, spec.Wrap(&lcfu.SyncAggregate), &lcfu.SignatureSlot) + return 0 } func (lcfu *LightClientFinalityUpdate) Deserialize(spec *common.Spec, dr *codec.DecodingReader) error { @@ -259,7 +251,7 @@ func LightClientOptimisticUpdateType(spec *common.Spec) *view.ContainerTypeDef { } func (lcou *LightClientOptimisticUpdate) FixedLength(spec *common.Spec) uint64 { - return codec.ContainerLength(&lcou.AttestedHeader, spec.Wrap(&lcou.SyncAggregate), &lcou.SignatureSlot) + return 0 } func (lcou *LightClientOptimisticUpdate) Deserialize(spec *common.Spec, dr *codec.DecodingReader) error { From d9dbf06b32f77c791416a26a1a13c1b30c649414 Mon Sep 17 00:00:00 2001 From: fearlessfe <505380967@qq.com> Date: Sun, 14 Apr 2024 18:15:02 +0800 Subject: [PATCH 7/7] feat: add lightclient type for deneb --- eth2/beacon/deneb/lightclient.go | 243 ++++++++++++++++++ .../ssz_static/ssz_static_test.go | 11 + 2 files changed, 254 insertions(+) create mode 100644 eth2/beacon/deneb/lightclient.go diff --git a/eth2/beacon/deneb/lightclient.go b/eth2/beacon/deneb/lightclient.go new file mode 100644 index 0000000..922d23b --- /dev/null +++ b/eth2/beacon/deneb/lightclient.go @@ -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, + ) +} diff --git a/tests/spec/test_runners/ssz_static/ssz_static_test.go b/tests/spec/test_runners/ssz_static/ssz_static_test.go index 3441b12..cf1bf9c 100644 --- a/tests/spec/test_runners/ssz_static/ssz_static_test.go +++ b/tests/spec/test_runners/ssz_static/ssz_static_test.go @@ -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" @@ -102,6 +103,7 @@ var objs = map[test_util.ForkName]map[string]ObjAllocator{ "altair": {}, "bellatrix": {}, "capella": {}, + "deneb": {}, } func init() { @@ -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 {