Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

*: support electra in existing codebase; tests #3487

Merged
merged 2 commits into from
Jan 31, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
128 changes: 100 additions & 28 deletions app/eth2wrap/eth2wrap_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
eth2api "github.com/attestantio/go-eth2-client/api"
eth2v1 "github.com/attestantio/go-eth2-client/api/v1"
eth2spec "github.com/attestantio/go-eth2-client/spec"
eth2e "github.com/attestantio/go-eth2-client/spec/electra"
eth2p0 "github.com/attestantio/go-eth2-client/spec/phase0"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -261,37 +262,108 @@ func TestCtxCancel(t *testing.T) {
}
}

func TestBlockAttestations(t *testing.T) {
atts := []*eth2spec.VersionedAttestation{
testutil.RandomDenebVersionedAttestation(),
testutil.RandomDenebVersionedAttestation(),
}

statusCode := http.StatusOK
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
require.Equal(t, http.MethodGet, r.Method)
require.Equal(t, "/eth/v2/beacon/blocks/head/attestations", r.URL.Path)
b, err := json.Marshal(struct {
Data []*eth2p0.Attestation
}{
Data: []*eth2p0.Attestation{atts[0].Deneb, atts[1].Deneb},
})
require.NoError(t, err)
func TestBlockAttestationsV2(t *testing.T) {
phase0Att1 := testutil.RandomPhase0Attestation()
phase0Att2 := testutil.RandomPhase0Attestation()
electraAtt1 := testutil.RandomElectraAttestation()
electraAtt2 := testutil.RandomElectraAttestation()

w.Header().Add("Eth-Consensus-Version", "deneb")
w.WriteHeader(statusCode)
_, _ = w.Write(b)
}))
tests := []struct {
version string
attestations []*eth2spec.VersionedAttestation
serverJSONStruct any
expErr string
}{
{
version: "electra",
attestations: []*eth2spec.VersionedAttestation{
{Version: eth2spec.DataVersionElectra, Electra: electraAtt1},
{Version: eth2spec.DataVersionElectra, Electra: electraAtt2},
},
serverJSONStruct: struct{ Data []*eth2e.Attestation }{Data: []*eth2e.Attestation{electraAtt1, electraAtt2}},
expErr: "",
},
{
version: "deneb",
attestations: []*eth2spec.VersionedAttestation{
{Version: eth2spec.DataVersionDeneb, Deneb: phase0Att1},
{Version: eth2spec.DataVersionDeneb, Deneb: phase0Att2},
},
serverJSONStruct: struct{ Data []*eth2p0.Attestation }{Data: []*eth2p0.Attestation{phase0Att1, phase0Att2}},
expErr: "",
},
{
version: "capella",
attestations: []*eth2spec.VersionedAttestation{
{Version: eth2spec.DataVersionCapella, Capella: phase0Att1},
{Version: eth2spec.DataVersionCapella, Capella: phase0Att2},
},
serverJSONStruct: struct{ Data []*eth2p0.Attestation }{Data: []*eth2p0.Attestation{phase0Att1, phase0Att2}},
expErr: "",
},
{
version: "bellatrix",
attestations: []*eth2spec.VersionedAttestation{
{Version: eth2spec.DataVersionBellatrix, Bellatrix: phase0Att1},
{Version: eth2spec.DataVersionBellatrix, Bellatrix: phase0Att2},
},
serverJSONStruct: struct{ Data []*eth2p0.Attestation }{Data: []*eth2p0.Attestation{phase0Att1, phase0Att2}},
expErr: "",
},
{
version: "altair",
attestations: []*eth2spec.VersionedAttestation{
{Version: eth2spec.DataVersionAltair, Altair: phase0Att1},
{Version: eth2spec.DataVersionAltair, Altair: phase0Att2},
},
serverJSONStruct: struct{ Data []*eth2p0.Attestation }{Data: []*eth2p0.Attestation{phase0Att1, phase0Att2}},
expErr: "",
},
{
version: "phase0",
attestations: []*eth2spec.VersionedAttestation{
{Version: eth2spec.DataVersionPhase0, Phase0: phase0Att1},
{Version: eth2spec.DataVersionPhase0, Phase0: phase0Att2},
},
serverJSONStruct: struct{ Data []*eth2p0.Attestation }{Data: []*eth2p0.Attestation{phase0Att1, phase0Att2}},
expErr: "",
},
{
version: "unknown version",
attestations: nil,
serverJSONStruct: struct{ Data []*eth2p0.Attestation }{Data: []*eth2p0.Attestation{phase0Att1, phase0Att2}},
expErr: "failed to get consensus version",
},
}
for _, test := range tests {
t.Run(test.version, func(t *testing.T) {
statusCode := http.StatusOK
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
require.Equal(t, http.MethodGet, r.Method)
require.Equal(t, "/eth/v2/beacon/blocks/head/attestations", r.URL.Path)
b, err := json.Marshal(test.serverJSONStruct)
require.NoError(t, err)

w.Header().Add("Eth-Consensus-Version", test.version)
w.WriteHeader(statusCode)
_, _ = w.Write(b)
}))

