Skip to content

Commit

Permalink
Added finality consensus-specs-tests (erigontech#6892)
Browse files Browse the repository at this point in the history
  • Loading branch information
Giulio2002 authored and finiteops committed Apr 10, 2023
1 parent 0c16f4e commit 733ae63
Show file tree
Hide file tree
Showing 6 changed files with 107 additions and 19 deletions.
11 changes: 11 additions & 0 deletions cmd/ef-tests-cl/collection.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"fmt"
"path"
)

Expand All @@ -10,6 +11,7 @@ var (
epochProcessingDivision = "epoch_processing"
)

// Epoch processing cases
var (
caseEffectiveBalanceUpdates = "effective_balance_updates"
caseEth1DataReset = "eth1_data_reset"
Expand All @@ -24,6 +26,14 @@ var (
caseSlashingsReset = "slashings_reset"
)

// transitionCoreTest
var finality = "finality/finality"

func placeholderTest() error {
fmt.Println("hallo")
return nil
}

// Following is just a map for all tests to their execution.
var TestCollection map[string]testFunc = map[string]testFunc{
path.Join(epochProcessingDivision, caseEffectiveBalanceUpdates): effectiveBalancesUpdateTest,
Expand All @@ -37,4 +47,5 @@ var TestCollection map[string]testFunc = map[string]testFunc{
path.Join(epochProcessingDivision, caseRewardsAndPenalties): rewardsAndPenaltiesTest,
path.Join(epochProcessingDivision, caseSlashings): slashingsTest,
path.Join(epochProcessingDivision, caseSlashingsReset): slashingsResetTest,
finality: finalityTestFunction,
}
Binary file removed cmd/ef-tests-cl/ef-tests-cl
Binary file not shown.
25 changes: 7 additions & 18 deletions cmd/ef-tests-cl/epoch_processing.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ import (
"os"

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

Expand All @@ -19,30 +17,21 @@ func getTestEpochProcessing(f func(s *transition.StateTransistor) error) testFun
err = fmt.Errorf("panic: %s", recovered)
}
}()
sszSnappyTest, err := os.ReadFile("pre.ssz_snappy")
if err != nil {
return err
}
sszSnappyExpected, err := os.ReadFile("post.ssz_snappy")
isErrExpected := os.IsNotExist(err)
if isErrExpected {
err = nil
}
// Read post and
testState, err := decodeStateFromFile("pre.ssz_snappy")
if err != nil {
return err
}
// Read post and
testState := state.New(&clparams.MainnetBeaconConfig)
if err := utils.DecodeSSZSnappyWithVersion(testState, sszSnappyTest, int(testVersion)); err != nil {
var isErrExpected bool
expectedState, err := decodeStateFromFile("post.ssz_snappy")
if os.IsNotExist(err) {
isErrExpected = true
} else {
return err
}
var expectedState *state.BeaconState
if !isErrExpected {
expectedState = state.New(&clparams.MainnetBeaconConfig)
if err := utils.DecodeSSZSnappyWithVersion(expectedState, sszSnappyExpected, int(testVersion)); err != nil {
return err
}
}

// Make up state transistor
s := transition.New(testState, &clparams.MainnetBeaconConfig, nil, false)
Expand Down
42 changes: 42 additions & 0 deletions cmd/ef-tests-cl/finality.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package main

import (
"fmt"

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

func finalityTestFunction() error {
testState, err := decodeStateFromFile("pre.ssz_snappy")
if err != nil {
return err
}
expectedState, err := decodeStateFromFile("post.ssz_snappy")
if err != nil {
return err
}
blocks, err := testBlocks()
if err != nil {
return err
}
transistor := transition.New(testState, &clparams.MainnetBeaconConfig, nil, false)
startSlot := testState.Slot()
for _, block := range blocks {
if err := transistor.TransitionState(block); err != nil {
return fmt.Errorf("cannot transition state: %s. slot=%d. start_slot=%d", err, block.Block.Slot, startSlot)
}
}
expectedRoot, err := expectedState.HashSSZ()
if err != nil {
return err
}
haveRoot, err := testState.HashSSZ()
if err != nil {
return err
}
if haveRoot != expectedRoot {
return fmt.Errorf("mismatching state roots.")
}
return nil
}
46 changes: 46 additions & 0 deletions cmd/ef-tests-cl/utils.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package main

import (
"fmt"
"os"

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

func decodeStateFromFile(filepath string) (*state.BeaconState, error) {
sszSnappy, err := os.ReadFile(filepath)
if err != nil {
return nil, err
}
testState := state.New(&clparams.MainnetBeaconConfig)
if err := utils.DecodeSSZSnappyWithVersion(testState, sszSnappy, int(testVersion)); err != nil {
return nil, err
}
return testState, nil
}

func testBlocks() ([]*cltypes.SignedBeaconBlock, error) {
i := 0
blocks := []*cltypes.SignedBeaconBlock{}
var err error
for {
var blockBytes []byte
blockBytes, err = os.ReadFile(fmt.Sprintf("blocks_%d.ssz_snappy", i))
if err != nil {
break
}
blk := &cltypes.SignedBeaconBlock{}
if err = utils.DecodeSSZSnappyWithVersion(blk, blockBytes, int(testVersion)); err != nil {
return nil, err
}
blocks = append(blocks, blk)
i++
}
if os.IsNotExist(err) {
err = nil
}
return blocks, err
}
2 changes: 1 addition & 1 deletion cmd/erigon-cl/core/state/accessors.go
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@ func (b *BeaconState) GetSeed(epoch uint64, domain [4]byte) libcommon.Hash {

// BaseRewardPerIncrement return base rewards for processing sync committee and duties.
func (b *BeaconState) BaseRewardPerIncrement() uint64 {
if b.activeValidatorsCache == nil {
if b.totalActiveBalanceCache == nil {
b._refreshActiveBalances()
}
return b.beaconConfig.EffectiveBalanceIncrement * b.beaconConfig.BaseRewardFactor / b.totalActiveBalanceRootCache
Expand Down

0 comments on commit 733ae63

Please sign in to comment.