2020-05-15 21:54:53 +00:00
|
|
|
package debug
|
2020-05-01 01:47:10 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"testing"
|
|
|
|
|
2021-02-16 07:45:34 +00:00
|
|
|
types "github.com/prysmaticlabs/eth2-types"
|
2020-05-06 01:42:11 +00:00
|
|
|
mock "github.com/prysmaticlabs/prysm/beacon-chain/blockchain/testing"
|
2020-05-01 01:47:10 +00:00
|
|
|
dbTest "github.com/prysmaticlabs/prysm/beacon-chain/db/testing"
|
|
|
|
"github.com/prysmaticlabs/prysm/beacon-chain/state/stategen"
|
2021-07-28 21:23:44 +00:00
|
|
|
pbrpc "github.com/prysmaticlabs/prysm/proto/prysm/v1alpha1"
|
2021-07-21 21:34:07 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/proto/prysm/v1alpha1/wrapper"
|
2021-09-23 18:53:46 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/testing/assert"
|
|
|
|
"github.com/prysmaticlabs/prysm/testing/require"
|
|
|
|
"github.com/prysmaticlabs/prysm/testing/util"
|
2020-05-01 01:47:10 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestServer_GetBeaconState(t *testing.T) {
|
2020-12-16 16:56:21 +00:00
|
|
|
db := dbTest.SetupDB(t)
|
2020-05-01 01:47:10 +00:00
|
|
|
ctx := context.Background()
|
2021-09-23 18:53:46 +00:00
|
|
|
st, err := util.NewBeaconState()
|
2021-02-08 20:00:09 +00:00
|
|
|
require.NoError(t, err)
|
2021-02-16 07:45:34 +00:00
|
|
|
slot := types.Slot(100)
|
2020-07-20 02:15:51 +00:00
|
|
|
require.NoError(t, st.SetSlot(slot))
|
2021-09-23 18:53:46 +00:00
|
|
|
b := util.NewBeaconBlock()
|
2020-08-27 18:13:32 +00:00
|
|
|
b.Block.Slot = slot
|
2021-07-06 15:34:05 +00:00
|
|
|
require.NoError(t, db.SaveBlock(ctx, wrapper.WrappedPhase0SignedBeaconBlock(b)))
|
2020-08-27 18:13:32 +00:00
|
|
|
gRoot, err := b.Block.HashTreeRoot()
|
2020-07-20 02:15:51 +00:00
|
|
|
require.NoError(t, err)
|
2020-12-16 16:56:21 +00:00
|
|
|
gen := stategen.New(db)
|
2020-07-20 02:15:51 +00:00
|
|
|
require.NoError(t, gen.SaveState(ctx, gRoot, st))
|
|
|
|
require.NoError(t, db.SaveState(ctx, st, gRoot))
|
2020-05-01 01:47:10 +00:00
|
|
|
bs := &Server{
|
2020-05-06 01:42:11 +00:00
|
|
|
StateGen: gen,
|
|
|
|
GenesisTimeFetcher: &mock.ChainService{},
|
2020-05-01 01:47:10 +00:00
|
|
|
}
|
2020-07-20 02:15:51 +00:00
|
|
|
_, err = bs.GetBeaconState(ctx, &pbrpc.BeaconStateRequest{})
|
|
|
|
assert.ErrorContains(t, "Need to specify either a block root or slot to request state", err)
|
2020-05-01 01:47:10 +00:00
|
|
|
req := &pbrpc.BeaconStateRequest{
|
|
|
|
QueryFilter: &pbrpc.BeaconStateRequest_BlockRoot{
|
|
|
|
BlockRoot: gRoot[:],
|
|
|
|
},
|
|
|
|
}
|
|
|
|
res, err := bs.GetBeaconState(ctx, req)
|
2020-07-20 02:15:51 +00:00
|
|
|
require.NoError(t, err)
|
2021-03-16 00:43:27 +00:00
|
|
|
wanted, err := st.MarshalSSZ()
|
2020-07-20 02:15:51 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
assert.DeepEqual(t, wanted, res.Encoded)
|
2020-05-01 01:47:10 +00:00
|
|
|
req = &pbrpc.BeaconStateRequest{
|
|
|
|
QueryFilter: &pbrpc.BeaconStateRequest_Slot{
|
2021-09-02 03:05:08 +00:00
|
|
|
Slot: slot + 1,
|
2020-05-01 01:47:10 +00:00
|
|
|
},
|
|
|
|
}
|
2021-09-02 03:05:08 +00:00
|
|
|
require.NoError(t, st.SetSlot(slot+1))
|
|
|
|
wanted, err = st.MarshalSSZ()
|
|
|
|
require.NoError(t, err)
|
2020-05-01 01:47:10 +00:00
|
|
|
res, err = bs.GetBeaconState(ctx, req)
|
2020-07-20 02:15:51 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
assert.DeepEqual(t, wanted, res.Encoded)
|
2020-05-01 01:47:10 +00:00
|
|
|
}
|
2020-05-06 01:42:11 +00:00
|
|
|
|
|
|
|
func TestServer_GetBeaconState_RequestFutureSlot(t *testing.T) {
|
2020-05-15 21:54:53 +00:00
|
|
|
ds := &Server{GenesisTimeFetcher: &mock.ChainService{}}
|
2020-05-06 01:42:11 +00:00
|
|
|
req := &pbrpc.BeaconStateRequest{
|
|
|
|
QueryFilter: &pbrpc.BeaconStateRequest_Slot{
|
2020-05-15 21:54:53 +00:00
|
|
|
Slot: ds.GenesisTimeFetcher.CurrentSlot() + 1,
|
2020-05-06 01:42:11 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
wanted := "Cannot retrieve information about a slot in the future"
|
2020-07-20 02:15:51 +00:00
|
|
|
_, err := ds.GetBeaconState(context.Background(), req)
|
|
|
|
assert.ErrorContains(t, wanted, err)
|
2020-05-06 01:42:11 +00:00
|
|
|
}
|