Skip to content

Commit

Permalink
building out test coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
kasey committed May 16, 2024
1 parent 785bb0c commit d619036
Show file tree
Hide file tree
Showing 4 changed files with 229 additions and 93 deletions.
148 changes: 109 additions & 39 deletions beacon-chain/execution/engine_client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1400,6 +1400,31 @@ func newTestIPCServer(t *testing.T) *rpc.Server {
}

func fixtures() map[string]interface{} {
s := fixturesStruct()
return map[string]interface{}{
"ExecutionBlock": s.ExecutionBlock,
"ExecutionPayloadBody": s.ExecutionPayloadBody,
"ExecutionPayload": s.ExecutionPayload,
"ExecutionPayloadCapella": s.ExecutionPayloadCapella,
"ExecutionPayloadDeneb": s.ExecutionPayloadDeneb,
"ExecutionPayloadElectra": s.ExecutionPayloadElectra,
"ExecutionPayloadCapellaWithValue": s.ExecutionPayloadWithValueCapella,
"ExecutionPayloadDenebWithValue": s.ExecutionPayloadWithValueDeneb,
"ExecutionPayloadElectraWithValue": s.ExecutionPayloadWithValueElectra,
"ValidPayloadStatus": s.ValidPayloadStatus,
"InvalidBlockHashStatus": s.InvalidBlockHashStatus,
"AcceptedStatus": s.AcceptedStatus,
"SyncingStatus": s.SyncingStatus,
"InvalidStatus": s.InvalidStatus,
"UnknownStatus": s.UnknownStatus,
"ForkchoiceUpdatedResponse": s.ForkchoiceUpdatedResponse,
"ForkchoiceUpdatedSyncingResponse": s.ForkchoiceUpdatedSyncingResponse,
"ForkchoiceUpdatedAcceptedResponse": s.ForkchoiceUpdatedAcceptedResponse,
"ForkchoiceUpdatedInvalidResponse": s.ForkchoiceUpdatedInvalidResponse,
}
}

