prysm-pulse/beacon-chain/db/kv/state_summary_test.go
Victor Farazdagi a069738c20
ETH2 Types: Slot (#8408)
* update shared/params

* update eth2-types deps

* update protobufs

* update shared/*

* fix testutil/state

* update beacon-chain/state

* update beacon-chain/db

* update tests

* fix test

* update beacon-chain/core

* update beacon-chain/blockchain

* update beacon-chain/cache

* beacon-chain/forkchoice

* update beacon-chain/operations

* update beacon-chain/p2p

* update beacon-chain/rpc

* update sync/initial-sync

* update deps

* update deps

* go fmt

* update beacon-chain/sync

* update endtoend/

* bazel build //beacon-chain - works w/o issues

* update slasher code

* udpate tools/

* update validator/

* update fastssz

* fix build

* fix test building

* update tests

* update ethereumapis deps

* fix tests

* update state/stategen

* fix build

* fix test

* add FarFutureSlot

* go imports

* Radek's suggestions

* Ivan's suggestions

* type conversions

* Nishant's suggestions

* add more tests to rpc_send_request

* fix test

* clean up

* fix conflicts

Co-authored-by: prylabs-bulldozer[bot] <58059840+prylabs-bulldozer[bot]@users.noreply.github.com>
Co-authored-by: nisdas <nishdas93@gmail.com>
2021-02-16 07:45:34 +00:00

65 lines
2.4 KiB
Go

package kv
import (
"context"
"testing"
types "github.com/prysmaticlabs/eth2-types"
pb "github.com/prysmaticlabs/prysm/proto/beacon/p2p/v1"
"github.com/prysmaticlabs/prysm/shared/bytesutil"
"github.com/prysmaticlabs/prysm/shared/testutil/assert"
"github.com/prysmaticlabs/prysm/shared/testutil/require"
)
func TestStateSummary_CanSaveRretrieve(t *testing.T) {
db := setupDB(t)
ctx := context.Background()
r1 := bytesutil.ToBytes32([]byte{'A'})
r2 := bytesutil.ToBytes32([]byte{'B'})
s1 := &pb.StateSummary{Slot: 1, Root: r1[:]}
// State summary should not exist yet.
require.Equal(t, false, db.HasStateSummary(ctx, r1), "State summary should not be saved")
require.NoError(t, db.SaveStateSummary(ctx, s1))
require.Equal(t, true, db.HasStateSummary(ctx, r1), "State summary should be saved")
saved, err := db.StateSummary(ctx, r1)
require.NoError(t, err)
assert.DeepEqual(t, s1, saved, "State summary does not equal")
// Save a new state summary.
s2 := &pb.StateSummary{Slot: 2, Root: r2[:]}
// State summary should not exist yet.
require.Equal(t, false, db.HasStateSummary(ctx, r2), "State summary should not be saved")
require.NoError(t, db.SaveStateSummary(ctx, s2))
require.Equal(t, true, db.HasStateSummary(ctx, r2), "State summary should be saved")
saved, err = db.StateSummary(ctx, r2)
require.NoError(t, err)
assert.DeepEqual(t, s2, saved, "State summary does not equal")
}
func TestStateSummary_CacheToDB(t *testing.T) {
db := setupDB(t)
summaries := make([]*pb.StateSummary, stateSummaryCachePruneCount-1)
for i := range summaries {
summaries[i] = &pb.StateSummary{Slot: types.Slot(i), Root: bytesutil.PadTo(bytesutil.Uint64ToBytesLittleEndian(uint64(i)), 32)}
}
require.NoError(t, db.SaveStateSummaries(context.Background(), summaries))
require.Equal(t, db.stateSummaryCache.len(), stateSummaryCachePruneCount-1)
require.NoError(t, db.SaveStateSummary(context.Background(), &pb.StateSummary{Slot: 1000, Root: []byte{'a', 'b'}}))
require.Equal(t, db.stateSummaryCache.len(), stateSummaryCachePruneCount)
require.NoError(t, db.SaveStateSummary(context.Background(), &pb.StateSummary{Slot: 1001, Root: []byte{'c', 'd'}}))
require.Equal(t, db.stateSummaryCache.len(), 1)
for i := range summaries {
r := bytesutil.Uint64ToBytesLittleEndian(uint64(i))
require.Equal(t, true, db.HasStateSummary(context.Background(), bytesutil.ToBytes32(r)))
}
}