prysm-pulse/validator/keymanager/remote-web3signer/v1/custom_mappers_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

604 lines
17 KiB
Go

package v1_test
import (
"reflect"
"testing"
"github.com/ethereum/go-ethereum/common/hexutil"
types "github.com/prysmaticlabs/eth2-types"
"github.com/prysmaticlabs/go-bitfield"
fieldparams "github.com/prysmaticlabs/prysm/config/fieldparams"
ethpb "github.com/prysmaticlabs/prysm/proto/prysm/v1alpha1"
v1 "github.com/prysmaticlabs/prysm/validator/keymanager/remote-web3signer/v1"
"github.com/prysmaticlabs/prysm/validator/keymanager/remote-web3signer/v1/mock"
)
func TestMapAggregateAndProof(t *testing.T) {
type args struct {
from *ethpb.AggregateAttestationAndProof
}
tests := []struct {
name string
args args
want *v1.AggregateAndProof
wantErr bool
}{
{
name: "HappyPathTest",
args: args{
from: &ethpb.AggregateAttestationAndProof{
AggregatorIndex: 0,
Aggregate: &ethpb.Attestation{
AggregationBits: bitfield.Bitlist{0b1101},
Data: &ethpb.AttestationData{
BeaconBlockRoot: make([]byte, fieldparams.RootLength),
Source: &ethpb.Checkpoint{
Root: make([]byte, fieldparams.RootLength),
},
Target: &ethpb.Checkpoint{
Root: make([]byte, fieldparams.RootLength),
},
},
Signature: make([]byte, 96),
},
SelectionProof: make([]byte, fieldparams.BLSSignatureLength),
},
},
want: &v1.AggregateAndProof{
AggregatorIndex: "0",
Aggregate: mock.MockAttestation(),
SelectionProof: hexutil.Encode(make([]byte, fieldparams.BLSSignatureLength)),
},
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := v1.MapAggregateAndProof(tt.args.from)
if (err != nil) != tt.wantErr {
t.Errorf("MapAggregateAndProof() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(got.Aggregate, tt.want.Aggregate) {
t.Errorf("MapAggregateAndProof() got = %v, want %v", got.Aggregate, tt.want.Aggregate)
}
})
}
}
func TestMapAttestation(t *testing.T) {
type args struct {
attestation *ethpb.Attestation
}
tests := []struct {
name string
args args
want *v1.Attestation
wantErr bool
}{
{
name: "HappyPathTest",
args: args{
attestation: &ethpb.Attestation{
AggregationBits: bitfield.Bitlist{0b1101},
Data: &ethpb.AttestationData{
BeaconBlockRoot: make([]byte, fieldparams.RootLength),
Source: &ethpb.Checkpoint{
Root: make([]byte, fieldparams.RootLength),
},
Target: &ethpb.Checkpoint{
Root: make([]byte, fieldparams.RootLength),
},
},
Signature: make([]byte, 96),
},
},
want: mock.MockAttestation(),
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := v1.MapAttestation(tt.args.attestation)
if (err != nil) != tt.wantErr {
t.Errorf("MapAttestation() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("MapAttestation() got = %v, want %v", got, tt.want)
}
})
}
}
func TestMapAttestationData(t *testing.T) {
type args struct {
data *ethpb.AttestationData
}
tests := []struct {
name string
args args
want *v1.AttestationData
wantErr bool
}{
{
name: "HappyPathTest",
args: args{
data: &ethpb.AttestationData{
BeaconBlockRoot: make([]byte, fieldparams.RootLength),
Source: &ethpb.Checkpoint{
Root: make([]byte, fieldparams.RootLength),
},
Target: &ethpb.Checkpoint{
Root: make([]byte, fieldparams.RootLength),
},
},
},
want: mock.MockAttestation().Data,
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := v1.MapAttestationData(tt.args.data)
if (err != nil) != tt.wantErr {
t.Errorf("MapAttestationData() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("MapAttestationData() got = %v, want %v", got, tt.want)
}
})
}
}
func TestMapAttesterSlashing(t *testing.T) {
type args struct {
slashing *ethpb.AttesterSlashing
}
tests := []struct {
name string
args args
want *v1.AttesterSlashing
wantErr bool
}{
{
name: "HappyPathTest",
args: args{
slashing: &ethpb.AttesterSlashing{
Attestation_1: &ethpb.IndexedAttestation{
AttestingIndices: []uint64{0, 1, 2},
Data: &ethpb.AttestationData{
BeaconBlockRoot: make([]byte, fieldparams.RootLength),
Source: &ethpb.Checkpoint{
Root: make([]byte, fieldparams.RootLength),
},
Target: &ethpb.Checkpoint{
Root: make([]byte, fieldparams.RootLength),
},
},
Signature: make([]byte, fieldparams.BLSSignatureLength),
},
Attestation_2: &ethpb.IndexedAttestation{
AttestingIndices: []uint64{0, 1, 2},
Data: &ethpb.AttestationData{
BeaconBlockRoot: make([]byte, fieldparams.RootLength),
Source: &ethpb.Checkpoint{
Root: make([]byte, fieldparams.RootLength),
},
Target: &ethpb.Checkpoint{
Root: make([]byte, fieldparams.RootLength),
},
},
Signature: make([]byte, fieldparams.BLSSignatureLength),
},
},
},
want: &v1.AttesterSlashing{
Attestation_1: mock.MockIndexedAttestation(),
Attestation_2: mock.MockIndexedAttestation(),
},
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := v1.MapAttesterSlashing(tt.args.slashing)
if (err != nil) != tt.wantErr {
t.Errorf("MapAttesterSlashing() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(got.Attestation_1, tt.want.Attestation_1) {
t.Errorf("MapAttesterSlashing() got = %v, want %v", got.Attestation_1, tt.want.Attestation_1)
}
})
}
}
func TestMapBeaconBlockAltair(t *testing.T) {
type args struct {
block *ethpb.BeaconBlockAltair
}
tests := []struct {
name string
args args
want *v1.BeaconBlockAltair
wantErr bool
}{
{
name: "Happy Path Test",
args: args{
block: &ethpb.BeaconBlockAltair{
Slot: 0,
ProposerIndex: 0,
ParentRoot: make([]byte, fieldparams.RootLength),
StateRoot: make([]byte, fieldparams.RootLength),
Body: &ethpb.BeaconBlockBodyAltair{
RandaoReveal: make([]byte, 32),
Eth1Data: &ethpb.Eth1Data{
DepositRoot: make([]byte, fieldparams.RootLength),
DepositCount: 0,
BlockHash: make([]byte, 32),
},
Graffiti: make([]byte, 32),
ProposerSlashings: []*ethpb.ProposerSlashing{
{
Header_1: &ethpb.SignedBeaconBlockHeader{
Header: &ethpb.BeaconBlockHeader{
Slot: 0,
ProposerIndex: 0,
ParentRoot: make([]byte, fieldparams.RootLength),
StateRoot: make([]byte, fieldparams.RootLength),
BodyRoot: make([]byte, fieldparams.RootLength),
},
Signature: make([]byte, fieldparams.BLSSignatureLength),
},
Header_2: &ethpb.SignedBeaconBlockHeader{
Header: &ethpb.BeaconBlockHeader{
Slot: 0,
ProposerIndex: 0,
ParentRoot: make([]byte, fieldparams.RootLength),
StateRoot: make([]byte, fieldparams.RootLength),
BodyRoot: make([]byte, fieldparams.RootLength),
},
Signature: make([]byte, fieldparams.BLSSignatureLength),
},
},
},
AttesterSlashings: []*ethpb.AttesterSlashing{
{
Attestation_1: &ethpb.IndexedAttestation{
AttestingIndices: []uint64{0, 1, 2},
Data: &ethpb.AttestationData{
BeaconBlockRoot: make([]byte, fieldparams.RootLength),
Source: &ethpb.Checkpoint{
Root: make([]byte, fieldparams.RootLength),
},
Target: &ethpb.Checkpoint{
Root: make([]byte, fieldparams.RootLength),
},
},
Signature: make([]byte, fieldparams.BLSSignatureLength),
},
Attestation_2: &ethpb.IndexedAttestation{
AttestingIndices: []uint64{0, 1, 2},
Data: &ethpb.AttestationData{
BeaconBlockRoot: make([]byte, fieldparams.RootLength),
Source: &ethpb.Checkpoint{
Root: make([]byte, fieldparams.RootLength),
},
Target: &ethpb.Checkpoint{
Root: make([]byte, fieldparams.RootLength),
},
},
Signature: make([]byte, fieldparams.BLSSignatureLength),
},
},
},
Attestations: []*ethpb.Attestation{
{
AggregationBits: bitfield.Bitlist{0b1101},
Data: &ethpb.AttestationData{
BeaconBlockRoot: make([]byte, fieldparams.RootLength),
Source: &ethpb.Checkpoint{
Root: make([]byte, fieldparams.RootLength),
},
Target: &ethpb.Checkpoint{
Root: make([]byte, fieldparams.RootLength),
},
},
Signature: make([]byte, 96),
},
},
Deposits: []*ethpb.Deposit{
{
Proof: [][]byte{[]byte("A")},
Data: &ethpb.Deposit_Data{
PublicKey: make([]byte, fieldparams.BLSPubkeyLength),
WithdrawalCredentials: make([]byte, 32),
Amount: 0,
Signature: make([]byte, fieldparams.BLSSignatureLength),
},
},
},
VoluntaryExits: []*ethpb.SignedVoluntaryExit{
{
Exit: &ethpb.VoluntaryExit{
Epoch: 0,
ValidatorIndex: 0,
},
Signature: make([]byte, fieldparams.BLSSignatureLength),
},
},
SyncAggregate: &ethpb.SyncAggregate{
SyncCommitteeSignature: make([]byte, fieldparams.BLSSignatureLength),
SyncCommitteeBits: mock.MockSyncComitteeBits(),
},
},
},
},
want: mock.MockBeaconBlockAltair(),
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := v1.MapBeaconBlockAltair(tt.args.block)
if (err != nil) != tt.wantErr {
t.Errorf("MapBeaconBlockAltair() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(got.Body, tt.want.Body) {
t.Errorf("MapBeaconBlockAltair() got = %v, want %v", got.Body.SyncAggregate, tt.want.Body.SyncAggregate)
}
})
}
}
func TestMapBeaconBlockBody(t *testing.T) {
type args struct {
body *ethpb.BeaconBlockBody
}
tests := []struct {
name string
args args
want *v1.BeaconBlockBody
wantErr bool
}{
{
name: "Happy Path Test",
args: args{
body: &ethpb.BeaconBlockBody{
RandaoReveal: make([]byte, 32),
Eth1Data: &ethpb.Eth1Data{
DepositRoot: make([]byte, fieldparams.RootLength),
DepositCount: 0,
BlockHash: make([]byte, 32),
},
Graffiti: make([]byte, 32),
ProposerSlashings: []*ethpb.ProposerSlashing{
{
Header_1: &ethpb.SignedBeaconBlockHeader{
Header: &ethpb.BeaconBlockHeader{
Slot: 0,
ProposerIndex: 0,
ParentRoot: make([]byte, fieldparams.RootLength),
StateRoot: make([]byte, fieldparams.RootLength),
BodyRoot: make([]byte, fieldparams.RootLength),
},
Signature: make([]byte, fieldparams.BLSSignatureLength),
},
Header_2: &ethpb.SignedBeaconBlockHeader{
Header: &ethpb.BeaconBlockHeader{
Slot: 0,
ProposerIndex: 0,
ParentRoot: make([]byte, fieldparams.RootLength),
StateRoot: make([]byte, fieldparams.RootLength),
BodyRoot: make([]byte, fieldparams.RootLength),
},
Signature: make([]byte, fieldparams.BLSSignatureLength),
},
},
},
AttesterSlashings: []*ethpb.AttesterSlashing{
{
Attestation_1: &ethpb.IndexedAttestation{
AttestingIndices: []uint64{0, 1, 2},
Data: &ethpb.AttestationData{
BeaconBlockRoot: make([]byte, fieldparams.RootLength),
Source: &ethpb.Checkpoint{
Root: make([]byte, fieldparams.RootLength),
},
Target: &ethpb.Checkpoint{
Root: make([]byte, fieldparams.RootLength),
},
},
Signature: make([]byte, fieldparams.BLSSignatureLength),
},
Attestation_2: &ethpb.IndexedAttestation{
AttestingIndices: []uint64{0, 1, 2},
Data: &ethpb.AttestationData{
BeaconBlockRoot: make([]byte, fieldparams.RootLength),
Source: &ethpb.Checkpoint{
Root: make([]byte, fieldparams.RootLength),
},
Target: &ethpb.Checkpoint{
Root: make([]byte, fieldparams.RootLength),
},
},
Signature: make([]byte, fieldparams.BLSSignatureLength),
},
},
},
Attestations: []*ethpb.Attestation{
{
AggregationBits: bitfield.Bitlist{0b1101},
Data: &ethpb.AttestationData{
BeaconBlockRoot: make([]byte, fieldparams.RootLength),
Source: &ethpb.Checkpoint{
Root: make([]byte, fieldparams.RootLength),
},
Target: &ethpb.Checkpoint{
Root: make([]byte, fieldparams.RootLength),
},
},
Signature: make([]byte, 96),
},
},
Deposits: []*ethpb.Deposit{
{
Proof: [][]byte{[]byte("A")},
Data: &ethpb.Deposit_Data{
PublicKey: make([]byte, fieldparams.BLSPubkeyLength),
WithdrawalCredentials: make([]byte, 32),
Amount: 0,
Signature: make([]byte, fieldparams.BLSSignatureLength),
},
},
},
VoluntaryExits: []*ethpb.SignedVoluntaryExit{
{
Exit: &ethpb.VoluntaryExit{
Epoch: 0,
ValidatorIndex: 0,
},
Signature: make([]byte, fieldparams.BLSSignatureLength),
},
},
},
},
want: mock.MockBeaconBlockBody(),
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := v1.MapBeaconBlockBody(tt.args.body)
if (err != nil) != tt.wantErr {
t.Errorf("MapBeaconBlockBody() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("MapBeaconBlockBody() got = %v, want %v", got, tt.want)
}
})
}
}
func TestMapContributionAndProof(t *testing.T) {
type args struct {
contribution *ethpb.ContributionAndProof
}
tests := []struct {
name string
args args
want *v1.ContributionAndProof
wantErr bool
}{
{
name: "Happy Path Test",
args: args{
contribution: &ethpb.ContributionAndProof{
AggregatorIndex: 0,
Contribution: &ethpb.SyncCommitteeContribution{
Slot: 0,
BlockRoot: make([]byte, fieldparams.RootLength),
SubcommitteeIndex: 0,
AggregationBits: mock.MockAggregationBits(),
Signature: make([]byte, fieldparams.BLSSignatureLength),
},
SelectionProof: make([]byte, fieldparams.BLSSignatureLength),
},
},
want: mock.MockContributionAndProof(),
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := v1.MapContributionAndProof(tt.args.contribution)
if (err != nil) != tt.wantErr {
t.Errorf("MapContributionAndProof() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("MapContributionAndProof() got = %v, want %v", got, tt.want)
}
})
}
}
func TestMapForkInfo(t *testing.T) {
type args struct {
slot types.Slot
genesisValidatorsRoot []byte
}
tests := []struct {
name string
args args
want *v1.ForkInfo
wantErr bool
}{
{
name: "Happy Path Test",
args: args{
slot: 0,
genesisValidatorsRoot: make([]byte, fieldparams.RootLength),
},
want: mock.MockForkInfo(),
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := v1.MapForkInfo(tt.args.slot, tt.args.genesisValidatorsRoot)
if (err != nil) != tt.wantErr {
t.Errorf("MapForkInfo() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("MapForkInfo() got = %v, want %v", got, tt.want)
}
})
}
}
func TestMapSyncAggregatorSelectionData(t *testing.T) {
type args struct {
data *ethpb.SyncAggregatorSelectionData
}
tests := []struct {
name string
args args
want *v1.SyncAggregatorSelectionData
wantErr bool
}{
{
name: "Happy Path Test",
args: args{
data: &ethpb.SyncAggregatorSelectionData{
Slot: 0,
SubcommitteeIndex: 0,
},
},
want: &v1.SyncAggregatorSelectionData{
Slot: "0",
SubcommitteeIndex: "0",
},
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := v1.MapSyncAggregatorSelectionData(tt.args.data)
if (err != nil) != tt.wantErr {
t.Errorf("MapSyncAggregatorSelectionData() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("MapSyncAggregatorSelectionData() got = %v, want %v", got, tt.want)
}
})
}
}