mirror of
https://gitlab.com/pulsechaincom/erigon-pulse.git
synced 2025-01-05 10:32:19 +00:00
3ab373787e
we need to extract this interface from the struct. i need to also break down the interface more, to better show what parts the caching is used, move some functions from the cache state to the underlying. don't merge
64 lines
1.7 KiB
Go
64 lines
1.7 KiB
Go
package consensus_tests
|
|
|
|
import (
|
|
"io/fs"
|
|
"testing"
|
|
|
|
"github.com/ledgerwatch/erigon/cl/phase1/core/state"
|
|
|
|
libcommon "github.com/ledgerwatch/erigon-lib/common"
|
|
"github.com/ledgerwatch/erigon-lib/types/clonable"
|
|
"github.com/ledgerwatch/erigon-lib/types/ssz"
|
|
|
|
"github.com/ledgerwatch/erigon/cl/utils"
|
|
"github.com/ledgerwatch/erigon/spectest"
|
|
"github.com/stretchr/testify/require"
|
|
"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) spectest.Handler {
|
|
return spectest.HandlerFunc(func(t *testing.T, fsroot fs.FS, c spectest.TestCase) (err error) {
|
|
rootBytes, err := fs.ReadFile(fsroot, rootsFile)
|
|
require.NoError(t, err)
|
|
root := Root{}
|
|
err = yaml.Unmarshal(rootBytes, &root)
|
|
require.NoError(t, err)
|
|
expectedRoot := libcommon.HexToHash(root.Root)
|
|
object := ref.Clone().(unmarshalerMarshalerHashable)
|
|
_, isBeaconState := object.(*state.CachingBeaconState)
|
|
|
|
snappyEncoded, err := fs.ReadFile(fsroot, serializedFile)
|
|
require.NoError(t, err)
|
|
encoded, err := utils.DecompressSnappy(snappyEncoded)
|
|
require.NoError(t, err)
|
|
if err := object.DecodeSSZ(encoded, int(c.Version())); err != nil && !isBeaconState {
|
|
return err
|
|
}
|
|
haveRoot, err := object.HashSSZ()
|
|
require.NoError(t, err)
|
|
require.EqualValues(t, expectedRoot, haveRoot)
|
|
// Cannot test it without a config.
|
|
// TODO: parse and use config
|
|
if isBeaconState {
|
|
return nil
|
|
}
|
|
haveEncoded, err := object.EncodeSSZ(nil)
|
|
require.NoError(t, err)
|
|
require.EqualValues(t, haveEncoded, encoded)
|
|
return nil
|
|
})
|
|
}
|