From e2834b94a0928fb034869dbaca5eca8474661d31 Mon Sep 17 00:00:00 2001 From: rkapka Date: Tue, 29 Nov 2022 18:24:23 +0100 Subject: [PATCH 01/18] proto+ssz --- proto/eth/v2/BUILD.bazel | 2 + proto/eth/v2/beacon_block.pb.go | 88 ++++++++++++++++----------------- proto/eth/v2/beacon_block.proto | 21 ++++---- proto/eth/v2/generated.ssz.go | 62 +++++++++++------------ proto/eth/v2/withdrawals.pb.go | 12 ++--- proto/eth/v2/withdrawals.proto | 2 +- 6 files changed, 94 insertions(+), 93 deletions(-) diff --git a/proto/eth/v2/BUILD.bazel b/proto/eth/v2/BUILD.bazel index 3058d8c3ad35..de4e7d242924 100644 --- a/proto/eth/v2/BUILD.bazel +++ b/proto/eth/v2/BUILD.bazel @@ -43,6 +43,8 @@ ssz_gen_marshal( "SignedBlindedBeaconBlockBellatrix", "SignedBeaconBlockCapella", "SignedBlindedBeaconBlockCapella", + "BlsToExecutionChange", + "SignedBlsToExecutionChange", ], ) diff --git a/proto/eth/v2/beacon_block.pb.go b/proto/eth/v2/beacon_block.pb.go index d10eb8a01357..4e29ca94ca61 100755 --- a/proto/eth/v2/beacon_block.pb.go +++ b/proto/eth/v2/beacon_block.pb.go @@ -707,17 +707,17 @@ func (x *SignedBeaconBlockBellatrix) GetSignature() []byte { return nil } -type SignedBlindedBeaconBlockBellatrix struct { +type SignedBeaconBlockCapella struct { state protoimpl.MessageState sizeCache protoimpl.SizeCache unknownFields protoimpl.UnknownFields - Message *BlindedBeaconBlockBellatrix `protobuf:"bytes,1,opt,name=message,proto3" json:"message,omitempty"` - Signature []byte `protobuf:"bytes,2,opt,name=signature,proto3" json:"signature,omitempty" ssz-size:"96"` + Message *BeaconBlockCapella `protobuf:"bytes,1,opt,name=message,proto3" json:"message,omitempty"` + Signature []byte `protobuf:"bytes,2,opt,name=signature,proto3" json:"signature,omitempty" ssz-size:"96"` } -func (x *SignedBlindedBeaconBlockBellatrix) Reset() { - *x = SignedBlindedBeaconBlockBellatrix{} +func (x *SignedBeaconBlockCapella) Reset() { + *x = SignedBeaconBlockCapella{} if protoimpl.UnsafeEnabled { mi := &file_proto_eth_v2_beacon_block_proto_msgTypes[8] ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) @@ -725,13 +725,13 @@ func (x *SignedBlindedBeaconBlockBellatrix) Reset() { } } -func (x *SignedBlindedBeaconBlockBellatrix) String() string { +func (x *SignedBeaconBlockCapella) String() string { return protoimpl.X.MessageStringOf(x) } -func (*SignedBlindedBeaconBlockBellatrix) ProtoMessage() {} +func (*SignedBeaconBlockCapella) ProtoMessage() {} -func (x *SignedBlindedBeaconBlockBellatrix) ProtoReflect() protoreflect.Message { +func (x *SignedBeaconBlockCapella) ProtoReflect() protoreflect.Message { mi := &file_proto_eth_v2_beacon_block_proto_msgTypes[8] if protoimpl.UnsafeEnabled && x != nil { ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) @@ -743,36 +743,36 @@ func (x *SignedBlindedBeaconBlockBellatrix) ProtoReflect() protoreflect.Message return mi.MessageOf(x) } -// Deprecated: Use SignedBlindedBeaconBlockBellatrix.ProtoReflect.Descriptor instead. -func (*SignedBlindedBeaconBlockBellatrix) Descriptor() ([]byte, []int) { +// Deprecated: Use SignedBeaconBlockCapella.ProtoReflect.Descriptor instead. +func (*SignedBeaconBlockCapella) Descriptor() ([]byte, []int) { return file_proto_eth_v2_beacon_block_proto_rawDescGZIP(), []int{8} } -func (x *SignedBlindedBeaconBlockBellatrix) GetMessage() *BlindedBeaconBlockBellatrix { +func (x *SignedBeaconBlockCapella) GetMessage() *BeaconBlockCapella { if x != nil { return x.Message } return nil } -func (x *SignedBlindedBeaconBlockBellatrix) GetSignature() []byte { +func (x *SignedBeaconBlockCapella) GetSignature() []byte { if x != nil { return x.Signature } return nil } -type SignedBeaconBlockCapella struct { +type SignedBlindedBeaconBlockBellatrix struct { state protoimpl.MessageState sizeCache protoimpl.SizeCache unknownFields protoimpl.UnknownFields - Message *BeaconBlockCapella `protobuf:"bytes,1,opt,name=message,proto3" json:"message,omitempty"` - Signature []byte `protobuf:"bytes,2,opt,name=signature,proto3" json:"signature,omitempty" ssz-size:"96"` + Message *BlindedBeaconBlockBellatrix `protobuf:"bytes,1,opt,name=message,proto3" json:"message,omitempty"` + Signature []byte `protobuf:"bytes,2,opt,name=signature,proto3" json:"signature,omitempty" ssz-size:"96"` } -func (x *SignedBeaconBlockCapella) Reset() { - *x = SignedBeaconBlockCapella{} +func (x *SignedBlindedBeaconBlockBellatrix) Reset() { + *x = SignedBlindedBeaconBlockBellatrix{} if protoimpl.UnsafeEnabled { mi := &file_proto_eth_v2_beacon_block_proto_msgTypes[9] ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) @@ -780,13 +780,13 @@ func (x *SignedBeaconBlockCapella) Reset() { } } -func (x *SignedBeaconBlockCapella) String() string { +func (x *SignedBlindedBeaconBlockBellatrix) String() string { return protoimpl.X.MessageStringOf(x) } -func (*SignedBeaconBlockCapella) ProtoMessage() {} +func (*SignedBlindedBeaconBlockBellatrix) ProtoMessage() {} -func (x *SignedBeaconBlockCapella) ProtoReflect() protoreflect.Message { +func (x *SignedBlindedBeaconBlockBellatrix) ProtoReflect() protoreflect.Message { mi := &file_proto_eth_v2_beacon_block_proto_msgTypes[9] if protoimpl.UnsafeEnabled && x != nil { ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) @@ -798,19 +798,19 @@ func (x *SignedBeaconBlockCapella) ProtoReflect() protoreflect.Message { return mi.MessageOf(x) } -// Deprecated: Use SignedBeaconBlockCapella.ProtoReflect.Descriptor instead. -func (*SignedBeaconBlockCapella) Descriptor() ([]byte, []int) { +// Deprecated: Use SignedBlindedBeaconBlockBellatrix.ProtoReflect.Descriptor instead. +func (*SignedBlindedBeaconBlockBellatrix) Descriptor() ([]byte, []int) { return file_proto_eth_v2_beacon_block_proto_rawDescGZIP(), []int{9} } -func (x *SignedBeaconBlockCapella) GetMessage() *BeaconBlockCapella { +func (x *SignedBlindedBeaconBlockBellatrix) GetMessage() *BlindedBeaconBlockBellatrix { if x != nil { return x.Message } return nil } -func (x *SignedBeaconBlockCapella) GetSignature() []byte { +func (x *SignedBlindedBeaconBlockBellatrix) GetSignature() []byte { if x != nil { return x.Signature } @@ -2070,21 +2070,21 @@ var file_proto_eth_v2_beacon_block_proto_rawDesc = []byte{ 0x74, 0x72, 0x69, 0x78, 0x52, 0x07, 0x6d, 0x65, 0x73, 0x73, 0x61, 0x67, 0x65, 0x12, 0x24, 0x0a, 0x09, 0x73, 0x69, 0x67, 0x6e, 0x61, 0x74, 0x75, 0x72, 0x65, 0x18, 0x02, 0x20, 0x01, 0x28, 0x0c, 0x42, 0x06, 0x8a, 0xb5, 0x18, 0x02, 0x39, 0x36, 0x52, 0x09, 0x73, 0x69, 0x67, 0x6e, 0x61, 0x74, - 0x75, 0x72, 0x65, 0x22, 0x91, 0x01, 0x0a, 0x21, 0x53, 0x69, 0x67, 0x6e, 0x65, 0x64, 0x42, 0x6c, + 0x75, 0x72, 0x65, 0x22, 0x7f, 0x0a, 0x18, 0x53, 0x69, 0x67, 0x6e, 0x65, 0x64, 0x42, 0x65, 0x61, + 0x63, 0x6f, 0x6e, 0x42, 0x6c, 0x6f, 0x63, 0x6b, 0x43, 0x61, 0x70, 0x65, 0x6c, 0x6c, 0x61, 0x12, + 0x3d, 0x0a, 0x07, 0x6d, 0x65, 0x73, 0x73, 0x61, 0x67, 0x65, 0x18, 0x01, 0x20, 0x01, 0x28, 0x0b, + 0x32, 0x23, 0x2e, 0x65, 0x74, 0x68, 0x65, 0x72, 0x65, 0x75, 0x6d, 0x2e, 0x65, 0x74, 0x68, 0x2e, + 0x76, 0x32, 0x2e, 0x42, 0x65, 0x61, 0x63, 0x6f, 0x6e, 0x42, 0x6c, 0x6f, 0x63, 0x6b, 0x43, 0x61, + 0x70, 0x65, 0x6c, 0x6c, 0x61, 0x52, 0x07, 0x6d, 0x65, 0x73, 0x73, 0x61, 0x67, 0x65, 0x12, 0x24, + 0x0a, 0x09, 0x73, 0x69, 0x67, 0x6e, 0x61, 0x74, 0x75, 0x72, 0x65, 0x18, 0x02, 0x20, 0x01, 0x28, + 0x0c, 0x42, 0x06, 0x8a, 0xb5, 0x18, 0x02, 0x39, 0x36, 0x52, 0x09, 0x73, 0x69, 0x67, 0x6e, 0x61, + 0x74, 0x75, 0x72, 0x65, 0x22, 0x91, 0x01, 0x0a, 0x21, 0x53, 0x69, 0x67, 0x6e, 0x65, 0x64, 0x42, + 0x6c, 0x69, 0x6e, 0x64, 0x65, 0x64, 0x42, 0x65, 0x61, 0x63, 0x6f, 0x6e, 0x42, 0x6c, 0x6f, 0x63, + 0x6b, 0x42, 0x65, 0x6c, 0x6c, 0x61, 0x74, 0x72, 0x69, 0x78, 0x12, 0x46, 0x0a, 0x07, 0x6d, 0x65, + 0x73, 0x73, 0x61, 0x67, 0x65, 0x18, 0x01, 0x20, 0x01, 0x28, 0x0b, 0x32, 0x2c, 0x2e, 0x65, 0x74, + 0x68, 0x65, 0x72, 0x65, 0x75, 0x6d, 0x2e, 0x65, 0x74, 0x68, 0x2e, 0x76, 0x32, 0x2e, 0x42, 0x6c, 0x69, 0x6e, 0x64, 0x65, 0x64, 0x42, 0x65, 0x61, 0x63, 0x6f, 0x6e, 0x42, 0x6c, 0x6f, 0x63, 0x6b, - 0x42, 0x65, 0x6c, 0x6c, 0x61, 0x74, 0x72, 0x69, 0x78, 0x12, 0x46, 0x0a, 0x07, 0x6d, 0x65, 0x73, - 0x73, 0x61, 0x67, 0x65, 0x18, 0x01, 0x20, 0x01, 0x28, 0x0b, 0x32, 0x2c, 0x2e, 0x65, 0x74, 0x68, - 0x65, 0x72, 0x65, 0x75, 0x6d, 0x2e, 0x65, 0x74, 0x68, 0x2e, 0x76, 0x32, 0x2e, 0x42, 0x6c, 0x69, - 0x6e, 0x64, 0x65, 0x64, 0x42, 0x65, 0x61, 0x63, 0x6f, 0x6e, 0x42, 0x6c, 0x6f, 0x63, 0x6b, 0x42, - 0x65, 0x6c, 0x6c, 0x61, 0x74, 0x72, 0x69, 0x78, 0x52, 0x07, 0x6d, 0x65, 0x73, 0x73, 0x61, 0x67, - 0x65, 0x12, 0x24, 0x0a, 0x09, 0x73, 0x69, 0x67, 0x6e, 0x61, 0x74, 0x75, 0x72, 0x65, 0x18, 0x02, - 0x20, 0x01, 0x28, 0x0c, 0x42, 0x06, 0x8a, 0xb5, 0x18, 0x02, 0x39, 0x36, 0x52, 0x09, 0x73, 0x69, - 0x67, 0x6e, 0x61, 0x74, 0x75, 0x72, 0x65, 0x22, 0x7f, 0x0a, 0x18, 0x53, 0x69, 0x67, 0x6e, 0x65, - 0x64, 0x42, 0x65, 0x61, 0x63, 0x6f, 0x6e, 0x42, 0x6c, 0x6f, 0x63, 0x6b, 0x43, 0x61, 0x70, 0x65, - 0x6c, 0x6c, 0x61, 0x12, 0x3d, 0x0a, 0x07, 0x6d, 0x65, 0x73, 0x73, 0x61, 0x67, 0x65, 0x18, 0x01, - 0x20, 0x01, 0x28, 0x0b, 0x32, 0x23, 0x2e, 0x65, 0x74, 0x68, 0x65, 0x72, 0x65, 0x75, 0x6d, 0x2e, - 0x65, 0x74, 0x68, 0x2e, 0x76, 0x32, 0x2e, 0x42, 0x65, 0x61, 0x63, 0x6f, 0x6e, 0x42, 0x6c, 0x6f, - 0x63, 0x6b, 0x43, 0x61, 0x70, 0x65, 0x6c, 0x6c, 0x61, 0x52, 0x07, 0x6d, 0x65, 0x73, 0x73, 0x61, + 0x42, 0x65, 0x6c, 0x6c, 0x61, 0x74, 0x72, 0x69, 0x78, 0x52, 0x07, 0x6d, 0x65, 0x73, 0x73, 0x61, 0x67, 0x65, 0x12, 0x24, 0x0a, 0x09, 0x73, 0x69, 0x67, 0x6e, 0x61, 0x74, 0x75, 0x72, 0x65, 0x18, 0x02, 0x20, 0x01, 0x28, 0x0c, 0x42, 0x06, 0x8a, 0xb5, 0x18, 0x02, 0x39, 0x36, 0x52, 0x09, 0x73, 0x69, 0x67, 0x6e, 0x61, 0x74, 0x75, 0x72, 0x65, 0x22, 0x8d, 0x01, 0x0a, 0x1f, 0x53, 0x69, 0x67, @@ -2495,8 +2495,8 @@ var file_proto_eth_v2_beacon_block_proto_goTypes = []interface{}{ (*BlindedBeaconBlockContainer)(nil), // 5: ethereum.eth.v2.BlindedBeaconBlockContainer (*SignedBlindedBeaconBlockContainer)(nil), // 6: ethereum.eth.v2.SignedBlindedBeaconBlockContainer (*SignedBeaconBlockBellatrix)(nil), // 7: ethereum.eth.v2.SignedBeaconBlockBellatrix - (*SignedBlindedBeaconBlockBellatrix)(nil), // 8: ethereum.eth.v2.SignedBlindedBeaconBlockBellatrix - (*SignedBeaconBlockCapella)(nil), // 9: ethereum.eth.v2.SignedBeaconBlockCapella + (*SignedBeaconBlockCapella)(nil), // 8: ethereum.eth.v2.SignedBeaconBlockCapella + (*SignedBlindedBeaconBlockBellatrix)(nil), // 9: ethereum.eth.v2.SignedBlindedBeaconBlockBellatrix (*SignedBlindedBeaconBlockCapella)(nil), // 10: ethereum.eth.v2.SignedBlindedBeaconBlockCapella (*SignedBeaconBlockAltair)(nil), // 11: ethereum.eth.v2.SignedBeaconBlockAltair (*BeaconBlockBellatrix)(nil), // 12: ethereum.eth.v2.BeaconBlockBellatrix @@ -2546,8 +2546,8 @@ var file_proto_eth_v2_beacon_block_proto_depIdxs = []int32{ 13, // 18: ethereum.eth.v2.SignedBlindedBeaconBlockContainer.bellatrix_block:type_name -> ethereum.eth.v2.BlindedBeaconBlockBellatrix 15, // 19: ethereum.eth.v2.SignedBlindedBeaconBlockContainer.capella_block:type_name -> ethereum.eth.v2.BlindedBeaconBlockCapella 12, // 20: ethereum.eth.v2.SignedBeaconBlockBellatrix.message:type_name -> ethereum.eth.v2.BeaconBlockBellatrix - 13, // 21: ethereum.eth.v2.SignedBlindedBeaconBlockBellatrix.message:type_name -> ethereum.eth.v2.BlindedBeaconBlockBellatrix - 14, // 22: ethereum.eth.v2.SignedBeaconBlockCapella.message:type_name -> ethereum.eth.v2.BeaconBlockCapella + 14, // 21: ethereum.eth.v2.SignedBeaconBlockCapella.message:type_name -> ethereum.eth.v2.BeaconBlockCapella + 13, // 22: ethereum.eth.v2.SignedBlindedBeaconBlockBellatrix.message:type_name -> ethereum.eth.v2.BlindedBeaconBlockBellatrix 15, // 23: ethereum.eth.v2.SignedBlindedBeaconBlockCapella.message:type_name -> ethereum.eth.v2.BlindedBeaconBlockCapella 16, // 24: ethereum.eth.v2.SignedBeaconBlockAltair.message:type_name -> ethereum.eth.v2.BeaconBlockAltair 17, // 25: ethereum.eth.v2.BeaconBlockBellatrix.body:type_name -> ethereum.eth.v2.BeaconBlockBodyBellatrix @@ -2708,7 +2708,7 @@ func file_proto_eth_v2_beacon_block_proto_init() { } } file_proto_eth_v2_beacon_block_proto_msgTypes[8].Exporter = func(v interface{}, i int) interface{} { - switch v := v.(*SignedBlindedBeaconBlockBellatrix); i { + switch v := v.(*SignedBeaconBlockCapella); i { case 0: return &v.state case 1: @@ -2720,7 +2720,7 @@ func file_proto_eth_v2_beacon_block_proto_init() { } } file_proto_eth_v2_beacon_block_proto_msgTypes[9].Exporter = func(v interface{}, i int) interface{} { - switch v := v.(*SignedBeaconBlockCapella); i { + switch v := v.(*SignedBlindedBeaconBlockBellatrix); i { case 0: return &v.state case 1: diff --git a/proto/eth/v2/beacon_block.proto b/proto/eth/v2/beacon_block.proto index 702c64e170f4..d188d97ffd84 100644 --- a/proto/eth/v2/beacon_block.proto +++ b/proto/eth/v2/beacon_block.proto @@ -96,15 +96,15 @@ message SignedBeaconBlockBellatrix { bytes signature = 2 [(ethereum.eth.ext.ssz_size) = "96"]; } -message SignedBlindedBeaconBlockBellatrix { - BlindedBeaconBlockBellatrix message = 1; +message SignedBeaconBlockCapella { + BeaconBlockCapella message = 1; // 96 byte BLS signature from the validator that produced this block. bytes signature = 2 [(ethereum.eth.ext.ssz_size) = "96"]; } -message SignedBeaconBlockCapella { - BeaconBlockCapella message = 1; +message SignedBlindedBeaconBlockBellatrix { + BlindedBeaconBlockBellatrix message = 1; // 96 byte BLS signature from the validator that produced this block. bytes signature = 2 [(ethereum.eth.ext.ssz_size) = "96"]; @@ -318,14 +318,13 @@ message BeaconBlockBodyCapella { // Sync aggregate object to track sync committee votes for light client support. v1.SyncAggregate sync_aggregate = 9; - // Execution payload: the embedded execution payload of the block [New in Bellatrix] + // Execution payload: the embedded execution payload of the block [Modified in Capella] ethereum.engine.v1.ExecutionPayloadCapella execution_payload = 10; - // At most MAX_BLS_TO_EXECUTION_CHANGES. [New in Capella] - repeated v2.SignedBLSToExecutionChange bls_to_execution_changes = 11 [(ethereum.eth.ext.ssz_max) = "16"]; + // BLS To Execution Changes: signed messages to change withdrawal credentials [New in Capella] + repeated SignedBLSToExecutionChange bls_to_execution_changes = 11 [(ethereum.eth.ext.ssz_max) = "16"]; } - message BlindedBeaconBlockBodyCapella { // The validators RANDAO reveal 96 byte value. bytes randao_reveal = 1 [(ethereum.eth.ext.ssz_size) = "96"]; @@ -358,11 +357,11 @@ message BlindedBeaconBlockBodyCapella { // Sync aggregate object to track sync committee votes for light client support. v1.SyncAggregate sync_aggregate = 9; - // Execution payload header: the embedded execution payload of the block [New in Bellatrix] + // Execution payload header: the embedded execution payload of the block [Modified in Capella] ethereum.engine.v1.ExecutionPayloadHeaderCapella execution_payload_header = 10; - // At most MAX_BLS_TO_EXECUTION_CHANGES. [New in Capella] - repeated v2.SignedBLSToExecutionChange bls_to_execution_changes = 11 [(ethereum.eth.ext.ssz_max) = "16"]; + // BLS To Execution Changes: signed messages to change withdrawal credentials [New in Capella] + repeated SignedBLSToExecutionChange bls_to_execution_changes = 11 [(ethereum.eth.ext.ssz_max) = "16"]; } message BeaconBlockBodyAltair { diff --git a/proto/eth/v2/generated.ssz.go b/proto/eth/v2/generated.ssz.go index 79d8270d5916..f72c63c90b1e 100644 --- a/proto/eth/v2/generated.ssz.go +++ b/proto/eth/v2/generated.ssz.go @@ -1,5 +1,5 @@ // Code generated by fastssz. DO NOT EDIT. -// Hash: 34f7923ca561f38dca1547740692d9db35da6b8c4cb074d5c68d7fbf7c2681b2 +// Hash: b0e2b862b471992f748a849f7e9246f72d77985e5d7e83fc704efd1aa1eeb9fa package eth import ( @@ -122,20 +122,20 @@ func (s *SignedBeaconBlockBellatrix) HashTreeRootWith(hh *ssz.Hasher) (err error return } -// MarshalSSZ ssz marshals the SignedBlindedBeaconBlockBellatrix object -func (s *SignedBlindedBeaconBlockBellatrix) MarshalSSZ() ([]byte, error) { +// MarshalSSZ ssz marshals the SignedBeaconBlockCapella object +func (s *SignedBeaconBlockCapella) MarshalSSZ() ([]byte, error) { return ssz.MarshalSSZ(s) } -// MarshalSSZTo ssz marshals the SignedBlindedBeaconBlockBellatrix object to a target array -func (s *SignedBlindedBeaconBlockBellatrix) MarshalSSZTo(buf []byte) (dst []byte, err error) { +// MarshalSSZTo ssz marshals the SignedBeaconBlockCapella object to a target array +func (s *SignedBeaconBlockCapella) MarshalSSZTo(buf []byte) (dst []byte, err error) { dst = buf offset := int(100) // Offset (0) 'Message' dst = ssz.WriteOffset(dst, offset) if s.Message == nil { - s.Message = new(BlindedBeaconBlockBellatrix) + s.Message = new(BeaconBlockCapella) } offset += s.Message.SizeSSZ() @@ -154,8 +154,8 @@ func (s *SignedBlindedBeaconBlockBellatrix) MarshalSSZTo(buf []byte) (dst []byte return } -// UnmarshalSSZ ssz unmarshals the SignedBlindedBeaconBlockBellatrix object -func (s *SignedBlindedBeaconBlockBellatrix) UnmarshalSSZ(buf []byte) error { +// UnmarshalSSZ ssz unmarshals the SignedBeaconBlockCapella object +func (s *SignedBeaconBlockCapella) UnmarshalSSZ(buf []byte) error { var err error size := uint64(len(buf)) if size < 100 { @@ -184,7 +184,7 @@ func (s *SignedBlindedBeaconBlockBellatrix) UnmarshalSSZ(buf []byte) error { { buf = tail[o0:] if s.Message == nil { - s.Message = new(BlindedBeaconBlockBellatrix) + s.Message = new(BeaconBlockCapella) } if err = s.Message.UnmarshalSSZ(buf); err != nil { return err @@ -193,26 +193,26 @@ func (s *SignedBlindedBeaconBlockBellatrix) UnmarshalSSZ(buf []byte) error { return err } -// SizeSSZ returns the ssz encoded size in bytes for the SignedBlindedBeaconBlockBellatrix object -func (s *SignedBlindedBeaconBlockBellatrix) SizeSSZ() (size int) { +// SizeSSZ returns the ssz encoded size in bytes for the SignedBeaconBlockCapella object +func (s *SignedBeaconBlockCapella) SizeSSZ() (size int) { size = 100 // Field (0) 'Message' if s.Message == nil { - s.Message = new(BlindedBeaconBlockBellatrix) + s.Message = new(BeaconBlockCapella) } size += s.Message.SizeSSZ() return } -// HashTreeRoot ssz hashes the SignedBlindedBeaconBlockBellatrix object -func (s *SignedBlindedBeaconBlockBellatrix) HashTreeRoot() ([32]byte, error) { +// HashTreeRoot ssz hashes the SignedBeaconBlockCapella object +func (s *SignedBeaconBlockCapella) HashTreeRoot() ([32]byte, error) { return ssz.HashWithDefaultHasher(s) } -// HashTreeRootWith ssz hashes the SignedBlindedBeaconBlockBellatrix object with a hasher -func (s *SignedBlindedBeaconBlockBellatrix) HashTreeRootWith(hh *ssz.Hasher) (err error) { +// HashTreeRootWith ssz hashes the SignedBeaconBlockCapella object with a hasher +func (s *SignedBeaconBlockCapella) HashTreeRootWith(hh *ssz.Hasher) (err error) { indx := hh.Index() // Field (0) 'Message' @@ -235,20 +235,20 @@ func (s *SignedBlindedBeaconBlockBellatrix) HashTreeRootWith(hh *ssz.Hasher) (er return } -// MarshalSSZ ssz marshals the SignedBeaconBlockCapella object -func (s *SignedBeaconBlockCapella) MarshalSSZ() ([]byte, error) { +// MarshalSSZ ssz marshals the SignedBlindedBeaconBlockBellatrix object +func (s *SignedBlindedBeaconBlockBellatrix) MarshalSSZ() ([]byte, error) { return ssz.MarshalSSZ(s) } -// MarshalSSZTo ssz marshals the SignedBeaconBlockCapella object to a target array -func (s *SignedBeaconBlockCapella) MarshalSSZTo(buf []byte) (dst []byte, err error) { +// MarshalSSZTo ssz marshals the SignedBlindedBeaconBlockBellatrix object to a target array +func (s *SignedBlindedBeaconBlockBellatrix) MarshalSSZTo(buf []byte) (dst []byte, err error) { dst = buf offset := int(100) // Offset (0) 'Message' dst = ssz.WriteOffset(dst, offset) if s.Message == nil { - s.Message = new(BeaconBlockCapella) + s.Message = new(BlindedBeaconBlockBellatrix) } offset += s.Message.SizeSSZ() @@ -267,8 +267,8 @@ func (s *SignedBeaconBlockCapella) MarshalSSZTo(buf []byte) (dst []byte, err err return } -// UnmarshalSSZ ssz unmarshals the SignedBeaconBlockCapella object -func (s *SignedBeaconBlockCapella) UnmarshalSSZ(buf []byte) error { +// UnmarshalSSZ ssz unmarshals the SignedBlindedBeaconBlockBellatrix object +func (s *SignedBlindedBeaconBlockBellatrix) UnmarshalSSZ(buf []byte) error { var err error size := uint64(len(buf)) if size < 100 { @@ -297,7 +297,7 @@ func (s *SignedBeaconBlockCapella) UnmarshalSSZ(buf []byte) error { { buf = tail[o0:] if s.Message == nil { - s.Message = new(BeaconBlockCapella) + s.Message = new(BlindedBeaconBlockBellatrix) } if err = s.Message.UnmarshalSSZ(buf); err != nil { return err @@ -306,26 +306,26 @@ func (s *SignedBeaconBlockCapella) UnmarshalSSZ(buf []byte) error { return err } -// SizeSSZ returns the ssz encoded size in bytes for the SignedBeaconBlockCapella object -func (s *SignedBeaconBlockCapella) SizeSSZ() (size int) { +// SizeSSZ returns the ssz encoded size in bytes for the SignedBlindedBeaconBlockBellatrix object +func (s *SignedBlindedBeaconBlockBellatrix) SizeSSZ() (size int) { size = 100 // Field (0) 'Message' if s.Message == nil { - s.Message = new(BeaconBlockCapella) + s.Message = new(BlindedBeaconBlockBellatrix) } size += s.Message.SizeSSZ() return } -// HashTreeRoot ssz hashes the SignedBeaconBlockCapella object -func (s *SignedBeaconBlockCapella) HashTreeRoot() ([32]byte, error) { +// HashTreeRoot ssz hashes the SignedBlindedBeaconBlockBellatrix object +func (s *SignedBlindedBeaconBlockBellatrix) HashTreeRoot() ([32]byte, error) { return ssz.HashWithDefaultHasher(s) } -// HashTreeRootWith ssz hashes the SignedBeaconBlockCapella object with a hasher -func (s *SignedBeaconBlockCapella) HashTreeRootWith(hh *ssz.Hasher) (err error) { +// HashTreeRootWith ssz hashes the SignedBlindedBeaconBlockBellatrix object with a hasher +func (s *SignedBlindedBeaconBlockBellatrix) HashTreeRootWith(hh *ssz.Hasher) (err error) { indx := hh.Index() // Field (0) 'Message' diff --git a/proto/eth/v2/withdrawals.pb.go b/proto/eth/v2/withdrawals.pb.go index 230e47eb72f2..8301f57db8b2 100755 --- a/proto/eth/v2/withdrawals.pb.go +++ b/proto/eth/v2/withdrawals.pb.go @@ -173,15 +173,15 @@ var file_proto_eth_v2_withdrawals_proto_rawDesc = []byte{ 0x67, 0x65, 0x52, 0x07, 0x6d, 0x65, 0x73, 0x73, 0x61, 0x67, 0x65, 0x12, 0x24, 0x0a, 0x09, 0x73, 0x69, 0x67, 0x6e, 0x61, 0x74, 0x75, 0x72, 0x65, 0x18, 0x02, 0x20, 0x01, 0x28, 0x0c, 0x42, 0x06, 0x8a, 0xb5, 0x18, 0x02, 0x39, 0x36, 0x52, 0x09, 0x73, 0x69, 0x67, 0x6e, 0x61, 0x74, 0x75, 0x72, - 0x65, 0x42, 0x83, 0x01, 0x0a, 0x13, 0x6f, 0x72, 0x67, 0x2e, 0x65, 0x74, 0x68, 0x65, 0x72, 0x65, + 0x65, 0x42, 0x81, 0x01, 0x0a, 0x13, 0x6f, 0x72, 0x67, 0x2e, 0x65, 0x74, 0x68, 0x65, 0x72, 0x65, 0x75, 0x6d, 0x2e, 0x65, 0x74, 0x68, 0x2e, 0x76, 0x32, 0x42, 0x10, 0x57, 0x69, 0x74, 0x68, 0x64, - 0x72, 0x61, 0x77, 0x61, 0x6c, 0x73, 0x50, 0x72, 0x6f, 0x74, 0x6f, 0x50, 0x01, 0x5a, 0x34, 0x67, + 0x72, 0x61, 0x77, 0x61, 0x6c, 0x73, 0x50, 0x72, 0x6f, 0x74, 0x6f, 0x50, 0x01, 0x5a, 0x32, 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, 0x76, 0x33, - 0x2f, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x2f, 0x70, 0x72, 0x79, 0x73, 0x6d, 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, + 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 ( diff --git a/proto/eth/v2/withdrawals.proto b/proto/eth/v2/withdrawals.proto index e850a7237b77..44e8dcff792f 100644 --- a/proto/eth/v2/withdrawals.proto +++ b/proto/eth/v2/withdrawals.proto @@ -18,7 +18,7 @@ package ethereum.eth.v2; import "proto/eth/ext/options.proto"; option csharp_namespace = "Ethereum.Eth.V2"; -option go_package = "github.com/prysmaticlabs/prysm/v3/proto/prysm/v2;eth"; +option go_package = "github.com/prysmaticlabs/prysm/v3/proto/eth/v2;eth"; option java_multiple_files = true; option java_outer_classname = "WithdrawalsProto"; option java_package = "org.ethereum.eth.v2"; From 2a43aa111a9323e2a8598a1d913c71289a24565f Mon Sep 17 00:00:00 2001 From: rkapka Date: Fri, 25 Nov 2022 16:51:03 +0100 Subject: [PATCH 02/18] refactor GetBlindedBlockSSZ (cherry picked from commit 97483c339f99b0d96bd81846a979383ffd2b0cda) # Conflicts: # beacon-chain/rpc/eth/beacon/blocks.go (cherry picked from commit 9e4e82d2c55e09be7568b28eaa33cdd1141445f4) # Conflicts: # beacon-chain/rpc/eth/beacon/blocks.go --- beacon-chain/rpc/eth/beacon/blinded_blocks.go | 157 ++++++++++++++++++ .../rpc/eth/beacon/blinded_blocks_test.go | 115 +++++++++++++ beacon-chain/rpc/eth/beacon/blocks.go | 136 --------------- beacon-chain/rpc/eth/beacon/blocks_test.go | 115 ------------- 4 files changed, 272 insertions(+), 251 deletions(-) diff --git a/beacon-chain/rpc/eth/beacon/blinded_blocks.go b/beacon-chain/rpc/eth/beacon/blinded_blocks.go index 5898b09b7482..11ab37f0572f 100644 --- a/beacon-chain/rpc/eth/beacon/blinded_blocks.go +++ b/beacon-chain/rpc/eth/beacon/blinded_blocks.go @@ -61,6 +61,45 @@ func (bs *Server) GetBlindedBlock(ctx context.Context, req *ethpbv1.BlockRequest return nil, status.Errorf(codes.Internal, "Unknown block type %T", blk) } +// GetBlindedBlockSSZ returns the SSZ-serialized version of the blinded beacon block for given block id. +func (bs *Server) GetBlindedBlockSSZ(ctx context.Context, req *ethpbv1.BlockRequest) (*ethpbv2.SSZContainer, error) { + ctx, span := trace.StartSpan(ctx, "beacon.GetBlindedBlockSSZ") + defer span.End() + + blk, err := bs.blockFromBlockID(ctx, req.BlockId) + err = handleGetBlockError(blk, err) + if err != nil { + return nil, err + } + + result, err := getBlindedSSZBlockPhase0(blk) + if result != nil { + return result, nil + } + // ErrUnsupportedGetter means that we have another block type + if !errors.Is(err, blocks.ErrUnsupportedGetter) { + return nil, status.Errorf(codes.Internal, "Could not get signed beacon block: %v", err) + } + result, err = getBlindedSSZBlockAltair(blk) + if result != nil { + return result, nil + } + // ErrUnsupportedGetter means that we have another block type + if !errors.Is(err, blocks.ErrUnsupportedGetter) { + return nil, status.Errorf(codes.Internal, "Could not get signed beacon block: %v", err) + } + result, err = bs.getBlindedSSZBlockBellatrix(ctx, blk) + if result != nil { + return result, nil + } + // ErrUnsupportedGetter means that we have another block type + if !errors.Is(err, blocks.ErrUnsupportedGetter) { + return nil, status.Errorf(codes.Internal, "Could not get signed beacon block: %v", err) + } + + return nil, status.Errorf(codes.Internal, "Unknown block type %T", blk) +} + func getBlindedBlockPhase0(blk interfaces.SignedBeaconBlock) (*ethpbv2.BlindedBlockResponse, error) { phase0Blk, err := blk.PbPhase0Block() if err != nil { @@ -244,3 +283,121 @@ func (bs *Server) getBlindedBlockCapella(ctx context.Context, blk interfaces.Sig ExecutionOptimistic: isOptimistic, }, nil } + +func getBlindedSSZBlockPhase0(blk interfaces.SignedBeaconBlock) (*ethpbv2.SSZContainer, error) { + phase0Blk, err := blk.PbPhase0Block() + if err != nil { + return nil, err + } + if phase0Blk == nil { + return nil, errNilBlock + } + signedBeaconBlock, err := migration.SignedBeaconBlock(blk) + if err != nil { + return nil, errors.Wrapf(err, "could not get signed beacon block") + } + sszBlock, err := signedBeaconBlock.MarshalSSZ() + if err != nil { + return nil, errors.Wrapf(err, "could not marshal block into SSZ") + } + return ðpbv2.SSZContainer{Version: ethpbv2.Version_PHASE0, ExecutionOptimistic: false, Data: sszBlock}, nil +} + +func getBlindedSSZBlockAltair(blk interfaces.SignedBeaconBlock) (*ethpbv2.SSZContainer, error) { + altairBlk, err := blk.PbAltairBlock() + if err != nil { + return nil, err + } + if altairBlk == nil { + return nil, errNilBlock + } + v2Blk, err := migration.V1Alpha1BeaconBlockAltairToV2(altairBlk.Block) + if err != nil { + return nil, errors.Wrapf(err, "could not get signed beacon block") + } + sig := blk.Signature() + data := ðpbv2.SignedBeaconBlockAltair{ + Message: v2Blk, + Signature: sig[:], + } + sszData, err := data.MarshalSSZ() + if err != nil { + return nil, errors.Wrapf(err, "could not marshal block into SSZ") + } + return ðpbv2.SSZContainer{Version: ethpbv2.Version_ALTAIR, ExecutionOptimistic: false, Data: sszData}, nil +} + +func (bs *Server) getBlindedSSZBlockBellatrix(ctx context.Context, blk interfaces.SignedBeaconBlock) (*ethpbv2.SSZContainer, error) { + bellatrixBlk, err := blk.PbBellatrixBlock() + if err != nil { + // ErrUnsupportedGetter means that we have another block type + if errors.Is(err, blocks.ErrUnsupportedGetter) { + if blindedBellatrixBlk, err := blk.PbBlindedBellatrixBlock(); err == nil { + if blindedBellatrixBlk == nil { + return nil, errNilBlock + } + v2Blk, err := migration.V1Alpha1BeaconBlockBlindedBellatrixToV2Blinded(blindedBellatrixBlk.Block) + if err != nil { + return nil, errors.Wrapf(err, "could not get signed beacon block") + } + root, err := blk.Block().HashTreeRoot() + if err != nil { + return nil, errors.Wrapf(err, "could not get block root") + } + isOptimistic, err := bs.OptimisticModeFetcher.IsOptimisticForRoot(ctx, root) + if err != nil { + return nil, errors.Wrapf(err, "could not check if block is optimistic") + } + sig := blk.Signature() + data := ðpbv2.SignedBlindedBeaconBlockBellatrix{ + Message: v2Blk, + Signature: sig[:], + } + sszData, err := data.MarshalSSZ() + if err != nil { + return nil, errors.Wrapf(err, "could not marshal block into SSZ") + } + return ðpbv2.SSZContainer{ + Version: ethpbv2.Version_BELLATRIX, + ExecutionOptimistic: isOptimistic, + Data: sszData, + }, nil + } + return nil, err + } + } + + if bellatrixBlk == nil { + return nil, errNilBlock + } + blindedBlkInterface, err := blk.ToBlinded() + if err != nil { + return nil, errors.Wrapf(err, "could not convert block to blinded block") + } + blindedBellatrixBlock, err := blindedBlkInterface.PbBlindedBellatrixBlock() + if err != nil { + return nil, errors.Wrapf(err, "could not get signed beacon block") + } + v2Blk, err := migration.V1Alpha1BeaconBlockBlindedBellatrixToV2Blinded(blindedBellatrixBlock.Block) + if err != nil { + return nil, errors.Wrapf(err, "could not get signed beacon block") + } + root, err := blk.Block().HashTreeRoot() + if err != nil { + return nil, errors.Wrapf(err, "could not get block root") + } + isOptimistic, err := bs.OptimisticModeFetcher.IsOptimisticForRoot(ctx, root) + if err != nil { + return nil, errors.Wrapf(err, "could not check if block is optimistic") + } + sig := blk.Signature() + data := ðpbv2.SignedBlindedBeaconBlockBellatrix{ + Message: v2Blk, + Signature: sig[:], + } + sszData, err := data.MarshalSSZ() + if err != nil { + return nil, errors.Wrapf(err, "could not marshal block into SSZ") + } + return ðpbv2.SSZContainer{Version: ethpbv2.Version_BELLATRIX, ExecutionOptimistic: isOptimistic, Data: sszData}, nil +} diff --git a/beacon-chain/rpc/eth/beacon/blinded_blocks_test.go b/beacon-chain/rpc/eth/beacon/blinded_blocks_test.go index 0b0333d7fb24..dfc03ff923f9 100644 --- a/beacon-chain/rpc/eth/beacon/blinded_blocks_test.go +++ b/beacon-chain/rpc/eth/beacon/blinded_blocks_test.go @@ -592,3 +592,118 @@ func TestServer_GetBlindedBlock(t *testing.T) { assert.Equal(t, true, blk.ExecutionOptimistic) }) } + +func TestServer_GetBlindedBlockSSZ(t *testing.T) { + t.Run("Phase 0", func(t *testing.T) { + beaconDB := dbTest.SetupDB(t) + ctx := context.Background() + + _, blkContainers := fillDBTestBlocks(ctx, t, beaconDB) + headBlock := blkContainers[len(blkContainers)-1] + + b2 := util.NewBeaconBlock() + b2.Block.Slot = 30 + b2.Block.ParentRoot = bytesutil.PadTo([]byte{1}, 32) + util.SaveBlock(t, ctx, beaconDB, b2) + + wsb, err := blocks.NewSignedBeaconBlock(headBlock.Block.(*ethpbalpha.BeaconBlockContainer_Phase0Block).Phase0Block) + require.NoError(t, err) + + bs := &Server{ + BeaconDB: beaconDB, + ChainInfoFetcher: &mock.ChainService{ + DB: beaconDB, + Block: wsb, + Root: headBlock.BlockRoot, + FinalizedCheckPoint: ðpbalpha.Checkpoint{Root: blkContainers[64].BlockRoot}, + }, + } + + blks, err := beaconDB.BlocksBySlot(ctx, 30) + require.Equal(t, true, len(blks) > 0) + require.NoError(t, err) + sszBlock, err := blks[0].MarshalSSZ() + require.NoError(t, err) + + resp, err := bs.GetBlindedBlockSSZ(ctx, ðpbv1.BlockRequest{BlockId: []byte("30")}) + require.NoError(t, err) + assert.NotNil(t, resp) + assert.DeepEqual(t, sszBlock, resp.Data) + assert.Equal(t, ethpbv2.Version_PHASE0, resp.Version) + }) + + t.Run("Altair", func(t *testing.T) { + beaconDB := dbTest.SetupDB(t) + ctx := context.Background() + + _, blkContainers := fillDBTestBlocksAltair(ctx, t, beaconDB) + headBlock := blkContainers[len(blkContainers)-1] + + b2 := util.NewBeaconBlockAltair() + b2.Block.Slot = 30 + b2.Block.ParentRoot = bytesutil.PadTo([]byte{1}, 32) + util.SaveBlock(t, ctx, beaconDB, b2) + + chainBlk, err := blocks.NewSignedBeaconBlock(headBlock.GetAltairBlock()) + require.NoError(t, err) + bs := &Server{ + BeaconDB: beaconDB, + ChainInfoFetcher: &mock.ChainService{ + DB: beaconDB, + Block: chainBlk, + Root: headBlock.BlockRoot, + FinalizedCheckPoint: ðpbalpha.Checkpoint{Root: blkContainers[64].BlockRoot}, + }, + } + + blks, err := beaconDB.BlocksBySlot(ctx, 30) + require.Equal(t, true, len(blks) > 0) + require.NoError(t, err) + sszBlock, err := blks[0].MarshalSSZ() + require.NoError(t, err) + + resp, err := bs.GetBlindedBlockSSZ(ctx, ðpbv1.BlockRequest{BlockId: []byte("30")}) + require.NoError(t, err) + assert.NotNil(t, resp) + assert.DeepEqual(t, sszBlock, resp.Data) + assert.Equal(t, ethpbv2.Version_ALTAIR, resp.Version) + }) + + t.Run("Bellatrix", func(t *testing.T) { + beaconDB := dbTest.SetupDB(t) + ctx := context.Background() + + _, blkContainers := fillDBTestBlocksBellatrixBlinded(ctx, t, beaconDB) + headBlock := blkContainers[len(blkContainers)-1] + + b2 := util.NewBlindedBeaconBlockBellatrix() + b2.Block.Slot = 30 + b2.Block.ParentRoot = bytesutil.PadTo([]byte{1}, 32) + util.SaveBlock(t, ctx, beaconDB, b2) + + chainBlk, err := blocks.NewSignedBeaconBlock(headBlock.GetBlindedBellatrixBlock()) + require.NoError(t, err) + bs := &Server{ + BeaconDB: beaconDB, + ChainInfoFetcher: &mock.ChainService{ + DB: beaconDB, + Block: chainBlk, + Root: headBlock.BlockRoot, + FinalizedCheckPoint: ðpbalpha.Checkpoint{Root: blkContainers[64].BlockRoot}, + }, + OptimisticModeFetcher: &mock.ChainService{}, + } + + blks, err := beaconDB.BlocksBySlot(ctx, 30) + require.Equal(t, true, len(blks) > 0) + require.NoError(t, err) + sszBlock, err := blks[0].MarshalSSZ() + require.NoError(t, err) + + resp, err := bs.GetBlindedBlockSSZ(ctx, ðpbv1.BlockRequest{BlockId: []byte("30")}) + require.NoError(t, err) + assert.NotNil(t, resp) + assert.DeepEqual(t, sszBlock, resp.Data) + assert.Equal(t, ethpbv2.Version_BELLATRIX, resp.Version) + }) +} diff --git a/beacon-chain/rpc/eth/beacon/blocks.go b/beacon-chain/rpc/eth/beacon/blocks.go index 33056a968c4d..f4ac2727e614 100644 --- a/beacon-chain/rpc/eth/beacon/blocks.go +++ b/beacon-chain/rpc/eth/beacon/blocks.go @@ -688,142 +688,6 @@ func (bs *Server) GetBlockSSZV2(ctx context.Context, req *ethpbv2.BlockRequestV2 return nil, status.Errorf(codes.Internal, "Unknown block type %T", blk) } -// GetBlindedBlockSSZ returns the SSZ-serialized version of the blinded beacon block for given block id. -func (bs *Server) GetBlindedBlockSSZ(ctx context.Context, req *ethpbv1.BlockRequest) (*ethpbv2.SSZContainer, error) { - ctx, span := trace.StartSpan(ctx, "beacon.GetBlindedBlockSSZ") - defer span.End() - - blk, err := bs.blockFromBlockID(ctx, req.BlockId) - err = handleGetBlockError(blk, err) - if err != nil { - return nil, err - } - - phase0Blk, err := blk.PbPhase0Block() - if err == nil { - if phase0Blk == nil { - return nil, status.Error(codes.Internal, "Nil block") - } - signedBeaconBlock, err := migration.SignedBeaconBlock(blk) - if err != nil { - return nil, status.Errorf(codes.Internal, "Could not get signed beacon block: %v", err) - } - sszBlock, err := signedBeaconBlock.MarshalSSZ() - if err != nil { - return nil, status.Errorf(codes.Internal, "Could not marshal block into SSZ: %v", err) - } - return ðpbv2.SSZContainer{Version: ethpbv2.Version_PHASE0, ExecutionOptimistic: false, Data: sszBlock}, nil - } - // ErrUnsupportedGetter means that we have another block type - if !errors.Is(err, blocks.ErrUnsupportedGetter) { - return nil, status.Errorf(codes.Internal, "Could not get signed beacon block: %v", err) - } - - altairBlk, err := blk.PbAltairBlock() - if err == nil { - if altairBlk == nil { - return nil, status.Error(codes.Internal, "Nil block") - } - v2Blk, err := migration.V1Alpha1BeaconBlockAltairToV2(altairBlk.Block) - if err != nil { - return nil, status.Errorf(codes.Internal, "Could not get signed beacon block: %v", err) - } - sig := blk.Signature() - data := ðpbv2.SignedBeaconBlockAltair{ - Message: v2Blk, - Signature: sig[:], - } - sszData, err := data.MarshalSSZ() - if err != nil { - return nil, status.Errorf(codes.Internal, "Could not marshal block into SSZ: %v", err) - } - return ðpbv2.SSZContainer{Version: ethpbv2.Version_ALTAIR, ExecutionOptimistic: false, Data: sszData}, nil - } - // ErrUnsupportedGetter means that we have another block type - if !errors.Is(err, blocks.ErrUnsupportedGetter) { - return nil, status.Errorf(codes.Internal, "Could not get signed beacon block: %v", err) - } - - bellatrixBlk, err := blk.PbBellatrixBlock() - if err == nil { - if bellatrixBlk == nil { - return nil, status.Error(codes.Internal, "Nil block") - } - blindedBlkInterface, err := blk.ToBlinded() - if err != nil { - return nil, status.Errorf(codes.Internal, "Could not convert block to blinded block: %v", err) - } - blindedBellatrixBlock, err := blindedBlkInterface.PbBlindedBellatrixBlock() - if err != nil { - return nil, status.Errorf(codes.Internal, "Could not get signed beacon block: %v", err) - } - v2Blk, err := migration.V1Alpha1BeaconBlockBlindedBellatrixToV2Blinded(blindedBellatrixBlock.Block) - if err != nil { - return nil, status.Errorf(codes.Internal, "Could not get signed beacon block: %v", err) - } - root, err := blk.Block().HashTreeRoot() - if err != nil { - return nil, status.Errorf(codes.Internal, "Could not get block root: %v", err) - } - isOptimistic, err := bs.OptimisticModeFetcher.IsOptimisticForRoot(ctx, root) - if err != nil { - return nil, status.Errorf(codes.Internal, "Could not check if block is optimistic: %v", err) - } - sig := blk.Signature() - data := ðpbv2.SignedBlindedBeaconBlockBellatrix{ - Message: v2Blk, - Signature: sig[:], - } - sszData, err := data.MarshalSSZ() - if err != nil { - return nil, status.Errorf(codes.Internal, "Could not marshal block into SSZ: %v", err) - } - return ðpbv2.SSZContainer{Version: ethpbv2.Version_BELLATRIX, ExecutionOptimistic: isOptimistic, Data: sszData}, nil - } - // ErrUnsupportedGetter means that we have another block type - if !errors.Is(err, blocks.ErrUnsupportedGetter) { - return nil, status.Errorf(codes.Internal, "Could not get signed beacon block: %v", err) - } - - if blindedBellatrixBlk, err := blk.PbBlindedBellatrixBlock(); err == nil { - if blindedBellatrixBlk == nil { - return nil, status.Error(codes.Internal, "Nil block") - } - v2Blk, err := migration.V1Alpha1BeaconBlockBlindedBellatrixToV2Blinded(blindedBellatrixBlk.Block) - if err != nil { - return nil, status.Errorf(codes.Internal, "Could not get signed beacon block: %v", err) - } - root, err := blk.Block().HashTreeRoot() - if err != nil { - return nil, status.Errorf(codes.Internal, "Could not get block root: %v", err) - } - isOptimistic, err := bs.OptimisticModeFetcher.IsOptimisticForRoot(ctx, root) - if err != nil { - return nil, status.Errorf(codes.Internal, "Could not check if block is optimistic: %v", err) - } - sig := blk.Signature() - data := ðpbv2.SignedBlindedBeaconBlockBellatrix{ - Message: v2Blk, - Signature: sig[:], - } - sszData, err := data.MarshalSSZ() - if err != nil { - return nil, status.Errorf(codes.Internal, "Could not marshal block into SSZ: %v", err) - } - return ðpbv2.SSZContainer{ - Version: ethpbv2.Version_BELLATRIX, - ExecutionOptimistic: isOptimistic, - Data: sszData, - }, nil - } - // ErrUnsupportedGetter means that we have another block type - if !errors.Is(err, blocks.ErrUnsupportedGetter) { - return nil, status.Errorf(codes.Internal, "Could not get signed beacon block: %v", err) - } - - return nil, status.Errorf(codes.Internal, "Unknown block type %T", blk) -} - // GetBlockRoot retrieves hashTreeRoot of BeaconBlock/BeaconBlockHeader. func (bs *Server) GetBlockRoot(ctx context.Context, req *ethpbv1.BlockRequest) (*ethpbv1.BlockRootResponse, error) { ctx, span := trace.StartSpan(ctx, "beacon.GetBlockRoot") diff --git a/beacon-chain/rpc/eth/beacon/blocks_test.go b/beacon-chain/rpc/eth/beacon/blocks_test.go index 1ae231330ef1..54ab0712a813 100644 --- a/beacon-chain/rpc/eth/beacon/blocks_test.go +++ b/beacon-chain/rpc/eth/beacon/blocks_test.go @@ -1736,121 +1736,6 @@ func TestServer_GetBlockSSZV2(t *testing.T) { }) } -func TestServer_GetBlindedBlockSSZ(t *testing.T) { - t.Run("Phase 0", func(t *testing.T) { - beaconDB := dbTest.SetupDB(t) - ctx := context.Background() - - _, blkContainers := fillDBTestBlocks(ctx, t, beaconDB) - headBlock := blkContainers[len(blkContainers)-1] - - b2 := util.NewBeaconBlock() - b2.Block.Slot = 30 - b2.Block.ParentRoot = bytesutil.PadTo([]byte{1}, 32) - util.SaveBlock(t, ctx, beaconDB, b2) - - wsb, err := blocks.NewSignedBeaconBlock(headBlock.Block.(*ethpbalpha.BeaconBlockContainer_Phase0Block).Phase0Block) - require.NoError(t, err) - - bs := &Server{ - BeaconDB: beaconDB, - ChainInfoFetcher: &mock.ChainService{ - DB: beaconDB, - Block: wsb, - Root: headBlock.BlockRoot, - FinalizedCheckPoint: ðpbalpha.Checkpoint{Root: blkContainers[64].BlockRoot}, - }, - } - - blks, err := beaconDB.BlocksBySlot(ctx, 30) - require.Equal(t, true, len(blks) > 0) - require.NoError(t, err) - sszBlock, err := blks[0].MarshalSSZ() - require.NoError(t, err) - - resp, err := bs.GetBlindedBlockSSZ(ctx, ðpbv1.BlockRequest{BlockId: []byte("30")}) - require.NoError(t, err) - assert.NotNil(t, resp) - assert.DeepEqual(t, sszBlock, resp.Data) - assert.Equal(t, ethpbv2.Version_PHASE0, resp.Version) - }) - - t.Run("Altair", func(t *testing.T) { - beaconDB := dbTest.SetupDB(t) - ctx := context.Background() - - _, blkContainers := fillDBTestBlocksAltair(ctx, t, beaconDB) - headBlock := blkContainers[len(blkContainers)-1] - - b2 := util.NewBeaconBlockAltair() - b2.Block.Slot = 30 - b2.Block.ParentRoot = bytesutil.PadTo([]byte{1}, 32) - util.SaveBlock(t, ctx, beaconDB, b2) - - chainBlk, err := blocks.NewSignedBeaconBlock(headBlock.GetAltairBlock()) - require.NoError(t, err) - bs := &Server{ - BeaconDB: beaconDB, - ChainInfoFetcher: &mock.ChainService{ - DB: beaconDB, - Block: chainBlk, - Root: headBlock.BlockRoot, - FinalizedCheckPoint: ðpbalpha.Checkpoint{Root: blkContainers[64].BlockRoot}, - }, - } - - blks, err := beaconDB.BlocksBySlot(ctx, 30) - require.Equal(t, true, len(blks) > 0) - require.NoError(t, err) - sszBlock, err := blks[0].MarshalSSZ() - require.NoError(t, err) - - resp, err := bs.GetBlindedBlockSSZ(ctx, ðpbv1.BlockRequest{BlockId: []byte("30")}) - require.NoError(t, err) - assert.NotNil(t, resp) - assert.DeepEqual(t, sszBlock, resp.Data) - assert.Equal(t, ethpbv2.Version_ALTAIR, resp.Version) - }) - - t.Run("Bellatrix", func(t *testing.T) { - beaconDB := dbTest.SetupDB(t) - ctx := context.Background() - - _, blkContainers := fillDBTestBlocksBellatrixBlinded(ctx, t, beaconDB) - headBlock := blkContainers[len(blkContainers)-1] - - b2 := util.NewBlindedBeaconBlockBellatrix() - b2.Block.Slot = 30 - b2.Block.ParentRoot = bytesutil.PadTo([]byte{1}, 32) - util.SaveBlock(t, ctx, beaconDB, b2) - - chainBlk, err := blocks.NewSignedBeaconBlock(headBlock.GetBlindedBellatrixBlock()) - require.NoError(t, err) - bs := &Server{ - BeaconDB: beaconDB, - ChainInfoFetcher: &mock.ChainService{ - DB: beaconDB, - Block: chainBlk, - Root: headBlock.BlockRoot, - FinalizedCheckPoint: ðpbalpha.Checkpoint{Root: blkContainers[64].BlockRoot}, - }, - OptimisticModeFetcher: &mock.ChainService{}, - } - - blks, err := beaconDB.BlocksBySlot(ctx, 30) - require.Equal(t, true, len(blks) > 0) - require.NoError(t, err) - sszBlock, err := blks[0].MarshalSSZ() - require.NoError(t, err) - - resp, err := bs.GetBlindedBlockSSZ(ctx, ðpbv1.BlockRequest{BlockId: []byte("30")}) - require.NoError(t, err) - assert.NotNil(t, resp) - assert.DeepEqual(t, sszBlock, resp.Data) - assert.Equal(t, ethpbv2.Version_BELLATRIX, resp.Version) - }) -} - func TestServer_GetBlockRoot(t *testing.T) { beaconDB := dbTest.SetupDB(t) ctx := context.Background() From 16da874b8f91b0f67b693c17b3a623ef65424e31 Mon Sep 17 00:00:00 2001 From: rkapka Date: Fri, 25 Nov 2022 17:01:32 +0100 Subject: [PATCH 03/18] add Capella version (cherry picked from commit 5d6fd0bbe663e5dd16df5b2e773f68982bbcd24e) (cherry picked from commit 82f6ddb693ac9e8b4336b30fae724e478b9e8ec0) --- beacon-chain/rpc/eth/beacon/blinded_blocks.go | 83 +++++++++++++++++++ .../rpc/eth/beacon/blinded_blocks_test.go | 38 +++++++++ 2 files changed, 121 insertions(+) diff --git a/beacon-chain/rpc/eth/beacon/blinded_blocks.go b/beacon-chain/rpc/eth/beacon/blinded_blocks.go index 11ab37f0572f..e51ed3b771b5 100644 --- a/beacon-chain/rpc/eth/beacon/blinded_blocks.go +++ b/beacon-chain/rpc/eth/beacon/blinded_blocks.go @@ -96,6 +96,14 @@ func (bs *Server) GetBlindedBlockSSZ(ctx context.Context, req *ethpbv1.BlockRequ if !errors.Is(err, blocks.ErrUnsupportedGetter) { return nil, status.Errorf(codes.Internal, "Could not get signed beacon block: %v", err) } + result, err = bs.getBlindedSSZBlockCapella(ctx, blk) + if result != nil { + return result, nil + } + // ErrUnsupportedGetter means that we have another block type + if !errors.Is(err, blocks.ErrUnsupportedGetter) { + return nil, status.Errorf(codes.Internal, "Could not get signed beacon block: %v", err) + } return nil, status.Errorf(codes.Internal, "Unknown block type %T", blk) } @@ -401,3 +409,78 @@ func (bs *Server) getBlindedSSZBlockBellatrix(ctx context.Context, blk interface } return ðpbv2.SSZContainer{Version: ethpbv2.Version_BELLATRIX, ExecutionOptimistic: isOptimistic, Data: sszData}, nil } + +func (bs *Server) getBlindedSSZBlockCapella(ctx context.Context, blk interfaces.SignedBeaconBlock) (*ethpbv2.SSZContainer, error) { + capellaBlk, err := blk.PbCapellaBlock() + if err != nil { + // ErrUnsupportedGetter means that we have another block type + if errors.Is(err, blocks.ErrUnsupportedGetter) { + if blindedCapellaBlk, err := blk.PbBlindedCapellaBlock(); err == nil { + if blindedCapellaBlk == nil { + return nil, errNilBlock + } + v2Blk, err := migration.V1Alpha1BeaconBlockBlindedCapellaToV2Blinded(blindedCapellaBlk.Block) + if err != nil { + return nil, errors.Wrapf(err, "could not get signed beacon block") + } + root, err := blk.Block().HashTreeRoot() + if err != nil { + return nil, errors.Wrapf(err, "could not get block root") + } + isOptimistic, err := bs.OptimisticModeFetcher.IsOptimisticForRoot(ctx, root) + if err != nil { + return nil, errors.Wrapf(err, "could not check if block is optimistic") + } + sig := blk.Signature() + data := ðpbv2.SignedBlindedBeaconBlockCapella{ + Message: v2Blk, + Signature: sig[:], + } + sszData, err := data.MarshalSSZ() + if err != nil { + return nil, errors.Wrapf(err, "could not marshal block into SSZ") + } + return ðpbv2.SSZContainer{ + Version: ethpbv2.Version_CAPELLA, + ExecutionOptimistic: isOptimistic, + Data: sszData, + }, nil + } + return nil, err + } + } + + if capellaBlk == nil { + return nil, errNilBlock + } + blindedBlkInterface, err := blk.ToBlinded() + if err != nil { + return nil, errors.Wrapf(err, "could not convert block to blinded block") + } + blindedCapellaBlock, err := blindedBlkInterface.PbBlindedCapellaBlock() + if err != nil { + return nil, errors.Wrapf(err, "could not get signed beacon block") + } + v2Blk, err := migration.V1Alpha1BeaconBlockBlindedCapellaToV2Blinded(blindedCapellaBlock.Block) + if err != nil { + return nil, errors.Wrapf(err, "could not get signed beacon block") + } + root, err := blk.Block().HashTreeRoot() + if err != nil { + return nil, errors.Wrapf(err, "could not get block root") + } + isOptimistic, err := bs.OptimisticModeFetcher.IsOptimisticForRoot(ctx, root) + if err != nil { + return nil, errors.Wrapf(err, "could not check if block is optimistic") + } + sig := blk.Signature() + data := ðpbv2.SignedBlindedBeaconBlockCapella{ + Message: v2Blk, + Signature: sig[:], + } + sszData, err := data.MarshalSSZ() + if err != nil { + return nil, errors.Wrapf(err, "could not marshal block into SSZ") + } + return ðpbv2.SSZContainer{Version: ethpbv2.Version_CAPELLA, ExecutionOptimistic: isOptimistic, Data: sszData}, nil +} diff --git a/beacon-chain/rpc/eth/beacon/blinded_blocks_test.go b/beacon-chain/rpc/eth/beacon/blinded_blocks_test.go index dfc03ff923f9..199317015847 100644 --- a/beacon-chain/rpc/eth/beacon/blinded_blocks_test.go +++ b/beacon-chain/rpc/eth/beacon/blinded_blocks_test.go @@ -706,4 +706,42 @@ func TestServer_GetBlindedBlockSSZ(t *testing.T) { assert.DeepEqual(t, sszBlock, resp.Data) assert.Equal(t, ethpbv2.Version_BELLATRIX, resp.Version) }) + + t.Run("Capella", func(t *testing.T) { + beaconDB := dbTest.SetupDB(t) + ctx := context.Background() + + _, blkContainers := fillDBTestBlocksCapellaBlinded(ctx, t, beaconDB) + headBlock := blkContainers[len(blkContainers)-1] + + b2 := util.NewBlindedBeaconBlockCapella() + b2.Block.Slot = 30 + b2.Block.ParentRoot = bytesutil.PadTo([]byte{1}, 32) + util.SaveBlock(t, ctx, beaconDB, b2) + + chainBlk, err := blocks.NewSignedBeaconBlock(headBlock.GetBlindedCapellaBlock()) + require.NoError(t, err) + bs := &Server{ + BeaconDB: beaconDB, + ChainInfoFetcher: &mock.ChainService{ + DB: beaconDB, + Block: chainBlk, + Root: headBlock.BlockRoot, + FinalizedCheckPoint: ðpbalpha.Checkpoint{Root: blkContainers[64].BlockRoot}, + }, + OptimisticModeFetcher: &mock.ChainService{}, + } + + blks, err := beaconDB.BlocksBySlot(ctx, 30) + require.Equal(t, true, len(blks) > 0) + require.NoError(t, err) + sszBlock, err := blks[0].MarshalSSZ() + require.NoError(t, err) + + resp, err := bs.GetBlindedBlockSSZ(ctx, ðpbv1.BlockRequest{BlockId: []byte("30")}) + require.NoError(t, err) + assert.NotNil(t, resp) + assert.DeepEqual(t, sszBlock, resp.Data) + assert.Equal(t, ethpbv2.Version_CAPELLA, resp.Version) + }) } From 65551b3c64d087ba81233618167bd60b1bc01b2c Mon Sep 17 00:00:00 2001 From: rkapka Date: Fri, 25 Nov 2022 17:38:41 +0100 Subject: [PATCH 04/18] support SSZ lol (cherry picked from commit 52bc2c8d617ac3e1254c493fa053cdce4a1ebd63) (cherry picked from commit d7d70bc25b3ee8acbea10aaf77d26cd1f8c5f26f) --- beacon-chain/rpc/apimiddleware/custom_handlers.go | 13 +++++++++++++ beacon-chain/rpc/apimiddleware/endpoint_factory.go | 1 + 2 files changed, 14 insertions(+) diff --git a/beacon-chain/rpc/apimiddleware/custom_handlers.go b/beacon-chain/rpc/apimiddleware/custom_handlers.go index 3099b83ac543..9f3420024d44 100644 --- a/beacon-chain/rpc/apimiddleware/custom_handlers.go +++ b/beacon-chain/rpc/apimiddleware/custom_handlers.go @@ -64,6 +64,19 @@ func handleGetBeaconBlockSSZV2(m *apimiddleware.ApiProxyMiddleware, endpoint api return handleGetSSZ(m, endpoint, w, req, config) } +func handleGetBlindedBeaconBlockSSZ( + m *apimiddleware.ApiProxyMiddleware, + endpoint apimiddleware.Endpoint, + w http.ResponseWriter, + req *http.Request, +) (handled bool) { + config := sszConfig{ + fileName: "beacon_block.ssz", + responseJson: &VersionedSSZResponseJson{}, + } + return handleGetSSZ(m, endpoint, w, req, config) +} + func handleSubmitBlockSSZ(m *apimiddleware.ApiProxyMiddleware, endpoint apimiddleware.Endpoint, w http.ResponseWriter, req *http.Request) (handled bool) { return handlePostSSZ(m, endpoint, w, req) } diff --git a/beacon-chain/rpc/apimiddleware/endpoint_factory.go b/beacon-chain/rpc/apimiddleware/endpoint_factory.go index fd6f81a58d94..63863ee85e4b 100644 --- a/beacon-chain/rpc/apimiddleware/endpoint_factory.go +++ b/beacon-chain/rpc/apimiddleware/endpoint_factory.go @@ -143,6 +143,7 @@ func (_ *BeaconEndpointFactory) Create(path string) (*apimiddleware.Endpoint, er endpoint.Hooks = apimiddleware.HookCollection{ OnPreSerializeMiddlewareResponseIntoJson: serializeBlindedBlock, } + endpoint.CustomHandlers = []apimiddleware.CustomHandler{handleGetBlindedBeaconBlockSSZ} case "/eth/v1/beacon/pool/attestations": endpoint.RequestQueryParams = []apimiddleware.QueryParam{{Name: "slot"}, {Name: "committee_index"}} endpoint.GetResponse = &AttestationsPoolResponseJson{} From 7477a9879b3e195c72bb15f629665cecf97a1ba3 Mon Sep 17 00:00:00 2001 From: rkapka Date: Tue, 29 Nov 2022 18:37:26 +0100 Subject: [PATCH 05/18] update withdrawals proto --- beacon-chain/core/blocks/withdrawals.go | 6 +- .../state/state-native/getters_withdrawal.go | 16 +-- encoding/ssz/htrutils.go | 4 +- proto/engine/v1/execution_engine.pb.go | 100 +++++++++--------- proto/engine/v1/execution_engine.proto | 4 +- proto/engine/v1/generated.ssz.go | 38 +++---- proto/prysm/v1alpha1/cloners.go | 8 +- proto/prysm/v1alpha1/cloners_test.go | 24 ++--- 8 files changed, 99 insertions(+), 101 deletions(-) diff --git a/beacon-chain/core/blocks/withdrawals.go b/beacon-chain/core/blocks/withdrawals.go index 1836fc00ab4c..24d4e7642ad6 100644 --- a/beacon-chain/core/blocks/withdrawals.go +++ b/beacon-chain/core/blocks/withdrawals.go @@ -120,13 +120,13 @@ func ProcessWithdrawals(st state.BeaconState, withdrawals []*enginev1.Withdrawal return nil, errInvalidWithdrawalNumber } for i, withdrawal := range withdrawals { - if withdrawal.WithdrawalIndex != expected[i].WithdrawalIndex { + if withdrawal.Index != expected[i].Index { return nil, errInvalidWithdrawalIndex } if withdrawal.ValidatorIndex != expected[i].ValidatorIndex { return nil, errInvalidValidatorIndex } - if !bytes.Equal(withdrawal.ExecutionAddress, expected[i].ExecutionAddress) { + if !bytes.Equal(withdrawal.Address, expected[i].Address) { return nil, errInvalidExecutionAddress } if withdrawal.Amount != expected[i].Amount { @@ -138,7 +138,7 @@ func ProcessWithdrawals(st state.BeaconState, withdrawals []*enginev1.Withdrawal } } if len(withdrawals) > 0 { - if err := st.SetNextWithdrawalIndex(withdrawals[len(withdrawals)-1].WithdrawalIndex + 1); err != nil { + if err := st.SetNextWithdrawalIndex(withdrawals[len(withdrawals)-1].Index + 1); err != nil { return nil, errors.Wrap(err, "could not set next withdrawal index") } nextValidatorIndex := withdrawals[len(withdrawals)-1].ValidatorIndex + 1 diff --git a/beacon-chain/state/state-native/getters_withdrawal.go b/beacon-chain/state/state-native/getters_withdrawal.go index de8de612017c..d1c81385c9f6 100644 --- a/beacon-chain/state/state-native/getters_withdrawal.go +++ b/beacon-chain/state/state-native/getters_withdrawal.go @@ -57,18 +57,18 @@ func (b *BeaconState) ExpectedWithdrawals() ([]*enginev1.Withdrawal, error) { balance := b.balances[validatorIndex] if balance > 0 && isFullyWithdrawableValidator(val, epoch) { withdrawals = append(withdrawals, &enginev1.Withdrawal{ - WithdrawalIndex: withdrawalIndex, - ValidatorIndex: validatorIndex, - ExecutionAddress: bytesutil.SafeCopyBytes(val.WithdrawalCredentials[ETH1AddressOffset:]), - Amount: balance, + Index: withdrawalIndex, + ValidatorIndex: validatorIndex, + Address: bytesutil.SafeCopyBytes(val.WithdrawalCredentials[ETH1AddressOffset:]), + Amount: balance, }) withdrawalIndex++ } else if isPartiallyWithdrawableValidator(val, balance) { withdrawals = append(withdrawals, &enginev1.Withdrawal{ - WithdrawalIndex: withdrawalIndex, - ValidatorIndex: validatorIndex, - ExecutionAddress: bytesutil.SafeCopyBytes(val.WithdrawalCredentials[ETH1AddressOffset:]), - Amount: balance - params.BeaconConfig().MaxEffectiveBalance, + Index: withdrawalIndex, + ValidatorIndex: validatorIndex, + Address: bytesutil.SafeCopyBytes(val.WithdrawalCredentials[ETH1AddressOffset:]), + Amount: balance - params.BeaconConfig().MaxEffectiveBalance, }) withdrawalIndex++ } diff --git a/encoding/ssz/htrutils.go b/encoding/ssz/htrutils.go index 49f5d20f4132..f63e78fd545b 100644 --- a/encoding/ssz/htrutils.go +++ b/encoding/ssz/htrutils.go @@ -167,11 +167,11 @@ func transactionRoot(tx []byte) ([32]byte, error) { func withdrawalRoot(hasher HashFn, w *enginev1.Withdrawal) ([32]byte, error) { fieldRoots := make([][32]byte, 4) if w != nil { - binary.LittleEndian.PutUint64(fieldRoots[0][:], w.WithdrawalIndex) + binary.LittleEndian.PutUint64(fieldRoots[0][:], w.Index) binary.LittleEndian.PutUint64(fieldRoots[1][:], uint64(w.ValidatorIndex)) - fieldRoots[2] = bytesutil.ToBytes32(w.ExecutionAddress) + fieldRoots[2] = bytesutil.ToBytes32(w.Address) binary.LittleEndian.PutUint64(fieldRoots[3][:], w.Amount) } return BitwiseMerkleize(hasher, fieldRoots, uint64(len(fieldRoots)), uint64(len(fieldRoots))) diff --git a/proto/engine/v1/execution_engine.pb.go b/proto/engine/v1/execution_engine.pb.go index 43d06b9bcc4a..164cf772ea33 100755 --- a/proto/engine/v1/execution_engine.pb.go +++ b/proto/engine/v1/execution_engine.pb.go @@ -1029,10 +1029,10 @@ type Withdrawal struct { sizeCache protoimpl.SizeCache unknownFields protoimpl.UnknownFields - WithdrawalIndex uint64 `protobuf:"varint,1,opt,name=withdrawal_index,json=withdrawalIndex,proto3" json:"withdrawal_index,omitempty"` - ValidatorIndex github_com_prysmaticlabs_prysm_v3_consensus_types_primitives.ValidatorIndex `protobuf:"varint,2,opt,name=validator_index,json=validatorIndex,proto3" json:"validator_index,omitempty" cast-type:"github.com/prysmaticlabs/prysm/v3/consensus-types/primitives.ValidatorIndex"` - ExecutionAddress []byte `protobuf:"bytes,3,opt,name=execution_address,json=executionAddress,proto3" json:"execution_address,omitempty" ssz-size:"20"` - Amount uint64 `protobuf:"varint,4,opt,name=amount,proto3" json:"amount,omitempty"` + Index uint64 `protobuf:"varint,1,opt,name=index,proto3" json:"index,omitempty"` + ValidatorIndex github_com_prysmaticlabs_prysm_v3_consensus_types_primitives.ValidatorIndex `protobuf:"varint,2,opt,name=validator_index,json=validatorIndex,proto3" json:"validator_index,omitempty" cast-type:"github.com/prysmaticlabs/prysm/v3/consensus-types/primitives.ValidatorIndex"` + Address []byte `protobuf:"bytes,3,opt,name=address,proto3" json:"address,omitempty" ssz-size:"20"` + Amount uint64 `protobuf:"varint,4,opt,name=amount,proto3" json:"amount,omitempty"` } func (x *Withdrawal) Reset() { @@ -1067,9 +1067,9 @@ func (*Withdrawal) Descriptor() ([]byte, []int) { return file_proto_engine_v1_execution_engine_proto_rawDescGZIP(), []int{9} } -func (x *Withdrawal) GetWithdrawalIndex() uint64 { +func (x *Withdrawal) GetIndex() uint64 { if x != nil { - return x.WithdrawalIndex + return x.Index } return 0 } @@ -1081,9 +1081,9 @@ func (x *Withdrawal) GetValidatorIndex() github_com_prysmaticlabs_prysm_v3_conse return github_com_prysmaticlabs_prysm_v3_consensus_types_primitives.ValidatorIndex(0) } -func (x *Withdrawal) GetExecutionAddress() []byte { +func (x *Withdrawal) GetAddress() []byte { if x != nil { - return x.ExecutionAddress + return x.Address } return nil } @@ -1440,49 +1440,47 @@ var file_proto_engine_v1_execution_engine_proto_rawDesc = []byte{ 0x69, 0x6e, 0x61, 0x6c, 0x69, 0x7a, 0x65, 0x64, 0x5f, 0x62, 0x6c, 0x6f, 0x63, 0x6b, 0x5f, 0x68, 0x61, 0x73, 0x68, 0x18, 0x03, 0x20, 0x01, 0x28, 0x0c, 0x42, 0x06, 0x8a, 0xb5, 0x18, 0x02, 0x33, 0x32, 0x52, 0x12, 0x66, 0x69, 0x6e, 0x61, 0x6c, 0x69, 0x7a, 0x65, 0x64, 0x42, 0x6c, 0x6f, 0x63, - 0x6b, 0x48, 0x61, 0x73, 0x68, 0x22, 0xfe, 0x01, 0x0a, 0x0a, 0x57, 0x69, 0x74, 0x68, 0x64, 0x72, - 0x61, 0x77, 0x61, 0x6c, 0x12, 0x29, 0x0a, 0x10, 0x77, 0x69, 0x74, 0x68, 0x64, 0x72, 0x61, 0x77, - 0x61, 0x6c, 0x5f, 0x69, 0x6e, 0x64, 0x65, 0x78, 0x18, 0x01, 0x20, 0x01, 0x28, 0x04, 0x52, 0x0f, - 0x77, 0x69, 0x74, 0x68, 0x64, 0x72, 0x61, 0x77, 0x61, 0x6c, 0x49, 0x6e, 0x64, 0x65, 0x78, 0x12, - 0x78, 0x0a, 0x0f, 0x76, 0x61, 0x6c, 0x69, 0x64, 0x61, 0x74, 0x6f, 0x72, 0x5f, 0x69, 0x6e, 0x64, - 0x65, 0x78, 0x18, 0x02, 0x20, 0x01, 0x28, 0x04, 0x42, 0x4f, 0x82, 0xb5, 0x18, 0x4b, 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, 0x76, 0x33, 0x2f, - 0x63, 0x6f, 0x6e, 0x73, 0x65, 0x6e, 0x73, 0x75, 0x73, 0x2d, 0x74, 0x79, 0x70, 0x65, 0x73, 0x2f, - 0x70, 0x72, 0x69, 0x6d, 0x69, 0x74, 0x69, 0x76, 0x65, 0x73, 0x2e, 0x56, 0x61, 0x6c, 0x69, 0x64, - 0x61, 0x74, 0x6f, 0x72, 0x49, 0x6e, 0x64, 0x65, 0x78, 0x52, 0x0e, 0x76, 0x61, 0x6c, 0x69, 0x64, - 0x61, 0x74, 0x6f, 0x72, 0x49, 0x6e, 0x64, 0x65, 0x78, 0x12, 0x33, 0x0a, 0x11, 0x65, 0x78, 0x65, - 0x63, 0x75, 0x74, 0x69, 0x6f, 0x6e, 0x5f, 0x61, 0x64, 0x64, 0x72, 0x65, 0x73, 0x73, 0x18, 0x03, - 0x20, 0x01, 0x28, 0x0c, 0x42, 0x06, 0x8a, 0xb5, 0x18, 0x02, 0x32, 0x30, 0x52, 0x10, 0x65, 0x78, - 0x65, 0x63, 0x75, 0x74, 0x69, 0x6f, 0x6e, 0x41, 0x64, 0x64, 0x72, 0x65, 0x73, 0x73, 0x12, 0x16, - 0x0a, 0x06, 0x61, 0x6d, 0x6f, 0x75, 0x6e, 0x74, 0x18, 0x04, 0x20, 0x01, 0x28, 0x04, 0x52, 0x06, - 0x61, 0x6d, 0x6f, 0x75, 0x6e, 0x74, 0x22, 0xd8, 0x01, 0x0a, 0x0b, 0x42, 0x6c, 0x6f, 0x62, 0x73, - 0x42, 0x75, 0x6e, 0x64, 0x6c, 0x65, 0x12, 0x25, 0x0a, 0x0a, 0x62, 0x6c, 0x6f, 0x63, 0x6b, 0x5f, - 0x68, 0x61, 0x73, 0x68, 0x18, 0x01, 0x20, 0x01, 0x28, 0x0c, 0x42, 0x06, 0x8a, 0xb5, 0x18, 0x02, - 0x33, 0x32, 0x52, 0x09, 0x62, 0x6c, 0x6f, 0x63, 0x6b, 0x48, 0x61, 0x73, 0x68, 0x12, 0x37, 0x0a, - 0x0f, 0x6b, 0x7a, 0x67, 0x5f, 0x63, 0x6f, 0x6d, 0x6d, 0x69, 0x74, 0x6d, 0x65, 0x6e, 0x74, 0x73, - 0x18, 0x02, 0x20, 0x03, 0x28, 0x0c, 0x42, 0x0e, 0x8a, 0xb5, 0x18, 0x04, 0x3f, 0x2c, 0x34, 0x38, - 0x92, 0xb5, 0x18, 0x02, 0x31, 0x36, 0x52, 0x0e, 0x6b, 0x7a, 0x67, 0x43, 0x6f, 0x6d, 0x6d, 0x69, - 0x74, 0x6d, 0x65, 0x6e, 0x74, 0x73, 0x12, 0x36, 0x0a, 0x05, 0x62, 0x6c, 0x6f, 0x62, 0x73, 0x18, - 0x03, 0x20, 0x03, 0x28, 0x0b, 0x32, 0x18, 0x2e, 0x65, 0x74, 0x68, 0x65, 0x72, 0x65, 0x75, 0x6d, - 0x2e, 0x65, 0x6e, 0x67, 0x69, 0x6e, 0x65, 0x2e, 0x76, 0x31, 0x2e, 0x42, 0x6c, 0x6f, 0x62, 0x42, - 0x06, 0x92, 0xb5, 0x18, 0x02, 0x31, 0x36, 0x52, 0x05, 0x62, 0x6c, 0x6f, 0x62, 0x73, 0x12, 0x31, - 0x0a, 0x10, 0x61, 0x67, 0x67, 0x72, 0x65, 0x67, 0x61, 0x74, 0x65, 0x64, 0x5f, 0x70, 0x72, 0x6f, - 0x6f, 0x66, 0x18, 0x04, 0x20, 0x01, 0x28, 0x0c, 0x42, 0x06, 0x8a, 0xb5, 0x18, 0x02, 0x34, 0x38, - 0x52, 0x0f, 0x61, 0x67, 0x67, 0x72, 0x65, 0x67, 0x61, 0x74, 0x65, 0x64, 0x50, 0x72, 0x6f, 0x6f, - 0x66, 0x22, 0x26, 0x0a, 0x04, 0x42, 0x6c, 0x6f, 0x62, 0x12, 0x1e, 0x0a, 0x04, 0x64, 0x61, 0x74, - 0x61, 0x18, 0x01, 0x20, 0x01, 0x28, 0x0c, 0x42, 0x0a, 0x8a, 0xb5, 0x18, 0x06, 0x31, 0x33, 0x31, - 0x30, 0x37, 0x32, 0x52, 0x04, 0x64, 0x61, 0x74, 0x61, 0x42, 0x96, 0x01, 0x0a, 0x16, 0x6f, 0x72, - 0x67, 0x2e, 0x65, 0x74, 0x68, 0x65, 0x72, 0x65, 0x75, 0x6d, 0x2e, 0x65, 0x6e, 0x67, 0x69, 0x6e, - 0x65, 0x2e, 0x76, 0x31, 0x42, 0x14, 0x45, 0x78, 0x65, 0x63, 0x75, 0x74, 0x69, 0x6f, 0x6e, 0x45, - 0x6e, 0x67, 0x69, 0x6e, 0x65, 0x50, 0x72, 0x6f, 0x74, 0x6f, 0x50, 0x01, 0x5a, 0x3a, 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, 0x76, 0x33, 0x2f, - 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x2f, 0x65, 0x6e, 0x67, 0x69, 0x6e, 0x65, 0x2f, 0x76, 0x31, 0x3b, - 0x65, 0x6e, 0x67, 0x69, 0x6e, 0x65, 0x76, 0x31, 0xaa, 0x02, 0x12, 0x45, 0x74, 0x68, 0x65, 0x72, - 0x65, 0x75, 0x6d, 0x2e, 0x45, 0x6e, 0x67, 0x69, 0x6e, 0x65, 0x2e, 0x56, 0x31, 0xca, 0x02, 0x12, - 0x45, 0x74, 0x68, 0x65, 0x72, 0x65, 0x75, 0x6d, 0x5c, 0x45, 0x6e, 0x67, 0x69, 0x6e, 0x65, 0x5c, - 0x76, 0x31, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33, + 0x6b, 0x48, 0x61, 0x73, 0x68, 0x22, 0xd6, 0x01, 0x0a, 0x0a, 0x57, 0x69, 0x74, 0x68, 0x64, 0x72, + 0x61, 0x77, 0x61, 0x6c, 0x12, 0x14, 0x0a, 0x05, 0x69, 0x6e, 0x64, 0x65, 0x78, 0x18, 0x01, 0x20, + 0x01, 0x28, 0x04, 0x52, 0x05, 0x69, 0x6e, 0x64, 0x65, 0x78, 0x12, 0x78, 0x0a, 0x0f, 0x76, 0x61, + 0x6c, 0x69, 0x64, 0x61, 0x74, 0x6f, 0x72, 0x5f, 0x69, 0x6e, 0x64, 0x65, 0x78, 0x18, 0x02, 0x20, + 0x01, 0x28, 0x04, 0x42, 0x4f, 0x82, 0xb5, 0x18, 0x4b, 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, 0x76, 0x33, 0x2f, 0x63, 0x6f, 0x6e, 0x73, 0x65, + 0x6e, 0x73, 0x75, 0x73, 0x2d, 0x74, 0x79, 0x70, 0x65, 0x73, 0x2f, 0x70, 0x72, 0x69, 0x6d, 0x69, + 0x74, 0x69, 0x76, 0x65, 0x73, 0x2e, 0x56, 0x61, 0x6c, 0x69, 0x64, 0x61, 0x74, 0x6f, 0x72, 0x49, + 0x6e, 0x64, 0x65, 0x78, 0x52, 0x0e, 0x76, 0x61, 0x6c, 0x69, 0x64, 0x61, 0x74, 0x6f, 0x72, 0x49, + 0x6e, 0x64, 0x65, 0x78, 0x12, 0x20, 0x0a, 0x07, 0x61, 0x64, 0x64, 0x72, 0x65, 0x73, 0x73, 0x18, + 0x03, 0x20, 0x01, 0x28, 0x0c, 0x42, 0x06, 0x8a, 0xb5, 0x18, 0x02, 0x32, 0x30, 0x52, 0x07, 0x61, + 0x64, 0x64, 0x72, 0x65, 0x73, 0x73, 0x12, 0x16, 0x0a, 0x06, 0x61, 0x6d, 0x6f, 0x75, 0x6e, 0x74, + 0x18, 0x04, 0x20, 0x01, 0x28, 0x04, 0x52, 0x06, 0x61, 0x6d, 0x6f, 0x75, 0x6e, 0x74, 0x22, 0xd8, + 0x01, 0x0a, 0x0b, 0x42, 0x6c, 0x6f, 0x62, 0x73, 0x42, 0x75, 0x6e, 0x64, 0x6c, 0x65, 0x12, 0x25, + 0x0a, 0x0a, 0x62, 0x6c, 0x6f, 0x63, 0x6b, 0x5f, 0x68, 0x61, 0x73, 0x68, 0x18, 0x01, 0x20, 0x01, + 0x28, 0x0c, 0x42, 0x06, 0x8a, 0xb5, 0x18, 0x02, 0x33, 0x32, 0x52, 0x09, 0x62, 0x6c, 0x6f, 0x63, + 0x6b, 0x48, 0x61, 0x73, 0x68, 0x12, 0x37, 0x0a, 0x0f, 0x6b, 0x7a, 0x67, 0x5f, 0x63, 0x6f, 0x6d, + 0x6d, 0x69, 0x74, 0x6d, 0x65, 0x6e, 0x74, 0x73, 0x18, 0x02, 0x20, 0x03, 0x28, 0x0c, 0x42, 0x0e, + 0x8a, 0xb5, 0x18, 0x04, 0x3f, 0x2c, 0x34, 0x38, 0x92, 0xb5, 0x18, 0x02, 0x31, 0x36, 0x52, 0x0e, + 0x6b, 0x7a, 0x67, 0x43, 0x6f, 0x6d, 0x6d, 0x69, 0x74, 0x6d, 0x65, 0x6e, 0x74, 0x73, 0x12, 0x36, + 0x0a, 0x05, 0x62, 0x6c, 0x6f, 0x62, 0x73, 0x18, 0x03, 0x20, 0x03, 0x28, 0x0b, 0x32, 0x18, 0x2e, + 0x65, 0x74, 0x68, 0x65, 0x72, 0x65, 0x75, 0x6d, 0x2e, 0x65, 0x6e, 0x67, 0x69, 0x6e, 0x65, 0x2e, + 0x76, 0x31, 0x2e, 0x42, 0x6c, 0x6f, 0x62, 0x42, 0x06, 0x92, 0xb5, 0x18, 0x02, 0x31, 0x36, 0x52, + 0x05, 0x62, 0x6c, 0x6f, 0x62, 0x73, 0x12, 0x31, 0x0a, 0x10, 0x61, 0x67, 0x67, 0x72, 0x65, 0x67, + 0x61, 0x74, 0x65, 0x64, 0x5f, 0x70, 0x72, 0x6f, 0x6f, 0x66, 0x18, 0x04, 0x20, 0x01, 0x28, 0x0c, + 0x42, 0x06, 0x8a, 0xb5, 0x18, 0x02, 0x34, 0x38, 0x52, 0x0f, 0x61, 0x67, 0x67, 0x72, 0x65, 0x67, + 0x61, 0x74, 0x65, 0x64, 0x50, 0x72, 0x6f, 0x6f, 0x66, 0x22, 0x26, 0x0a, 0x04, 0x42, 0x6c, 0x6f, + 0x62, 0x12, 0x1e, 0x0a, 0x04, 0x64, 0x61, 0x74, 0x61, 0x18, 0x01, 0x20, 0x01, 0x28, 0x0c, 0x42, + 0x0a, 0x8a, 0xb5, 0x18, 0x06, 0x31, 0x33, 0x31, 0x30, 0x37, 0x32, 0x52, 0x04, 0x64, 0x61, 0x74, + 0x61, 0x42, 0x96, 0x01, 0x0a, 0x16, 0x6f, 0x72, 0x67, 0x2e, 0x65, 0x74, 0x68, 0x65, 0x72, 0x65, + 0x75, 0x6d, 0x2e, 0x65, 0x6e, 0x67, 0x69, 0x6e, 0x65, 0x2e, 0x76, 0x31, 0x42, 0x14, 0x45, 0x78, + 0x65, 0x63, 0x75, 0x74, 0x69, 0x6f, 0x6e, 0x45, 0x6e, 0x67, 0x69, 0x6e, 0x65, 0x50, 0x72, 0x6f, + 0x74, 0x6f, 0x50, 0x01, 0x5a, 0x3a, 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, 0x76, 0x33, 0x2f, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x2f, 0x65, 0x6e, + 0x67, 0x69, 0x6e, 0x65, 0x2f, 0x76, 0x31, 0x3b, 0x65, 0x6e, 0x67, 0x69, 0x6e, 0x65, 0x76, 0x31, + 0xaa, 0x02, 0x12, 0x45, 0x74, 0x68, 0x65, 0x72, 0x65, 0x75, 0x6d, 0x2e, 0x45, 0x6e, 0x67, 0x69, + 0x6e, 0x65, 0x2e, 0x56, 0x31, 0xca, 0x02, 0x12, 0x45, 0x74, 0x68, 0x65, 0x72, 0x65, 0x75, 0x6d, + 0x5c, 0x45, 0x6e, 0x67, 0x69, 0x6e, 0x65, 0x5c, 0x76, 0x31, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, + 0x6f, 0x33, } var ( diff --git a/proto/engine/v1/execution_engine.proto b/proto/engine/v1/execution_engine.proto index 700ad0a6224f..c3e284ac40ae 100644 --- a/proto/engine/v1/execution_engine.proto +++ b/proto/engine/v1/execution_engine.proto @@ -137,13 +137,13 @@ message ForkchoiceState { // The withdrawal receipt with the recipient address and amount withdrawn message Withdrawal { // Withdrawal index for accounting purposes - uint64 withdrawal_index = 1; + uint64 index = 1; // Validator index for the withdrawal uint64 validator_index = 2 [(ethereum.eth.ext.cast_type) = "github.com/prysmaticlabs/prysm/v3/consensus-types/primitives.ValidatorIndex"]; // The execution address receiving the funds - bytes execution_address = 3 [(ethereum.eth.ext.ssz_size) = "20"]; + bytes address = 3 [(ethereum.eth.ext.ssz_size) = "20"]; // The withdrawn amount in Gwei uint64 amount = 4; diff --git a/proto/engine/v1/generated.ssz.go b/proto/engine/v1/generated.ssz.go index d94ffa8b28a4..329806ec752b 100644 --- a/proto/engine/v1/generated.ssz.go +++ b/proto/engine/v1/generated.ssz.go @@ -1,5 +1,5 @@ // Code generated by fastssz. DO NOT EDIT. -// Hash: 0861ebfee0a62ce9e10d8b677c0f8aa1f12c99ee69d3a864018818270a981c57 +// Hash: edbc5aa03156793a03df9d7bed8634b21e5f8b384316f8925d25a5d693045bf8 package enginev1 import ( @@ -1509,18 +1509,18 @@ func (w *Withdrawal) MarshalSSZ() ([]byte, error) { func (w *Withdrawal) MarshalSSZTo(buf []byte) (dst []byte, err error) { dst = buf - // Field (0) 'WithdrawalIndex' - dst = ssz.MarshalUint64(dst, w.WithdrawalIndex) + // Field (0) 'Index' + dst = ssz.MarshalUint64(dst, w.Index) // Field (1) 'ValidatorIndex' dst = ssz.MarshalUint64(dst, uint64(w.ValidatorIndex)) - // Field (2) 'ExecutionAddress' - if size := len(w.ExecutionAddress); size != 20 { - err = ssz.ErrBytesLengthFn("--.ExecutionAddress", size, 20) + // Field (2) 'Address' + if size := len(w.Address); size != 20 { + err = ssz.ErrBytesLengthFn("--.Address", size, 20) return } - dst = append(dst, w.ExecutionAddress...) + dst = append(dst, w.Address...) // Field (3) 'Amount' dst = ssz.MarshalUint64(dst, w.Amount) @@ -1536,17 +1536,17 @@ func (w *Withdrawal) UnmarshalSSZ(buf []byte) error { return ssz.ErrSize } - // Field (0) 'WithdrawalIndex' - w.WithdrawalIndex = ssz.UnmarshallUint64(buf[0:8]) + // Field (0) 'Index' + w.Index = ssz.UnmarshallUint64(buf[0:8]) // Field (1) 'ValidatorIndex' w.ValidatorIndex = github_com_prysmaticlabs_prysm_v3_consensus_types_primitives.ValidatorIndex(ssz.UnmarshallUint64(buf[8:16])) - // Field (2) 'ExecutionAddress' - if cap(w.ExecutionAddress) == 0 { - w.ExecutionAddress = make([]byte, 0, len(buf[16:36])) + // Field (2) 'Address' + if cap(w.Address) == 0 { + w.Address = make([]byte, 0, len(buf[16:36])) } - w.ExecutionAddress = append(w.ExecutionAddress, buf[16:36]...) + w.Address = append(w.Address, buf[16:36]...) // Field (3) 'Amount' w.Amount = ssz.UnmarshallUint64(buf[36:44]) @@ -1569,18 +1569,18 @@ func (w *Withdrawal) HashTreeRoot() ([32]byte, error) { func (w *Withdrawal) HashTreeRootWith(hh *ssz.Hasher) (err error) { indx := hh.Index() - // Field (0) 'WithdrawalIndex' - hh.PutUint64(w.WithdrawalIndex) + // Field (0) 'Index' + hh.PutUint64(w.Index) // Field (1) 'ValidatorIndex' hh.PutUint64(uint64(w.ValidatorIndex)) - // Field (2) 'ExecutionAddress' - if size := len(w.ExecutionAddress); size != 20 { - err = ssz.ErrBytesLengthFn("--.ExecutionAddress", size, 20) + // Field (2) 'Address' + if size := len(w.Address); size != 20 { + err = ssz.ErrBytesLengthFn("--.Address", size, 20) return } - hh.PutBytes(w.ExecutionAddress) + hh.PutBytes(w.Address) // Field (3) 'Amount' hh.PutUint64(w.Amount) diff --git a/proto/prysm/v1alpha1/cloners.go b/proto/prysm/v1alpha1/cloners.go index 643fb8dd5fd3..6d984bc137e2 100644 --- a/proto/prysm/v1alpha1/cloners.go +++ b/proto/prysm/v1alpha1/cloners.go @@ -672,10 +672,10 @@ func CopyWithdrawal(withdrawal *enginev1.Withdrawal) *enginev1.Withdrawal { } return &enginev1.Withdrawal{ - WithdrawalIndex: withdrawal.WithdrawalIndex, - ValidatorIndex: withdrawal.ValidatorIndex, - ExecutionAddress: bytesutil.SafeCopyBytes(withdrawal.ExecutionAddress), - Amount: withdrawal.Amount, + Index: withdrawal.Index, + ValidatorIndex: withdrawal.ValidatorIndex, + Address: bytesutil.SafeCopyBytes(withdrawal.Address), + Amount: withdrawal.Amount, } } diff --git a/proto/prysm/v1alpha1/cloners_test.go b/proto/prysm/v1alpha1/cloners_test.go index fce1efde9f54..b24deefd1438 100644 --- a/proto/prysm/v1alpha1/cloners_test.go +++ b/proto/prysm/v1alpha1/cloners_test.go @@ -878,16 +878,16 @@ func genPayloadCapella() *enginev1.ExecutionPayloadCapella { Transactions: [][]byte{{'a'}, {'b'}, {'c'}}, Withdrawals: []*enginev1.Withdrawal{ { - WithdrawalIndex: 123, - ValidatorIndex: 123, - ExecutionAddress: bytes(20), - Amount: 123, + Index: 123, + ValidatorIndex: 123, + Address: bytes(20), + Amount: 123, }, { - WithdrawalIndex: 124, - ValidatorIndex: 456, - ExecutionAddress: bytes(20), - Amount: 456, + Index: 124, + ValidatorIndex: 456, + Address: bytes(20), + Amount: 456, }, }, } @@ -942,10 +942,10 @@ func genWithdrawals(num int) []*enginev1.Withdrawal { func genWithdrawal() *enginev1.Withdrawal { return &enginev1.Withdrawal{ - WithdrawalIndex: 123456, - ValidatorIndex: 654321, - ExecutionAddress: bytes(20), - Amount: 55555, + Index: 123456, + ValidatorIndex: 654321, + Address: bytes(20), + Amount: 55555, } } From d5680e11b89fc02a1f9d0d2d7972a1bcef77dfff Mon Sep 17 00:00:00 2001 From: rkapka Date: Tue, 29 Nov 2022 15:09:21 +0100 Subject: [PATCH 06/18] refactor and test GetBlockV2 (cherry picked from commit c1d4eaa79d4df04bd284ec65cf261b6f5f260a97) # Conflicts: # beacon-chain/rpc/eth/beacon/blocks.go --- beacon-chain/rpc/eth/beacon/blinded_blocks.go | 1 + beacon-chain/rpc/eth/beacon/blocks.go | 299 ++++++++++++------ beacon-chain/rpc/eth/beacon/blocks_test.go | 176 +++++++++++ 3 files changed, 374 insertions(+), 102 deletions(-) diff --git a/beacon-chain/rpc/eth/beacon/blinded_blocks.go b/beacon-chain/rpc/eth/beacon/blinded_blocks.go index e51ed3b771b5..3523dce8d523 100644 --- a/beacon-chain/rpc/eth/beacon/blinded_blocks.go +++ b/beacon-chain/rpc/eth/beacon/blinded_blocks.go @@ -256,6 +256,7 @@ func (bs *Server) getBlindedBlockCapella(ctx context.Context, blk interfaces.Sig } return nil, err } + return nil, err } if capellaBlk == nil { diff --git a/beacon-chain/rpc/eth/beacon/blocks.go b/beacon-chain/rpc/eth/beacon/blocks.go index f4ac2727e614..a7559d212ad2 100644 --- a/beacon-chain/rpc/eth/beacon/blocks.go +++ b/beacon-chain/rpc/eth/beacon/blocks.go @@ -423,128 +423,38 @@ func (bs *Server) GetBlockV2(ctx context.Context, req *ethpbv2.BlockRequestV2) ( return nil, err } - phase0Blk, err := blk.PbPhase0Block() - if err == nil { - if phase0Blk == nil { - return nil, status.Error(codes.Internal, "Nil block") - } - v1Blk, err := migration.SignedBeaconBlock(blk) - if err != nil { - return nil, status.Errorf(codes.Internal, "Could not get signed beacon block: %v", err) - } - return ðpbv2.BlockResponseV2{ - Version: ethpbv2.Version_PHASE0, - Data: ðpbv2.SignedBeaconBlockContainer{ - Message: ðpbv2.SignedBeaconBlockContainer_Phase0Block{Phase0Block: v1Blk.Block}, - Signature: v1Blk.Signature, - }, - ExecutionOptimistic: false, - }, nil + result, err := getBlockPhase0(blk) + if result != nil { + return result, nil } // ErrUnsupportedGetter means that we have another block type if !errors.Is(err, blocks.ErrUnsupportedGetter) { return nil, status.Errorf(codes.Internal, "Could not get signed beacon block: %v", err) } - - altairBlk, err := blk.PbAltairBlock() - if err == nil { - if altairBlk == nil { - return nil, status.Error(codes.Internal, "Nil block") - } - v2Blk, err := migration.V1Alpha1BeaconBlockAltairToV2(altairBlk.Block) - if err != nil { - return nil, status.Errorf(codes.Internal, "Could not get signed beacon block: %v", err) - } - sig := blk.Signature() - return ðpbv2.BlockResponseV2{ - Version: ethpbv2.Version_ALTAIR, - Data: ðpbv2.SignedBeaconBlockContainer{ - Message: ðpbv2.SignedBeaconBlockContainer_AltairBlock{AltairBlock: v2Blk}, - Signature: sig[:], - }, - ExecutionOptimistic: false, - }, nil + result, err = getBlockAltair(blk) + if result != nil { + return result, nil } // ErrUnsupportedGetter means that we have another block type if !errors.Is(err, blocks.ErrUnsupportedGetter) { return nil, status.Errorf(codes.Internal, "Could not get signed beacon block: %v", err) } - - bellatrixBlk, err := blk.PbBellatrixBlock() - if err == nil { - if bellatrixBlk == nil { - return nil, status.Error(codes.Internal, "Nil block") - } - v2Blk, err := migration.V1Alpha1BeaconBlockBellatrixToV2(bellatrixBlk.Block) - if err != nil { - return nil, status.Errorf(codes.Internal, "Could not get signed beacon block: %v", err) - } - root, err := blk.Block().HashTreeRoot() - if err != nil { - return nil, status.Errorf(codes.Internal, "Could not get block root: %v", err) - } - isOptimistic, err := bs.OptimisticModeFetcher.IsOptimisticForRoot(ctx, root) - if err != nil { - return nil, status.Errorf(codes.Internal, "Could not check if block is optimistic: %v", err) - } - sig := blk.Signature() - return ðpbv2.BlockResponseV2{ - Version: ethpbv2.Version_BELLATRIX, - Data: ðpbv2.SignedBeaconBlockContainer{ - Message: ðpbv2.SignedBeaconBlockContainer_BellatrixBlock{BellatrixBlock: v2Blk}, - Signature: sig[:], - }, - ExecutionOptimistic: isOptimistic, - }, nil + result, err = bs.getBlockBellatrix(ctx, blk) + if result != nil { + return result, nil } // ErrUnsupportedGetter means that we have another block type if !errors.Is(err, blocks.ErrUnsupportedGetter) { return nil, status.Errorf(codes.Internal, "Could not get signed beacon block: %v", err) } - - if blindedBellatrixBlk, err := blk.PbBlindedBellatrixBlock(); err == nil { - if blindedBellatrixBlk == nil { - return nil, status.Error(codes.Internal, "Nil block") - } - signedFullBlock, err := bs.ExecutionPayloadReconstructor.ReconstructFullBellatrixBlock(ctx, blk) - if err != nil { - return nil, status.Errorf( - codes.Internal, - "Could not reconstruct full execution payload to create signed beacon block: %v", - err, - ) - } - bellatrixBlk, err = signedFullBlock.PbBellatrixBlock() - if err != nil { - return nil, status.Errorf(codes.Internal, "Could not get signed beacon block: %v", err) - } - v2Blk, err := migration.V1Alpha1BeaconBlockBellatrixToV2(bellatrixBlk.Block) - if err != nil { - return nil, status.Errorf(codes.Internal, "Could not get signed beacon block: %v", err) - } - root, err := blk.Block().HashTreeRoot() - if err != nil { - return nil, status.Errorf(codes.Internal, "Could not get block root: %v", err) - } - isOptimistic, err := bs.OptimisticModeFetcher.IsOptimisticForRoot(ctx, root) - if err != nil { - return nil, status.Errorf(codes.Internal, "Could not check if block is optimistic: %v", err) - } - sig := blk.Signature() - return ðpbv2.BlockResponseV2{ - Version: ethpbv2.Version_BELLATRIX, - Data: ðpbv2.SignedBeaconBlockContainer{ - Message: ðpbv2.SignedBeaconBlockContainer_BellatrixBlock{BellatrixBlock: v2Blk}, - Signature: sig[:], - }, - ExecutionOptimistic: isOptimistic, - }, nil + result, err = bs.getBlockCapella(ctx, blk) + if result != nil { + return result, nil } // ErrUnsupportedGetter means that we have another block type if !errors.Is(err, blocks.ErrUnsupportedGetter) { return nil, status.Errorf(codes.Internal, "Could not get signed beacon block: %v", err) } - return nil, status.Errorf(codes.Internal, "Unknown block type %T", blk) } @@ -877,6 +787,191 @@ func handleGetBlockError(blk interfaces.SignedBeaconBlock, err error) error { return nil } +func getBlockPhase0(blk interfaces.SignedBeaconBlock) (*ethpbv2.BlockResponseV2, error) { + phase0Blk, err := blk.PbPhase0Block() + if err != nil { + return nil, err + } + if phase0Blk == nil { + return nil, errNilBlock + } + v1Blk, err := migration.SignedBeaconBlock(blk) + if err != nil { + return nil, errors.Wrapf(err, "could not get signed beacon block") + } + return ðpbv2.BlockResponseV2{ + Version: ethpbv2.Version_PHASE0, + Data: ðpbv2.SignedBeaconBlockContainer{ + Message: ðpbv2.SignedBeaconBlockContainer_Phase0Block{Phase0Block: v1Blk.Block}, + Signature: v1Blk.Signature, + }, + ExecutionOptimistic: false, + }, nil +} + +func getBlockAltair(blk interfaces.SignedBeaconBlock) (*ethpbv2.BlockResponseV2, error) { + altairBlk, err := blk.PbAltairBlock() + if err != nil { + return nil, err + } + if altairBlk == nil { + return nil, errNilBlock + } + v2Blk, err := migration.V1Alpha1BeaconBlockAltairToV2(altairBlk.Block) + if err != nil { + return nil, errors.Wrapf(err, "could not get signed beacon block") + } + sig := blk.Signature() + return ðpbv2.BlockResponseV2{ + Version: ethpbv2.Version_ALTAIR, + Data: ðpbv2.SignedBeaconBlockContainer{ + Message: ðpbv2.SignedBeaconBlockContainer_AltairBlock{AltairBlock: v2Blk}, + Signature: sig[:], + }, + ExecutionOptimistic: false, + }, nil +} + +func (bs *Server) getBlockBellatrix(ctx context.Context, blk interfaces.SignedBeaconBlock) (*ethpbv2.BlockResponseV2, error) { + bellatrixBlk, err := blk.PbBellatrixBlock() + if err != nil { + // ErrUnsupportedGetter means that we have another block type + if errors.Is(err, blocks.ErrUnsupportedGetter) { + if blindedBellatrixBlk, err := blk.PbBlindedBellatrixBlock(); err == nil { + if blindedBellatrixBlk == nil { + return nil, errNilBlock + } + signedFullBlock, err := bs.ExecutionPayloadReconstructor.ReconstructFullBellatrixBlock(ctx, blk) + if err != nil { + return nil, errors.Wrapf(err, "could not reconstruct full execution payload to create signed beacon block") + } + bellatrixBlk, err = signedFullBlock.PbBellatrixBlock() + if err != nil { + return nil, errors.Wrapf(err, "could not get signed beacon block") + } + v2Blk, err := migration.V1Alpha1BeaconBlockBellatrixToV2(bellatrixBlk.Block) + if err != nil { + return nil, errors.Wrapf(err, "could not convert beacon block") + } + root, err := blk.Block().HashTreeRoot() + if err != nil { + return nil, errors.Wrapf(err, "could not get block root") + } + isOptimistic, err := bs.OptimisticModeFetcher.IsOptimisticForRoot(ctx, root) + if err != nil { + return nil, errors.Wrapf(err, "could not check if block is optimistic") + } + sig := blk.Signature() + return ðpbv2.BlockResponseV2{ + Version: ethpbv2.Version_BELLATRIX, + Data: ðpbv2.SignedBeaconBlockContainer{ + Message: ðpbv2.SignedBeaconBlockContainer_BellatrixBlock{BellatrixBlock: v2Blk}, + Signature: sig[:], + }, + ExecutionOptimistic: isOptimistic, + }, nil + } + return nil, err + } + return nil, err + } + + if bellatrixBlk == nil { + return nil, errNilBlock + } + v2Blk, err := migration.V1Alpha1BeaconBlockBellatrixToV2(bellatrixBlk.Block) + if err != nil { + return nil, errors.Wrapf(err, "could not convert beacon block") + } + root, err := blk.Block().HashTreeRoot() + if err != nil { + return nil, errors.Wrapf(err, "could not get block root") + } + isOptimistic, err := bs.OptimisticModeFetcher.IsOptimisticForRoot(ctx, root) + if err != nil { + return nil, errors.Wrapf(err, "could not check if block is optimistic") + } + sig := blk.Signature() + return ðpbv2.BlockResponseV2{ + Version: ethpbv2.Version_BELLATRIX, + Data: ðpbv2.SignedBeaconBlockContainer{ + Message: ðpbv2.SignedBeaconBlockContainer_BellatrixBlock{BellatrixBlock: v2Blk}, + Signature: sig[:], + }, + ExecutionOptimistic: isOptimistic, + }, nil +} + +func (bs *Server) getBlockCapella(ctx context.Context, blk interfaces.SignedBeaconBlock) (*ethpbv2.BlockResponseV2, error) { + capellaBlk, err := blk.PbCapellaBlock() + if err != nil { + // ErrUnsupportedGetter means that we have another block type + if errors.Is(err, blocks.ErrUnsupportedGetter) { + if blindedCapellaBlk, err := blk.PbBlindedCapellaBlock(); err == nil { + if blindedCapellaBlk == nil { + return nil, errNilBlock + } + signedFullBlock, err := bs.ExecutionPayloadReconstructor.ReconstructFullBellatrixBlock(ctx, blk) + if err != nil { + return nil, errors.Wrapf(err, "could not reconstruct full execution payload to create signed beacon block") + } + capellaBlk, err = signedFullBlock.PbCapellaBlock() + if err != nil { + return nil, errors.Wrapf(err, "could not get signed beacon block") + } + v2Blk, err := migration.V1Alpha1BeaconBlockCapellaToV2(capellaBlk.Block) + if err != nil { + return nil, errors.Wrapf(err, "could not convert beacon block") + } + root, err := blk.Block().HashTreeRoot() + if err != nil { + return nil, errors.Wrapf(err, "could not get block root") + } + isOptimistic, err := bs.OptimisticModeFetcher.IsOptimisticForRoot(ctx, root) + if err != nil { + return nil, errors.Wrapf(err, "could not check if block is optimistic") + } + sig := blk.Signature() + return ðpbv2.BlockResponseV2{ + Version: ethpbv2.Version_CAPELLA, + Data: ðpbv2.SignedBeaconBlockContainer{ + Message: ðpbv2.SignedBeaconBlockContainer_CapellaBlock{CapellaBlock: v2Blk}, + Signature: sig[:], + }, + ExecutionOptimistic: isOptimistic, + }, nil + } + return nil, err + } + return nil, err + } + + if capellaBlk == nil { + return nil, errNilBlock + } + v2Blk, err := migration.V1Alpha1BeaconBlockCapellaToV2(capellaBlk.Block) + if err != nil { + return nil, errors.Wrapf(err, "could not convert beacon block") + } + root, err := blk.Block().HashTreeRoot() + if err != nil { + return nil, errors.Wrapf(err, "could not get block root") + } + isOptimistic, err := bs.OptimisticModeFetcher.IsOptimisticForRoot(ctx, root) + if err != nil { + return nil, errors.Wrapf(err, "could not check if block is optimistic") + } + sig := blk.Signature() + return ðpbv2.BlockResponseV2{ + Version: ethpbv2.Version_CAPELLA, + Data: ðpbv2.SignedBeaconBlockContainer{ + Message: ðpbv2.SignedBeaconBlockContainer_CapellaBlock{CapellaBlock: v2Blk}, + Signature: sig[:], + }, + ExecutionOptimistic: isOptimistic, + }, nil +} + func (bs *Server) submitPhase0Block(ctx context.Context, phase0Blk *ethpbv1.BeaconBlock, sig []byte) error { v1alpha1Blk, err := migration.V1ToV1Alpha1SignedBlock(ðpbv1.SignedBeaconBlock{Block: phase0Blk, Signature: sig}) if err != nil { diff --git a/beacon-chain/rpc/eth/beacon/blocks_test.go b/beacon-chain/rpc/eth/beacon/blocks_test.go index 54ab0712a813..9635816f32ff 100644 --- a/beacon-chain/rpc/eth/beacon/blocks_test.go +++ b/beacon-chain/rpc/eth/beacon/blocks_test.go @@ -158,6 +158,48 @@ func fillDBTestBlocksBellatrix(ctx context.Context, t *testing.T, beaconDB db.Da return genBlk, blkContainers } +func fillDBTestBlocksCapella(ctx context.Context, t *testing.T, beaconDB db.Database) (*ethpbalpha.SignedBeaconBlockCapella, []*ethpbalpha.BeaconBlockContainer) { + parentRoot := [32]byte{1, 2, 3} + genBlk := util.NewBeaconBlockCapella() + genBlk.Block.ParentRoot = parentRoot[:] + root, err := genBlk.Block.HashTreeRoot() + require.NoError(t, err) + util.SaveBlock(t, ctx, beaconDB, genBlk) + require.NoError(t, beaconDB.SaveGenesisBlockRoot(ctx, root)) + + count := types.Slot(100) + blks := make([]interfaces.SignedBeaconBlock, count) + blkContainers := make([]*ethpbalpha.BeaconBlockContainer, count) + for i := types.Slot(0); i < count; i++ { + b := util.NewBeaconBlockCapella() + b.Block.Slot = i + b.Block.ParentRoot = bytesutil.PadTo([]byte{uint8(i)}, 32) + att1 := util.NewAttestation() + att1.Data.Slot = i + att1.Data.CommitteeIndex = types.CommitteeIndex(i) + att2 := util.NewAttestation() + att2.Data.Slot = i + att2.Data.CommitteeIndex = types.CommitteeIndex(i + 1) + b.Block.Body.Attestations = []*ethpbalpha.Attestation{att1, att2} + root, err := b.Block.HashTreeRoot() + require.NoError(t, err) + signedB, err := blocks.NewSignedBeaconBlock(b) + require.NoError(t, err) + blks[i] = signedB + blkContainers[i] = ðpbalpha.BeaconBlockContainer{ + Block: ðpbalpha.BeaconBlockContainer_CapellaBlock{CapellaBlock: b}, BlockRoot: root[:]} + } + require.NoError(t, beaconDB.SaveBlocks(ctx, blks)) + headRoot := bytesutil.ToBytes32(blkContainers[len(blks)-1].BlockRoot) + summary := ðpbalpha.StateSummary{ + Root: headRoot[:], + Slot: blkContainers[len(blks)-1].Block.(*ethpbalpha.BeaconBlockContainer_CapellaBlock).CapellaBlock.Block.Slot, + } + require.NoError(t, beaconDB.SaveStateSummary(ctx, summary)) + require.NoError(t, beaconDB.SaveHeadBlockRoot(ctx, headRoot)) + return genBlk, blkContainers +} + func fillDBTestBlocksBellatrixBlinded(ctx context.Context, t *testing.T, beaconDB db.Database) (*ethpbalpha.SignedBlindedBeaconBlockBellatrix, []*ethpbalpha.BeaconBlockContainer) { parentRoot := [32]byte{1, 2, 3} genBlk := util.NewBlindedBeaconBlockBellatrix() @@ -1544,6 +1586,140 @@ func TestServer_GetBlockV2(t *testing.T) { } }) + t.Run("Capella", func(t *testing.T) { + beaconDB := dbTest.SetupDB(t) + ctx := context.Background() + + genBlk, blkContainers := fillDBTestBlocksCapella(ctx, t, beaconDB) + canonicalRoots := make(map[[32]byte]bool) + + for _, bContr := range blkContainers { + canonicalRoots[bytesutil.ToBytes32(bContr.BlockRoot)] = true + } + headBlock := blkContainers[len(blkContainers)-1] + nextSlot := headBlock.GetCapellaBlock().Block.Slot + 1 + + b2 := util.NewBeaconBlockCapella() + b2.Block.Slot = 30 + b2.Block.ParentRoot = bytesutil.PadTo([]byte{1}, 32) + util.SaveBlock(t, ctx, beaconDB, b2) + b3 := util.NewBeaconBlockCapella() + b3.Block.Slot = 30 + b3.Block.ParentRoot = bytesutil.PadTo([]byte{4}, 32) + util.SaveBlock(t, ctx, beaconDB, b3) + b4 := util.NewBeaconBlockCapella() + b4.Block.Slot = nextSlot + b4.Block.ParentRoot = bytesutil.PadTo([]byte{8}, 32) + util.SaveBlock(t, ctx, beaconDB, b4) + + chainBlk, err := blocks.NewSignedBeaconBlock(headBlock.GetCapellaBlock()) + require.NoError(t, err) + mockChainService := &mock.ChainService{ + DB: beaconDB, + Block: chainBlk, + Root: headBlock.BlockRoot, + FinalizedCheckPoint: ðpbalpha.Checkpoint{Root: blkContainers[64].BlockRoot}, + CanonicalRoots: canonicalRoots, + } + bs := &Server{ + BeaconDB: beaconDB, + ChainInfoFetcher: mockChainService, + HeadFetcher: mockChainService, + OptimisticModeFetcher: mockChainService, + ExecutionPayloadReconstructor: &executionTest.EngineClient{ + ExecutionPayloadByBlockHash: map[[32]byte]*enginev1.ExecutionPayload{}, + }, + } + + root, err := genBlk.Block.HashTreeRoot() + require.NoError(t, err) + + tests := []struct { + name string + blockID []byte + want *ethpbalpha.SignedBeaconBlockCapella + wantErr bool + }{ + { + name: "slot", + blockID: []byte("30"), + want: blkContainers[30].GetCapellaBlock(), + }, + { + name: "bad formatting", + blockID: []byte("3bad0"), + wantErr: true, + }, + { + name: "canonical", + blockID: []byte("30"), + want: blkContainers[30].GetCapellaBlock(), + }, + { + name: "non canonical", + blockID: []byte(fmt.Sprintf("%d", nextSlot)), + wantErr: true, + }, + { + name: "head", + blockID: []byte("head"), + want: headBlock.GetCapellaBlock(), + }, + { + name: "finalized", + blockID: []byte("finalized"), + want: blkContainers[64].GetCapellaBlock(), + }, + { + name: "genesis", + blockID: []byte("genesis"), + want: genBlk, + }, + { + name: "genesis root", + blockID: root[:], + want: genBlk, + }, + { + name: "root", + blockID: blkContainers[20].BlockRoot, + want: blkContainers[20].GetCapellaBlock(), + }, + { + name: "non-existent root", + blockID: bytesutil.PadTo([]byte("hi there"), 32), + wantErr: true, + }, + { + name: "no block", + blockID: []byte("105"), + wantErr: true, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + blk, err := bs.GetBlockV2(ctx, ðpbv2.BlockRequestV2{ + BlockId: tt.blockID, + }) + if tt.wantErr { + require.NotEqual(t, err, nil) + return + } + require.NoError(t, err) + + v2Block, err := migration.V1Alpha1BeaconBlockCapellaToV2(tt.want.Block) + require.NoError(t, err) + + b, ok := blk.Data.Message.(*ethpbv2.SignedBeaconBlockContainer_CapellaBlock) + require.Equal(t, true, ok) + if !reflect.DeepEqual(b.CapellaBlock, v2Block) { + t.Error("Expected blocks to equal") + } + assert.Equal(t, ethpbv2.Version_CAPELLA, blk.Version) + }) + } + }) + t.Run("execution optimistic", func(t *testing.T) { beaconDB := dbTest.SetupDB(t) ctx := context.Background() From 4f5cfb5e9237eb4a00e5e01f677b8578813250ea Mon Sep 17 00:00:00 2001 From: rkapka Date: Tue, 29 Nov 2022 15:39:10 +0100 Subject: [PATCH 07/18] refactor and test GetSSZBlockV2 (cherry picked from commit fbc4e73d31c2f68d55d1e2bb8e7f2d8c7458c0a0) # Conflicts: # beacon-chain/rpc/eth/beacon/blocks.go --- beacon-chain/rpc/eth/beacon/blinded_blocks.go | 47 +-- beacon-chain/rpc/eth/beacon/blocks.go | 312 ++++++++++++------ beacon-chain/rpc/eth/beacon/blocks_test.go | 38 +++ 3 files changed, 247 insertions(+), 150 deletions(-) diff --git a/beacon-chain/rpc/eth/beacon/blinded_blocks.go b/beacon-chain/rpc/eth/beacon/blinded_blocks.go index 3523dce8d523..c347dd2d5074 100644 --- a/beacon-chain/rpc/eth/beacon/blinded_blocks.go +++ b/beacon-chain/rpc/eth/beacon/blinded_blocks.go @@ -72,7 +72,7 @@ func (bs *Server) GetBlindedBlockSSZ(ctx context.Context, req *ethpbv1.BlockRequ return nil, err } - result, err := getBlindedSSZBlockPhase0(blk) + result, err := getSSZBlockPhase0(blk) if result != nil { return result, nil } @@ -80,7 +80,7 @@ func (bs *Server) GetBlindedBlockSSZ(ctx context.Context, req *ethpbv1.BlockRequ if !errors.Is(err, blocks.ErrUnsupportedGetter) { return nil, status.Errorf(codes.Internal, "Could not get signed beacon block: %v", err) } - result, err = getBlindedSSZBlockAltair(blk) + result, err = getSSZBlockAltair(blk) if result != nil { return result, nil } @@ -293,49 +293,6 @@ func (bs *Server) getBlindedBlockCapella(ctx context.Context, blk interfaces.Sig }, nil } -func getBlindedSSZBlockPhase0(blk interfaces.SignedBeaconBlock) (*ethpbv2.SSZContainer, error) { - phase0Blk, err := blk.PbPhase0Block() - if err != nil { - return nil, err - } - if phase0Blk == nil { - return nil, errNilBlock - } - signedBeaconBlock, err := migration.SignedBeaconBlock(blk) - if err != nil { - return nil, errors.Wrapf(err, "could not get signed beacon block") - } - sszBlock, err := signedBeaconBlock.MarshalSSZ() - if err != nil { - return nil, errors.Wrapf(err, "could not marshal block into SSZ") - } - return ðpbv2.SSZContainer{Version: ethpbv2.Version_PHASE0, ExecutionOptimistic: false, Data: sszBlock}, nil -} - -func getBlindedSSZBlockAltair(blk interfaces.SignedBeaconBlock) (*ethpbv2.SSZContainer, error) { - altairBlk, err := blk.PbAltairBlock() - if err != nil { - return nil, err - } - if altairBlk == nil { - return nil, errNilBlock - } - v2Blk, err := migration.V1Alpha1BeaconBlockAltairToV2(altairBlk.Block) - if err != nil { - return nil, errors.Wrapf(err, "could not get signed beacon block") - } - sig := blk.Signature() - data := ðpbv2.SignedBeaconBlockAltair{ - Message: v2Blk, - Signature: sig[:], - } - sszData, err := data.MarshalSSZ() - if err != nil { - return nil, errors.Wrapf(err, "could not marshal block into SSZ") - } - return ðpbv2.SSZContainer{Version: ethpbv2.Version_ALTAIR, ExecutionOptimistic: false, Data: sszData}, nil -} - func (bs *Server) getBlindedSSZBlockBellatrix(ctx context.Context, blk interfaces.SignedBeaconBlock) (*ethpbv2.SSZContainer, error) { bellatrixBlk, err := blk.PbBellatrixBlock() if err != nil { diff --git a/beacon-chain/rpc/eth/beacon/blocks.go b/beacon-chain/rpc/eth/beacon/blocks.go index a7559d212ad2..0be028517570 100644 --- a/beacon-chain/rpc/eth/beacon/blocks.go +++ b/beacon-chain/rpc/eth/beacon/blocks.go @@ -469,126 +469,33 @@ func (bs *Server) GetBlockSSZV2(ctx context.Context, req *ethpbv2.BlockRequestV2 return nil, err } - phase0Blk, err := blk.PbPhase0Block() - if err == nil { - if phase0Blk == nil { - return nil, status.Error(codes.Internal, "Nil block") - } - signedBeaconBlock, err := migration.SignedBeaconBlock(blk) - if err != nil { - return nil, status.Errorf(codes.Internal, "Could not get signed beacon block: %v", err) - } - sszBlock, err := signedBeaconBlock.MarshalSSZ() - if err != nil { - return nil, status.Errorf(codes.Internal, "Could not marshal block into SSZ: %v", err) - } - return ðpbv2.SSZContainer{Version: ethpbv2.Version_PHASE0, ExecutionOptimistic: false, Data: sszBlock}, nil + result, err := getSSZBlockPhase0(blk) + if result != nil { + return result, nil } // ErrUnsupportedGetter means that we have another block type if !errors.Is(err, blocks.ErrUnsupportedGetter) { return nil, status.Errorf(codes.Internal, "Could not get signed beacon block: %v", err) } - - altairBlk, err := blk.PbAltairBlock() - if err == nil { - if altairBlk == nil { - return nil, status.Error(codes.Internal, "Nil block") - } - v2Blk, err := migration.V1Alpha1BeaconBlockAltairToV2(altairBlk.Block) - if err != nil { - return nil, status.Errorf(codes.Internal, "Could not get signed beacon block: %v", err) - } - sig := blk.Signature() - data := ðpbv2.SignedBeaconBlockAltair{ - Message: v2Blk, - Signature: sig[:], - } - sszData, err := data.MarshalSSZ() - if err != nil { - return nil, status.Errorf(codes.Internal, "Could not marshal block into SSZ: %v", err) - } - return ðpbv2.SSZContainer{Version: ethpbv2.Version_ALTAIR, ExecutionOptimistic: false, Data: sszData}, nil + result, err = getSSZBlockAltair(blk) + if result != nil { + return result, nil } // ErrUnsupportedGetter means that we have another block type if !errors.Is(err, blocks.ErrUnsupportedGetter) { return nil, status.Errorf(codes.Internal, "Could not get signed beacon block: %v", err) } - - bellatrixBlk, err := blk.PbBellatrixBlock() - if err == nil { - if bellatrixBlk == nil { - return nil, status.Error(codes.Internal, "Nil block") - } - v2Blk, err := migration.V1Alpha1BeaconBlockBellatrixToV2(bellatrixBlk.Block) - if err != nil { - return nil, status.Errorf(codes.Internal, "Could not get signed beacon block: %v", err) - } - root, err := blk.Block().HashTreeRoot() - if err != nil { - return nil, status.Errorf(codes.Internal, "Could not get block root: %v", err) - } - isOptimistic, err := bs.OptimisticModeFetcher.IsOptimisticForRoot(ctx, root) - if err != nil { - return nil, status.Errorf(codes.Internal, "Could not check if block is optimistic: %v", err) - } - sig := blk.Signature() - data := ðpbv2.SignedBeaconBlockBellatrix{ - Message: v2Blk, - Signature: sig[:], - } - sszData, err := data.MarshalSSZ() - if err != nil { - return nil, status.Errorf(codes.Internal, "Could not marshal block into SSZ: %v", err) - } - return ðpbv2.SSZContainer{Version: ethpbv2.Version_BELLATRIX, ExecutionOptimistic: isOptimistic, Data: sszData}, nil + result, err = bs.getSSZBlockBellatrix(ctx, blk) + if result != nil { + return result, nil } // ErrUnsupportedGetter means that we have another block type if !errors.Is(err, blocks.ErrUnsupportedGetter) { return nil, status.Errorf(codes.Internal, "Could not get signed beacon block: %v", err) } - - if blindedBellatrixBlk, err := blk.PbBlindedBellatrixBlock(); err == nil { - if blindedBellatrixBlk == nil { - return nil, status.Error(codes.Internal, "Nil block") - } - signedFullBlock, err := bs.ExecutionPayloadReconstructor.ReconstructFullBellatrixBlock(ctx, blk) - if err != nil { - return nil, status.Errorf( - codes.Internal, - "Could not reconstruct full execution payload to create signed beacon block: %v", - err, - ) - } - bellatrixBlk, err = signedFullBlock.PbBellatrixBlock() - if err != nil { - return nil, status.Errorf(codes.Internal, "Could not get signed beacon block: %v", err) - } - v2Blk, err := migration.V1Alpha1BeaconBlockBellatrixToV2(bellatrixBlk.Block) - if err != nil { - return nil, status.Errorf(codes.Internal, "Could not get signed beacon block: %v", err) - } - root, err := blk.Block().HashTreeRoot() - if err != nil { - return nil, status.Errorf(codes.Internal, "Could not get block root: %v", err) - } - isOptimistic, err := bs.OptimisticModeFetcher.IsOptimisticForRoot(ctx, root) - if err != nil { - return nil, status.Errorf(codes.Internal, "Could not check if block is optimistic: %v", err) - } - sig := blk.Signature() - data := ðpbv2.SignedBeaconBlockBellatrix{ - Message: v2Blk, - Signature: sig[:], - } - sszData, err := data.MarshalSSZ() - if err != nil { - return nil, status.Errorf(codes.Internal, "Could not marshal block into SSZ: %v", err) - } - return ðpbv2.SSZContainer{ - Version: ethpbv2.Version_BELLATRIX, - ExecutionOptimistic: isOptimistic, - Data: sszData, - }, nil + result, err = bs.getSSZBlockCapella(ctx, blk) + if result != nil { + return result, nil } // ErrUnsupportedGetter means that we have another block type if !errors.Is(err, blocks.ErrUnsupportedGetter) { @@ -972,6 +879,201 @@ func (bs *Server) getBlockCapella(ctx context.Context, blk interfaces.SignedBeac }, nil } +func getSSZBlockPhase0(blk interfaces.SignedBeaconBlock) (*ethpbv2.SSZContainer, error) { + phase0Blk, err := blk.PbPhase0Block() + if err != nil { + return nil, err + } + if phase0Blk == nil { + return nil, errNilBlock + } + signedBeaconBlock, err := migration.SignedBeaconBlock(blk) + if err != nil { + return nil, errors.Wrapf(err, "could not get signed beacon block") + } + sszBlock, err := signedBeaconBlock.MarshalSSZ() + if err != nil { + return nil, errors.Wrapf(err, "could not marshal block into SSZ") + } + return ðpbv2.SSZContainer{Version: ethpbv2.Version_PHASE0, ExecutionOptimistic: false, Data: sszBlock}, nil +} + +func getSSZBlockAltair(blk interfaces.SignedBeaconBlock) (*ethpbv2.SSZContainer, error) { + altairBlk, err := blk.PbAltairBlock() + if err != nil { + return nil, err + } + if altairBlk == nil { + return nil, errNilBlock + } + v2Blk, err := migration.V1Alpha1BeaconBlockAltairToV2(altairBlk.Block) + if err != nil { + return nil, errors.Wrapf(err, "could not get signed beacon block") + } + sig := blk.Signature() + data := ðpbv2.SignedBeaconBlockAltair{ + Message: v2Blk, + Signature: sig[:], + } + sszData, err := data.MarshalSSZ() + if err != nil { + return nil, errors.Wrapf(err, "could not marshal block into SSZ") + } + return ðpbv2.SSZContainer{Version: ethpbv2.Version_ALTAIR, ExecutionOptimistic: false, Data: sszData}, nil +} + +func (bs *Server) getSSZBlockBellatrix(ctx context.Context, blk interfaces.SignedBeaconBlock) (*ethpbv2.SSZContainer, error) { + bellatrixBlk, err := blk.PbBellatrixBlock() + if err != nil { + // ErrUnsupportedGetter means that we have another block type + if errors.Is(err, blocks.ErrUnsupportedGetter) { + if blindedBellatrixBlk, err := blk.PbBlindedBellatrixBlock(); err == nil { + if blindedBellatrixBlk == nil { + return nil, errNilBlock + } + signedFullBlock, err := bs.ExecutionPayloadReconstructor.ReconstructFullBellatrixBlock(ctx, blk) + if err != nil { + return nil, errors.Wrapf(err, "could not reconstruct full execution payload to create signed beacon block") + } + bellatrixBlk, err = signedFullBlock.PbBellatrixBlock() + if err != nil { + return nil, errors.Wrapf(err, "could not get signed beacon block") + } + v2Blk, err := migration.V1Alpha1BeaconBlockBellatrixToV2(bellatrixBlk.Block) + if err != nil { + return nil, errors.Wrapf(err, "could not convert signed beacon block") + } + root, err := blk.Block().HashTreeRoot() + if err != nil { + return nil, errors.Wrapf(err, "could not get block root") + } + isOptimistic, err := bs.OptimisticModeFetcher.IsOptimisticForRoot(ctx, root) + if err != nil { + return nil, errors.Wrapf(err, "could not check if block is optimistic") + } + sig := blk.Signature() + data := ðpbv2.SignedBeaconBlockBellatrix{ + Message: v2Blk, + Signature: sig[:], + } + sszData, err := data.MarshalSSZ() + if err != nil { + return nil, errors.Wrapf(err, "could not marshal block into SSZ") + } + return ðpbv2.SSZContainer{ + Version: ethpbv2.Version_BELLATRIX, + ExecutionOptimistic: isOptimistic, + Data: sszData, + }, nil + } + return nil, err + } + return nil, err + } + + if bellatrixBlk == nil { + return nil, errNilBlock + } + v2Blk, err := migration.V1Alpha1BeaconBlockBellatrixToV2(bellatrixBlk.Block) + if err != nil { + return nil, errors.Wrapf(err, "could not convert signed beacon block") + } + root, err := blk.Block().HashTreeRoot() + if err != nil { + return nil, errors.Wrapf(err, "could not get block root") + } + isOptimistic, err := bs.OptimisticModeFetcher.IsOptimisticForRoot(ctx, root) + if err != nil { + return nil, errors.Wrapf(err, "could not check if block is optimistic") + } + sig := blk.Signature() + data := ðpbv2.SignedBeaconBlockBellatrix{ + Message: v2Blk, + Signature: sig[:], + } + sszData, err := data.MarshalSSZ() + if err != nil { + return nil, errors.Wrapf(err, "could not marshal block into SSZ") + } + return ðpbv2.SSZContainer{Version: ethpbv2.Version_BELLATRIX, ExecutionOptimistic: isOptimistic, Data: sszData}, nil +} + +func (bs *Server) getSSZBlockCapella(ctx context.Context, blk interfaces.SignedBeaconBlock) (*ethpbv2.SSZContainer, error) { + capellaBlk, err := blk.PbCapellaBlock() + if err != nil { + // ErrUnsupportedGetter means that we have another block type + if errors.Is(err, blocks.ErrUnsupportedGetter) { + if blindedCapellaBlk, err := blk.PbBlindedCapellaBlock(); err == nil { + if blindedCapellaBlk == nil { + return nil, errNilBlock + } + signedFullBlock, err := bs.ExecutionPayloadReconstructor.ReconstructFullBellatrixBlock(ctx, blk) + if err != nil { + return nil, errors.Wrapf(err, "could not reconstruct full execution payload to create signed beacon block") + } + capellaBlk, err = signedFullBlock.PbCapellaBlock() + if err != nil { + return nil, errors.Wrapf(err, "could not get signed beacon block") + } + v2Blk, err := migration.V1Alpha1BeaconBlockCapellaToV2(capellaBlk.Block) + if err != nil { + return nil, errors.Wrapf(err, "could not convert signed beacon block") + } + root, err := blk.Block().HashTreeRoot() + if err != nil { + return nil, errors.Wrapf(err, "could not get block root") + } + isOptimistic, err := bs.OptimisticModeFetcher.IsOptimisticForRoot(ctx, root) + if err != nil { + return nil, errors.Wrapf(err, "could not check if block is optimistic") + } + sig := blk.Signature() + data := ðpbv2.SignedBeaconBlockCapella{ + Message: v2Blk, + Signature: sig[:], + } + sszData, err := data.MarshalSSZ() + if err != nil { + return nil, errors.Wrapf(err, "could not marshal block into SSZ") + } + return ðpbv2.SSZContainer{ + Version: ethpbv2.Version_CAPELLA, + ExecutionOptimistic: isOptimistic, + Data: sszData, + }, nil + } + return nil, err + } + return nil, err + } + + if capellaBlk == nil { + return nil, errNilBlock + } + v2Blk, err := migration.V1Alpha1BeaconBlockCapellaToV2(capellaBlk.Block) + if err != nil { + return nil, errors.Wrapf(err, "could not convert signed beacon block") + } + root, err := blk.Block().HashTreeRoot() + if err != nil { + return nil, errors.Wrapf(err, "could not get block root") + } + isOptimistic, err := bs.OptimisticModeFetcher.IsOptimisticForRoot(ctx, root) + if err != nil { + return nil, errors.Wrapf(err, "could not check if block is optimistic") + } + sig := blk.Signature() + data := ðpbv2.SignedBeaconBlockCapella{ + Message: v2Blk, + Signature: sig[:], + } + sszData, err := data.MarshalSSZ() + if err != nil { + return nil, errors.Wrapf(err, "could not marshal block into SSZ") + } + return ðpbv2.SSZContainer{Version: ethpbv2.Version_CAPELLA, ExecutionOptimistic: isOptimistic, Data: sszData}, nil +} + func (bs *Server) submitPhase0Block(ctx context.Context, phase0Blk *ethpbv1.BeaconBlock, sig []byte) error { v1alpha1Blk, err := migration.V1ToV1Alpha1SignedBlock(ðpbv1.SignedBeaconBlock{Block: phase0Blk, Signature: sig}) if err != nil { diff --git a/beacon-chain/rpc/eth/beacon/blocks_test.go b/beacon-chain/rpc/eth/beacon/blocks_test.go index 9635816f32ff..0d56e0cdec46 100644 --- a/beacon-chain/rpc/eth/beacon/blocks_test.go +++ b/beacon-chain/rpc/eth/beacon/blocks_test.go @@ -1910,6 +1910,44 @@ func TestServer_GetBlockSSZV2(t *testing.T) { assert.DeepEqual(t, sszBlock, resp.Data) assert.Equal(t, ethpbv2.Version_BELLATRIX, resp.Version) }) + + t.Run("Capella", func(t *testing.T) { + beaconDB := dbTest.SetupDB(t) + ctx := context.Background() + + _, blkContainers := fillDBTestBlocksCapella(ctx, t, beaconDB) + headBlock := blkContainers[len(blkContainers)-1] + + b2 := util.NewBeaconBlockCapella() + b2.Block.Slot = 30 + b2.Block.ParentRoot = bytesutil.PadTo([]byte{1}, 32) + util.SaveBlock(t, ctx, beaconDB, b2) + + chainBlk, err := blocks.NewSignedBeaconBlock(headBlock.GetCapellaBlock()) + require.NoError(t, err) + bs := &Server{ + BeaconDB: beaconDB, + ChainInfoFetcher: &mock.ChainService{ + DB: beaconDB, + Block: chainBlk, + Root: headBlock.BlockRoot, + FinalizedCheckPoint: ðpbalpha.Checkpoint{Root: blkContainers[64].BlockRoot}, + }, + OptimisticModeFetcher: &mock.ChainService{}, + } + + blks, err := beaconDB.BlocksBySlot(ctx, 30) + require.Equal(t, true, len(blks) > 0) + require.NoError(t, err) + sszBlock, err := blks[0].MarshalSSZ() + require.NoError(t, err) + + resp, err := bs.GetBlockSSZV2(ctx, ðpbv2.BlockRequestV2{BlockId: []byte("30")}) + require.NoError(t, err) + assert.NotNil(t, resp) + assert.DeepEqual(t, sszBlock, resp.Data) + assert.Equal(t, ethpbv2.Version_CAPELLA, resp.Version) + }) } func TestServer_GetBlockRoot(t *testing.T) { From 7706221ca1581259756a6006f1478d17a20f6699 Mon Sep 17 00:00:00 2001 From: rkapka Date: Tue, 29 Nov 2022 17:31:26 +0100 Subject: [PATCH 08/18] test other functions (cherry picked from commit 31d4a4cd1165b882d823696e5983ac6635262ec2) --- beacon-chain/rpc/eth/beacon/BUILD.bazel | 1 + beacon-chain/rpc/eth/beacon/blocks_test.go | 228 ++++++++++++++++++++- testing/util/block.go | 67 ++++++ testing/util/merge.go | 5 + 4 files changed, 293 insertions(+), 8 deletions(-) diff --git a/beacon-chain/rpc/eth/beacon/BUILD.bazel b/beacon-chain/rpc/eth/beacon/BUILD.bazel index 080ff99c8292..3c9308a1a379 100644 --- a/beacon-chain/rpc/eth/beacon/BUILD.bazel +++ b/beacon-chain/rpc/eth/beacon/BUILD.bazel @@ -106,6 +106,7 @@ go_test( "//consensus-types/interfaces:go_default_library", "//consensus-types/primitives:go_default_library", "//crypto/bls:go_default_library", + "//crypto/hash:go_default_library", "//encoding/bytesutil:go_default_library", "//encoding/ssz:go_default_library", "//proto/engine/v1:go_default_library", diff --git a/beacon-chain/rpc/eth/beacon/blocks_test.go b/beacon-chain/rpc/eth/beacon/blocks_test.go index 0d56e0cdec46..266e453966d9 100644 --- a/beacon-chain/rpc/eth/beacon/blocks_test.go +++ b/beacon-chain/rpc/eth/beacon/blocks_test.go @@ -18,6 +18,7 @@ import ( "github.com/prysmaticlabs/prysm/v3/consensus-types/blocks" "github.com/prysmaticlabs/prysm/v3/consensus-types/interfaces" types "github.com/prysmaticlabs/prysm/v3/consensus-types/primitives" + "github.com/prysmaticlabs/prysm/v3/crypto/hash" "github.com/prysmaticlabs/prysm/v3/encoding/bytesutil" "github.com/prysmaticlabs/prysm/v3/encoding/ssz" enginev1 "github.com/prysmaticlabs/prysm/v3/proto/engine/v1" @@ -663,6 +664,44 @@ func TestServer_SubmitBlock_OK(t *testing.T) { _, err = beaconChainServer.SubmitBlock(context.Background(), blockReq) assert.NoError(t, err, "Could not propose block correctly") }) + + t.Run("Capella", func(t *testing.T) { + beaconDB := dbTest.SetupDB(t) + ctx := context.Background() + + genesis := util.NewBeaconBlockCapella() + util.SaveBlock(t, context.Background(), beaconDB, genesis) + + numDeposits := uint64(64) + beaconState, _ := util.DeterministicGenesisState(t, numDeposits) + bsRoot, err := beaconState.HashTreeRoot(ctx) + require.NoError(t, err) + genesisRoot, err := genesis.Block.HashTreeRoot() + require.NoError(t, err) + require.NoError(t, beaconDB.SaveState(ctx, beaconState, genesisRoot), "Could not save genesis state") + + c := &mock.ChainService{Root: bsRoot[:], State: beaconState} + beaconChainServer := &Server{ + BeaconDB: beaconDB, + BlockReceiver: c, + ChainInfoFetcher: c, + BlockNotifier: c.BlockNotifier(), + Broadcaster: mockp2p.NewTestP2P(t), + HeadFetcher: c, + } + req := util.NewBeaconBlockCapella() + req.Block.Slot = 5 + req.Block.ParentRoot = bsRoot[:] + v2Block, err := migration.V1Alpha1BeaconBlockCapellaToV2(req.Block) + require.NoError(t, err) + util.SaveBlock(t, ctx, beaconDB, req) + blockReq := ðpbv2.SignedBeaconBlockContainer{ + Message: ðpbv2.SignedBeaconBlockContainer_CapellaBlock{CapellaBlock: v2Block}, + Signature: req.Signature, + } + _, err = beaconChainServer.SubmitBlock(context.Background(), blockReq) + assert.NoError(t, err, "Could not propose block correctly") + }) } func TestServer_SubmitBlockSSZ_OK(t *testing.T) { @@ -747,13 +786,6 @@ func TestServer_SubmitBlockSSZ_OK(t *testing.T) { }) t.Run("Bellatrix", func(t *testing.T) { - // INFO: This code block can be removed once Bellatrix - // fork epoch is set to a value other than math.MaxUint64 - cfg := params.BeaconConfig() - cfg.BellatrixForkEpoch = cfg.AltairForkEpoch + 1000 - cfg.ForkVersionSchedule[bytesutil.ToBytes4(cfg.BellatrixForkVersion)] = cfg.AltairForkEpoch + 1000 - params.OverrideBeaconConfig(cfg) - beaconDB := dbTest.SetupDB(t) ctx := context.Background() @@ -792,6 +824,55 @@ func TestServer_SubmitBlockSSZ_OK(t *testing.T) { _, err = beaconChainServer.SubmitBlockSSZ(sszCtx, blockReq) assert.NoError(t, err, "Could not propose block correctly") }) + + t.Run("Capella", func(t *testing.T) { + t.Skip("This test needs Capella fork version configured properly") + + // INFO: This code block can be removed once Capella + // fork epoch is set to a value other than math.MaxUint64 + cfg := params.BeaconConfig() + cfg.CapellaForkEpoch = cfg.BellatrixForkEpoch + 1000 + cfg.ForkVersionSchedule[bytesutil.ToBytes4(cfg.CapellaForkVersion)] = cfg.BellatrixForkEpoch + 1000 + params.OverrideBeaconConfig(cfg) + + beaconDB := dbTest.SetupDB(t) + ctx := context.Background() + + genesis := util.NewBeaconBlockCapella() + util.SaveBlock(t, context.Background(), beaconDB, genesis) + + numDeposits := uint64(64) + beaconState, _ := util.DeterministicGenesisState(t, numDeposits) + bsRoot, err := beaconState.HashTreeRoot(ctx) + require.NoError(t, err) + genesisRoot, err := genesis.Block.HashTreeRoot() + require.NoError(t, err) + require.NoError(t, beaconDB.SaveState(ctx, beaconState, genesisRoot), "Could not save genesis state") + + c := &mock.ChainService{Root: bsRoot[:], State: beaconState} + beaconChainServer := &Server{ + BeaconDB: beaconDB, + BlockReceiver: c, + ChainInfoFetcher: c, + BlockNotifier: c.BlockNotifier(), + Broadcaster: mockp2p.NewTestP2P(t), + HeadFetcher: c, + } + req := util.NewBeaconBlockCapella() + req.Block.Slot = params.BeaconConfig().SlotsPerEpoch.Mul(uint64(params.BeaconConfig().CapellaForkEpoch)) + req.Block.ParentRoot = bsRoot[:] + util.SaveBlock(t, ctx, beaconDB, req) + blockSsz, err := req.MarshalSSZ() + require.NoError(t, err) + blockReq := ðpbv2.SSZContainer{ + Data: blockSsz, + } + md := metadata.MD{} + md.Set(versionHeader, "capella") + sszCtx := metadata.NewIncomingContext(ctx, md) + _, err = beaconChainServer.SubmitBlockSSZ(sszCtx, blockReq) + assert.NoError(t, err, "Could not propose block correctly") + }) } func TestServer_SubmitBlindedBlockSSZ_OK(t *testing.T) { @@ -930,6 +1011,64 @@ func TestServer_SubmitBlindedBlockSSZ_OK(t *testing.T) { _, err = beaconChainServer.SubmitBlindedBlockSSZ(sszCtx, blockReq) assert.NoError(t, err, "Could not propose block correctly") }) + + t.Run("Capella", func(t *testing.T) { + t.Skip("This test needs Capella fork version configured properly") + + // INFO: This code block can be removed once Capella + // fork epoch is set to a value other than math.MaxUint64 + params.SetupTestConfigCleanup(t) + cfg := params.BeaconConfig() + cfg.CapellaForkEpoch = cfg.BellatrixForkEpoch + 1000 + cfg.ForkVersionSchedule[bytesutil.ToBytes4(cfg.CapellaForkVersion)] = cfg.BellatrixForkEpoch + 1000 + params.OverrideBeaconConfig(cfg) + + beaconDB := dbTest.SetupDB(t) + ctx := context.Background() + + genesis := util.NewBeaconBlockCapella() + util.SaveBlock(t, context.Background(), beaconDB, genesis) + + numDeposits := uint64(64) + beaconState, _ := util.DeterministicGenesisState(t, numDeposits) + bsRoot, err := beaconState.HashTreeRoot(ctx) + require.NoError(t, err) + genesisRoot, err := genesis.Block.HashTreeRoot() + require.NoError(t, err) + require.NoError(t, beaconDB.SaveState(ctx, beaconState, genesisRoot), "Could not save genesis state") + + c := &mock.ChainService{Root: bsRoot[:], State: beaconState} + alphaServer := &validator.Server{ + SyncCommitteePool: synccommittee.NewStore(), + P2P: &mockp2p.MockBroadcaster{}, + BlockBuilder: &builderTest.MockBuilderService{}, + BlockReceiver: c, + BlockNotifier: &mock.MockBlockNotifier{}, + } + beaconChainServer := &Server{ + BeaconDB: beaconDB, + BlockReceiver: c, + ChainInfoFetcher: c, + BlockNotifier: c.BlockNotifier(), + Broadcaster: mockp2p.NewTestP2P(t), + HeadFetcher: c, + V1Alpha1ValidatorServer: alphaServer, + } + req := util.NewBlindedBeaconBlockCapella() + req.Block.Slot = params.BeaconConfig().SlotsPerEpoch.Mul(uint64(params.BeaconConfig().CapellaForkEpoch)) + req.Block.ParentRoot = bsRoot[:] + util.SaveBlock(t, ctx, beaconDB, req) + blockSsz, err := req.MarshalSSZ() + require.NoError(t, err) + blockReq := ðpbv2.SSZContainer{ + Data: blockSsz, + } + md := metadata.MD{} + md.Set(versionHeader, "capella") + sszCtx := metadata.NewIncomingContext(ctx, md) + _, err = beaconChainServer.SubmitBlindedBlockSSZ(sszCtx, blockReq) + assert.NoError(t, err, "Could not propose block correctly") + }) } func TestSubmitBlindedBlock(t *testing.T) { @@ -1007,7 +1146,7 @@ func TestSubmitBlindedBlock(t *testing.T) { assert.NoError(t, err, "Could not propose block correctly") }) - t.Run("Bellatrix - OK", func(t *testing.T) { + t.Run("Bellatrix", func(t *testing.T) { transactions := [][]byte{[]byte("transaction1"), []byte("transaction2")} transactionsRoot, err := ssz.TransactionsRoot(transactions) require.NoError(t, err) @@ -1060,6 +1199,79 @@ func TestSubmitBlindedBlock(t *testing.T) { _, err = beaconChainServer.SubmitBlindedBlock(context.Background(), blockReq) assert.NoError(t, err) }) + + t.Run("Capella", func(t *testing.T) { + transactions := [][]byte{[]byte("transaction1"), []byte("transaction2")} + transactionsRoot, err := ssz.TransactionsRoot(transactions) + require.NoError(t, err) + + withdrawals := []*enginev1.Withdrawal{ + { + Index: 1, + ValidatorIndex: 1, + Address: bytesutil.PadTo([]byte("address1"), 20), + Amount: 1, + }, + { + Index: 2, + ValidatorIndex: 2, + Address: bytesutil.PadTo([]byte("address2"), 20), + Amount: 2, + }, + } + withdrawalsRoot, err := ssz.WithdrawalSliceRoot(hash.CustomSHA256Hasher(), withdrawals, 16) + require.NoError(t, err) + + beaconDB := dbTest.SetupDB(t) + ctx := context.Background() + + genesis := util.NewBeaconBlockCapella() + util.SaveBlock(t, context.Background(), beaconDB, genesis) + + numDeposits := uint64(64) + beaconState, _ := util.DeterministicGenesisState(t, numDeposits) + bsRoot, err := beaconState.HashTreeRoot(ctx) + require.NoError(t, err) + genesisRoot, err := genesis.Block.HashTreeRoot() + require.NoError(t, err) + require.NoError(t, beaconDB.SaveState(ctx, beaconState, genesisRoot), "Could not save genesis state") + + c := &mock.ChainService{Root: bsRoot[:], State: beaconState} + alphaServer := &validator.Server{ + SyncCommitteePool: synccommittee.NewStore(), + P2P: &mockp2p.MockBroadcaster{}, + BlockBuilder: &builderTest.MockBuilderService{}, + BlockReceiver: c, + BlockNotifier: &mock.MockBlockNotifier{}, + } + beaconChainServer := &Server{ + BeaconDB: beaconDB, + BlockReceiver: c, + ChainInfoFetcher: c, + BlockNotifier: c.BlockNotifier(), + Broadcaster: mockp2p.NewTestP2P(t), + V1Alpha1ValidatorServer: alphaServer, + } + + blk := util.NewBeaconBlockCapella() + blk.Block.Slot = 5 + blk.Block.ParentRoot = bsRoot[:] + blk.Block.Body.ExecutionPayload.Transactions = transactions + blk.Block.Body.ExecutionPayload.Withdrawals = withdrawals + blindedBlk := util.NewBlindedBeaconBlockCapellaV2() + blindedBlk.Message.Slot = 5 + blindedBlk.Message.ParentRoot = bsRoot[:] + blindedBlk.Message.Body.ExecutionPayloadHeader.TransactionsRoot = transactionsRoot[:] + blindedBlk.Message.Body.ExecutionPayloadHeader.WithdrawalsRoot = withdrawalsRoot[:] + util.SaveBlock(t, ctx, beaconDB, blk) + + blockReq := ðpbv2.SignedBlindedBeaconBlockContainer{ + Message: ðpbv2.SignedBlindedBeaconBlockContainer_CapellaBlock{CapellaBlock: blindedBlk.Message}, + Signature: blindedBlk.Signature, + } + _, err = beaconChainServer.SubmitBlindedBlock(context.Background(), blockReq) + assert.NoError(t, err) + }) } func TestServer_GetBlock(t *testing.T) { diff --git a/testing/util/block.go b/testing/util/block.go index 97c5200fa4ca..110bd2cae585 100644 --- a/testing/util/block.go +++ b/testing/util/block.go @@ -1024,6 +1024,73 @@ func HydrateBlindedBeaconBlockBodyCapella(b *ethpb.BlindedBeaconBlockBodyCapella return b } +// HydrateV2SignedBlindedBeaconBlockCapella hydrates a signed blinded beacon block with correct field length sizes +// to comply with fssz marshalling and unmarshalling rules. +func HydrateV2SignedBlindedBeaconBlockCapella(b *v2.SignedBlindedBeaconBlockCapella) *v2.SignedBlindedBeaconBlockCapella { + if b.Signature == nil { + b.Signature = make([]byte, fieldparams.BLSSignatureLength) + } + b.Message = HydrateV2BlindedBeaconBlockCapella(b.Message) + return b +} + +// HydrateV2BlindedBeaconBlockCapella hydrates a blinded beacon block with correct field length sizes +// to comply with fssz marshalling and unmarshalling rules. +func HydrateV2BlindedBeaconBlockCapella(b *v2.BlindedBeaconBlockCapella) *v2.BlindedBeaconBlockCapella { + if b == nil { + b = &v2.BlindedBeaconBlockCapella{} + } + if b.ParentRoot == nil { + b.ParentRoot = make([]byte, fieldparams.RootLength) + } + if b.StateRoot == nil { + b.StateRoot = make([]byte, fieldparams.RootLength) + } + b.Body = HydrateV2BlindedBeaconBlockBodyCapella(b.Body) + return b +} + +// HydrateV2BlindedBeaconBlockBodyCapella hydrates a blinded beacon block body with correct field length sizes +// to comply with fssz marshalling and unmarshalling rules. +func HydrateV2BlindedBeaconBlockBodyCapella(b *v2.BlindedBeaconBlockBodyCapella) *v2.BlindedBeaconBlockBodyCapella { + if b == nil { + b = &v2.BlindedBeaconBlockBodyCapella{} + } + if b.RandaoReveal == nil { + b.RandaoReveal = make([]byte, fieldparams.BLSSignatureLength) + } + if b.Graffiti == nil { + b.Graffiti = make([]byte, 32) + } + if b.Eth1Data == nil { + b.Eth1Data = &v1.Eth1Data{ + DepositRoot: make([]byte, fieldparams.RootLength), + BlockHash: make([]byte, 32), + } + } + if b.SyncAggregate == nil { + b.SyncAggregate = &v1.SyncAggregate{ + SyncCommitteeBits: make([]byte, 64), + SyncCommitteeSignature: make([]byte, fieldparams.BLSSignatureLength), + } + } + if b.ExecutionPayloadHeader == nil { + b.ExecutionPayloadHeader = &enginev1.ExecutionPayloadHeaderCapella{ + ParentHash: make([]byte, 32), + FeeRecipient: make([]byte, 20), + StateRoot: make([]byte, fieldparams.RootLength), + ReceiptsRoot: make([]byte, fieldparams.RootLength), + LogsBloom: make([]byte, 256), + PrevRandao: make([]byte, 32), + BaseFeePerGas: make([]byte, 32), + BlockHash: make([]byte, 32), + TransactionsRoot: make([]byte, fieldparams.RootLength), + WithdrawalsRoot: make([]byte, fieldparams.RootLength), + } + } + return b +} + func SaveBlock(tb assertions.AssertionTestingTB, ctx context.Context, db iface.NoHeadAccessDatabase, b interface{}) interfaces.SignedBeaconBlock { wsb, err := blocks.NewSignedBeaconBlock(b) require.NoError(tb, err) diff --git a/testing/util/merge.go b/testing/util/merge.go index 0ae7a1ba1579..051617299257 100644 --- a/testing/util/merge.go +++ b/testing/util/merge.go @@ -29,3 +29,8 @@ func NewBeaconBlockCapella() *ethpb.SignedBeaconBlockCapella { func NewBlindedBeaconBlockCapella() *ethpb.SignedBlindedBeaconBlockCapella { return HydrateSignedBlindedBeaconBlockCapella(ðpb.SignedBlindedBeaconBlockCapella{}) } + +// NewBlindedBeaconBlockCapellaV2 creates a blinded beacon block with minimum marshalable fields. +func NewBlindedBeaconBlockCapellaV2() *v2.SignedBlindedBeaconBlockCapella { + return HydrateV2SignedBlindedBeaconBlockCapella(&v2.SignedBlindedBeaconBlockCapella{}) +} From 4fad0e06f1c87eae67edcf74f7dfddd26d8a4457 Mon Sep 17 00:00:00 2001 From: rkapka Date: Tue, 29 Nov 2022 17:35:03 +0100 Subject: [PATCH 09/18] move stuff to blinded_blocks.go (cherry picked from commit 0a9e1658ddb28f45ae5c1cb9fc2703cbb8c6708d) # Conflicts: # beacon-chain/rpc/eth/beacon/blocks.go --- beacon-chain/rpc/eth/beacon/blinded_blocks.go | 150 +++++++ .../rpc/eth/beacon/blinded_blocks_test.go | 407 ++++++++++++++++++ beacon-chain/rpc/eth/beacon/blocks.go | 117 +---- beacon-chain/rpc/eth/beacon/blocks_test.go | 404 ----------------- 4 files changed, 569 insertions(+), 509 deletions(-) diff --git a/beacon-chain/rpc/eth/beacon/blinded_blocks.go b/beacon-chain/rpc/eth/beacon/blinded_blocks.go index c347dd2d5074..a5f31448f633 100644 --- a/beacon-chain/rpc/eth/beacon/blinded_blocks.go +++ b/beacon-chain/rpc/eth/beacon/blinded_blocks.go @@ -4,14 +4,20 @@ import ( "context" "github.com/pkg/errors" + "github.com/prysmaticlabs/prysm/v3/config/params" "github.com/prysmaticlabs/prysm/v3/consensus-types/blocks" "github.com/prysmaticlabs/prysm/v3/consensus-types/interfaces" + "github.com/prysmaticlabs/prysm/v3/encoding/ssz/detect" + "github.com/prysmaticlabs/prysm/v3/network/forks" ethpbv1 "github.com/prysmaticlabs/prysm/v3/proto/eth/v1" ethpbv2 "github.com/prysmaticlabs/prysm/v3/proto/eth/v2" "github.com/prysmaticlabs/prysm/v3/proto/migration" + eth "github.com/prysmaticlabs/prysm/v3/proto/prysm/v1alpha1" "go.opencensus.io/trace" "google.golang.org/grpc/codes" + "google.golang.org/grpc/metadata" "google.golang.org/grpc/status" + "google.golang.org/protobuf/types/known/emptypb" ) // GetBlindedBlock retrieves blinded block for given block id. @@ -108,6 +114,112 @@ func (bs *Server) GetBlindedBlockSSZ(ctx context.Context, req *ethpbv1.BlockRequ return nil, status.Errorf(codes.Internal, "Unknown block type %T", blk) } +// SubmitBlindedBlock instructs the beacon node to use the components of the `SignedBlindedBeaconBlock` to construct +// and publish a `SignedBeaconBlock` by swapping out the `transactions_root` for the corresponding full list of `transactions`. +// The beacon node should broadcast a newly constructed `SignedBeaconBlock` to the beacon network, +// to be included in the beacon chain. The beacon node is not required to validate the signed +// `BeaconBlock`, and a successful response (20X) only indicates that the broadcast has been +// successful. The beacon node is expected to integrate the new block into its state, and +// therefore validate the block internally, however blocks which fail the validation are still +// broadcast but a different status code is returned (202). +func (bs *Server) SubmitBlindedBlock(ctx context.Context, req *ethpbv2.SignedBlindedBeaconBlockContainer) (*emptypb.Empty, error) { + ctx, span := trace.StartSpan(ctx, "beacon.SubmitBlindedBlock") + defer span.End() + + capellaBlkContainer, ok := req.Message.(*ethpbv2.SignedBlindedBeaconBlockContainer_CapellaBlock) + if ok { + if err := bs.submitBlindedCapellaBlock(ctx, capellaBlkContainer.CapellaBlock, req.Signature); err != nil { + return nil, err + } + } + + bellatrixBlkContainer, ok := req.Message.(*ethpbv2.SignedBlindedBeaconBlockContainer_BellatrixBlock) + if ok { + if err := bs.submitBlindedBellatrixBlock(ctx, bellatrixBlkContainer.BellatrixBlock, req.Signature); err != nil { + return nil, err + } + } + + // At the end we check forks that don't have blinded blocks. + phase0BlkContainer, ok := req.Message.(*ethpbv2.SignedBlindedBeaconBlockContainer_Phase0Block) + if ok { + if err := bs.submitPhase0Block(ctx, phase0BlkContainer.Phase0Block, req.Signature); err != nil { + return nil, err + } + } + altairBlkContainer, ok := req.Message.(*ethpbv2.SignedBlindedBeaconBlockContainer_AltairBlock) + if ok { + if err := bs.submitAltairBlock(ctx, altairBlkContainer.AltairBlock, req.Signature); err != nil { + return nil, err + } + } + + return &emptypb.Empty{}, nil +} + +// SubmitBlindedBlockSSZ instructs the beacon node to use the components of the `SignedBlindedBeaconBlock` to construct +// and publish a `SignedBeaconBlock` by swapping out the `transactions_root` for the corresponding full list of `transactions`. +// The beacon node should broadcast a newly constructed `SignedBeaconBlock` to the beacon network, +// to be included in the beacon chain. The beacon node is not required to validate the signed +// `BeaconBlock`, and a successful response (20X) only indicates that the broadcast has been +// successful. The beacon node is expected to integrate the new block into its state, and +// therefore validate the block internally, however blocks which fail the validation are still +// broadcast but a different status code is returned (202). +// +// The provided block must be SSZ-serialized. +func (bs *Server) SubmitBlindedBlockSSZ(ctx context.Context, req *ethpbv2.SSZContainer) (*emptypb.Empty, error) { + ctx, span := trace.StartSpan(ctx, "beacon.SubmitBlindedBlockSSZ") + defer span.End() + + md, ok := metadata.FromIncomingContext(ctx) + if !ok { + return &emptypb.Empty{}, status.Errorf(codes.Internal, "Could not read"+versionHeader+" header") + } + ver := md.Get(versionHeader) + if len(ver) == 0 { + return &emptypb.Empty{}, status.Errorf(codes.Internal, "Could not read"+versionHeader+" header") + } + schedule := forks.NewOrderedSchedule(params.BeaconConfig()) + forkVer, err := schedule.VersionForName(ver[0]) + if err != nil { + return &emptypb.Empty{}, status.Errorf(codes.Internal, "Could not determine fork version: %v", err) + } + unmarshaler, err := detect.FromForkVersion(forkVer) + if err != nil { + return &emptypb.Empty{}, status.Errorf(codes.Internal, "Could not create unmarshaler: %v", err) + } + block, err := unmarshaler.UnmarshalBlindedBeaconBlock(req.Data) + if err != nil { + return &emptypb.Empty{}, status.Errorf(codes.Internal, "Could not unmarshal request data into block: %v", err) + } + + if block.IsBlinded() { + b, err := block.PbBlindedBellatrixBlock() + if err != nil { + b, err := block.PbBlindedCapellaBlock() + if err != nil { + return &emptypb.Empty{}, status.Errorf(codes.Internal, "Could not get blinded block: %v", err) + } + bb, err := migration.V1Alpha1BeaconBlockBlindedCapellaToV2Blinded(b.Block) + if err != nil { + return &emptypb.Empty{}, status.Errorf(codes.Internal, "Could not migrate block: %v", err) + } + return &emptypb.Empty{}, bs.submitBlindedCapellaBlock(ctx, bb, b.Signature) + } + bb, err := migration.V1Alpha1BeaconBlockBlindedBellatrixToV2Blinded(b.Block) + if err != nil { + return &emptypb.Empty{}, status.Errorf(codes.Internal, "Could not migrate block: %v", err) + } + return &emptypb.Empty{}, bs.submitBlindedBellatrixBlock(ctx, bb, b.Signature) + } + + root, err := block.Block().HashTreeRoot() + if err != nil { + return &emptypb.Empty{}, status.Errorf(codes.Internal, "Could not compute block's hash tree root: %v", err) + } + return &emptypb.Empty{}, bs.submitBlock(ctx, root, block) +} + func getBlindedBlockPhase0(blk interfaces.SignedBeaconBlock) (*ethpbv2.BlindedBlockResponse, error) { phase0Blk, err := blk.PbPhase0Block() if err != nil { @@ -442,3 +554,41 @@ func (bs *Server) getBlindedSSZBlockCapella(ctx context.Context, blk interfaces. } return ðpbv2.SSZContainer{Version: ethpbv2.Version_CAPELLA, ExecutionOptimistic: isOptimistic, Data: sszData}, nil } + +func (bs *Server) submitBlindedBellatrixBlock(ctx context.Context, blindedBellatrixBlk *ethpbv2.BlindedBeaconBlockBellatrix, sig []byte) error { + b, err := migration.BlindedBellatrixToV1Alpha1SignedBlock(ðpbv2.SignedBlindedBeaconBlockBellatrix{ + Message: blindedBellatrixBlk, + Signature: sig, + }) + if err != nil { + return status.Errorf(codes.Internal, "Could not get blinded block: %v", err) + } + _, err = bs.V1Alpha1ValidatorServer.ProposeBeaconBlock(ctx, ð.GenericSignedBeaconBlock{ + Block: ð.GenericSignedBeaconBlock_BlindedBellatrix{ + BlindedBellatrix: b, + }, + }) + if err != nil { + return status.Errorf(codes.Internal, "Could not propose blinded block: %v", err) + } + return nil +} + +func (bs *Server) submitBlindedCapellaBlock(ctx context.Context, blindedCapellaBlk *ethpbv2.BlindedBeaconBlockCapella, sig []byte) error { + b, err := migration.BlindedCapellaToV1Alpha1SignedBlock(ðpbv2.SignedBlindedBeaconBlockCapella{ + Message: blindedCapellaBlk, + Signature: sig, + }) + if err != nil { + return status.Errorf(codes.Internal, "Could not get blinded block: %v", err) + } + _, err = bs.V1Alpha1ValidatorServer.ProposeBeaconBlock(ctx, ð.GenericSignedBeaconBlock{ + Block: ð.GenericSignedBeaconBlock_BlindedCapella{ + BlindedCapella: b, + }, + }) + if err != nil { + return status.Errorf(codes.Internal, "Could not propose blinded block: %v", err) + } + return nil +} diff --git a/beacon-chain/rpc/eth/beacon/blinded_blocks_test.go b/beacon-chain/rpc/eth/beacon/blinded_blocks_test.go index 199317015847..71738cda9af2 100644 --- a/beacon-chain/rpc/eth/beacon/blinded_blocks_test.go +++ b/beacon-chain/rpc/eth/beacon/blinded_blocks_test.go @@ -7,10 +7,17 @@ import ( "testing" mock "github.com/prysmaticlabs/prysm/v3/beacon-chain/blockchain/testing" + builderTest "github.com/prysmaticlabs/prysm/v3/beacon-chain/builder/testing" dbTest "github.com/prysmaticlabs/prysm/v3/beacon-chain/db/testing" executionTest "github.com/prysmaticlabs/prysm/v3/beacon-chain/execution/testing" + "github.com/prysmaticlabs/prysm/v3/beacon-chain/operations/synccommittee" + mockp2p "github.com/prysmaticlabs/prysm/v3/beacon-chain/p2p/testing" + "github.com/prysmaticlabs/prysm/v3/beacon-chain/rpc/prysm/v1alpha1/validator" + "github.com/prysmaticlabs/prysm/v3/config/params" "github.com/prysmaticlabs/prysm/v3/consensus-types/blocks" + "github.com/prysmaticlabs/prysm/v3/crypto/hash" "github.com/prysmaticlabs/prysm/v3/encoding/bytesutil" + "github.com/prysmaticlabs/prysm/v3/encoding/ssz" enginev1 "github.com/prysmaticlabs/prysm/v3/proto/engine/v1" ethpbv1 "github.com/prysmaticlabs/prysm/v3/proto/eth/v1" ethpbv2 "github.com/prysmaticlabs/prysm/v3/proto/eth/v2" @@ -19,6 +26,7 @@ import ( "github.com/prysmaticlabs/prysm/v3/testing/assert" "github.com/prysmaticlabs/prysm/v3/testing/require" "github.com/prysmaticlabs/prysm/v3/testing/util" + "google.golang.org/grpc/metadata" ) func TestServer_GetBlindedBlock(t *testing.T) { @@ -745,3 +753,402 @@ func TestServer_GetBlindedBlockSSZ(t *testing.T) { assert.Equal(t, ethpbv2.Version_CAPELLA, resp.Version) }) } + +func TestServer_SubmitBlindedBlockSSZ_OK(t *testing.T) { + t.Run("Phase 0", func(t *testing.T) { + beaconDB := dbTest.SetupDB(t) + ctx := context.Background() + + genesis := util.NewBeaconBlock() + util.SaveBlock(t, context.Background(), beaconDB, genesis) + + numDeposits := uint64(64) + beaconState, _ := util.DeterministicGenesisState(t, numDeposits) + bsRoot, err := beaconState.HashTreeRoot(ctx) + require.NoError(t, err) + genesisRoot, err := genesis.Block.HashTreeRoot() + require.NoError(t, err) + require.NoError(t, beaconDB.SaveState(ctx, beaconState, genesisRoot), "Could not save genesis state") + + c := &mock.ChainService{Root: bsRoot[:], State: beaconState} + beaconChainServer := &Server{ + BeaconDB: beaconDB, + BlockReceiver: c, + ChainInfoFetcher: c, + BlockNotifier: c.BlockNotifier(), + Broadcaster: mockp2p.NewTestP2P(t), + HeadFetcher: c, + } + req := util.NewBeaconBlock() + req.Block.Slot = 5 + req.Block.ParentRoot = bsRoot[:] + util.SaveBlock(t, ctx, beaconDB, req) + blockSsz, err := req.MarshalSSZ() + require.NoError(t, err) + blockReq := ðpbv2.SSZContainer{ + Data: blockSsz, + } + md := metadata.MD{} + md.Set(versionHeader, "phase0") + sszCtx := metadata.NewIncomingContext(ctx, md) + _, err = beaconChainServer.SubmitBlindedBlockSSZ(sszCtx, blockReq) + assert.NoError(t, err, "Could not propose block correctly") + }) + + t.Run("Altair", func(t *testing.T) { + beaconDB := dbTest.SetupDB(t) + ctx := context.Background() + + genesis := util.NewBeaconBlockAltair() + util.SaveBlock(t, context.Background(), beaconDB, genesis) + + numDeposits := uint64(64) + beaconState, _ := util.DeterministicGenesisState(t, numDeposits) + bsRoot, err := beaconState.HashTreeRoot(ctx) + require.NoError(t, err) + genesisRoot, err := genesis.Block.HashTreeRoot() + require.NoError(t, err) + require.NoError(t, beaconDB.SaveState(ctx, beaconState, genesisRoot), "Could not save genesis state") + + c := &mock.ChainService{Root: bsRoot[:], State: beaconState} + beaconChainServer := &Server{ + BeaconDB: beaconDB, + BlockReceiver: c, + ChainInfoFetcher: c, + BlockNotifier: c.BlockNotifier(), + Broadcaster: mockp2p.NewTestP2P(t), + HeadFetcher: c, + } + req := util.NewBeaconBlockAltair() + req.Block.Slot = params.BeaconConfig().SlotsPerEpoch.Mul(uint64(params.BeaconConfig().AltairForkEpoch)) + req.Block.ParentRoot = bsRoot[:] + util.SaveBlock(t, ctx, beaconDB, req) + blockSsz, err := req.MarshalSSZ() + require.NoError(t, err) + blockReq := ðpbv2.SSZContainer{ + Data: blockSsz, + } + md := metadata.MD{} + md.Set(versionHeader, "altair") + sszCtx := metadata.NewIncomingContext(ctx, md) + _, err = beaconChainServer.SubmitBlindedBlockSSZ(sszCtx, blockReq) + assert.NoError(t, err, "Could not propose block correctly") + }) + + t.Run("Bellatrix", func(t *testing.T) { + // INFO: This code block can be removed once Bellatrix + // fork epoch is set to a value other than math.MaxUint64 + params.SetupTestConfigCleanup(t) + cfg := params.BeaconConfig() + cfg.BellatrixForkEpoch = cfg.AltairForkEpoch + 1000 + cfg.ForkVersionSchedule[bytesutil.ToBytes4(cfg.BellatrixForkVersion)] = cfg.AltairForkEpoch + 1000 + params.OverrideBeaconConfig(cfg) + + beaconDB := dbTest.SetupDB(t) + ctx := context.Background() + + genesis := util.NewBeaconBlockBellatrix() + util.SaveBlock(t, context.Background(), beaconDB, genesis) + + numDeposits := uint64(64) + beaconState, _ := util.DeterministicGenesisState(t, numDeposits) + bsRoot, err := beaconState.HashTreeRoot(ctx) + require.NoError(t, err) + genesisRoot, err := genesis.Block.HashTreeRoot() + require.NoError(t, err) + require.NoError(t, beaconDB.SaveState(ctx, beaconState, genesisRoot), "Could not save genesis state") + + c := &mock.ChainService{Root: bsRoot[:], State: beaconState} + alphaServer := &validator.Server{ + SyncCommitteePool: synccommittee.NewStore(), + P2P: &mockp2p.MockBroadcaster{}, + BlockBuilder: &builderTest.MockBuilderService{}, + BlockReceiver: c, + BlockNotifier: &mock.MockBlockNotifier{}, + } + beaconChainServer := &Server{ + BeaconDB: beaconDB, + BlockReceiver: c, + ChainInfoFetcher: c, + BlockNotifier: c.BlockNotifier(), + Broadcaster: mockp2p.NewTestP2P(t), + HeadFetcher: c, + V1Alpha1ValidatorServer: alphaServer, + } + req := util.NewBlindedBeaconBlockBellatrix() + req.Block.Slot = params.BeaconConfig().SlotsPerEpoch.Mul(uint64(params.BeaconConfig().BellatrixForkEpoch)) + req.Block.ParentRoot = bsRoot[:] + util.SaveBlock(t, ctx, beaconDB, req) + blockSsz, err := req.MarshalSSZ() + require.NoError(t, err) + blockReq := ðpbv2.SSZContainer{ + Data: blockSsz, + } + md := metadata.MD{} + md.Set(versionHeader, "bellatrix") + sszCtx := metadata.NewIncomingContext(ctx, md) + _, err = beaconChainServer.SubmitBlindedBlockSSZ(sszCtx, blockReq) + assert.NoError(t, err, "Could not propose block correctly") + }) + + t.Run("Capella", func(t *testing.T) { + t.Skip("This test needs Capella fork version configured properly") + + // INFO: This code block can be removed once Capella + // fork epoch is set to a value other than math.MaxUint64 + params.SetupTestConfigCleanup(t) + cfg := params.BeaconConfig() + cfg.CapellaForkEpoch = cfg.BellatrixForkEpoch + 1000 + cfg.ForkVersionSchedule[bytesutil.ToBytes4(cfg.CapellaForkVersion)] = cfg.BellatrixForkEpoch + 1000 + params.OverrideBeaconConfig(cfg) + + beaconDB := dbTest.SetupDB(t) + ctx := context.Background() + + genesis := util.NewBeaconBlockCapella() + util.SaveBlock(t, context.Background(), beaconDB, genesis) + + numDeposits := uint64(64) + beaconState, _ := util.DeterministicGenesisState(t, numDeposits) + bsRoot, err := beaconState.HashTreeRoot(ctx) + require.NoError(t, err) + genesisRoot, err := genesis.Block.HashTreeRoot() + require.NoError(t, err) + require.NoError(t, beaconDB.SaveState(ctx, beaconState, genesisRoot), "Could not save genesis state") + + c := &mock.ChainService{Root: bsRoot[:], State: beaconState} + alphaServer := &validator.Server{ + SyncCommitteePool: synccommittee.NewStore(), + P2P: &mockp2p.MockBroadcaster{}, + BlockBuilder: &builderTest.MockBuilderService{}, + BlockReceiver: c, + BlockNotifier: &mock.MockBlockNotifier{}, + } + beaconChainServer := &Server{ + BeaconDB: beaconDB, + BlockReceiver: c, + ChainInfoFetcher: c, + BlockNotifier: c.BlockNotifier(), + Broadcaster: mockp2p.NewTestP2P(t), + HeadFetcher: c, + V1Alpha1ValidatorServer: alphaServer, + } + req := util.NewBlindedBeaconBlockCapella() + req.Block.Slot = params.BeaconConfig().SlotsPerEpoch.Mul(uint64(params.BeaconConfig().CapellaForkEpoch)) + req.Block.ParentRoot = bsRoot[:] + util.SaveBlock(t, ctx, beaconDB, req) + blockSsz, err := req.MarshalSSZ() + require.NoError(t, err) + blockReq := ðpbv2.SSZContainer{ + Data: blockSsz, + } + md := metadata.MD{} + md.Set(versionHeader, "capella") + sszCtx := metadata.NewIncomingContext(ctx, md) + _, err = beaconChainServer.SubmitBlindedBlockSSZ(sszCtx, blockReq) + assert.NoError(t, err, "Could not propose block correctly") + }) +} + +func TestSubmitBlindedBlock(t *testing.T) { + t.Run("Phase 0", func(t *testing.T) { + beaconDB := dbTest.SetupDB(t) + ctx := context.Background() + + genesis := util.NewBeaconBlock() + util.SaveBlock(t, context.Background(), beaconDB, genesis) + + numDeposits := uint64(64) + beaconState, _ := util.DeterministicGenesisState(t, numDeposits) + bsRoot, err := beaconState.HashTreeRoot(ctx) + require.NoError(t, err) + genesisRoot, err := genesis.Block.HashTreeRoot() + require.NoError(t, err) + require.NoError(t, beaconDB.SaveState(ctx, beaconState, genesisRoot), "Could not save genesis state") + + c := &mock.ChainService{Root: bsRoot[:], State: beaconState} + beaconChainServer := &Server{ + BeaconDB: beaconDB, + BlockReceiver: c, + ChainInfoFetcher: c, + BlockNotifier: c.BlockNotifier(), + Broadcaster: mockp2p.NewTestP2P(t), + } + req := util.NewBeaconBlock() + req.Block.Slot = 5 + req.Block.ParentRoot = bsRoot[:] + v1Block, err := migration.V1Alpha1ToV1SignedBlock(req) + require.NoError(t, err) + util.SaveBlock(t, ctx, beaconDB, req) + blockReq := ðpbv2.SignedBlindedBeaconBlockContainer{ + Message: ðpbv2.SignedBlindedBeaconBlockContainer_Phase0Block{Phase0Block: v1Block.Block}, + Signature: v1Block.Signature, + } + _, err = beaconChainServer.SubmitBlindedBlock(context.Background(), blockReq) + assert.NoError(t, err, "Could not propose block correctly") + }) + + t.Run("Altair", func(t *testing.T) { + beaconDB := dbTest.SetupDB(t) + ctx := context.Background() + + genesis := util.NewBeaconBlockAltair() + util.SaveBlock(t, context.Background(), beaconDB, genesis) + + numDeposits := uint64(64) + beaconState, _ := util.DeterministicGenesisState(t, numDeposits) + bsRoot, err := beaconState.HashTreeRoot(ctx) + require.NoError(t, err) + genesisRoot, err := genesis.Block.HashTreeRoot() + require.NoError(t, err) + require.NoError(t, beaconDB.SaveState(ctx, beaconState, genesisRoot), "Could not save genesis state") + + c := &mock.ChainService{Root: bsRoot[:], State: beaconState} + beaconChainServer := &Server{ + BeaconDB: beaconDB, + BlockReceiver: c, + ChainInfoFetcher: c, + BlockNotifier: c.BlockNotifier(), + Broadcaster: mockp2p.NewTestP2P(t), + } + req := util.NewBeaconBlockAltair() + req.Block.Slot = 5 + req.Block.ParentRoot = bsRoot[:] + v2Block, err := migration.V1Alpha1BeaconBlockAltairToV2(req.Block) + require.NoError(t, err) + util.SaveBlock(t, ctx, beaconDB, req) + blockReq := ðpbv2.SignedBlindedBeaconBlockContainer{ + Message: ðpbv2.SignedBlindedBeaconBlockContainer_AltairBlock{AltairBlock: v2Block}, + Signature: req.Signature, + } + _, err = beaconChainServer.SubmitBlindedBlock(context.Background(), blockReq) + assert.NoError(t, err, "Could not propose block correctly") + }) + + t.Run("Bellatrix", func(t *testing.T) { + transactions := [][]byte{[]byte("transaction1"), []byte("transaction2")} + transactionsRoot, err := ssz.TransactionsRoot(transactions) + require.NoError(t, err) + + beaconDB := dbTest.SetupDB(t) + ctx := context.Background() + + genesis := util.NewBeaconBlockBellatrix() + util.SaveBlock(t, context.Background(), beaconDB, genesis) + + numDeposits := uint64(64) + beaconState, _ := util.DeterministicGenesisState(t, numDeposits) + bsRoot, err := beaconState.HashTreeRoot(ctx) + require.NoError(t, err) + genesisRoot, err := genesis.Block.HashTreeRoot() + require.NoError(t, err) + require.NoError(t, beaconDB.SaveState(ctx, beaconState, genesisRoot), "Could not save genesis state") + + c := &mock.ChainService{Root: bsRoot[:], State: beaconState} + alphaServer := &validator.Server{ + SyncCommitteePool: synccommittee.NewStore(), + P2P: &mockp2p.MockBroadcaster{}, + BlockBuilder: &builderTest.MockBuilderService{}, + BlockReceiver: c, + BlockNotifier: &mock.MockBlockNotifier{}, + } + beaconChainServer := &Server{ + BeaconDB: beaconDB, + BlockReceiver: c, + ChainInfoFetcher: c, + BlockNotifier: c.BlockNotifier(), + Broadcaster: mockp2p.NewTestP2P(t), + V1Alpha1ValidatorServer: alphaServer, + } + + blk := util.NewBeaconBlockBellatrix() + blk.Block.Slot = 5 + blk.Block.ParentRoot = bsRoot[:] + blk.Block.Body.ExecutionPayload.Transactions = transactions + blindedBlk := util.NewBlindedBeaconBlockBellatrixV2() + blindedBlk.Message.Slot = 5 + blindedBlk.Message.ParentRoot = bsRoot[:] + blindedBlk.Message.Body.ExecutionPayloadHeader.TransactionsRoot = transactionsRoot[:] + util.SaveBlock(t, ctx, beaconDB, blk) + + blockReq := ðpbv2.SignedBlindedBeaconBlockContainer{ + Message: ðpbv2.SignedBlindedBeaconBlockContainer_BellatrixBlock{BellatrixBlock: blindedBlk.Message}, + Signature: blindedBlk.Signature, + } + _, err = beaconChainServer.SubmitBlindedBlock(context.Background(), blockReq) + assert.NoError(t, err) + }) + + t.Run("Capella", func(t *testing.T) { + transactions := [][]byte{[]byte("transaction1"), []byte("transaction2")} + transactionsRoot, err := ssz.TransactionsRoot(transactions) + require.NoError(t, err) + + withdrawals := []*enginev1.Withdrawal{ + { + Index: 1, + ValidatorIndex: 1, + Address: bytesutil.PadTo([]byte("address1"), 20), + Amount: 1, + }, + { + Index: 2, + ValidatorIndex: 2, + Address: bytesutil.PadTo([]byte("address2"), 20), + Amount: 2, + }, + } + withdrawalsRoot, err := ssz.WithdrawalSliceRoot(hash.CustomSHA256Hasher(), withdrawals, 16) + require.NoError(t, err) + + beaconDB := dbTest.SetupDB(t) + ctx := context.Background() + + genesis := util.NewBeaconBlockCapella() + util.SaveBlock(t, context.Background(), beaconDB, genesis) + + numDeposits := uint64(64) + beaconState, _ := util.DeterministicGenesisState(t, numDeposits) + bsRoot, err := beaconState.HashTreeRoot(ctx) + require.NoError(t, err) + genesisRoot, err := genesis.Block.HashTreeRoot() + require.NoError(t, err) + require.NoError(t, beaconDB.SaveState(ctx, beaconState, genesisRoot), "Could not save genesis state") + + c := &mock.ChainService{Root: bsRoot[:], State: beaconState} + alphaServer := &validator.Server{ + SyncCommitteePool: synccommittee.NewStore(), + P2P: &mockp2p.MockBroadcaster{}, + BlockBuilder: &builderTest.MockBuilderService{}, + BlockReceiver: c, + BlockNotifier: &mock.MockBlockNotifier{}, + } + beaconChainServer := &Server{ + BeaconDB: beaconDB, + BlockReceiver: c, + ChainInfoFetcher: c, + BlockNotifier: c.BlockNotifier(), + Broadcaster: mockp2p.NewTestP2P(t), + V1Alpha1ValidatorServer: alphaServer, + } + + blk := util.NewBeaconBlockCapella() + blk.Block.Slot = 5 + blk.Block.ParentRoot = bsRoot[:] + blk.Block.Body.ExecutionPayload.Transactions = transactions + blk.Block.Body.ExecutionPayload.Withdrawals = withdrawals + blindedBlk := util.NewBlindedBeaconBlockCapellaV2() + blindedBlk.Message.Slot = 5 + blindedBlk.Message.ParentRoot = bsRoot[:] + blindedBlk.Message.Body.ExecutionPayloadHeader.TransactionsRoot = transactionsRoot[:] + blindedBlk.Message.Body.ExecutionPayloadHeader.WithdrawalsRoot = withdrawalsRoot[:] + util.SaveBlock(t, ctx, beaconDB, blk) + + blockReq := ðpbv2.SignedBlindedBeaconBlockContainer{ + Message: ðpbv2.SignedBlindedBeaconBlockContainer_CapellaBlock{CapellaBlock: blindedBlk.Message}, + Signature: blindedBlk.Signature, + } + _, err = beaconChainServer.SubmitBlindedBlock(context.Background(), blockReq) + assert.NoError(t, err) + }) +} diff --git a/beacon-chain/rpc/eth/beacon/blocks.go b/beacon-chain/rpc/eth/beacon/blocks.go index 0be028517570..0ac1444ccc15 100644 --- a/beacon-chain/rpc/eth/beacon/blocks.go +++ b/beacon-chain/rpc/eth/beacon/blocks.go @@ -23,7 +23,6 @@ import ( ethpbv1 "github.com/prysmaticlabs/prysm/v3/proto/eth/v1" ethpbv2 "github.com/prysmaticlabs/prysm/v3/proto/eth/v2" "github.com/prysmaticlabs/prysm/v3/proto/migration" - eth "github.com/prysmaticlabs/prysm/v3/proto/prysm/v1alpha1" "github.com/prysmaticlabs/prysm/v3/time/slots" "go.opencensus.io/trace" "google.golang.org/grpc/codes" @@ -276,97 +275,6 @@ func (bs *Server) SubmitBlockSSZ(ctx context.Context, req *ethpbv2.SSZContainer) return &emptypb.Empty{}, bs.submitBlock(ctx, root, block) } -// SubmitBlindedBlock instructs the beacon node to use the components of the `SignedBlindedBeaconBlock` to construct -// and publish a `SignedBeaconBlock` by swapping out the `transactions_root` for the corresponding full list of `transactions`. -// The beacon node should broadcast a newly constructed `SignedBeaconBlock` to the beacon network, -// to be included in the beacon chain. The beacon node is not required to validate the signed -// `BeaconBlock`, and a successful response (20X) only indicates that the broadcast has been -// successful. The beacon node is expected to integrate the new block into its state, and -// therefore validate the block internally, however blocks which fail the validation are still -// broadcast but a different status code is returned (202). -func (bs *Server) SubmitBlindedBlock(ctx context.Context, req *ethpbv2.SignedBlindedBeaconBlockContainer) (*emptypb.Empty, error) { - ctx, span := trace.StartSpan(ctx, "beacon.SubmitBlindedBlock") - defer span.End() - - bellatrixBlkContainer, ok := req.Message.(*ethpbv2.SignedBlindedBeaconBlockContainer_BellatrixBlock) - if ok { - if err := bs.submitBlindedBellatrixBlock(ctx, bellatrixBlkContainer.BellatrixBlock, req.Signature); err != nil { - return nil, err - } - } - - // At the end we check forks that don't have blinded blocks. - phase0BlkContainer, ok := req.Message.(*ethpbv2.SignedBlindedBeaconBlockContainer_Phase0Block) - if ok { - if err := bs.submitPhase0Block(ctx, phase0BlkContainer.Phase0Block, req.Signature); err != nil { - return nil, err - } - } - altairBlkContainer, ok := req.Message.(*ethpbv2.SignedBlindedBeaconBlockContainer_AltairBlock) - if ok { - if err := bs.submitAltairBlock(ctx, altairBlkContainer.AltairBlock, req.Signature); err != nil { - return nil, err - } - } - - return &emptypb.Empty{}, nil -} - -// SubmitBlindedBlockSSZ instructs the beacon node to use the components of the `SignedBlindedBeaconBlock` to construct -// and publish a `SignedBeaconBlock` by swapping out the `transactions_root` for the corresponding full list of `transactions`. -// The beacon node should broadcast a newly constructed `SignedBeaconBlock` to the beacon network, -// to be included in the beacon chain. The beacon node is not required to validate the signed -// `BeaconBlock`, and a successful response (20X) only indicates that the broadcast has been -// successful. The beacon node is expected to integrate the new block into its state, and -// therefore validate the block internally, however blocks which fail the validation are still -// broadcast but a different status code is returned (202). -// -// The provided block must be SSZ-serialized. -func (bs *Server) SubmitBlindedBlockSSZ(ctx context.Context, req *ethpbv2.SSZContainer) (*emptypb.Empty, error) { - ctx, span := trace.StartSpan(ctx, "beacon.SubmitBlindedBlockSSZ") - defer span.End() - - md, ok := metadata.FromIncomingContext(ctx) - if !ok { - return &emptypb.Empty{}, status.Errorf(codes.Internal, "Could not read"+versionHeader+" header") - } - ver := md.Get(versionHeader) - if len(ver) == 0 { - return &emptypb.Empty{}, status.Errorf(codes.Internal, "Could not read"+versionHeader+" header") - } - schedule := forks.NewOrderedSchedule(params.BeaconConfig()) - forkVer, err := schedule.VersionForName(ver[0]) - if err != nil { - return &emptypb.Empty{}, status.Errorf(codes.Internal, "Could not determine fork version: %v", err) - } - unmarshaler, err := detect.FromForkVersion(forkVer) - if err != nil { - return &emptypb.Empty{}, status.Errorf(codes.Internal, "Could not create unmarshaler: %v", err) - } - block, err := unmarshaler.UnmarshalBlindedBeaconBlock(req.Data) - if err != nil { - return &emptypb.Empty{}, status.Errorf(codes.Internal, "Could not unmarshal request data into block: %v", err) - } - - if block.IsBlinded() { - b, err := block.PbBlindedBellatrixBlock() - if err != nil { - return &emptypb.Empty{}, status.Errorf(codes.Internal, "Could not get blinded block: %v", err) - } - bb, err := migration.V1Alpha1BeaconBlockBlindedBellatrixToV2Blinded(b.Block) - if err != nil { - return &emptypb.Empty{}, status.Errorf(codes.Internal, "Could not migrate block: %v", err) - } - return &emptypb.Empty{}, bs.submitBlindedBellatrixBlock(ctx, bb, b.Signature) - } - - root, err := block.Block().HashTreeRoot() - if err != nil { - return &emptypb.Empty{}, status.Errorf(codes.Internal, "Could not compute block's hash tree root: %v", err) - } - return &emptypb.Empty{}, bs.submitBlock(ctx, root, block) -} - // GetBlock retrieves block details for given block ID. func (bs *Server) GetBlock(ctx context.Context, req *ethpbv1.BlockRequest) (*ethpbv1.BlockResponse, error) { ctx, span := trace.StartSpan(ctx, "beacon.GetBlock") @@ -1127,23 +1035,22 @@ func (bs *Server) submitBellatrixBlock(ctx context.Context, bellatrixBlk *ethpbv return bs.submitBlock(ctx, root, wrappedBellatrixBlk) } -func (bs *Server) submitBlindedBellatrixBlock(ctx context.Context, blindedBellatrixBlk *ethpbv2.BlindedBeaconBlockBellatrix, sig []byte) error { - b, err := migration.BlindedBellatrixToV1Alpha1SignedBlock(ðpbv2.SignedBlindedBeaconBlockBellatrix{ - Message: blindedBellatrixBlk, - Signature: sig, - }) +func (bs *Server) submitCapellaBlock(ctx context.Context, capellaBlk *ethpbv2.BeaconBlockCapella, sig []byte) error { + v1alpha1Blk, err := migration.CapellaToV1Alpha1SignedBlock(ðpbv2.SignedBeaconBlockCapella{Message: capellaBlk, Signature: sig}) + if err != nil { + return status.Errorf(codes.InvalidArgument, "Could not convert block to v1 block") + } + wrappedCapellaBlk, err := blocks.NewSignedBeaconBlock(v1alpha1Blk) if err != nil { - return status.Errorf(codes.Internal, "Could not get blinded block: %v", err) + return status.Errorf(codes.InvalidArgument, "Could not prepare block") } - _, err = bs.V1Alpha1ValidatorServer.ProposeBeaconBlock(ctx, ð.GenericSignedBeaconBlock{ - Block: ð.GenericSignedBeaconBlock_BlindedBellatrix{ - BlindedBellatrix: b, - }, - }) + + root, err := capellaBlk.HashTreeRoot() if err != nil { - return status.Errorf(codes.Internal, "Could not propose blinded block: %v", err) + return status.Errorf(codes.InvalidArgument, "Could not tree hash block: %v", err) } - return nil + + return bs.submitBlock(ctx, root, wrappedCapellaBlk) } func (bs *Server) submitBlock(ctx context.Context, blockRoot [fieldparams.RootLength]byte, block interfaces.SignedBeaconBlock) error { diff --git a/beacon-chain/rpc/eth/beacon/blocks_test.go b/beacon-chain/rpc/eth/beacon/blocks_test.go index 266e453966d9..8d6220e11ac6 100644 --- a/beacon-chain/rpc/eth/beacon/blocks_test.go +++ b/beacon-chain/rpc/eth/beacon/blocks_test.go @@ -7,20 +7,15 @@ import ( "testing" mock "github.com/prysmaticlabs/prysm/v3/beacon-chain/blockchain/testing" - builderTest "github.com/prysmaticlabs/prysm/v3/beacon-chain/builder/testing" "github.com/prysmaticlabs/prysm/v3/beacon-chain/db" dbTest "github.com/prysmaticlabs/prysm/v3/beacon-chain/db/testing" executionTest "github.com/prysmaticlabs/prysm/v3/beacon-chain/execution/testing" - "github.com/prysmaticlabs/prysm/v3/beacon-chain/operations/synccommittee" mockp2p "github.com/prysmaticlabs/prysm/v3/beacon-chain/p2p/testing" - "github.com/prysmaticlabs/prysm/v3/beacon-chain/rpc/prysm/v1alpha1/validator" "github.com/prysmaticlabs/prysm/v3/config/params" "github.com/prysmaticlabs/prysm/v3/consensus-types/blocks" "github.com/prysmaticlabs/prysm/v3/consensus-types/interfaces" types "github.com/prysmaticlabs/prysm/v3/consensus-types/primitives" - "github.com/prysmaticlabs/prysm/v3/crypto/hash" "github.com/prysmaticlabs/prysm/v3/encoding/bytesutil" - "github.com/prysmaticlabs/prysm/v3/encoding/ssz" enginev1 "github.com/prysmaticlabs/prysm/v3/proto/engine/v1" ethpbv1 "github.com/prysmaticlabs/prysm/v3/proto/eth/v1" ethpbv2 "github.com/prysmaticlabs/prysm/v3/proto/eth/v2" @@ -875,405 +870,6 @@ func TestServer_SubmitBlockSSZ_OK(t *testing.T) { }) } -func TestServer_SubmitBlindedBlockSSZ_OK(t *testing.T) { - t.Run("Phase 0", func(t *testing.T) { - beaconDB := dbTest.SetupDB(t) - ctx := context.Background() - - genesis := util.NewBeaconBlock() - util.SaveBlock(t, context.Background(), beaconDB, genesis) - - numDeposits := uint64(64) - beaconState, _ := util.DeterministicGenesisState(t, numDeposits) - bsRoot, err := beaconState.HashTreeRoot(ctx) - require.NoError(t, err) - genesisRoot, err := genesis.Block.HashTreeRoot() - require.NoError(t, err) - require.NoError(t, beaconDB.SaveState(ctx, beaconState, genesisRoot), "Could not save genesis state") - - c := &mock.ChainService{Root: bsRoot[:], State: beaconState} - beaconChainServer := &Server{ - BeaconDB: beaconDB, - BlockReceiver: c, - ChainInfoFetcher: c, - BlockNotifier: c.BlockNotifier(), - Broadcaster: mockp2p.NewTestP2P(t), - HeadFetcher: c, - } - req := util.NewBeaconBlock() - req.Block.Slot = 5 - req.Block.ParentRoot = bsRoot[:] - util.SaveBlock(t, ctx, beaconDB, req) - blockSsz, err := req.MarshalSSZ() - require.NoError(t, err) - blockReq := ðpbv2.SSZContainer{ - Data: blockSsz, - } - md := metadata.MD{} - md.Set(versionHeader, "phase0") - sszCtx := metadata.NewIncomingContext(ctx, md) - _, err = beaconChainServer.SubmitBlindedBlockSSZ(sszCtx, blockReq) - assert.NoError(t, err, "Could not propose block correctly") - }) - - t.Run("Altair", func(t *testing.T) { - beaconDB := dbTest.SetupDB(t) - ctx := context.Background() - - genesis := util.NewBeaconBlockAltair() - util.SaveBlock(t, context.Background(), beaconDB, genesis) - - numDeposits := uint64(64) - beaconState, _ := util.DeterministicGenesisState(t, numDeposits) - bsRoot, err := beaconState.HashTreeRoot(ctx) - require.NoError(t, err) - genesisRoot, err := genesis.Block.HashTreeRoot() - require.NoError(t, err) - require.NoError(t, beaconDB.SaveState(ctx, beaconState, genesisRoot), "Could not save genesis state") - - c := &mock.ChainService{Root: bsRoot[:], State: beaconState} - beaconChainServer := &Server{ - BeaconDB: beaconDB, - BlockReceiver: c, - ChainInfoFetcher: c, - BlockNotifier: c.BlockNotifier(), - Broadcaster: mockp2p.NewTestP2P(t), - HeadFetcher: c, - } - req := util.NewBeaconBlockAltair() - req.Block.Slot = params.BeaconConfig().SlotsPerEpoch.Mul(uint64(params.BeaconConfig().AltairForkEpoch)) - req.Block.ParentRoot = bsRoot[:] - util.SaveBlock(t, ctx, beaconDB, req) - blockSsz, err := req.MarshalSSZ() - require.NoError(t, err) - blockReq := ðpbv2.SSZContainer{ - Data: blockSsz, - } - md := metadata.MD{} - md.Set(versionHeader, "altair") - sszCtx := metadata.NewIncomingContext(ctx, md) - _, err = beaconChainServer.SubmitBlindedBlockSSZ(sszCtx, blockReq) - assert.NoError(t, err, "Could not propose block correctly") - }) - - t.Run("Bellatrix", func(t *testing.T) { - // INFO: This code block can be removed once Bellatrix - // fork epoch is set to a value other than math.MaxUint64 - params.SetupTestConfigCleanup(t) - cfg := params.BeaconConfig() - cfg.BellatrixForkEpoch = cfg.AltairForkEpoch + 1000 - cfg.ForkVersionSchedule[bytesutil.ToBytes4(cfg.BellatrixForkVersion)] = cfg.AltairForkEpoch + 1000 - params.OverrideBeaconConfig(cfg) - - beaconDB := dbTest.SetupDB(t) - ctx := context.Background() - - genesis := util.NewBeaconBlockBellatrix() - util.SaveBlock(t, context.Background(), beaconDB, genesis) - - numDeposits := uint64(64) - beaconState, _ := util.DeterministicGenesisState(t, numDeposits) - bsRoot, err := beaconState.HashTreeRoot(ctx) - require.NoError(t, err) - genesisRoot, err := genesis.Block.HashTreeRoot() - require.NoError(t, err) - require.NoError(t, beaconDB.SaveState(ctx, beaconState, genesisRoot), "Could not save genesis state") - - c := &mock.ChainService{Root: bsRoot[:], State: beaconState} - alphaServer := &validator.Server{ - SyncCommitteePool: synccommittee.NewStore(), - P2P: &mockp2p.MockBroadcaster{}, - BlockBuilder: &builderTest.MockBuilderService{}, - BlockReceiver: c, - BlockNotifier: &mock.MockBlockNotifier{}, - } - beaconChainServer := &Server{ - BeaconDB: beaconDB, - BlockReceiver: c, - ChainInfoFetcher: c, - BlockNotifier: c.BlockNotifier(), - Broadcaster: mockp2p.NewTestP2P(t), - HeadFetcher: c, - V1Alpha1ValidatorServer: alphaServer, - } - req := util.NewBlindedBeaconBlockBellatrix() - req.Block.Slot = params.BeaconConfig().SlotsPerEpoch.Mul(uint64(params.BeaconConfig().BellatrixForkEpoch)) - req.Block.ParentRoot = bsRoot[:] - util.SaveBlock(t, ctx, beaconDB, req) - blockSsz, err := req.MarshalSSZ() - require.NoError(t, err) - blockReq := ðpbv2.SSZContainer{ - Data: blockSsz, - } - md := metadata.MD{} - md.Set(versionHeader, "bellatrix") - sszCtx := metadata.NewIncomingContext(ctx, md) - _, err = beaconChainServer.SubmitBlindedBlockSSZ(sszCtx, blockReq) - assert.NoError(t, err, "Could not propose block correctly") - }) - - t.Run("Capella", func(t *testing.T) { - t.Skip("This test needs Capella fork version configured properly") - - // INFO: This code block can be removed once Capella - // fork epoch is set to a value other than math.MaxUint64 - params.SetupTestConfigCleanup(t) - cfg := params.BeaconConfig() - cfg.CapellaForkEpoch = cfg.BellatrixForkEpoch + 1000 - cfg.ForkVersionSchedule[bytesutil.ToBytes4(cfg.CapellaForkVersion)] = cfg.BellatrixForkEpoch + 1000 - params.OverrideBeaconConfig(cfg) - - beaconDB := dbTest.SetupDB(t) - ctx := context.Background() - - genesis := util.NewBeaconBlockCapella() - util.SaveBlock(t, context.Background(), beaconDB, genesis) - - numDeposits := uint64(64) - beaconState, _ := util.DeterministicGenesisState(t, numDeposits) - bsRoot, err := beaconState.HashTreeRoot(ctx) - require.NoError(t, err) - genesisRoot, err := genesis.Block.HashTreeRoot() - require.NoError(t, err) - require.NoError(t, beaconDB.SaveState(ctx, beaconState, genesisRoot), "Could not save genesis state") - - c := &mock.ChainService{Root: bsRoot[:], State: beaconState} - alphaServer := &validator.Server{ - SyncCommitteePool: synccommittee.NewStore(), - P2P: &mockp2p.MockBroadcaster{}, - BlockBuilder: &builderTest.MockBuilderService{}, - BlockReceiver: c, - BlockNotifier: &mock.MockBlockNotifier{}, - } - beaconChainServer := &Server{ - BeaconDB: beaconDB, - BlockReceiver: c, - ChainInfoFetcher: c, - BlockNotifier: c.BlockNotifier(), - Broadcaster: mockp2p.NewTestP2P(t), - HeadFetcher: c, - V1Alpha1ValidatorServer: alphaServer, - } - req := util.NewBlindedBeaconBlockCapella() - req.Block.Slot = params.BeaconConfig().SlotsPerEpoch.Mul(uint64(params.BeaconConfig().CapellaForkEpoch)) - req.Block.ParentRoot = bsRoot[:] - util.SaveBlock(t, ctx, beaconDB, req) - blockSsz, err := req.MarshalSSZ() - require.NoError(t, err) - blockReq := ðpbv2.SSZContainer{ - Data: blockSsz, - } - md := metadata.MD{} - md.Set(versionHeader, "capella") - sszCtx := metadata.NewIncomingContext(ctx, md) - _, err = beaconChainServer.SubmitBlindedBlockSSZ(sszCtx, blockReq) - assert.NoError(t, err, "Could not propose block correctly") - }) -} - -func TestSubmitBlindedBlock(t *testing.T) { - t.Run("Phase 0", func(t *testing.T) { - beaconDB := dbTest.SetupDB(t) - ctx := context.Background() - - genesis := util.NewBeaconBlock() - util.SaveBlock(t, context.Background(), beaconDB, genesis) - - numDeposits := uint64(64) - beaconState, _ := util.DeterministicGenesisState(t, numDeposits) - bsRoot, err := beaconState.HashTreeRoot(ctx) - require.NoError(t, err) - genesisRoot, err := genesis.Block.HashTreeRoot() - require.NoError(t, err) - require.NoError(t, beaconDB.SaveState(ctx, beaconState, genesisRoot), "Could not save genesis state") - - c := &mock.ChainService{Root: bsRoot[:], State: beaconState} - beaconChainServer := &Server{ - BeaconDB: beaconDB, - BlockReceiver: c, - ChainInfoFetcher: c, - BlockNotifier: c.BlockNotifier(), - Broadcaster: mockp2p.NewTestP2P(t), - } - req := util.NewBeaconBlock() - req.Block.Slot = 5 - req.Block.ParentRoot = bsRoot[:] - v1Block, err := migration.V1Alpha1ToV1SignedBlock(req) - require.NoError(t, err) - util.SaveBlock(t, ctx, beaconDB, req) - blockReq := ðpbv2.SignedBlindedBeaconBlockContainer{ - Message: ðpbv2.SignedBlindedBeaconBlockContainer_Phase0Block{Phase0Block: v1Block.Block}, - Signature: v1Block.Signature, - } - _, err = beaconChainServer.SubmitBlindedBlock(context.Background(), blockReq) - assert.NoError(t, err, "Could not propose block correctly") - }) - - t.Run("Altair", func(t *testing.T) { - beaconDB := dbTest.SetupDB(t) - ctx := context.Background() - - genesis := util.NewBeaconBlockAltair() - util.SaveBlock(t, context.Background(), beaconDB, genesis) - - numDeposits := uint64(64) - beaconState, _ := util.DeterministicGenesisState(t, numDeposits) - bsRoot, err := beaconState.HashTreeRoot(ctx) - require.NoError(t, err) - genesisRoot, err := genesis.Block.HashTreeRoot() - require.NoError(t, err) - require.NoError(t, beaconDB.SaveState(ctx, beaconState, genesisRoot), "Could not save genesis state") - - c := &mock.ChainService{Root: bsRoot[:], State: beaconState} - beaconChainServer := &Server{ - BeaconDB: beaconDB, - BlockReceiver: c, - ChainInfoFetcher: c, - BlockNotifier: c.BlockNotifier(), - Broadcaster: mockp2p.NewTestP2P(t), - } - req := util.NewBeaconBlockAltair() - req.Block.Slot = 5 - req.Block.ParentRoot = bsRoot[:] - v2Block, err := migration.V1Alpha1BeaconBlockAltairToV2(req.Block) - require.NoError(t, err) - util.SaveBlock(t, ctx, beaconDB, req) - blockReq := ðpbv2.SignedBlindedBeaconBlockContainer{ - Message: ðpbv2.SignedBlindedBeaconBlockContainer_AltairBlock{AltairBlock: v2Block}, - Signature: req.Signature, - } - _, err = beaconChainServer.SubmitBlindedBlock(context.Background(), blockReq) - assert.NoError(t, err, "Could not propose block correctly") - }) - - t.Run("Bellatrix", func(t *testing.T) { - transactions := [][]byte{[]byte("transaction1"), []byte("transaction2")} - transactionsRoot, err := ssz.TransactionsRoot(transactions) - require.NoError(t, err) - - beaconDB := dbTest.SetupDB(t) - ctx := context.Background() - - genesis := util.NewBeaconBlockBellatrix() - util.SaveBlock(t, context.Background(), beaconDB, genesis) - - numDeposits := uint64(64) - beaconState, _ := util.DeterministicGenesisState(t, numDeposits) - bsRoot, err := beaconState.HashTreeRoot(ctx) - require.NoError(t, err) - genesisRoot, err := genesis.Block.HashTreeRoot() - require.NoError(t, err) - require.NoError(t, beaconDB.SaveState(ctx, beaconState, genesisRoot), "Could not save genesis state") - - c := &mock.ChainService{Root: bsRoot[:], State: beaconState} - alphaServer := &validator.Server{ - SyncCommitteePool: synccommittee.NewStore(), - P2P: &mockp2p.MockBroadcaster{}, - BlockBuilder: &builderTest.MockBuilderService{}, - BlockReceiver: c, - BlockNotifier: &mock.MockBlockNotifier{}, - } - beaconChainServer := &Server{ - BeaconDB: beaconDB, - BlockReceiver: c, - ChainInfoFetcher: c, - BlockNotifier: c.BlockNotifier(), - Broadcaster: mockp2p.NewTestP2P(t), - V1Alpha1ValidatorServer: alphaServer, - } - - blk := util.NewBeaconBlockBellatrix() - blk.Block.Slot = 5 - blk.Block.ParentRoot = bsRoot[:] - blk.Block.Body.ExecutionPayload.Transactions = transactions - blindedBlk := util.NewBlindedBeaconBlockBellatrixV2() - blindedBlk.Message.Slot = 5 - blindedBlk.Message.ParentRoot = bsRoot[:] - blindedBlk.Message.Body.ExecutionPayloadHeader.TransactionsRoot = transactionsRoot[:] - util.SaveBlock(t, ctx, beaconDB, blk) - - blockReq := ðpbv2.SignedBlindedBeaconBlockContainer{ - Message: ðpbv2.SignedBlindedBeaconBlockContainer_BellatrixBlock{BellatrixBlock: blindedBlk.Message}, - Signature: blindedBlk.Signature, - } - _, err = beaconChainServer.SubmitBlindedBlock(context.Background(), blockReq) - assert.NoError(t, err) - }) - - t.Run("Capella", func(t *testing.T) { - transactions := [][]byte{[]byte("transaction1"), []byte("transaction2")} - transactionsRoot, err := ssz.TransactionsRoot(transactions) - require.NoError(t, err) - - withdrawals := []*enginev1.Withdrawal{ - { - Index: 1, - ValidatorIndex: 1, - Address: bytesutil.PadTo([]byte("address1"), 20), - Amount: 1, - }, - { - Index: 2, - ValidatorIndex: 2, - Address: bytesutil.PadTo([]byte("address2"), 20), - Amount: 2, - }, - } - withdrawalsRoot, err := ssz.WithdrawalSliceRoot(hash.CustomSHA256Hasher(), withdrawals, 16) - require.NoError(t, err) - - beaconDB := dbTest.SetupDB(t) - ctx := context.Background() - - genesis := util.NewBeaconBlockCapella() - util.SaveBlock(t, context.Background(), beaconDB, genesis) - - numDeposits := uint64(64) - beaconState, _ := util.DeterministicGenesisState(t, numDeposits) - bsRoot, err := beaconState.HashTreeRoot(ctx) - require.NoError(t, err) - genesisRoot, err := genesis.Block.HashTreeRoot() - require.NoError(t, err) - require.NoError(t, beaconDB.SaveState(ctx, beaconState, genesisRoot), "Could not save genesis state") - - c := &mock.ChainService{Root: bsRoot[:], State: beaconState} - alphaServer := &validator.Server{ - SyncCommitteePool: synccommittee.NewStore(), - P2P: &mockp2p.MockBroadcaster{}, - BlockBuilder: &builderTest.MockBuilderService{}, - BlockReceiver: c, - BlockNotifier: &mock.MockBlockNotifier{}, - } - beaconChainServer := &Server{ - BeaconDB: beaconDB, - BlockReceiver: c, - ChainInfoFetcher: c, - BlockNotifier: c.BlockNotifier(), - Broadcaster: mockp2p.NewTestP2P(t), - V1Alpha1ValidatorServer: alphaServer, - } - - blk := util.NewBeaconBlockCapella() - blk.Block.Slot = 5 - blk.Block.ParentRoot = bsRoot[:] - blk.Block.Body.ExecutionPayload.Transactions = transactions - blk.Block.Body.ExecutionPayload.Withdrawals = withdrawals - blindedBlk := util.NewBlindedBeaconBlockCapellaV2() - blindedBlk.Message.Slot = 5 - blindedBlk.Message.ParentRoot = bsRoot[:] - blindedBlk.Message.Body.ExecutionPayloadHeader.TransactionsRoot = transactionsRoot[:] - blindedBlk.Message.Body.ExecutionPayloadHeader.WithdrawalsRoot = withdrawalsRoot[:] - util.SaveBlock(t, ctx, beaconDB, blk) - - blockReq := ðpbv2.SignedBlindedBeaconBlockContainer{ - Message: ðpbv2.SignedBlindedBeaconBlockContainer_CapellaBlock{CapellaBlock: blindedBlk.Message}, - Signature: blindedBlk.Signature, - } - _, err = beaconChainServer.SubmitBlindedBlock(context.Background(), blockReq) - assert.NoError(t, err) - }) -} - func TestServer_GetBlock(t *testing.T) { beaconDB := dbTest.SetupDB(t) ctx := context.Background() From 346716e3c4df3640ece44606c3a041778627e3e3 Mon Sep 17 00:00:00 2001 From: rkapka Date: Tue, 29 Nov 2022 18:45:23 +0100 Subject: [PATCH 10/18] fix migration code --- proto/migration/BUILD.bazel | 2 + proto/migration/v1alpha1_to_v2.go | 251 +++++++++++++++++++++++++++++- 2 files changed, 246 insertions(+), 7 deletions(-) diff --git a/proto/migration/BUILD.bazel b/proto/migration/BUILD.bazel index 8bc17c64357c..57e79a10b259 100644 --- a/proto/migration/BUILD.bazel +++ b/proto/migration/BUILD.bazel @@ -11,7 +11,9 @@ go_library( visibility = ["//visibility:public"], deps = [ "//beacon-chain/state:go_default_library", + "//config/fieldparams:go_default_library", "//consensus-types/interfaces:go_default_library", + "//crypto/hash:go_default_library", "//encoding/bytesutil:go_default_library", "//encoding/ssz:go_default_library", "//proto/engine/v1:go_default_library", diff --git a/proto/migration/v1alpha1_to_v2.go b/proto/migration/v1alpha1_to_v2.go index 15377260cb7f..4cf1b05eedae 100644 --- a/proto/migration/v1alpha1_to_v2.go +++ b/proto/migration/v1alpha1_to_v2.go @@ -3,6 +3,8 @@ package migration import ( "github.com/pkg/errors" "github.com/prysmaticlabs/prysm/v3/beacon-chain/state" + fieldparams "github.com/prysmaticlabs/prysm/v3/config/fieldparams" + "github.com/prysmaticlabs/prysm/v3/crypto/hash" "github.com/prysmaticlabs/prysm/v3/encoding/bytesutil" "github.com/prysmaticlabs/prysm/v3/encoding/ssz" enginev1 "github.com/prysmaticlabs/prysm/v3/proto/engine/v1" @@ -51,6 +53,19 @@ func BellatrixToV1Alpha1SignedBlock(bellatrixBlk *ethpbv2.SignedBeaconBlockBella return v1alpha1Block, nil } +// CapellaToV1Alpha1SignedBlock converts a v2 SignedBeaconBlockCapella proto to a v1alpha1 proto. +func CapellaToV1Alpha1SignedBlock(capellaBlk *ethpbv2.SignedBeaconBlockCapella) (*ethpbalpha.SignedBeaconBlockCapella, error) { + marshaledBlk, err := proto.Marshal(capellaBlk) + if err != nil { + return nil, errors.Wrap(err, "could not marshal block") + } + v1alpha1Block := ðpbalpha.SignedBeaconBlockCapella{} + if err := proto.Unmarshal(marshaledBlk, v1alpha1Block); err != nil { + return nil, errors.Wrap(err, "could not unmarshal block") + } + return v1alpha1Block, nil +} + // BlindedBellatrixToV1Alpha1SignedBlock converts a v2 SignedBlindedBeaconBlockBellatrix proto to a v1alpha1 proto. func BlindedBellatrixToV1Alpha1SignedBlock(bellatrixBlk *ethpbv2.SignedBlindedBeaconBlockBellatrix) (*ethpbalpha.SignedBlindedBeaconBlockBellatrix, error) { marshaledBlk, err := proto.Marshal(bellatrixBlk) @@ -64,6 +79,19 @@ func BlindedBellatrixToV1Alpha1SignedBlock(bellatrixBlk *ethpbv2.SignedBlindedBe return v1alpha1Block, nil } +// BlindedCapellaToV1Alpha1SignedBlock converts a v2 SignedBlindedBeaconBlockCapella proto to a v1alpha1 proto. +func BlindedCapellaToV1Alpha1SignedBlock(capellaBlk *ethpbv2.SignedBlindedBeaconBlockCapella) (*ethpbalpha.SignedBlindedBeaconBlockCapella, error) { + marshaledBlk, err := proto.Marshal(capellaBlk) + if err != nil { + return nil, errors.Wrap(err, "could not marshal block") + } + v1alpha1Block := ðpbalpha.SignedBlindedBeaconBlockCapella{} + if err := proto.Unmarshal(marshaledBlk, v1alpha1Block); err != nil { + return nil, errors.Wrap(err, "could not unmarshal block") + } + return v1alpha1Block, nil +} + // V1Alpha1BeaconBlockBellatrixToV2 converts a v1alpha1 Bellatrix beacon block to a v2 // Bellatrix block. func V1Alpha1BeaconBlockBellatrixToV2(v1alpha1Block *ethpbalpha.BeaconBlockBellatrix) (*ethpbv2.BeaconBlockBellatrix, error) { @@ -78,6 +106,20 @@ func V1Alpha1BeaconBlockBellatrixToV2(v1alpha1Block *ethpbalpha.BeaconBlockBella return v2Block, nil } +// V1Alpha1BeaconBlockCapellaToV2 converts a v1alpha1 Capella beacon block to a v2 +// Capella block. +func V1Alpha1BeaconBlockCapellaToV2(v1alpha1Block *ethpbalpha.BeaconBlockCapella) (*ethpbv2.BeaconBlockCapella, error) { + marshaledBlk, err := proto.Marshal(v1alpha1Block) + if err != nil { + return nil, errors.Wrap(err, "could not marshal block") + } + v2Block := ðpbv2.BeaconBlockCapella{} + if err := proto.Unmarshal(marshaledBlk, v2Block); err != nil { + return nil, errors.Wrap(err, "could not unmarshal block") + } + return v2Block, nil +} + // V1Alpha1BeaconBlockBlindedBellatrixToV2Blinded converts a v1alpha1 Blinded Bellatrix beacon block to a v2 Blinded Bellatrix block. func V1Alpha1BeaconBlockBlindedBellatrixToV2Blinded(v1alpha1Block *ethpbalpha.BlindedBeaconBlockBellatrix) (*ethpbv2.BlindedBeaconBlockBellatrix, error) { marshaledBlk, err := proto.Marshal(v1alpha1Block) @@ -91,6 +133,19 @@ func V1Alpha1BeaconBlockBlindedBellatrixToV2Blinded(v1alpha1Block *ethpbalpha.Bl return v2Block, nil } +// V1Alpha1BeaconBlockBlindedCapellaToV2Blinded converts a v1alpha1 Blinded Capella beacon block to a v2 Blinded Capella block. +func V1Alpha1BeaconBlockBlindedCapellaToV2Blinded(v1alpha1Block *ethpbalpha.BlindedBeaconBlockCapella) (*ethpbv2.BlindedBeaconBlockCapella, error) { + marshaledBlk, err := proto.Marshal(v1alpha1Block) + if err != nil { + return nil, errors.Wrap(err, "could not marshal block") + } + v2Block := ðpbv2.BlindedBeaconBlockCapella{} + if err := proto.Unmarshal(marshaledBlk, v2Block); err != nil { + return nil, errors.Wrap(err, "could not unmarshal block") + } + return v2Block, nil +} + // V1Alpha1BeaconBlockBellatrixToV2Blinded converts a v1alpha1 Bellatrix beacon block to a v2 // blinded Bellatrix block. func V1Alpha1BeaconBlockBellatrixToV2Blinded(v1alpha1Block *ethpbalpha.BeaconBlockBellatrix) (*ethpbv2.BlindedBeaconBlockBellatrix, error) { @@ -263,15 +318,197 @@ func V1Alpha1BeaconBlockBellatrixToV2Blinded(v1alpha1Block *ethpbalpha.BeaconBlo return v2Block, nil } -// V1Alpha1BeaconBlockBlindedCapellaToV2Blinded converts a v1alpha1 blinded Capella beacon block to a v2 blinded Capella block. -func V1Alpha1BeaconBlockBlindedCapellaToV2Blinded(v1alpha1Block *ethpbalpha.BlindedBeaconBlockCapella) (*ethpbv2.BlindedBeaconBlockCapella, error) { - marshaledBlk, err := proto.Marshal(v1alpha1Block) +// V1Alpha1BeaconBlockCapellaToV2Blinded converts a v1alpha1 Capella beacon block to a v2 +// blinded Capella block. +func V1Alpha1BeaconBlockCapellaToV2Blinded(v1alpha1Block *ethpbalpha.BeaconBlockCapella) (*ethpbv2.BlindedBeaconBlockCapella, error) { + sourceProposerSlashings := v1alpha1Block.Body.ProposerSlashings + resultProposerSlashings := make([]*ethpbv1.ProposerSlashing, len(sourceProposerSlashings)) + for i, s := range sourceProposerSlashings { + resultProposerSlashings[i] = ðpbv1.ProposerSlashing{ + SignedHeader_1: ðpbv1.SignedBeaconBlockHeader{ + Message: ðpbv1.BeaconBlockHeader{ + Slot: s.Header_1.Header.Slot, + ProposerIndex: s.Header_1.Header.ProposerIndex, + ParentRoot: bytesutil.SafeCopyBytes(s.Header_1.Header.ParentRoot), + StateRoot: bytesutil.SafeCopyBytes(s.Header_1.Header.StateRoot), + BodyRoot: bytesutil.SafeCopyBytes(s.Header_1.Header.BodyRoot), + }, + Signature: bytesutil.SafeCopyBytes(s.Header_1.Signature), + }, + SignedHeader_2: ðpbv1.SignedBeaconBlockHeader{ + Message: ðpbv1.BeaconBlockHeader{ + Slot: s.Header_2.Header.Slot, + ProposerIndex: s.Header_2.Header.ProposerIndex, + ParentRoot: bytesutil.SafeCopyBytes(s.Header_2.Header.ParentRoot), + StateRoot: bytesutil.SafeCopyBytes(s.Header_2.Header.StateRoot), + BodyRoot: bytesutil.SafeCopyBytes(s.Header_2.Header.BodyRoot), + }, + Signature: bytesutil.SafeCopyBytes(s.Header_2.Signature), + }, + } + } + + sourceAttesterSlashings := v1alpha1Block.Body.AttesterSlashings + resultAttesterSlashings := make([]*ethpbv1.AttesterSlashing, len(sourceAttesterSlashings)) + for i, s := range sourceAttesterSlashings { + att1Indices := make([]uint64, len(s.Attestation_1.AttestingIndices)) + copy(att1Indices, s.Attestation_1.AttestingIndices) + att2Indices := make([]uint64, len(s.Attestation_2.AttestingIndices)) + copy(att2Indices, s.Attestation_2.AttestingIndices) + resultAttesterSlashings[i] = ðpbv1.AttesterSlashing{ + Attestation_1: ðpbv1.IndexedAttestation{ + AttestingIndices: att1Indices, + Data: ðpbv1.AttestationData{ + Slot: s.Attestation_1.Data.Slot, + Index: s.Attestation_1.Data.CommitteeIndex, + BeaconBlockRoot: bytesutil.SafeCopyBytes(s.Attestation_1.Data.BeaconBlockRoot), + Source: ðpbv1.Checkpoint{ + Epoch: s.Attestation_1.Data.Source.Epoch, + Root: bytesutil.SafeCopyBytes(s.Attestation_1.Data.Source.Root), + }, + Target: ðpbv1.Checkpoint{ + Epoch: s.Attestation_1.Data.Target.Epoch, + Root: bytesutil.SafeCopyBytes(s.Attestation_1.Data.Target.Root), + }, + }, + Signature: bytesutil.SafeCopyBytes(s.Attestation_1.Signature), + }, + Attestation_2: ðpbv1.IndexedAttestation{ + AttestingIndices: att2Indices, + Data: ðpbv1.AttestationData{ + Slot: s.Attestation_2.Data.Slot, + Index: s.Attestation_2.Data.CommitteeIndex, + BeaconBlockRoot: bytesutil.SafeCopyBytes(s.Attestation_2.Data.BeaconBlockRoot), + Source: ðpbv1.Checkpoint{ + Epoch: s.Attestation_2.Data.Source.Epoch, + Root: bytesutil.SafeCopyBytes(s.Attestation_2.Data.Source.Root), + }, + Target: ðpbv1.Checkpoint{ + Epoch: s.Attestation_2.Data.Target.Epoch, + Root: bytesutil.SafeCopyBytes(s.Attestation_2.Data.Target.Root), + }, + }, + Signature: bytesutil.SafeCopyBytes(s.Attestation_2.Signature), + }, + } + } + + sourceAttestations := v1alpha1Block.Body.Attestations + resultAttestations := make([]*ethpbv1.Attestation, len(sourceAttestations)) + for i, a := range sourceAttestations { + resultAttestations[i] = ðpbv1.Attestation{ + AggregationBits: bytesutil.SafeCopyBytes(a.AggregationBits), + Data: ðpbv1.AttestationData{ + Slot: a.Data.Slot, + Index: a.Data.CommitteeIndex, + BeaconBlockRoot: bytesutil.SafeCopyBytes(a.Data.BeaconBlockRoot), + Source: ðpbv1.Checkpoint{ + Epoch: a.Data.Source.Epoch, + Root: bytesutil.SafeCopyBytes(a.Data.Source.Root), + }, + Target: ðpbv1.Checkpoint{ + Epoch: a.Data.Target.Epoch, + Root: bytesutil.SafeCopyBytes(a.Data.Target.Root), + }, + }, + Signature: bytesutil.SafeCopyBytes(a.Signature), + } + } + + sourceDeposits := v1alpha1Block.Body.Deposits + resultDeposits := make([]*ethpbv1.Deposit, len(sourceDeposits)) + for i, d := range sourceDeposits { + resultDeposits[i] = ðpbv1.Deposit{ + Proof: bytesutil.SafeCopy2dBytes(d.Proof), + Data: ðpbv1.Deposit_Data{ + Pubkey: bytesutil.SafeCopyBytes(d.Data.PublicKey), + WithdrawalCredentials: bytesutil.SafeCopyBytes(d.Data.WithdrawalCredentials), + Amount: d.Data.Amount, + Signature: bytesutil.SafeCopyBytes(d.Data.Signature), + }, + } + } + + sourceExits := v1alpha1Block.Body.VoluntaryExits + resultExits := make([]*ethpbv1.SignedVoluntaryExit, len(sourceExits)) + for i, e := range sourceExits { + resultExits[i] = ðpbv1.SignedVoluntaryExit{ + Message: ðpbv1.VoluntaryExit{ + Epoch: e.Exit.Epoch, + ValidatorIndex: e.Exit.ValidatorIndex, + }, + Signature: bytesutil.SafeCopyBytes(e.Signature), + } + } + + transactionsRoot, err := ssz.TransactionsRoot(v1alpha1Block.Body.ExecutionPayload.Transactions) if err != nil { - return nil, errors.Wrap(err, "could not marshal block") + return nil, errors.Wrapf(err, "could not calculate transactions root") } - v2Block := ðpbv2.BlindedBeaconBlockCapella{} - if err := proto.Unmarshal(marshaledBlk, v2Block); err != nil { - return nil, errors.Wrap(err, "could not unmarshal block") + + withdrawalsRoot, err := ssz.WithdrawalSliceRoot( + hash.CustomSHA256Hasher(), + v1alpha1Block.Body.ExecutionPayload.Withdrawals, + fieldparams.MaxWithdrawalsPerPayload, + ) + if err != nil { + return nil, errors.Wrapf(err, "could not calculate transactions root") + } + + changes := make([]*ethpbv2.SignedBLSToExecutionChange, len(v1alpha1Block.Body.BlsToExecutionChanges)) + for i, change := range v1alpha1Block.Body.BlsToExecutionChanges { + changes[i] = ðpbv2.SignedBLSToExecutionChange{ + Message: ðpbv2.BLSToExecutionChange{ + ValidatorIndex: change.Message.ValidatorIndex, + FromBlsPubkey: bytesutil.SafeCopyBytes(change.Message.FromBlsPubkey), + ToExecutionAddress: bytesutil.SafeCopyBytes(change.Message.ToExecutionAddress), + }, + Signature: bytesutil.SafeCopyBytes(change.Signature), + } + } + + resultBlockBody := ðpbv2.BlindedBeaconBlockBodyCapella{ + RandaoReveal: bytesutil.SafeCopyBytes(v1alpha1Block.Body.RandaoReveal), + Eth1Data: ðpbv1.Eth1Data{ + DepositRoot: bytesutil.SafeCopyBytes(v1alpha1Block.Body.Eth1Data.DepositRoot), + DepositCount: v1alpha1Block.Body.Eth1Data.DepositCount, + BlockHash: bytesutil.SafeCopyBytes(v1alpha1Block.Body.Eth1Data.BlockHash), + }, + Graffiti: bytesutil.SafeCopyBytes(v1alpha1Block.Body.Graffiti), + ProposerSlashings: resultProposerSlashings, + AttesterSlashings: resultAttesterSlashings, + Attestations: resultAttestations, + Deposits: resultDeposits, + VoluntaryExits: resultExits, + SyncAggregate: ðpbv1.SyncAggregate{ + SyncCommitteeBits: bytesutil.SafeCopyBytes(v1alpha1Block.Body.SyncAggregate.SyncCommitteeBits), + SyncCommitteeSignature: bytesutil.SafeCopyBytes(v1alpha1Block.Body.SyncAggregate.SyncCommitteeSignature), + }, + ExecutionPayloadHeader: &enginev1.ExecutionPayloadHeaderCapella{ + ParentHash: bytesutil.SafeCopyBytes(v1alpha1Block.Body.ExecutionPayload.ParentHash), + FeeRecipient: bytesutil.SafeCopyBytes(v1alpha1Block.Body.ExecutionPayload.FeeRecipient), + StateRoot: bytesutil.SafeCopyBytes(v1alpha1Block.Body.ExecutionPayload.StateRoot), + ReceiptsRoot: bytesutil.SafeCopyBytes(v1alpha1Block.Body.ExecutionPayload.ReceiptsRoot), + LogsBloom: bytesutil.SafeCopyBytes(v1alpha1Block.Body.ExecutionPayload.LogsBloom), + PrevRandao: bytesutil.SafeCopyBytes(v1alpha1Block.Body.ExecutionPayload.PrevRandao), + BlockNumber: v1alpha1Block.Body.ExecutionPayload.BlockNumber, + GasLimit: v1alpha1Block.Body.ExecutionPayload.GasLimit, + GasUsed: v1alpha1Block.Body.ExecutionPayload.GasUsed, + Timestamp: v1alpha1Block.Body.ExecutionPayload.Timestamp, + ExtraData: bytesutil.SafeCopyBytes(v1alpha1Block.Body.ExecutionPayload.ExtraData), + BaseFeePerGas: bytesutil.SafeCopyBytes(v1alpha1Block.Body.ExecutionPayload.BaseFeePerGas), + BlockHash: bytesutil.SafeCopyBytes(v1alpha1Block.Body.ExecutionPayload.BlockHash), + TransactionsRoot: transactionsRoot[:], + WithdrawalsRoot: withdrawalsRoot[:], + }, + BlsToExecutionChanges: changes, + } + v2Block := ðpbv2.BlindedBeaconBlockCapella{ + Slot: v1alpha1Block.Slot, + ProposerIndex: v1alpha1Block.ProposerIndex, + ParentRoot: bytesutil.SafeCopyBytes(v1alpha1Block.ParentRoot), + StateRoot: bytesutil.SafeCopyBytes(v1alpha1Block.StateRoot), + Body: resultBlockBody, } return v2Block, nil } From 817bc27f1a6f923b76656a04e4eae2fb17a2bd9a Mon Sep 17 00:00:00 2001 From: rkapka Date: Tue, 29 Nov 2022 20:45:28 +0100 Subject: [PATCH 11/18] add Capella to SubmitBlock --- beacon-chain/rpc/eth/beacon/blocks.go | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/beacon-chain/rpc/eth/beacon/blocks.go b/beacon-chain/rpc/eth/beacon/blocks.go index 0ac1444ccc15..628e4fcaf0b8 100644 --- a/beacon-chain/rpc/eth/beacon/blocks.go +++ b/beacon-chain/rpc/eth/beacon/blocks.go @@ -233,6 +233,13 @@ func (bs *Server) SubmitBlock(ctx context.Context, req *ethpbv2.SignedBeaconBloc return nil, err } } + capellaBlkContainer, ok := req.Message.(*ethpbv2.SignedBeaconBlockContainer_CapellaBlock) + if ok { + if err := bs.submitCapellaBlock(ctx, capellaBlkContainer.CapellaBlock, req.Signature); err != nil { + return nil, err + } + } + return &emptypb.Empty{}, nil } From 27cad231cd5afe8afa2a9502d5a1968e1a1b0372 Mon Sep 17 00:00:00 2001 From: rkapka Date: Tue, 29 Nov 2022 20:54:39 +0100 Subject: [PATCH 12/18] custom hooks --- .../rpc/apimiddleware/custom_hooks.go | 47 ++++++++++++++++++- 1 file changed, 46 insertions(+), 1 deletion(-) diff --git a/beacon-chain/rpc/apimiddleware/custom_hooks.go b/beacon-chain/rpc/apimiddleware/custom_hooks.go index fbfeba9222c5..aeef93ee360d 100644 --- a/beacon-chain/rpc/apimiddleware/custom_hooks.go +++ b/beacon-chain/rpc/apimiddleware/custom_hooks.go @@ -233,11 +233,21 @@ type bellatrixPublishBlockRequestJson struct { Signature string `json:"signature" hex:"true"` } +type capellaPublishBlockRequestJson struct { + CapellaBlock *BeaconBlockCapellaJson `json:"capella_block"` + Signature string `json:"signature" hex:"true"` +} + type bellatrixPublishBlindedBlockRequestJson struct { BellatrixBlock *BlindedBeaconBlockBellatrixJson `json:"bellatrix_block"` Signature string `json:"signature" hex:"true"` } +type capellaPublishBlindedBlockRequestJson struct { + CapellaBlock *BlindedBeaconBlockCapellaJson `json:"capella_block"` + Signature string `json:"signature" hex:"true"` +} + // setInitialPublishBlockPostRequest is triggered before we deserialize the request JSON into a struct. // We don't know which version of the block got posted, but we can determine it from the slot. // We know that blocks of all versions have a Message field with a Slot field, @@ -308,6 +318,15 @@ func preparePublishedBlock(endpoint *apimiddleware.Endpoint, _ http.ResponseWrit endpoint.PostRequest = actualPostReq return nil } + if block, ok := endpoint.PostRequest.(*SignedBeaconBlockCapellaContainerJson); ok { + // Prepare post request that can be properly decoded on gRPC side. + actualPostReq := &capellaPublishBlockRequestJson{ + CapellaBlock: block.Message, + Signature: block.Signature, + } + endpoint.PostRequest = actualPostReq + return nil + } return apimiddleware.InternalServerError(errors.New("unsupported block type")) } @@ -342,8 +361,10 @@ func setInitialPublishBlindedBlockPostRequest(endpoint *apimiddleware.Endpoint, endpoint.PostRequest = &SignedBeaconBlockContainerJson{} } else if currentEpoch < params.BeaconConfig().BellatrixForkEpoch { endpoint.PostRequest = &SignedBeaconBlockAltairContainerJson{} - } else { + } else if currentEpoch < params.BeaconConfig().CapellaForkEpoch { endpoint.PostRequest = &SignedBlindedBeaconBlockBellatrixContainerJson{} + } else { + endpoint.PostRequest = &SignedBlindedBeaconBlockCapellaContainerJson{} } req.Body = ioutil.NopCloser(bytes.NewBuffer(buf)) return true, nil @@ -381,6 +402,15 @@ func preparePublishedBlindedBlock(endpoint *apimiddleware.Endpoint, _ http.Respo endpoint.PostRequest = actualPostReq return nil } + if block, ok := endpoint.PostRequest.(*SignedBlindedBeaconBlockCapellaContainerJson); ok { + // Prepare post request that can be properly decoded on gRPC side. + actualPostReq := &capellaPublishBlindedBlockRequestJson{ + CapellaBlock: block.Message, + Signature: block.Signature, + } + endpoint.PostRequest = actualPostReq + return nil + } return apimiddleware.InternalServerError(errors.New("unsupported block type")) } @@ -439,6 +469,12 @@ type bellatrixBlockResponseJson struct { ExecutionOptimistic bool `json:"execution_optimistic"` } +type capellaBlockResponseJson struct { + Version string `json:"version"` + Data *SignedBeaconBlockCapellaContainerJson `json:"data"` + ExecutionOptimistic bool `json:"execution_optimistic"` +} + type bellatrixBlindedBlockResponseJson struct { Version string `json:"version"` Data *SignedBlindedBeaconBlockBellatrixContainerJson `json:"data"` @@ -486,6 +522,15 @@ func serializeV2Block(response interface{}) (apimiddleware.RunDefault, []byte, a }, ExecutionOptimistic: respContainer.ExecutionOptimistic, } + case strings.EqualFold(respContainer.Version, strings.ToLower(ethpbv2.Version_CAPELLA.String())): + actualRespContainer = &capellaBlockResponseJson{ + Version: respContainer.Version, + Data: &SignedBeaconBlockCapellaContainerJson{ + Message: respContainer.Data.CapellaBlock, + Signature: respContainer.Data.Signature, + }, + ExecutionOptimistic: respContainer.ExecutionOptimistic, + } default: return false, nil, apimiddleware.InternalServerError(fmt.Errorf("unsupported block version '%s'", respContainer.Version)) } From 266379ae546add8973209673a514d0091990a12e Mon Sep 17 00:00:00 2001 From: rkapka Date: Tue, 29 Nov 2022 21:02:20 +0100 Subject: [PATCH 13/18] missing structs --- beacon-chain/rpc/apimiddleware/structs.go | 60 ++++++++++++++++++++++- 1 file changed, 59 insertions(+), 1 deletion(-) diff --git a/beacon-chain/rpc/apimiddleware/structs.go b/beacon-chain/rpc/apimiddleware/structs.go index 8ad375996d06..0133653b0124 100644 --- a/beacon-chain/rpc/apimiddleware/structs.go +++ b/beacon-chain/rpc/apimiddleware/structs.go @@ -374,6 +374,7 @@ type SignedBeaconBlockContainerV2Json struct { Phase0Block *BeaconBlockJson `json:"phase0_block"` AltairBlock *BeaconBlockAltairJson `json:"altair_block"` BellatrixBlock *BeaconBlockBellatrixJson `json:"bellatrix_block"` + CapellaBlock *BeaconBlockCapellaJson `json:"capella_block"` Signature string `json:"signature" hex:"true"` } @@ -407,6 +408,11 @@ type SignedBeaconBlockBellatrixContainerJson struct { Signature string `json:"signature" hex:"true"` } +type SignedBeaconBlockCapellaContainerJson struct { + Message *BeaconBlockCapellaJson `json:"message"` + Signature string `json:"signature" hex:"true"` +} + type SignedBlindedBeaconBlockBellatrixContainerJson struct { Message *BlindedBeaconBlockBellatrixJson `json:"message"` Signature string `json:"signature" hex:"true"` @@ -433,6 +439,14 @@ type BeaconBlockBellatrixJson struct { Body *BeaconBlockBodyBellatrixJson `json:"body"` } +type BeaconBlockCapellaJson struct { + Slot string `json:"slot"` + ProposerIndex string `json:"proposer_index"` + ParentRoot string `json:"parent_root" hex:"true"` + StateRoot string `json:"state_root" hex:"true"` + Body *BeaconBlockBodyCapellaJson `json:"body"` +} + type BlindedBeaconBlockBellatrixJson struct { Slot string `json:"slot"` ProposerIndex string `json:"proposer_index"` @@ -474,6 +488,20 @@ type BeaconBlockBodyBellatrixJson struct { ExecutionPayload *ExecutionPayloadJson `json:"execution_payload"` } +type BeaconBlockBodyCapellaJson struct { + RandaoReveal string `json:"randao_reveal" hex:"true"` + Eth1Data *Eth1DataJson `json:"eth1_data"` + Graffiti string `json:"graffiti" hex:"true"` + ProposerSlashings []*ProposerSlashingJson `json:"proposer_slashings"` + AttesterSlashings []*AttesterSlashingJson `json:"attester_slashings"` + Attestations []*AttestationJson `json:"attestations"` + Deposits []*DepositJson `json:"deposits"` + VoluntaryExits []*SignedVoluntaryExitJson `json:"voluntary_exits"` + SyncAggregate *SyncAggregateJson `json:"sync_aggregate"` + ExecutionPayload *ExecutionPayloadCapellaJson `json:"execution_payload"` + BLSToExecutionChanges []*SignedBLSToExecutionChangeJson `json:"bls_to_execution_changes"` +} + type BlindedBeaconBlockBodyBellatrixJson struct { RandaoReveal string `json:"randao_reveal" hex:"true"` Eth1Data *Eth1DataJson `json:"eth1_data"` @@ -498,7 +526,7 @@ type BlindedBeaconBlockBodyCapellaJson struct { VoluntaryExits []*SignedVoluntaryExitJson `json:"voluntary_exits"` SyncAggregate *SyncAggregateJson `json:"sync_aggregate"` ExecutionPayloadHeader *ExecutionPayloadHeaderCapellaJson `json:"execution_payload_header"` - BLSToExecutionChanges []*BLSToExecutionChangeJson `json:"bls_to_execution_changes"` + BLSToExecutionChanges []*SignedBLSToExecutionChangeJson `json:"bls_to_execution_changes"` } type ExecutionPayloadJson struct { @@ -518,6 +546,24 @@ type ExecutionPayloadJson struct { Transactions []string `json:"transactions" hex:"true"` } +type ExecutionPayloadCapellaJson struct { + ParentHash string `json:"parent_hash" hex:"true"` + FeeRecipient string `json:"fee_recipient" hex:"true"` + StateRoot string `json:"state_root" hex:"true"` + ReceiptsRoot string `json:"receipts_root" hex:"true"` + LogsBloom string `json:"logs_bloom" hex:"true"` + PrevRandao string `json:"prev_randao" hex:"true"` + BlockNumber string `json:"block_number"` + GasLimit string `json:"gas_limit"` + GasUsed string `json:"gas_used"` + TimeStamp string `json:"timestamp"` + ExtraData string `json:"extra_data" hex:"true"` + BaseFeePerGas string `json:"base_fee_per_gas" uint256:"true"` + BlockHash string `json:"block_hash" hex:"true"` + Transactions []string `json:"transactions" hex:"true"` + Withdrawals []*WithdrawalJson `json:"withdrawals"` +} + type ExecutionPayloadHeaderJson struct { ParentHash string `json:"parent_hash" hex:"true"` FeeRecipient string `json:"fee_recipient" hex:"true"` @@ -629,6 +675,11 @@ type BLSToExecutionChangeJson struct { ToExecutionAddress string `json:"to_execution_address" hex:"true"` } +type SignedBLSToExecutionChangeJson struct { + BLSToExecutionChange *BLSToExecutionChangeJson `json:"message"` + Signature string `json:"signature" hex:"true"` +} + type DepositJson struct { Proof []string `json:"proof" hex:"true"` Data *Deposit_DataJson `json:"data"` @@ -683,6 +734,13 @@ type VersionJson struct { Version string `json:"version"` } +type WithdrawalJson struct { + WithdrawalIndex string `json:"index"` + ValidatorIndex string `json:"validator_index"` + ExecutionAddress string `json:"address" hex:"true"` + Amount string `json:"amount"` +} + type BeaconStateJson struct { GenesisTime string `json:"genesis_time"` GenesisValidatorsRoot string `json:"genesis_validators_root" hex:"true"` From 19294247d701f5bee15221389c0fb78c973e7518 Mon Sep 17 00:00:00 2001 From: rkapka Date: Tue, 29 Nov 2022 21:05:54 +0100 Subject: [PATCH 14/18] fix tests --- consensus-types/blocks/proto_test.go | 6 +++--- encoding/ssz/htrutils_test.go | 18 +++++++++--------- 2 files changed, 12 insertions(+), 12 deletions(-) diff --git a/consensus-types/blocks/proto_test.go b/consensus-types/blocks/proto_test.go index c7da10196d2d..b12279fd108e 100644 --- a/consensus-types/blocks/proto_test.go +++ b/consensus-types/blocks/proto_test.go @@ -1243,9 +1243,9 @@ func getFields() fields { }, Withdrawals: []*enginev1.Withdrawal{ { - WithdrawalIndex: 128, - ExecutionAddress: b20, - Amount: 128, + Index: 128, + Address: b20, + Amount: 128, }, }, } diff --git a/encoding/ssz/htrutils_test.go b/encoding/ssz/htrutils_test.go index eeca0b730f41..fcc9dffeed85 100644 --- a/encoding/ssz/htrutils_test.go +++ b/encoding/ssz/htrutils_test.go @@ -174,17 +174,17 @@ func TestWithdrawalRoot(t *testing.T) { { name: "empty", input: &enginev1.Withdrawal{ - ExecutionAddress: make([]byte, 20), + Address: make([]byte, 20), }, want: [32]byte{0xdb, 0x56, 0x11, 0x4e, 0x0, 0xfd, 0xd4, 0xc1, 0xf8, 0x5c, 0x89, 0x2b, 0xf3, 0x5a, 0xc9, 0xa8, 0x92, 0x89, 0xaa, 0xec, 0xb1, 0xeb, 0xd0, 0xa9, 0x6c, 0xde, 0x60, 0x6a, 0x74, 0x8b, 0x5d, 0x71}, }, { name: "non-empty", input: &enginev1.Withdrawal{ - WithdrawalIndex: 123, - ValidatorIndex: 123123, - ExecutionAddress: []byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0}, - Amount: 50, + Index: 123, + ValidatorIndex: 123123, + Address: []byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0}, + Amount: 50, }, want: [32]byte{0x4f, 0xca, 0x3a, 0x43, 0x6e, 0xcc, 0x34, 0xad, 0x33, 0xde, 0x3c, 0x22, 0xa3, 0x32, 0x27, 0xa, 0x8c, 0x4e, 0x75, 0xd8, 0x39, 0xc1, 0xd7, 0x55, 0x78, 0x77, 0xd7, 0x14, 0x6b, 0x34, 0x6a, 0xb6}, }, @@ -213,10 +213,10 @@ func TestWithrawalSliceRoot(t *testing.T) { { name: "non-empty", input: []*enginev1.Withdrawal{&enginev1.Withdrawal{ - WithdrawalIndex: 123, - ValidatorIndex: 123123, - ExecutionAddress: []byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0}, - Amount: 50, + Index: 123, + ValidatorIndex: 123123, + Address: []byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0}, + Amount: 50, }, }, want: [32]byte{0x10, 0x34, 0x29, 0xd1, 0x34, 0x30, 0xa0, 0x1c, 0x4, 0xdd, 0x3, 0xed, 0xe6, 0xa6, 0x33, 0xb2, 0xc9, 0x24, 0x23, 0x5c, 0x43, 0xca, 0xb2, 0x32, 0xaa, 0xed, 0xfe, 0xd5, 0x9, 0x78, 0xd1, 0x6f}, From 0fd2488182eaed756de90c3a636734712fbd8a97 Mon Sep 17 00:00:00 2001 From: rkapka Date: Tue, 29 Nov 2022 21:05:54 +0100 Subject: [PATCH 15/18] fix tests --- beacon-chain/core/blocks/withdrawals_test.go | 16 +++--- .../state-native/getters_withdrawal_test.go | 56 +++++++++---------- .../state/state-native/hasher_test.go | 8 --- consensus-types/blocks/execution_test.go | 8 +-- 4 files changed, 40 insertions(+), 48 deletions(-) diff --git a/beacon-chain/core/blocks/withdrawals_test.go b/beacon-chain/core/blocks/withdrawals_test.go index a964cf08a81e..89ed5eb90683 100644 --- a/beacon-chain/core/blocks/withdrawals_test.go +++ b/beacon-chain/core/blocks/withdrawals_test.go @@ -276,18 +276,18 @@ func TestProcessWithdrawals(t *testing.T) { } fullWithdrawal := func(i types.ValidatorIndex, idx uint64) *enginev1.Withdrawal { return &enginev1.Withdrawal{ - WithdrawalIndex: idx, - ValidatorIndex: i, - ExecutionAddress: executionAddress(i), - Amount: withdrawalAmount(i), + Index: idx, + ValidatorIndex: i, + Address: executionAddress(i), + Amount: withdrawalAmount(i), } } partialWithdrawal := func(i types.ValidatorIndex, idx uint64) *enginev1.Withdrawal { return &enginev1.Withdrawal{ - WithdrawalIndex: idx, - ValidatorIndex: i, - ExecutionAddress: executionAddress(i), - Amount: withdrawalAmount(i) - maxEffectiveBalance, + Index: idx, + ValidatorIndex: i, + Address: executionAddress(i), + Amount: withdrawalAmount(i) - maxEffectiveBalance, } } tests := []Test{ diff --git a/beacon-chain/state/state-native/getters_withdrawal_test.go b/beacon-chain/state/state-native/getters_withdrawal_test.go index b9b7afcb8575..6f4f1216bc71 100644 --- a/beacon-chain/state/state-native/getters_withdrawal_test.go +++ b/beacon-chain/state/state-native/getters_withdrawal_test.go @@ -153,10 +153,10 @@ func TestExpectedWithdrawals(t *testing.T) { require.NoError(t, err) require.Equal(t, 1, len(expected)) withdrawal := &enginev1.Withdrawal{ - WithdrawalIndex: 0, - ValidatorIndex: 3, - ExecutionAddress: s.validators[3].WithdrawalCredentials[12:], - Amount: s.balances[3], + Index: 0, + ValidatorIndex: 3, + Address: s.validators[3].WithdrawalCredentials[12:], + Amount: s.balances[3], } require.DeepEqual(t, withdrawal, expected[0]) }) @@ -181,10 +181,10 @@ func TestExpectedWithdrawals(t *testing.T) { require.NoError(t, err) require.Equal(t, 1, len(expected)) withdrawal := &enginev1.Withdrawal{ - WithdrawalIndex: 0, - ValidatorIndex: 3, - ExecutionAddress: s.validators[3].WithdrawalCredentials[12:], - Amount: params.BeaconConfig().MinDepositAmount, + Index: 0, + ValidatorIndex: 3, + Address: s.validators[3].WithdrawalCredentials[12:], + Amount: params.BeaconConfig().MinDepositAmount, } require.DeepEqual(t, withdrawal, expected[0]) }) @@ -212,16 +212,16 @@ func TestExpectedWithdrawals(t *testing.T) { require.Equal(t, 2, len(expected)) withdrawalFull := &enginev1.Withdrawal{ - WithdrawalIndex: 1, - ValidatorIndex: 7, - ExecutionAddress: s.validators[7].WithdrawalCredentials[12:], - Amount: s.balances[7], + Index: 1, + ValidatorIndex: 7, + Address: s.validators[7].WithdrawalCredentials[12:], + Amount: s.balances[7], } withdrawalPartial := &enginev1.Withdrawal{ - WithdrawalIndex: 0, - ValidatorIndex: 3, - ExecutionAddress: s.validators[3].WithdrawalCredentials[12:], - Amount: params.BeaconConfig().MinDepositAmount, + Index: 0, + ValidatorIndex: 3, + Address: s.validators[3].WithdrawalCredentials[12:], + Amount: params.BeaconConfig().MinDepositAmount, } require.DeepEqual(t, withdrawalPartial, expected[0]) require.DeepEqual(t, withdrawalFull, expected[1]) @@ -246,10 +246,10 @@ func TestExpectedWithdrawals(t *testing.T) { require.NoError(t, err) require.Equal(t, params.BeaconConfig().MaxWithdrawalsPerPayload, uint64(len(expected))) withdrawal := &enginev1.Withdrawal{ - WithdrawalIndex: 0, - ValidatorIndex: 0, - ExecutionAddress: s.validators[0].WithdrawalCredentials[12:], - Amount: 1, + Index: 0, + ValidatorIndex: 0, + Address: s.validators[0].WithdrawalCredentials[12:], + Amount: 1, } require.DeepEqual(t, withdrawal, expected[0]) }) @@ -273,10 +273,10 @@ func TestExpectedWithdrawals(t *testing.T) { require.NoError(t, err) require.Equal(t, params.BeaconConfig().MaxWithdrawalsPerPayload, uint64(len(expected))) withdrawal := &enginev1.Withdrawal{ - WithdrawalIndex: 0, - ValidatorIndex: 0, - ExecutionAddress: s.validators[0].WithdrawalCredentials[12:], - Amount: params.BeaconConfig().MaxEffectiveBalance, + Index: 0, + ValidatorIndex: 0, + Address: s.validators[0].WithdrawalCredentials[12:], + Amount: params.BeaconConfig().MaxEffectiveBalance, } require.DeepEqual(t, withdrawal, expected[0]) }) @@ -300,10 +300,10 @@ func TestExpectedWithdrawals(t *testing.T) { require.NoError(t, err) require.Equal(t, params.BeaconConfig().MaxWithdrawalsPerPayload, uint64(len(expected))) withdrawal := &enginev1.Withdrawal{ - WithdrawalIndex: 0, - ValidatorIndex: 0, - ExecutionAddress: s.validators[0].WithdrawalCredentials[12:], - Amount: params.BeaconConfig().MaxEffectiveBalance + 1, + Index: 0, + ValidatorIndex: 0, + Address: s.validators[0].WithdrawalCredentials[12:], + Amount: params.BeaconConfig().MaxEffectiveBalance + 1, } require.DeepEqual(t, withdrawal, expected[0]) }) diff --git a/beacon-chain/state/state-native/hasher_test.go b/beacon-chain/state/state-native/hasher_test.go index b21c62124c8c..416277a96863 100644 --- a/beacon-chain/state/state-native/hasher_test.go +++ b/beacon-chain/state/state-native/hasher_test.go @@ -464,11 +464,3 @@ func executionPayloadHeaderCapella() *enginev1.ExecutionPayloadHeaderCapella { WithdrawalsRoot: wr[:], } } - -func withdrawal() *enginev1.Withdrawal { - return &enginev1.Withdrawal{ - WithdrawalIndex: 123, - ExecutionAddress: bytesutil.PadTo([]byte("address"), 20), - Amount: 123, - } -} diff --git a/consensus-types/blocks/execution_test.go b/consensus-types/blocks/execution_test.go index 0e1e6cf7d50b..8e2ed5a89b04 100644 --- a/consensus-types/blocks/execution_test.go +++ b/consensus-types/blocks/execution_test.go @@ -98,10 +98,10 @@ func TestWrapExecutionPayloadCapella(t *testing.T) { BlockHash: []byte("blockhash"), Transactions: [][]byte{[]byte("transaction")}, Withdrawals: []*enginev1.Withdrawal{{ - WithdrawalIndex: 55, - ValidatorIndex: 66, - ExecutionAddress: []byte("executionaddress"), - Amount: 77, + Index: 55, + ValidatorIndex: 66, + Address: []byte("executionaddress"), + Amount: 77, }}, } payload, err := blocks.WrappedExecutionPayloadCapella(data) From a2c00fde5d3b312c66d117c7f17b7163dfa1015b Mon Sep 17 00:00:00 2001 From: rkapka Date: Wed, 14 Dec 2022 15:07:04 +0100 Subject: [PATCH 16/18] review --- proto/eth/v2/BUILD.bazel | 4 +- proto/migration/BUILD.bazel | 2 - proto/migration/v1alpha1_to_v2.go | 197 ------------------------------ 3 files changed, 2 insertions(+), 201 deletions(-) diff --git a/proto/eth/v2/BUILD.bazel b/proto/eth/v2/BUILD.bazel index de4e7d242924..10f85f546b19 100644 --- a/proto/eth/v2/BUILD.bazel +++ b/proto/eth/v2/BUILD.bazel @@ -38,13 +38,13 @@ ssz_gen_marshal( "//proto/eth/v1:go_default_library", ], objs = [ + "BlsToExecutionChange", "SignedBeaconBlockAltair", "SignedBeaconBlockBellatrix", "SignedBlindedBeaconBlockBellatrix", "SignedBeaconBlockCapella", "SignedBlindedBeaconBlockCapella", - "BlsToExecutionChange", - "SignedBlsToExecutionChange", + "SignedBlsToExecutionChange", ], ) diff --git a/proto/migration/BUILD.bazel b/proto/migration/BUILD.bazel index 57e79a10b259..8bc17c64357c 100644 --- a/proto/migration/BUILD.bazel +++ b/proto/migration/BUILD.bazel @@ -11,9 +11,7 @@ go_library( visibility = ["//visibility:public"], deps = [ "//beacon-chain/state:go_default_library", - "//config/fieldparams:go_default_library", "//consensus-types/interfaces:go_default_library", - "//crypto/hash:go_default_library", "//encoding/bytesutil:go_default_library", "//encoding/ssz:go_default_library", "//proto/engine/v1:go_default_library", diff --git a/proto/migration/v1alpha1_to_v2.go b/proto/migration/v1alpha1_to_v2.go index b1dfb58c3dfa..f1c4d35b85a6 100644 --- a/proto/migration/v1alpha1_to_v2.go +++ b/proto/migration/v1alpha1_to_v2.go @@ -3,8 +3,6 @@ package migration import ( "github.com/pkg/errors" "github.com/prysmaticlabs/prysm/v3/beacon-chain/state" - fieldparams "github.com/prysmaticlabs/prysm/v3/config/fieldparams" - "github.com/prysmaticlabs/prysm/v3/crypto/hash" "github.com/prysmaticlabs/prysm/v3/encoding/bytesutil" "github.com/prysmaticlabs/prysm/v3/encoding/ssz" enginev1 "github.com/prysmaticlabs/prysm/v3/proto/engine/v1" @@ -318,201 +316,6 @@ func V1Alpha1BeaconBlockBellatrixToV2Blinded(v1alpha1Block *ethpbalpha.BeaconBlo return v2Block, nil } -// V1Alpha1BeaconBlockCapellaToV2Blinded converts a v1alpha1 Capella beacon block to a v2 -// blinded Capella block. -func V1Alpha1BeaconBlockCapellaToV2Blinded(v1alpha1Block *ethpbalpha.BeaconBlockCapella) (*ethpbv2.BlindedBeaconBlockCapella, error) { - sourceProposerSlashings := v1alpha1Block.Body.ProposerSlashings - resultProposerSlashings := make([]*ethpbv1.ProposerSlashing, len(sourceProposerSlashings)) - for i, s := range sourceProposerSlashings { - resultProposerSlashings[i] = ðpbv1.ProposerSlashing{ - SignedHeader_1: ðpbv1.SignedBeaconBlockHeader{ - Message: ðpbv1.BeaconBlockHeader{ - Slot: s.Header_1.Header.Slot, - ProposerIndex: s.Header_1.Header.ProposerIndex, - ParentRoot: bytesutil.SafeCopyBytes(s.Header_1.Header.ParentRoot), - StateRoot: bytesutil.SafeCopyBytes(s.Header_1.Header.StateRoot), - BodyRoot: bytesutil.SafeCopyBytes(s.Header_1.Header.BodyRoot), - }, - Signature: bytesutil.SafeCopyBytes(s.Header_1.Signature), - }, - SignedHeader_2: ðpbv1.SignedBeaconBlockHeader{ - Message: ðpbv1.BeaconBlockHeader{ - Slot: s.Header_2.Header.Slot, - ProposerIndex: s.Header_2.Header.ProposerIndex, - ParentRoot: bytesutil.SafeCopyBytes(s.Header_2.Header.ParentRoot), - StateRoot: bytesutil.SafeCopyBytes(s.Header_2.Header.StateRoot), - BodyRoot: bytesutil.SafeCopyBytes(s.Header_2.Header.BodyRoot), - }, - Signature: bytesutil.SafeCopyBytes(s.Header_2.Signature), - }, - } - } - - sourceAttesterSlashings := v1alpha1Block.Body.AttesterSlashings - resultAttesterSlashings := make([]*ethpbv1.AttesterSlashing, len(sourceAttesterSlashings)) - for i, s := range sourceAttesterSlashings { - att1Indices := make([]uint64, len(s.Attestation_1.AttestingIndices)) - copy(att1Indices, s.Attestation_1.AttestingIndices) - att2Indices := make([]uint64, len(s.Attestation_2.AttestingIndices)) - copy(att2Indices, s.Attestation_2.AttestingIndices) - resultAttesterSlashings[i] = ðpbv1.AttesterSlashing{ - Attestation_1: ðpbv1.IndexedAttestation{ - AttestingIndices: att1Indices, - Data: ðpbv1.AttestationData{ - Slot: s.Attestation_1.Data.Slot, - Index: s.Attestation_1.Data.CommitteeIndex, - BeaconBlockRoot: bytesutil.SafeCopyBytes(s.Attestation_1.Data.BeaconBlockRoot), - Source: ðpbv1.Checkpoint{ - Epoch: s.Attestation_1.Data.Source.Epoch, - Root: bytesutil.SafeCopyBytes(s.Attestation_1.Data.Source.Root), - }, - Target: ðpbv1.Checkpoint{ - Epoch: s.Attestation_1.Data.Target.Epoch, - Root: bytesutil.SafeCopyBytes(s.Attestation_1.Data.Target.Root), - }, - }, - Signature: bytesutil.SafeCopyBytes(s.Attestation_1.Signature), - }, - Attestation_2: ðpbv1.IndexedAttestation{ - AttestingIndices: att2Indices, - Data: ðpbv1.AttestationData{ - Slot: s.Attestation_2.Data.Slot, - Index: s.Attestation_2.Data.CommitteeIndex, - BeaconBlockRoot: bytesutil.SafeCopyBytes(s.Attestation_2.Data.BeaconBlockRoot), - Source: ðpbv1.Checkpoint{ - Epoch: s.Attestation_2.Data.Source.Epoch, - Root: bytesutil.SafeCopyBytes(s.Attestation_2.Data.Source.Root), - }, - Target: ðpbv1.Checkpoint{ - Epoch: s.Attestation_2.Data.Target.Epoch, - Root: bytesutil.SafeCopyBytes(s.Attestation_2.Data.Target.Root), - }, - }, - Signature: bytesutil.SafeCopyBytes(s.Attestation_2.Signature), - }, - } - } - - sourceAttestations := v1alpha1Block.Body.Attestations - resultAttestations := make([]*ethpbv1.Attestation, len(sourceAttestations)) - for i, a := range sourceAttestations { - resultAttestations[i] = ðpbv1.Attestation{ - AggregationBits: bytesutil.SafeCopyBytes(a.AggregationBits), - Data: ðpbv1.AttestationData{ - Slot: a.Data.Slot, - Index: a.Data.CommitteeIndex, - BeaconBlockRoot: bytesutil.SafeCopyBytes(a.Data.BeaconBlockRoot), - Source: ðpbv1.Checkpoint{ - Epoch: a.Data.Source.Epoch, - Root: bytesutil.SafeCopyBytes(a.Data.Source.Root), - }, - Target: ðpbv1.Checkpoint{ - Epoch: a.Data.Target.Epoch, - Root: bytesutil.SafeCopyBytes(a.Data.Target.Root), - }, - }, - Signature: bytesutil.SafeCopyBytes(a.Signature), - } - } - - sourceDeposits := v1alpha1Block.Body.Deposits - resultDeposits := make([]*ethpbv1.Deposit, len(sourceDeposits)) - for i, d := range sourceDeposits { - resultDeposits[i] = ðpbv1.Deposit{ - Proof: bytesutil.SafeCopy2dBytes(d.Proof), - Data: ðpbv1.Deposit_Data{ - Pubkey: bytesutil.SafeCopyBytes(d.Data.PublicKey), - WithdrawalCredentials: bytesutil.SafeCopyBytes(d.Data.WithdrawalCredentials), - Amount: d.Data.Amount, - Signature: bytesutil.SafeCopyBytes(d.Data.Signature), - }, - } - } - - sourceExits := v1alpha1Block.Body.VoluntaryExits - resultExits := make([]*ethpbv1.SignedVoluntaryExit, len(sourceExits)) - for i, e := range sourceExits { - resultExits[i] = ðpbv1.SignedVoluntaryExit{ - Message: ðpbv1.VoluntaryExit{ - Epoch: e.Exit.Epoch, - ValidatorIndex: e.Exit.ValidatorIndex, - }, - Signature: bytesutil.SafeCopyBytes(e.Signature), - } - } - - transactionsRoot, err := ssz.TransactionsRoot(v1alpha1Block.Body.ExecutionPayload.Transactions) - if err != nil { - return nil, errors.Wrapf(err, "could not calculate transactions root") - } - - withdrawalsRoot, err := ssz.WithdrawalSliceRoot( - hash.CustomSHA256Hasher(), - v1alpha1Block.Body.ExecutionPayload.Withdrawals, - fieldparams.MaxWithdrawalsPerPayload, - ) - if err != nil { - return nil, errors.Wrapf(err, "could not calculate transactions root") - } - - changes := make([]*ethpbv2.SignedBLSToExecutionChange, len(v1alpha1Block.Body.BlsToExecutionChanges)) - for i, change := range v1alpha1Block.Body.BlsToExecutionChanges { - changes[i] = ðpbv2.SignedBLSToExecutionChange{ - Message: ðpbv2.BLSToExecutionChange{ - ValidatorIndex: change.Message.ValidatorIndex, - FromBlsPubkey: bytesutil.SafeCopyBytes(change.Message.FromBlsPubkey), - ToExecutionAddress: bytesutil.SafeCopyBytes(change.Message.ToExecutionAddress), - }, - Signature: bytesutil.SafeCopyBytes(change.Signature), - } - } - - resultBlockBody := ðpbv2.BlindedBeaconBlockBodyCapella{ - RandaoReveal: bytesutil.SafeCopyBytes(v1alpha1Block.Body.RandaoReveal), - Eth1Data: ðpbv1.Eth1Data{ - DepositRoot: bytesutil.SafeCopyBytes(v1alpha1Block.Body.Eth1Data.DepositRoot), - DepositCount: v1alpha1Block.Body.Eth1Data.DepositCount, - BlockHash: bytesutil.SafeCopyBytes(v1alpha1Block.Body.Eth1Data.BlockHash), - }, - Graffiti: bytesutil.SafeCopyBytes(v1alpha1Block.Body.Graffiti), - ProposerSlashings: resultProposerSlashings, - AttesterSlashings: resultAttesterSlashings, - Attestations: resultAttestations, - Deposits: resultDeposits, - VoluntaryExits: resultExits, - SyncAggregate: ðpbv1.SyncAggregate{ - SyncCommitteeBits: bytesutil.SafeCopyBytes(v1alpha1Block.Body.SyncAggregate.SyncCommitteeBits), - SyncCommitteeSignature: bytesutil.SafeCopyBytes(v1alpha1Block.Body.SyncAggregate.SyncCommitteeSignature), - }, - ExecutionPayloadHeader: &enginev1.ExecutionPayloadHeaderCapella{ - ParentHash: bytesutil.SafeCopyBytes(v1alpha1Block.Body.ExecutionPayload.ParentHash), - FeeRecipient: bytesutil.SafeCopyBytes(v1alpha1Block.Body.ExecutionPayload.FeeRecipient), - StateRoot: bytesutil.SafeCopyBytes(v1alpha1Block.Body.ExecutionPayload.StateRoot), - ReceiptsRoot: bytesutil.SafeCopyBytes(v1alpha1Block.Body.ExecutionPayload.ReceiptsRoot), - LogsBloom: bytesutil.SafeCopyBytes(v1alpha1Block.Body.ExecutionPayload.LogsBloom), - PrevRandao: bytesutil.SafeCopyBytes(v1alpha1Block.Body.ExecutionPayload.PrevRandao), - BlockNumber: v1alpha1Block.Body.ExecutionPayload.BlockNumber, - GasLimit: v1alpha1Block.Body.ExecutionPayload.GasLimit, - GasUsed: v1alpha1Block.Body.ExecutionPayload.GasUsed, - Timestamp: v1alpha1Block.Body.ExecutionPayload.Timestamp, - ExtraData: bytesutil.SafeCopyBytes(v1alpha1Block.Body.ExecutionPayload.ExtraData), - BaseFeePerGas: bytesutil.SafeCopyBytes(v1alpha1Block.Body.ExecutionPayload.BaseFeePerGas), - BlockHash: bytesutil.SafeCopyBytes(v1alpha1Block.Body.ExecutionPayload.BlockHash), - TransactionsRoot: transactionsRoot[:], - WithdrawalsRoot: withdrawalsRoot[:], - }, - BlsToExecutionChanges: changes, - } - v2Block := ðpbv2.BlindedBeaconBlockCapella{ - Slot: v1alpha1Block.Slot, - ProposerIndex: v1alpha1Block.ProposerIndex, - ParentRoot: bytesutil.SafeCopyBytes(v1alpha1Block.ParentRoot), - StateRoot: bytesutil.SafeCopyBytes(v1alpha1Block.StateRoot), - Body: resultBlockBody, - } - return v2Block, nil -} - // BeaconStateAltairToProto converts a state.BeaconState object to its protobuf equivalent. func BeaconStateAltairToProto(altairState state.BeaconState) (*ethpbv2.BeaconState, error) { sourceFork := altairState.Fork() From 93cca9d56c98f2bd8861b5fa2592fa7aef3300d8 Mon Sep 17 00:00:00 2001 From: rkapka Date: Wed, 14 Dec 2022 15:21:41 +0100 Subject: [PATCH 17/18] fix build issues --- beacon-chain/rpc/eth/beacon/blocks.go | 8 ++++---- consensus-types/payload-attribute/getters_test.go | 4 ++-- proto/engine/v1/json_marshal_unmarshal.go | 8 ++++---- proto/engine/v1/json_marshal_unmarshal_test.go | 8 ++++---- 4 files changed, 14 insertions(+), 14 deletions(-) diff --git a/beacon-chain/rpc/eth/beacon/blocks.go b/beacon-chain/rpc/eth/beacon/blocks.go index 628e4fcaf0b8..d7ea8dbf85a3 100644 --- a/beacon-chain/rpc/eth/beacon/blocks.go +++ b/beacon-chain/rpc/eth/beacon/blocks.go @@ -663,7 +663,7 @@ func (bs *Server) getBlockBellatrix(ctx context.Context, blk interfaces.SignedBe if blindedBellatrixBlk == nil { return nil, errNilBlock } - signedFullBlock, err := bs.ExecutionPayloadReconstructor.ReconstructFullBellatrixBlock(ctx, blk) + signedFullBlock, err := bs.ExecutionPayloadReconstructor.ReconstructFullBlock(ctx, blk) if err != nil { return nil, errors.Wrapf(err, "could not reconstruct full execution payload to create signed beacon block") } @@ -733,7 +733,7 @@ func (bs *Server) getBlockCapella(ctx context.Context, blk interfaces.SignedBeac if blindedCapellaBlk == nil { return nil, errNilBlock } - signedFullBlock, err := bs.ExecutionPayloadReconstructor.ReconstructFullBellatrixBlock(ctx, blk) + signedFullBlock, err := bs.ExecutionPayloadReconstructor.ReconstructFullBlock(ctx, blk) if err != nil { return nil, errors.Wrapf(err, "could not reconstruct full execution payload to create signed beacon block") } @@ -846,7 +846,7 @@ func (bs *Server) getSSZBlockBellatrix(ctx context.Context, blk interfaces.Signe if blindedBellatrixBlk == nil { return nil, errNilBlock } - signedFullBlock, err := bs.ExecutionPayloadReconstructor.ReconstructFullBellatrixBlock(ctx, blk) + signedFullBlock, err := bs.ExecutionPayloadReconstructor.ReconstructFullBlock(ctx, blk) if err != nil { return nil, errors.Wrapf(err, "could not reconstruct full execution payload to create signed beacon block") } @@ -922,7 +922,7 @@ func (bs *Server) getSSZBlockCapella(ctx context.Context, blk interfaces.SignedB if blindedCapellaBlk == nil { return nil, errNilBlock } - signedFullBlock, err := bs.ExecutionPayloadReconstructor.ReconstructFullBellatrixBlock(ctx, blk) + signedFullBlock, err := bs.ExecutionPayloadReconstructor.ReconstructFullBlock(ctx, blk) if err != nil { return nil, errors.Wrapf(err, "could not reconstruct full execution payload to create signed beacon block") } diff --git a/consensus-types/payload-attribute/getters_test.go b/consensus-types/payload-attribute/getters_test.go index ea5ece102393..cb357bd4a632 100644 --- a/consensus-types/payload-attribute/getters_test.go +++ b/consensus-types/payload-attribute/getters_test.go @@ -58,7 +58,7 @@ func TestPayloadAttributeGetters(t *testing.T) { { name: "Get withdrawals (capella)", tc: func(t *testing.T) { - wd := []*enginev1.Withdrawal{{WithdrawalIndex: 1}, {WithdrawalIndex: 2}, {WithdrawalIndex: 3}} + wd := []*enginev1.Withdrawal{{Index: 1}, {Index: 2}, {Index: 3}} a, err := New(&enginev1.PayloadAttributesV2{Withdrawals: wd}) require.NoError(t, err) got, err := a.Withdrawals() @@ -126,7 +126,7 @@ func TestPayloadAttributeGetters(t *testing.T) { Timestamp: 1, PrevRandao: []byte{1, 2, 3}, SuggestedFeeRecipient: []byte{4, 5, 6}, - Withdrawals: []*enginev1.Withdrawal{{WithdrawalIndex: 1}, {WithdrawalIndex: 2}, {WithdrawalIndex: 3}}, + Withdrawals: []*enginev1.Withdrawal{{Index: 1}, {Index: 2}, {Index: 3}}, } a, err := New(p) require.NoError(t, err) diff --git a/proto/engine/v1/json_marshal_unmarshal.go b/proto/engine/v1/json_marshal_unmarshal.go index dda72bc83656..15b804512412 100644 --- a/proto/engine/v1/json_marshal_unmarshal.go +++ b/proto/engine/v1/json_marshal_unmarshal.go @@ -160,9 +160,9 @@ func (j *withdrawalJSON) ToWithdrawal() (*Withdrawal, error) { } func (w *Withdrawal) MarshalJSON() ([]byte, error) { - index := hexutil.Uint64(w.WithdrawalIndex) + index := hexutil.Uint64(w.Index) validatorIndex := hexutil.Uint64(w.ValidatorIndex) - address := common.BytesToAddress(w.ExecutionAddress) + address := common.BytesToAddress(w.Address) wei := new(big.Int).SetUint64(1000000000) amountWei := new(big.Int).Mul(new(big.Int).SetUint64(w.Amount), wei) return json.Marshal(withdrawalJSON{ @@ -188,9 +188,9 @@ func (w *Withdrawal) UnmarshalJSON(enc []byte) error { return errors.New("missing execution address") } *w = Withdrawal{} - w.WithdrawalIndex = uint64(*dec.Index) + w.Index = uint64(*dec.Index) w.ValidatorIndex = types.ValidatorIndex(*dec.Validator) - w.ExecutionAddress = dec.Address.Bytes() + w.Address = dec.Address.Bytes() wei := new(big.Int).SetUint64(1000000000) amountWei, err := hexutil.DecodeBig(dec.Amount) if err != nil { diff --git a/proto/engine/v1/json_marshal_unmarshal_test.go b/proto/engine/v1/json_marshal_unmarshal_test.go index 615d899fa030..8ed09ee27e35 100644 --- a/proto/engine/v1/json_marshal_unmarshal_test.go +++ b/proto/engine/v1/json_marshal_unmarshal_test.go @@ -397,13 +397,13 @@ func TestJsonMarshalUnmarshal(t *testing.T) { require.DeepEqual(t, want.MixDigest, payloadPb.MixDigest) require.DeepEqual(t, want.Nonce, payloadPb.Nonce) require.Equal(t, 2, len(payloadPb.Withdrawals)) - require.Equal(t, uint64(1), payloadPb.Withdrawals[0].WithdrawalIndex) + require.Equal(t, uint64(1), payloadPb.Withdrawals[0].Index) require.Equal(t, types.ValidatorIndex(1), payloadPb.Withdrawals[0].ValidatorIndex) - require.DeepEqual(t, bytesutil.PadTo([]byte("address1"), 20), payloadPb.Withdrawals[0].ExecutionAddress) + require.DeepEqual(t, bytesutil.PadTo([]byte("address1"), 20), payloadPb.Withdrawals[0].Address) require.Equal(t, uint64(1), payloadPb.Withdrawals[0].Amount) - require.Equal(t, uint64(2), payloadPb.Withdrawals[1].WithdrawalIndex) + require.Equal(t, uint64(2), payloadPb.Withdrawals[1].Index) require.Equal(t, types.ValidatorIndex(2), payloadPb.Withdrawals[1].ValidatorIndex) - require.DeepEqual(t, bytesutil.PadTo([]byte("address2"), 20), payloadPb.Withdrawals[1].ExecutionAddress) + require.DeepEqual(t, bytesutil.PadTo([]byte("address2"), 20), payloadPb.Withdrawals[1].Address) require.Equal(t, uint64(2), payloadPb.Withdrawals[1].Amount) }) } From 0c7604c8620ec099118438cd78f2b811c8408777 Mon Sep 17 00:00:00 2001 From: rkapka Date: Thu, 15 Dec 2022 15:20:35 +0100 Subject: [PATCH 18/18] replace ioutil with io --- beacon-chain/rpc/apimiddleware/custom_hooks.go | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/beacon-chain/rpc/apimiddleware/custom_hooks.go b/beacon-chain/rpc/apimiddleware/custom_hooks.go index 929a1d28646b..8c2c11381413 100644 --- a/beacon-chain/rpc/apimiddleware/custom_hooks.go +++ b/beacon-chain/rpc/apimiddleware/custom_hooks.go @@ -5,7 +5,6 @@ import ( "encoding/json" "fmt" "io" - "io/ioutil" "net/http" "strconv" "strings" @@ -345,7 +344,7 @@ func setInitialPublishBlindedBlockPostRequest(endpoint *apimiddleware.Endpoint, } }{} - buf, err := ioutil.ReadAll(req.Body) + buf, err := io.ReadAll(req.Body) if err != nil { return false, apimiddleware.InternalServerErrorWithMessage(err, "could not read body") } @@ -366,7 +365,7 @@ func setInitialPublishBlindedBlockPostRequest(endpoint *apimiddleware.Endpoint, } else { endpoint.PostRequest = &SignedBlindedBeaconBlockCapellaContainerJson{} } - req.Body = ioutil.NopCloser(bytes.NewBuffer(buf)) + req.Body = io.NopCloser(bytes.NewBuffer(buf)) return true, nil }