Skip to content

Commit

Permalink
Handle both snake_case and camelCase for BlindedBeaconBlockBodyPartial
Browse files Browse the repository at this point in the history
  • Loading branch information
jparyani committed Dec 17, 2021
1 parent 48203bf commit 1ce6950
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 4 deletions.
62 changes: 62 additions & 0 deletions lib/router_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"math/big"
"net/http"
"net/http/httptest"
"strings"
"testing"

"github.com/ethereum/go-ethereum/common"
Expand Down Expand Up @@ -352,6 +353,8 @@ func TestRelayervice_GetPayloadAndPropose(t *testing.T) {
TransactionsRoot: common.HexToHash("0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421"),
}
payloadBytes, err := json.Marshal(payload)
// make block_hash be snake_case
payloadBytes = []byte(strings.Replace(string(payloadBytes), "blockHash", "block_hash", -1))
require.Nil(t, err)

tests := []httpTestWithMethods{
Expand Down Expand Up @@ -399,3 +402,62 @@ func TestRelayervice_GetPayloadAndPropose(t *testing.T) {
testHTTPMethodWithDifferentRPC(t, tt.jsonRPCMethodCaller, tt.jsonRPCMethodProxy, &tt.httpTest, tt.skipRespCheck, store)
}
}

func TestRelayervice_GetPayloadAndProposeCamelCase(t *testing.T) {
store := NewStore()

payload := ExecutionPayloadWithTxRootV1{
BlockHash: common.HexToHash("0x0000000000000000000000000000000000000000000000000000000000000001"),
StateRoot: common.HexToHash("0x0000000000000000000000000000000000000000000000000000000000000003"),
BaseFeePerGas: big.NewInt(4),
Transactions: &[]string{},
TransactionsRoot: common.HexToHash("0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421"),
}
payloadBytes, err := json.Marshal(payload)
require.Nil(t, err)

tests := []httpTestWithMethods{
{
httpTest{
"get payload from execution and store it",
[]interface{}{"0x1"},
payload,
200,
200,
1,
0,
false,
true,
},
"builder_getPayloadHeaderV1",
"engine_getPayloadV1",
true, // this endpoint transforms Transactions into TransactionsRoot, so skip equality check
},
{
httpTest{
"block cache hit",
[]interface{}{SignedBlindedBeaconBlock{
Message: &BlindedBeaconBlock{
ParentRoot: "0x0000000000000000000000000000000000000000000000000000000000000001",
StateRoot: "0x0000000000000000000000000000000000000000000000000000000000000003",
Body: []byte(`{"executionPayloadHeader": ` + string(payloadBytes) + `}`),
},
Signature: "0x0000000000000000000000000000000000000000000000000000000000000002",
}},
payload,
200,
200,
0,
0,
false,
false,
},
"builder_proposeBlindedBlockV1",
"builder_proposeBlindedBlockV1",
false,
},
}
for _, tt := range tests {
testHTTPMethodWithDifferentRPC(t, tt.jsonRPCMethodCaller, tt.jsonRPCMethodProxy, &tt.httpTest, tt.skipRespCheck, store)
}
}
14 changes: 13 additions & 1 deletion lib/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,19 @@ func (m *RelayService) ProposeBlindedBlockV1(r *http.Request, args *SignedBlinde
return err
}

payloadCached := m.store.Get(common.HexToHash(body.ExecutionPayload.BlockHash))
var blockHash string
// Deal with allowing both camelCase and snake_case in BlindedBlock
if body.ExecutionPayload.BlockHash != "" {
blockHash = body.ExecutionPayload.BlockHash
} else if body.ExecutionPayload.BlockHashCamel != "" {
blockHash = body.ExecutionPayload.BlockHashCamel
} else if body.ExecutionPayloadCamel.BlockHash != "" {
blockHash = body.ExecutionPayloadCamel.BlockHash
} else if body.ExecutionPayloadCamel.BlockHashCamel != "" {
blockHash = body.ExecutionPayloadCamel.BlockHashCamel
}

payloadCached := m.store.Get(common.HexToHash(blockHash))
if payloadCached != nil {
log.Println(green("ProposeBlindedBlockV1: ✓ revealing previous payload from execution client: "), payloadCached.BlockHash, payloadCached.Number, payloadCached.TransactionsRoot)
*result = *payloadCached
Expand Down
8 changes: 5 additions & 3 deletions lib/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,10 @@ type BlindedBeaconBlock struct {
Body json.RawMessage `json:"body"`
}

// BlindedBeaconBlockBodyPartial a partial block body only containing a payload
// BlindedBeaconBlockBodyPartial a partial block body only containing a payload, in both snake_case and camelCase
type BlindedBeaconBlockBodyPartial struct {
ExecutionPayload ExecutionPayloadHeaderOnlyBlockHash `json:"execution_payload_header"`
ExecutionPayload ExecutionPayloadHeaderOnlyBlockHash `json:"execution_payload_header"`
ExecutionPayloadCamel ExecutionPayloadHeaderOnlyBlockHash `json:"executionPayloadHeader"`
}

//go:generate go run github.com/fjl/gencodec -type ExecutionPayloadWithTxRootV1 -field-override executionPayloadHeaderMarshaling -out gen_ed.go
Expand All @@ -51,7 +52,8 @@ type ExecutionPayloadWithTxRootV1 struct {

// ExecutionPayloadHeaderOnlyBlockHash an execution payload with only a block hash, used for BlindedBeaconBlockBodyPartial
type ExecutionPayloadHeaderOnlyBlockHash struct {
BlockHash string `json:"block_hash"`
BlockHash string `json:"block_hash"`
BlockHashCamel string `json:"blockHash"`
}

// JSON type overrides for executableData.
Expand Down

0 comments on commit 1ce6950

Please sign in to comment.