Skip to content

Commit

Permalink
Upgrade go-boost-utils (#488)
Browse files Browse the repository at this point in the history
* Upgrade go-boost-utils

* pr comments

* remove commented out code
  • Loading branch information
avalonche committed Jan 19, 2024
1 parent ece5601 commit d4bb90a
Show file tree
Hide file tree
Showing 30 changed files with 431 additions and 499 deletions.
3 changes: 2 additions & 1 deletion .golangci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,8 @@ linters-settings:
#
# Easier to read with only one of the versioned payloads.
#
- 'api.VersionedExecutionPayload'
- 'VersionedExecutionPayload'
- 'VersionedSignedBuilderBid'

#
# Structures outside our control that have a ton of settings. It doesn't
Expand Down
3 changes: 1 addition & 2 deletions beaconclient/beacon_client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"testing"
"time"

"github.com/flashbots/go-boost-utils/types"
"github.com/flashbots/mev-boost-relay/common"
"github.com/gorilla/mux"
"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -194,7 +193,7 @@ func TestFetchValidators(t *testing.T) {

// only beacon 2 should have a validator, and should be used by default
backend.beaconInstances[0].MockFetchValidatorsErr = nil
backend.beaconInstances[1].SetValidators(make(map[types.PubkeyHex]ValidatorResponseEntry))
backend.beaconInstances[1].SetValidators(make(map[common.PubkeyHex]ValidatorResponseEntry))
backend.beaconInstances[2].MockFetchValidatorsErr = nil
backend.beaconInstances[2].AddValidator(entry)

Expand Down
12 changes: 6 additions & 6 deletions beaconclient/mock_beacon_instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ import (
"time"

"github.com/attestantio/go-eth2-client/spec"
"github.com/flashbots/go-boost-utils/types"
"github.com/flashbots/mev-boost-relay/common"
)

type MockBeaconInstance struct {
mu sync.RWMutex
validatorSet map[types.PubkeyHex]ValidatorResponseEntry
validatorSet map[common.PubkeyHex]ValidatorResponseEntry

MockSyncStatus *SyncStatusPayloadData
MockSyncStatusErr error
Expand All @@ -23,7 +23,7 @@ type MockBeaconInstance struct {

func NewMockBeaconInstance() *MockBeaconInstance {
return &MockBeaconInstance{
validatorSet: make(map[types.PubkeyHex]ValidatorResponseEntry),
validatorSet: make(map[common.PubkeyHex]ValidatorResponseEntry),

MockSyncStatus: &SyncStatusPayloadData{
HeadSlot: 1,
Expand All @@ -44,17 +44,17 @@ func NewMockBeaconInstance() *MockBeaconInstance {

func (c *MockBeaconInstance) AddValidator(entry ValidatorResponseEntry) {
c.mu.Lock()
c.validatorSet[types.NewPubkeyHex(entry.Validator.Pubkey)] = entry
c.validatorSet[common.NewPubkeyHex(entry.Validator.Pubkey)] = entry
c.mu.Unlock()
}

func (c *MockBeaconInstance) SetValidators(validatorSet map[types.PubkeyHex]ValidatorResponseEntry) {
func (c *MockBeaconInstance) SetValidators(validatorSet map[common.PubkeyHex]ValidatorResponseEntry) {
c.mu.Lock()
c.validatorSet = validatorSet
c.mu.Unlock()
}

func (c *MockBeaconInstance) IsValidator(pubkey types.PubkeyHex) bool {
func (c *MockBeaconInstance) IsValidator(pubkey common.PubkeyHex) bool {
c.mu.RLock()
_, found := c.validatorSet[pubkey]
c.mu.RUnlock()
Expand Down
4 changes: 0 additions & 4 deletions beaconclient/mock_multi_beacon_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,6 @@ func (*MockMultiBeaconClient) SubscribeToHeadEvents(slotC chan HeadEventData) {}
func (*MockMultiBeaconClient) SubscribeToPayloadAttributesEvents(payloadAttrC chan PayloadAttributesEvent) {
}

// func (*MockMultiBeaconClient) FetchValidators(headSlot uint64) (map[types.PubkeyHex]ValidatorResponseEntry, error) {
// return nil, nil
// }

func (*MockMultiBeaconClient) GetStateValidators(stateID string) (*GetStateValidatorsResponse, error) {
return nil, nil
}
Expand Down
2 changes: 1 addition & 1 deletion beaconclient/multi_beacon_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,7 @@ func (c *MultiBeaconClient) PublishBlock(block *spec.VersionedSignedBeaconBlock)
c.log.WithError(err).Warn("failed to publish block as block slot is missing")
return 0, err
}
blockHash, err := block.BlockHash()
blockHash, err := block.ExecutionBlockHash()
if err != nil {
c.log.WithError(err).Warn("failed to publish block as block hash is missing")
return 0, err
Expand Down
3 changes: 1 addition & 2 deletions beaconclient/prod_beacon_instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (

"github.com/attestantio/go-eth2-client/spec"
"github.com/attestantio/go-eth2-client/spec/capella"
"github.com/flashbots/go-boost-utils/types"
"github.com/flashbots/mev-boost-relay/common"
"github.com/r3labs/sse/v2"
"github.com/sirupsen/logrus"
Expand Down Expand Up @@ -243,7 +242,7 @@ type GetBlockResponse struct {
Message struct {
Slot uint64 `json:"slot,string"`
Body struct {
ExecutionPayload types.ExecutionPayload `json:"execution_payload"`
ExecutionPayload capella.ExecutionPayload `json:"execution_payload"`
}
}
}
Expand Down
42 changes: 25 additions & 17 deletions common/test_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,18 @@ import (
"io"
"os"
"testing"
"time"

"github.com/attestantio/go-builder-client/api/capella"
apiv1 "github.com/attestantio/go-builder-client/api/v1"
"github.com/attestantio/go-builder-client/spec"
consensusspec "github.com/attestantio/go-eth2-client/spec"
"github.com/attestantio/go-eth2-client/spec/bellatrix"
consensuscapella "github.com/attestantio/go-eth2-client/spec/capella"
"github.com/attestantio/go-eth2-client/spec/phase0"
"github.com/flashbots/go-boost-utils/bls"
boostTypes "github.com/flashbots/go-boost-utils/types"
"github.com/flashbots/go-boost-utils/ssz"
"github.com/flashbots/go-boost-utils/utils"
"github.com/sirupsen/logrus"
"github.com/stretchr/testify/require"
)
Expand All @@ -31,33 +35,37 @@ func check(err error, args ...interface{}) {
}

// _HexToAddress converts a hexadecimal string to an Ethereum address
func _HexToAddress(s string) (ret boostTypes.Address) {
check(ret.UnmarshalText([]byte(s)), " _HexToAddress: ", s)
func _HexToAddress(s string) (ret bellatrix.ExecutionAddress) {
ret, err := utils.HexToAddress(s)
check(err, " _HexToAddress: ", s)
return ret
}

// _HexToPubkey converts a hexadecimal string to a BLS Public Key
func _HexToPubkey(s string) (ret boostTypes.PublicKey) {
check(ret.UnmarshalText([]byte(s)), " _HexToPubkey: ", s)
return
func _HexToPubkey(s string) (ret phase0.BLSPubKey) {
ret, err := utils.HexToPubkey(s)
check(err, " _HexToPubkey: ", s)
return ret
}

// _HexToSignature converts a hexadecimal string to a BLS Signature
func _HexToSignature(s string) (ret boostTypes.Signature) {
check(ret.UnmarshalText([]byte(s)), " _HexToSignature: ", s)
return
func _HexToSignature(s string) (ret phase0.BLSSignature) {
ret, err := utils.HexToSignature(s)
check(err, " _HexToSignature: ", s)
return ret
}

// _HexToHash converts a hexadecimal string to a Hash
func _HexToHash(s string) (ret boostTypes.Hash) {
check(ret.FromSlice([]byte(s)), " _HexToHash: ", s)
return
func _HexToHash(s string) (ret phase0.Hash32) {
ret, err := utils.HexToHash(s)
check(err, " _HexToHash: ", s)
return ret
}

var ValidPayloadRegisterValidator = boostTypes.SignedValidatorRegistration{
Message: &boostTypes.RegisterValidatorRequestMessage{
var ValidPayloadRegisterValidator = apiv1.SignedValidatorRegistration{
Message: &apiv1.ValidatorRegistration{
FeeRecipient: _HexToAddress("0xdb65fEd33dc262Fe09D9a2Ba8F80b329BA25f941"),
Timestamp: 1606824043,
Timestamp: time.Unix(1606824043, 0),
GasLimit: 30000000,
Pubkey: _HexToPubkey(
"0x84e975405f8691ad7118527ee9ee4ed2e4e8bae973f6e29aa9ca9ee4aea83605ae3536d22acc9aa1af0545064eacf82e"),
Expand All @@ -67,7 +75,7 @@ var ValidPayloadRegisterValidator = boostTypes.SignedValidatorRegistration{
}

func TestBuilderSubmitBlockRequest(sk *bls.SecretKey, bid *BidTraceV2) spec.VersionedSubmitBlockRequest {
signature, err := boostTypes.SignMessage(bid, boostTypes.DomainBuilder, sk)
signature, err := ssz.SignMessage(bid, ssz.DomainBuilder, sk)
check(err, " SignMessage: ", bid, sk)
return spec.VersionedSubmitBlockRequest{ //nolint:exhaustruct
Version: consensusspec.DataVersionCapella,
Expand All @@ -77,7 +85,7 @@ func TestBuilderSubmitBlockRequest(sk *bls.SecretKey, bid *BidTraceV2) spec.Vers
ExecutionPayload: &consensuscapella.ExecutionPayload{ //nolint:exhaustruct
Transactions: []bellatrix.Transaction{[]byte{0x03}},
Timestamp: bid.Slot * 12, // 12 seconds per slot.
PrevRandao: _HexToHash("01234567890123456789012345678901"),
PrevRandao: _HexToHash("0xcf8e0d4e9587369b2301d0790347320302cc0943d5a1884560367e8208d920f2"),
Withdrawals: []*consensuscapella.Withdrawal{},
},
},
Expand Down
41 changes: 26 additions & 15 deletions common/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"errors"
"fmt"
"os"
"strings"

apiv1 "github.com/attestantio/go-builder-client/api/v1"
consensusbellatrix "github.com/attestantio/go-eth2-client/spec/bellatrix"
Expand Down Expand Up @@ -64,10 +65,10 @@ type EthNetworkDetails struct {
CapellaForkVersionHex string
DenebForkVersionHex string

DomainBuilder boostTypes.Domain
DomainBeaconProposerBellatrix boostTypes.Domain
DomainBeaconProposerCapella boostTypes.Domain
DomainBeaconProposerDeneb boostTypes.Domain
DomainBuilder phase0.Domain
DomainBeaconProposerBellatrix phase0.Domain
DomainBeaconProposerCapella phase0.Domain
DomainBeaconProposerDeneb phase0.Domain
}

func NewEthNetworkDetails(networkName string) (ret *EthNetworkDetails, err error) {
Expand All @@ -76,10 +77,10 @@ func NewEthNetworkDetails(networkName string) (ret *EthNetworkDetails, err error
var bellatrixForkVersion string
var capellaForkVersion string
var denebForkVersion string
var domainBuilder boostTypes.Domain
var domainBeaconProposerBellatrix boostTypes.Domain
var domainBeaconProposerCapella boostTypes.Domain
var domainBeaconProposerDeneb boostTypes.Domain
var domainBuilder phase0.Domain
var domainBeaconProposerBellatrix phase0.Domain
var domainBeaconProposerCapella phase0.Domain
var domainBeaconProposerDeneb phase0.Domain

switch networkName {
case EthNetworkHolesky:
Expand Down Expand Up @@ -116,22 +117,22 @@ func NewEthNetworkDetails(networkName string) (ret *EthNetworkDetails, err error
return nil, fmt.Errorf("%w: %s", ErrUnknownNetwork, networkName)
}

domainBuilder, err = ComputeDomain(boostTypes.DomainTypeAppBuilder, genesisForkVersion, boostTypes.Root{}.String())
domainBuilder, err = ComputeDomain(ssz.DomainTypeAppBuilder, genesisForkVersion, phase0.Root{}.String())
if err != nil {
return nil, err
}

domainBeaconProposerBellatrix, err = ComputeDomain(boostTypes.DomainTypeBeaconProposer, bellatrixForkVersion, genesisValidatorsRoot)
domainBeaconProposerBellatrix, err = ComputeDomain(ssz.DomainTypeBeaconProposer, bellatrixForkVersion, genesisValidatorsRoot)
if err != nil {
return nil, err
}

domainBeaconProposerCapella, err = ComputeDomain(boostTypes.DomainTypeBeaconProposer, capellaForkVersion, genesisValidatorsRoot)
domainBeaconProposerCapella, err = ComputeDomain(ssz.DomainTypeBeaconProposer, capellaForkVersion, genesisValidatorsRoot)
if err != nil {
return nil, err
}

domainBeaconProposerDeneb, err = ComputeDomain(boostTypes.DomainTypeBeaconProposer, denebForkVersion, genesisValidatorsRoot)
domainBeaconProposerDeneb, err = ComputeDomain(ssz.DomainTypeBeaconProposer, denebForkVersion, genesisValidatorsRoot)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -176,10 +177,20 @@ func (e *EthNetworkDetails) String() string {
e.DomainBeaconProposerDeneb)
}

type PubkeyHex string

func NewPubkeyHex(pk string) PubkeyHex {
return PubkeyHex(strings.ToLower(pk))
}

func (p PubkeyHex) String() string {
return string(p)
}

type BuilderGetValidatorsResponseEntry struct {
Slot uint64 `json:"slot,string"`
ValidatorIndex uint64 `json:"validator_index,string"`
Entry *boostTypes.SignedValidatorRegistration `json:"entry"`
Slot uint64 `json:"slot,string"`
ValidatorIndex uint64 `json:"validator_index,string"`
Entry *apiv1.SignedValidatorRegistration `json:"entry"`
}

type BidTraceV2 struct {
Expand Down
57 changes: 37 additions & 20 deletions common/types_spec.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ import (
utilbellatrix "github.com/attestantio/go-eth2-client/util/bellatrix"
utilcapella "github.com/attestantio/go-eth2-client/util/capella"
"github.com/flashbots/go-boost-utils/bls"
"github.com/flashbots/go-boost-utils/ssz"
boostTypes "github.com/flashbots/go-boost-utils/types"
"github.com/flashbots/go-boost-utils/utils"
"github.com/pkg/errors"
)

Expand All @@ -34,7 +36,7 @@ var NilResponse = struct{}{}

var ZeroU256 = boostTypes.IntToU256(0)

func BuildGetHeaderResponse(payload *spec.VersionedSubmitBlockRequest, sk *bls.SecretKey, pubkey *boostTypes.PublicKey, domain boostTypes.Domain) (*spec.VersionedSignedBuilderBid, error) {
func BuildGetHeaderResponse(payload *spec.VersionedSubmitBlockRequest, sk *bls.SecretKey, pubkey *phase0.BLSPubKey, domain phase0.Domain) (*spec.VersionedSignedBuilderBid, error) {
if payload == nil {
return nil, ErrMissingRequest
}
Expand All @@ -44,7 +46,7 @@ func BuildGetHeaderResponse(payload *spec.VersionedSubmitBlockRequest, sk *bls.S
}

if payload.Capella != nil {
signedBuilderBid, err := CapellaBuilderSubmitBlockRequestToSignedBuilderBid(payload.Capella, sk, (*phase0.BLSPubKey)(pubkey), domain)
signedBuilderBid, err := CapellaBuilderSubmitBlockRequestToSignedBuilderBid(payload.Capella, sk, pubkey, domain)
if err != nil {
return nil, err
}
Expand All @@ -68,30 +70,45 @@ func BuildGetPayloadResponse(payload *spec.VersionedSubmitBlockRequest) (*api.Ve
return nil, ErrEmptyPayload
}

func BuilderSubmitBlockRequestToSignedBuilderBid(req *boostTypes.BuilderSubmitBlockRequest, sk *bls.SecretKey, pubkey *boostTypes.PublicKey, domain boostTypes.Domain) (*boostTypes.SignedBuilderBid, error) {
header, err := boostTypes.PayloadToPayloadHeader(req.ExecutionPayload)
func BuilderSubmitBlockRequestToSignedBuilderBid(req *spec.VersionedSubmitBlockRequest, sk *bls.SecretKey, pubkey *phase0.BLSPubKey, domain phase0.Domain) (*spec.VersionedSignedBuilderBid, error) {
value, err := req.Value()
if err != nil {
return nil, err
}

builderBid := boostTypes.BuilderBid{
Value: req.Message.Value,
Header: header,
Pubkey: *pubkey,
}
switch req.Version {
case consensusspec.DataVersionCapella:
header, err := utils.PayloadToPayloadHeader(&api.VersionedExecutionPayload{Version: req.Version, Capella: req.Capella.ExecutionPayload})
if err != nil {
return nil, err
}

sig, err := boostTypes.SignMessage(&builderBid, domain, sk)
if err != nil {
return nil, err
}
builderBid := capella.BuilderBid{
Value: value,
Header: header.Capella,
Pubkey: *pubkey,
}

return &boostTypes.SignedBuilderBid{
Message: &builderBid,
Signature: sig,
}, nil
sig, err := ssz.SignMessage(&builderBid, domain, sk)
if err != nil {
return nil, err
}

return &spec.VersionedSignedBuilderBid{
Version: consensusspec.DataVersionCapella,
Capella: &capella.SignedBuilderBid{
Message: &builderBid,
Signature: sig,
},
}, nil
case consensusspec.DataVersionUnknown, consensusspec.DataVersionPhase0, consensusspec.DataVersionAltair, consensusspec.DataVersionBellatrix, consensusspec.DataVersionDeneb:
return nil, errors.Wrap(ErrInvalidVersion, fmt.Sprintf("%s is not supported", req.Version.String()))
default:
return nil, errors.Wrap(ErrInvalidVersion, fmt.Sprintf("%s is not supported", req.Version.String()))
}
}

func CapellaBuilderSubmitBlockRequestToSignedBuilderBid(req *capella.SubmitBlockRequest, sk *bls.SecretKey, pubkey *phase0.BLSPubKey, domain boostTypes.Domain) (*capella.SignedBuilderBid, error) {
func CapellaBuilderSubmitBlockRequestToSignedBuilderBid(req *capella.SubmitBlockRequest, sk *bls.SecretKey, pubkey *phase0.BLSPubKey, domain phase0.Domain) (*capella.SignedBuilderBid, error) {
header, err := CapellaPayloadToPayloadHeader(req.ExecutionPayload)
if err != nil {
return nil, err
Expand All @@ -103,14 +120,14 @@ func CapellaBuilderSubmitBlockRequestToSignedBuilderBid(req *capella.SubmitBlock
Pubkey: *pubkey,
}

sig, err := boostTypes.SignMessage(&builderBid, domain, sk)
sig, err := ssz.SignMessage(&builderBid, domain, sk)
if err != nil {
return nil, err
}

return &capella.SignedBuilderBid{
Message: &builderBid,
Signature: phase0.BLSSignature(sig),
Signature: sig,
}, nil
}

Expand Down
Loading

0 comments on commit d4bb90a

Please sign in to comment.