prysm-pulse/validator/keymanager/remote-web3signer/keymanager_test.go
james-prysm aed6e13498
Web3Signer: CLI implementation (#10056)
* initial commit for cli integration of web3signer

* resolving conflicts and execution

* remove aggregation slot from proto

* rem aggregation slot

* define a sync message block root struct

* fix sync message name

* sync message block root struct

* amend where sync committee block root is used

* altered switch statement to return correct json request by type

* fixing fork data import, types, and unit tests

* reverting unwanted changes

* reverting more unwanted changes

* fixing deepsource issues

* fixing formatting

* more fixes for deepsource and code clean up

* only want to fetch once for fetch validating public keys

* adding more comments

* new unit tests for requests and fixing a mapper issue

* Update validator/client/validator.go

Co-authored-by: Raul Jordan <raul@prysmaticlabs.com>

* Update validator/accounts/wallet/wallet.go

Co-authored-by: Raul Jordan <raul@prysmaticlabs.com>

* adjusting comment

* adjusting comment

* fixing import organization

* including more unit tests

* adding new cli edit

* adding in checks for wallet initialize

* adding web3signer flags to main.go

* some how resolved files did not save correctly

* adding in check to make sure web flag only works with types imported and derived

* Update validator/client/sync_committee.go

Co-authored-by: Raul Jordan <raul@prysmaticlabs.com>

* Update validator/client/aggregate.go

Co-authored-by: Raul Jordan <raul@prysmaticlabs.com>

* Update validator/accounts/wallet/wallet.go

Co-authored-by: Raul Jordan <raul@prysmaticlabs.com>

* Update cmd/validator/wallet/wallet.go

Co-authored-by: Raul Jordan <raul@prysmaticlabs.com>

* Update cmd/validator/wallet/wallet.go

Co-authored-by: Raul Jordan <raul@prysmaticlabs.com>

* Update cmd/validator/main.go

Co-authored-by: Raul Jordan <raul@prysmaticlabs.com>

* Update cmd/validator/flags/flags.go

Co-authored-by: Raul Jordan <raul@prysmaticlabs.com>

* Update cmd/validator/flags/flags.go

Co-authored-by: Raul Jordan <raul@prysmaticlabs.com>

* Update cmd/validator/wallet/wallet.go

Co-authored-by: Raul Jordan <raul@prysmaticlabs.com>

* Update cmd/validator/wallet/wallet.go

Co-authored-by: Raul Jordan <raul@prysmaticlabs.com>

* reverting changes that accidently got checked in

* reverting

* reverting

* continuing to revert unintenteded changes

* reverting

* removing more unneeded changes

* addressing review comment

* initial refactor

* adding in more clarifying comments

* fixing mock

* resolving desource issues

* addressing gosec scan for helper go file

* addressing gosec

* trying to fix bazel build

* removal of interface to fix build

* fixing maligned struct

* addressing deepsource

* fixing deepsource

* addressing efficiency of type checking

* fixing bazel test failure

* fixing go linter errors

* gaz

* web changes

* add w3signer

* new kind

* proper use

* align

* adding prysm validator flags to help flags list

* addressing root comment

* ci lint

* fixing standardapi tests

* fixing accounts_test after removal of keymanager from rpc server

* fixing more unit tests

* Update cmd/validator/flags/flags.go

Co-authored-by: Raul Jordan <raul@prysmaticlabs.com>

* Update cmd/validator/flags/flags.go

Co-authored-by: Raul Jordan <raul@prysmaticlabs.com>

* Update validator/client/service.go

Co-authored-by: Raul Jordan <raul@prysmaticlabs.com>

* Update validator/client/service.go

Co-authored-by: Raul Jordan <raul@prysmaticlabs.com>

* addressing missed err checks

* fixing mock tests

* fixing gofmt

* unskipping minimal e2e test and removing related TODOs

* Update testing/endtoend/components/validator.go

Co-authored-by: Preston Van Loon <preston@prysmaticlabs.com>

* Update testing/endtoend/components/validator.go

Co-authored-by: Preston Van Loon <preston@prysmaticlabs.com>

* adding some error wrapers to clarify failure point

* fixing bazel build with new error checks

* taking preston's advice to make test fail faster to understand what's going on with the test

* checking if genesis validators root is not zero hash

* adding check for genesis validators root giving zero hash

* fixing missing dependency

* adding check for wallet

* log all

* fixing errors for http responses

* switching marshal to pretty print

* adding pretty sign request test

* fixing base url setting

* adding in check for web3signer and temporary wallet instead of having to open the wallet

* refactoring web3signer to not require wallet

* bazel build fix

* fixing gazelle build

* adding content type of request

* fixing more bazel

* removing unused code

* removing unused comments

* adding skip test back in

* addressing a validation and error message

* fix parse

* body

* fixing logic for datadir

* improving error handling

* show resp

* fix

* sign resp as str

* point of pointer remove

* sign resp

* unmarshal sig resp

* read body as str

* adding more verbose logging

* removing unused result

* fixing unit test

* reconfiguring files to properly nest code and mocks

* fix build issue

* using context when using client function calls

* fixing based on suggestion

* addressing comments

* gaz

* removing defined max timeout

* reverting json print pretty

* Update validator/accounts/wallet_edit.go

Co-authored-by: Preston Van Loon <preston@prysmaticlabs.com>

* removing unneeded code restrictions

* should not introduce new code that may impact existing key manager types

* adjusting comments

* adding in json validation

* running go mod tidy

* some logging

* more logs

* fixing typo

* remove logs

* testing without byte trim

* fixing order or properties

* gaz

* tidy

* reverting some logs

* removing the confusing comments

* Update validator/client/aggregate.go

Co-authored-by: Raul Jordan <raul@prysmaticlabs.com>

* Update validator/client/aggregate.go

Co-authored-by: Raul Jordan <raul@prysmaticlabs.com>

* addressing pr comments

* editing bytes test

* Run gazelle update-repos

* run gazelle

* improving unit test coverage

* fixing text

* fixing a potential escaped error

Co-authored-by: Raul Jordan <raul@prysmaticlabs.com>
Co-authored-by: Preston Van Loon <preston@prysmaticlabs.com>
2022-01-31 10:44:17 -06:00

269 lines
7.1 KiB
Go

package remote_web3signer
import (
"context"
"encoding/hex"
"fmt"
"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"
"github.com/prysmaticlabs/prysm/validator/keymanager/remote-web3signer/internal"
"github.com/prysmaticlabs/prysm/validator/keymanager/remote-web3signer/v1/mock"
"github.com/stretchr/testify/assert"
)
type MockClient struct {
Signature string
PublicKeys []string
isThrowingError bool
}
func (mc *MockClient) Sign(_ context.Context, _ string, _ internal.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: mock.GetMockSignRequest("AGGREGATION_SLOT"),
},
want: desiredSig,
wantErr: false,
},
{
name: "AGGREGATE_AND_PROOF",
args: args{
request: mock.GetMockSignRequest("AGGREGATE_AND_PROOF"),
},
want: desiredSig,
wantErr: false,
},
{
name: "ATTESTATION",
args: args{
request: mock.GetMockSignRequest("ATTESTATION"),
},
want: desiredSig,
wantErr: false,
},
{
name: "BLOCK",
args: args{
request: mock.GetMockSignRequest("BLOCK"),
},
want: desiredSig,
wantErr: false,
},
{
name: "BLOCK_V2",
args: args{
request: mock.GetMockSignRequest("BLOCK_V2"),
},
want: desiredSig,
wantErr: false,
},
{
name: "RANDAO_REVEAL",
args: args{
request: mock.GetMockSignRequest("RANDAO_REVEAL"),
},
want: desiredSig,
wantErr: false,
},
{
name: "SYNC_COMMITTEE_CONTRIBUTION_AND_PROOF",
args: args{
request: mock.GetMockSignRequest("SYNC_COMMITTEE_CONTRIBUTION_AND_PROOF"),
},
want: desiredSig,
wantErr: false,
},
{
name: "SYNC_COMMITTEE_MESSAGE",
args: args{
request: mock.GetMockSignRequest("SYNC_COMMITTEE_MESSAGE"),
},
want: desiredSig,
wantErr: false,
},
{
name: "SYNC_COMMITTEE_SELECTION_PROOF",
args: args{
request: mock.GetMockSignRequest("SYNC_COMMITTEE_SELECTION_PROOF"),
},
want: desiredSig,
wantErr: false,
},
{
name: "VOLUNTARY_EXIT",
args: args{
request: mock.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)
}