From df044b4e6fbbef89aad05d125a757628a776fec2 Mon Sep 17 00:00:00 2001 From: terence tsao Date: Fri, 14 Jul 2023 09:03:16 +0100 Subject: [PATCH] fix: use hexutil bytes for blobs bundle --- beacon-chain/execution/engine_client_test.go | 6 +++--- encoding/bytesutil/BUILD.bazel | 2 ++ encoding/bytesutil/bytes.go | 14 ++++++++++++++ encoding/bytesutil/bytes_test.go | 9 +++++++++ proto/engine/v1/json_marshal_unmarshal.go | 12 ++++++------ proto/engine/v1/json_marshal_unmarshal_test.go | 6 +++--- 6 files changed, 37 insertions(+), 12 deletions(-) diff --git a/beacon-chain/execution/engine_client_test.go b/beacon-chain/execution/engine_client_test.go index 3476258b5b61..1277d9e88c65 100644 --- a/beacon-chain/execution/engine_client_test.go +++ b/beacon-chain/execution/engine_client_test.go @@ -1479,9 +1479,9 @@ func fixtures() map[string]interface{} { }, BlockValue: "0x11fffffffff", BlobsBundle: &pb.BlobBundleJSON{ - Commitments: [][48]byte{bytesutil.ToBytes48([]byte("commitment1")), bytesutil.ToBytes48([]byte("commitment2"))}, - Proofs: [][48]byte{bytesutil.ToBytes48([]byte("proof1")), bytesutil.ToBytes48([]byte("proof2"))}, - Blobs: [][]byte{{'a'}, {'b'}}, + Commitments: []hexutil.Bytes{[]byte("commitment1"), []byte("commitment2")}, + Proofs: []hexutil.Bytes{[]byte("proof1"), []byte("proof2")}, + Blobs: []hexutil.Bytes{{'a'}, {'b'}}, }, } parent := bytesutil.PadTo([]byte("parentHash"), fieldparams.RootLength) diff --git a/encoding/bytesutil/BUILD.bazel b/encoding/bytesutil/BUILD.bazel index d64411a30818..07abff1b7142 100644 --- a/encoding/bytesutil/BUILD.bazel +++ b/encoding/bytesutil/BUILD.bazel @@ -16,6 +16,7 @@ go_library( deps = [ "//config/fieldparams:go_default_library", "//consensus-types/primitives:go_default_library", + "@com_github_ethereum_go_ethereum//common/hexutil:go_default_library", "@com_github_pkg_errors//:go_default_library", ], ) @@ -35,5 +36,6 @@ go_test( "//config/fieldparams:go_default_library", "//testing/assert:go_default_library", "//testing/require:go_default_library", + "@com_github_ethereum_go_ethereum//common/hexutil:go_default_library", ], ) diff --git a/encoding/bytesutil/bytes.go b/encoding/bytesutil/bytes.go index 42eb1977bc4b..0c21a56ce387 100644 --- a/encoding/bytesutil/bytes.go +++ b/encoding/bytesutil/bytes.go @@ -3,6 +3,8 @@ package bytesutil import ( "fmt" + + "github.com/ethereum/go-ethereum/common/hexutil" ) // ToBytes48Array is a convenience method for converting an array of @@ -100,6 +102,18 @@ func SafeCopy2d32Bytes(ary [][32]byte) [][32]byte { return nil } +// SafeCopy2dHexUtilBytes will copy and return a non-nil 2d hex util byte slice, otherwise it returns nil. +func SafeCopy2dHexUtilBytes(ary []hexutil.Bytes) [][]byte { + if ary != nil { + copied := make([][]byte, len(ary)) + for i, a := range ary { + copied[i] = SafeCopyBytes(a) + } + return copied + } + return nil +} + // ReverseBytes32Slice will reverse the provided slice's order. func ReverseBytes32Slice(arr [][32]byte) [][32]byte { for i, j := 0, len(arr)-1; i < j; i, j = i+1, j-1 { diff --git a/encoding/bytesutil/bytes_test.go b/encoding/bytesutil/bytes_test.go index 35708f24f173..dfd69b272343 100644 --- a/encoding/bytesutil/bytes_test.go +++ b/encoding/bytesutil/bytes_test.go @@ -5,6 +5,7 @@ import ( "reflect" "testing" + "github.com/ethereum/go-ethereum/common/hexutil" "github.com/prysmaticlabs/prysm/v4/encoding/bytesutil" "github.com/prysmaticlabs/prysm/v4/testing/assert" ) @@ -161,6 +162,14 @@ func TestSafeCopy2d32Bytes(t *testing.T) { assert.DeepEqual(t, input, output) } +func TestSafeCopy2dHexUtilBytes(t *testing.T) { + input := make([]hexutil.Bytes, 2) + input[0] = hexutil.Bytes{'a'} + input[1] = hexutil.Bytes{'b'} + output := bytesutil.SafeCopy2dHexUtilBytes(input) + assert.DeepEqual(t, output, [][]byte{{'a'}, {'b'}}) +} + func TestToBytes48Array(t *testing.T) { tests := []struct { a [][]byte diff --git a/proto/engine/v1/json_marshal_unmarshal.go b/proto/engine/v1/json_marshal_unmarshal.go index 22e4f511a543..05855b0eae26 100644 --- a/proto/engine/v1/json_marshal_unmarshal.go +++ b/proto/engine/v1/json_marshal_unmarshal.go @@ -713,16 +713,16 @@ func (f *ForkchoiceState) UnmarshalJSON(enc []byte) error { } type BlobBundleJSON struct { - Commitments [][48]byte `json:"commitments"` - Proofs [][48]byte `json:"proofs"` - Blobs [][]byte `json:"blobs"` + Commitments []hexutil.Bytes `json:"commitments"` + Proofs []hexutil.Bytes `json:"proofs"` + Blobs []hexutil.Bytes `json:"blobs"` } func (b BlobBundleJSON) ToProto() *BlobsBundle { return &BlobsBundle{ - KzgCommitments: bytesutil.SafeCopy2dBytes(bytesutil.FromBytes48Array(b.Commitments)), - Proofs: bytesutil.SafeCopy2dBytes(bytesutil.FromBytes48Array(b.Proofs)), - Blobs: bytesutil.SafeCopy2dBytes(b.Blobs), + KzgCommitments: bytesutil.SafeCopy2dHexUtilBytes(b.Commitments), + Proofs: bytesutil.SafeCopy2dHexUtilBytes(b.Proofs), + Blobs: bytesutil.SafeCopy2dHexUtilBytes(b.Blobs), } } diff --git a/proto/engine/v1/json_marshal_unmarshal_test.go b/proto/engine/v1/json_marshal_unmarshal_test.go index d603c23a0b6b..e80504201581 100644 --- a/proto/engine/v1/json_marshal_unmarshal_test.go +++ b/proto/engine/v1/json_marshal_unmarshal_test.go @@ -223,9 +223,9 @@ func TestJsonMarshalUnmarshal(t *testing.T) { resp := &enginev1.GetPayloadV3ResponseJson{ BlobsBundle: &enginev1.BlobBundleJSON{ - Commitments: [][48]byte{{'a'}, {'b'}, {'c'}, {'d'}}, - Proofs: [][48]byte{{'e'}, {'f'}, {'g'}, {'h'}}, - Blobs: [][]byte{{'i'}, {'j'}, {'k'}, {'l'}}, + Commitments: []hexutil.Bytes{{'a'}, {'b'}, {'c'}, {'d'}}, + Proofs: []hexutil.Bytes{{'e'}, {'f'}, {'g'}, {'h'}}, + Blobs: []hexutil.Bytes{{'i'}, {'j'}, {'k'}, {'l'}}, }, BlockValue: fmt.Sprint("0x123"), ExecutionPayload: &enginev1.ExecutionPayloadDenebJSON{