2021-03-18 21:00:00 +00:00
|
|
|
package kv
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"os"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/bazelbuild/rules_go/go/tools/bazel"
|
2022-08-16 12:20:13 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/v3/beacon-chain/db/iface"
|
|
|
|
"github.com/prysmaticlabs/prysm/v3/config/params"
|
|
|
|
"github.com/prysmaticlabs/prysm/v3/testing/assert"
|
|
|
|
"github.com/prysmaticlabs/prysm/v3/testing/require"
|
|
|
|
"github.com/prysmaticlabs/prysm/v3/testing/util"
|
2021-03-18 21:00:00 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestStore_SaveGenesisData(t *testing.T) {
|
|
|
|
ctx := context.Background()
|
|
|
|
db := setupDB(t)
|
|
|
|
|
2021-09-23 18:53:46 +00:00
|
|
|
gs, err := util.NewBeaconState()
|
2021-03-18 21:00:00 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
assert.NoError(t, db.SaveGenesisData(ctx, gs))
|
|
|
|
|
|
|
|
testGenesisDataSaved(t, db)
|
|
|
|
}
|
|
|
|
|
|
|
|
func testGenesisDataSaved(t *testing.T, db iface.Database) {
|
|
|
|
ctx := context.Background()
|
|
|
|
|
|
|
|
gb, err := db.GenesisBlock(ctx)
|
2022-05-20 07:16:53 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.NotNil(t, gb)
|
2021-03-18 21:00:00 +00:00
|
|
|
|
2021-05-26 16:19:54 +00:00
|
|
|
gbHTR, err := gb.Block().HashTreeRoot()
|
2022-05-20 07:16:53 +00:00
|
|
|
require.NoError(t, err)
|
2021-03-18 21:00:00 +00:00
|
|
|
|
|
|
|
gss, err := db.StateSummary(ctx, gbHTR)
|
2022-05-20 07:16:53 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.NotNil(t, gss)
|
2021-03-18 21:00:00 +00:00
|
|
|
|
|
|
|
head, err := db.HeadBlock(ctx)
|
2022-05-20 07:16:53 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.NotNil(t, head)
|
2021-03-18 21:00:00 +00:00
|
|
|
|
2021-05-26 16:19:54 +00:00
|
|
|
headHTR, err := head.Block().HashTreeRoot()
|
2022-05-20 07:16:53 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, gbHTR, headHTR, "head block does not match genesis block")
|
2021-03-18 21:00:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestLoadGenesisFromFile(t *testing.T) {
|
2022-05-20 07:16:53 +00:00
|
|
|
// for this test to work, we need the active config to have these properties:
|
|
|
|
// - fork version schedule that matches mainnnet.genesis.ssz
|
|
|
|
// - name that does not match params.MainnetName - otherwise we'll trigger the codepath that loads the state
|
|
|
|
// from the compiled binary.
|
|
|
|
// to do that, first we need to rewrite the mainnet fork schedule so it won't conflict with a renamed config that
|
|
|
|
// uses the mainnet fork schedule. construct the differently named mainnet config and set it active.
|
|
|
|
// finally, revert all this at the end of the test.
|
|
|
|
|
|
|
|
// first get the real mainnet out of the way by overwriting it schedule.
|
|
|
|
cfg, err := params.ByName(params.MainnetName)
|
|
|
|
require.NoError(t, err)
|
|
|
|
cfg = cfg.Copy()
|
|
|
|
reversioned := cfg.Copy()
|
|
|
|
params.FillTestVersions(reversioned, 127)
|
|
|
|
undo, err := params.SetActiveWithUndo(reversioned)
|
|
|
|
require.NoError(t, err)
|
|
|
|
defer func() {
|
|
|
|
require.NoError(t, undo())
|
|
|
|
}()
|
|
|
|
|
|
|
|
// then set up a new config, which uses the real mainnet schedule, and activate it
|
|
|
|
cfg.ConfigName = "genesis-test"
|
|
|
|
undo2, err := params.SetActiveWithUndo(cfg)
|
|
|
|
require.NoError(t, err)
|
|
|
|
defer func() {
|
|
|
|
require.NoError(t, undo2())
|
|
|
|
}()
|
|
|
|
|
2021-03-18 21:00:00 +00:00
|
|
|
fp := "testdata/mainnet.genesis.ssz"
|
|
|
|
rfp, err := bazel.Runfile(fp)
|
|
|
|
if err == nil {
|
|
|
|
fp = rfp
|
|
|
|
}
|
2022-03-30 22:23:34 +00:00
|
|
|
sb, err := os.ReadFile(fp)
|
2022-05-20 07:16:53 +00:00
|
|
|
require.NoError(t, err)
|
2021-03-18 21:00:00 +00:00
|
|
|
|
|
|
|
db := setupDB(t)
|
2022-05-20 07:16:53 +00:00
|
|
|
require.NoError(t, db.LoadGenesis(context.Background(), sb))
|
2021-03-18 21:00:00 +00:00
|
|
|
testGenesisDataSaved(t, db)
|
|
|
|
|
|
|
|
// Loading the same genesis again should not throw an error
|
2022-05-20 07:16:53 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.NoError(t, db.LoadGenesis(context.Background(), sb))
|
|
|
|
testGenesisDataSaved(t, db)
|
2021-03-18 21:00:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestLoadGenesisFromFile_mismatchedForkVersion(t *testing.T) {
|
|
|
|
fp := "testdata/altona.genesis.ssz"
|
|
|
|
rfp, err := bazel.Runfile(fp)
|
|
|
|
if err == nil {
|
|
|
|
fp = rfp
|
|
|
|
}
|
2022-03-30 22:23:34 +00:00
|
|
|
sb, err := os.ReadFile(fp)
|
2021-03-18 21:00:00 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
// Loading a genesis with the wrong fork version as beacon config should throw an error.
|
|
|
|
db := setupDB(t)
|
2022-03-30 22:23:34 +00:00
|
|
|
assert.ErrorContains(t, "does not match config genesis fork version", db.LoadGenesis(context.Background(), sb))
|
2021-03-18 21:00:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestEnsureEmbeddedGenesis(t *testing.T) {
|
|
|
|
params.SetupTestConfigCleanup(t)
|
2022-05-20 07:16:53 +00:00
|
|
|
// Embedded Genesis works with Mainnet config
|
|
|
|
cfg := params.MainnetConfig().Copy()
|
|
|
|
cfg.SecondsPerSlot = 1
|
|
|
|
undo, err := params.SetActiveWithUndo(cfg)
|
|
|
|
require.NoError(t, err)
|
|
|
|
defer func() {
|
|
|
|
require.NoError(t, undo())
|
|
|
|
}()
|
2021-03-18 21:00:00 +00:00
|
|
|
|
|
|
|
ctx := context.Background()
|
|
|
|
db := setupDB(t)
|
|
|
|
|
|
|
|
gb, err := db.GenesisBlock(ctx)
|
|
|
|
assert.NoError(t, err)
|
2021-05-26 16:19:54 +00:00
|
|
|
if gb != nil && !gb.IsNil() {
|
2021-03-18 21:00:00 +00:00
|
|
|
t.Fatal("Genesis block exists already")
|
|
|
|
}
|
|
|
|
|
|
|
|
gs, err := db.GenesisState(ctx)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.NotNil(t, gs, "an embedded genesis state does not exist")
|
|
|
|
|
|
|
|
assert.NoError(t, db.EnsureEmbeddedGenesis(ctx))
|
|
|
|
|
|
|
|
gb, err = db.GenesisBlock(ctx)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.NotNil(t, gb)
|
|
|
|
|
|
|
|
testGenesisDataSaved(t, db)
|
|
|
|
}
|