prysm-pulse/beacon-chain/db/kv/genesis_test.go
Nishant Das caf9bdbc6f
Use Block Interface Across Prysm (#8918)
* commit initial work

* checkpoint current work

* gaz

* checkpoint

* req/resp changes

* initial-sync

* finally works

* fix error

* fix bugs

* fix issue

* fix issues

* fix refs

* tests

* more text fixes

* more text fixes

* more text fixes

* fix tests

* fix tests

* tests

* finally fix builds

* finally

* comments

* fix fuzz

* share common library

* fix

* fix

* add in more defensive nil checks

* add in more defensive nil checks

* imports

* Apply suggestions from code review

Co-authored-by: terence tsao <terence@prysmaticlabs.com>

* Apply suggestions from code review

Co-authored-by: terence tsao <terence@prysmaticlabs.com>

* Update shared/interfaces/block_interface.go

Co-authored-by: terence tsao <terence@prysmaticlabs.com>

* Update shared/interfaces/block_wrapper.go

Co-authored-by: terence tsao <terence@prysmaticlabs.com>

* Update shared/interfaces/block_interface.go

Co-authored-by: terence tsao <terence@prysmaticlabs.com>

* imports

* fix bad changes

* fix

* terence's review

* terence's review

* fmt

* Update beacon-chain/rpc/beacon/blocks.go

Co-authored-by: Radosław Kapka <rkapka@wp.pl>

* fix tests

* fix

* fix all tests

Co-authored-by: terence tsao <terence@prysmaticlabs.com>
Co-authored-by: Radosław Kapka <rkapka@wp.pl>
2021-05-26 16:19:54 +00:00

118 lines
2.8 KiB
Go

package kv
import (
"context"
"os"
"testing"
"github.com/bazelbuild/rules_go/go/tools/bazel"
"github.com/prysmaticlabs/prysm/beacon-chain/db/iface"
"github.com/prysmaticlabs/prysm/shared/params"
"github.com/prysmaticlabs/prysm/shared/testutil"
"github.com/prysmaticlabs/prysm/shared/testutil/assert"
)
func TestStore_SaveGenesisData(t *testing.T) {
ctx := context.Background()
db := setupDB(t)
gs, err := testutil.NewBeaconState()
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)
gbHTR, err := gb.Block().HashTreeRoot()
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)
headHTR, err := head.Block().HashTreeRoot()
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
}
r, err := os.Open(fp)
assert.NoError(t, err)
defer func() {
assert.NoError(t, r.Close())
}()
db := setupDB(t)
assert.NoError(t, db.LoadGenesis(context.Background(), r))
testGenesisDataSaved(t, db)
// Loading the same genesis again should not throw an error
_, err = r.Seek(0, 0)
assert.NoError(t, err)
assert.NoError(t, db.LoadGenesis(context.Background(), r))
}
func TestLoadGenesisFromFile_mismatchedForkVersion(t *testing.T) {
fp := "testdata/altona.genesis.ssz"
rfp, err := bazel.Runfile(fp)
if err == nil {
fp = rfp
}
r, err := os.Open(fp)
assert.NoError(t, err)
defer func() {
assert.NoError(t, r.Close())
}()
// Loading a genesis with the wrong fork version as beacon config should throw an error.
db := setupDB(t)
assert.ErrorContains(t, "does not match config genesis fork version", db.LoadGenesis(context.Background(), r))
}
func TestEnsureEmbeddedGenesis(t *testing.T) {
// Embedded Genesis works with Mainnet config
params.SetupTestConfigCleanup(t)
cfg := params.BeaconConfig()
cfg.ConfigName = params.ConfigNames[params.Mainnet]
params.OverrideBeaconConfig(cfg)
ctx := context.Background()
db := setupDB(t)
gb, err := db.GenesisBlock(ctx)
assert.NoError(t, err)
if gb != nil && !gb.IsNil() {
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)
}