-
Notifications
You must be signed in to change notification settings - Fork 136
testutil: vmock payload attestation and simnet e2e #4670
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
Open
KaloyanTanev
wants to merge
2
commits into
gloas
Choose a base branch
from
kalo/gloas-ptc-validatormock
base: gloas
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+306
−1
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,117 @@ | ||
| // Copyright © 2022-2026 Obol Labs Inc. Licensed under the terms of a Business Source License 1.1 | ||
|
|
||
| package validatormock | ||
|
|
||
| import ( | ||
| "context" | ||
|
|
||
| eth2client "github.com/attestantio/go-eth2-client" | ||
| 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" | ||
| "github.com/attestantio/go-eth2-client/spec/gloas" | ||
| eth2p0 "github.com/attestantio/go-eth2-client/spec/phase0" | ||
|
|
||
| "github.com/obolnetwork/charon/app/errors" | ||
| "github.com/obolnetwork/charon/app/eth2wrap" | ||
| "github.com/obolnetwork/charon/eth2util/signing" | ||
| ) | ||
|
|
||
| // PayloadAttest performs the payload timeliness committee duty for the provided slot. | ||
| // It is stateless and does nothing if no active validator is a payload timeliness | ||
| // committee member for the slot. | ||
| func PayloadAttest(ctx context.Context, eth2Cl eth2wrap.Client, signFunc SignFunc, slot eth2p0.Slot) error { | ||
| valMap, err := eth2Cl.ActiveValidators(ctx) | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
KaloyanTanev marked this conversation as resolved.
|
||
|
|
||
| _, slotsPerEpoch, err := eth2wrap.FetchSlotsConfig(ctx, eth2Cl) | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| epoch := eth2p0.Epoch(uint64(slot) / slotsPerEpoch) | ||
|
|
||
| var indexes []eth2p0.ValidatorIndex | ||
| for index := range valMap { | ||
| indexes = append(indexes, index) | ||
| } | ||
|
|
||
| eth2Resp, err := eth2Cl.PTCDuties(ctx, ð2api.PTCDutiesOpts{ | ||
| Epoch: epoch, | ||
| Indices: indexes, | ||
| }) | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| var duties []*eth2v1.PTCDuty | ||
|
|
||
| for _, duty := range eth2Resp.Data { | ||
| if duty.Slot == slot { | ||
| duties = append(duties, duty) | ||
| } | ||
| } | ||
|
|
||
| if len(duties) == 0 { | ||
| return nil | ||
| } | ||
|
|
||
| // The payload attestation data is per-slot, all committee members attest to the same data. | ||
| dataResp, err := eth2Cl.PayloadAttestationData(ctx, ð2api.PayloadAttestationDataOpts{Slot: slot}) | ||
| if errors.Is(err, eth2client.ErrNoPayloadAttestationData) { | ||
| // The beacon node has seen no block for the slot, so there is nothing to attest. | ||
| return nil | ||
| } else if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| versioned := dataResp.Data | ||
| if versioned == nil { | ||
| return errors.New("versioned payload attestation data is nil") | ||
| } | ||
|
Copilot marked this conversation as resolved.
|
||
|
|
||
| var data *gloas.PayloadAttestationData | ||
|
|
||
| switch versioned.Version { | ||
| case eth2spec.DataVersionGloas: | ||
| data = versioned.Gloas | ||
| default: | ||
| return errors.New("unknown payload attestation data version") | ||
| } | ||
|
|
||
| if data == nil { | ||
| return errors.New("no gloas payload attestation data") | ||
| } | ||
|
|
||
| root, err := data.HashTreeRoot() | ||
| if err != nil { | ||
| return errors.Wrap(err, "hash payload attestation data") | ||
| } | ||
|
|
||
| sigData, err := signing.GetDataRoot(ctx, eth2Cl, signing.DomainPTCAttester, epoch, root) | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| var msgs []*eth2spec.VersionedPayloadAttestationMessage | ||
|
|
||
| for _, duty := range duties { | ||
| sig, err := signFunc(duty.PubKey, sigData[:]) | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| msgs = append(msgs, ð2spec.VersionedPayloadAttestationMessage{ | ||
| Version: versioned.Version, | ||
| Gloas: &gloas.PayloadAttestationMessage{ | ||
| ValidatorIndex: duty.ValidatorIndex, | ||
| Data: data, | ||
| Signature: sig, | ||
| }, | ||
| }) | ||
| } | ||
|
|
||
| return eth2Cl.SubmitPayloadAttestationMessages(ctx, ð2api.SubmitPayloadAttestationMessagesOpts{Messages: msgs}) | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,120 @@ | ||
| // Copyright © 2022-2026 Obol Labs Inc. Licensed under the terms of a Business Source License 1.1 | ||
|
|
||
| package validatormock_test | ||
|
|
||
| import ( | ||
| "context" | ||
| "testing" | ||
|
|
||
| eth2client "github.com/attestantio/go-eth2-client" | ||
| 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" | ||
| eth2p0 "github.com/attestantio/go-eth2-client/spec/phase0" | ||
| "github.com/stretchr/testify/require" | ||
|
|
||
| "github.com/obolnetwork/charon/testutil" | ||
| "github.com/obolnetwork/charon/testutil/beaconmock" | ||
| "github.com/obolnetwork/charon/testutil/validatormock" | ||
| ) | ||
|
|
||
| func TestPayloadAttest(t *testing.T) { | ||
| const slot = 123 | ||
|
|
||
| valSet := beaconmock.ValidatorSetA | ||
|
|
||
| var ( | ||
| dutyIdx eth2p0.ValidatorIndex | ||
| dutyPubkey eth2p0.BLSPubKey | ||
| ) | ||
|
|
||
| for idx, val := range valSet { | ||
| dutyIdx = idx | ||
| dutyPubkey = val.Validator.PublicKey | ||
|
|
||
| break | ||
| } | ||
|
|
||
| attData := testutil.RandomVersionedPayloadAttestationData() | ||
| attData.Gloas.Slot = slot | ||
|
|
||
| sig := testutil.RandomEth2Signature() | ||
| signFunc := func(key eth2p0.BLSPubKey, _ []byte) (eth2p0.BLSSignature, error) { | ||
| require.Equal(t, dutyPubkey, key) | ||
| return sig, nil | ||
| } | ||
|
|
||
| newMock := func(t *testing.T) beaconmock.Mock { | ||
| t.Helper() | ||
|
|
||
| bmock, err := beaconmock.New(t.Context(), beaconmock.WithValidatorSet(valSet)) | ||
| require.NoError(t, err) | ||
|
|
||
| bmock.PTCDutiesFunc = func(context.Context, eth2p0.Epoch, []eth2p0.ValidatorIndex) ([]*eth2v1.PTCDuty, error) { | ||
| return []*eth2v1.PTCDuty{{ | ||
| PubKey: dutyPubkey, | ||
| Slot: slot, | ||
| ValidatorIndex: dutyIdx, | ||
| }}, nil | ||
| } | ||
| bmock.PayloadAttestationDataFunc = func(context.Context, eth2p0.Slot) (*eth2spec.VersionedPayloadAttestationData, error) { | ||
| return attData, nil | ||
| } | ||
|
|
||
| return bmock | ||
| } | ||
|
|
||
| t.Run("submit message", func(t *testing.T) { | ||
| bmock := newMock(t) | ||
|
|
||
| var submitted *eth2api.SubmitPayloadAttestationMessagesOpts | ||
|
|
||
| bmock.SubmitPayloadAttestationMessagesFunc = func(_ context.Context, opts *eth2api.SubmitPayloadAttestationMessagesOpts) error { | ||
| submitted = opts | ||
| return nil | ||
| } | ||
|
|
||
| require.NoError(t, validatormock.PayloadAttest(t.Context(), bmock, signFunc, slot)) | ||
| require.NotNil(t, submitted) | ||
| require.Len(t, submitted.Messages, 1) | ||
|
|
||
| msg := submitted.Messages[0] | ||
| require.Equal(t, eth2spec.DataVersionGloas, msg.Version) | ||
| require.Equal(t, dutyIdx, msg.Gloas.ValidatorIndex) | ||
| require.Equal(t, attData.Gloas, msg.Gloas.Data) | ||
| require.Equal(t, sig, msg.Gloas.Signature) | ||
| }) | ||
|
|
||
| t.Run("no duty for slot", func(t *testing.T) { | ||
| bmock := newMock(t) | ||
| bmock.SubmitPayloadAttestationMessagesFunc = func(context.Context, *eth2api.SubmitPayloadAttestationMessagesOpts) error { | ||
| require.Fail(t, "unexpected submission") | ||
| return nil | ||
| } | ||
|
|
||
| require.NoError(t, validatormock.PayloadAttest(t.Context(), bmock, signFunc, slot+1)) | ||
| }) | ||
|
|
||
| t.Run("no block seen", func(t *testing.T) { | ||
| bmock := newMock(t) | ||
| bmock.PayloadAttestationDataFunc = func(context.Context, eth2p0.Slot) (*eth2spec.VersionedPayloadAttestationData, error) { | ||
| return nil, eth2client.ErrNoPayloadAttestationData | ||
| } | ||
| bmock.SubmitPayloadAttestationMessagesFunc = func(context.Context, *eth2api.SubmitPayloadAttestationMessagesOpts) error { | ||
| require.Fail(t, "unexpected submission") | ||
| return nil | ||
| } | ||
|
|
||
| require.NoError(t, validatormock.PayloadAttest(t.Context(), bmock, signFunc, slot)) | ||
| }) | ||
|
|
||
| t.Run("unknown version", func(t *testing.T) { | ||
| bmock := newMock(t) | ||
| bmock.PayloadAttestationDataFunc = func(context.Context, eth2p0.Slot) (*eth2spec.VersionedPayloadAttestationData, error) { | ||
| return ð2spec.VersionedPayloadAttestationData{Version: eth2spec.DataVersionElectra}, nil | ||
| } | ||
|
|
||
| require.ErrorContains(t, validatormock.PayloadAttest(t.Context(), bmock, signFunc, slot), | ||
| "unknown payload attestation data version") | ||
| }) | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.