2021-03-18 21:00:00 +00:00
|
|
|
package kv
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"os"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/bazelbuild/rules_go/go/tools/bazel"
|
|
|
|
"github.com/prysmaticlabs/prysm/beacon-chain/db/iface"
|
2021-09-21 19:59:25 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/config/params"
|
2021-09-23 18:53:46 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/testing/assert"
|
|
|
|
"github.com/prysmaticlabs/prysm/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)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.NotNil(t, gb)
|
|
|
|
|
2021-05-26 16:19:54 +00:00
|
|
|
gbHTR, err := gb.Block().HashTreeRoot()
|
2021-03-18 21:00:00 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
gss, err := db.StateSummary(ctx, gbHTR)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.NotNil(t, gss)
|
|
|
|
|
|
|
|
head, err := db.HeadBlock(ctx)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.NotNil(t, head)
|
|
|
|
|
2021-05-26 16:19:54 +00:00
|
|
|
headHTR, err := head.Block().HashTreeRoot()
|
2021-03-18 21:00:00 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, gbHTR, headHTR, "head block does not match genesis block")
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestLoadGenesisFromFile(t *testing.T) {
|
|
|
|
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)
|
2021-03-18 21:00:00 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
db := setupDB(t)
|
2022-03-30 22:23:34 +00:00
|
|
|
assert.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
|
|
|
|
assert.NoError(t, err)
|
2022-03-30 22:23:34 +00:00
|
|
|
assert.NoError(t, db.LoadGenesis(context.Background(), sb))
|
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) {
|
|
|
|
// Embedded Genesis works with Mainnet config
|
|
|
|
params.SetupTestConfigCleanup(t)
|
|
|
|
cfg := params.BeaconConfig()
|
2022-05-06 21:42:27 +00:00
|
|
|
cfg.ConfigName = params.MainnetName
|
2021-03-18 21:00:00 +00:00
|
|
|
params.OverrideBeaconConfig(cfg)
|
|
|
|
|
|
|
|
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)
|
|
|
|
}
|