Skip to content

Commit

Permalink
Publish block V1 flag fix (#585)
Browse files Browse the repository at this point in the history
* fix USE_V1_PUBLISH_BLOCK_ENDPOINT flag

USE_V1_PUBLISH_BLOCK_ENDPOINT was accidentally using the v2 publish endpoint when set instead of v1, and vice versa.

* add header version

* update lint

---------

Co-authored-by: Alexander Tesfamichael <[email protected]>
  • Loading branch information
avalonche and alextes authored Feb 15, 2024
1 parent d8a0d7b commit 108d5e3
Show file tree
Hide file tree
Showing 20 changed files with 131 additions and 88 deletions.
10 changes: 5 additions & 5 deletions .github/workflows/checks.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ jobs:
- name: Set up Go
uses: actions/setup-go@v3
with:
go-version: ^1.20
go-version: ^1.21
id: go

- name: Check out code into the Go module directory
Expand All @@ -40,20 +40,20 @@ jobs:
- name: Set up Go
uses: actions/setup-go@v3
with:
go-version: ^1.20
go-version: ^1.21
id: go

- name: Check out code into the Go module directory
uses: actions/checkout@v2
uses: actions/checkout@v3

- name: Install gofumpt
run: go install mvdan.cc/[email protected]

- name: Install staticcheck
run: go install honnef.co/go/tools/cmd/[email protected].2
run: go install honnef.co/go/tools/cmd/[email protected].6

- name: Install golangci-lint
run: go install github.com/golangci/golangci-lint/cmd/golangci-lint@v1.51.2
run: go install github.com/golangci/golangci-lint/cmd/golangci-lint@v1.55.0

- name: Lint
run: make lint
Expand Down
9 changes: 8 additions & 1 deletion .golangci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ linters:
- deadcode
- varcheck
- interfacebloat
- exhaustruct

#
# Disabled because of generics:
Expand Down Expand Up @@ -121,4 +122,10 @@ issues:
exclude-rules:
- path: (.+)_test.go
linters:
- exhaustruct
- exhaustruct
- path: database/.*.go
linters:
- goconst
- path: cmd/tool/.*.go
linters:
- goconst
8 changes: 4 additions & 4 deletions beaconclient/beacon_client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ func TestBeaconInstance(t *testing.T) {

vals, err := bc.GetStateValidators("1")
require.NoError(t, err)
require.Equal(t, 1, len(vals.Data))
require.Len(t, vals.Data, 1)
require.Contains(t, validatorResponseEntryToMap(vals.Data), "0x93247f2209abcacf57b75a51dafae777f9dd38bc7053d1af526f220a7489a6d3a2753e5f3e8b1cfe39b56f43611df74a")
}

Expand Down Expand Up @@ -188,7 +188,7 @@ func TestFetchValidators(t *testing.T) {

validators, err := backend.beaconClient.GetStateValidators("1")
require.NoError(t, err)
require.Equal(t, 1, len(validators.Data))
require.Len(t, validators.Data, 1)
require.Contains(t, validatorResponseEntryToMap(validators.Data), testPubKey)

// only beacon 2 should have a validator, and should be used by default
Expand All @@ -199,7 +199,7 @@ func TestFetchValidators(t *testing.T) {

validators, err = backend.beaconClient.GetStateValidators("1")
require.NoError(t, err)
require.Equal(t, 1, len(validators.Data))
require.Len(t, validators.Data, 1)
})
}

Expand Down Expand Up @@ -239,5 +239,5 @@ func TestGetForkSchedule(t *testing.T) {

forkSchedule, err := bc.GetForkSchedule()
require.NoError(t, err)
require.Equal(t, 4, len(forkSchedule.Data))
require.Len(t, forkSchedule.Data, 4)
}
7 changes: 4 additions & 3 deletions beaconclient/prod_beacon_instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"net/http"
"os"
"strings"
"time"

