-
Notifications
You must be signed in to change notification settings - Fork 1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement GetBeaconState Endpoint (#5668)
* implement get beacon state * gaz * Merge branch 'master' into implement-debug-state * passing tests * enable with featureconfig * struct oder * Update beacon-chain/rpc/beacon/state.go * Merge refs/heads/master into implement-debug-state * lint resolve * Merge branch 'implement-debug-state' of github.com:prysmaticlabs/prysm into implement-debug-state * tested at runtime * fix build * Merge branch 'master' into implement-debug-state * Merge refs/heads/master into implement-debug-state * Merge refs/heads/master into implement-debug-state * Merge refs/heads/master into implement-debug-state * Merge refs/heads/master into implement-debug-state * Merge refs/heads/master into implement-debug-state * build and fmt * conf * Merge refs/heads/master into implement-debug-state * Merge refs/heads/master into implement-debug-state * Merge refs/heads/master into implement-debug-state * Merge refs/heads/master into implement-debug-state * Merge refs/heads/master into implement-debug-state
- Loading branch information
1 parent
e1ac1d3
commit 2e33595
Showing
13 changed files
with
328 additions
and
161 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package beacon | ||
|
||
import ( | ||
"context" | ||
|
||
pbp2p "github.com/prysmaticlabs/prysm/proto/beacon/p2p/v1" | ||
pbrpc "github.com/prysmaticlabs/prysm/proto/beacon/rpc/v1" | ||
"github.com/prysmaticlabs/prysm/shared/bytesutil" | ||
"github.com/prysmaticlabs/prysm/shared/featureconfig" | ||
"google.golang.org/grpc/codes" | ||
"google.golang.org/grpc/status" | ||
) | ||
|
||
// GetBeaconState retrieves a beacon state | ||
// from the beacon node by either a slot or block root. | ||
func (bs *Server) GetBeaconState( | ||
ctx context.Context, | ||
req *pbrpc.BeaconStateRequest, | ||
) (*pbp2p.BeaconState, error) { | ||
if !featureconfig.Get().NewStateMgmt { | ||
return nil, status.Error(codes.FailedPrecondition, "requires --enable-new-state-mgmt to function") | ||
} | ||
switch q := req.QueryFilter.(type) { | ||
case *pbrpc.BeaconStateRequest_Slot: | ||
st, err := bs.StateGen.StateBySlot(ctx, q.Slot) | ||
if err != nil { | ||
return nil, status.Errorf(codes.Internal, "could not compute state by slot: %v", err) | ||
} | ||
return st.CloneInnerState(), nil | ||
case *pbrpc.BeaconStateRequest_BlockRoot: | ||
st, err := bs.StateGen.StateByRoot(ctx, bytesutil.ToBytes32(q.BlockRoot)) | ||
if err != nil { | ||
return nil, status.Errorf(codes.Internal, "could not compute state by block root: %v", err) | ||
} | ||
return st.CloneInnerState(), nil | ||
default: | ||
return nil, status.Error(codes.InvalidArgument, "need to specify either a block root or slot to request state") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
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) | ||
} | ||
} |
Oops, something went wrong.