Files
prysm/validator/client/beacon-api/propose_exit_test.go
james-prysm cf94ccbf72 node fallback cleanup (#16316)
**What type of PR is this?**

 Other

**What does this PR do? Why is it needed?**

Follow up to https://github.com/OffchainLabs/prysm/pull/16215 this pr
improves logging, fixes stuttering in package naming, adds additional
unit tests, and deduplicates fallback node code.

**Which issues(s) does this PR fix?**

fixes a potential race if reconnecting to the same host very quickly
which has a stale connection still.

**Other notes for review**

**Acknowledgements**

- [x] I have read
[CONTRIBUTING.md](https://github.com/prysmaticlabs/prysm/blob/develop/CONTRIBUTING.md).
- [x] I have included a uniquely named [changelog fragment
file](https://github.com/prysmaticlabs/prysm/blob/develop/CONTRIBUTING.md#maintaining-changelogmd).
- [x] I have added a description with sufficient context for reviewers
to understand this PR.
- [x] I have tested that my changes work as expected and I added a
testing plan to the PR description (if applicable).
2026-02-04 15:59:42 +00:00

111 lines
3.0 KiB
Go

package beacon_api
import (
"bytes"
"encoding/json"
"testing"
"github.com/OffchainLabs/prysm/v7/api/server/structs"
ethpb "github.com/OffchainLabs/prysm/v7/proto/prysm/v1alpha1"
"github.com/OffchainLabs/prysm/v7/testing/assert"
"github.com/OffchainLabs/prysm/v7/testing/require"
"github.com/OffchainLabs/prysm/v7/validator/client/beacon-api/mock"
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/pkg/errors"
"go.uber.org/mock/gomock"
)
const proposeExitTestEndpoint = "/eth/v1/beacon/pool/voluntary_exits"
func TestProposeExit_Valid(t *testing.T) {
const signature = "0xd0a030a1d6b4f8217062ccc98088fbd908797f107aaa825f2366f090445fa79a6417789aa1d232c4f9b1e56671165bde25eb5586f94fc5677df593b99369684e8f413b1bfbd3fa6f20615244f9381895c71d4f7136c528092a3d03294a98be2d"
ctrl := gomock.NewController(t)
defer ctrl.Finish()
jsonSignedVoluntaryExit := structs.SignedVoluntaryExit{
Message: &structs.VoluntaryExit{
Epoch: "1",
ValidatorIndex: "2",
},
Signature: signature,
}
marshalledVoluntaryExit, err := json.Marshal(jsonSignedVoluntaryExit)
require.NoError(t, err)
ctx := t.Context()
handler := mock.NewMockJsonRestHandler(ctrl)
handler.EXPECT().Post(
gomock.Any(),
proposeExitTestEndpoint,
nil,
bytes.NewBuffer(marshalledVoluntaryExit),
nil,
).Return(
nil,
).Times(1)
decodedSignature, err := hexutil.Decode(signature)
require.NoError(t, err)
protoSignedVoluntaryExit := &ethpb.SignedVoluntaryExit{
Exit: &ethpb.VoluntaryExit{
Epoch: 1,
ValidatorIndex: 2,
},
Signature: decodedSignature,
}
expectedExitRoot, err := protoSignedVoluntaryExit.Exit.HashTreeRoot()
require.NoError(t, err)
validatorClient := &beaconApiValidatorClient{handler: handler}
exitResponse, err := validatorClient.proposeExit(ctx, protoSignedVoluntaryExit)
require.NoError(t, err)
assert.DeepEqual(t, expectedExitRoot[:], exitResponse.ExitRoot)
}
func TestProposeExit_NilSignedVoluntaryExit(t *testing.T) {
validatorClient := &beaconApiValidatorClient{}
_, err := validatorClient.proposeExit(t.Context(), nil)
assert.ErrorContains(t, "signed voluntary exit is nil", err)
}
func TestProposeExit_NilExit(t *testing.T) {
validatorClient := &beaconApiValidatorClient{}
_, err := validatorClient.proposeExit(t.Context(), &ethpb.SignedVoluntaryExit{})
assert.ErrorContains(t, "exit is nil", err)
}
func TestProposeExit_BadRequest(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()
ctx := t.Context()
handler := mock.NewMockJsonRestHandler(ctrl)
handler.EXPECT().Post(
gomock.Any(),
proposeExitTestEndpoint,
nil,
gomock.Any(),
nil,
).Return(
errors.New("foo error"),
).Times(1)
protoSignedVoluntaryExit := &ethpb.SignedVoluntaryExit{
Exit: &ethpb.VoluntaryExit{
Epoch: 1,
ValidatorIndex: 2,
},
Signature: []byte{3},
}
validatorClient := &beaconApiValidatorClient{handler: handler}
_, err := validatorClient.proposeExit(ctx, protoSignedVoluntaryExit)
assert.ErrorContains(t, "foo error", err)
}