Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Capella beacon block #11566

Merged
merged 27 commits into from
Oct 31, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
2e356b6
in progress
rkapka Oct 20, 2022
6511fbe
done, no tests yet
rkapka Oct 20, 2022
cf7cd2a
fix ToBlinded()
rkapka Oct 21, 2022
49b0655
Revert "Auxiliary commit to revert individual files from 2e356b6f5b15…
rkapka Oct 21, 2022
dda154a
tests
rkapka Oct 21, 2022
19b61dc
Merge branch '__develop' into capella-block
rkapka Oct 21, 2022
241c081
fix tests
rkapka Oct 24, 2022
32dd4f8
Merge branch '__develop' into capella-block
rkapka Oct 24, 2022
b357390
one more fix
rkapka Oct 24, 2022
fa99628
and one more
rkapka Oct 24, 2022
e8441f4
review
rkapka Oct 24, 2022
5c0ed0e
fix proto_test
rkapka Oct 24, 2022
aeea110
another fix
rkapka Oct 24, 2022
555d55d
do not return error when nil object is wrapped
rkapka Oct 24, 2022
92e6668
allow nil payload in body.Proto()
rkapka Oct 25, 2022
0e6abcf
Merge branch '__develop' into capella-block
rkapka Oct 25, 2022
dc8d0f4
correctly assert error
rkapka Oct 25, 2022
8b03145
nil checks in body.Execution()
rkapka Oct 25, 2022
c562467
Merge branch 'develop' into capella-block
rkapka Oct 25, 2022
84cf3d0
Merge branch 'develop' into capella-block
terencechain Oct 27, 2022
141e20c
Merge branch 'develop' into capella-block
rkapka Oct 27, 2022
5736c1f
simplify PR
rkapka Oct 28, 2022
75f535a
Revert "Auxiliary commit to revert individual files from 5736c1f22f2d…
rkapka Oct 28, 2022
541e010
fix slice sizes in cloner tests
rkapka Oct 28, 2022
a6a599d
better payload tests
rkapka Oct 31, 2022
393e12f
Merge branch 'develop' into capella-block
rkapka Oct 31, 2022
979f443
review
rkapka Oct 31, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
151 changes: 151 additions & 0 deletions consensus-types/blocks/execution_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,113 @@ func TestWrapExecutionPayloadHeader_SSZ(t *testing.T) {
assert.NoError(t, wsb.UnmarshalSSZ(encoded))
}

func TestWrapExecutionPayloadCapella(t *testing.T) {
data := &enginev1.ExecutionPayloadCapella{
ParentHash: []byte("parenthash"),
FeeRecipient: []byte("feerecipient"),
StateRoot: []byte("stateroot"),
ReceiptsRoot: []byte("receiptsroot"),
LogsBloom: []byte("logsbloom"),
PrevRandao: []byte("prevrandao"),
BlockNumber: 11,
GasLimit: 22,
GasUsed: 33,
Timestamp: 44,
ExtraData: []byte("extradata"),
BaseFeePerGas: []byte("basefeepergas"),
BlockHash: []byte("blockhash"),
Transactions: [][]byte{[]byte("transaction")},
Withdrawals: []*enginev1.Withdrawal{{
WithdrawalIndex: 55,
ValidatorIndex: 66,
ExecutionAddress: []byte("executionaddress"),
Amount: 77,
}},
}
payload, err := blocks.WrappedExecutionPayloadCapella(data)
require.NoError(t, err)

assert.DeepEqual(t, data, payload.Proto())
}

func TestWrapExecutionPayloadHeaderCapella(t *testing.T) {
data := &enginev1.ExecutionPayloadHeaderCapella{
ParentHash: []byte("parenthash"),
FeeRecipient: []byte("feerecipient"),
StateRoot: []byte("stateroot"),
ReceiptsRoot: []byte("receiptsroot"),
LogsBloom: []byte("logsbloom"),
PrevRandao: []byte("prevrandao"),
BlockNumber: 11,
GasLimit: 22,
GasUsed: 33,
Timestamp: 44,
ExtraData: []byte("extradata"),
BaseFeePerGas: []byte("basefeepergas"),
BlockHash: []byte("blockhash"),
TransactionsRoot: []byte("transactionsroot"),
WithdrawalsRoot: []byte("withdrawalsroot"),
}
payload, err := blocks.WrappedExecutionPayloadHeaderCapella(data)
require.NoError(t, err)

assert.DeepEqual(t, data, payload.Proto())
}

