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

Validate blockhash that the relayer responds with #299

Merged
merged 5 commits into from
Oct 20, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
2 changes: 1 addition & 1 deletion server/mock_relay.go
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ func (m *mockRelay) handleGetPayload(w http.ResponseWriter, req *http.Request) {
// Build the default response.
response := m.MakeGetPayloadResponse(
"0xe28385e7bd68df656cd0042b74b69c3104b5356ed1f20eb69f1f925df47a3ab7",
"0xe28385e7bd68df656cd0042b74b69c3104b5356ed1f20eb69f1f925df47a3ab1",
"0x534809bd2b6832edff8d8ce4cb0e50068804fd1ef432c8362ad708a74fdc0e46",
"0xdb65fEd33dc262Fe09D9a2Ba8F80b329BA25f941",
12345,
)
Expand Down
11 changes: 11 additions & 0 deletions server/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -507,6 +507,17 @@ func (m *BoostService) handleGetPayload(w http.ResponseWriter, req *http.Request
return
}

// Ensure the response blockhash matches the response block
calculatedBlockHash, err := types.CalculateHash(responsePayload.Data)
if err != nil {
log.WithError(err).Error("could not calculate block hash")
} else if responsePayload.Data.BlockHash != calculatedBlockHash {
log.WithFields(logrus.Fields{
"calculatedBlockHash": calculatedBlockHash.String(),
"responseBlockHash": responsePayload.Data.BlockHash.String(),
}).Error("responseBlockHash does not equal hash calculated from response block")
}

// Lock before accessing the shared payload
mu.Lock()
defer mu.Unlock()
Expand Down
25 changes: 21 additions & 4 deletions server/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,25 @@ func (be *testBackend) request(t *testing.T, method, path string, payload any) *
return rr
}

func blindedBlockToExecutionPayload(signedBlindedBeaconBlock *types.SignedBlindedBeaconBlock) *types.ExecutionPayload {
header := signedBlindedBeaconBlock.Message.Body.ExecutionPayloadHeader
return &types.ExecutionPayload{
ParentHash: header.ParentHash,
FeeRecipient: header.FeeRecipient,
StateRoot: header.StateRoot,
ReceiptsRoot: header.ReceiptsRoot,
LogsBloom: header.LogsBloom,
Random: header.Random,
BlockNumber: header.BlockNumber,
GasLimit: header.GasLimit,
GasUsed: header.GasUsed,
Timestamp: header.Timestamp,
ExtraData: header.ExtraData,
BaseFeePerGas: header.BaseFeePerGas,
BlockHash: header.BlockHash,
}
}

func TestNewBoostServiceErrors(t *testing.T) {
t.Run("errors when no relays", func(t *testing.T) {
_, err := NewBoostService(BoostServiceOpts{testLog, ":123", []RelayEntry{}, []*url.URL{}, "0x00000000", true, time.Second, time.Second, time.Second})
Expand Down Expand Up @@ -468,7 +487,7 @@ func TestGetPayload(t *testing.T) {
SyncAggregate: &types.SyncAggregate{},
ExecutionPayloadHeader: &types.ExecutionPayloadHeader{
ParentHash: _HexToHash("0xe28385e7bd68df656cd0042b74b69c3104b5356ed1f20eb69f1f925df47a3ab7"),
BlockHash: _HexToHash("0xe28385e7bd68df656cd0042b74b69c3104b5356ed1f20eb69f1f925df47a3ab1"),
BlockHash: _HexToHash("0x534809bd2b6832edff8d8ce4cb0e50068804fd1ef432c8362ad708a74fdc0e46"),
BlockNumber: 12345,
FeeRecipient: _HexToAddress("0xdb65fEd33dc262Fe09D9a2Ba8F80b329BA25f941"),
},
Expand Down Expand Up @@ -617,9 +636,7 @@ func TestGetPayloadToOriginRelayOnly(t *testing.T) {

// Prepare getPayload response
backend.relays[0].GetPayloadResponse = &types.GetPayloadResponse{
Data: &types.ExecutionPayload{
BlockHash: signedBlindedBeaconBlock.Message.Body.ExecutionPayloadHeader.BlockHash,
},
Data: blindedBlockToExecutionPayload(signedBlindedBeaconBlock),
}

// call getPayload, ensure it's only called on relay 0 (origin of the bid)
Expand Down