Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added tests for ssz_static in consensus tests and fixed beacon blocks encoding #7141

Merged
merged 1 commit into from
Mar 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions cl/cltypes/beacon_block.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ func (b *BeaconBody) EncodeSSZ(dst []byte) ([]byte, error) {
if len(b.AttesterSlashings) > MaxAttesterSlashings {
return nil, fmt.Errorf("Encode(SSZ): too many attester slashings")
}
if len(b.ProposerSlashings) > MaxAttesterSlashings {
if len(b.ProposerSlashings) > MaxProposerSlashings {
return nil, fmt.Errorf("Encode(SSZ): too many proposer slashings")
}
if len(b.Attestations) > MaxAttestations {
Expand Down Expand Up @@ -196,7 +196,9 @@ func (b *BeaconBody) EncodeSSZ(dst []byte) ([]byte, error) {
}

for _, deposit := range b.Deposits {
buf = deposit.EncodeSSZ(buf)
if buf, err = deposit.EncodeSSZ(buf); err != nil {
return nil, err
}
}

for _, exit := range b.VoluntaryExits {
Expand Down Expand Up @@ -253,6 +255,10 @@ func (b *BeaconBody) EncodingSizeSSZ() (size int) {
return
}

func (b *BeaconBody) DecodeSSZ(buf []byte) error {
panic("yo")
}

func (b *BeaconBody) DecodeSSZWithVersion(buf []byte, version int) error {
b.Version = clparams.StateVersion(version)
var err error
Expand Down
4 changes: 4 additions & 0 deletions cl/cltypes/checkpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ type Checkpoint struct {
Root libcommon.Hash
}

func (c *Checkpoint) DecodeSSZWithVersion(buf []byte, _ int) error {
return c.DecodeSSZ(buf)
}

func (c *Checkpoint) Equal(other *Checkpoint) bool {
return c.Epoch == other.Epoch && c.Root == other.Root
}
Expand Down
12 changes: 12 additions & 0 deletions cl/cltypes/clone.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ func (*PendingAttestation) Clone() clonable.Clonable {
return &PendingAttestation{}
}

func (*BeaconBody) Clone() clonable.Clonable {
return &BeaconBody{}
}

func (*Eth1Block) Clone() clonable.Clonable {
return &Eth1Block{}
}
Expand Down Expand Up @@ -38,6 +42,14 @@ func (*Attestation) Clone() clonable.Clonable {
return &Attestation{}
}

func (*Checkpoint) Clone() clonable.Clonable {
return &Checkpoint{}
}

func (*DepositData) Clone() clonable.Clonable {
return &DepositData{}
}

func (*Status) Clone() clonable.Clonable {
return &Status{}
}
Expand Down
13 changes: 8 additions & 5 deletions cl/cltypes/validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,13 @@ type DepositData struct {
Root libcommon.Hash // Ignored if not for hashing
}

func (d *DepositData) EncodeSSZ(dst []byte) []byte {
func (d *DepositData) EncodeSSZ(dst []byte) ([]byte, error) {
buf := dst
buf = append(buf, d.PubKey[:]...)
buf = append(buf, d.WithdrawalCredentials[:]...)
buf = append(buf, ssz.Uint64SSZ(d.Amount)...)
buf = append(buf, d.Signature[:]...)
return buf
return buf, nil
}

func (d *DepositData) DecodeSSZ(buf []byte) error {
Expand All @@ -41,6 +41,10 @@ func (d *DepositData) DecodeSSZ(buf []byte) error {
return nil
}

func (d *DepositData) DecodeSSZWithVersion(buf []byte, _ int) error {
return d.DecodeSSZ(buf)
}

func (d *DepositData) EncodingSizeSSZ() int {
return 184
}
Expand Down Expand Up @@ -83,14 +87,13 @@ type Deposit struct {
Data *DepositData
}

func (d *Deposit) EncodeSSZ(dst []byte) []byte {
func (d *Deposit) EncodeSSZ(dst []byte) ([]byte, error) {

buf := dst
for _, proofSeg := range d.Proof {
buf = append(buf, proofSeg[:]...)
}
buf = d.Data.EncodeSSZ(buf)
return buf
return d.Data.EncodeSSZ(buf)
}

func (d *Deposit) DecodeSSZ(buf []byte) error {
Expand Down
38 changes: 33 additions & 5 deletions cmd/ef-tests-cl/consensus_tests/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,18 @@ package consensustests
import (
"fmt"
"path"

"github.com/ledgerwatch/erigon/cl/clparams"
"github.com/ledgerwatch/erigon/cl/cltypes"
"github.com/ledgerwatch/erigon/cmd/erigon-cl/core/state"
)

type testFunc func(context testContext) error

var (
operationsDivision = "operations"
epochProcessingDivision = "epoch_processing"
sszDivision = "ssz_static"
)

// Epoch processing cases
Expand Down Expand Up @@ -54,6 +59,20 @@ var random = "random/random"
// transitionCore
var transitionCore = "transition/core"

// ssz_static cases

var (
validatorCase = "Validator"
beaconStateCase = "BeaconState"
checkpointCase = "Checkpoint"
depositCase = "Deposit"
depositDataCase = "DepositData"
signedBeaconBlockCase = "SignedBeaconBlock"
beaconBlockCase = "BeaconBlock"
beaconBodyCase = "BeaconBody"
// If you wanna do the rest go ahead but the important ones are all covered. also each of the above include all other encodings.
)

// Stays here bc debugging >:-(
func placeholderTest() error {
fmt.Println("hallo")
Expand Down Expand Up @@ -83,9 +102,18 @@ var handlers map[string]testFunc = map[string]testFunc{
path.Join(operationsDivision, caseVoluntaryExit): operationVoluntaryExitHandler,
path.Join(operationsDivision, caseWithdrawal): operationWithdrawalHandler,
path.Join(operationsDivision, caseBlsChange): operationSignedBlsChangeHandler,
transitionCore: transitionTestFunction,
sanityBlocks: testSanityFunction,
sanitySlots: testSanityFunctionSlot,
finality: finalityTestFunction,
random: testSanityFunction, // Same as sanity handler.
path.Join(sszDivision, validatorCase): getSSZStaticConsensusTest(&cltypes.Validator{}),
path.Join(sszDivision, beaconStateCase): getSSZStaticConsensusTest(state.New(&clparams.MainnetBeaconConfig)),
path.Join(sszDivision, checkpointCase): getSSZStaticConsensusTest(&cltypes.Checkpoint{}),
path.Join(sszDivision, depositCase): getSSZStaticConsensusTest(&cltypes.Deposit{}),
path.Join(sszDivision, depositDataCase): getSSZStaticConsensusTest(&cltypes.DepositData{}),
path.Join(sszDivision, depositDataCase): getSSZStaticConsensusTest(&cltypes.DepositData{}),
path.Join(sszDivision, signedBeaconBlockCase): getSSZStaticConsensusTest(&cltypes.SignedBeaconBlock{}),
path.Join(sszDivision, beaconBlockCase): getSSZStaticConsensusTest(&cltypes.BeaconBlock{}),
path.Join(sszDivision, beaconBodyCase): getSSZStaticConsensusTest(&cltypes.BeaconBody{}),
transitionCore: transitionTestFunction,
sanityBlocks: testSanityFunction,
sanitySlots: testSanityFunctionSlot,
finality: finalityTestFunction,
random: testSanityFunction, // Same as sanity handler.
}
76 changes: 76 additions & 0 deletions cmd/ef-tests-cl/consensus_tests/ssz_static.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package consensustests

import (
"bytes"
"fmt"
"os"

libcommon "github.com/ledgerwatch/erigon-lib/common"
"github.com/ledgerwatch/erigon/cl/cltypes/clonable"
"github.com/ledgerwatch/erigon/cl/cltypes/ssz"
"github.com/ledgerwatch/erigon/cl/utils"
"github.com/ledgerwatch/erigon/cmd/erigon-cl/core/state"

"gopkg.in/yaml.v2"
)

type unmarshalerMarshalerHashable interface {
ssz.EncodableSSZ
ssz.HashableSSZ
clonable.Clonable
}

type Root struct {
Root string `yaml:"root"`
}

const rootsFile = "roots.yaml"
const serializedFile = "serialized.ssz_snappy"

func getSSZStaticConsensusTest[T unmarshalerMarshalerHashable](ref T) testFunc {
return func(context testContext) error {
rootBytes, err := os.ReadFile(rootsFile)
if err != nil {
return err
}
root := Root{}
if err := yaml.Unmarshal(rootBytes, &root); err != nil {
return err
}
expectedRoot := libcommon.HexToHash(root.Root)
object := ref.Clone().(unmarshalerMarshalerHashable)
_, isBeaconState := object.(*state.BeaconState)

snappyEncoded, err := os.ReadFile(serializedFile)
if err != nil {
return err
}
encoded, err := utils.DecompressSnappy(snappyEncoded)
if err != nil {
return err
}

if err := object.DecodeSSZWithVersion(encoded, int(context.version)); err != nil && !isBeaconState {
return err
}
haveRoot, err := object.HashSSZ()
if err != nil {
return err
}
if libcommon.Hash(haveRoot) != expectedRoot {
return fmt.Errorf("roots mismatch")
}
// Cannot test it without a config.
if isBeaconState {
return nil
}
haveEncoded, err := object.EncodeSSZ(nil)
if err != nil {
return err
}
if !bytes.Equal(haveEncoded, encoded) {
return fmt.Errorf("re-encoding mismatch")
}
return nil
}
}
4 changes: 2 additions & 2 deletions cmd/erigon-cl/core/state/ssz.go
Original file line number Diff line number Diff line change
Expand Up @@ -446,6 +446,6 @@ func (b *BeaconState) EncodingSizeSSZ() (size int) {
return
}

func (*BeaconState) Clone() clonable.Clonable {
return &BeaconState{}
func (b *BeaconState) Clone() clonable.Clonable {
return &BeaconState{beaconConfig: b.beaconConfig}
}
2 changes: 2 additions & 0 deletions cmd/erigon-cl/core/state/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -222,9 +222,11 @@ func (b *BeaconState) _initializeValidatorsPhase0() error {
}

func (b *BeaconState) initBeaconState() error {

if b.touchedLeaves == nil {
b.touchedLeaves = make(map[StateLeafIndex]bool)
}

b.publicKeyIndicies = make(map[[48]byte]uint64)
b._refreshActiveBalances()
for i, validator := range b.validators {
Expand Down
2 changes: 0 additions & 2 deletions cmd/sentinel/sentinel/sentinel.go
Original file line number Diff line number Diff line change
Expand Up @@ -206,8 +206,6 @@ func New(

s.handshaker = handshake.New(ctx, cfg.GenesisConfig, cfg.BeaconConfig, host, rule)

// removed IdDelta in recent version of libp2p
host.RemoveStreamHandler("/p2p/id/delta/1.0.0")
s.host = host
s.peers = peers.New(s.host)

Expand Down