github.com/prysmaticlabs/prysm@v1.4.4/beacon-chain/rpc/prysm/v1alpha1/debug/state_test.go (about) 1 package debug 2 3 import ( 4 "context" 5 "testing" 6 7 types "github.com/prysmaticlabs/eth2-types" 8 mock "github.com/prysmaticlabs/prysm/beacon-chain/blockchain/testing" 9 dbTest "github.com/prysmaticlabs/prysm/beacon-chain/db/testing" 10 "github.com/prysmaticlabs/prysm/beacon-chain/state/stategen" 11 pbrpc "github.com/prysmaticlabs/prysm/proto/beacon/rpc/v1" 12 "github.com/prysmaticlabs/prysm/proto/eth/v1alpha1/wrapper" 13 "github.com/prysmaticlabs/prysm/shared/testutil" 14 "github.com/prysmaticlabs/prysm/shared/testutil/assert" 15 "github.com/prysmaticlabs/prysm/shared/testutil/require" 16 ) 17 18 func TestServer_GetBeaconState(t *testing.T) { 19 db := dbTest.SetupDB(t) 20 ctx := context.Background() 21 st, err := testutil.NewBeaconState() 22 require.NoError(t, err) 23 slot := types.Slot(100) 24 require.NoError(t, st.SetSlot(slot)) 25 b := testutil.NewBeaconBlock() 26 b.Block.Slot = slot 27 require.NoError(t, db.SaveBlock(ctx, wrapper.WrappedPhase0SignedBeaconBlock(b))) 28 gRoot, err := b.Block.HashTreeRoot() 29 require.NoError(t, err) 30 gen := stategen.New(db) 31 require.NoError(t, gen.SaveState(ctx, gRoot, st)) 32 require.NoError(t, db.SaveState(ctx, st, gRoot)) 33 bs := &Server{ 34 StateGen: gen, 35 GenesisTimeFetcher: &mock.ChainService{}, 36 } 37 _, err = bs.GetBeaconState(ctx, &pbrpc.BeaconStateRequest{}) 38 assert.ErrorContains(t, "Need to specify either a block root or slot to request state", err) 39 req := &pbrpc.BeaconStateRequest{ 40 QueryFilter: &pbrpc.BeaconStateRequest_BlockRoot{ 41 BlockRoot: gRoot[:], 42 }, 43 } 44 res, err := bs.GetBeaconState(ctx, req) 45 require.NoError(t, err) 46 wanted, err := st.MarshalSSZ() 47 require.NoError(t, err) 48 assert.DeepEqual(t, wanted, res.Encoded) 49 req = &pbrpc.BeaconStateRequest{ 50 QueryFilter: &pbrpc.BeaconStateRequest_Slot{ 51 Slot: slot, 52 }, 53 } 54 res, err = bs.GetBeaconState(ctx, req) 55 require.NoError(t, err) 56 assert.DeepEqual(t, wanted, res.Encoded) 57 } 58 59 func TestServer_GetBeaconState_RequestFutureSlot(t *testing.T) { 60 ds := &Server{GenesisTimeFetcher: &mock.ChainService{}} 61 req := &pbrpc.BeaconStateRequest{ 62 QueryFilter: &pbrpc.BeaconStateRequest_Slot{ 63 Slot: ds.GenesisTimeFetcher.CurrentSlot() + 1, 64 }, 65 } 66 wanted := "Cannot retrieve information about a slot in the future" 67 _, err := ds.GetBeaconState(context.Background(), req) 68 assert.ErrorContains(t, wanted, err) 69 }