2022-09-06 18:29:44 +00:00
|
|
|
package builder
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"testing"
|
2023-05-22 14:54:09 +00:00
|
|
|
"time"
|
2022-09-06 18:29:44 +00:00
|
|
|
|
2024-02-15 05:46:47 +00:00
|
|
|
buildertesting "github.com/prysmaticlabs/prysm/v5/api/client/builder/testing"
|
|
|
|
blockchainTesting "github.com/prysmaticlabs/prysm/v5/beacon-chain/blockchain/testing"
|
|
|
|
dbtesting "github.com/prysmaticlabs/prysm/v5/beacon-chain/db/testing"
|
|
|
|
"github.com/prysmaticlabs/prysm/v5/encoding/bytesutil"
|
|
|
|
eth "github.com/prysmaticlabs/prysm/v5/proto/prysm/v1alpha1"
|
|
|
|
"github.com/prysmaticlabs/prysm/v5/testing/assert"
|
|
|
|
"github.com/prysmaticlabs/prysm/v5/testing/require"
|
2022-09-06 18:29:44 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func Test_NewServiceWithBuilder(t *testing.T) {
|
|
|
|
s, err := NewService(context.Background(), WithBuilderClient(&buildertesting.MockClient{}))
|
|
|
|
require.NoError(t, err)
|
|
|
|
assert.Equal(t, true, s.Configured())
|
|
|
|
}
|
|
|
|
|
|
|
|
func Test_NewServiceWithoutBuilder(t *testing.T) {
|
|
|
|
s, err := NewService(context.Background())
|
|
|
|
require.NoError(t, err)
|
|
|
|
assert.Equal(t, false, s.Configured())
|
|
|
|
}
|
|
|
|
|
|
|
|
func Test_RegisterValidator(t *testing.T) {
|
|
|
|
ctx := context.Background()
|
|
|
|
db := dbtesting.SetupDB(t)
|
|
|
|
headFetcher := &blockchainTesting.ChainService{}
|
|
|
|
builder := buildertesting.NewClient()
|
|
|
|
s, err := NewService(ctx, WithDatabase(db), WithHeadFetcher(headFetcher), WithBuilderClient(&builder))
|
|
|
|
require.NoError(t, err)
|
|
|
|
pubkey := bytesutil.ToBytes48([]byte("pubkey"))
|
|
|
|
var feeRecipient [20]byte
|
|
|
|
require.NoError(t, s.RegisterValidator(ctx, []*eth.SignedValidatorRegistrationV1{{Message: ð.ValidatorRegistrationV1{Pubkey: pubkey[:], FeeRecipient: feeRecipient[:]}}}))
|
|
|
|
assert.Equal(t, true, builder.RegisteredVals[pubkey])
|
|
|
|
}
|
2023-04-14 05:42:57 +00:00
|
|
|
|
2023-05-22 14:54:09 +00:00
|
|
|
func Test_RegisterValidator_WithCache(t *testing.T) {
|
|
|
|
ctx := context.Background()
|
|
|
|
headFetcher := &blockchainTesting.ChainService{}
|
|
|
|
builder := buildertesting.NewClient()
|
|
|
|
s, err := NewService(ctx, WithRegistrationCache(), WithHeadFetcher(headFetcher), WithBuilderClient(&builder))
|
|
|
|
require.NoError(t, err)
|
|
|
|
pubkey := bytesutil.ToBytes48([]byte("pubkey"))
|
|
|
|
var feeRecipient [20]byte
|
|
|
|
reg := ð.ValidatorRegistrationV1{Pubkey: pubkey[:], Timestamp: uint64(time.Now().UTC().Unix()), FeeRecipient: feeRecipient[:]}
|
|
|
|
require.NoError(t, s.RegisterValidator(ctx, []*eth.SignedValidatorRegistrationV1{{Message: reg}}))
|
|
|
|
registration, err := s.registrationCache.RegistrationByIndex(0)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.DeepEqual(t, reg, registration)
|
|
|
|
}
|
|
|
|
|
2023-04-14 05:42:57 +00:00
|
|
|
func Test_BuilderMethodsWithouClient(t *testing.T) {
|
|
|
|
s, err := NewService(context.Background())
|
|
|
|
require.NoError(t, err)
|
|
|
|
assert.Equal(t, false, s.Configured())
|
|
|
|
|
|
|
|
_, err = s.GetHeader(context.Background(), 0, [32]byte{}, [48]byte{})
|
|
|
|
assert.ErrorContains(t, ErrNoBuilder.Error(), err)
|
|
|
|
|
2023-11-29 14:28:37 +00:00
|
|
|
_, _, err = s.SubmitBlindedBlock(context.Background(), nil)
|
2023-04-14 05:42:57 +00:00
|
|
|
assert.ErrorContains(t, ErrNoBuilder.Error(), err)
|
|
|
|
|
|
|
|
err = s.RegisterValidator(context.Background(), nil)
|
|
|
|
assert.ErrorContains(t, ErrNoBuilder.Error(), err)
|
|
|
|
}
|