Skip to content

Commit

Permalink
MEV-1263 : Avoid MustFromHex (#35)
Browse files Browse the repository at this point in the history
* MEV-1263 : Avoid MustFromHex

* Improved Err Message

* go mod

* Update capella.go

Co-authored-by: jzblox <[email protected]>

* Update deneb.go

Co-authored-by: jzblox <[email protected]>

* rm import

---------

Co-authored-by: jzblox <[email protected]>
  • Loading branch information
colink14110476 and jzblox authored May 16, 2024
1 parent 6debc5d commit c6a172f
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 9 deletions.
12 changes: 9 additions & 3 deletions capella.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package relay_grpc

import (
"fmt"

"github.com/attestantio/go-builder-client/api/capella"
v1 "github.com/attestantio/go-builder-client/api/v1"
consensusspec "github.com/attestantio/go-eth2-client/spec"
Expand Down Expand Up @@ -110,7 +112,7 @@ func CapellaRequestToProtoRequestWithShortIDs(block *capella.SubmitBlockRequest,
}
}

func ProtoRequestToCapellaRequest(block *SubmitBlockRequest) *capella.SubmitBlockRequest {
func ProtoRequestToCapellaRequest(block *SubmitBlockRequest) (*capella.SubmitBlockRequest, error) {
transactions := []bellatrix.Transaction{}
for _, tx := range block.ExecutionPayload.Transactions {
transactions = append(transactions, tx.RawData)
Expand All @@ -126,6 +128,10 @@ func ProtoRequestToCapellaRequest(block *SubmitBlockRequest) *capella.SubmitBloc
Address: b20(withdrawal.Address),
})
}
value, err := uint256.FromHex(block.BidTrace.Value)
if err != nil {
return nil, fmt.Errorf("failed to convert capella block value %s to uint256: %s", block.BidTrace.Value, err.Error())
}

return &capella.SubmitBlockRequest{
Message: &v1.BidTrace{
Expand All @@ -137,7 +143,7 @@ func ProtoRequestToCapellaRequest(block *SubmitBlockRequest) *capella.SubmitBloc
ProposerFeeRecipient: b20(block.BidTrace.ProposerFeeRecipient),
GasLimit: block.BidTrace.GasLimit,
GasUsed: block.BidTrace.GasUsed,
Value: uint256.MustFromHex(block.BidTrace.Value),
Value: value,
},
ExecutionPayload: &consensus.ExecutionPayload{
ParentHash: b32(block.ExecutionPayload.ParentHash),
Expand All @@ -157,5 +163,5 @@ func ProtoRequestToCapellaRequest(block *SubmitBlockRequest) *capella.SubmitBloc
Withdrawals: withdrawals,
},
Signature: b96(block.Signature),
}
}, nil
}
13 changes: 10 additions & 3 deletions deneb.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package relay_grpc

import (
"fmt"

apiDeneb "github.com/attestantio/go-builder-client/api/deneb"
v1 "github.com/attestantio/go-builder-client/api/v1"
consensusspec "github.com/attestantio/go-eth2-client/spec"
Expand Down Expand Up @@ -120,7 +122,7 @@ func DenebRequestToProtoRequestWithShortIDs(block *apiDeneb.SubmitBlockRequest,
}
}

func ProtoRequestToDenebRequest(block *SubmitBlockRequest) *apiDeneb.SubmitBlockRequest {
func ProtoRequestToDenebRequest(block *SubmitBlockRequest) (*apiDeneb.SubmitBlockRequest, error) {
transactions := []bellatrix.Transaction{}
for _, tx := range block.ExecutionPayload.Transactions {
transactions = append(transactions, tx.RawData)
Expand Down Expand Up @@ -156,6 +158,11 @@ func ProtoRequestToDenebRequest(block *SubmitBlockRequest) *apiDeneb.SubmitBlock
blobsBundle.Blobs = append(blobsBundle.Blobs, consensus.Blob(blob))
}

value, err := uint256.FromHex(block.BidTrace.Value)
if err != nil {
return nil, fmt.Errorf("failed to convert deneb block value %s to uint256: %s", block.BidTrace.Value, err.Error())
}

return &apiDeneb.SubmitBlockRequest{
Message: &v1.BidTrace{
Slot: block.BidTrace.Slot,
Expand All @@ -166,7 +173,7 @@ func ProtoRequestToDenebRequest(block *SubmitBlockRequest) *apiDeneb.SubmitBlock
ProposerFeeRecipient: b20(block.BidTrace.ProposerFeeRecipient),
GasLimit: block.BidTrace.GasLimit,
GasUsed: block.BidTrace.GasUsed,
Value: uint256.MustFromHex(block.BidTrace.Value),
Value: value,
},
ExecutionPayload: &consensus.ExecutionPayload{
ParentHash: b32(block.ExecutionPayload.ParentHash),
Expand All @@ -189,7 +196,7 @@ func ProtoRequestToDenebRequest(block *SubmitBlockRequest) *apiDeneb.SubmitBlock
},
BlobsBundle: blobsBundle,
Signature: b96(block.Signature),
}
}, nil
}

// Add Commitments, Proofs, Data to BlobsBundle
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ require (
github.com/attestantio/go-builder-client v0.4.2
github.com/attestantio/go-eth2-client v0.19.9
github.com/holiman/uint256 v1.2.4
github.com/pkg/errors v0.9.1
google.golang.org/grpc v1.57.0
google.golang.org/protobuf v1.32.0
)
Expand All @@ -21,7 +22,6 @@ require (
github.com/mattn/go-isatty v0.0.20 // indirect
github.com/minio/sha256-simd v1.0.1 // indirect
github.com/mitchellh/mapstructure v1.5.0 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/prysmaticlabs/go-bitfield v0.0.0-20210809151128-385d8c5e3fb7 // indirect
golang.org/x/crypto v0.18.0 // indirect
golang.org/x/net v0.11.0 // indirect
Expand Down
12 changes: 10 additions & 2 deletions utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,22 @@ func VersionedRequestToProtoRequestWithShortIDs(block *builderSpec.VersionedSubm
func ProtoRequestToVersionedRequest(block *SubmitBlockRequest) (*builderSpec.VersionedSubmitBlockRequest, error) {
switch consensusspec.DataVersion(block.Version) {
case consensusspec.DataVersionCapella:
blockRequest, err := ProtoRequestToCapellaRequest(block)
if err != nil {
return nil, err
}
return &builderSpec.VersionedSubmitBlockRequest{
Version: consensusspec.DataVersionCapella,
Capella: ProtoRequestToCapellaRequest(block),
Capella: blockRequest,
}, nil
case consensusspec.DataVersionDeneb:
blockRequest, err := ProtoRequestToDenebRequest(block)
if err != nil {
return nil, err
}
return &builderSpec.VersionedSubmitBlockRequest{
Version: consensusspec.DataVersionDeneb,
Deneb: ProtoRequestToDenebRequest(block),
Deneb: blockRequest,
}, nil
default:
return nil, errors.Wrap(ErrInvalidVersion, fmt.Sprintf("%s is not supported", consensusspec.DataVersion(block.Version)))
Expand Down

0 comments on commit c6a172f

Please sign in to comment.