Skip to content

Commit

Permalink
Upgrade go-boost-utils (#523)
Browse files Browse the repository at this point in the history
  • Loading branch information
avalonche authored Jun 28, 2023
1 parent 99b9695 commit 3c26cd7
Show file tree
Hide file tree
Showing 12 changed files with 153 additions and 306 deletions.
65 changes: 35 additions & 30 deletions cmd/test-cli/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,13 @@ import (
"os"
"strconv"

"github.com/attestantio/go-builder-client/api"
"github.com/attestantio/go-builder-client/spec"
"github.com/attestantio/go-eth2-client/api/v1/capella"
"github.com/attestantio/go-eth2-client/spec/altair"
"github.com/attestantio/go-eth2-client/spec/phase0"
"github.com/ethereum/go-ethereum/common"
boostTypes "github.com/flashbots/go-boost-utils/types"
"github.com/flashbots/go-boost-utils/ssz"
"github.com/flashbots/mev-boost/server"
"github.com/sirupsen/logrus"
)
Expand All @@ -25,7 +30,7 @@ func doGenerateValidator(filePath string, gasLimit uint64, feeRecipient string)
log.WithField("file", filePath).Info("Saved validator data")
}

func doRegisterValidator(v validatorPrivateData, boostEndpoint string, builderSigningDomain boostTypes.Domain) {
func doRegisterValidator(v validatorPrivateData, boostEndpoint string, builderSigningDomain phase0.Domain) {
message, err := v.PrepareRegistrationMessage(builderSigningDomain)
if err != nil {
log.WithError(err).Fatal("Could not prepare registration message")
Expand All @@ -39,7 +44,7 @@ func doRegisterValidator(v validatorPrivateData, boostEndpoint string, builderSi
log.WithError(err).Info("Registered validator")
}

func doGetHeader(v validatorPrivateData, boostEndpoint string, beaconNode Beacon, engineEndpoint string, builderSigningDomain boostTypes.Domain) boostTypes.GetHeaderResponse {
func doGetHeader(v validatorPrivateData, boostEndpoint string, beaconNode Beacon, engineEndpoint string, builderSigningDomain phase0.Domain) spec.VersionedSignedBuilderBid {
// Mergemock needs to call forkchoice update before getHeader, for non-mergemock beacon node this is a no-op
err := beaconNode.onGetHeader()
if err != nil {
Expand All @@ -66,17 +71,17 @@ func doGetHeader(v validatorPrivateData, boostEndpoint string, beaconNode Beacon

uri := fmt.Sprintf("%s/eth/v1/builder/header/%d/%s/%s", boostEndpoint, currentBlock.Slot+1, currentBlockHash, v.Pk.String())

var getHeaderResp boostTypes.GetHeaderResponse
var getHeaderResp spec.VersionedSignedBuilderBid
if _, err := server.SendHTTPRequest(context.TODO(), *http.DefaultClient, http.MethodGet, uri, "test-cli", nil, nil, &getHeaderResp); err != nil {
log.WithError(err).WithField("currentBlockHash", currentBlockHash).Fatal("Could not get header")
}

if getHeaderResp.Data.Message == nil {
if getHeaderResp.Capella.Message == nil {
log.Fatal("Did not receive correct header")
}
log.WithField("header", *getHeaderResp.Data.Message).Info("Got header from boost")
log.WithField("header", *getHeaderResp.Capella.Message).Info("Got header from boost")

ok, err := boostTypes.VerifySignature(getHeaderResp.Data.Message, builderSigningDomain, getHeaderResp.Data.Message.Pubkey[:], getHeaderResp.Data.Signature[:])
ok, err := ssz.VerifySignature(getHeaderResp.Capella.Message, builderSigningDomain, getHeaderResp.Capella.Message.Pubkey[:], getHeaderResp.Capella.Signature[:])
if err != nil {
log.WithError(err).Fatal("Could not verify builder bid signature")
}
Expand All @@ -87,25 +92,25 @@ func doGetHeader(v validatorPrivateData, boostEndpoint string, beaconNode Beacon
return getHeaderResp
}

func doGetPayload(v validatorPrivateData, boostEndpoint string, beaconNode Beacon, engineEndpoint string, builderSigningDomain, proposerSigningDomain boostTypes.Domain) {
func doGetPayload(v validatorPrivateData, boostEndpoint string, beaconNode Beacon, engineEndpoint string, builderSigningDomain, proposerSigningDomain phase0.Domain) {
header := doGetHeader(v, boostEndpoint, beaconNode, engineEndpoint, builderSigningDomain)

blindedBeaconBlock := boostTypes.BlindedBeaconBlock{
blindedBeaconBlock := capella.BlindedBeaconBlock{
Slot: 0,
ProposerIndex: 0,
ParentRoot: boostTypes.Root{},
StateRoot: boostTypes.Root{},
Body: &boostTypes.BlindedBeaconBlockBody{
RandaoReveal: boostTypes.Signature{},
Eth1Data: &boostTypes.Eth1Data{},
Graffiti: boostTypes.Hash{},
ProposerSlashings: []*boostTypes.ProposerSlashing{},
AttesterSlashings: []*boostTypes.AttesterSlashing{},
Attestations: []*boostTypes.Attestation{},
Deposits: []*boostTypes.Deposit{},
VoluntaryExits: []*boostTypes.SignedVoluntaryExit{},
SyncAggregate: &boostTypes.SyncAggregate{},
ExecutionPayloadHeader: header.Data.Message.Header,
ParentRoot: phase0.Root{},
StateRoot: phase0.Root{},
Body: &capella.BlindedBeaconBlockBody{
RANDAOReveal: phase0.BLSSignature{},
ETH1Data: &phase0.ETH1Data{},
Graffiti: phase0.Hash32{},
ProposerSlashings: []*phase0.ProposerSlashing{},
AttesterSlashings: []*phase0.AttesterSlashing{},
Attestations: []*phase0.Attestation{},
Deposits: []*phase0.Deposit{},
VoluntaryExits: []*phase0.SignedVoluntaryExit{},
SyncAggregate: &altair.SyncAggregate{},
ExecutionPayloadHeader: header.Capella.Message.Header,
},
}

Expand All @@ -114,19 +119,19 @@ func doGetPayload(v validatorPrivateData, boostEndpoint string, beaconNode Beaco
log.WithError(err).Fatal("could not sign blinded beacon block")
}

payload := boostTypes.SignedBlindedBeaconBlock{
payload := capella.SignedBlindedBeaconBlock{
Message: &blindedBeaconBlock,
Signature: signature,
}
var respPayload boostTypes.GetPayloadResponse
var respPayload api.VersionedExecutionPayload
if _, err := server.SendHTTPRequest(context.TODO(), *http.DefaultClient, http.MethodPost, boostEndpoint+"/eth/v1/builder/blinded_blocks", "test-cli", nil, payload, &respPayload); err != nil {
log.WithError(err).Fatal("could not get payload")
}

if respPayload.Data == nil {
if respPayload.IsEmpty() {
log.Fatal("Did not receive correct payload")
}
log.WithField("payload", *respPayload.Data).Info("got payload from mev-boost")
log.WithField("payload", respPayload).Info("got payload from mev-boost")
}

func main() {
Expand Down Expand Up @@ -210,7 +215,7 @@ func main() {
if err := registerCommand.Parse(os.Args[2:]); err != nil {
log.Fatal(err)
}
builderSigningDomain, err := server.ComputeDomain(boostTypes.DomainTypeAppBuilder, genesisForkVersionStr, boostTypes.Root{}.String())
builderSigningDomain, err := server.ComputeDomain(ssz.DomainTypeAppBuilder, genesisForkVersionStr, phase0.Root{}.String())
if err != nil {
log.WithError(err).Fatal("computing signing domain failed")
}
Expand All @@ -219,7 +224,7 @@ func main() {
if err := getHeaderCommand.Parse(os.Args[2:]); err != nil {
log.Fatal(err)
}
builderSigningDomain, err := server.ComputeDomain(boostTypes.DomainTypeAppBuilder, genesisForkVersionStr, boostTypes.Root{}.String())
builderSigningDomain, err := server.ComputeDomain(ssz.DomainTypeAppBuilder, genesisForkVersionStr, phase0.Root{}.String())
if err != nil {
log.WithError(err).Fatal("computing signing domain failed")
}
Expand All @@ -228,11 +233,11 @@ func main() {
if err := getPayloadCommand.Parse(os.Args[2:]); err != nil {
log.Fatal(err)
}
builderSigningDomain, err := server.ComputeDomain(boostTypes.DomainTypeAppBuilder, genesisForkVersionStr, boostTypes.Root{}.String())
builderSigningDomain, err := server.ComputeDomain(ssz.DomainTypeAppBuilder, genesisForkVersionStr, phase0.Root{}.String())
if err != nil {
log.WithError(err).Fatal("computing signing domain failed")
}
proposerSigningDomain, err := server.ComputeDomain(boostTypes.DomainTypeBeaconProposer, bellatrixForkVersionStr, genesisValidatorsRootStr)
proposerSigningDomain, err := server.ComputeDomain(ssz.DomainTypeBeaconProposer, bellatrixForkVersionStr, genesisValidatorsRootStr)
if err != nil {
log.WithError(err).Fatal("computing signing domain failed")
}
Expand Down
36 changes: 21 additions & 15 deletions cmd/test-cli/validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,20 @@ package main

import (
"encoding/json"
"errors"
"os"
"time"

apiv1 "github.com/attestantio/go-builder-client/api/v1"
"github.com/attestantio/go-eth2-client/spec/phase0"
"github.com/ethereum/go-ethereum/common/hexutil"
"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"
)

var errInvalidLength = errors.New("invalid length")

type validatorPrivateData struct {
Sk hexutil.Bytes
Pk hexutil.Bytes
Expand Down Expand Up @@ -46,38 +52,38 @@ func newRandomValidator(gasLimit uint64, feeRecipient string) validatorPrivateDa
return validatorPrivateData{bls.SecretKeyToBytes(sk), bls.PublicKeyToBytes(pk), hexutil.Uint64(gasLimit), feeRecipient}
}

func (v *validatorPrivateData) PrepareRegistrationMessage(builderSigningDomain boostTypes.Domain) ([]boostTypes.SignedValidatorRegistration, error) {
pk := boostTypes.PublicKey{}
err := pk.FromSlice(v.Pk)
if err != nil {
return []boostTypes.SignedValidatorRegistration{}, err
func (v *validatorPrivateData) PrepareRegistrationMessage(builderSigningDomain phase0.Domain) ([]apiv1.SignedValidatorRegistration, error) {
pk := phase0.BLSPubKey{}
if len(v.Pk) != len(pk) {
return []apiv1.SignedValidatorRegistration{}, errInvalidLength
}
copy(pk[:], v.Pk)

addr, err := boostTypes.HexToAddress(v.FeeRecipientHex)
addr, err := utils.HexToAddress(v.FeeRecipientHex)
if err != nil {
return []boostTypes.SignedValidatorRegistration{}, err
return []apiv1.SignedValidatorRegistration{}, err
}
msg := boostTypes.RegisterValidatorRequestMessage{
msg := apiv1.ValidatorRegistration{
FeeRecipient: addr,
Timestamp: uint64(time.Now().Unix()),
Timestamp: time.Now(),
Pubkey: pk,
GasLimit: uint64(v.GasLimit),
}
signature, err := v.Sign(&msg, builderSigningDomain)
if err != nil {
return []boostTypes.SignedValidatorRegistration{}, err
return []apiv1.SignedValidatorRegistration{}, err
}

return []boostTypes.SignedValidatorRegistration{{
return []apiv1.SignedValidatorRegistration{{
Message: &msg,
Signature: signature,
}}, nil
}

func (v *validatorPrivateData) Sign(msg boostTypes.HashTreeRoot, domain boostTypes.Domain) (boostTypes.Signature, error) {
func (v *validatorPrivateData) Sign(msg ssz.ObjWithHashTreeRoot, domain phase0.Domain) (phase0.BLSSignature, error) {
sk, err := bls.SecretKeyFromBytes(v.Sk)
if err != nil {
return boostTypes.Signature{}, err
return phase0.BLSSignature{}, err
}
return boostTypes.SignMessage(msg, domain, sk)
return ssz.SignMessage(msg, domain, sk)
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ go 1.20

require (
github.com/ethereum/go-ethereum v1.12.0
github.com/flashbots/go-boost-utils v1.6.0
github.com/flashbots/go-boost-utils v1.7.1-0.20230625230411-8c44018f4777
github.com/flashbots/go-utils v0.4.8
github.com/google/uuid v1.3.0
github.com/gorilla/mux v1.8.0
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,8 @@ github.com/fatih/color v1.13.0/go.mod h1:kLAiJbzzSOZDVNGyDpeOxJ47H46qBXwg5ILebYF
github.com/fatih/structs v1.1.0/go.mod h1:9NiDSp5zOcgEDl+j00MP/WkGVPOlPRLejGD8Ga6PJ7M=
github.com/ferranbt/fastssz v0.1.3 h1:ZI+z3JH05h4kgmFXdHuR1aWYsgrg7o+Fw7/NCzM16Mo=
github.com/ferranbt/fastssz v0.1.3/go.mod h1:0Y9TEd/9XuFlh7mskMPfXiI2Dkw4Ddg9EyXt1W7MRvE=
github.com/flashbots/go-boost-utils v1.6.0 h1:XIcPDG6q0Tesh3kcCXyv61ncD/WVYOMCO2OVzPznvMA=
github.com/flashbots/go-boost-utils v1.6.0/go.mod h1:fL9Jc738zFENkbn5HNnVzIfuRcvCO1djlT/ylyT34Zw=
github.com/flashbots/go-boost-utils v1.7.1-0.20230625230411-8c44018f4777 h1:mnwpdE/JW74My5u3Ag1IMX8PgCa7aZwgFNPB20HN/DM=
github.com/flashbots/go-boost-utils v1.7.1-0.20230625230411-8c44018f4777/go.mod h1:Rckma9iiHGuw4PQj/BFV1ddN13lUib4sTfF3QUY40Z8=
github.com/flashbots/go-utils v0.4.8 h1:WDJXryrqShGq4HFe+p1kGjObXSqzT7Sy/+9YvFpr5tM=
github.com/flashbots/go-utils v0.4.8/go.mod h1:dBmSv4Cpqij4xKP50bdisAvFIo4/EgsY97BMpVjPzr0=
github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo=
Expand Down
42 changes: 21 additions & 21 deletions server/mock_relay.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,15 @@ import (
"github.com/attestantio/go-builder-client/api"
apibellatrix "github.com/attestantio/go-builder-client/api/bellatrix"
apicapella "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"
"github.com/attestantio/go-eth2-client/spec/capella"
"github.com/attestantio/go-eth2-client/spec/phase0"
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/flashbots/go-boost-utils/bls"
"github.com/flashbots/go-boost-utils/types"
"github.com/flashbots/go-boost-utils/ssz"
"github.com/gorilla/mux"
"github.com/holiman/uint256"
"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -58,9 +59,8 @@ type mockRelay struct {
handlerOverrideGetPayload func(w http.ResponseWriter, req *http.Request)

// Default responses placeholders, used if overrider does not exist
GetHeaderResponse *spec.VersionedSignedBuilderBid
GetBellatrixPayloadResponse *types.GetPayloadResponse
GetCapellaPayloadResponse *api.VersionedExecutionPayload
GetHeaderResponse *spec.VersionedSignedBuilderBid
GetPayloadResponse *api.VersionedExecutionPayload

// Server section
Server *httptest.Server
Expand Down Expand Up @@ -141,7 +141,7 @@ func (m *mockRelay) handleStatus(w http.ResponseWriter, req *http.Request) {
fmt.Fprintf(w, `{}`)
}

// By default, handleRegisterValidator returns a default types.SignedValidatorRegistration
// By default, handleRegisterValidator returns a default apiv1.SignedValidatorRegistration
func (m *mockRelay) handleRegisterValidator(w http.ResponseWriter, req *http.Request) {
m.mu.Lock()
defer m.mu.Unlock()
Expand All @@ -154,7 +154,7 @@ func (m *mockRelay) handleRegisterValidator(w http.ResponseWriter, req *http.Req

// defaultHandleRegisterValidator returns the default handler for handleRegisterValidator
func (m *mockRelay) defaultHandleRegisterValidator(w http.ResponseWriter, req *http.Request) {
payload := []types.SignedValidatorRegistration{}
payload := []apiv1.SignedValidatorRegistration{}
if err := DecodeJSON(req.Body, &payload); err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
Expand All @@ -172,45 +172,45 @@ func (m *mockRelay) MakeGetHeaderResponse(value uint64, blockHash, parentHash, p
// Fill the payload with custom values.
message := &apibellatrix.BuilderBid{
Header: &bellatrix.ExecutionPayloadHeader{
BlockHash: phase0.Hash32(_HexToHash(blockHash)),
ParentHash: phase0.Hash32(_HexToHash(parentHash)),
BlockHash: _HexToHash(blockHash),
ParentHash: _HexToHash(parentHash),
},
Value: uint256.NewInt(value),
Pubkey: phase0.BLSPubKey(_HexToPubkey(publicKey)),
Pubkey: _HexToPubkey(publicKey),
}

// Sign the message.
signature, err := types.SignMessage(message, types.DomainBuilder, m.secretKey)
signature, err := ssz.SignMessage(message, ssz.DomainBuilder, m.secretKey)
require.NoError(m.t, err)

return &spec.VersionedSignedBuilderBid{
Version: consensusspec.DataVersionCapella,
Bellatrix: &apibellatrix.SignedBuilderBid{
Message: message,
Signature: phase0.BLSSignature(signature),
Signature: signature,
},
}
case consensusspec.DataVersionCapella:
// Fill the payload with custom values.
message := &apicapella.BuilderBid{
Header: &capella.ExecutionPayloadHeader{
BlockHash: phase0.Hash32(_HexToHash(blockHash)),
ParentHash: phase0.Hash32(_HexToHash(parentHash)),
BlockHash: _HexToHash(blockHash),
ParentHash: _HexToHash(parentHash),
WithdrawalsRoot: phase0.Root{},
},
Value: uint256.NewInt(value),
Pubkey: phase0.BLSPubKey(_HexToPubkey(publicKey)),
Pubkey: _HexToPubkey(publicKey),
}

// Sign the message.
signature, err := types.SignMessage(message, types.DomainBuilder, m.secretKey)
signature, err := ssz.SignMessage(message, ssz.DomainBuilder, m.secretKey)
require.NoError(m.t, err)

return &spec.VersionedSignedBuilderBid{
Version: consensusspec.DataVersionCapella,
Capella: &apicapella.SignedBuilderBid{
Message: message,
Signature: phase0.BLSSignature(signature),
Signature: signature,
},
}
case consensusspec.DataVersionAltair, consensusspec.DataVersionPhase0, consensusspec.DataVersionDeneb:
Expand Down Expand Up @@ -261,10 +261,10 @@ func (m *mockRelay) MakeGetPayloadResponse(parentHash, blockHash, feeRecipient s
return &api.VersionedExecutionPayload{
Version: consensusspec.DataVersionCapella,
Capella: &capella.ExecutionPayload{
ParentHash: phase0.Hash32(_HexToHash(parentHash)),
BlockHash: phase0.Hash32(_HexToHash(blockHash)),
ParentHash: _HexToHash(parentHash),
BlockHash: _HexToHash(blockHash),
BlockNumber: blockNumber,
FeeRecipient: bellatrix.ExecutionAddress(_HexToAddress(feeRecipient)),
FeeRecipient: _HexToAddress(feeRecipient),
Withdrawals: make([]*capella.Withdrawal, 0),
},
}
Expand Down Expand Up @@ -296,8 +296,8 @@ func (m *mockRelay) defaultHandleGetPayload(w http.ResponseWriter) {
12345,
)

if m.GetCapellaPayloadResponse != nil {
response = m.GetCapellaPayloadResponse
if m.GetPayloadResponse != nil {
response = m.GetPayloadResponse
}

if err := json.NewEncoder(w).Encode(response); err != nil {
Expand Down
Loading

0 comments on commit 3c26cd7

Please sign in to comment.