mirror of
https://gitlab.com/pulsechaincom/prysm-pulse.git
synced 2025-01-10 11:41:21 +00:00
8da8855ad5
* breaking up changes from cli pr * reverting some changes, adding in changes from remote-web3signer * adding raul's change * adding fork info to signing calls * fixing imports * gaz * fixing gofmt * removing unneeded comment * Update validator/client/aggregate.go Co-authored-by: Preston Van Loon <preston@prysmaticlabs.com> * Update validator/client/sync_committee.go Co-authored-by: Preston Van Loon <preston@prysmaticlabs.com> * addressing comments * revert proto changes * proto changes * reserve * reserve * switching to passing slot from passing fork, using slot to find the fork * removing unneeded check * fixing missed unit test * optional * optional * gaz * improving some definitions with constants * improving some definitions with constants * rem opt * rem * gaz * moving mocks to its own folder * adding in bazel field to fix * fixing type check error * fixing build * fixing strict imports * fixing dependencies * changing bazel build * changing bazel build * changing bazel build * removing testing only dependency * removing dependency on testing util package * update bazel build * Update checktags_test.go * Update active_balance.go * Update sync_committee_minimal.go * Update sync_committee_mainnet.go * Update active_balance_disabled.go * Update committee.go * Update committee_disabled.go * Update sync_committee.pb.gw.go * Update powchain.pb.gw.go * Update proposer_indices.go * Update proposer_indices_disabled.go * Update sync_committee.go * Update mainnet.go * Update p2p_messages.pb.gw.go * Update finalized_block_root_container.pb.gw.go * Update beacon_block.pb.gw.go * Update attestation.pb.gw.go * Update secret_key_test.go * Update beacon_state.pb.gw.go * Update version.pb.gw.go * Update sync_committee.pb.gw.go * Update sync_committee_disabled.go * Update mainnet_test.go * Update minimal.go * Update signature_test.go * Update gocast.go * Update cgo_symbolizer.go * Update validator.pb.gw.go * Update beacon_state.pb.gw.go * Update signature.go * Update public_key_test.go * Update minimal_test.go * Update checktags_test.go * Update bls_benchmark_test.go * Update public_key.go * Update secret_key.go * Update aliases.go * Update init.go * Update stub.go * Update journald_linux.go * Update attestation.pb.gw.go * Update config_utils_develop.go * Update stub.go * Update stub.go * Update beacon_block.pb.gw.go * Update validator.pb.gw.go * Update node.pb.gw.go * Update config_utils_prod.go * Update journald.go * Update beacon_block.pb.gw.go * Update beacon_chain.pb.gw.go * Update beacon_state.pb.gw.go * Update events.pb.gw.go * Update validator/keymanager/remote-web3signer/keymanager.go Co-authored-by: Preston Van Loon <preston@prysmaticlabs.com> * addressing comments from review * updating length of comment * Update validator/keymanager/remote-web3signer/keymanager.go Co-authored-by: Raul Jordan <raul@prysmaticlabs.com> * Update stub.go revert changes * Update validator/client/aggregate_test.go Co-authored-by: Raul Jordan <raul@prysmaticlabs.com> * addressing final comments * fixing gofmt Co-authored-by: Raul Jordan <raul@prysmaticlabs.com> Co-authored-by: Preston Van Loon <preston@prysmaticlabs.com>
292 lines
7.6 KiB
Go
292 lines
7.6 KiB
Go
package remote_web3signer
|
|
|
|
import (
|
|
"bytes"
|
|
"context"
|
|
"encoding/hex"
|
|
"encoding/json"
|
|
"fmt"
|
|
"io/ioutil"
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/ethereum/go-ethereum/common/hexutil"
|
|
"github.com/prysmaticlabs/prysm/crypto/bls"
|
|
"github.com/prysmaticlabs/prysm/encoding/bytesutil"
|
|
validatorpb "github.com/prysmaticlabs/prysm/proto/prysm/v1alpha1/validator-client"
|
|
"github.com/prysmaticlabs/prysm/testing/require"
|
|
v1 "github.com/prysmaticlabs/prysm/validator/keymanager/remote-web3signer/v1"
|
|
"github.com/stretchr/testify/assert"
|
|
)
|
|
|
|
type MockClient struct {
|
|
Signature string
|
|
PublicKeys []string
|
|
isThrowingError bool
|
|
}
|
|
|
|
func (mc *MockClient) Sign(_ context.Context, _ string, _ SignRequestJson) (bls.Signature, error) {
|
|
decoded, err := hexutil.Decode(mc.Signature)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return bls.SignatureFromBytes(decoded)
|
|
}
|
|
func (mc *MockClient) GetPublicKeys(_ context.Context, _ string) ([][48]byte, error) {
|
|
var keys [][48]byte
|
|
for _, pk := range mc.PublicKeys {
|
|
decoded, err := hex.DecodeString(strings.TrimPrefix(pk, "0x"))
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
keys = append(keys, bytesutil.ToBytes48(decoded))
|
|
}
|
|
if mc.isThrowingError {
|
|
return nil, fmt.Errorf("mock error")
|
|
}
|
|
return keys, nil
|
|
}
|
|
|
|
func TestKeymanager_Sign(t *testing.T) {
|
|
client := &MockClient{
|
|
Signature: "0xb3baa751d0a9132cfe93e4e3d5ff9075111100e3789dca219ade5a24d27e19d16b3353149da1833e9b691bb38634e8dc04469be7032132906c927d7e1a49b414730612877bc6b2810c8f202daf793d1ab0d6b5cb21d52f9e52e883859887a5d9",
|
|
}
|
|
ctx := context.Background()
|
|
root, err := hexutil.Decode("0x270d43e74ce340de4bca2b1936beca0f4f5408d9e78aec4850920baf659d5b69")
|
|
if err != nil {
|
|
fmt.Printf("error: %v", err)
|
|
}
|
|
config := &SetupConfig{
|
|
BaseEndpoint: "example.com",
|
|
GenesisValidatorsRoot: root,
|
|
PublicKeysURL: "example2.com/api/v1/eth2/publicKeys",
|
|
}
|
|
km, err := NewKeymanager(ctx, config)
|
|
if err != nil {
|
|
fmt.Printf("error: %v", err)
|
|
}
|
|
km.client = client
|
|
desiredSigBytes, err := hexutil.Decode(client.Signature)
|
|
if err != nil {
|
|
fmt.Printf("error: %v", err)
|
|
}
|
|
desiredSig, err := bls.SignatureFromBytes(desiredSigBytes)
|
|
if err != nil {
|
|
fmt.Printf("error: %v", err)
|
|
}
|
|
type args struct {
|
|
request *validatorpb.SignRequest
|
|
}
|
|
tests := []struct {
|
|
name string
|
|
args args
|
|
want bls.Signature
|
|
wantErr bool
|
|
}{
|
|
{
|
|
name: "AGGREGATION_SLOT",
|
|
args: args{
|
|
request: v1.GetMockSignRequest("AGGREGATION_SLOT"),
|
|
},
|
|
want: desiredSig,
|
|
wantErr: false,
|
|
},
|
|
{
|
|
name: "AGGREGATE_AND_PROOF",
|
|
args: args{
|
|
request: v1.GetMockSignRequest("AGGREGATE_AND_PROOF"),
|
|
},
|
|
want: desiredSig,
|
|
wantErr: false,
|
|
},
|
|
{
|
|
name: "ATTESTATION",
|
|
args: args{
|
|
request: v1.GetMockSignRequest("ATTESTATION"),
|
|
},
|
|
want: desiredSig,
|
|
wantErr: false,
|
|
},
|
|
{
|
|
name: "BLOCK",
|
|
args: args{
|
|
request: v1.GetMockSignRequest("BLOCK"),
|
|
},
|
|
want: desiredSig,
|
|
wantErr: false,
|
|
},
|
|
{
|
|
name: "BLOCK_V2",
|
|
args: args{
|
|
request: v1.GetMockSignRequest("BLOCK_V2"),
|
|
},
|
|
want: desiredSig,
|
|
wantErr: false,
|
|
},
|
|
{
|
|
name: "RANDAO_REVEAL",
|
|
args: args{
|
|
request: v1.GetMockSignRequest("RANDAO_REVEAL"),
|
|
},
|
|
want: desiredSig,
|
|
wantErr: false,
|
|
},
|
|
{
|
|
name: "SYNC_COMMITTEE_CONTRIBUTION_AND_PROOF",
|
|
args: args{
|
|
request: v1.GetMockSignRequest("SYNC_COMMITTEE_CONTRIBUTION_AND_PROOF"),
|
|
},
|
|
want: desiredSig,
|
|
wantErr: false,
|
|
},
|
|
{
|
|
name: "SYNC_COMMITTEE_MESSAGE",
|
|
args: args{
|
|
request: v1.GetMockSignRequest("SYNC_COMMITTEE_MESSAGE"),
|
|
},
|
|
want: desiredSig,
|
|
wantErr: false,
|
|
},
|
|
{
|
|
name: "SYNC_COMMITTEE_SELECTION_PROOF",
|
|
args: args{
|
|
request: v1.GetMockSignRequest("SYNC_COMMITTEE_SELECTION_PROOF"),
|
|
},
|
|
want: desiredSig,
|
|
wantErr: false,
|
|
},
|
|
{
|
|
name: "VOLUNTARY_EXIT",
|
|
args: args{
|
|
request: v1.GetMockSignRequest("VOLUNTARY_EXIT"),
|
|
},
|
|
want: desiredSig,
|
|
wantErr: false,
|
|
},
|
|
}
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
got, err := km.Sign(ctx, tt.args.request)
|
|
if (err != nil) != tt.wantErr {
|
|
t.Errorf("GetVoluntaryExitSignRequest() error = %v, wantErr %v", err, tt.wantErr)
|
|
return
|
|
}
|
|
require.DeepEqual(t, got, tt.want)
|
|
})
|
|
}
|
|
|
|
}
|
|
|
|
func TestKeymanager_FetchValidatingPublicKeys_HappyPath_WithKeyList(t *testing.T) {
|
|
ctx := context.Background()
|
|
decodedKey, err := hexutil.Decode("0xa2b5aaad9c6efefe7bb9b1243a043404f3362937cfb6b31833929833173f476630ea2cfeb0d9ddf15f97ca8685948820")
|
|
if err != nil {
|
|
fmt.Printf("error: %v", err)
|
|
}
|
|
keys := [][48]byte{
|
|
bytesutil.ToBytes48(decodedKey),
|
|
}
|
|
root, err := hexutil.Decode("0x270d43e74ce340de4bca2b1936beca0f4f5408d9e78aec4850920baf659d5b69")
|
|
if err != nil {
|
|
fmt.Printf("error: %v", err)
|
|
}
|
|
config := &SetupConfig{
|
|
BaseEndpoint: "example.com",
|
|
GenesisValidatorsRoot: root,
|
|
ProvidedPublicKeys: keys,
|
|
}
|
|
km, err := NewKeymanager(ctx, config)
|
|
if err != nil {
|
|
fmt.Printf("error: %v", err)
|
|
}
|
|
resp, err := km.FetchValidatingPublicKeys(ctx)
|
|
if err != nil {
|
|
fmt.Printf("error: %v", err)
|
|
}
|
|
assert.NotNil(t, resp)
|
|
assert.Nil(t, err)
|
|
assert.EqualValues(t, resp, keys)
|
|
}
|
|
|
|
func TestKeymanager_FetchValidatingPublicKeys_HappyPath_WithExternalURL(t *testing.T) {
|
|
ctx := context.Background()
|
|
client := &MockClient{
|
|
PublicKeys: []string{"0xa2b5aaad9c6efefe7bb9b1243a043404f3362937cfb6b31833929833173f476630ea2cfeb0d9ddf15f97ca8685948820"},
|
|
}
|
|
decodedKey, err := hexutil.Decode("0xa2b5aaad9c6efefe7bb9b1243a043404f3362937cfb6b31833929833173f476630ea2cfeb0d9ddf15f97ca8685948820")
|
|
if err != nil {
|
|
fmt.Printf("error: %v", err)
|
|
}
|
|
keys := [][48]byte{
|
|
bytesutil.ToBytes48(decodedKey),
|
|
}
|
|
root, err := hexutil.Decode("0x270d43e74ce340de4bca2b1936beca0f4f5408d9e78aec4850920baf659d5b69")
|
|
if err != nil {
|
|
fmt.Printf("error: %v", err)
|
|
}
|
|
config := &SetupConfig{
|
|
BaseEndpoint: "example.com",
|
|
GenesisValidatorsRoot: root,
|
|
PublicKeysURL: "example2.com/api/v1/eth2/publicKeys",
|
|
}
|
|
km, err := NewKeymanager(ctx, config)
|
|
if err != nil {
|
|
fmt.Printf("error: %v", err)
|
|
}
|
|
km.client = client
|
|
resp, err := km.FetchValidatingPublicKeys(ctx)
|
|
if err != nil {
|
|
fmt.Printf("error: %v", err)
|
|
}
|
|
assert.NotNil(t, resp)
|
|
assert.Nil(t, err)
|
|
assert.EqualValues(t, resp, keys)
|
|
}
|
|
|
|
func TestKeymanager_FetchValidatingPublicKeys_WithExternalURL_ThrowsError(t *testing.T) {
|
|
ctx := context.Background()
|
|
client := &MockClient{
|
|
PublicKeys: []string{"0xa2b5aaad9c6efefe7bb9b1243a043404f3362937cfb6b31833929833173f476630ea2cfeb0d9ddf15f97ca8685948820"},
|
|
isThrowingError: true,
|
|
}
|
|
root, err := hexutil.Decode("0x270d43e74ce340de4bca2b1936beca0f4f5408d9e78aec4850920baf659d5b69")
|
|
if err != nil {
|
|
fmt.Printf("error: %v", err)
|
|
}
|
|
config := &SetupConfig{
|
|
BaseEndpoint: "example.com",
|
|
GenesisValidatorsRoot: root,
|
|
PublicKeysURL: "example2.com/api/v1/eth2/publicKeys",
|
|
}
|
|
km, err := NewKeymanager(ctx, config)
|
|
if err != nil {
|
|
fmt.Printf("error: %v", err)
|
|
}
|
|
km.client = client
|
|
resp, err := km.FetchValidatingPublicKeys(ctx)
|
|
assert.NotNil(t, err)
|
|
assert.Nil(t, resp)
|
|
assert.Equal(t, fmt.Errorf("mock error"), err)
|
|
}
|
|
|
|
func TestUnmarshalConfigFile_HappyPath(t *testing.T) {
|
|
fakeConfig := struct {
|
|
BaseEndpoint string
|
|
GenesisValidatorsRoot []byte
|
|
PublicKeysURL string
|
|
ProvidedPublicKeys [][48]byte
|
|
}{}
|
|
fakeConfig.BaseEndpoint = "example.com"
|
|
fmt.Printf("%v", fakeConfig)
|
|
var buffer bytes.Buffer
|
|
b, err := json.Marshal(fakeConfig)
|
|
require.NoError(t, err)
|
|
_, err = buffer.Write(b)
|
|
require.NoError(t, err)
|
|
r := ioutil.NopCloser(&buffer)
|
|
|
|
config, err := UnmarshalConfigFile(r)
|
|
assert.NoError(t, err)
|
|
assert.Equal(t, fakeConfig.BaseEndpoint, config.BaseEndpoint)
|
|
}
|