mirror of
https://gitlab.com/pulsechaincom/prysm-pulse.git
synced 2024-12-25 12:57:18 +00:00
8281634131
* Return status.Errorf instead of plain errors from gRPC functions * return plain errors from helper functions * change errors to lowercase in node * correct test expectations * extracted StateFetcher * StateFetcher tests * extract beacon state creation option and fix state tests * add comment to StateFetcher * remove empty line * overwrite config name * fix field names * remove FillRootsNaturalOpt helper * reduce statefetcher package visibility * gzl * add missing error assertion Co-authored-by: prylabs-bulldozer[bot] <58059840+prylabs-bulldozer[bot]@users.noreply.github.com> Co-authored-by: terence tsao <terence@prysmaticlabs.com>
202 lines
5.7 KiB
Go
202 lines
5.7 KiB
Go
package statefetcher
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"strconv"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/ethereum/go-ethereum/common/hexutil"
|
|
types "github.com/prysmaticlabs/eth2-types"
|
|
eth "github.com/prysmaticlabs/ethereumapis/eth/v1alpha1"
|
|
chainMock "github.com/prysmaticlabs/prysm/beacon-chain/blockchain/testing"
|
|
testDB "github.com/prysmaticlabs/prysm/beacon-chain/db/testing"
|
|
"github.com/prysmaticlabs/prysm/beacon-chain/state/stategen"
|
|
pb "github.com/prysmaticlabs/prysm/proto/beacon/p2p/v1"
|
|
"github.com/prysmaticlabs/prysm/shared/bytesutil"
|
|
"github.com/prysmaticlabs/prysm/shared/params"
|
|
"github.com/prysmaticlabs/prysm/shared/testutil"
|
|
"github.com/prysmaticlabs/prysm/shared/testutil/assert"
|
|
"github.com/prysmaticlabs/prysm/shared/testutil/require"
|
|
)
|
|
|
|
func TestGetStateRoot(t *testing.T) {
|
|
ctx := context.Background()
|
|
|
|
// We fill state and block roots with hex representations of natural numbers starting with 0.
|
|
// Example: 16 becomes 0x00...0f
|
|
fillRoots := func(state *pb.BeaconState) {
|
|
rootsLen := params.MainnetConfig().SlotsPerHistoricalRoot
|
|
roots := make([][]byte, rootsLen)
|
|
for i := types.Slot(0); i < rootsLen; i++ {
|
|
roots[i] = make([]byte, 32)
|
|
}
|
|
for j := 0; j < len(roots); j++ {
|
|
// Remove '0x' prefix and left-pad '0' to have 64 chars in total.
|
|
s := fmt.Sprintf("%064s", hexutil.EncodeUint64(uint64(j))[2:])
|
|
h, err := hexutil.Decode("0x" + s)
|
|
require.NoError(t, err, "Failed to decode root "+s)
|
|
roots[j] = h
|
|
}
|
|
state.StateRoots = roots
|
|
state.BlockRoots = roots
|
|
}
|
|
headSlot := types.Slot(123)
|
|
fillSlot := func(state *pb.BeaconState) {
|
|
state.Slot = headSlot
|
|
}
|
|
state, err := testutil.NewBeaconState(fillRoots, fillSlot)
|
|
require.NoError(t, err)
|
|
stateRoot, err := state.HashTreeRoot(ctx)
|
|
require.NoError(t, err)
|
|
|
|
t.Run("Head", func(t *testing.T) {
|
|
f := StateFetcher{
|
|
ChainInfoFetcher: &chainMock.ChainService{State: state},
|
|
}
|
|
|
|
s, err := f.State(ctx, []byte("head"))
|
|
require.NoError(t, err)
|
|
sRoot, err := s.HashTreeRoot(ctx)
|
|
require.NoError(t, err)
|
|
assert.DeepEqual(t, stateRoot, sRoot)
|
|
})
|
|
|
|
t.Run("Genesis", func(t *testing.T) {
|
|
params.SetupTestConfigCleanup(t)
|
|
cfg := params.BeaconConfig()
|
|
cfg.ConfigName = "test"
|
|
params.OverrideBeaconConfig(cfg)
|
|
|
|
db := testDB.SetupDB(t)
|
|
b := testutil.NewBeaconBlock()
|
|
b.Block.StateRoot = bytesutil.PadTo([]byte("foo"), 32)
|
|
require.NoError(t, db.SaveBlock(ctx, b))
|
|
r, err := b.Block.HashTreeRoot()
|
|
require.NoError(t, err)
|
|
|
|
state, err := testutil.NewBeaconState(func(state *pb.BeaconState) {
|
|
state.BlockRoots[0] = r[:]
|
|
})
|
|
require.NoError(t, err)
|
|
stateRoot, err := state.HashTreeRoot(ctx)
|
|
require.NoError(t, err)
|
|
|
|
require.NoError(t, db.SaveStateSummary(ctx, &pb.StateSummary{Root: r[:]}))
|
|
require.NoError(t, db.SaveGenesisBlockRoot(ctx, r))
|
|
require.NoError(t, db.SaveState(ctx, state, r))
|
|
|
|
f := StateFetcher{
|
|
BeaconDB: db,
|
|
}
|
|
|
|
s, err := f.State(ctx, []byte("genesis"))
|
|
require.NoError(t, err)
|
|
sRoot, err := s.HashTreeRoot(ctx)
|
|
require.NoError(t, err)
|
|
assert.DeepEqual(t, stateRoot, sRoot)
|
|
})
|
|
|
|
t.Run("Finalized", func(t *testing.T) {
|
|
stateGen := stategen.NewMockService()
|
|
stateGen.StatesByRoot[stateRoot] = state
|
|
|
|
f := StateFetcher{
|
|
ChainInfoFetcher: &chainMock.ChainService{
|
|
FinalizedCheckPoint: ð.Checkpoint{
|
|
Root: stateRoot[:],
|
|
},
|
|
},
|
|
StateGenService: stateGen,
|
|
}
|
|
|
|
s, err := f.State(ctx, []byte("finalized"))
|
|
require.NoError(t, err)
|
|
sRoot, err := s.HashTreeRoot(ctx)
|
|
require.NoError(t, err)
|
|
assert.Equal(t, stateRoot, sRoot)
|
|
})
|
|
|
|
t.Run("Justified", func(t *testing.T) {
|
|
stateGen := stategen.NewMockService()
|
|
stateGen.StatesByRoot[stateRoot] = state
|
|
|
|
f := StateFetcher{
|
|
ChainInfoFetcher: &chainMock.ChainService{
|
|
CurrentJustifiedCheckPoint: ð.Checkpoint{
|
|
Root: stateRoot[:],
|
|
},
|
|
},
|
|
StateGenService: stateGen,
|
|
}
|
|
|
|
s, err := f.State(ctx, []byte("justified"))
|
|
require.NoError(t, err)
|
|
sRoot, err := s.HashTreeRoot(ctx)
|
|
require.NoError(t, err)
|
|
assert.DeepEqual(t, stateRoot, sRoot)
|
|
})
|
|
|
|
t.Run("Hex root", func(t *testing.T) {
|
|
stateId, err := hexutil.Decode("0x" + strings.Repeat("0", 63) + "1")
|
|
require.NoError(t, err)
|
|
stateGen := stategen.NewMockService()
|
|
stateGen.StatesByRoot[bytesutil.ToBytes32(stateId)] = state
|
|
|
|
f := StateFetcher{
|
|
ChainInfoFetcher: &chainMock.ChainService{State: state},
|
|
StateGenService: stateGen,
|
|
}
|
|
|
|
s, err := f.State(ctx, stateId)
|
|
require.NoError(t, err)
|
|
sRoot, err := s.HashTreeRoot(ctx)
|
|
require.NoError(t, err)
|
|
assert.DeepEqual(t, stateRoot, sRoot)
|
|
})
|
|
|
|
t.Run("Hex root not found", func(t *testing.T) {
|
|
f := StateFetcher{
|
|
ChainInfoFetcher: &chainMock.ChainService{State: state},
|
|
}
|
|
stateId, err := hexutil.Decode("0x" + strings.Repeat("f", 64))
|
|
require.NoError(t, err)
|
|
_, err = f.State(ctx, stateId)
|
|
require.ErrorContains(t, "state not found in the last 8192 state roots in head state", err)
|
|
})
|
|
|
|
t.Run("Slot", func(t *testing.T) {
|
|
stateGen := stategen.NewMockService()
|
|
stateGen.StatesBySlot[headSlot] = state
|
|
|
|
f := StateFetcher{
|
|
GenesisTimeFetcher: &chainMock.ChainService{Slot: &headSlot},
|
|
StateGenService: stateGen,
|
|
}
|
|
|
|
s, err := f.State(ctx, []byte(strconv.FormatUint(uint64(headSlot), 10)))
|
|
require.NoError(t, err)
|
|
sRoot, err := s.HashTreeRoot(ctx)
|
|
require.NoError(t, err)
|
|
assert.Equal(t, stateRoot, sRoot)
|
|
})
|
|
|
|
t.Run("Slot too big", func(t *testing.T) {
|
|
f := StateFetcher{
|
|
GenesisTimeFetcher: &chainMock.ChainService{
|
|
Genesis: time.Now(),
|
|
},
|
|
}
|
|
_, err := f.State(ctx, []byte(strconv.FormatUint(1, 10)))
|
|
assert.ErrorContains(t, "slot cannot be in the future", err)
|
|
})
|
|
|
|
t.Run("Invalid state", func(t *testing.T) {
|
|
f := StateFetcher{}
|
|
_, err := f.State(ctx, []byte("foo"))
|
|
require.ErrorContains(t, "invalid state ID: foo", err)
|
|
})
|
|
}
|