func TestWrapExecutionPayloadCapella_IsNil(t *testing.T) {
_, err := blocks.WrappedExecutionPayloadCapella(nil)
require.Equal(t, blocks.ErrNilObjectWrapped, err)

data := &enginev1.ExecutionPayloadCapella{GasUsed: 54}
payload, err := blocks.WrappedExecutionPayloadCapella(data)
require.NoError(t, err)

assert.Equal(t, false, payload.IsNil())
}

func TestWrapExecutionPayloadHeaderCapella_IsNil(t *testing.T) {
_, err := blocks.WrappedExecutionPayloadHeaderCapella(nil)
require.Equal(t, blocks.ErrNilObjectWrapped, err)

data := &enginev1.ExecutionPayloadHeaderCapella{GasUsed: 54}
payload, err := blocks.WrappedExecutionPayloadHeaderCapella(data)
require.NoError(t, err)

assert.Equal(t, false, payload.IsNil())
}

func TestWrapExecutionPayloadCapella_SSZ(t *testing.T) {
payload := createWrappedPayloadCapella(t)
rt, err := payload.HashTreeRoot()
assert.NoError(t, err)
assert.NotEmpty(t, rt)

var b []byte
b, err = payload.MarshalSSZTo(b)
assert.NoError(t, err)
assert.NotEqual(t, 0, len(b))
encoded, err := payload.MarshalSSZ()
require.NoError(t, err)
assert.NotEqual(t, 0, payload.SizeSSZ())
assert.NoError(t, payload.UnmarshalSSZ(encoded))
}

func TestWrapExecutionPayloadHeaderCapella_SSZ(t *testing.T) {
payload := createWrappedPayloadHeaderCapella(t)
rt, err := payload.HashTreeRoot()
assert.NoError(t, err)
assert.NotEmpty(t, rt)

var b []byte
b, err = payload.MarshalSSZTo(b)
assert.NoError(t, err)
assert.NotEqual(t, 0, len(b))
encoded, err := payload.MarshalSSZ()
require.NoError(t, err)
assert.NotEqual(t, 0, payload.SizeSSZ())
assert.NoError(t, payload.UnmarshalSSZ(encoded))
}

