2020-05-01 01:47:10 +00:00
|
|
|
package beacon
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/gogo/protobuf/proto"
|
|
|
|
ethpb "github.com/prysmaticlabs/ethereumapis/eth/v1alpha1"
|
|
|
|
"github.com/prysmaticlabs/prysm/beacon-chain/cache"
|
|
|
|
dbTest "github.com/prysmaticlabs/prysm/beacon-chain/db/testing"
|
|
|
|
"github.com/prysmaticlabs/prysm/beacon-chain/state/stategen"
|
2020-05-05 04:30:24 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/beacon-chain/state/stateutil"
|
2020-05-01 01:47:10 +00:00
|
|
|
pbrpc "github.com/prysmaticlabs/prysm/proto/beacon/rpc/v1"
|
|
|
|
"github.com/prysmaticlabs/prysm/shared/featureconfig"
|
|
|
|
"github.com/prysmaticlabs/prysm/shared/testutil"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestServer_GetBeaconState(t *testing.T) {
|
|
|
|
resetCfg := featureconfig.InitWithReset(&featureconfig.Flags{NewStateMgmt: true})
|
|
|
|
defer resetCfg()
|
|
|
|
|
|
|
|
db := dbTest.SetupDB(t)
|
|
|
|
ctx := context.Background()
|
|
|
|
st := testutil.NewBeaconState()
|
|
|
|
slot := uint64(100)
|
|
|
|
if err := st.SetSlot(slot); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
b := ðpb.SignedBeaconBlock{Block: ðpb.BeaconBlock{
|
|
|
|
Slot: slot,
|
|
|
|
}}
|
|
|
|
if err := db.SaveBlock(ctx, b); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2020-05-05 04:30:24 +00:00
|
|
|
gRoot, err := stateutil.BlockRoot(b.Block)
|
2020-05-01 01:47:10 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
gen := stategen.New(db, cache.NewStateSummaryCache())
|
|
|
|
if err := gen.SaveState(ctx, gRoot, st); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if err := db.SaveState(ctx, st, gRoot); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
bs := &Server{
|
|
|
|
BeaconDB: db,
|
|
|
|
StateGen: gen,
|
|
|
|
}
|
|
|
|
if _, err := bs.GetBeaconState(ctx, &pbrpc.BeaconStateRequest{}); err == nil {
|
|
|
|
t.Errorf("Expected error without a query filter, received nil")
|
|
|
|
}
|
|
|
|
req := &pbrpc.BeaconStateRequest{
|
|
|
|
QueryFilter: &pbrpc.BeaconStateRequest_BlockRoot{
|
|
|
|
BlockRoot: gRoot[:],
|
|
|
|
},
|
|
|
|
}
|
|
|
|
res, err := bs.GetBeaconState(ctx, req)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
wanted := st.CloneInnerState()
|
|
|
|
if !proto.Equal(wanted, res) {
|
|
|
|
t.Errorf("Wanted %v, received %v", wanted, res)
|
|
|
|
}
|
|
|
|
req = &pbrpc.BeaconStateRequest{
|
|
|
|
QueryFilter: &pbrpc.BeaconStateRequest_Slot{
|
|
|
|
Slot: slot,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
res, err = bs.GetBeaconState(ctx, req)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if !proto.Equal(wanted, res) {
|
|
|
|
t.Errorf("Wanted %v, received %v", wanted, res)
|
|
|
|
}
|
|
|
|
}
|