package beacon import ( "context" "testing" "github.com/gogo/protobuf/proto" ethpb "github.com/prysmaticlabs/ethereumapis/eth/v1alpha1" "github.com/prysmaticlabs/go-ssz" "github.com/prysmaticlabs/prysm/beacon-chain/cache" dbTest "github.com/prysmaticlabs/prysm/beacon-chain/db/testing" "github.com/prysmaticlabs/prysm/beacon-chain/state/stategen" 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) defer dbTest.TeardownDB(t, db) 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) } gRoot, err := ssz.HashTreeRoot(b.Block) 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) } }