Implement GetStateV2 in the beacon API (#9506)

* implementation

# Conflicts:
#	beacon-chain/state/v2/BUILD.bazel

* Revert "Auxiliary commit to revert individual files from 2cbe98c88777cac071876fe97f85029fad964e51"

This reverts commit edc4ff52e7796aefd1782e31eaf40231a3134693.

* tests

* fix function call

* make state package visible to migration

* fix span names

Co-authored-by: terence tsao <terence@prysmaticlabs.com>
Co-authored-by: Raul Jordan <raul@prysmaticlabs.com>
This commit is contained in:
Radosław Kapka
2021-09-02 21:28:55 +02:00
committed by GitHub
parent 1f48accb0e
commit c45fe5cc1c
8 changed files with 416 additions and 54 deletions

View File

@@ -13,8 +13,11 @@ go_library(
"//beacon-chain/db:go_default_library",
"//beacon-chain/rpc/eth/helpers:go_default_library",
"//beacon-chain/rpc/statefetcher:go_default_library",
"//beacon-chain/state/v2:go_default_library",
"//proto/eth/v1:go_default_library",
"//proto/eth/v2:go_default_library",
"//proto/migration:go_default_library",
"//shared/version:go_default_library",
"@io_opencensus_go//trace:go_default_library",
"@org_golang_google_grpc//codes:go_default_library",
"@org_golang_google_grpc//status:go_default_library",
@@ -30,6 +33,7 @@ go_test(
"//beacon-chain/blockchain/testing:go_default_library",
"//beacon-chain/rpc/testutil:go_default_library",
"//proto/eth/v1:go_default_library",
"//proto/eth/v2:go_default_library",
"//shared/bytesutil:go_default_library",
"//shared/testutil:go_default_library",
"//shared/testutil/assert:go_default_library",

View File

@@ -4,17 +4,20 @@ import (
"context"
"github.com/prysmaticlabs/prysm/beacon-chain/rpc/eth/helpers"
statev2 "github.com/prysmaticlabs/prysm/beacon-chain/state/v2"
ethpbv1 "github.com/prysmaticlabs/prysm/proto/eth/v1"
ethpbv2 "github.com/prysmaticlabs/prysm/proto/eth/v2"
"github.com/prysmaticlabs/prysm/proto/migration"
"github.com/prysmaticlabs/prysm/shared/version"
"go.opencensus.io/trace"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
"google.golang.org/protobuf/types/known/emptypb"
)
// GetBeaconState returns the full beacon state for a given state id.
// GetBeaconState returns the full beacon state for a given state ID.
func (ds *Server) GetBeaconState(ctx context.Context, req *ethpbv1.StateRequest) (*ethpbv1.BeaconStateResponse, error) {
ctx, span := trace.StartSpan(ctx, "beaconv1.GetBeaconState")
ctx, span := trace.StartSpan(ctx, "debug.GetBeaconState")
defer span.End()
state, err := ds.StateFetcher.State(ctx, req.StateId)
@@ -32,9 +35,9 @@ func (ds *Server) GetBeaconState(ctx context.Context, req *ethpbv1.StateRequest)
}, nil
}
// GetBeaconStateSSZ returns the SSZ-serialized version of the full beacon state object for given stateId.
// GetBeaconStateSSZ returns the SSZ-serialized version of the full beacon state object for given state ID.
func (ds *Server) GetBeaconStateSSZ(ctx context.Context, req *ethpbv1.StateRequest) (*ethpbv1.BeaconStateSSZResponse, error) {
ctx, span := trace.StartSpan(ctx, "beaconv1.GetBeaconStateSSZ")
ctx, span := trace.StartSpan(ctx, "debug.GetBeaconStateSSZ")
defer span.End()
state, err := ds.StateFetcher.State(ctx, req.StateId)
@@ -50,12 +53,61 @@ func (ds *Server) GetBeaconStateSSZ(ctx context.Context, req *ethpbv1.StateReque
return &ethpbv1.BeaconStateSSZResponse{Data: sszState}, nil
}
func (ds *Server) GetBeaconStateV2(ctx context.Context, request *ethpbv2.StateRequestV2) (*ethpbv2.BeaconStateResponseV2, error) {
panic("implement me")
// GetBeaconStateV2 returns the full beacon state for a given state ID.
func (ds *Server) GetBeaconStateV2(ctx context.Context, req *ethpbv2.StateRequestV2) (*ethpbv2.BeaconStateResponseV2, error) {
ctx, span := trace.StartSpan(ctx, "debug.GetBeaconStateV2")
defer span.End()
state, err := ds.StateFetcher.State(ctx, req.StateId)
if err != nil {
return nil, helpers.PrepareStateFetchGRPCError(err)
}
switch state.Version() {
case version.Phase0:
protoState, err := state.ToProto()
if err != nil {
return nil, status.Errorf(codes.Internal, "Could not convert state to proto: %v", err)
}
return &ethpbv2.BeaconStateResponseV2{
Data: &ethpbv2.BeaconStateContainer{
State: &ethpbv2.BeaconStateContainer_Phase0State{Phase0State: protoState},
},
}, nil
case version.Altair:
altairState, ok := state.(*statev2.BeaconState)
if !ok {
return nil, status.Error(codes.Internal, "Altair state type assertion failed")
}
protoState, err := migration.BeaconStateAltairToV2(altairState)
if err != nil {
return nil, status.Errorf(codes.Internal, "Could not convert state to proto: %v", err)
}
return &ethpbv2.BeaconStateResponseV2{
Data: &ethpbv2.BeaconStateContainer{
State: &ethpbv2.BeaconStateContainer_AltairState{AltairState: protoState},
},
}, nil
default:
return nil, status.Error(codes.Internal, "Unsupported state version")
}
}
func (ds *Server) GetBeaconStateSSZV2(ctx context.Context, request *ethpbv2.StateRequestV2) (*ethpbv2.BeaconStateSSZResponseV2, error) {
panic("implement me")
// GetBeaconStateSSZV2 returns the SSZ-serialized version of the full beacon state object for given state ID.
func (ds *Server) GetBeaconStateSSZV2(ctx context.Context, req *ethpbv2.StateRequestV2) (*ethpbv2.BeaconStateSSZResponseV2, error) {
ctx, span := trace.StartSpan(ctx, "debug.GetBeaconStateSSZV2")
defer span.End()
state, err := ds.StateFetcher.State(ctx, req.StateId)
if err != nil {
return nil, helpers.PrepareStateFetchGRPCError(err)
}
sszState, err := state.MarshalSSZ()
if err != nil {
return nil, status.Errorf(codes.Internal, "Could not marshal state into SSZ: %v", err)
}
return &ethpbv2.BeaconStateSSZResponseV2{Data: sszState}, nil
}
// ListForkChoiceHeads retrieves the fork choice leaves for the current head.

View File

@@ -7,7 +7,8 @@ import (
types "github.com/prysmaticlabs/eth2-types"
blockchainmock "github.com/prysmaticlabs/prysm/beacon-chain/blockchain/testing"
"github.com/prysmaticlabs/prysm/beacon-chain/rpc/testutil"
ethpb "github.com/prysmaticlabs/prysm/proto/eth/v1"
ethpbv1 "github.com/prysmaticlabs/prysm/proto/eth/v1"
ethpbv2 "github.com/prysmaticlabs/prysm/proto/eth/v2"
"github.com/prysmaticlabs/prysm/shared/bytesutil"
sharedtestutil "github.com/prysmaticlabs/prysm/shared/testutil"
"github.com/prysmaticlabs/prysm/shared/testutil/assert"
@@ -23,13 +24,43 @@ func TestGetBeaconState(t *testing.T) {
BeaconState: fakeState,
},
}
resp, err := server.GetBeaconState(context.Background(), &ethpb.StateRequest{
resp, err := server.GetBeaconState(context.Background(), &ethpbv1.StateRequest{
StateId: make([]byte, 0),
})
require.NoError(t, err)
assert.NotNil(t, resp)
}
func TestGetBeaconStateV2(t *testing.T) {
t.Run("Phase 0", func(t *testing.T) {
fakeState, err := sharedtestutil.NewBeaconState()
require.NoError(t, err)
server := &Server{
StateFetcher: &testutil.MockFetcher{
BeaconState: fakeState,
},
}
resp, err := server.GetBeaconStateV2(context.Background(), &ethpbv2.StateRequestV2{
StateId: make([]byte, 0),
})
require.NoError(t, err)
assert.NotNil(t, resp)
})
t.Run("Altair", func(t *testing.T) {
fakeState, _ := sharedtestutil.DeterministicGenesisStateAltair(t, 1)
server := &Server{
StateFetcher: &testutil.MockFetcher{
BeaconState: fakeState,
},
}
resp, err := server.GetBeaconStateV2(context.Background(), &ethpbv2.StateRequestV2{
StateId: make([]byte, 0),
})
require.NoError(t, err)
assert.NotNil(t, resp)
})
}
func TestGetBeaconStateSSZ(t *testing.T) {
fakeState, err := sharedtestutil.NewBeaconState()
require.NoError(t, err)
@@ -41,7 +72,7 @@ func TestGetBeaconStateSSZ(t *testing.T) {
BeaconState: fakeState,
},
}
resp, err := server.GetBeaconStateSSZ(context.Background(), &ethpb.StateRequest{
resp, err := server.GetBeaconStateSSZ(context.Background(), &ethpbv1.StateRequest{
StateId: make([]byte, 0),
})
require.NoError(t, err)
@@ -50,6 +81,46 @@ func TestGetBeaconStateSSZ(t *testing.T) {
assert.DeepEqual(t, sszState, resp.Data)
}
func TestGetBeaconStateSSZV2(t *testing.T) {
t.Run("Phase 0", func(t *testing.T) {
fakeState, err := sharedtestutil.NewBeaconState()
require.NoError(t, err)
sszState, err := fakeState.MarshalSSZ()
require.NoError(t, err)
server := &Server{
StateFetcher: &testutil.MockFetcher{
BeaconState: fakeState,
},
}
resp, err := server.GetBeaconStateSSZV2(context.Background(), &ethpbv2.StateRequestV2{
StateId: make([]byte, 0),
})
require.NoError(t, err)
assert.NotNil(t, resp)
assert.DeepEqual(t, sszState, resp.Data)
})
t.Run("Altair", func(t *testing.T) {
fakeState, _ := sharedtestutil.DeterministicGenesisStateAltair(t, 1)
sszState, err := fakeState.MarshalSSZ()
require.NoError(t, err)
server := &Server{
StateFetcher: &testutil.MockFetcher{
BeaconState: fakeState,
},
}
resp, err := server.GetBeaconStateSSZV2(context.Background(), &ethpbv2.StateRequestV2{
StateId: make([]byte, 0),
})
require.NoError(t, err)
assert.NotNil(t, resp)
assert.DeepEqual(t, sszState, resp.Data)
})
}
func TestListForkChoiceHeads(t *testing.T) {
ctx := context.Background()

View File

@@ -19,6 +19,7 @@ go_library(
importpath = "github.com/prysmaticlabs/prysm/beacon-chain/state/v2",
visibility = [
"//beacon-chain:__subpackages__",
"//proto/migration:__subpackages__",
"//shared/testutil:__pkg__",
"//spectest:__subpackages__",
],

View File

@@ -313,7 +313,7 @@ type BeaconStateResponseV2 struct {
sizeCache protoimpl.SizeCache
unknownFields protoimpl.UnknownFields
Data *BeaconStateV2 `protobuf:"bytes,1,opt,name=data,proto3" json:"data,omitempty"`
Data *BeaconStateContainer `protobuf:"bytes,1,opt,name=data,proto3" json:"data,omitempty"`
}
func (x *BeaconStateResponseV2) Reset() {
@@ -348,7 +348,7 @@ func (*BeaconStateResponseV2) Descriptor() ([]byte, []int) {
return file_proto_eth_v2_beacon_state_proto_rawDescGZIP(), []int{2}
}
func (x *BeaconStateResponseV2) GetData() *BeaconStateV2 {
func (x *BeaconStateResponseV2) GetData() *BeaconStateContainer {
if x != nil {
return x.Data
}
@@ -402,6 +402,86 @@ func (x *BeaconStateSSZResponseV2) GetData() []byte {
return nil
}
type BeaconStateContainer struct {
state protoimpl.MessageState
sizeCache protoimpl.SizeCache
unknownFields protoimpl.UnknownFields
// Types that are assignable to State:
// *BeaconStateContainer_Phase0State
// *BeaconStateContainer_AltairState
State isBeaconStateContainer_State `protobuf_oneof:"state"`
}
func (x *BeaconStateContainer) Reset() {
*x = BeaconStateContainer{}
if protoimpl.UnsafeEnabled {
mi := &file_proto_eth_v2_beacon_state_proto_msgTypes[4]
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
ms.StoreMessageInfo(mi)
}
}
func (x *BeaconStateContainer) String() string {
return protoimpl.X.MessageStringOf(x)
}
func (*BeaconStateContainer) ProtoMessage() {}
func (x *BeaconStateContainer) ProtoReflect() protoreflect.Message {
mi := &file_proto_eth_v2_beacon_state_proto_msgTypes[4]
if protoimpl.UnsafeEnabled && x != nil {
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
if ms.LoadMessageInfo() == nil {
ms.StoreMessageInfo(mi)
}
return ms
}
return mi.MessageOf(x)
}
// Deprecated: Use BeaconStateContainer.ProtoReflect.Descriptor instead.
func (*BeaconStateContainer) Descriptor() ([]byte, []int) {
return file_proto_eth_v2_beacon_state_proto_rawDescGZIP(), []int{4}
}
func (m *BeaconStateContainer) GetState() isBeaconStateContainer_State {
if m != nil {
return m.State
}
return nil
}
func (x *BeaconStateContainer) GetPhase0State() *v1.BeaconState {
if x, ok := x.GetState().(*BeaconStateContainer_Phase0State); ok {
return x.Phase0State
}
return nil
}
func (x *BeaconStateContainer) GetAltairState() *BeaconStateV2 {
if x, ok := x.GetState().(*BeaconStateContainer_AltairState); ok {
return x.AltairState
}
return nil
}
type isBeaconStateContainer_State interface {
isBeaconStateContainer_State()
}
type BeaconStateContainer_Phase0State struct {
Phase0State *v1.BeaconState `protobuf:"bytes,1,opt,name=phase0State,proto3,oneof"`
}
type BeaconStateContainer_AltairState struct {
AltairState *BeaconStateV2 `protobuf:"bytes,2,opt,name=altairState,proto3,oneof"`
}
func (*BeaconStateContainer_Phase0State) isBeaconStateContainer_State() {}
func (*BeaconStateContainer_AltairState) isBeaconStateContainer_State() {}
var File_proto_eth_v2_beacon_state_proto protoreflect.FileDescriptor
var file_proto_eth_v2_beacon_state_proto_rawDesc = []byte{
@@ -530,23 +610,34 @@ var file_proto_eth_v2_beacon_state_proto_rawDesc = []byte{
0x74, 0x65, 0x65, 0x22, 0x2b, 0x0a, 0x0e, 0x53, 0x74, 0x61, 0x74, 0x65, 0x52, 0x65, 0x71, 0x75,
0x65, 0x73, 0x74, 0x56, 0x32, 0x12, 0x19, 0x0a, 0x08, 0x73, 0x74, 0x61, 0x74, 0x65, 0x5f, 0x69,
0x64, 0x18, 0x01, 0x20, 0x01, 0x28, 0x0c, 0x52, 0x07, 0x73, 0x74, 0x61, 0x74, 0x65, 0x49, 0x64,
0x22, 0x4b, 0x0a, 0x15, 0x42, 0x65, 0x61, 0x63, 0x6f, 0x6e, 0x53, 0x74, 0x61, 0x74, 0x65, 0x52,
0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x56, 0x32, 0x12, 0x32, 0x0a, 0x04, 0x64, 0x61, 0x74,
0x61, 0x18, 0x01, 0x20, 0x01, 0x28, 0x0b, 0x32, 0x1e, 0x2e, 0x65, 0x74, 0x68, 0x65, 0x72, 0x65,
0x22, 0x52, 0x0a, 0x15, 0x42, 0x65, 0x61, 0x63, 0x6f, 0x6e, 0x53, 0x74, 0x61, 0x74, 0x65, 0x52,
0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x56, 0x32, 0x12, 0x39, 0x0a, 0x04, 0x64, 0x61, 0x74,
0x61, 0x18, 0x01, 0x20, 0x01, 0x28, 0x0b, 0x32, 0x25, 0x2e, 0x65, 0x74, 0x68, 0x65, 0x72, 0x65,
0x75, 0x6d, 0x2e, 0x65, 0x74, 0x68, 0x2e, 0x76, 0x32, 0x2e, 0x42, 0x65, 0x61, 0x63, 0x6f, 0x6e,
0x53, 0x74, 0x61, 0x74, 0x65, 0x56, 0x32, 0x52, 0x04, 0x64, 0x61, 0x74, 0x61, 0x22, 0x2e, 0x0a,
0x18, 0x42, 0x65, 0x61, 0x63, 0x6f, 0x6e, 0x53, 0x74, 0x61, 0x74, 0x65, 0x53, 0x53, 0x5a, 0x52,
0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x56, 0x32, 0x12, 0x12, 0x0a, 0x04, 0x64, 0x61, 0x74,
0x61, 0x18, 0x01, 0x20, 0x01, 0x28, 0x0c, 0x52, 0x04, 0x64, 0x61, 0x74, 0x61, 0x42, 0x80, 0x01,
0x0a, 0x13, 0x6f, 0x72, 0x67, 0x2e, 0x65, 0x74, 0x68, 0x65, 0x72, 0x65, 0x75, 0x6d, 0x2e, 0x65,
0x74, 0x68, 0x2e, 0x76, 0x32, 0x42, 0x12, 0x53, 0x79, 0x6e, 0x63, 0x43, 0x6f, 0x6d, 0x6d, 0x69,
0x74, 0x74, 0x65, 0x65, 0x50, 0x72, 0x6f, 0x74, 0x6f, 0x50, 0x01, 0x5a, 0x2f, 0x67, 0x69, 0x74,
0x68, 0x75, 0x62, 0x2e, 0x63, 0x6f, 0x6d, 0x2f, 0x70, 0x72, 0x79, 0x73, 0x6d, 0x61, 0x74, 0x69,
0x63, 0x6c, 0x61, 0x62, 0x73, 0x2f, 0x70, 0x72, 0x79, 0x73, 0x6d, 0x2f, 0x70, 0x72, 0x6f, 0x74,
0x6f, 0x2f, 0x65, 0x74, 0x68, 0x2f, 0x76, 0x32, 0x3b, 0x65, 0x74, 0x68, 0xaa, 0x02, 0x0f, 0x45,
0x74, 0x68, 0x65, 0x72, 0x65, 0x75, 0x6d, 0x2e, 0x45, 0x74, 0x68, 0x2e, 0x56, 0x32, 0xca, 0x02,
0x0f, 0x45, 0x74, 0x68, 0x65, 0x72, 0x65, 0x75, 0x6d, 0x5c, 0x45, 0x74, 0x68, 0x5c, 0x76, 0x32,
0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33,
0x53, 0x74, 0x61, 0x74, 0x65, 0x43, 0x6f, 0x6e, 0x74, 0x61, 0x69, 0x6e, 0x65, 0x72, 0x52, 0x04,
0x64, 0x61, 0x74, 0x61, 0x22, 0x2e, 0x0a, 0x18, 0x42, 0x65, 0x61, 0x63, 0x6f, 0x6e, 0x53, 0x74,
0x61, 0x74, 0x65, 0x53, 0x53, 0x5a, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x56, 0x32,
0x12, 0x12, 0x0a, 0x04, 0x64, 0x61, 0x74, 0x61, 0x18, 0x01, 0x20, 0x01, 0x28, 0x0c, 0x52, 0x04,
0x64, 0x61, 0x74, 0x61, 0x22, 0xa5, 0x01, 0x0a, 0x14, 0x42, 0x65, 0x61, 0x63, 0x6f, 0x6e, 0x53,
0x74, 0x61, 0x74, 0x65, 0x43, 0x6f, 0x6e, 0x74, 0x61, 0x69, 0x6e, 0x65, 0x72, 0x12, 0x40, 0x0a,
0x0b, 0x70, 0x68, 0x61, 0x73, 0x65, 0x30, 0x53, 0x74, 0x61, 0x74, 0x65, 0x18, 0x01, 0x20, 0x01,
0x28, 0x0b, 0x32, 0x1c, 0x2e, 0x65, 0x74, 0x68, 0x65, 0x72, 0x65, 0x75, 0x6d, 0x2e, 0x65, 0x74,
0x68, 0x2e, 0x76, 0x31, 0x2e, 0x42, 0x65, 0x61, 0x63, 0x6f, 0x6e, 0x53, 0x74, 0x61, 0x74, 0x65,
0x48, 0x00, 0x52, 0x0b, 0x70, 0x68, 0x61, 0x73, 0x65, 0x30, 0x53, 0x74, 0x61, 0x74, 0x65, 0x12,
0x42, 0x0a, 0x0b, 0x61, 0x6c, 0x74, 0x61, 0x69, 0x72, 0x53, 0x74, 0x61, 0x74, 0x65, 0x18, 0x02,
0x20, 0x01, 0x28, 0x0b, 0x32, 0x1e, 0x2e, 0x65, 0x74, 0x68, 0x65, 0x72, 0x65, 0x75, 0x6d, 0x2e,
0x65, 0x74, 0x68, 0x2e, 0x76, 0x32, 0x2e, 0x42, 0x65, 0x61, 0x63, 0x6f, 0x6e, 0x53, 0x74, 0x61,
0x74, 0x65, 0x56, 0x32, 0x48, 0x00, 0x52, 0x0b, 0x61, 0x6c, 0x74, 0x61, 0x69, 0x72, 0x53, 0x74,
0x61, 0x74, 0x65, 0x42, 0x07, 0x0a, 0x05, 0x73, 0x74, 0x61, 0x74, 0x65, 0x42, 0x80, 0x01, 0x0a,
0x13, 0x6f, 0x72, 0x67, 0x2e, 0x65, 0x74, 0x68, 0x65, 0x72, 0x65, 0x75, 0x6d, 0x2e, 0x65, 0x74,
0x68, 0x2e, 0x76, 0x32, 0x42, 0x12, 0x53, 0x79, 0x6e, 0x63, 0x43, 0x6f, 0x6d, 0x6d, 0x69, 0x74,
0x74, 0x65, 0x65, 0x50, 0x72, 0x6f, 0x74, 0x6f, 0x50, 0x01, 0x5a, 0x2f, 0x67, 0x69, 0x74, 0x68,
0x75, 0x62, 0x2e, 0x63, 0x6f, 0x6d, 0x2f, 0x70, 0x72, 0x79, 0x73, 0x6d, 0x61, 0x74, 0x69, 0x63,
0x6c, 0x61, 0x62, 0x73, 0x2f, 0x70, 0x72, 0x79, 0x73, 0x6d, 0x2f, 0x70, 0x72, 0x6f, 0x74, 0x6f,
0x2f, 0x65, 0x74, 0x68, 0x2f, 0x76, 0x32, 0x3b, 0x65, 0x74, 0x68, 0xaa, 0x02, 0x0f, 0x45, 0x74,
0x68, 0x65, 0x72, 0x65, 0x75, 0x6d, 0x2e, 0x45, 0x74, 0x68, 0x2e, 0x56, 0x32, 0xca, 0x02, 0x0f,
0x45, 0x74, 0x68, 0x65, 0x72, 0x65, 0x75, 0x6d, 0x5c, 0x45, 0x74, 0x68, 0x5c, 0x76, 0x32, 0x62,
0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33,
}
var (
@@ -561,36 +652,40 @@ func file_proto_eth_v2_beacon_state_proto_rawDescGZIP() []byte {
return file_proto_eth_v2_beacon_state_proto_rawDescData
}
var file_proto_eth_v2_beacon_state_proto_msgTypes = make([]protoimpl.MessageInfo, 4)
var file_proto_eth_v2_beacon_state_proto_msgTypes = make([]protoimpl.MessageInfo, 5)
var file_proto_eth_v2_beacon_state_proto_goTypes = []interface{}{
(*BeaconStateV2)(nil), // 0: ethereum.eth.v2.BeaconStateV2
(*StateRequestV2)(nil), // 1: ethereum.eth.v2.StateRequestV2
(*BeaconStateResponseV2)(nil), // 2: ethereum.eth.v2.BeaconStateResponseV2
(*BeaconStateSSZResponseV2)(nil), // 3: ethereum.eth.v2.BeaconStateSSZResponseV2
(*v1.Fork)(nil), // 4: ethereum.eth.v1.Fork
(*v1.BeaconBlockHeader)(nil), // 5: ethereum.eth.v1.BeaconBlockHeader
(*v1.Eth1Data)(nil), // 6: ethereum.eth.v1.Eth1Data
(*v1.Validator)(nil), // 7: ethereum.eth.v1.Validator
(*v1.Checkpoint)(nil), // 8: ethereum.eth.v1.Checkpoint
(*SyncCommittee)(nil), // 9: ethereum.eth.v2.SyncCommittee
(*BeaconStateContainer)(nil), // 4: ethereum.eth.v2.BeaconStateContainer
(*v1.Fork)(nil), // 5: ethereum.eth.v1.Fork
(*v1.BeaconBlockHeader)(nil), // 6: ethereum.eth.v1.BeaconBlockHeader
(*v1.Eth1Data)(nil), // 7: ethereum.eth.v1.Eth1Data
(*v1.Validator)(nil), // 8: ethereum.eth.v1.Validator
(*v1.Checkpoint)(nil), // 9: ethereum.eth.v1.Checkpoint
(*SyncCommittee)(nil), // 10: ethereum.eth.v2.SyncCommittee
(*v1.BeaconState)(nil), // 11: ethereum.eth.v1.BeaconState
}
var file_proto_eth_v2_beacon_state_proto_depIdxs = []int32{
4, // 0: ethereum.eth.v2.BeaconStateV2.fork:type_name -> ethereum.eth.v1.Fork
5, // 1: ethereum.eth.v2.BeaconStateV2.latest_block_header:type_name -> ethereum.eth.v1.BeaconBlockHeader
6, // 2: ethereum.eth.v2.BeaconStateV2.eth1_data:type_name -> ethereum.eth.v1.Eth1Data
6, // 3: ethereum.eth.v2.BeaconStateV2.eth1_data_votes:type_name -> ethereum.eth.v1.Eth1Data
7, // 4: ethereum.eth.v2.BeaconStateV2.validators:type_name -> ethereum.eth.v1.Validator
8, // 5: ethereum.eth.v2.BeaconStateV2.previous_justified_checkpoint:type_name -> ethereum.eth.v1.Checkpoint
8, // 6: ethereum.eth.v2.BeaconStateV2.current_justified_checkpoint:type_name -> ethereum.eth.v1.Checkpoint
8, // 7: ethereum.eth.v2.BeaconStateV2.finalized_checkpoint:type_name -> ethereum.eth.v1.Checkpoint
9, // 8: ethereum.eth.v2.BeaconStateV2.current_sync_committee:type_name -> ethereum.eth.v2.SyncCommittee
9, // 9: ethereum.eth.v2.BeaconStateV2.next_sync_committee:type_name -> ethereum.eth.v2.SyncCommittee
0, // 10: ethereum.eth.v2.BeaconStateResponseV2.data:type_name -> ethereum.eth.v2.BeaconStateV2
11, // [11:11] is the sub-list for method output_type
11, // [11:11] is the sub-list for method input_type
11, // [11:11] is the sub-list for extension type_name
11, // [11:11] is the sub-list for extension extendee
0, // [0:11] is the sub-list for field type_name
5, // 0: ethereum.eth.v2.BeaconStateV2.fork:type_name -> ethereum.eth.v1.Fork
6, // 1: ethereum.eth.v2.BeaconStateV2.latest_block_header:type_name -> ethereum.eth.v1.BeaconBlockHeader
7, // 2: ethereum.eth.v2.BeaconStateV2.eth1_data:type_name -> ethereum.eth.v1.Eth1Data
7, // 3: ethereum.eth.v2.BeaconStateV2.eth1_data_votes:type_name -> ethereum.eth.v1.Eth1Data
8, // 4: ethereum.eth.v2.BeaconStateV2.validators:type_name -> ethereum.eth.v1.Validator
9, // 5: ethereum.eth.v2.BeaconStateV2.previous_justified_checkpoint:type_name -> ethereum.eth.v1.Checkpoint
9, // 6: ethereum.eth.v2.BeaconStateV2.current_justified_checkpoint:type_name -> ethereum.eth.v1.Checkpoint
9, // 7: ethereum.eth.v2.BeaconStateV2.finalized_checkpoint:type_name -> ethereum.eth.v1.Checkpoint
10, // 8: ethereum.eth.v2.BeaconStateV2.current_sync_committee:type_name -> ethereum.eth.v2.SyncCommittee
10, // 9: ethereum.eth.v2.BeaconStateV2.next_sync_committee:type_name -> ethereum.eth.v2.SyncCommittee
4, // 10: ethereum.eth.v2.BeaconStateResponseV2.data:type_name -> ethereum.eth.v2.BeaconStateContainer
11, // 11: ethereum.eth.v2.BeaconStateContainer.phase0State:type_name -> ethereum.eth.v1.BeaconState
0, // 12: ethereum.eth.v2.BeaconStateContainer.altairState:type_name -> ethereum.eth.v2.BeaconStateV2
13, // [13:13] is the sub-list for method output_type
13, // [13:13] is the sub-list for method input_type
13, // [13:13] is the sub-list for extension type_name
13, // [13:13] is the sub-list for extension extendee
0, // [0:13] is the sub-list for field type_name
}
func init() { file_proto_eth_v2_beacon_state_proto_init() }
@@ -648,6 +743,22 @@ func file_proto_eth_v2_beacon_state_proto_init() {
return nil
}
}
file_proto_eth_v2_beacon_state_proto_msgTypes[4].Exporter = func(v interface{}, i int) interface{} {
switch v := v.(*BeaconStateContainer); i {
case 0:
return &v.state
case 1:
return &v.sizeCache
case 2:
return &v.unknownFields
default:
return nil
}
}
}
file_proto_eth_v2_beacon_state_proto_msgTypes[4].OneofWrappers = []interface{}{
(*BeaconStateContainer_Phase0State)(nil),
(*BeaconStateContainer_AltairState)(nil),
}
type x struct{}
out := protoimpl.TypeBuilder{
@@ -655,7 +766,7 @@ func file_proto_eth_v2_beacon_state_proto_init() {
GoPackagePath: reflect.TypeOf(x{}).PkgPath(),
RawDescriptor: file_proto_eth_v2_beacon_state_proto_rawDesc,
NumEnums: 0,
NumMessages: 4,
NumMessages: 5,
NumExtensions: 0,
NumServices: 0,
},

View File

@@ -81,9 +81,16 @@ message StateRequestV2 {
}
message BeaconStateResponseV2 {
BeaconStateV2 data = 1;
BeaconStateContainer data = 1;
}
message BeaconStateSSZResponseV2 {
bytes data = 1;
}
message BeaconStateContainer {
oneof state {
v1.BeaconState phase0State = 1;
BeaconStateV2 altairState = 2;
}
}

View File

@@ -9,10 +9,12 @@ go_library(
importpath = "github.com/prysmaticlabs/prysm/proto/migration",
visibility = ["//visibility:public"],
deps = [
"//beacon-chain/state/v2:go_default_library",
"//proto/eth/v1:go_default_library",
"//proto/eth/v2:go_default_library",
"//proto/prysm/v1alpha1:go_default_library",
"//proto/prysm/v1alpha1/block:go_default_library",
"//shared/bytesutil:go_default_library",
"@com_github_pkg_errors//:go_default_library",
"@org_golang_google_protobuf//proto:go_default_library",
],

View File

@@ -2,10 +2,12 @@ package migration
import (
"github.com/pkg/errors"
statev2 "github.com/prysmaticlabs/prysm/beacon-chain/state/v2"
ethpbv1 "github.com/prysmaticlabs/prysm/proto/eth/v1"
ethpbv2 "github.com/prysmaticlabs/prysm/proto/eth/v2"
ethpbalpha "github.com/prysmaticlabs/prysm/proto/prysm/v1alpha1"
"github.com/prysmaticlabs/prysm/proto/prysm/v1alpha1/block"
"github.com/prysmaticlabs/prysm/shared/bytesutil"
"google.golang.org/protobuf/proto"
)
@@ -381,3 +383,115 @@ func V1Alpha1BeaconBlockAltairToV2(v1alpha1Block *ethpbalpha.BeaconBlockAltair)
}
return v2Block, nil
}
func BeaconStateAltairToV2(altairState *statev2.BeaconState) (*ethpbv2.BeaconStateV2, error) {
sourceFork := altairState.Fork()
sourceLatestBlockHeader := altairState.LatestBlockHeader()
sourceEth1Data := altairState.Eth1Data()
sourceEth1DataVotes := altairState.Eth1DataVotes()
sourceValidators := altairState.Validators()
sourcePrevJustifiedCheckpoint := altairState.PreviousJustifiedCheckpoint()
sourceCurrJustifiedCheckpoint := altairState.CurrentJustifiedCheckpoint()
sourceFinalizedCheckpoint := altairState.FinalizedCheckpoint()
resultEth1DataVotes := make([]*ethpbv1.Eth1Data, len(sourceEth1DataVotes))
for i, vote := range sourceEth1DataVotes {
resultEth1DataVotes[i] = &ethpbv1.Eth1Data{
DepositRoot: bytesutil.SafeCopyBytes(vote.DepositRoot),
DepositCount: vote.DepositCount,
BlockHash: bytesutil.SafeCopyBytes(vote.BlockHash),
}
}
resultValidators := make([]*ethpbv1.Validator, len(sourceValidators))
for i, validator := range sourceValidators {
resultValidators[i] = &ethpbv1.Validator{
Pubkey: bytesutil.SafeCopyBytes(validator.PublicKey),
WithdrawalCredentials: bytesutil.SafeCopyBytes(validator.WithdrawalCredentials),
EffectiveBalance: validator.EffectiveBalance,
Slashed: validator.Slashed,
ActivationEligibilityEpoch: validator.ActivationEligibilityEpoch,
ActivationEpoch: validator.ActivationEpoch,
ExitEpoch: validator.ExitEpoch,
WithdrawableEpoch: validator.WithdrawableEpoch,
}
}
sourcePrevEpochParticipation, err := altairState.PreviousEpochParticipation()
if err != nil {
return nil, errors.Wrap(err, "could not get previous epoch participation")
}
sourceCurrEpochParticipation, err := altairState.CurrentEpochParticipation()
if err != nil {
return nil, errors.Wrap(err, "could not get current epoch participation")
}
sourceInactivityScores, err := altairState.InactivityScores()
if err != nil {
return nil, errors.Wrap(err, "could not get inactivity scores")
}
sourceCurrSyncCommittee, err := altairState.CurrentSyncCommittee()
if err != nil {
return nil, errors.Wrap(err, "could not get current sync committee")
}
sourceNextSyncCommittee, err := altairState.NextSyncCommittee()
if err != nil {
return nil, errors.Wrap(err, "could not get next sync committee")
}
result := &ethpbv2.BeaconStateV2{
GenesisTime: altairState.GenesisTime(),
GenesisValidatorsRoot: bytesutil.SafeCopyBytes(altairState.GenesisValidatorRoot()),
Slot: altairState.Slot(),
Fork: &ethpbv1.Fork{
PreviousVersion: bytesutil.SafeCopyBytes(sourceFork.PreviousVersion),
CurrentVersion: bytesutil.SafeCopyBytes(sourceFork.CurrentVersion),
Epoch: sourceFork.Epoch,
},
LatestBlockHeader: &ethpbv1.BeaconBlockHeader{
Slot: sourceLatestBlockHeader.Slot,
ProposerIndex: sourceLatestBlockHeader.ProposerIndex,
ParentRoot: bytesutil.SafeCopyBytes(sourceLatestBlockHeader.ParentRoot),
StateRoot: bytesutil.SafeCopyBytes(sourceLatestBlockHeader.StateRoot),
BodyRoot: bytesutil.SafeCopyBytes(sourceLatestBlockHeader.BodyRoot),
},
BlockRoots: bytesutil.SafeCopy2dBytes(altairState.BlockRoots()),
StateRoots: bytesutil.SafeCopy2dBytes(altairState.StateRoots()),
HistoricalRoots: bytesutil.SafeCopy2dBytes(altairState.HistoricalRoots()),
Eth1Data: &ethpbv1.Eth1Data{
DepositRoot: bytesutil.SafeCopyBytes(sourceEth1Data.DepositRoot),
DepositCount: sourceEth1Data.DepositCount,
BlockHash: bytesutil.SafeCopyBytes(sourceEth1Data.BlockHash),
},
Eth1DataVotes: resultEth1DataVotes,
Eth1DepositIndex: altairState.Eth1DepositIndex(),
Validators: resultValidators,
Balances: altairState.Balances(),
RandaoMixes: bytesutil.SafeCopy2dBytes(altairState.RandaoMixes()),
Slashings: altairState.Slashings(),
PreviousEpochParticipation: bytesutil.SafeCopyBytes(sourcePrevEpochParticipation),
CurrentEpochParticipation: bytesutil.SafeCopyBytes(sourceCurrEpochParticipation),
JustificationBits: bytesutil.SafeCopyBytes(altairState.JustificationBits()),
PreviousJustifiedCheckpoint: &ethpbv1.Checkpoint{
Epoch: sourcePrevJustifiedCheckpoint.Epoch,
Root: bytesutil.SafeCopyBytes(sourcePrevJustifiedCheckpoint.Root),
},
CurrentJustifiedCheckpoint: &ethpbv1.Checkpoint{
Epoch: sourceCurrJustifiedCheckpoint.Epoch,
Root: bytesutil.SafeCopyBytes(sourceCurrJustifiedCheckpoint.Root),
},
FinalizedCheckpoint: &ethpbv1.Checkpoint{
Epoch: sourceFinalizedCheckpoint.Epoch,
Root: bytesutil.SafeCopyBytes(sourceFinalizedCheckpoint.Root),
},
InactivityScores: sourceInactivityScores,
CurrentSyncCommittee: &ethpbv2.SyncCommittee{
Pubkeys: bytesutil.SafeCopy2dBytes(sourceCurrSyncCommittee.Pubkeys),
AggregatePubkey: bytesutil.SafeCopyBytes(sourceCurrSyncCommittee.AggregatePubkey),
},
NextSyncCommittee: &ethpbv2.SyncCommittee{
Pubkeys: bytesutil.SafeCopy2dBytes(sourceNextSyncCommittee.Pubkeys),
AggregatePubkey: bytesutil.SafeCopyBytes(sourceNextSyncCommittee.AggregatePubkey),
},
}
return result, nil
}