func createWrappedPayload(t testing.TB) interfaces.ExecutionData {
wsb, err := blocks.WrappedExecutionPayload(&enginev1.ExecutionPayload{
ParentHash: make([]byte, fieldparams.RootLength),
Expand Down Expand Up @@ -122,3 +229,47 @@ func createWrappedPayloadHeader(t testing.TB) interfaces.ExecutionData {
require.NoError(t, err)
return wsb
}

func createWrappedPayloadCapella(t testing.TB) interfaces.ExecutionData {
payload, err := blocks.WrappedExecutionPayloadCapella(&enginev1.ExecutionPayloadCapella{
ParentHash: make([]byte, fieldparams.RootLength),
FeeRecipient: make([]byte, fieldparams.FeeRecipientLength),
StateRoot: make([]byte, fieldparams.RootLength),
ReceiptsRoot: make([]byte, fieldparams.RootLength),
LogsBloom: make([]byte, fieldparams.LogsBloomLength),
PrevRandao: make([]byte, fieldparams.RootLength),
BlockNumber: 0,
GasLimit: 0,
GasUsed: 0,
Timestamp: 0,
ExtraData: make([]byte, 0),
BaseFeePerGas: make([]byte, fieldparams.RootLength),
BlockHash: make([]byte, fieldparams.RootLength),
Transactions: make([][]byte, 0),
Withdrawals: make([]*enginev1.Withdrawal, 0),
})
require.NoError(t, err)
return payload
}

func createWrappedPayloadHeaderCapella(t testing.TB) interfaces.ExecutionData {
payload, err := blocks.WrappedExecutionPayloadHeaderCapella(&enginev1.ExecutionPayloadHeaderCapella{
ParentHash: make([]byte, fieldparams.RootLength),
FeeRecipient: make([]byte, fieldparams.FeeRecipientLength),
StateRoot: make([]byte, fieldparams.RootLength),
ReceiptsRoot: make([]byte, fieldparams.RootLength),
LogsBloom: make([]byte, fieldparams.LogsBloomLength),
PrevRandao: make([]byte, fieldparams.RootLength),
BlockNumber: 0,
GasLimit: 0,
GasUsed: 0,
Timestamp: 0,
ExtraData: make([]byte, 0),
BaseFeePerGas: make([]byte, fieldparams.RootLength),
BlockHash: make([]byte, fieldparams.RootLength),
TransactionsRoot: make([]byte, fieldparams.RootLength),
WithdrawalsRoot: make([]byte, fieldparams.RootLength),
})
require.NoError(t, err)
return payload
}
128 changes: 103 additions & 25 deletions consensus-types/blocks/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,14 @@ func NewSignedBeaconBlock(i interface{}) (interfaces.SignedBeaconBlock, error) {
return initBlindedSignedBlockFromProtoBellatrix(b.BlindedBellatrix)
case *eth.SignedBlindedBeaconBlockBellatrix:
return initBlindedSignedBlockFromProtoBellatrix(b)
case *eth.GenericSignedBeaconBlock_Capella:
return initSignedBlockFromProtoCapella(b.Capella)
case *eth.SignedBeaconBlockCapella:
return initSignedBlockFromProtoCapella(b)
case *eth.GenericSignedBeaconBlock_BlindedCapella:
return initBlindedSignedBlockFromProtoCapella(b.BlindedCapella)
case *eth.SignedBlindedBeaconBlockCapella:
return initBlindedSignedBlockFromProtoCapella(b)
default:
return nil, errors.Wrapf(ErrUnsupportedSignedBeaconBlock, "unable to create block from type %T", i)
}
Expand All @@ -73,6 +81,14 @@ func NewBeaconBlock(i interface{}) (interfaces.BeaconBlock, error) {
return initBlindedBlockFromProtoBellatrix(b.BlindedBellatrix)
case *eth.BlindedBeaconBlockBellatrix:
return initBlindedBlockFromProtoBellatrix(b)
case *eth.GenericBeaconBlock_Capella:
return initBlockFromProtoCapella(b.Capella)
case *eth.BeaconBlockCapella:
return initBlockFromProtoCapella(b)
case *eth.GenericBeaconBlock_BlindedCapella:
return initBlindedBlockFromProtoCapella(b.BlindedCapella)
case *eth.BlindedBeaconBlockCapella:
return initBlindedBlockFromProtoCapella(b)
default:
return nil, errors.Wrapf(errUnsupportedBeaconBlock, "unable to create block from type %T", i)
}
Expand All @@ -91,6 +107,10 @@ func NewBeaconBlockBody(i interface{}) (interfaces.BeaconBlockBody, error) {
return initBlockBodyFromProtoBellatrix(b)
case *eth.BlindedBeaconBlockBodyBellatrix:
return initBlindedBlockBodyFromProtoBellatrix(b)
case *eth.BeaconBlockBodyCapella:
return initBlockBodyFromProtoCapella(b)
case *eth.BlindedBeaconBlockBodyCapella:
return initBlindedBlockBodyFromProtoCapella(b)
default:
return nil, errors.Wrapf(errUnsupportedBeaconBlockBody, "unable to create block body from type %T", i)
}
Expand Down Expand Up @@ -131,6 +151,19 @@ func BuildSignedBeaconBlock(blk interfaces.BeaconBlock, signature []byte) (inter
return nil, errIncorrectBlockVersion
}
return NewSignedBeaconBlock(&eth.SignedBeaconBlockBellatrix{Block: pb, Signature: signature})
case version.Capella:
if blk.IsBlinded() {
pb, ok := pb.(*eth.BlindedBeaconBlockCapella)
if !ok {
return nil, errIncorrectBlockVersion
}
return NewSignedBeaconBlock(&eth.SignedBlindedBeaconBlockCapella{Block: pb, Signature: signature})
}
pb, ok := pb.(*eth.BeaconBlockCapella)
if !ok {
return nil, errIncorrectBlockVersion
}
return NewSignedBeaconBlock(&eth.SignedBeaconBlockCapella{Block: pb, Signature: signature})
default:
return nil, errUnsupportedBeaconBlock
}
Expand All @@ -139,7 +172,7 @@ func BuildSignedBeaconBlock(blk interfaces.BeaconBlock, signature []byte) (inter
// BuildSignedBeaconBlockFromExecutionPayload takes a signed, blinded beacon block and converts into
// a full, signed beacon block by specifying an execution payload.
func BuildSignedBeaconBlockFromExecutionPayload(
blk interfaces.SignedBeaconBlock, payload *enginev1.ExecutionPayload,
blk interfaces.SignedBeaconBlock, payload interface{},
) (interfaces.SignedBeaconBlock, error) {
if err := BeaconBlockIsNil(blk); err != nil {
return nil, err
Expand All @@ -153,16 +186,26 @@ func BuildSignedBeaconBlockFromExecutionPayload(
return nil, errors.Wrap(err, "could not get execution payload header")
default:
}
wrappedPayload, err := WrappedExecutionPayload(payload)
if err != nil {
return nil, err

var wrappedPayload interfaces.ExecutionData
var wrapErr error
switch p := payload.(type) {
case *enginev1.ExecutionPayload:
wrappedPayload, wrapErr = WrappedExecutionPayload(p)
case *enginev1.ExecutionPayloadCapella:
wrappedPayload, wrapErr = WrappedExecutionPayloadCapella(p)
default:
return nil, fmt.Errorf("%T is not a type of execution payload", p)
}
if wrapErr != nil {
return nil, wrapErr
}
empty, err := IsEmptyExecutionData(wrappedPayload)
if err != nil {
return nil, err
}
if !empty {
payloadRoot, err := payload.HashTreeRoot()
payloadRoot, err := wrappedPayload.HashTreeRoot()
if err != nil {
return nil, errors.Wrap(err, "could not hash tree root execution payload")
}
Expand All @@ -187,26 +230,61 @@ func BuildSignedBeaconBlockFromExecutionPayload(
randaoReveal := b.Body().RandaoReveal()
graffiti := b.Body().Graffiti()
sig := blk.Signature()
bellatrixFullBlock := &eth.SignedBeaconBlockBellatrix{
Block: &eth.BeaconBlockBellatrix{
Slot: b.Slot(),
ProposerIndex: b.ProposerIndex(),
ParentRoot: parentRoot[:],
StateRoot: stateRoot[:],
Body: &eth.BeaconBlockBodyBellatrix{
RandaoReveal: randaoReveal[:],
Eth1Data: b.Body().Eth1Data(),
Graffiti: graffiti[:],
ProposerSlashings: b.Body().ProposerSlashings(),
AttesterSlashings: b.Body().AttesterSlashings(),
Attestations: b.Body().Attestations(),
Deposits: b.Body().Deposits(),
VoluntaryExits: b.Body().VoluntaryExits(),
SyncAggregate: syncAgg,
ExecutionPayload: payload,

var fullBlock interface{}
switch p := payload.(type) {
case *enginev1.ExecutionPayload:
fullBlock = &eth.SignedBeaconBlockBellatrix{
Block: &eth.BeaconBlockBellatrix{
Slot: b.Slot(),
ProposerIndex: b.ProposerIndex(),
ParentRoot: parentRoot[:],
StateRoot: stateRoot[:],
Body: &eth.BeaconBlockBodyBellatrix{
RandaoReveal: randaoReveal[:],
Eth1Data: b.Body().Eth1Data(),
Graffiti: graffiti[:],
ProposerSlashings: b.Body().ProposerSlashings(),
AttesterSlashings: b.Body().AttesterSlashings(),
Attestations: b.Body().Attestations(),
Deposits: b.Body().Deposits(),
VoluntaryExits: b.Body().VoluntaryExits(),
SyncAggregate: syncAgg,
ExecutionPayload: p,
},
},
Signature: sig[:],
}
case *enginev1.ExecutionPayloadCapella:
blsToExecutionChanges, err := b.Body().BLSToExecutionChanges()
if err != nil {
return nil, err
}
fullBlock = &eth.SignedBeaconBlockCapella{
Block: &eth.BeaconBlockCapella{
Slot: b.Slot(),
ProposerIndex: b.ProposerIndex(),
ParentRoot: parentRoot[:],
StateRoot: stateRoot[:],
Body: &eth.BeaconBlockBodyCapella{
RandaoReveal: randaoReveal[:],
Eth1Data: b.Body().Eth1Data(),
Graffiti: graffiti[:],
ProposerSlashings: b.Body().ProposerSlashings(),
AttesterSlashings: b.Body().AttesterSlashings(),
Attestations: b.Body().Attestations(),
Deposits: b.Body().Deposits(),
VoluntaryExits: b.Body().VoluntaryExits(),
SyncAggregate: syncAgg,
ExecutionPayload: p,
BlsToExecutionChanges: blsToExecutionChanges,
},
},
},
Signature: sig[:],
Signature: sig[:],
}
default:
return nil, fmt.Errorf("%T is not a type of execution payload", p)
}
return NewSignedBeaconBlock(bellatrixFullBlock)

return NewSignedBeaconBlock(fullBlock)
}
Loading