func fixturesStruct() *payloadFixtures {
foo := bytesutil.ToBytes32([]byte("foo"))
bar := bytesutil.PadTo([]byte("bar"), 20)
baz := bytesutil.PadTo([]byte("baz"), 256)
Expand Down Expand Up @@ -1441,7 +1466,7 @@ func fixtures() map[string]interface{} {
Transactions: [][]byte{foo[:]},
Withdrawals: []*pb.Withdrawal{},
}
executionPayloadFixtureDeneb := &pb.ExecutionPayloadDeneb{
emptyExecutionPayloadDeneb := &pb.ExecutionPayloadDeneb{
ParentHash: foo[:],
FeeRecipient: bar,
StateRoot: foo[:],
Expand All @@ -1455,11 +1480,29 @@ func fixtures() map[string]interface{} {
ExtraData: foo[:],
BaseFeePerGas: bytesutil.PadTo(baseFeePerGas.Bytes(), fieldparams.RootLength),
BlockHash: foo[:],
Transactions: [][]byte{foo[:]},
Withdrawals: []*pb.Withdrawal{},
BlobGasUsed: 2,
ExcessBlobGas: 3,
}
executionPayloadFixtureDeneb := &pb.ExecutionPayloadDeneb{
ParentHash: emptyExecutionPayloadDeneb.ParentHash,
FeeRecipient: emptyExecutionPayloadDeneb.FeeRecipient,
StateRoot: emptyExecutionPayloadDeneb.StateRoot,
ReceiptsRoot: emptyExecutionPayloadDeneb.ReceiptsRoot,
LogsBloom: emptyExecutionPayloadDeneb.LogsBloom,
PrevRandao: emptyExecutionPayloadDeneb.PrevRandao,
BlockNumber: emptyExecutionPayloadDeneb.BlockNumber,
GasLimit: emptyExecutionPayloadDeneb.GasLimit,
GasUsed: emptyExecutionPayloadDeneb.GasUsed,
Timestamp: emptyExecutionPayloadDeneb.Timestamp,
ExtraData: emptyExecutionPayloadDeneb.ExtraData,
BaseFeePerGas: emptyExecutionPayloadDeneb.BaseFeePerGas,
BlockHash: emptyExecutionPayloadDeneb.BlockHash,
BlobGasUsed: emptyExecutionPayloadDeneb.BlobGasUsed,
ExcessBlobGas: emptyExecutionPayloadDeneb.ExcessBlobGas,
// added on top of the empty payload
Transactions: [][]byte{foo[:]},
Withdrawals: []*pb.Withdrawal{},
}
withdrawalRequests := make([]pb.WithdrawalRequestV1, 3)
for i := range withdrawalRequests {
amount := hexutil.Uint64(i)
Expand Down Expand Up @@ -1572,22 +1615,24 @@ func fixtures() map[string]interface{} {
executionPayloadWithValueFixtureElectra := &pb.GetPayloadV4ResponseJson{
ShouldOverrideBuilder: true,
ExecutionPayload: &pb.ExecutionPayloadElectraJSON{
ParentHash: &common.Hash{'a'},
FeeRecipient: &common.Address{'b'},
StateRoot: &common.Hash{'c'},
ReceiptsRoot: &common.Hash{'d'},
LogsBloom: &hexutil.Bytes{'e'},
PrevRandao: &common.Hash{'f'},
BaseFeePerGas: "0x123",
BlockHash: &common.Hash{'g'},
Transactions: []hexutil.Bytes{{'h'}},
Withdrawals: []*pb.Withdrawal{},
BlockNumber: &hexUint,
GasLimit: &hexUint,
GasUsed: &hexUint,
Timestamp: &hexUint,
BlobGasUsed: &bgu,
ExcessBlobGas: &ebg,
ParentHash: &common.Hash{'a'},
FeeRecipient: &common.Address{'b'},
StateRoot: &common.Hash{'c'},
ReceiptsRoot: &common.Hash{'d'},
LogsBloom: &hexutil.Bytes{'e'},
PrevRandao: &common.Hash{'f'},
BaseFeePerGas: "0x123",
BlockHash: &common.Hash{'g'},
Transactions: []hexutil.Bytes{{'h'}},
Withdrawals: []*pb.Withdrawal{},
BlockNumber: &hexUint,
GasLimit: &hexUint,
GasUsed: &hexUint,
Timestamp: &hexUint,
BlobGasUsed: &bgu,
ExcessBlobGas: &ebg,
DepositRequests: depositRequests,
WithdrawalRequests: withdrawalRequests,
},
BlockValue: "0x11fffffffff",
BlobsBundle: &pb.BlobBundleJSON{
Expand Down Expand Up @@ -1680,27 +1725,52 @@ func fixtures() map[string]interface{} {
Status: pb.PayloadStatus_UNKNOWN,
LatestValidHash: foo[:],
}
return map[string]interface{}{
"ExecutionBlock": executionBlock,
"ExecutionPayloadBody": executionPayloadBodyFixture,
"ExecutionPayload": executionPayloadFixture,
"ExecutionPayloadCapella": executionPayloadFixtureCapella,
"ExecutionPayloadDeneb": executionPayloadFixtureDeneb,
"ExecutionPayloadElectra": executionPayloadFixtureElectra,
"ExecutionPayloadCapellaWithValue": executionPayloadWithValueFixtureCapella,
"ExecutionPayloadDenebWithValue": executionPayloadWithValueFixtureDeneb,
"ExecutionPayloadElectraWithValue": executionPayloadWithValueFixtureElectra,
"ValidPayloadStatus": validStatus,
"InvalidBlockHashStatus": inValidBlockHashStatus,
"AcceptedStatus": acceptedStatus,
"SyncingStatus": syncingStatus,
"InvalidStatus": invalidStatus,
"UnknownStatus": unknownStatus,
"ForkchoiceUpdatedResponse": forkChoiceResp,
"ForkchoiceUpdatedSyncingResponse": forkChoiceSyncingResp,
"ForkchoiceUpdatedAcceptedResponse": forkChoiceAcceptedResp,
"ForkchoiceUpdatedInvalidResponse": forkChoiceInvalidResp,
return &payloadFixtures{
ExecutionBlock: executionBlock,
ExecutionPayloadBody: executionPayloadBodyFixture,
ExecutionPayload: executionPayloadFixture,
ExecutionPayloadCapella: executionPayloadFixtureCapella,
ExecutionPayloadDeneb: executionPayloadFixtureDeneb,
EmptyExecutionPayloadDeneb: emptyExecutionPayloadDeneb,
ExecutionPayloadElectra: executionPayloadFixtureElectra,
ExecutionPayloadWithValueCapella: executionPayloadWithValueFixtureCapella,
ExecutionPayloadWithValueDeneb: executionPayloadWithValueFixtureDeneb,
ExecutionPayloadWithValueElectra: executionPayloadWithValueFixtureElectra,
ValidPayloadStatus: validStatus,
InvalidBlockHashStatus: inValidBlockHashStatus,
AcceptedStatus: acceptedStatus,
SyncingStatus: syncingStatus,
InvalidStatus: invalidStatus,
UnknownStatus: unknownStatus,
ForkchoiceUpdatedResponse: forkChoiceResp,
ForkchoiceUpdatedSyncingResponse: forkChoiceSyncingResp,
ForkchoiceUpdatedAcceptedResponse: forkChoiceAcceptedResp,
ForkchoiceUpdatedInvalidResponse: forkChoiceInvalidResp,
}

}

type payloadFixtures struct {
ExecutionBlock *pb.ExecutionBlock
ExecutionPayloadBody *pb.ExecutionPayloadBodyV1Or2
ExecutionPayload *pb.ExecutionPayload
ExecutionPayloadCapella *pb.ExecutionPayloadCapella
EmptyExecutionPayloadDeneb *pb.ExecutionPayloadDeneb
ExecutionPayloadDeneb *pb.ExecutionPayloadDeneb
ExecutionPayloadElectra *pb.ExecutionPayloadElectra
ExecutionPayloadWithValueCapella *pb.GetPayloadV2ResponseJson
ExecutionPayloadWithValueDeneb *pb.GetPayloadV3ResponseJson
ExecutionPayloadWithValueElectra *pb.GetPayloadV4ResponseJson
ValidPayloadStatus *pb.PayloadStatus
InvalidBlockHashStatus *pb.PayloadStatus
AcceptedStatus *pb.PayloadStatus
SyncingStatus *pb.PayloadStatus
InvalidStatus *pb.PayloadStatus
UnknownStatus *pb.PayloadStatus
ForkchoiceUpdatedResponse *ForkchoiceUpdatedResponse
ForkchoiceUpdatedSyncingResponse *ForkchoiceUpdatedResponse
ForkchoiceUpdatedAcceptedResponse *ForkchoiceUpdatedResponse
ForkchoiceUpdatedInvalidResponse *ForkchoiceUpdatedResponse
}

func TestHeaderByHash_NotFound(t *testing.T) {
Expand Down
43 changes: 24 additions & 19 deletions beacon-chain/execution/payload_body.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,32 +142,37 @@ func (r *blindedBlockReconstructor) requestBodiesByHash(ctx context.Context, cli
return nilBodies, nil
}

func (r *blindedBlockReconstructor) payloadForHeader(header interfaces.ExecutionData, v int) (proto.Message, error) {
bodyKey := bytesutil.ToBytes32(header.BlockHash())
if bodyKey == params.BeaconConfig().ZeroHash {
payload, err := buildEmptyExecutionPayload(v)
if err != nil {
return nil, errors.Wrapf(err, "failed to reconstruct payload for body hash %#x", bodyKey)
}
return payload, nil
}
body, ok := r.bodies[bodyKey]
if !ok {
return nil, errors.Wrapf(errNilPayloadBody, "hash %#x", bodyKey)
}
ed, err := fullPayloadFromPayloadBody(header, body, v)
if err != nil {
return nil, errors.Wrapf(err, "failed to reconstruct payload for body hash %#x", bodyKey)
}
return ed.Proto(), nil
}

func (r *blindedBlockReconstructor) unblinded() ([]interfaces.SignedBeaconBlock, error) {
unblinded := make([]interfaces.SignedBeaconBlock, len(r.orderedBlocks))
for i := range r.orderedBlocks {
blk, header := r.orderedBlocks[i].block, r.orderedBlocks[i].header
bodyKey := bytesutil.ToBytes32(header.BlockHash())
var payload proto.Message
if bodyKey == params.BeaconConfig().ZeroHash {
var err error
payload, err = buildEmptyExecutionPayload(blk.Version())
if err != nil {
return nil, errors.Wrapf(err, "failed to reconstruct payload for body hash %#x", bodyKey)
}
} else {
body, ok := r.bodies[bodyKey]
if !ok {
return nil, errors.Wrapf(errNilPayloadBody, "hash %#x", bodyKey)
}
ed, err := fullPayloadFromPayloadBody(header, body, blk.Version())
if err != nil {
return nil, errors.Wrapf(err, "failed to reconstruct payload for body hash %#x", bodyKey)
}
payload = ed.Proto()
payload, err := r.payloadForHeader(header, blk.Version())
if err != nil {
return nil, err
}
full, err := blocks.BuildSignedBeaconBlockFromExecutionPayload(blk, payload)
if err != nil {
return nil, errors.Wrapf(err, "failed to build full block from execution payload for block hash %#x", bodyKey)
return nil, errors.Wrapf(err, "failed to build full block from execution payload for block hash %#x", header.BlockHash())
}
unblinded[i] = full
}
Expand Down
Loading

0 comments on commit d619036

Please sign in to comment.