"github.com/attestantio/go-eth2-client/spec/capella"
Expand Down Expand Up @@ -244,12 +245,12 @@ func (c *ProdBeaconInstance) GetURI() string {
func (c *ProdBeaconInstance) PublishBlock(block *common.VersionedSignedProposal, broadcastMode BroadcastMode) (code int, err error) {
var uri string
if c.ffUseV1PublishBlockEndpoint {
uri = fmt.Sprintf("%s/eth/v2/beacon/blocks?broadcast_validation=%s", c.beaconURI, broadcastMode)
} else {
uri = fmt.Sprintf("%s/eth/v1/beacon/blocks", c.beaconURI)
} else {
uri = fmt.Sprintf("%s/eth/v2/beacon/blocks?broadcast_validation=%s", c.beaconURI, broadcastMode)
}
headers := http.Header{}
headers.Add("Eth-Consensus-Version", common.ForkVersionStringCapella) // optional in v1, required in v2
headers.Add("Eth-Consensus-Version", strings.ToLower(block.Version.String())) // optional in v1, required in v2
return fetchBeacon(http.MethodPost, uri, block, nil, nil, headers, false)
}

Expand Down
43 changes: 22 additions & 21 deletions common/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"errors"
"fmt"
"os"
"strconv"
"strings"

builderApiV1 "github.com/attestantio/go-builder-client/api/v1"
Expand Down Expand Up @@ -194,8 +195,8 @@ type BuilderGetValidatorsResponseEntry struct {

type BidTraceV2 struct {
builderApiV1.BidTrace
BlockNumber uint64 `db:"block_number" json:"block_number,string"`
NumTx uint64 `db:"num_tx" json:"num_tx,string"`
BlockNumber uint64 `db:"block_number" json:"block_number,string"`
NumTx uint64 `db:"num_tx" json:"num_tx,string"`
}

type BidTraceV2JSON struct {
Expand Down Expand Up @@ -267,17 +268,17 @@ func (b *BidTraceV2JSON) CSVHeader() []string {

func (b *BidTraceV2JSON) ToCSVRecord() []string {
return []string{
fmt.Sprint(b.Slot),
strconv.FormatUint(b.Slot, 10),
b.ParentHash,
b.BlockHash,
b.BuilderPubkey,
b.ProposerPubkey,
b.ProposerFeeRecipient,
fmt.Sprint(b.GasLimit),
fmt.Sprint(b.GasUsed),
strconv.FormatUint(b.GasLimit, 10),
strconv.FormatUint(b.GasUsed, 10),
b.Value,
fmt.Sprint(b.NumTx),
fmt.Sprint(b.BlockNumber),
strconv.FormatUint(b.NumTx, 10),
strconv.FormatUint(b.BlockNumber, 10),
}
}

Expand Down Expand Up @@ -309,20 +310,20 @@ func (b *BidTraceV2WithTimestampJSON) CSVHeader() []string {

func (b *BidTraceV2WithTimestampJSON) ToCSVRecord() []string {
return []string{
fmt.Sprint(b.Slot),
strconv.FormatUint(b.Slot, 10),
b.ParentHash,
b.BlockHash,
b.BuilderPubkey,
b.ProposerPubkey,
b.ProposerFeeRecipient,
fmt.Sprint(b.GasLimit),
fmt.Sprint(b.GasUsed),
strconv.FormatUint(b.GasLimit, 10),
strconv.FormatUint(b.GasUsed, 10),
b.Value,
fmt.Sprint(b.NumTx),
fmt.Sprint(b.BlockNumber),
fmt.Sprint(b.Timestamp),
fmt.Sprint(b.TimestampMs),
fmt.Sprint(b.OptimisticSubmission),
strconv.FormatUint(b.NumTx, 10),
strconv.FormatUint(b.BlockNumber, 10),
strconv.FormatInt(b.Timestamp, 10),
strconv.FormatInt(b.TimestampMs, 10),
strconv.FormatBool(b.OptimisticSubmission),
}
}

Expand Down Expand Up @@ -599,7 +600,7 @@ func (s *SubmitBlockRequestV2Optimistic) MarshalSSZTo(buf []byte) (dst []byte, e
s.Message = new(builderApiV1.BidTrace)
}
if dst, err = s.Message.MarshalSSZTo(dst); err != nil {
return
return nil, err
}

// Offset (1) 'ExecutionPayloadHeader'
Expand All @@ -624,13 +625,13 @@ func (s *SubmitBlockRequestV2Optimistic) MarshalSSZTo(buf []byte) (dst []byte, e

// Field (1) 'ExecutionPayloadHeader'
if dst, err = s.ExecutionPayloadHeader.MarshalSSZTo(dst); err != nil {
return
return nil, err
}

// Field (3) 'Transactions'
if size := len(s.Transactions); size > 1073741824 {
err = ssz.ErrListTooBigFn("SubmitBlockRequestV2Optimistic.Transactions", size, 1073741824)
return
return nil, err
}
{
offset = 4 * len(s.Transactions)
Expand All @@ -642,19 +643,19 @@ func (s *SubmitBlockRequestV2Optimistic) MarshalSSZTo(buf []byte) (dst []byte, e
for ii := 0; ii < len(s.Transactions); ii++ {
if size := len(s.Transactions[ii]); size > 1073741824 {
err = ssz.ErrBytesLengthFn("SubmitBlockRequestV2Optimistic.Transactions[ii]", size, 1073741824)
return
return nil, err
}
dst = append(dst, s.Transactions[ii]...)
}

// Field (4) 'Withdrawals'
if size := len(s.Withdrawals); size > 16 {
err = ssz.ErrListTooBigFn("SubmitBlockRequestV2Optimistic.Withdrawals", size, 16)
return
return nil, err
}
for ii := 0; ii < len(s.Withdrawals); ii++ {
if dst, err = s.Withdrawals[ii].MarshalSSZTo(dst); err != nil {
return
return nil, err
}
}
return dst, nil
Expand Down
28 changes: 14 additions & 14 deletions common/types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,24 +16,24 @@ import (
func makeTestSubmitBlockRequestV2Optimistic(t *testing.T) *SubmitBlockRequestV2Optimistic {
t.Helper()
testParentHash, err := utils.HexToHash("0xec51bd499a3fa0270f1446fbf05ff0b61157cfe4ec719bb4c3e834e339ee9c5c")
require.Nil(t, err)
require.NoError(t, err)
testBlockHash, err := utils.HexToHash("0x3f5b5aaa800a3d25c3f75e72dc45da89fdd58168f1358a9f94aac8b029361a0a")
require.Nil(t, err)
require.NoError(t, err)
testRandao, err := utils.HexToHash("0x8cf6b7fbfbaf80da001fe769fd02e9b8dbfa0a646d9cf51b5d7137dd4f8103cc")
require.Nil(t, err)
require.NoError(t, err)
testRoot, err := utils.HexToHash("0x7554727cee6d976a1dfdad80b392b37c87f0651ff5b01f6a0b3402bcfce92077")
require.Nil(t, err)
require.NoError(t, err)
testBuilderPubkey, err := utils.HexToPubkey("0xae7bde4839fa905b7d8125fd84cfdcd0c32cd74e1be3fa24263d71b520fc78113326ce0a90b95d73f19e6d8150a2f73b")
require.Nil(t, err)
require.NoError(t, err)
testProposerPubkey, err := utils.HexToPubkey("0xbb8e223239fa905b7d8125fd84cfdcd0c32cd74e1be3fa24263d71b520fc78113326ce0a90b95d73f19e6d8150a2f73b")
require.Nil(t, err)
require.NoError(t, err)
testAddress, err := utils.HexToAddress("0x95222290DD7278Aa3Ddd389Cc1E1d165CC4BAfe5")
require.Nil(t, err)
require.NoError(t, err)
testSignature, err := utils.HexToSignature("0xb06311be19c92307c06070578af9ad147c9c6ea902439eac19f785f3dca478c354b79a0af9b09839c3d06c1ccf2185b0162f4d4fbf981220f77586b52ed9ae8a8acfc953baaa30dee15e1b112913c6cbe02c780d7b5363a4af16563fe55c2e88")
require.Nil(t, err)
require.NoError(t, err)
testValue := new(uint256.Int)
err = testValue.SetFromDecimal("100")
require.Nil(t, err)
require.NoError(t, err)

return &SubmitBlockRequestV2Optimistic{
Message: &builderApiV1.BidTrace{
Expand Down Expand Up @@ -108,13 +108,13 @@ func TestSubmitBlockRequestV2Optimistic(t *testing.T) {

// Encode the object.
sszObj, err := obj.MarshalSSZ()
require.Nil(t, err)
require.Equal(t, obj.SizeSSZ(), len(sszObj))
require.NoError(t, err)
require.Len(t, sszObj, obj.SizeSSZ())

// Unmarshal the full object.
unmarshal := new(SubmitBlockRequestV2Optimistic)
err = unmarshal.UnmarshalSSZ(sszObj)
require.Nil(t, err)
require.NoError(t, err)

compareV2RequestEquality(t, obj, unmarshal)

Expand All @@ -125,10 +125,10 @@ func TestSubmitBlockRequestV2Optimistic(t *testing.T) {
// Unmarshal just the header.
unmarshalHeader := new(SubmitBlockRequestV2Optimistic)
err = unmarshalHeader.UnmarshalSSZHeaderOnly(sszObj)
require.Nil(t, err)
require.NoError(t, err)

compareV2RequestEquality(t, obj, unmarshalHeader)

// Make sure size is correct (must have 32 bytes of ExtraData).
require.Equal(t, unmarshalHeader.SizeSSZ(), 944)
require.Equal(t, 944, unmarshalHeader.SizeSSZ())
}
4 changes: 2 additions & 2 deletions common/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ func TestGetEnvStrSlice(t *testing.T) {
testEnvVar := "TESTENV_TestGetEnvStrSlice"
os.Unsetenv(testEnvVar)
r := GetEnvStrSlice(testEnvVar, nil)
require.Len(t, r, 0)
require.Empty(t, r)

t.Setenv(testEnvVar, "")
r = GetEnvStrSlice(testEnvVar, nil)
Expand Down Expand Up @@ -185,7 +185,7 @@ func TestGetBlockSubmissionInfo(t *testing.T) {
submission, err := GetBlockSubmissionInfo(tc.payload)
require.Equal(t, tc.expected, submission)
if tc.err == "" {
require.Nil(t, err)
require.NoError(t, err)
} else {
require.Equal(t, tc.err, err.Error())
}
Expand Down
9 changes: 5 additions & 4 deletions database/database_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"database/sql"
"fmt"
"os"
"strconv"
"testing"
"time"

Expand Down Expand Up @@ -205,7 +206,7 @@ func TestMigrations(t *testing.T) {
rowCount := 0
err := db.DB.QueryRow(query).Scan(&rowCount)
require.NoError(t, err)
require.Equal(t, len(migrations.Migrations.Migrations), rowCount)
require.Len(t, migrations.Migrations.Migrations, rowCount)
}

func TestSetBlockBuilderStatus(t *testing.T) {
Expand Down Expand Up @@ -448,7 +449,7 @@ func TestGetBuilderSubmissions(t *testing.T) {
require.Equal(t, optimisticSubmission, e.OptimisticSubmission)
require.Equal(t, pubkey, e.BuilderPubkey)
require.Equal(t, feeRecipient.String(), e.ProposerFeeRecipient)
require.Equal(t, fmt.Sprint(collateral), e.Value)
require.Equal(t, strconv.Itoa(collateral), e.Value)
}

func TestUpsertTooLateGetPayload(t *testing.T) {
Expand All @@ -462,7 +463,7 @@ func TestUpsertTooLateGetPayload(t *testing.T) {

entries, err := db.GetTooLateGetPayload(slot)
require.NoError(t, err)
require.Equal(t, 1, len(entries))
require.Len(t, entries, 1)
entry := entries[0]
require.Equal(t, pk, entry.ProposerPubkey)
require.Equal(t, hash, entry.BlockHash)
Expand All @@ -484,7 +485,7 @@ func TestUpsertTooLateGetPayload(t *testing.T) {

entries, err = db.GetTooLateGetPayload(slot)
require.NoError(t, err)
require.Equal(t, 2, len(entries))
require.Len(t, entries, 2)
entry = entries[1]
require.Equal(t, hash2, entry.BlockHash)
}
6 changes: 3 additions & 3 deletions database/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package database

import (
"database/sql"
"fmt"
"strconv"
"time"

builderApiV1 "github.com/attestantio/go-builder-client/api/v1"
Expand Down Expand Up @@ -114,9 +114,9 @@ var ExecutionPayloadEntryCSVHeader = []string{"id", "inserted_at", "slot", "prop

func (e *ExecutionPayloadEntry) ToCSVRecord() []string {
return []string{
fmt.Sprint(e.ID),
strconv.FormatInt(e.ID, 10),
e.InsertedAt.UTC().String(),
fmt.Sprint(e.Slot),
strconv.FormatUint(e.Slot, 10),
e.ProposerPubkey,
e.BlockHash,
e.Version,
Expand Down
2 changes: 1 addition & 1 deletion database/typesconv_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,5 +44,5 @@ func TestExecutionPayloadEntryToExecutionPayloadDeneb(t *testing.T) {
payload, err := ExecutionPayloadEntryToExecutionPayload(entry)
require.NoError(t, err)
require.Equal(t, "0xbd1ae4f7edb2315d2df70a8d9881fab8d6763fb1c00533ae729050928c38d05a", payload.Deneb.ExecutionPayload.BlockHash.String())
require.Equal(t, 1, len(payload.Deneb.BlobsBundle.Blobs))
require.Len(t, payload.Deneb.BlobsBundle.Blobs, 1)
}
Loading

0 comments on commit 108d5e3

Please sign in to comment.