Beacon API: update GetStateRoot (#8437)

* Address various feedbacks

* Gaz

* More nil check

* Update beacon-chain/rpc/beaconv1/state_test.go

Co-authored-by: Radosław Kapka <rkapka@wp.pl>

* Update beacon-chain/rpc/beaconv1/state_test.go

Co-authored-by: Radosław Kapka <rkapka@wp.pl>

* Update beacon-chain/rpc/beaconv1/state_test.go

Co-authored-by: Radosław Kapka <rkapka@wp.pl>

* Update beacon-chain/rpc/beaconv1/state_test.go

Co-authored-by: Radosław Kapka <rkapka@wp.pl>

* Update beacon-chain/rpc/beaconv1/state_test.go

Co-authored-by: Radosław Kapka <rkapka@wp.pl>

Co-authored-by: Radosław Kapka <rkapka@wp.pl>
This commit is contained in:
terence tsao 2021-02-12 07:38:36 -08:00 committed by GitHub
parent 7f6b15271a
commit 66471c2f13
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 122 additions and 106 deletions

View File

@ -61,7 +61,6 @@ go_test(
"//beacon-chain/db/testing:go_default_library",
"//beacon-chain/p2p/testing:go_default_library",
"//beacon-chain/powchain/testing:go_default_library",
"//beacon-chain/state/stategen:go_default_library",
"//proto/beacon/p2p/v1:go_default_library",
"//proto/migration:go_default_library",
"//shared/bytesutil:go_default_library",

View File

@ -102,47 +102,55 @@ func (bs *Server) GetFinalityCheckpoints(ctx context.Context, req *ethpb.StateRe
}
func (bs *Server) headStateRoot(ctx context.Context) ([]byte, error) {
stateRoot, err := bs.ChainInfoFetcher.HeadRoot(ctx)
b, err := bs.ChainInfoFetcher.HeadBlock(ctx)
if err != nil {
return nil, status.Errorf(codes.Internal, "Failed to obtain root: %v", err)
return nil, status.Errorf(codes.Internal, "Could not get head block: %v", err)
}
return stateRoot, nil
if b == nil || b.Block == nil {
return nil, status.Error(codes.Internal, "Nil block")
}
return b.Block.StateRoot, nil
}
func (bs *Server) genesisStateRoot(ctx context.Context) ([]byte, error) {
stateRoot, err := bs.ChainStartFetcher.PreGenesisState().HashTreeRoot(ctx)
b, err := bs.BeaconDB.GenesisBlock(ctx)
if err != nil {
return nil, status.Errorf(codes.Internal, "Failed to obtain root: %v", err)
return nil, status.Errorf(codes.Internal, "Could not get genesis block: %v", err)
}
return stateRoot[:], nil
if b == nil || b.Block == nil {
return nil, status.Error(codes.Internal, "Nil block")
}
return b.Block.StateRoot, nil
}
func (bs *Server) finalizedStateRoot(ctx context.Context) ([]byte, error) {
var blockRoot [32]byte
copy(blockRoot[:], bs.ChainInfoFetcher.FinalizedCheckpt().Root)
state, err := bs.StateGenService.StateByRoot(ctx, blockRoot)
cp, err := bs.BeaconDB.FinalizedCheckpoint(ctx)
if err != nil {
return nil, status.Errorf(codes.Internal, "Failed to obtain state: %v", err)
return nil, status.Errorf(codes.Internal, "Could not get finalized checkpoint: %v", err)
}
stateRoot, err := state.HashTreeRoot(ctx)
b, err := bs.BeaconDB.Block(ctx, bytesutil.ToBytes32(cp.Root))
if err != nil {
return nil, status.Errorf(codes.Internal, "Failed to obtain root: %v", err)
return nil, status.Errorf(codes.Internal, "Could not get finalized block: %v", err)
}
return stateRoot[:], nil
if b == nil || b.Block == nil {
return nil, status.Error(codes.Internal, "Nil block")
}
return b.Block.StateRoot, nil
}
func (bs *Server) justifiedStateRoot(ctx context.Context) ([]byte, error) {
var blockRoot [32]byte
copy(blockRoot[:], bs.ChainInfoFetcher.CurrentJustifiedCheckpt().Root)
state, err := bs.StateGenService.StateByRoot(ctx, blockRoot)
cp, err := bs.BeaconDB.JustifiedCheckpoint(ctx)
if err != nil {
return nil, status.Errorf(codes.Internal, "Failed to obtain state: %v", err)
return nil, status.Errorf(codes.Internal, "Could not get justified checkpoint: %v", err)
}
stateRoot, err := state.HashTreeRoot(ctx)
b, err := bs.BeaconDB.Block(ctx, bytesutil.ToBytes32(cp.Root))
if err != nil {
return nil, status.Errorf(codes.Internal, "Failed to obtain root: %v", err)
return nil, status.Errorf(codes.Internal, "Could not get justified block: %v", err)
}
return stateRoot[:], nil
if b == nil || b.Block == nil {
return nil, status.Error(codes.Internal, "Nil block")
}
return b.Block.StateRoot, nil
}
func (bs *Server) stateRootByHex(ctx context.Context, stateId []byte) ([]byte, error) {
@ -159,21 +167,26 @@ func (bs *Server) stateRootByHex(ctx context.Context, stateId []byte) ([]byte, e
}
return nil, status.Errorf(
codes.NotFound,
"State not found in the last %d states", len(headState.StateRoots()))
"State not found in the last %d state roots in head state", len(headState.StateRoots()))
}
func (bs *Server) stateRootBySlot(ctx context.Context, slot uint64) ([]byte, error) {
currentSlot := bs.ChainInfoFetcher.HeadSlot()
currentSlot := bs.GenesisTimeFetcher.CurrentSlot()
if slot > currentSlot {
return nil, status.Errorf(codes.Internal, "Slot cannot be in the future")
}
state, err := bs.StateGenService.StateBySlot(ctx, slot)
e, blks, err := bs.BeaconDB.BlocksBySlot(ctx, slot)
if err != nil {
return nil, status.Errorf(codes.Internal, "Failed to obtain state: %v", err)
return nil, status.Errorf(codes.Internal, "Could not get blocks: %v", err)
}
stateRoot, err := state.HashTreeRoot(ctx)
if err != nil {
return nil, status.Errorf(codes.Internal, "Failed to obtain root: %v", err)
if !e {
return nil, status.Errorf(codes.NotFound, "No block exists")
}
return stateRoot[:], nil
if len(blks) != 1 {
return nil, status.Errorf(codes.Internal, "Multiple blocks exist in same slot")
}
if blks[0] == nil || blks[0].Block == nil {
return nil, status.Error(codes.Internal, "Nil block")
}
return blks[0].Block.StateRoot, nil
}

View File

@ -13,9 +13,9 @@ import (
ethpb "github.com/prysmaticlabs/ethereumapis/eth/v1"
eth "github.com/prysmaticlabs/ethereumapis/eth/v1alpha1"
chainMock "github.com/prysmaticlabs/prysm/beacon-chain/blockchain/testing"
powMock "github.com/prysmaticlabs/prysm/beacon-chain/powchain/testing"
"github.com/prysmaticlabs/prysm/beacon-chain/state/stategen"
testDB "github.com/prysmaticlabs/prysm/beacon-chain/db/testing"
pb "github.com/prysmaticlabs/prysm/proto/beacon/p2p/v1"
"github.com/prysmaticlabs/prysm/shared/bytesutil"
"github.com/prysmaticlabs/prysm/shared/params"
"github.com/prysmaticlabs/prysm/shared/testutil"
"github.com/prysmaticlabs/prysm/shared/testutil/assert"
@ -76,95 +76,90 @@ func TestGetGenesis(t *testing.T) {
}
func TestGetStateRoot(t *testing.T) {
db := testDB.SetupDB(t)
ctx := context.Background()
root := []byte("123456")
t.Run("Head", func(t *testing.T) {
chainService := &chainMock.ChainService{
Root: root,
}
b := testutil.NewBeaconBlock()
b.Block.StateRoot = bytesutil.PadTo([]byte("head"), 32)
s := Server{
ChainInfoFetcher: chainService,
ChainInfoFetcher: &chainMock.ChainService{Block: b},
}
resp, err := s.GetStateRoot(ctx, &ethpb.StateRequest{
StateId: []byte("head"),
})
require.NoError(t, err)
assert.DeepEqual(t, root, resp.Data.StateRoot)
assert.DeepEqual(t, bytesutil.PadTo([]byte("head"), 32), resp.Data.StateRoot)
})
t.Run("Genesis", func(t *testing.T) {
state, err := testutil.NewBeaconState()
b := testutil.NewBeaconBlock()
b.Block.StateRoot = bytesutil.PadTo([]byte("genesis"), 32)
require.NoError(t, db.SaveBlock(ctx, b))
r, err := b.Block.HashTreeRoot()
require.NoError(t, err)
chainFetcher := &powMock.POWChain{
GenesisState: state,
}
require.NoError(t, db.SaveStateSummary(ctx, &pb.StateSummary{Root: r[:]}))
require.NoError(t, db.SaveGenesisBlockRoot(ctx, r))
s := Server{
ChainStartFetcher: chainFetcher,
BeaconDB: db,
}
resp, err := s.GetStateRoot(ctx, &ethpb.StateRequest{
StateId: []byte("genesis"),
})
require.NoError(t, err)
expectedRoot, err := state.HashTreeRoot(ctx)
require.NoError(t, err)
var b [32]byte
copy(b[:], resp.Data.StateRoot)
assert.DeepEqual(t, expectedRoot, b)
assert.DeepEqual(t, bytesutil.PadTo([]byte("genesis"), 32), resp.Data.StateRoot)
})
t.Run("Finalized", func(t *testing.T) {
state, err := testutil.NewBeaconState()
parent := testutil.NewBeaconBlock()
parentR, err := parent.Block.HashTreeRoot()
require.NoError(t, err)
var blockRoot [32]byte
copy(blockRoot[:], "block_root")
chainService := &chainMock.ChainService{
FinalizedCheckPoint: &eth.Checkpoint{
Root: blockRoot[:],
},
}
stateGen := stategen.NewMockService()
stateGen.StatesByRoot[blockRoot] = state
require.NoError(t, db.SaveBlock(ctx, parent))
require.NoError(t, db.SaveGenesisBlockRoot(ctx, parentR))
b := testutil.NewBeaconBlock()
b.Block.ParentRoot = parentR[:]
b.Block.StateRoot = bytesutil.PadTo([]byte("finalized"), 32)
require.NoError(t, db.SaveBlock(ctx, b))
r, err := b.Block.HashTreeRoot()
require.NoError(t, err)
require.NoError(t, db.SaveStateSummary(ctx, &pb.StateSummary{Root: r[:]}))
require.NoError(t, db.SaveFinalizedCheckpoint(ctx, &eth.Checkpoint{Root: r[:]}))
s := Server{
ChainInfoFetcher: chainService,
StateGenService: stateGen,
BeaconDB: db,
}
resp, err := s.GetStateRoot(ctx, &ethpb.StateRequest{
StateId: []byte("finalized"),
})
require.NoError(t, err)
expectedRoot, err := state.HashTreeRoot(ctx)
require.NoError(t, err)
var b [32]byte
copy(b[:], resp.Data.StateRoot)
assert.DeepEqual(t, expectedRoot, b)
assert.DeepEqual(t, bytesutil.PadTo([]byte("finalized"), 32), resp.Data.StateRoot)
})
t.Run("Justified", func(t *testing.T) {
state, err := testutil.NewBeaconState()
parent := testutil.NewBeaconBlock()
parentR, err := parent.Block.HashTreeRoot()
require.NoError(t, err)
var blockRoot [32]byte
copy(blockRoot[:], "block_root")
chainService := &chainMock.ChainService{
CurrentJustifiedCheckPoint: &eth.Checkpoint{
Root: blockRoot[:],
},
}
stateGen := stategen.NewMockService()
stateGen.StatesByRoot[blockRoot] = state
require.NoError(t, db.SaveBlock(ctx, parent))
require.NoError(t, db.SaveGenesisBlockRoot(ctx, parentR))
b := testutil.NewBeaconBlock()
b.Block.ParentRoot = parentR[:]
b.Block.StateRoot = bytesutil.PadTo([]byte("justified"), 32)
require.NoError(t, db.SaveBlock(ctx, b))
r, err := b.Block.HashTreeRoot()
require.NoError(t, err)
require.NoError(t, db.SaveStateSummary(ctx, &pb.StateSummary{Root: r[:]}))
require.NoError(t, db.SaveJustifiedCheckpoint(ctx, &eth.Checkpoint{Root: r[:]}))
s := Server{
ChainInfoFetcher: chainService,
StateGenService: stateGen,
BeaconDB: db,
}
resp, err := s.GetStateRoot(ctx, &ethpb.StateRequest{
StateId: []byte("justified"),
})
require.NoError(t, err)
expectedRoot, err := state.HashTreeRoot(ctx)
require.NoError(t, err)
var b [32]byte
copy(b[:], resp.Data.StateRoot)
assert.DeepEqual(t, expectedRoot, b)
assert.DeepEqual(t, bytesutil.PadTo([]byte("justified"), 32), resp.Data.StateRoot)
})
t.Run("Hex root", func(t *testing.T) {
@ -217,45 +212,54 @@ func TestGetStateRoot(t *testing.T) {
_, err = s.GetStateRoot(ctx, &ethpb.StateRequest{
StateId: stateId,
})
assert.ErrorContains(t, fmt.Sprintf("State not found in the last %d states", len(state.StateRoots())), err)
assert.ErrorContains(t, fmt.Sprintf("State not found in the last %d state roots in head state", len(state.StateRoots())), err)
})
t.Run("Slot", func(t *testing.T) {
state, err := testutil.NewBeaconState(func(state *pb.BeaconState) {
state.Slot = 100
})
require.NoError(t, err)
chainService := &chainMock.ChainService{
State: state,
}
stateGen := stategen.NewMockService()
stateGen.StatesBySlot[100] = state
b := testutil.NewBeaconBlock()
b.Block.Slot = 100
b.Block.StateRoot = bytesutil.PadTo([]byte("slot"), 32)
require.NoError(t, db.SaveBlock(ctx, b))
s := Server{
ChainInfoFetcher: chainService,
StateGenService: stateGen,
BeaconDB: db,
GenesisTimeFetcher: &chainMock.ChainService{},
}
resp, err := s.GetStateRoot(ctx, &ethpb.StateRequest{
StateId: []byte("100"),
})
require.NoError(t, err)
expectedRoot, err := state.HashTreeRoot(ctx)
require.NoError(t, err)
var b [32]byte
copy(b[:], resp.Data.StateRoot)
assert.DeepEqual(t, expectedRoot, b)
assert.DeepEqual(t, bytesutil.PadTo([]byte("slot"), 32), resp.Data.StateRoot)
})
t.Run("Multiple slots", func(t *testing.T) {
b := testutil.NewBeaconBlock()
b.Block.Slot = 100
b.Block.StateRoot = bytesutil.PadTo([]byte("slot"), 32)
require.NoError(t, db.SaveBlock(ctx, b))
b = testutil.NewBeaconBlock()
b.Block.Slot = 100
b.Block.StateRoot = bytesutil.PadTo([]byte("sLot"), 32)
require.NoError(t, db.SaveBlock(ctx, b))
s := Server{
BeaconDB: db,
GenesisTimeFetcher: &chainMock.ChainService{},
}
_, err := s.GetStateRoot(ctx, &ethpb.StateRequest{
StateId: []byte("100"),
})
assert.ErrorContains(t, "Multiple blocks exist in same slot", err)
})
t.Run("Slot too big", func(t *testing.T) {
state, err := testutil.NewBeaconState()
require.NoError(t, err)
chainService := &chainMock.ChainService{
State: state,
}
s := Server{
ChainInfoFetcher: chainService,
GenesisTimeFetcher: &chainMock.ChainService{
Genesis: time.Now(),
},
}
_, err = s.GetStateRoot(ctx, &ethpb.StateRequest{
StateId: []byte(strconv.FormatUint(state.Slot()+1, 10)),
_, err := s.GetStateRoot(ctx, &ethpb.StateRequest{
StateId: []byte(strconv.FormatUint(1, 10)),
})
assert.ErrorContains(t, "Slot cannot be in the future", err)
})