2022-01-26 14:48:20 +00:00
|
|
|
package mock
|
2021-03-16 15:00:05 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2022-03-10 17:19:04 +00:00
|
|
|
"errors"
|
2021-03-16 15:00:05 +00:00
|
|
|
|
2022-08-16 12:20:13 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/v3/async/event"
|
|
|
|
"github.com/prysmaticlabs/prysm/v3/beacon-chain/core/signing"
|
|
|
|
fieldparams "github.com/prysmaticlabs/prysm/v3/config/fieldparams"
|
|
|
|
"github.com/prysmaticlabs/prysm/v3/crypto/bls"
|
|
|
|
"github.com/prysmaticlabs/prysm/v3/encoding/bytesutil"
|
|
|
|
ethpbservice "github.com/prysmaticlabs/prysm/v3/proto/eth/service"
|
|
|
|
validatorpb "github.com/prysmaticlabs/prysm/v3/proto/prysm/v1alpha1/validator-client"
|
|
|
|
"github.com/prysmaticlabs/prysm/v3/testing/util"
|
|
|
|
"github.com/prysmaticlabs/prysm/v3/time/slots"
|
|
|
|
"github.com/prysmaticlabs/prysm/v3/validator/keymanager"
|
2021-03-16 15:00:05 +00:00
|
|
|
)
|
|
|
|
|
2021-04-23 12:06:05 +00:00
|
|
|
// MockKeymanager --
|
2021-03-16 15:00:05 +00:00
|
|
|
type MockKeymanager struct {
|
2022-01-06 17:33:08 +00:00
|
|
|
PublicKeys [][fieldparams.BLSPubkeyLength]byte
|
|
|
|
ReloadPublicKeysChan chan [][fieldparams.BLSPubkeyLength]byte
|
2021-04-30 15:15:22 +00:00
|
|
|
ReloadPublicKeysCalled bool
|
|
|
|
accountsChangedFeed *event.Feed
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewMock() MockKeymanager {
|
|
|
|
return MockKeymanager{
|
|
|
|
accountsChangedFeed: new(event.Feed),
|
2022-01-06 17:33:08 +00:00
|
|
|
ReloadPublicKeysChan: make(chan [][fieldparams.BLSPubkeyLength]byte, 1),
|
2021-04-30 15:15:22 +00:00
|
|
|
}
|
2021-03-16 15:00:05 +00:00
|
|
|
}
|
|
|
|
|
2021-04-23 12:06:05 +00:00
|
|
|
// FetchValidatingPublicKeys --
|
2022-01-06 17:33:08 +00:00
|
|
|
func (m *MockKeymanager) FetchValidatingPublicKeys(context.Context) ([][fieldparams.BLSPubkeyLength]byte, error) {
|
2021-03-16 15:00:05 +00:00
|
|
|
return m.PublicKeys, nil
|
|
|
|
}
|
|
|
|
|
2021-04-23 12:06:05 +00:00
|
|
|
// Sign --
|
2022-06-06 19:32:41 +00:00
|
|
|
func (*MockKeymanager) Sign(_ context.Context, s *validatorpb.SignRequest) (bls.Signature, error) {
|
|
|
|
key, err := bls.RandKey()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
st, _ := util.DeterministicGenesisState(nil, 1)
|
|
|
|
e := slots.ToEpoch(st.Slot())
|
|
|
|
byteValue, err := signing.ComputeDomainAndSign(st, e, s.SigningSlot, bytesutil.ToBytes4(s.SignatureDomain), key)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return bls.SignatureFromBytes(byteValue)
|
2021-03-16 15:00:05 +00:00
|
|
|
}
|
|
|
|
|
2021-04-23 12:06:05 +00:00
|
|
|
// SubscribeAccountChanges --
|
2022-01-06 17:33:08 +00:00
|
|
|
func (m *MockKeymanager) SubscribeAccountChanges(chan [][fieldparams.BLSPubkeyLength]byte) event.Subscription {
|
2021-04-30 15:15:22 +00:00
|
|
|
return m.accountsChangedFeed.Subscribe(m.ReloadPublicKeysChan)
|
2021-03-16 15:00:05 +00:00
|
|
|
}
|
|
|
|
|
2021-04-23 12:06:05 +00:00
|
|
|
// ReloadPublicKeys --
|
2022-01-06 17:33:08 +00:00
|
|
|
func (m *MockKeymanager) ReloadPublicKeys(context.Context) ([][fieldparams.BLSPubkeyLength]byte, error) {
|
2021-04-30 15:15:22 +00:00
|
|
|
m.ReloadPublicKeysCalled = true
|
|
|
|
m.ReloadPublicKeysChan <- m.PublicKeys
|
2021-03-16 15:00:05 +00:00
|
|
|
return m.PublicKeys, nil
|
|
|
|
}
|
2022-03-10 17:19:04 +00:00
|
|
|
|
|
|
|
// ExtractKeystores --
|
|
|
|
func (*MockKeymanager) ExtractKeystores(
|
2022-06-27 13:34:38 +00:00
|
|
|
_ context.Context, _ []bls.PublicKey, _ string,
|
2022-03-10 17:19:04 +00:00
|
|
|
) ([]*keymanager.Keystore, error) {
|
|
|
|
return nil, errors.New("extracting keys not supported for a remote keymanager")
|
|
|
|
}
|
2022-03-22 03:04:09 +00:00
|
|
|
|
|
|
|
// ListKeymanagerAccounts --
|
|
|
|
func (*MockKeymanager) ListKeymanagerAccounts(
|
|
|
|
context.Context, keymanager.ListKeymanagerAccountConfig) error {
|
|
|
|
return nil
|
|
|
|
}
|
2022-03-30 21:52:54 +00:00
|
|
|
|
|
|
|
func (*MockKeymanager) DeleteKeystores(context.Context, [][]byte,
|
|
|
|
) ([]*ethpbservice.DeletedKeystoreStatus, error) {
|
|
|
|
return nil, nil
|
|
|
|
}
|