Skip to content

Commit

Permalink
update attestant library
Browse files Browse the repository at this point in the history
  • Loading branch information
avalonche committed Jan 5, 2023
1 parent 548ca8f commit 8ce5596
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 39 deletions.
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ require (
require (
github.com/StackExchange/wmi v0.0.0-20180116203802-5d049714c4a6 // indirect
github.com/VictoriaMetrics/fastcache v1.6.0 // indirect
github.com/attestantio/go-builder-client v0.1.12
github.com/attestantio/go-eth2-client v0.15.0
github.com/attestantio/go-builder-client v0.2.6-0.20230105014332-e601ac7db862
github.com/attestantio/go-eth2-client v0.15.1
github.com/btcsuite/btcd v0.22.0-beta // indirect
github.com/btcsuite/btcd/btcec/v2 v2.2.1 // indirect
github.com/cespare/xxhash/v2 v2.1.2 // indirect
Expand Down
8 changes: 6 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,14 @@ github.com/alecthomas/units v0.0.0-20190717042225-c3de453c63f4/go.mod h1:ybxpYRF
github.com/alecthomas/units v0.0.0-20190924025748-f65c72e2690d/go.mod h1:rBZYJk541a8SKzHPHnH3zbiI+7dagKZ0cgpgrD7Fyho=
github.com/allegro/bigcache v1.2.1-0.20190218064605-e24eb225f156 h1:eMwmnE/GDgah4HI848JfFxHt+iPb26b4zyfspmqY0/8=
github.com/allegro/bigcache v1.2.1-0.20190218064605-e24eb225f156/go.mod h1:Cb/ax3seSYIx7SuZdm2G2xzfwmv3TPSk2ucNfQESPXM=
github.com/attestantio/go-builder-client v0.1.12 h1:b1FT2e6dLx//7gMowqOnq/Y2N7klYX15VyoXYh3poIk=
github.com/attestantio/go-builder-client v0.1.12/go.mod h1:p3cRZBxRS7TBltjM/MR8I5whqyBHXRb5W7lK7u9AHHA=
github.com/attestantio/go-builder-client v0.2.1 h1:iIbTze3WyVMWmaMsvLoUcwwgWBui4aT627o74MVbsog=
github.com/attestantio/go-builder-client v0.2.1/go.mod h1:j23oeFcAtXEWT77k8UPSieobZ4N1VFr00IpJM8kWPiw=
github.com/attestantio/go-builder-client v0.2.6-0.20230105014332-e601ac7db862 h1:Vf+TqRpbjuAo5QJJ/KRm0LhHKq0xyiY15IerKVDK0OQ=
github.com/attestantio/go-builder-client v0.2.6-0.20230105014332-e601ac7db862/go.mod h1:GHh6Qmuyl9hE+rykag9yDclAIlNK/ZT6sz4GKGtvglI=
github.com/attestantio/go-eth2-client v0.15.0 h1:Ia8U1EPYFJ8KB/vsQ2+oEhzuPgCePlBkWXg1R3e0oWw=
github.com/attestantio/go-eth2-client v0.15.0/go.mod h1:5kLLzdlyPGboWr8tAwnG/4Kpi43BHd/HWp++WmmP6Ws=
github.com/attestantio/go-eth2-client v0.15.1 h1:LbtRcjc1eXbtsi9TeCYYFV7VtvLYIOrXQkXjFW0B1SE=
github.com/attestantio/go-eth2-client v0.15.1/go.mod h1:5kLLzdlyPGboWr8tAwnG/4Kpi43BHd/HWp++WmmP6Ws=
github.com/benbjohnson/clock v1.1.0 h1:Q92kusRqC1XV2MjkWETPvjJVqKetz1OzxZB7mHJLju8=
github.com/beorn7/perks v0.0.0-20180321164747-3a771d992973/go.mod h1:Dwedo/Wpr24TaqPxmxbtue+5NUziq4I4S80YR8gNf3Q=
github.com/beorn7/perks v1.0.0/go.mod h1:KWe93zE9D1o94FZ5RNwFwVgaQK1VOXiVxmqh+CedLV8=
Expand Down
23 changes: 12 additions & 11 deletions server/mock_relay.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@ type mockRelay struct {

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

// Server section
Server *httptest.Server
Expand Down Expand Up @@ -237,23 +237,24 @@ func (m *mockRelay) handleGetPayload(w http.ResponseWriter, req *http.Request) {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)

if m.GetCapellaPayloadResponse != nil {
if err := json.NewEncoder(w).Encode(m.GetCapellaPayloadResponse); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
return
}

// Build the default response.
response := m.MakeGetPayloadResponse(
"0xe28385e7bd68df656cd0042b74b69c3104b5356ed1f20eb69f1f925df47a3ab7",
"0x534809bd2b6832edff8d8ce4cb0e50068804fd1ef432c8362ad708a74fdc0e46",
"0xdb65fEd33dc262Fe09D9a2Ba8F80b329BA25f941",
12345,
)
if m.GetPayloadResponse != nil {
response = m.GetPayloadResponse
}

if m.GetAttestantPayloadResponse != nil {
if err := json.NewEncoder(w).Encode(m.GetAttestantPayloadResponse); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
return
if m.GetBellatrixPayloadResponse != nil {
response = m.GetBellatrixPayloadResponse
}

if err := json.NewEncoder(w).Encode(response); err != nil {
Expand Down
14 changes: 7 additions & 7 deletions server/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -625,27 +625,27 @@ func (m *BoostService) processCapellaPayload(w http.ResponseWriter, req *http.Re
return
}

if responsePayload.Bellatrix == nil || types.Hash(responsePayload.Bellatrix.BlockHash) == nilHash {
if responsePayload.Capella == nil || types.Hash(responsePayload.Capella.BlockHash) == nilHash {
log.Error("response with empty data!")
return
}

// Ensure the response blockhash matches the request
if payload.Message.Body.ExecutionPayloadHeader.BlockHash != responsePayload.Bellatrix.BlockHash {
if payload.Message.Body.ExecutionPayloadHeader.BlockHash != responsePayload.Capella.BlockHash {
log.WithFields(logrus.Fields{
"responseBlockHash": responsePayload.Bellatrix.BlockHash.String(),
"responseBlockHash": responsePayload.Capella.BlockHash.String(),
}).Error("requestBlockHash does not equal responseBlockHash")
return
}

// Ensure the response blockhash matches the response block
calculatedBlockHash, err := ComputeBlockHash(responsePayload.Bellatrix)
calculatedBlockHash, err := ComputeBlockHash(responsePayload.Capella)
if err != nil {
log.WithError(err).Error("could not calculate block hash")
} else if responsePayload.Bellatrix.BlockHash != calculatedBlockHash {
} else if responsePayload.Capella.BlockHash != calculatedBlockHash {
log.WithFields(logrus.Fields{
"calculatedBlockHash": calculatedBlockHash.String(),
"responseBlockHash": responsePayload.Bellatrix.BlockHash.String(),
"responseBlockHash": responsePayload.Capella.BlockHash.String(),
}).Error("responseBlockHash does not equal hash calculated from response block")
}

Expand All @@ -668,7 +668,7 @@ func (m *BoostService) processCapellaPayload(w http.ResponseWriter, req *http.Re
wg.Wait()

// If no payload has been received from relay, log loudly about withholding!
if result.Bellatrix == nil || types.Hash(result.Bellatrix.BlockHash) == nilHash {
if result.Capella == nil || types.Hash(result.Capella.BlockHash) == nilHash {
originRelays := RelayEntriesToStrings(originalBid.relays)
log.WithField("relays", strings.Join(originRelays, ", ")).Error("no payload received from relay!")
m.respondError(w, http.StatusBadGateway, errNoSuccessfulRelayResponse.Error())
Expand Down
30 changes: 16 additions & 14 deletions server/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,9 @@ import (

"github.com/attestantio/go-builder-client/api"
apiv1capella "github.com/attestantio/go-eth2-client/api/v1/capella"
"github.com/attestantio/go-eth2-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/flashbots/go-boost-utils/types"
"github.com/stretchr/testify/require"
)
Expand Down Expand Up @@ -78,7 +79,7 @@ func (be *testBackend) request(t *testing.T, method, path string, payload any) *
return rr
}

func blindedBlockToExecutionPayload(signedBlindedBeaconBlock *types.SignedBlindedBeaconBlock) *types.ExecutionPayload {
func blindedBlockToExecutionPayloadBellatrix(signedBlindedBeaconBlock *types.SignedBlindedBeaconBlock) *types.ExecutionPayload {
header := signedBlindedBeaconBlock.Message.Body.ExecutionPayloadHeader
return &types.ExecutionPayload{
ParentHash: header.ParentHash,
Expand All @@ -97,9 +98,9 @@ func blindedBlockToExecutionPayload(signedBlindedBeaconBlock *types.SignedBlinde
}
}

func blindedBlockToExecutionPayloadAttestant(signedBlindedBeaconBlock *apiv1capella.SignedBlindedBeaconBlock) *bellatrix.ExecutionPayload {
func blindedBlockToExecutionPayloadCapella(signedBlindedBeaconBlock *apiv1capella.SignedBlindedBeaconBlock) *capella.ExecutionPayload {
header := signedBlindedBeaconBlock.Message.Body.ExecutionPayloadHeader
return &bellatrix.ExecutionPayload{
return &capella.ExecutionPayload{
ParentHash: header.ParentHash,
FeeRecipient: header.FeeRecipient,
StateRoot: header.StateRoot,
Expand All @@ -114,6 +115,7 @@ func blindedBlockToExecutionPayloadAttestant(signedBlindedBeaconBlock *apiv1cape
BaseFeePerGas: header.BaseFeePerGas,
BlockHash: header.BlockHash,
Transactions: make([]bellatrix.Transaction, 0),
Withdrawals: make([]*capella.Withdrawal, 0),
}
}

Expand Down Expand Up @@ -588,15 +590,15 @@ func TestGetPayload(t *testing.T) {
resp := new(types.GetPayloadResponse)

// 1/2 failing responses are okay
backend.relays[0].GetPayloadResponse = resp
backend.relays[0].GetBellatrixPayloadResponse = resp
rr := backend.request(t, http.MethodPost, path, payload)
require.GreaterOrEqual(t, backend.relays[1].GetRequestCount(path)+backend.relays[0].GetRequestCount(path), 1)
require.Equal(t, http.StatusOK, rr.Code, rr.Body.String())

// 2/2 failing responses are okay
backend = newTestBackend(t, 2, time.Second)
backend.relays[0].GetPayloadResponse = resp
backend.relays[1].GetPayloadResponse = resp
backend.relays[0].GetBellatrixPayloadResponse = resp
backend.relays[1].GetBellatrixPayloadResponse = resp
rr = backend.request(t, http.MethodPost, path, payload)
require.Equal(t, 1, backend.relays[0].GetRequestCount(path))
require.Equal(t, 1, backend.relays[1].GetRequestCount(path))
Expand Down Expand Up @@ -672,7 +674,7 @@ func TestGetPayloadWithTestdata(t *testing.T) {
BlockHash: signedBlindedBeaconBlock.Message.Body.ExecutionPayloadHeader.BlockHash,
},
}
backend.relays[0].GetPayloadResponse = &mockResp
backend.relays[0].GetBellatrixPayloadResponse = &mockResp

rr := backend.request(t, http.MethodPost, path, signedBlindedBeaconBlock)
require.Equal(t, http.StatusOK, rr.Code, rr.Body.String())
Expand All @@ -697,9 +699,9 @@ func TestGetPayloadCapella(t *testing.T) {
backend := newTestBackend(t, 1, time.Second)

// Prepare getPayload response
backend.relays[0].GetAttestantPayloadResponse = &api.VersionedExecutionPayload{
Version: spec.DataVersionBellatrix,
Bellatrix: blindedBlockToExecutionPayloadAttestant(signedBlindedBeaconBlock),
backend.relays[0].GetCapellaPayloadResponse = &api.VersionedExecutionPayload{
Version: consensusspec.DataVersionCapella,
Capella: blindedBlockToExecutionPayloadCapella(signedBlindedBeaconBlock),
}

// call getPayload, ensure it's only called on relay 0 (origin of the bid)
Expand All @@ -711,7 +713,7 @@ func TestGetPayloadCapella(t *testing.T) {
resp := new(api.VersionedExecutionPayload)
err = json.Unmarshal(rr.Body.Bytes(), resp)
require.NoError(t, err)
require.Equal(t, signedBlindedBeaconBlock.Message.Body.ExecutionPayloadHeader.BlockHash, resp.Bellatrix.BlockHash)
require.Equal(t, signedBlindedBeaconBlock.Message.Body.ExecutionPayloadHeader.BlockHash, resp.Capella.BlockHash)
}

func TestGetPayloadToOriginRelayOnly(t *testing.T) {
Expand Down Expand Up @@ -739,8 +741,8 @@ func TestGetPayloadToOriginRelayOnly(t *testing.T) {
require.Equal(t, 1, backend.relays[1].GetRequestCount(getHeaderPath))

// Prepare getPayload response
backend.relays[0].GetPayloadResponse = &types.GetPayloadResponse{
Data: blindedBlockToExecutionPayload(signedBlindedBeaconBlock),
backend.relays[0].GetBellatrixPayloadResponse = &types.GetPayloadResponse{
Data: blindedBlockToExecutionPayloadBellatrix(signedBlindedBeaconBlock),
}

// call getPayload, ensure it's only called on relay 0 (origin of the bid)
Expand Down
6 changes: 3 additions & 3 deletions server/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
"strings"
"time"

"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"
"github.com/ethereum/go-ethereum/common/hexutil"
Expand Down Expand Up @@ -148,15 +148,15 @@ func weiBigIntToEthBigFloat(wei *big.Int) (ethValue *big.Float) {
return
}

func ComputeBlockHash(payload *bellatrix.ExecutionPayload) (phase0.Hash32, error) {
func ComputeBlockHash(payload *capella.ExecutionPayload) (phase0.Hash32, error) {
header, err := executionPayloadToBlockHeader(payload)
if err != nil {
return phase0.Hash32{}, err
}
return phase0.Hash32(header.Hash()), nil
}

func executionPayloadToBlockHeader(payload *bellatrix.ExecutionPayload) (*types.Header, error) {
func executionPayloadToBlockHeader(payload *capella.ExecutionPayload) (*types.Header, error) {
transactionData := make([]*types.Transaction, len(payload.Transactions))
for i, encTx := range payload.Transactions {
var tx types.Transaction
Expand Down

0 comments on commit 8ce5596

Please sign in to comment.