cl := eth2wrap.NewHTTPAdapterForT(t, srv.URL, time.Hour)
resp, err := cl.BlockAttestationsV2(context.Background(), "head")
require.NoError(t, err)
require.Equal(t, atts, resp)
cl := eth2wrap.NewHTTPAdapterForT(t, srv.URL, time.Hour)
resp, err := cl.BlockAttestationsV2(context.Background(), "head")
if test.expErr != "" {
require.ErrorContains(t, err, test.expErr)
} else {
require.NoError(t, err)
}
require.Equal(t, test.attestations, resp)

statusCode = http.StatusNotFound
resp, err = cl.BlockAttestationsV2(context.Background(), "head")
require.NoError(t, err)
require.Empty(t, resp)
statusCode = http.StatusNotFound
resp, err = cl.BlockAttestationsV2(context.Background(), "head")
require.NoError(t, err)
require.Empty(t, resp)
})
}
}

// TestOneError tests the case where one of the servers returns errors.
Expand Down
13 changes: 13 additions & 0 deletions app/eth2wrap/synthproposer.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
eth2api "github.com/attestantio/go-eth2-client/api"
eth2v1 "github.com/attestantio/go-eth2-client/api/v1"
eth2deneb "github.com/attestantio/go-eth2-client/api/v1/deneb"
eth2electra "github.com/attestantio/go-eth2-client/api/v1/electra"
eth2spec "github.com/attestantio/go-eth2-client/spec"
"github.com/attestantio/go-eth2-client/spec/bellatrix"
eth2p0 "github.com/attestantio/go-eth2-client/spec/phase0"
Expand Down Expand Up @@ -174,6 +175,14 @@
proposal.Deneb.Block.ProposerIndex = vIdx
proposal.Deneb.Block.Body.ExecutionPayload.FeeRecipient = feeRecipient
proposal.Deneb.Block.Body.ExecutionPayload.Transactions = fraction(proposal.Deneb.Block.Body.ExecutionPayload.Transactions)
case eth2spec.DataVersionElectra:
proposal.Electra = &eth2electra.BlockContents{}
proposal.Electra.Block = signedBlock.Electra.Message
proposal.Electra.Block.Body.Graffiti = GetSyntheticGraffiti()
proposal.Electra.Block.Slot = slot
proposal.Electra.Block.ProposerIndex = vIdx
proposal.Electra.Block.Body.ExecutionPayload.FeeRecipient = feeRecipient
proposal.Electra.Block.Body.ExecutionPayload.Transactions = fraction(proposal.Electra.Block.Body.ExecutionPayload.Transactions)
default:
return nil, errors.New("unsupported proposal version")
}
Expand Down Expand Up @@ -225,6 +234,8 @@
graffiti = block.Capella.Message.Body.Graffiti
case eth2spec.DataVersionDeneb:
graffiti = block.Deneb.Message.Body.Graffiti
case eth2spec.DataVersionElectra:
graffiti = block.Electra.Message.Body.Graffiti
default:
return false
}
Expand All @@ -246,6 +257,8 @@
graffiti = block.Capella.Message.Body.Graffiti
case eth2spec.DataVersionDeneb:
graffiti = block.Deneb.SignedBlock.Message.Body.Graffiti
case eth2spec.DataVersionElectra:
graffiti = block.Electra.SignedBlock.Message.Body.Graffiti

Check warning on line 261 in app/eth2wrap/synthproposer.go

View check run for this annotation

Codecov / codecov/patch

app/eth2wrap/synthproposer.go#L260-L261

Added lines #L260 - L261 were not covered by tests
default:
return false
}
Expand Down
Loading
Loading