From 952e9375e04cba86d53215540c3b281bb43409c9 Mon Sep 17 00:00:00 2001 From: James He Date: Fri, 7 Jan 2022 09:00:05 -0500 Subject: [PATCH 01/19] initial commit --- .../remote-web3signer/object_mapper.go | 1 + .../remote-web3signer/web3signer_types.go | 309 ++++++++++++++++++ 2 files changed, 310 insertions(+) create mode 100644 validator/keymanager/remote-web3signer/object_mapper.go create mode 100644 validator/keymanager/remote-web3signer/web3signer_types.go diff --git a/validator/keymanager/remote-web3signer/object_mapper.go b/validator/keymanager/remote-web3signer/object_mapper.go new file mode 100644 index 000000000000..f46d5c595c33 --- /dev/null +++ b/validator/keymanager/remote-web3signer/object_mapper.go @@ -0,0 +1 @@ +package remote_web3signer diff --git a/validator/keymanager/remote-web3signer/web3signer_types.go b/validator/keymanager/remote-web3signer/web3signer_types.go new file mode 100644 index 000000000000..a4bb8b63213b --- /dev/null +++ b/validator/keymanager/remote-web3signer/web3signer_types.go @@ -0,0 +1,309 @@ +package remote_web3signer + +import ( + ethpb "github.com/prysmaticlabs/prysm/proto/prysm/v1alpha1" +) + +/* Web3Signer Specs are found on the following link: https://consensys.github.io/web3signer/web3signer-eth2.html */ + +// AggregationSlotSignRequest is a request object for web3signer sign api. +type AggregationSlotSignRequest struct { + Type string `json:"type"` + ForkInfo *ForkInfo `json:"fork_info"` + SigningRoot string `json:"signingRoot"` + AggregationSlot *AggregationSlot `json:"aggregation_slot"` +} + +// AggregationSlotSignRequest is a request object for web3signer sign api. +type AggregateAndProofSignRequest struct { + Type string `json:"type"` + ForkInfo *ForkInfo `json:"fork_info"` + SigningRoot string `json:"signingRoot"` + AggregateAndProof *AggregateAndProof `json:"aggregation_slot"` +} + +// AttestationSignRequest is a request object for web3signer sign api. +type AttestationSignRequest struct { + Type string `json:"type"` + ForkInfo *ForkInfo `json:"fork_info"` + SigningRoot string `json:"signingRoot"` + Attestation *AttestationData `json:"attestation"` +} + +// BlockSignRequest is a request object for web3signer sign api. +type BlockSignRequest struct { + Type string `json:"type"` + ForkInfo *ForkInfo `json:"fork_info"` + SigningRoot string `json:"signingRoot"` + Block *ethpb.BeaconBlockBody `json:"block"` +} + +// BlockV2AltairSignRequest is a request object for web3signer sign api. +type BlockV2AltairSignRequest struct { + Type string `json:"type"` + ForkInfo *ForkInfo `json:"fork_info"` + SigningRoot string `json:"signingRoot"` + BeaconBlock *BeaconBlockAltairBlockV2 `json:"beacon_block"` +} + +// BlockV2SignRequest is a request object for web3signer sign api. +type BlockV2SignRequest struct { + Type string `json:"type"` + ForkInfo *ForkInfo `json:"fork_info"` + SigningRoot string `json:"signingRoot"` + BeaconBlock *BeaconBlockBlockV2 `json:"beacon_block"` +} + +// DepositSignRequest Not currently supported by Prysm. +// DepositSignRequest is a request object for web3signer sign api. +type DepositSignRequest struct { + Type string `json:"type"` + SigningRoot string `json:"signingRoot"` + Deposit *Deposit `json:"deposit"` +} + +// RandaoRevealSignRequest is a request object for web3signer sign api. +type RandaoRevealSignRequest struct { + Type string `json:"type"` + ForkInfo *ForkInfo `json:"fork_info"` + SigningRoot string `json:"signingRoot"` + RandaoReveal *RandaoReveal `json:"randao_reveal"` +} + +// VoluntaryExitSignRequest is a request object for web3signer sign api. +type VoluntaryExitSignRequest struct { + Type string `json:"type"` + ForkInfo *ForkInfo `json:"fork_info"` + SigningRoot string `json:"signingRoot"` + VoluntaryExit *VoluntaryExit `json:"voluntary_exit"` +} + +// SyncCommitteeMessageSignRequest is a request object for web3signer sign api. +type SyncCommitteeMessageSignRequest struct { + Type string `json:"type"` + ForkInfo *ForkInfo `json:"fork_info"` + SigningRoot string `json:"signingRoot"` + SyncCommittee *SyncCommitteeMessage `json:"sync_committee_message"` +} + +// SyncCommitteeSelectionProofSignRequest is a request object for web3signer sign api. +type SyncCommitteeSelectionProofSignRequest struct { + Type string `json:"type"` + ForkInfo *ForkInfo + SigningRoot string + SyncCommittee *SyncAggregatorSelectionData +} + +// SyncCommitteeContributionAndProofSignRequest is a request object for web3signer sign api. +type SyncCommitteeContributionAndProofSignRequest struct { + Type string `json:"type"` + ForkInfo *ForkInfo + SigningRoot string + SyncCommittee *ethpb.ContributionAndProof +} + +//////////////////////////////////////////////////////////////////////////////// +// sub properties of Sign Requests ///////////////////////////////////////////// +//////////////////////////////////////////////////////////////////////////////// + +// ForkInfo a sub property object of the Sign request +type ForkInfo struct { + Fork *Fork `json:"fork"` + GenesisValidatorsRoot string `json:"genesis_validators_root"` +} + +// Fork a sub property of ForkInfo. +type Fork struct { + PreviousVersion string `json:"previous_version"` + CurrentVersion string `json:"current_version"` + Epoch string `json:"epoch"` +} + +// AggregationSlot a sub property of AggregationSlotSignRequest. +type AggregationSlot struct { + Slot string `json:"slot"` +} + +// AggregateAndProof a sub property of AggregateAndProofSignRequest. +type AggregateAndProof struct { + AggregatorIndex string `json:"aggregator_index"` + Aggregate *Attestation `json:"aggregate"` + SelectionProof string `json:"selection_proof"` +} + +type Attestation struct { + AggregationBits string `json:"aggregation_bits"` + Data *AttestationData `json:"data"` + Signature string `json:"signature"` +} + +type AttestationData struct { + Slot string `json:"slot"` + // Prysm uses CommitteeIndex but web3signer uses index. + Index string `json:"index"` + BeaconBlockRoot string `json:"beacon_block_root"` + Source *Checkpoint `json:"source"` + Target *Checkpoint `json:"target"` +} + +type Checkpoint struct { + Epoch string `json:"epoch"` + Root string `json:"root"` +} + +type BeaconBlock struct { + Slot string `json:"slot"` + ProposerIndex string `json:"proposer_index"` + ParentRoot string `json:"parent_root"` + StateRoot string `json:"state_root"` + Body *BeaconBlockBody `json:"body"` +} + +type BeaconBlockBody struct { + RandaoReveal string `json:"randao_reveal"` + Eth1Data *Eth1Data `json:"eth1_data"` + Graffiti string `json:"graffiti"` + ProposerSlashings []*ProposerSlashing `json:"proposer_slashings"` + AttesterSlashings []*AttesterSlashing `json:"attester_slashings"` + Attestations []*Attestation `json:"attestations"` + Deposits []*Deposit `json:"deposits"` + VoluntaryExits []*SignedVoluntaryExit `json:"voluntary_exits"` +} + +type Eth1Data struct { + DepositRoot string `json:"deposit_root"` + DepositCount string `json:"deposit_count"` /* uint64 */ + BlockHash string `json:"block_hash"` +} + +type ProposerSlashing struct { + // Prysm uses Header_1 but web3signer uses signed_header_1. + SignedHeader_1 *SignedBeaconBlockHeader `json:"signed_header_1"` + // Prysm uses Header_2 but web3signer uses signed_header_2. + SignedHeader_2 *SignedBeaconBlockHeader `json:"signed_header_2"` +} + +type SignedBeaconBlockHeader struct { + Message *BeaconBlockHeader + Signature string `json:"signature"` +} + +type BeaconBlockHeader struct { + Slot string `json:"slot"` /* uint64 */ + ProposerIndex string `json:"proposer_index"` /* uint64 */ + ParentRoot string `json:"parent_root"` /* Hash32 */ + StateRoot string `json:"state_root"` /* Hash32 */ + BodyRoot string `json:"body_root"` /* Hash32 */ +} + +type AttesterSlashing struct { + Attestation_1 *IndexedAttestation `json:"attestation_1"` + Attestation_2 *IndexedAttestation `json:"attestation_2"` +} + +type IndexedAttestation struct { + AttestingIndices []string `json:"attesting_indices"` /* uint64[] */ + Data *AttestationData `json:"data"` + Signature string `json:"signature"` +} + +type Deposit struct { + Proof []string `json:"proof"` + Data *DepositData `json:"data"` +} + +// Prysm uses Deposit_data instead of DepositData which is inconsistent naming +type DepositData struct { + PublicKey string `json:"public_key"` + WithdrawalCredentials string `json:"withdrawal_credentials"` + Amount string `json:"amount"` /* uint64 */ + Signature string `json:"signature"` +} + +type SignedVoluntaryExit struct { + // Prysm uses Exit instead of Message + Message *VoluntaryExit `json:"message"` + Signature string `json:"signature"` +} + +type VoluntaryExit struct { + Epoch string `json:"epoch"` /* uint64 */ + ValidatorIndex string `json:"validator_index"` /* uint64 */ +} + +// BeaconBlockAltairBlockV2 a sub property of BlockV2AltairSignRequest. +type BeaconBlockAltairBlockV2 struct { + Version string `json:"version"` + Block *ethpb.BeaconBlockAltair `json:"beacon_block"` +} + +type BeaconBlockAltair struct { + Slot string `json:"slot"` + ProposerIndex string `json:"proposer_index"` + ParentRoot string `json:"parent_root"` + StateRoot string `json:"state_root"` + Body *BeaconBlockBodyAltair `json:"body"` +} + +type BeaconBlockBodyAltair struct { + RandaoReveal string `json:"randao_reveal"` + Eth1Data *Eth1Data `json:"eth1_data"` + Graffiti string `json:"graffiti"` /* Hash32 */ + ProposerSlashings []*ProposerSlashing `json:"proposer_slashings"` + AttesterSlashings []*AttesterSlashing `json:"attester_slashings"` + Attestations []*Attestation `json:"attestations"` + Deposits []*Deposit `json:"deposits"` + VoluntaryExits []*SignedVoluntaryExit `json:"voluntary_exits"` + SyncAggregate *SyncAggregate `json:"sync_aggregate"` +} + +type SyncAggregate struct { + SyncCommitteeBits string `json:"sync_committee_bits"` /* SSZ hexadecimal string */ + SyncCommitteeSignature string `json:"sync_committee_signature"` /* 96 byte hexadecimal string */ +} + +// BeaconBlockBlockV2 a sub property of BlockV2SignRequest. +type BeaconBlockBlockV2 struct { + Version string `json:"version"` + Block *BeaconBlock `json:"beacon_block"` +} + +// RandaoReveal a sub property of RandaoRevealSignRequest. +type RandaoReveal struct { + Epoch string `json:"epoch"` /* uint64 */ +} + +type SyncCommitteeMessage struct { + BeaconBlockRoot string `json:"beacon_block_root"` /* Hash32 */ + Slot string `json:"slot"` /* uint64 */ + // Prysm uses v2/syncommittee and has the following extra properties : ValidatorIndex, Signature +} + +type SyncAggregatorSelectionData struct { + Slot string `json:"slot"` /* uint64 */ + SubcommitteeIndex string `json:"subcommittee_index"` /* uint64 */ +} + +type ContributionAndProof struct { + AggregatorIndex string `json:"aggregator_index"` /* uint64 */ + SelectionProof string `json:"selection_proof"` /* 96 byte hexadecimal */ + Contribution *SyncCommitteeContribution +} + +type SyncCommitteeContribution struct { + Slot string `json:"slot"` /* uint64 */ + // Prysm uses v2/validator + BeaconBlockRoot string `json:"block_root"` /* Hash32 */ + SubcommitteeIndex string `json:"subcommittee_index"` /* uint64 */ + AggregationBits string `json:"aggregation_bits"` /* SSZ hexadecimal string */ + Signature string `json:"signature"` /* 96 byte hexadecimal string */ +} + +//////////////////////////////////////////////////////////////////////////////// +//////////////////////////////////////////////////////////////////////////////// +//////////////////////////////////////////////////////////////////////////////// + +// signResponse the response object of the web3signer sign api. +type signResponse struct { + Signature string `json:"signature"` +} From 33c2a262311a81e9827d6df87ae881f07be489a2 Mon Sep 17 00:00:00 2001 From: James He Date: Fri, 7 Jan 2022 09:16:31 -0500 Subject: [PATCH 02/19] fixing some types and renaming file --- .../keymanager/remote-web3signer/client.go | 26 ++------------- .../{object_mapper.go => custom_mappers.go} | 0 .../remote-web3signer/web3signer_types.go | 32 ++++++++----------- 3 files changed, 16 insertions(+), 42 deletions(-) rename validator/keymanager/remote-web3signer/{object_mapper.go => custom_mappers.go} (100%) diff --git a/validator/keymanager/remote-web3signer/client.go b/validator/keymanager/remote-web3signer/client.go index b09561a7c8d2..1a367eb9126f 100644 --- a/validator/keymanager/remote-web3signer/client.go +++ b/validator/keymanager/remote-web3signer/client.go @@ -47,7 +47,8 @@ func newApiClient(baseEndpoint string) (*apiClient, error) { }, nil } -// SignRequest is a request object for web3signer sign api. +// TODO: will be removed and replaced in a future PR SignRequest is a request object for web3signer sign api. + type SignRequest struct { Type string `json:"type"` ForkInfo *ForkInfo `json:"fork_info"` @@ -55,29 +56,6 @@ type SignRequest struct { AggregationSlot *AggregationSlot `json:"aggregation_slot"` } -// ForkInfo a sub property object of the Sign request,in the future before the merge to remove the need to send the entire block body and just use the block_body_root. -type ForkInfo struct { - Fork *Fork `json:"fork"` - GenesisValidatorsRoot string `json:"genesis_validators_root"` -} - -// Fork a sub property of ForkInfo. -type Fork struct { - PreviousVersion string `json:"previous_version"` - CurrentVersion string `json:"current_version"` - Epoch string `json:"epoch"` -} - -// AggregationSlot a sub property of SignRequest. -type AggregationSlot struct { - Slot string `json:"slot"` -} - -// signResponse the response object of the web3signer sign api. -type signResponse struct { - Signature string `json:"signature"` -} - // Sign is a wrapper method around the web3signer sign api. func (client *apiClient) Sign(_ context.Context, pubKey string, request *SignRequest) (bls.Signature, error) { requestPath := ethApiNamespace + pubKey diff --git a/validator/keymanager/remote-web3signer/object_mapper.go b/validator/keymanager/remote-web3signer/custom_mappers.go similarity index 100% rename from validator/keymanager/remote-web3signer/object_mapper.go rename to validator/keymanager/remote-web3signer/custom_mappers.go diff --git a/validator/keymanager/remote-web3signer/web3signer_types.go b/validator/keymanager/remote-web3signer/web3signer_types.go index a4bb8b63213b..5dbe5b749bd4 100644 --- a/validator/keymanager/remote-web3signer/web3signer_types.go +++ b/validator/keymanager/remote-web3signer/web3signer_types.go @@ -1,9 +1,5 @@ package remote_web3signer -import ( - ethpb "github.com/prysmaticlabs/prysm/proto/prysm/v1alpha1" -) - /* Web3Signer Specs are found on the following link: https://consensys.github.io/web3signer/web3signer-eth2.html */ // AggregationSlotSignRequest is a request object for web3signer sign api. @@ -32,10 +28,10 @@ type AttestationSignRequest struct { // BlockSignRequest is a request object for web3signer sign api. type BlockSignRequest struct { - Type string `json:"type"` - ForkInfo *ForkInfo `json:"fork_info"` - SigningRoot string `json:"signingRoot"` - Block *ethpb.BeaconBlockBody `json:"block"` + Type string `json:"type"` + ForkInfo *ForkInfo `json:"fork_info"` + SigningRoot string `json:"signingRoot"` + Block *BeaconBlockBody `json:"block"` } // BlockV2AltairSignRequest is a request object for web3signer sign api. @@ -88,18 +84,18 @@ type SyncCommitteeMessageSignRequest struct { // SyncCommitteeSelectionProofSignRequest is a request object for web3signer sign api. type SyncCommitteeSelectionProofSignRequest struct { - Type string `json:"type"` - ForkInfo *ForkInfo - SigningRoot string - SyncCommittee *SyncAggregatorSelectionData + Type string `json:"type"` + ForkInfo *ForkInfo `json:"fork_info"` + SigningRoot string `json:"signingRoot"` + SyncCommittee *SyncAggregatorSelectionData `json:"sync_committee_selection_proof"` } // SyncCommitteeContributionAndProofSignRequest is a request object for web3signer sign api. type SyncCommitteeContributionAndProofSignRequest struct { - Type string `json:"type"` - ForkInfo *ForkInfo - SigningRoot string - SyncCommittee *ethpb.ContributionAndProof + Type string `json:"type"` + ForkInfo *ForkInfo `json:"fork_info"` + SigningRoot string `json:"signingRoot"` + SyncCommittee *ContributionAndProof `json:"sync_committee_contribution_and_proof"` } //////////////////////////////////////////////////////////////////////////////// @@ -233,8 +229,8 @@ type VoluntaryExit struct { // BeaconBlockAltairBlockV2 a sub property of BlockV2AltairSignRequest. type BeaconBlockAltairBlockV2 struct { - Version string `json:"version"` - Block *ethpb.BeaconBlockAltair `json:"beacon_block"` + Version string `json:"version"` + Block *BeaconBlockAltair `json:"beacon_block"` } type BeaconBlockAltair struct { From 95f199309e9eb8eb467615fd2b5fbf47e731ff8d Mon Sep 17 00:00:00 2001 From: James He Date: Fri, 7 Jan 2022 15:41:17 -0500 Subject: [PATCH 03/19] initial map function commit and moving to a v1 folder --- .../keymanager/remote-web3signer/README.md | 3 + .../keymanager/remote-web3signer/client.go | 12 +- .../remote-web3signer/client_test.go | 8 +- .../remote-web3signer/custom_mappers.go | 1 - .../remote-web3signer/keymanager.go | 8 +- .../remote-web3signer/v1/custom_mappers.go | 266 ++++++++++++++++++ .../{ => v1}/web3signer_types.go | 7 +- 7 files changed, 290 insertions(+), 15 deletions(-) create mode 100644 validator/keymanager/remote-web3signer/README.md delete mode 100644 validator/keymanager/remote-web3signer/custom_mappers.go create mode 100644 validator/keymanager/remote-web3signer/v1/custom_mappers.go rename validator/keymanager/remote-web3signer/{ => v1}/web3signer_types.go (98%) diff --git a/validator/keymanager/remote-web3signer/README.md b/validator/keymanager/remote-web3signer/README.md new file mode 100644 index 000000000000..e7db92afdd82 --- /dev/null +++ b/validator/keymanager/remote-web3signer/README.md @@ -0,0 +1,3 @@ +# Web3Signer + +WIP \ No newline at end of file diff --git a/validator/keymanager/remote-web3signer/client.go b/validator/keymanager/remote-web3signer/client.go index 1a367eb9126f..737dd467763e 100644 --- a/validator/keymanager/remote-web3signer/client.go +++ b/validator/keymanager/remote-web3signer/client.go @@ -10,6 +10,8 @@ import ( "net/url" "time" + v1 "github.com/prysmaticlabs/prysm/validator/keymanager/remote-web3signer/v1" + "github.com/ethereum/go-ethereum/common/hexutil" "github.com/pkg/errors" "github.com/prysmaticlabs/prysm/crypto/bls" @@ -50,10 +52,10 @@ func newApiClient(baseEndpoint string) (*apiClient, error) { // TODO: will be removed and replaced in a future PR SignRequest is a request object for web3signer sign api. type SignRequest struct { - Type string `json:"type"` - ForkInfo *ForkInfo `json:"fork_info"` - SigningRoot string `json:"signingRoot"` - AggregationSlot *AggregationSlot `json:"aggregation_slot"` + Type string `json:"type"` + ForkInfo *v1.ForkInfo `json:"fork_info"` + SigningRoot string `json:"signingRoot"` + AggregationSlot *v1.AggregationSlot `json:"aggregation_slot"` } // Sign is a wrapper method around the web3signer sign api. @@ -73,7 +75,7 @@ func (client *apiClient) Sign(_ context.Context, pubKey string, request *SignReq if resp.StatusCode == 412 { return nil, errors.Wrap(err, "signing operation failed due to slashing protection rules") } - signResp := &signResponse{} + signResp := &v1.SignResponse{} if err := client.unmarshalResponse(resp.Body, &signResp); err != nil { return nil, err } diff --git a/validator/keymanager/remote-web3signer/client_test.go b/validator/keymanager/remote-web3signer/client_test.go index 03e3c5cdd1c0..9f3c78a4ed51 100644 --- a/validator/keymanager/remote-web3signer/client_test.go +++ b/validator/keymanager/remote-web3signer/client_test.go @@ -8,6 +8,8 @@ import ( "net/http" "testing" + v1 "github.com/prysmaticlabs/prysm/validator/keymanager/remote-web3signer/v1" + "github.com/stretchr/testify/assert" ) @@ -22,17 +24,17 @@ func (m *mockTransport) RoundTrip(*http.Request) (*http.Response, error) { } func getClientMockSignRequest() *SignRequest { - forkData := &Fork{ + forkData := &v1.Fork{ PreviousVersion: "", CurrentVersion: "", Epoch: "", } - forkInfoData := &ForkInfo{ + forkInfoData := &v1.ForkInfo{ Fork: forkData, GenesisValidatorsRoot: "", } - AggregationSlotData := &AggregationSlot{Slot: ""} + AggregationSlotData := &v1.AggregationSlot{Slot: ""} // remember to replace signing root with hex encoding remove 0x web3SignerRequest := SignRequest{ Type: "foo", diff --git a/validator/keymanager/remote-web3signer/custom_mappers.go b/validator/keymanager/remote-web3signer/custom_mappers.go deleted file mode 100644 index f46d5c595c33..000000000000 --- a/validator/keymanager/remote-web3signer/custom_mappers.go +++ /dev/null @@ -1 +0,0 @@ -package remote_web3signer diff --git a/validator/keymanager/remote-web3signer/keymanager.go b/validator/keymanager/remote-web3signer/keymanager.go index 5b806a3d2e1f..b596eb2ad227 100644 --- a/validator/keymanager/remote-web3signer/keymanager.go +++ b/validator/keymanager/remote-web3signer/keymanager.go @@ -4,6 +4,8 @@ import ( "context" "fmt" + v1 "github.com/prysmaticlabs/prysm/validator/keymanager/remote-web3signer/v1" + "github.com/ethereum/go-ethereum/common/hexutil" "github.com/pkg/errors" "github.com/prysmaticlabs/prysm/async/event" @@ -85,16 +87,16 @@ func (km *Keymanager) Sign(ctx context.Context, request *validatorpb.SignRequest return nil, err } - forkData := &Fork{ + forkData := &v1.Fork{ PreviousVersion: hexutil.Encode(request.Fork.PreviousVersion), CurrentVersion: hexutil.Encode(request.Fork.CurrentVersion), Epoch: fmt.Sprint(request.Fork.Epoch), } - forkInfoData := &ForkInfo{ + forkInfoData := &v1.ForkInfo{ Fork: forkData, GenesisValidatorsRoot: hexutil.Encode(km.genesisValidatorsRoot), } - aggregationSlotData := &AggregationSlot{Slot: fmt.Sprint(request.AggregationSlot)} + aggregationSlotData := &v1.AggregationSlot{Slot: fmt.Sprint(request.AggregationSlot)} web3SignerRequest := SignRequest{ Type: signRequestType, ForkInfo: forkInfoData, diff --git a/validator/keymanager/remote-web3signer/v1/custom_mappers.go b/validator/keymanager/remote-web3signer/v1/custom_mappers.go new file mode 100644 index 000000000000..0ea096c9eb66 --- /dev/null +++ b/validator/keymanager/remote-web3signer/v1/custom_mappers.go @@ -0,0 +1,266 @@ +package v1 + +import ( + "fmt" + + "github.com/ethereum/go-ethereum/common/hexutil" + ethpb "github.com/prysmaticlabs/prysm/proto/prysm/v1alpha1" +) + +// All Mappings represent version 1.0 of the Web3Signer specs i.e. /api/v1/eth2 + +// MapForkInfo maps the eth2.ForkInfo proto to the Web3Signer spec. +func MapForkInfo(from *ethpb.Fork, genesisValidatorsRoot []byte) (*ForkInfo, error) { + if from == nil { + return nil, fmt.Errorf("fork info is nil") + } + forkData := &Fork{ + PreviousVersion: hexutil.Encode(from.PreviousVersion), + CurrentVersion: hexutil.Encode(from.CurrentVersion), + Epoch: fmt.Sprint(from.Epoch), + } + return &ForkInfo{ + Fork: forkData, + GenesisValidatorsRoot: hexutil.Encode(genesisValidatorsRoot), + }, nil +} + +// MapAggregateAndProof maps the eth2.AggregateAndProof proto to the Web3Signer spec. +func MapAggregateAndProof(from *ethpb.AggregateAttestationAndProof) (*AggregateAndProof, error) { + if from == nil { + return nil, fmt.Errorf("AggregateAttestationAndProof is nil") + } + aggregate, err := MapAttestation(from.Aggregate) + if err != nil { + return nil, err + } + return &AggregateAndProof{ + AggregatorIndex: fmt.Sprint(from.AggregatorIndex), + Aggregate: aggregate, + SelectionProof: hexutil.Encode(from.SelectionProof), + }, nil +} + +// MapAttestation maps the eth2.Attestation proto to the Web3Signer spec. +func MapAttestation(attestation *ethpb.Attestation) (*Attestation, error) { + if attestation == nil { + return nil, fmt.Errorf("attestation is nil") + } + data, err := MapAttestationData(attestation.Data) + if err != nil { + return nil, err + } + return &Attestation{ + Data: data, + AggregationBits: hexutil.Encode(attestation.AggregationBits), + Signature: hexutil.Encode(attestation.Signature), + }, nil +} + +// MapAttestationData maps the eth2.AttestationData proto to the Web3Signer spec. +func MapAttestationData(data *ethpb.AttestationData) (*AttestationData, error) { + if data == nil { + return nil, fmt.Errorf("attestation data is nil") + } + source, err := MapCheckPoint(data.Source) + if err != nil { + return nil, err + } + target, err := MapCheckPoint(data.Target) + if err != nil { + return nil, err + } + return &AttestationData{ + Slot: fmt.Sprint(data.Slot), + Index: fmt.Sprint(data.CommitteeIndex), + BeaconBlockRoot: hexutil.Encode(data.BeaconBlockRoot), + Source: source, + Target: target, + }, nil +} + +// MapCheckPoint maps the eth2.Checkpoint proto to the Web3Signer spec. +func MapCheckPoint(checkpoint *ethpb.Checkpoint) (*Checkpoint, error) { + if checkpoint == nil { + return nil, fmt.Errorf("checkpoint is nil") + } + return &Checkpoint{ + Epoch: fmt.Sprint(checkpoint.Epoch), + Root: hexutil.Encode(checkpoint.Root), + }, nil +} + +// MapBeaconBlockBody maps the eth2.BeaconBlockBody proto to the Web3Signer spec. +func MapBeaconBlockBody(body *ethpb.BeaconBlockBody) (*BeaconBlockBody, error) { + if body == nil { + return nil, fmt.Errorf("beacon block body is nil") + } + block := &BeaconBlockBody{ + RandaoReveal: hexutil.Encode(body.RandaoReveal), + Eth1Data: &Eth1Data{ + DepositRoot: hexutil.Encode(body.Eth1Data.DepositRoot), + BlockHash: hexutil.Encode(body.Eth1Data.BlockHash), + }, + Graffiti: hexutil.Encode(body.Graffiti), + ProposerSlashings: make([]*ProposerSlashing, len(body.ProposerSlashings)), + AttesterSlashings: make([]*AttesterSlashing, len(body.AttesterSlashings)), + Attestations: make([]*Attestation, len(body.Attestations)), + Deposits: make([]*Deposit, len(body.Deposits)), + VoluntaryExits: make([]*SignedVoluntaryExit, len(body.VoluntaryExits)), + } + for i, slashing := range body.ProposerSlashings { + slashing, err := MapProposerSlashing(slashing) + if err != nil { + return nil, err + } + block.ProposerSlashings[i] = slashing + } + for i, slashing := range body.AttesterSlashings { + slashing, err := MapAttesterSlashing(slashing) + if err != nil { + return nil, err + } + block.AttesterSlashings[i] = slashing + } + for i, attestation := range body.Attestations { + attestation, err := MapAttestation(attestation) + if err != nil { + return nil, err + } + block.Attestations[i] = attestation + } + for i, Deposit := range body.Deposits { + deposit, err := MapDeposit(Deposit) + if err != nil { + return nil, err + } + block.Deposits[i] = deposit + } + for i, signedVoluntaryExit := range body.VoluntaryExits { + signedVoluntaryExit, err := MapSignedVoluntaryExit(signedVoluntaryExit) + if err != nil { + return nil, err + } + block.VoluntaryExits[i] = signedVoluntaryExit + } + return block, nil +} + +// MapProposerSlashing maps the eth2.ProposerSlashing proto to the Web3Signer spec. +func MapProposerSlashing(slashing *ethpb.ProposerSlashing) (*ProposerSlashing, error) { + if slashing == nil { + return nil, fmt.Errorf("proposer slashing is nil") + } + signedHeader1, err := MapSignedBeaconBlockHeader(slashing.Header_1) + if err != nil { + return nil, err + } + signedHeader2, err := MapSignedBeaconBlockHeader(slashing.Header_2) + if err != nil { + return nil, err + } + return &ProposerSlashing{ + SignedHeader_1: signedHeader1, + SignedHeader_2: signedHeader2, + }, nil +} + +func MapSignedBeaconBlockHeader(signedHeader *ethpb.SignedBeaconBlockHeader) (*SignedBeaconBlockHeader, error) { + if signedHeader == nil { + return nil, fmt.Errorf("signed beacon block header is nil") + } + return &SignedBeaconBlockHeader{ + Message: &BeaconBlockHeader{ + Slot: fmt.Sprint(signedHeader.Header.Slot), + ProposerIndex: fmt.Sprint(signedHeader.Header.ProposerIndex), + ParentRoot: hexutil.Encode( + signedHeader.Header.ParentRoot, + ), + StateRoot: hexutil.Encode( + signedHeader.Header.StateRoot, + ), + BodyRoot: hexutil.Encode( + signedHeader.Header.BodyRoot, + ), + }, + Signature: hexutil.Encode( + signedHeader.Signature, + ), + }, nil +} + +func MapAttesterSlashing(slashing *ethpb.AttesterSlashing) (*AttesterSlashing, error) { + if slashing == nil { + return nil, fmt.Errorf("attester slashing is nil") + } + attestation1, err := MapIndexedAttestation(slashing.Attestation_1) + if err != nil { + return nil, err + } + attestation2, err := MapIndexedAttestation(slashing.Attestation_2) + if err != nil { + return nil, err + } + return &AttesterSlashing{ + Attestation_1: attestation1, + Attestation_2: attestation2, + }, nil +} + +func MapIndexedAttestation(attestation *ethpb.IndexedAttestation) (*IndexedAttestation, error) { + if attestation == nil { + return nil, fmt.Errorf("indexed attestation is nil") + } + attestingIndices := make([]string, len(attestation.AttestingIndices)) + for i, indices := range attestation.AttestingIndices { + attestingIndices[i] = fmt.Sprint(indices) + } + attestationData, err := MapAttestationData(attestation.Data) + if err != nil { + return nil, err + } + return &IndexedAttestation{ + AttestingIndices: attestingIndices, + Data: attestationData, + }, nil +} + +func MapDeposit(deposit *ethpb.Deposit) (*Deposit, error) { + if deposit == nil { + return nil, fmt.Errorf("deposit is nil") + } + proof := make([]string, len(deposit.Proof)) + for i, p := range proof { + proof[i] = fmt.Sprint(p) + } + return &Deposit{ + Proof: proof, + Data: &DepositData{ + PublicKey: hexutil.Encode( + deposit.Data.PublicKey, + ), + WithdrawalCredentials: hexutil.Encode( + deposit.Data.WithdrawalCredentials, + ), + Amount: fmt.Sprint(deposit.Data.Amount), + Signature: hexutil.Encode( + deposit.Data.Signature, + ), + }, + }, nil +} + +func MapSignedVoluntaryExit(signedVoluntaryExit *ethpb.SignedVoluntaryExit) (*SignedVoluntaryExit, error) { + if signedVoluntaryExit == nil { + return nil, fmt.Errorf("signed voluntary exit is nil") + } + return &SignedVoluntaryExit{ + Message: &VoluntaryExit{ + Epoch: fmt.Sprint(signedVoluntaryExit.Exit.Epoch), + ValidatorIndex: fmt.Sprint(signedVoluntaryExit.Exit.ValidatorIndex), + }, + Signature: hexutil.Encode( + signedVoluntaryExit.Signature, + ), + }, nil +} diff --git a/validator/keymanager/remote-web3signer/web3signer_types.go b/validator/keymanager/remote-web3signer/v1/web3signer_types.go similarity index 98% rename from validator/keymanager/remote-web3signer/web3signer_types.go rename to validator/keymanager/remote-web3signer/v1/web3signer_types.go index 5dbe5b749bd4..1a0a1e252062 100644 --- a/validator/keymanager/remote-web3signer/web3signer_types.go +++ b/validator/keymanager/remote-web3signer/v1/web3signer_types.go @@ -1,6 +1,7 @@ -package remote_web3signer +package v1 /* Web3Signer Specs are found on the following link: https://consensys.github.io/web3signer/web3signer-eth2.html */ +// All Types represent version 1.0 of the Web3Signer specs i.e. /api/v1/eth2 // AggregationSlotSignRequest is a request object for web3signer sign api. type AggregationSlotSignRequest struct { @@ -299,7 +300,7 @@ type SyncCommitteeContribution struct { //////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////// -// signResponse the response object of the web3signer sign api. -type signResponse struct { +// SignResponse the response object of the web3signer sign api. +type SignResponse struct { Signature string `json:"signature"` } From 98615a005ed2d46d4b2efe0907771802c90a2d5f Mon Sep 17 00:00:00 2001 From: James He Date: Mon, 10 Jan 2022 10:05:42 -0500 Subject: [PATCH 04/19] adding in remaining mapper functions and comments for types and initial commit on unit tests --- .../remote-web3signer/v1/custom_mappers.go | 128 +++++ .../v1/custom_mappers_test.go | 451 ++++++++++++++++++ .../remote-web3signer/v1/web3signer_types.go | 28 +- 3 files changed, 604 insertions(+), 3 deletions(-) create mode 100644 validator/keymanager/remote-web3signer/v1/custom_mappers_test.go diff --git a/validator/keymanager/remote-web3signer/v1/custom_mappers.go b/validator/keymanager/remote-web3signer/v1/custom_mappers.go index 0ea096c9eb66..ae6fc4cd9a37 100644 --- a/validator/keymanager/remote-web3signer/v1/custom_mappers.go +++ b/validator/keymanager/remote-web3signer/v1/custom_mappers.go @@ -165,6 +165,7 @@ func MapProposerSlashing(slashing *ethpb.ProposerSlashing) (*ProposerSlashing, e }, nil } +// MapAttesterSlashing maps the eth2.AttesterSlashing proto to the Web3Signer spec. func MapSignedBeaconBlockHeader(signedHeader *ethpb.SignedBeaconBlockHeader) (*SignedBeaconBlockHeader, error) { if signedHeader == nil { return nil, fmt.Errorf("signed beacon block header is nil") @@ -189,6 +190,7 @@ func MapSignedBeaconBlockHeader(signedHeader *ethpb.SignedBeaconBlockHeader) (*S }, nil } +// MapAttesterSlashing maps the eth2.AttesterSlashing proto to the Web3Signer spec. func MapAttesterSlashing(slashing *ethpb.AttesterSlashing) (*AttesterSlashing, error) { if slashing == nil { return nil, fmt.Errorf("attester slashing is nil") @@ -207,6 +209,7 @@ func MapAttesterSlashing(slashing *ethpb.AttesterSlashing) (*AttesterSlashing, e }, nil } +// MapIndexedAttestation maps the eth2.IndexedAttestation proto to the Web3Signer spec. func MapIndexedAttestation(attestation *ethpb.IndexedAttestation) (*IndexedAttestation, error) { if attestation == nil { return nil, fmt.Errorf("indexed attestation is nil") @@ -225,6 +228,7 @@ func MapIndexedAttestation(attestation *ethpb.IndexedAttestation) (*IndexedAttes }, nil } +// MapDeposit maps the eth2.Deposit proto to the Web3Signer spec. func MapDeposit(deposit *ethpb.Deposit) (*Deposit, error) { if deposit == nil { return nil, fmt.Errorf("deposit is nil") @@ -250,6 +254,7 @@ func MapDeposit(deposit *ethpb.Deposit) (*Deposit, error) { }, nil } +// MapSignedVoluntaryExit maps the eth2.SignedVoluntaryExit proto to the Web3Signer spec. func MapSignedVoluntaryExit(signedVoluntaryExit *ethpb.SignedVoluntaryExit) (*SignedVoluntaryExit, error) { if signedVoluntaryExit == nil { return nil, fmt.Errorf("signed voluntary exit is nil") @@ -264,3 +269,126 @@ func MapSignedVoluntaryExit(signedVoluntaryExit *ethpb.SignedVoluntaryExit) (*Si ), }, nil } + +// MapBeaconBlockAltair maps the eth2.BeaconBlockAltair proto to the Web3Signer spec. +func MapBeaconBlockAltair(block *ethpb.BeaconBlockAltair) (*BeaconBlockAltair, error) { + if block == nil { + return nil, fmt.Errorf("beacon block altair is nil") + } + body, err := MapBeaconBlockBodyAltair(block.Body) + if err != nil { + return nil, err + } + return &BeaconBlockAltair{ + Slot: fmt.Sprint(block.Slot), + Body: body, + ParentRoot: hexutil.Encode( + block.ParentRoot, + ), + StateRoot: hexutil.Encode( + block.StateRoot, + ), + }, nil +} + +// MapBeaconBlockBodyAltair maps the eth2.BeaconBlockBodyAltair proto to the Web3Signer spec. +func MapBeaconBlockBodyAltair(body *ethpb.BeaconBlockBodyAltair) (*BeaconBlockBodyAltair, error) { + if body == nil { + return nil, fmt.Errorf("beacon block body altair is nil") + } + + block := &BeaconBlockBodyAltair{ + RandaoReveal: hexutil.Encode(body.RandaoReveal), + Eth1Data: &Eth1Data{ + DepositRoot: hexutil.Encode(body.Eth1Data.DepositRoot), + BlockHash: hexutil.Encode(body.Eth1Data.BlockHash), + }, + Graffiti: hexutil.Encode(body.Graffiti), + ProposerSlashings: make([]*ProposerSlashing, len(body.ProposerSlashings)), + AttesterSlashings: make([]*AttesterSlashing, len(body.AttesterSlashings)), + Attestations: make([]*Attestation, len(body.Attestations)), + Deposits: make([]*Deposit, len(body.Deposits)), + VoluntaryExits: make([]*SignedVoluntaryExit, len(body.VoluntaryExits)), + SyncAggregate: &SyncAggregate{ + SyncCommitteeBits: fmt.Sprint(body.SyncAggregate.SyncCommitteeBits), + SyncCommitteeSignature: hexutil.Encode(body.SyncAggregate.SyncCommitteeSignature), + }, + } + for i, slashing := range body.ProposerSlashings { + proposer, err := MapProposerSlashing(slashing) + if err != nil { + return nil, err + } + block.ProposerSlashings[i] = proposer + } + for i, slashing := range body.AttesterSlashings { + attesterSlashing, err := MapAttesterSlashing(slashing) + if err != nil { + return nil, err + } + block.AttesterSlashings[i] = attesterSlashing + } + for i, attestation := range body.Attestations { + attestation, err := MapAttestation(attestation) + if err != nil { + return nil, err + } + block.Attestations[i] = attestation + } + for i, deposit := range body.Deposits { + deposit, err := MapDeposit(deposit) + if err != nil { + return nil, err + } + block.Deposits[i] = deposit + } + for i, exit := range body.VoluntaryExits { + + exit, err := MapSignedVoluntaryExit(exit) + if err != nil { + return nil, err + } + block.VoluntaryExits[i] = exit + } + return block, nil +} + +// MapSyncCommitteeMessage maps the eth2.SyncCommitteeMessage proto to the Web3Signer spec. +func MapSyncCommitteeMessage(message *ethpb.SyncCommitteeMessage) (*SyncCommitteeMessage, error) { + if message == nil { + return nil, fmt.Errorf("sync committee message is nil") + } + return &SyncCommitteeMessage{ + BeaconBlockRoot: hexutil.Encode(message.BlockRoot), + Slot: fmt.Sprint(message.Slot), + }, nil +} + +// MapSyncAggregatorSelectionData maps the eth2.SyncAggregatorSelectionData proto to the Web3Signer spec. +func MapSyncAggregatorSelectionData(data *ethpb.SyncAggregatorSelectionData) (*SyncAggregatorSelectionData, error) { + if data == nil { + return nil, fmt.Errorf("sync aggregator selection data is nil") + } + return &SyncAggregatorSelectionData{ + Slot: fmt.Sprint(data.Slot), + SubcommitteeIndex: fmt.Sprint(data.SubcommitteeIndex), + }, nil +} + +// MapContributionAndProof maps the eth2.ContributionAndProof proto to the Web3Signer spec. +func MapContributionAndProof(contribution *ethpb.ContributionAndProof) (*ContributionAndProof, error) { + if contribution == nil { + return nil, fmt.Errorf("contribution and proof is nil") + } + return &ContributionAndProof{ + AggregatorIndex: fmt.Sprint(contribution.AggregatorIndex), + SelectionProof: hexutil.Encode(contribution.SelectionProof), + Contribution: &SyncCommitteeContribution{ + Slot: fmt.Sprint(contribution.Contribution.Slot), + BeaconBlockRoot: hexutil.Encode(contribution.Contribution.BlockRoot), + SubcommitteeIndex: fmt.Sprint(contribution.Contribution.SubcommitteeIndex), + AggregationBits: hexutil.Encode(contribution.Contribution.AggregationBits), + Signature: hexutil.Encode(contribution.Contribution.Signature), + }, + }, nil +} diff --git a/validator/keymanager/remote-web3signer/v1/custom_mappers_test.go b/validator/keymanager/remote-web3signer/v1/custom_mappers_test.go new file mode 100644 index 000000000000..a4ba09129af2 --- /dev/null +++ b/validator/keymanager/remote-web3signer/v1/custom_mappers_test.go @@ -0,0 +1,451 @@ +package v1 + +import ( + "reflect" + "testing" + + ethpb "github.com/prysmaticlabs/prysm/proto/prysm/v1alpha1" +) + +func TestMapAggregateAndProof(t *testing.T) { + type args struct { + from *ethpb.AggregateAttestationAndProof + } + tests := []struct { + name string + args args + want *AggregateAndProof + wantErr bool + }{ + // TODO: Add test cases. + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + got, err := MapAggregateAndProof(tt.args.from) + if (err != nil) != tt.wantErr { + t.Errorf("MapAggregateAndProof() error = %v, wantErr %v", err, tt.wantErr) + return + } + if !reflect.DeepEqual(got, tt.want) { + t.Errorf("MapAggregateAndProof() got = %v, want %v", got, tt.want) + } + }) + } +} + +func TestMapAttestation(t *testing.T) { + type args struct { + attestation *ethpb.Attestation + } + tests := []struct { + name string + args args + want *Attestation + wantErr bool + }{ + // TODO: Add test cases. + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + got, err := 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 *AttestationData + wantErr bool + }{ + // TODO: Add test cases. + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + got, err := 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 *AttesterSlashing + wantErr bool + }{ + // TODO: Add test cases. + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + got, err := MapAttesterSlashing(tt.args.slashing) + if (err != nil) != tt.wantErr { + t.Errorf("MapAttesterSlashing() error = %v, wantErr %v", err, tt.wantErr) + return + } + if !reflect.DeepEqual(got, tt.want) { + t.Errorf("MapAttesterSlashing() got = %v, want %v", got, tt.want) + } + }) + } +} + +func TestMapBeaconBlockAltair(t *testing.T) { + type args struct { + block *ethpb.BeaconBlockAltair + } + tests := []struct { + name string + args args + want *BeaconBlockAltair + wantErr bool + }{ + // TODO: Add test cases. + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + got, err := MapBeaconBlockAltair(tt.args.block) + if (err != nil) != tt.wantErr { + t.Errorf("MapBeaconBlockAltair() error = %v, wantErr %v", err, tt.wantErr) + return + } + if !reflect.DeepEqual(got, tt.want) { + t.Errorf("MapBeaconBlockAltair() got = %v, want %v", got, tt.want) + } + }) + } +} + +func TestMapBeaconBlockBody(t *testing.T) { + type args struct { + body *ethpb.BeaconBlockBody + } + tests := []struct { + name string + args args + want *BeaconBlockBody + wantErr bool + }{ + // TODO: Add test cases. + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + got, err := 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 TestMapBeaconBlockBodyAltair(t *testing.T) { + type args struct { + body *ethpb.BeaconBlockBodyAltair + } + tests := []struct { + name string + args args + want *BeaconBlockBodyAltair + wantErr bool + }{ + // TODO: Add test cases. + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + got, err := MapBeaconBlockBodyAltair(tt.args.body) + if (err != nil) != tt.wantErr { + t.Errorf("MapBeaconBlockBodyAltair() error = %v, wantErr %v", err, tt.wantErr) + return + } + if !reflect.DeepEqual(got, tt.want) { + t.Errorf("MapBeaconBlockBodyAltair() got = %v, want %v", got, tt.want) + } + }) + } +} + +func TestMapCheckPoint(t *testing.T) { + type args struct { + checkpoint *ethpb.Checkpoint + } + tests := []struct { + name string + args args + want *Checkpoint + wantErr bool + }{ + // TODO: Add test cases. + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + got, err := MapCheckPoint(tt.args.checkpoint) + if (err != nil) != tt.wantErr { + t.Errorf("MapCheckPoint() error = %v, wantErr %v", err, tt.wantErr) + return + } + if !reflect.DeepEqual(got, tt.want) { + t.Errorf("MapCheckPoint() 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 *ContributionAndProof + wantErr bool + }{ + // TODO: Add test cases. + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + got, err := 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 TestMapDeposit(t *testing.T) { + type args struct { + deposit *ethpb.Deposit + } + tests := []struct { + name string + args args + want *Deposit + wantErr bool + }{ + // TODO: Add test cases. + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + got, err := MapDeposit(tt.args.deposit) + if (err != nil) != tt.wantErr { + t.Errorf("MapDeposit() error = %v, wantErr %v", err, tt.wantErr) + return + } + if !reflect.DeepEqual(got, tt.want) { + t.Errorf("MapDeposit() got = %v, want %v", got, tt.want) + } + }) + } +} + +func TestMapForkInfo(t *testing.T) { + type args struct { + from *ethpb.Fork + genesisValidatorsRoot []byte + } + tests := []struct { + name string + args args + want *ForkInfo + wantErr bool + }{ + // TODO: Add test cases. + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + got, err := MapForkInfo(tt.args.from, 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 TestMapIndexedAttestation(t *testing.T) { + type args struct { + attestation *ethpb.IndexedAttestation + } + tests := []struct { + name string + args args + want *IndexedAttestation + wantErr bool + }{ + // TODO: Add test cases. + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + got, err := MapIndexedAttestation(tt.args.attestation) + if (err != nil) != tt.wantErr { + t.Errorf("MapIndexedAttestation() error = %v, wantErr %v", err, tt.wantErr) + return + } + if !reflect.DeepEqual(got, tt.want) { + t.Errorf("MapIndexedAttestation() got = %v, want %v", got, tt.want) + } + }) + } +} + +func TestMapProposerSlashing(t *testing.T) { + type args struct { + slashing *ethpb.ProposerSlashing + } + tests := []struct { + name string + args args + want *ProposerSlashing + wantErr bool + }{ + // TODO: Add test cases. + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + got, err := MapProposerSlashing(tt.args.slashing) + if (err != nil) != tt.wantErr { + t.Errorf("MapProposerSlashing() error = %v, wantErr %v", err, tt.wantErr) + return + } + if !reflect.DeepEqual(got, tt.want) { + t.Errorf("MapProposerSlashing() got = %v, want %v", got, tt.want) + } + }) + } +} + +func TestMapSignedBeaconBlockHeader(t *testing.T) { + type args struct { + signedHeader *ethpb.SignedBeaconBlockHeader + } + tests := []struct { + name string + args args + want *SignedBeaconBlockHeader + wantErr bool + }{ + // TODO: Add test cases. + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + got, err := MapSignedBeaconBlockHeader(tt.args.signedHeader) + if (err != nil) != tt.wantErr { + t.Errorf("MapSignedBeaconBlockHeader() error = %v, wantErr %v", err, tt.wantErr) + return + } + if !reflect.DeepEqual(got, tt.want) { + t.Errorf("MapSignedBeaconBlockHeader() got = %v, want %v", got, tt.want) + } + }) + } +} + +func TestMapSignedVoluntaryExit(t *testing.T) { + type args struct { + signedVoluntaryExit *ethpb.SignedVoluntaryExit + } + tests := []struct { + name string + args args + want *SignedVoluntaryExit + wantErr bool + }{ + // TODO: Add test cases. + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + got, err := MapSignedVoluntaryExit(tt.args.signedVoluntaryExit) + if (err != nil) != tt.wantErr { + t.Errorf("MapSignedVoluntaryExit() error = %v, wantErr %v", err, tt.wantErr) + return + } + if !reflect.DeepEqual(got, tt.want) { + t.Errorf("MapSignedVoluntaryExit() 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 *SyncAggregatorSelectionData + wantErr bool + }{ + // TODO: Add test cases. + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + got, err := 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) + } + }) + } +} + +func TestMapSyncCommitteeMessage(t *testing.T) { + type args struct { + message *ethpb.SyncCommitteeMessage + } + tests := []struct { + name string + args args + want *SyncCommitteeMessage + wantErr bool + }{ + // TODO: Add test cases. + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + got, err := MapSyncCommitteeMessage(tt.args.message) + if (err != nil) != tt.wantErr { + t.Errorf("MapSyncCommitteeMessage() error = %v, wantErr %v", err, tt.wantErr) + return + } + if !reflect.DeepEqual(got, tt.want) { + t.Errorf("MapSyncCommitteeMessage() got = %v, want %v", got, tt.want) + } + }) + } +} diff --git a/validator/keymanager/remote-web3signer/v1/web3signer_types.go b/validator/keymanager/remote-web3signer/v1/web3signer_types.go index 1a0a1e252062..3421c5e7786d 100644 --- a/validator/keymanager/remote-web3signer/v1/web3signer_types.go +++ b/validator/keymanager/remote-web3signer/v1/web3signer_types.go @@ -128,12 +128,14 @@ type AggregateAndProof struct { SelectionProof string `json:"selection_proof"` } +// Attestation a sub property of AggregateAndProofSignRequest. type Attestation struct { AggregationBits string `json:"aggregation_bits"` Data *AttestationData `json:"data"` Signature string `json:"signature"` } +// AttestationData a sub property of Attestation. type AttestationData struct { Slot string `json:"slot"` // Prysm uses CommitteeIndex but web3signer uses index. @@ -143,11 +145,13 @@ type AttestationData struct { Target *Checkpoint `json:"target"` } +// Checkpoint a sub property of AttestationData. type Checkpoint struct { Epoch string `json:"epoch"` Root string `json:"root"` } +// BeaconBlock a sub property of BeaconBlockBlockV2. type BeaconBlock struct { Slot string `json:"slot"` ProposerIndex string `json:"proposer_index"` @@ -156,6 +160,7 @@ type BeaconBlock struct { Body *BeaconBlockBody `json:"body"` } +// BeaconBlockBody a sub property of BeaconBlock. type BeaconBlockBody struct { RandaoReveal string `json:"randao_reveal"` Eth1Data *Eth1Data `json:"eth1_data"` @@ -167,12 +172,14 @@ type BeaconBlockBody struct { VoluntaryExits []*SignedVoluntaryExit `json:"voluntary_exits"` } +// Eth1Data a sub property of BeaconBlockBody. type Eth1Data struct { DepositRoot string `json:"deposit_root"` DepositCount string `json:"deposit_count"` /* uint64 */ BlockHash string `json:"block_hash"` } +// ProposerSlashing a sub property of BeaconBlockBody. type ProposerSlashing struct { // Prysm uses Header_1 but web3signer uses signed_header_1. SignedHeader_1 *SignedBeaconBlockHeader `json:"signed_header_1"` @@ -180,11 +187,13 @@ type ProposerSlashing struct { SignedHeader_2 *SignedBeaconBlockHeader `json:"signed_header_2"` } +// SignedBeaconBlockHeader is a sub property of ProposerSlashing. type SignedBeaconBlockHeader struct { Message *BeaconBlockHeader Signature string `json:"signature"` } +// BeaconBlockHeader is a sub property of SignedBeaconBlockHeader. type BeaconBlockHeader struct { Slot string `json:"slot"` /* uint64 */ ProposerIndex string `json:"proposer_index"` /* uint64 */ @@ -193,23 +202,27 @@ type BeaconBlockHeader struct { BodyRoot string `json:"body_root"` /* Hash32 */ } +// AttesterSlashing a sub property of BeaconBlockBody. type AttesterSlashing struct { Attestation_1 *IndexedAttestation `json:"attestation_1"` Attestation_2 *IndexedAttestation `json:"attestation_2"` } +// IndexedAttestation a sub property of AttesterSlashing. type IndexedAttestation struct { AttestingIndices []string `json:"attesting_indices"` /* uint64[] */ Data *AttestationData `json:"data"` Signature string `json:"signature"` } +// Deposit a sub property of DepositSignRequest. type Deposit struct { Proof []string `json:"proof"` Data *DepositData `json:"data"` } -// Prysm uses Deposit_data instead of DepositData which is inconsistent naming +// DepositData a sub property of Deposit. +// DepositData :Prysm uses Deposit_data instead of DepositData which is inconsistent naming type DepositData struct { PublicKey string `json:"public_key"` WithdrawalCredentials string `json:"withdrawal_credentials"` @@ -217,12 +230,14 @@ type DepositData struct { Signature string `json:"signature"` } +// SignedVoluntaryExit is a sub property of BeaconBlockBody. type SignedVoluntaryExit struct { // Prysm uses Exit instead of Message Message *VoluntaryExit `json:"message"` Signature string `json:"signature"` } +// VoluntaryExit a sub property of SignedVoluntaryExit. type VoluntaryExit struct { Epoch string `json:"epoch"` /* uint64 */ ValidatorIndex string `json:"validator_index"` /* uint64 */ @@ -234,6 +249,7 @@ type BeaconBlockAltairBlockV2 struct { Block *BeaconBlockAltair `json:"beacon_block"` } +// BeaconBlockAltair a sub property of BeaconBlockAltairBlockV2. type BeaconBlockAltair struct { Slot string `json:"slot"` ProposerIndex string `json:"proposer_index"` @@ -242,6 +258,7 @@ type BeaconBlockAltair struct { Body *BeaconBlockBodyAltair `json:"body"` } +// BeaconBlockBodyAltair a sub property of BeaconBlockAltair. type BeaconBlockBodyAltair struct { RandaoReveal string `json:"randao_reveal"` Eth1Data *Eth1Data `json:"eth1_data"` @@ -254,6 +271,7 @@ type BeaconBlockBodyAltair struct { SyncAggregate *SyncAggregate `json:"sync_aggregate"` } +// SyncAggregate is a sub property of BeaconBlockBodyAltair. type SyncAggregate struct { SyncCommitteeBits string `json:"sync_committee_bits"` /* SSZ hexadecimal string */ SyncCommitteeSignature string `json:"sync_committee_signature"` /* 96 byte hexadecimal string */ @@ -270,26 +288,30 @@ type RandaoReveal struct { Epoch string `json:"epoch"` /* uint64 */ } +// SyncCommitteeMessage a sub property of SyncCommitteeSignRequest. type SyncCommitteeMessage struct { BeaconBlockRoot string `json:"beacon_block_root"` /* Hash32 */ Slot string `json:"slot"` /* uint64 */ - // Prysm uses v2/syncommittee and has the following extra properties : ValidatorIndex, Signature + // Prysm uses BlockRoot instead of BeaconBlockRoot and has the following extra properties : ValidatorIndex, Signature } +// SyncAggregatorSelectionData a sub property of SyncAggregatorSelectionSignRequest. type SyncAggregatorSelectionData struct { Slot string `json:"slot"` /* uint64 */ SubcommitteeIndex string `json:"subcommittee_index"` /* uint64 */ } +// ContributionAndProof a sub property of AggregatorSelectionSignRequest. type ContributionAndProof struct { AggregatorIndex string `json:"aggregator_index"` /* uint64 */ SelectionProof string `json:"selection_proof"` /* 96 byte hexadecimal */ Contribution *SyncCommitteeContribution } +// SyncCommitteeContribution a sub property of AggregatorSelectionSignRequest. type SyncCommitteeContribution struct { Slot string `json:"slot"` /* uint64 */ - // Prysm uses v2/validator + // Prysm uses BlockRoot instead of BeaconBlockRoot BeaconBlockRoot string `json:"block_root"` /* Hash32 */ SubcommitteeIndex string `json:"subcommittee_index"` /* uint64 */ AggregationBits string `json:"aggregation_bits"` /* SSZ hexadecimal string */ From d3e02b262479c83fb00cd245db015c18a7b40327 Mon Sep 17 00:00:00 2001 From: James He Date: Mon, 10 Jan 2022 10:32:02 -0500 Subject: [PATCH 05/19] adding readme --- .../keymanager/remote-web3signer/README.md | 55 ++++++++++++++++++- 1 file changed, 54 insertions(+), 1 deletion(-) diff --git a/validator/keymanager/remote-web3signer/README.md b/validator/keymanager/remote-web3signer/README.md index e7db92afdd82..82697b29cf23 100644 --- a/validator/keymanager/remote-web3signer/README.md +++ b/validator/keymanager/remote-web3signer/README.md @@ -1,3 +1,56 @@ # Web3Signer -WIP \ No newline at end of file +Web3Signer is a popular remote signer tool by Consensys to allow users to store validation keys outside the validation +client and signed without the vc knowing the private keys. documentation on the Web3signer tool can be found +here https://consensys.github.io/web3signer/web3signer-eth2.html and https://docs.web3signer.consensys.net/en/latest/ + +issue: https://github.com/prysmaticlabs/prysm/issues/9994 + +## Support + +WIP + +## Features + +### CLI + +WIP + +### API + +- Get Public keys: returns all public keys currently stored with web3signer excluding newly added keys if reload keys + was not run. +- Sign: Signs a message with a given public key. There are several types of messages that can be signed ( web3signer + type to prysm type): + - BLOCK <- *validatorpb.SignRequest_Block + - ATTESTATION <- *validatorpb.SignRequest_AttestationData + - AGGREGATE_AND_PROOF <- *validatorpb.SignRequest_AggregateAttestationAndProof + - AGGREGATION_SLOT <- *validatorpb.SignRequest_Slot + - BLOCK_V2 <- *validatorpb.SignRequest_BlockV2 + - BLOCK_V3 <- *validatorpb.SignRequest_BlockV3 + - DEPOSIT <- not supported + - RANDAO_REVEAL <- *validatorpb.SignRequest_Epoch + - VOLUNTARY_EXIT <- *validatorpb.SignRequest_Exit + - SYNC_COMMITTEE_MESSAGE <- *validatorpb.SignRequest_SyncMessageBlockRoot + - SYNC_COMMITTEE_SELECTION_PROOF <- *validatorpb.SignRequest_SyncAggregatorSelectionData + - SYNC_COMMITTEE_CONTRIBUTION_AND_PROOF <- *validatorpb.SignRequest_ContributionAndProof +- Reload Keys: reloads all public keys from the web3signer. +- Get Server Status: returns OK if the web3signer is ok. + +## Files Added and Files Changed + +- Files Added: + - validator/keymanager/remote-web3signer package + +- Files Modified: + - modified: cmd/validator/flags/flags.go + - modified: validator/accounts/accounts_backup.go + - modified: validator/accounts/accounts_list.go + - modified: validator/accounts/iface/wallet.go + - modified: validator/accounts/userprompt/prompt.go + - modified: validator/accounts/wallet/wallet.go + - modified: validator/accounts/wallet_create.go + - modified: validator/client/runner.go + - modified: validator/client/validator.go + - modified: validator/keymanager/remote-web3signer/keymanager.go + - modified: validator/keymanager/types.go \ No newline at end of file From 014d16a317c3bc582f394057a851466c9f52ce0e Mon Sep 17 00:00:00 2001 From: James He Date: Mon, 10 Jan 2022 18:54:32 -0500 Subject: [PATCH 06/19] fixing unit tests and errors caught --- .../remote-web3signer/v1/custom_mappers.go | 17 +- .../v1/custom_mappers_test.go | 487 +++++++++++------- .../keymanager/remote-web3signer/v1/mocks.go | 215 ++++++++ 3 files changed, 514 insertions(+), 205 deletions(-) create mode 100644 validator/keymanager/remote-web3signer/v1/mocks.go diff --git a/validator/keymanager/remote-web3signer/v1/custom_mappers.go b/validator/keymanager/remote-web3signer/v1/custom_mappers.go index ae6fc4cd9a37..963e4f5ebbf2 100644 --- a/validator/keymanager/remote-web3signer/v1/custom_mappers.go +++ b/validator/keymanager/remote-web3signer/v1/custom_mappers.go @@ -98,8 +98,9 @@ func MapBeaconBlockBody(body *ethpb.BeaconBlockBody) (*BeaconBlockBody, error) { block := &BeaconBlockBody{ RandaoReveal: hexutil.Encode(body.RandaoReveal), Eth1Data: &Eth1Data{ - DepositRoot: hexutil.Encode(body.Eth1Data.DepositRoot), - BlockHash: hexutil.Encode(body.Eth1Data.BlockHash), + DepositRoot: hexutil.Encode(body.Eth1Data.DepositRoot), + DepositCount: fmt.Sprint(0), + BlockHash: hexutil.Encode(body.Eth1Data.BlockHash), }, Graffiti: hexutil.Encode(body.Graffiti), ProposerSlashings: make([]*ProposerSlashing, len(body.ProposerSlashings)), @@ -225,6 +226,7 @@ func MapIndexedAttestation(attestation *ethpb.IndexedAttestation) (*IndexedAttes return &IndexedAttestation{ AttestingIndices: attestingIndices, Data: attestationData, + Signature: hexutil.Encode(attestation.Signature), }, nil } @@ -234,8 +236,8 @@ func MapDeposit(deposit *ethpb.Deposit) (*Deposit, error) { return nil, fmt.Errorf("deposit is nil") } proof := make([]string, len(deposit.Proof)) - for i, p := range proof { - proof[i] = fmt.Sprint(p) + for i, p := range deposit.Proof { + proof[i] = hexutil.Encode(p) } return &Deposit{ Proof: proof, @@ -300,8 +302,9 @@ func MapBeaconBlockBodyAltair(body *ethpb.BeaconBlockBodyAltair) (*BeaconBlockBo block := &BeaconBlockBodyAltair{ RandaoReveal: hexutil.Encode(body.RandaoReveal), Eth1Data: &Eth1Data{ - DepositRoot: hexutil.Encode(body.Eth1Data.DepositRoot), - BlockHash: hexutil.Encode(body.Eth1Data.BlockHash), + DepositRoot: hexutil.Encode(body.Eth1Data.DepositRoot), + DepositCount: fmt.Sprint(body.Eth1Data.DepositCount), + BlockHash: hexutil.Encode(body.Eth1Data.BlockHash), }, Graffiti: hexutil.Encode(body.Graffiti), ProposerSlashings: make([]*ProposerSlashing, len(body.ProposerSlashings)), @@ -310,7 +313,7 @@ func MapBeaconBlockBodyAltair(body *ethpb.BeaconBlockBodyAltair) (*BeaconBlockBo Deposits: make([]*Deposit, len(body.Deposits)), VoluntaryExits: make([]*SignedVoluntaryExit, len(body.VoluntaryExits)), SyncAggregate: &SyncAggregate{ - SyncCommitteeBits: fmt.Sprint(body.SyncAggregate.SyncCommitteeBits), + SyncCommitteeBits: hexutil.Encode(body.SyncAggregate.SyncCommitteeBits), SyncCommitteeSignature: hexutil.Encode(body.SyncAggregate.SyncCommitteeSignature), }, } diff --git a/validator/keymanager/remote-web3signer/v1/custom_mappers_test.go b/validator/keymanager/remote-web3signer/v1/custom_mappers_test.go index a4ba09129af2..d685dc12ea29 100644 --- a/validator/keymanager/remote-web3signer/v1/custom_mappers_test.go +++ b/validator/keymanager/remote-web3signer/v1/custom_mappers_test.go @@ -4,6 +4,10 @@ import ( "reflect" "testing" + "github.com/ethereum/go-ethereum/common/hexutil" + fieldparams "github.com/prysmaticlabs/prysm/config/fieldparams" + "github.com/prysmaticlabs/prysm/testing/util" + ethpb "github.com/prysmaticlabs/prysm/proto/prysm/v1alpha1" ) @@ -17,7 +21,22 @@ func TestMapAggregateAndProof(t *testing.T) { want *AggregateAndProof wantErr bool }{ - // TODO: Add test cases. + { + name: "HappyPathTest", + args: args{ + from: ðpb.AggregateAttestationAndProof{ + AggregatorIndex: 0, + Aggregate: util.NewAttestation(), + SelectionProof: make([]byte, fieldparams.BLSSignatureLength), + }, + }, + want: &AggregateAndProof{ + AggregatorIndex: "0", + Aggregate: MockAttestation(), + SelectionProof: hexutil.Encode(make([]byte, fieldparams.BLSSignatureLength)), + }, + wantErr: false, + }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { @@ -26,8 +45,8 @@ func TestMapAggregateAndProof(t *testing.T) { t.Errorf("MapAggregateAndProof() error = %v, wantErr %v", err, tt.wantErr) return } - if !reflect.DeepEqual(got, tt.want) { - t.Errorf("MapAggregateAndProof() got = %v, want %v", got, tt.want) + if !reflect.DeepEqual(got.Aggregate, tt.want.Aggregate) { + t.Errorf("MapAggregateAndProof() got = %v, want %v", got.Aggregate, tt.want.Aggregate) } }) } @@ -43,7 +62,14 @@ func TestMapAttestation(t *testing.T) { want *Attestation wantErr bool }{ - // TODO: Add test cases. + { + name: "HappyPathTest", + args: args{ + attestation: util.NewAttestation(), + }, + want: MockAttestation(), + wantErr: false, + }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { @@ -69,7 +95,14 @@ func TestMapAttestationData(t *testing.T) { want *AttestationData wantErr bool }{ - // TODO: Add test cases. + { + name: "HappyPathTest", + args: args{ + data: util.NewAttestation().Data, + }, + want: MockAttestation().Data, + wantErr: false, + }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { @@ -95,7 +128,28 @@ func TestMapAttesterSlashing(t *testing.T) { want *AttesterSlashing wantErr bool }{ - // TODO: Add test cases. + { + name: "HappyPathTest", + args: args{ + slashing: ðpb.AttesterSlashing{ + Attestation_1: ðpb.IndexedAttestation{ + AttestingIndices: []uint64{0, 1, 2}, + Data: util.NewAttestation().Data, + Signature: make([]byte, fieldparams.BLSSignatureLength), + }, + Attestation_2: ðpb.IndexedAttestation{ + AttestingIndices: []uint64{0, 1, 2}, + Data: util.NewAttestation().Data, + Signature: make([]byte, fieldparams.BLSSignatureLength), + }, + }, + }, + want: &AttesterSlashing{ + Attestation_1: MockIndexedAttestation(), + Attestation_2: MockIndexedAttestation(), + }, + wantErr: false, + }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { @@ -104,8 +158,8 @@ func TestMapAttesterSlashing(t *testing.T) { t.Errorf("MapAttesterSlashing() error = %v, wantErr %v", err, tt.wantErr) return } - if !reflect.DeepEqual(got, tt.want) { - t.Errorf("MapAttesterSlashing() got = %v, want %v", got, tt.want) + if !reflect.DeepEqual(got.Attestation_1, tt.want.Attestation_1) { + t.Errorf("MapAttesterSlashing() got = %v, want %v", got.Attestation_1, tt.want.Attestation_1) } }) } @@ -121,7 +175,93 @@ func TestMapBeaconBlockAltair(t *testing.T) { want *BeaconBlockAltair wantErr bool }{ - // TODO: Add test cases. + { + name: "Happy Path Test", + args: args{ + block: ðpb.BeaconBlockAltair{ + Slot: 0, + ProposerIndex: 0, + ParentRoot: make([]byte, fieldparams.RootLength), + StateRoot: make([]byte, fieldparams.RootLength), + Body: ðpb.BeaconBlockBodyAltair{ + RandaoReveal: make([]byte, 32), + Eth1Data: ðpb.Eth1Data{ + DepositRoot: make([]byte, fieldparams.RootLength), + DepositCount: 0, + BlockHash: make([]byte, 32), + }, + Graffiti: make([]byte, 32), + ProposerSlashings: []*ethpb.ProposerSlashing{ + { + Header_1: ðpb.SignedBeaconBlockHeader{ + Header: ðpb.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: ðpb.SignedBeaconBlockHeader{ + Header: ðpb.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{ + ðpb.AttesterSlashing{ + Attestation_1: ðpb.IndexedAttestation{ + AttestingIndices: []uint64{0, 1, 2}, + Data: util.NewAttestation().Data, + Signature: make([]byte, fieldparams.BLSSignatureLength), + }, + Attestation_2: ðpb.IndexedAttestation{ + AttestingIndices: []uint64{0, 1, 2}, + Data: util.NewAttestation().Data, + Signature: make([]byte, fieldparams.BLSSignatureLength), + }, + }, + }, + Attestations: []*ethpb.Attestation{ + util.NewAttestation(), + }, + Deposits: []*ethpb.Deposit{ + ðpb.Deposit{ + Proof: [][]byte{[]byte("A")}, + Data: ðpb.Deposit_Data{ + PublicKey: make([]byte, fieldparams.BLSPubkeyLength), + WithdrawalCredentials: make([]byte, 32), + Amount: 0, + Signature: make([]byte, fieldparams.BLSSignatureLength), + }, + }, + }, + VoluntaryExits: []*ethpb.SignedVoluntaryExit{ + ðpb.SignedVoluntaryExit{ + Exit: ðpb.VoluntaryExit{ + Epoch: 0, + ValidatorIndex: 0, + }, + Signature: make([]byte, fieldparams.BLSSignatureLength), + }, + }, + SyncAggregate: ðpb.SyncAggregate{ + SyncCommitteeSignature: make([]byte, fieldparams.BLSSignatureLength), + SyncCommitteeBits: make([]byte, 64), + }, + }, + }, + }, + want: MockBeaconBlockAltair(), + wantErr: false, + }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { @@ -130,8 +270,8 @@ func TestMapBeaconBlockAltair(t *testing.T) { t.Errorf("MapBeaconBlockAltair() error = %v, wantErr %v", err, tt.wantErr) return } - if !reflect.DeepEqual(got, tt.want) { - t.Errorf("MapBeaconBlockAltair() got = %v, want %v", got, tt.want) + if !reflect.DeepEqual(got.Body, tt.want.Body) { + t.Errorf("MapBeaconBlockAltair() got = %v, want %v", got.Body.SyncAggregate, tt.want.Body.SyncAggregate) } }) } @@ -147,7 +287,83 @@ func TestMapBeaconBlockBody(t *testing.T) { want *BeaconBlockBody wantErr bool }{ - // TODO: Add test cases. + { + name: "Happy Path Test", + args: args{ + body: ðpb.BeaconBlockBody{ + RandaoReveal: make([]byte, 32), + Eth1Data: ðpb.Eth1Data{ + DepositRoot: make([]byte, fieldparams.RootLength), + DepositCount: 0, + BlockHash: make([]byte, 32), + }, + Graffiti: make([]byte, 32), + ProposerSlashings: []*ethpb.ProposerSlashing{ + { + Header_1: ðpb.SignedBeaconBlockHeader{ + Header: ðpb.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: ðpb.SignedBeaconBlockHeader{ + Header: ðpb.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{ + ðpb.AttesterSlashing{ + Attestation_1: ðpb.IndexedAttestation{ + AttestingIndices: []uint64{0, 1, 2}, + Data: util.NewAttestation().Data, + Signature: make([]byte, fieldparams.BLSSignatureLength), + }, + Attestation_2: ðpb.IndexedAttestation{ + AttestingIndices: []uint64{0, 1, 2}, + Data: util.NewAttestation().Data, + Signature: make([]byte, fieldparams.BLSSignatureLength), + }, + }, + }, + Attestations: []*ethpb.Attestation{ + util.NewAttestation(), + }, + Deposits: []*ethpb.Deposit{ + ðpb.Deposit{ + Proof: [][]byte{[]byte("A")}, + Data: ðpb.Deposit_Data{ + PublicKey: make([]byte, fieldparams.BLSPubkeyLength), + WithdrawalCredentials: make([]byte, 32), + Amount: 0, + Signature: make([]byte, fieldparams.BLSSignatureLength), + }, + }, + }, + VoluntaryExits: []*ethpb.SignedVoluntaryExit{ + ðpb.SignedVoluntaryExit{ + Exit: ðpb.VoluntaryExit{ + Epoch: 0, + ValidatorIndex: 0, + }, + Signature: make([]byte, fieldparams.BLSSignatureLength), + }, + }, + }, + }, + want: MockBeaconBlockBody(), + wantErr: false, + }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { @@ -163,58 +379,6 @@ func TestMapBeaconBlockBody(t *testing.T) { } } -func TestMapBeaconBlockBodyAltair(t *testing.T) { - type args struct { - body *ethpb.BeaconBlockBodyAltair - } - tests := []struct { - name string - args args - want *BeaconBlockBodyAltair - wantErr bool - }{ - // TODO: Add test cases. - } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - got, err := MapBeaconBlockBodyAltair(tt.args.body) - if (err != nil) != tt.wantErr { - t.Errorf("MapBeaconBlockBodyAltair() error = %v, wantErr %v", err, tt.wantErr) - return - } - if !reflect.DeepEqual(got, tt.want) { - t.Errorf("MapBeaconBlockBodyAltair() got = %v, want %v", got, tt.want) - } - }) - } -} - -func TestMapCheckPoint(t *testing.T) { - type args struct { - checkpoint *ethpb.Checkpoint - } - tests := []struct { - name string - args args - want *Checkpoint - wantErr bool - }{ - // TODO: Add test cases. - } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - got, err := MapCheckPoint(tt.args.checkpoint) - if (err != nil) != tt.wantErr { - t.Errorf("MapCheckPoint() error = %v, wantErr %v", err, tt.wantErr) - return - } - if !reflect.DeepEqual(got, tt.want) { - t.Errorf("MapCheckPoint() got = %v, want %v", got, tt.want) - } - }) - } -} - func TestMapContributionAndProof(t *testing.T) { type args struct { contribution *ethpb.ContributionAndProof @@ -225,7 +389,23 @@ func TestMapContributionAndProof(t *testing.T) { want *ContributionAndProof wantErr bool }{ - // TODO: Add test cases. + { + name: "Happy Path Test", + args: args{ + contribution: ðpb.ContributionAndProof{ + AggregatorIndex: 0, + Contribution: ðpb.SyncCommitteeContribution{ + Slot: 0, + BlockRoot: make([]byte, fieldparams.RootLength), + SubcommitteeIndex: 0, + AggregationBits: make([]byte, 64), + Signature: make([]byte, fieldparams.BLSSignatureLength), + }, + SelectionProof: make([]byte, fieldparams.BLSSignatureLength), + }, + }, + want: MockContributionAndProof(), + }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { @@ -241,44 +421,31 @@ func TestMapContributionAndProof(t *testing.T) { } } -func TestMapDeposit(t *testing.T) { - type args struct { - deposit *ethpb.Deposit - } - tests := []struct { - name string - args args - want *Deposit - wantErr bool - }{ - // TODO: Add test cases. - } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - got, err := MapDeposit(tt.args.deposit) - if (err != nil) != tt.wantErr { - t.Errorf("MapDeposit() error = %v, wantErr %v", err, tt.wantErr) - return - } - if !reflect.DeepEqual(got, tt.want) { - t.Errorf("MapDeposit() got = %v, want %v", got, tt.want) - } - }) - } -} - func TestMapForkInfo(t *testing.T) { type args struct { from *ethpb.Fork genesisValidatorsRoot []byte } + tests := []struct { name string args args want *ForkInfo wantErr bool }{ - // TODO: Add test cases. + { + name: "Happy Path Test", + args: args{ + from: ðpb.Fork{ + PreviousVersion: make([]byte, 4), + CurrentVersion: make([]byte, 4), + Epoch: 0, + }, + genesisValidatorsRoot: make([]byte, fieldparams.RootLength), + }, + want: MockForkInfo(), + wantErr: false, + }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { @@ -294,110 +461,6 @@ func TestMapForkInfo(t *testing.T) { } } -func TestMapIndexedAttestation(t *testing.T) { - type args struct { - attestation *ethpb.IndexedAttestation - } - tests := []struct { - name string - args args - want *IndexedAttestation - wantErr bool - }{ - // TODO: Add test cases. - } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - got, err := MapIndexedAttestation(tt.args.attestation) - if (err != nil) != tt.wantErr { - t.Errorf("MapIndexedAttestation() error = %v, wantErr %v", err, tt.wantErr) - return - } - if !reflect.DeepEqual(got, tt.want) { - t.Errorf("MapIndexedAttestation() got = %v, want %v", got, tt.want) - } - }) - } -} - -func TestMapProposerSlashing(t *testing.T) { - type args struct { - slashing *ethpb.ProposerSlashing - } - tests := []struct { - name string - args args - want *ProposerSlashing - wantErr bool - }{ - // TODO: Add test cases. - } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - got, err := MapProposerSlashing(tt.args.slashing) - if (err != nil) != tt.wantErr { - t.Errorf("MapProposerSlashing() error = %v, wantErr %v", err, tt.wantErr) - return - } - if !reflect.DeepEqual(got, tt.want) { - t.Errorf("MapProposerSlashing() got = %v, want %v", got, tt.want) - } - }) - } -} - -func TestMapSignedBeaconBlockHeader(t *testing.T) { - type args struct { - signedHeader *ethpb.SignedBeaconBlockHeader - } - tests := []struct { - name string - args args - want *SignedBeaconBlockHeader - wantErr bool - }{ - // TODO: Add test cases. - } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - got, err := MapSignedBeaconBlockHeader(tt.args.signedHeader) - if (err != nil) != tt.wantErr { - t.Errorf("MapSignedBeaconBlockHeader() error = %v, wantErr %v", err, tt.wantErr) - return - } - if !reflect.DeepEqual(got, tt.want) { - t.Errorf("MapSignedBeaconBlockHeader() got = %v, want %v", got, tt.want) - } - }) - } -} - -func TestMapSignedVoluntaryExit(t *testing.T) { - type args struct { - signedVoluntaryExit *ethpb.SignedVoluntaryExit - } - tests := []struct { - name string - args args - want *SignedVoluntaryExit - wantErr bool - }{ - // TODO: Add test cases. - } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - got, err := MapSignedVoluntaryExit(tt.args.signedVoluntaryExit) - if (err != nil) != tt.wantErr { - t.Errorf("MapSignedVoluntaryExit() error = %v, wantErr %v", err, tt.wantErr) - return - } - if !reflect.DeepEqual(got, tt.want) { - t.Errorf("MapSignedVoluntaryExit() got = %v, want %v", got, tt.want) - } - }) - } -} - func TestMapSyncAggregatorSelectionData(t *testing.T) { type args struct { data *ethpb.SyncAggregatorSelectionData @@ -408,7 +471,20 @@ func TestMapSyncAggregatorSelectionData(t *testing.T) { want *SyncAggregatorSelectionData wantErr bool }{ - // TODO: Add test cases. + { + name: "Happy Path Test", + args: args{ + data: ðpb.SyncAggregatorSelectionData{ + Slot: 0, + SubcommitteeIndex: 0, + }, + }, + want: &SyncAggregatorSelectionData{ + Slot: "0", + SubcommitteeIndex: "0", + }, + wantErr: false, + }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { @@ -434,7 +510,22 @@ func TestMapSyncCommitteeMessage(t *testing.T) { want *SyncCommitteeMessage wantErr bool }{ - // TODO: Add test cases. + { + name: "Happy Path Test", + args: args{ + message: ðpb.SyncCommitteeMessage{ + Slot: 0, + BlockRoot: make([]byte, fieldparams.RootLength), + ValidatorIndex: 0, + Signature: make([]byte, fieldparams.BLSSignatureLength), + }, + }, + want: &SyncCommitteeMessage{ + Slot: "0", + BeaconBlockRoot: hexutil.Encode(make([]byte, fieldparams.RootLength)), + }, + wantErr: false, + }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { diff --git a/validator/keymanager/remote-web3signer/v1/mocks.go b/validator/keymanager/remote-web3signer/v1/mocks.go new file mode 100644 index 000000000000..a3af5dd85f21 --- /dev/null +++ b/validator/keymanager/remote-web3signer/v1/mocks.go @@ -0,0 +1,215 @@ +package v1 + +import ( + "github.com/ethereum/go-ethereum/common/hexutil" + "github.com/prysmaticlabs/go-bitfield" + fieldparams "github.com/prysmaticlabs/prysm/config/fieldparams" +) + +// MockForkInfo is a mock implementation of the ForkInfo. +func MockForkInfo() *ForkInfo { + return &ForkInfo{ + Fork: &Fork{ + PreviousVersion: hexutil.Encode(make([]byte, 4)), + CurrentVersion: hexutil.Encode(make([]byte, 4)), + Epoch: "0", + }, + GenesisValidatorsRoot: hexutil.Encode(make([]byte, fieldparams.RootLength)), + } + +} + +// MockAttestation is a mock implementation of the Attestation. +func MockAttestation() *Attestation { + return &Attestation{ + AggregationBits: hexutil.Encode(bitfield.Bitlist{0b1101}), + Data: &AttestationData{ + Slot: "0", + Index: "0", + BeaconBlockRoot: hexutil.Encode(make([]byte, fieldparams.RootLength)), + Source: &Checkpoint{ + Epoch: "0", + Root: hexutil.Encode(make([]byte, fieldparams.RootLength)), + }, + Target: &Checkpoint{ + Epoch: "0", + Root: hexutil.Encode(make([]byte, fieldparams.RootLength)), + }, + }, + Signature: hexutil.Encode(make([]byte, fieldparams.BLSSignatureLength)), + } +} + +func MockIndexedAttestation() *IndexedAttestation { + return &IndexedAttestation{ + AttestingIndices: []string{"0", "1", "2"}, + Data: &AttestationData{ + Slot: "0", + Index: "0", + BeaconBlockRoot: hexutil.Encode(make([]byte, fieldparams.RootLength)), + Source: &Checkpoint{ + Epoch: "0", + Root: hexutil.Encode(make([]byte, fieldparams.RootLength)), + }, + Target: &Checkpoint{ + Epoch: "0", + Root: hexutil.Encode(make([]byte, fieldparams.RootLength)), + }, + }, + Signature: hexutil.Encode(make([]byte, fieldparams.BLSSignatureLength)), + } +} + +func MockBeaconBlockAltair() *BeaconBlockAltair { + return &BeaconBlockAltair{ + Slot: "0", + ProposerIndex: "0", + ParentRoot: hexutil.Encode(make([]byte, fieldparams.RootLength)), + StateRoot: hexutil.Encode(make([]byte, fieldparams.RootLength)), + Body: &BeaconBlockBodyAltair{ + RandaoReveal: hexutil.Encode(make([]byte, 32)), + Eth1Data: &Eth1Data{ + DepositRoot: hexutil.Encode(make([]byte, fieldparams.RootLength)), + DepositCount: "0", + BlockHash: hexutil.Encode(make([]byte, 32)), + }, + Graffiti: hexutil.Encode(make([]byte, 32)), + ProposerSlashings: []*ProposerSlashing{ + &ProposerSlashing{ + SignedHeader_1: &SignedBeaconBlockHeader{ + Message: &BeaconBlockHeader{ + Slot: "0", + ProposerIndex: "0", + ParentRoot: hexutil.Encode(make([]byte, fieldparams.RootLength)), + StateRoot: hexutil.Encode(make([]byte, fieldparams.RootLength)), + BodyRoot: hexutil.Encode(make([]byte, fieldparams.RootLength)), + }, + Signature: hexutil.Encode(make([]byte, fieldparams.BLSSignatureLength)), + }, + SignedHeader_2: &SignedBeaconBlockHeader{ + Message: &BeaconBlockHeader{ + Slot: "0", + ProposerIndex: "0", + ParentRoot: hexutil.Encode(make([]byte, fieldparams.RootLength)), + StateRoot: hexutil.Encode(make([]byte, fieldparams.RootLength)), + BodyRoot: hexutil.Encode(make([]byte, fieldparams.RootLength)), + }, + Signature: hexutil.Encode(make([]byte, fieldparams.BLSSignatureLength)), + }, + }, + }, + AttesterSlashings: []*AttesterSlashing{ + &AttesterSlashing{ + Attestation_1: MockIndexedAttestation(), + Attestation_2: MockIndexedAttestation(), + }, + }, + Attestations: []*Attestation{ + MockAttestation(), + }, + Deposits: []*Deposit{ + &Deposit{ + Proof: []string{"0x41"}, + Data: &DepositData{ + PublicKey: hexutil.Encode(make([]byte, fieldparams.BLSPubkeyLength)), + WithdrawalCredentials: hexutil.Encode(make([]byte, 32)), + Amount: "0", + Signature: hexutil.Encode(make([]byte, fieldparams.BLSSignatureLength)), + }, + }, + }, + VoluntaryExits: []*SignedVoluntaryExit{ + &SignedVoluntaryExit{ + Message: &VoluntaryExit{ + Epoch: "0", + ValidatorIndex: "0", + }, + Signature: hexutil.Encode(make([]byte, fieldparams.BLSSignatureLength)), + }, + }, + SyncAggregate: &SyncAggregate{ + SyncCommitteeSignature: hexutil.Encode(make([]byte, fieldparams.BLSSignatureLength)), + SyncCommitteeBits: hexutil.Encode(make([]byte, 64)), + }, + }, + } +} + +func MockBeaconBlockBody() *BeaconBlockBody { + return &BeaconBlockBody{ + RandaoReveal: hexutil.Encode(make([]byte, 32)), + Eth1Data: &Eth1Data{ + DepositRoot: hexutil.Encode(make([]byte, fieldparams.RootLength)), + DepositCount: "0", + BlockHash: hexutil.Encode(make([]byte, 32)), + }, + Graffiti: hexutil.Encode(make([]byte, 32)), + ProposerSlashings: []*ProposerSlashing{ + &ProposerSlashing{ + SignedHeader_1: &SignedBeaconBlockHeader{ + Message: &BeaconBlockHeader{ + Slot: "0", + ProposerIndex: "0", + ParentRoot: hexutil.Encode(make([]byte, fieldparams.RootLength)), + StateRoot: hexutil.Encode(make([]byte, fieldparams.RootLength)), + BodyRoot: hexutil.Encode(make([]byte, fieldparams.RootLength)), + }, + Signature: hexutil.Encode(make([]byte, fieldparams.BLSSignatureLength)), + }, + SignedHeader_2: &SignedBeaconBlockHeader{ + Message: &BeaconBlockHeader{ + Slot: "0", + ProposerIndex: "0", + ParentRoot: hexutil.Encode(make([]byte, fieldparams.RootLength)), + StateRoot: hexutil.Encode(make([]byte, fieldparams.RootLength)), + BodyRoot: hexutil.Encode(make([]byte, fieldparams.RootLength)), + }, + Signature: hexutil.Encode(make([]byte, fieldparams.BLSSignatureLength)), + }, + }, + }, + AttesterSlashings: []*AttesterSlashing{ + &AttesterSlashing{ + Attestation_1: MockIndexedAttestation(), + Attestation_2: MockIndexedAttestation(), + }, + }, + Attestations: []*Attestation{ + MockAttestation(), + }, + Deposits: []*Deposit{ + &Deposit{ + Proof: []string{"0x41"}, + Data: &DepositData{ + PublicKey: hexutil.Encode(make([]byte, fieldparams.BLSPubkeyLength)), + WithdrawalCredentials: hexutil.Encode(make([]byte, 32)), + Amount: "0", + Signature: hexutil.Encode(make([]byte, fieldparams.BLSSignatureLength)), + }, + }, + }, + VoluntaryExits: []*SignedVoluntaryExit{ + &SignedVoluntaryExit{ + Message: &VoluntaryExit{ + Epoch: "0", + ValidatorIndex: "0", + }, + Signature: hexutil.Encode(make([]byte, fieldparams.BLSSignatureLength)), + }, + }, + } +} + +func MockContributionAndProof() *ContributionAndProof { + return &ContributionAndProof{ + AggregatorIndex: "0", + Contribution: &SyncCommitteeContribution{ + Slot: "0", + BeaconBlockRoot: hexutil.Encode(make([]byte, fieldparams.RootLength)), + SubcommitteeIndex: "0", + AggregationBits: hexutil.Encode(make([]byte, 64)), + Signature: hexutil.Encode(make([]byte, fieldparams.BLSSignatureLength)), + }, + SelectionProof: hexutil.Encode(make([]byte, fieldparams.BLSSignatureLength)), + } +} From 554c55abc004f72cde9704f4d8d5aa3c4d7a4f2a Mon Sep 17 00:00:00 2001 From: James He Date: Tue, 11 Jan 2022 09:38:02 -0500 Subject: [PATCH 07/19] fixed a few missed properties and names --- .../remote-web3signer/v1/web3signer_types.go | 50 +++++++++---------- 1 file changed, 24 insertions(+), 26 deletions(-) diff --git a/validator/keymanager/remote-web3signer/v1/web3signer_types.go b/validator/keymanager/remote-web3signer/v1/web3signer_types.go index 3421c5e7786d..63f5fc9b8428 100644 --- a/validator/keymanager/remote-web3signer/v1/web3signer_types.go +++ b/validator/keymanager/remote-web3signer/v1/web3signer_types.go @@ -16,7 +16,7 @@ type AggregateAndProofSignRequest struct { Type string `json:"type"` ForkInfo *ForkInfo `json:"fork_info"` SigningRoot string `json:"signingRoot"` - AggregateAndProof *AggregateAndProof `json:"aggregation_slot"` + AggregateAndProof *AggregateAndProof `json:"aggregation_and_proof"` } // AttestationSignRequest is a request object for web3signer sign api. @@ -29,10 +29,10 @@ type AttestationSignRequest struct { // BlockSignRequest is a request object for web3signer sign api. type BlockSignRequest struct { - Type string `json:"type"` - ForkInfo *ForkInfo `json:"fork_info"` - SigningRoot string `json:"signingRoot"` - Block *BeaconBlockBody `json:"block"` + Type string `json:"type"` + ForkInfo *ForkInfo `json:"fork_info"` + SigningRoot string `json:"signingRoot"` + Block *BeaconBlock `json:"block"` } // BlockV2AltairSignRequest is a request object for web3signer sign api. @@ -53,11 +53,11 @@ type BlockV2SignRequest struct { // DepositSignRequest Not currently supported by Prysm. // DepositSignRequest is a request object for web3signer sign api. -type DepositSignRequest struct { - Type string `json:"type"` - SigningRoot string `json:"signingRoot"` - Deposit *Deposit `json:"deposit"` -} +//type DepositSignRequest struct { +// Type string `json:"type"` +// SigningRoot string `json:"signingRoot"` +// Deposit *Deposit `json:"deposit"` +//} // RandaoRevealSignRequest is a request object for web3signer sign api. type RandaoRevealSignRequest struct { @@ -123,9 +123,9 @@ type AggregationSlot struct { // AggregateAndProof a sub property of AggregateAndProofSignRequest. type AggregateAndProof struct { - AggregatorIndex string `json:"aggregator_index"` + AggregatorIndex string `json:"aggregator_index"` /* uint64 */ Aggregate *Attestation `json:"aggregate"` - SelectionProof string `json:"selection_proof"` + SelectionProof string `json:"selection_proof"` /* 96 bytes */ } // Attestation a sub property of AggregateAndProofSignRequest. @@ -137,9 +137,8 @@ type Attestation struct { // AttestationData a sub property of Attestation. type AttestationData struct { - Slot string `json:"slot"` - // Prysm uses CommitteeIndex but web3signer uses index. - Index string `json:"index"` + Slot string `json:"slot"` /* uint64 */ + Index string `json:"index"` /* uint64 */ // Prysm uses CommitteeIndex but web3signer uses index. BeaconBlockRoot string `json:"beacon_block_root"` Source *Checkpoint `json:"source"` Target *Checkpoint `json:"target"` @@ -153,8 +152,8 @@ type Checkpoint struct { // BeaconBlock a sub property of BeaconBlockBlockV2. type BeaconBlock struct { - Slot string `json:"slot"` - ProposerIndex string `json:"proposer_index"` + Slot string `json:"slot"` /* uint64 */ + ProposerIndex string `json:"proposer_index"` /* uint64 */ ParentRoot string `json:"parent_root"` StateRoot string `json:"state_root"` Body *BeaconBlockBody `json:"body"` @@ -164,7 +163,7 @@ type BeaconBlock struct { type BeaconBlockBody struct { RandaoReveal string `json:"randao_reveal"` Eth1Data *Eth1Data `json:"eth1_data"` - Graffiti string `json:"graffiti"` + Graffiti string `json:"graffiti"` // 32 bytes ProposerSlashings []*ProposerSlashing `json:"proposer_slashings"` AttesterSlashings []*AttesterSlashing `json:"attester_slashings"` Attestations []*Attestation `json:"attestations"` @@ -189,8 +188,8 @@ type ProposerSlashing struct { // SignedBeaconBlockHeader is a sub property of ProposerSlashing. type SignedBeaconBlockHeader struct { - Message *BeaconBlockHeader - Signature string `json:"signature"` + Message *BeaconBlockHeader `json:"message"` + Signature string `json:"signature"` } // BeaconBlockHeader is a sub property of SignedBeaconBlockHeader. @@ -303,16 +302,15 @@ type SyncAggregatorSelectionData struct { // ContributionAndProof a sub property of AggregatorSelectionSignRequest. type ContributionAndProof struct { - AggregatorIndex string `json:"aggregator_index"` /* uint64 */ - SelectionProof string `json:"selection_proof"` /* 96 byte hexadecimal */ - Contribution *SyncCommitteeContribution + AggregatorIndex string `json:"aggregator_index"` /* uint64 */ + SelectionProof string `json:"selection_proof"` /* 96 byte hexadecimal */ + Contribution *SyncCommitteeContribution `json:"contribution"` } // SyncCommitteeContribution a sub property of AggregatorSelectionSignRequest. type SyncCommitteeContribution struct { - Slot string `json:"slot"` /* uint64 */ - // Prysm uses BlockRoot instead of BeaconBlockRoot - BeaconBlockRoot string `json:"block_root"` /* Hash32 */ + Slot string `json:"slot"` /* uint64 */ + BeaconBlockRoot string `json:"block_root"` /* Hash32 */ // Prysm uses BlockRoot instead of BeaconBlockRoot SubcommitteeIndex string `json:"subcommittee_index"` /* uint64 */ AggregationBits string `json:"aggregation_bits"` /* SSZ hexadecimal string */ Signature string `json:"signature"` /* 96 byte hexadecimal string */ From 1bcb0b75dd8a4f1767519eaf4cbfc5f2a97789b4 Mon Sep 17 00:00:00 2001 From: James He Date: Tue, 11 Jan 2022 12:08:27 -0500 Subject: [PATCH 08/19] updating error handling to be more descriptive --- .../remote-web3signer/v1/custom_mappers.go | 38 ++++++++++--------- 1 file changed, 20 insertions(+), 18 deletions(-) diff --git a/validator/keymanager/remote-web3signer/v1/custom_mappers.go b/validator/keymanager/remote-web3signer/v1/custom_mappers.go index 963e4f5ebbf2..a4c03e7920bd 100644 --- a/validator/keymanager/remote-web3signer/v1/custom_mappers.go +++ b/validator/keymanager/remote-web3signer/v1/custom_mappers.go @@ -3,6 +3,8 @@ package v1 import ( "fmt" + "github.com/pkg/errors" + "github.com/ethereum/go-ethereum/common/hexutil" ethpb "github.com/prysmaticlabs/prysm/proto/prysm/v1alpha1" ) @@ -64,11 +66,11 @@ func MapAttestationData(data *ethpb.AttestationData) (*AttestationData, error) { } source, err := MapCheckPoint(data.Source) if err != nil { - return nil, err + return nil, errors.Wrap(err, "could not map source for attestation data") } target, err := MapCheckPoint(data.Target) if err != nil { - return nil, err + return nil, errors.Wrap(err, "could not map target for attestation data") } return &AttestationData{ Slot: fmt.Sprint(data.Slot), @@ -112,35 +114,35 @@ func MapBeaconBlockBody(body *ethpb.BeaconBlockBody) (*BeaconBlockBody, error) { for i, slashing := range body.ProposerSlashings { slashing, err := MapProposerSlashing(slashing) if err != nil { - return nil, err + return nil, fmt.Errorf("could not map proposer slashing at index %v: %v", i, err) } block.ProposerSlashings[i] = slashing } for i, slashing := range body.AttesterSlashings { slashing, err := MapAttesterSlashing(slashing) if err != nil { - return nil, err + return nil, fmt.Errorf("could not map attester slashing at index %v: %v", i, err) } block.AttesterSlashings[i] = slashing } for i, attestation := range body.Attestations { attestation, err := MapAttestation(attestation) if err != nil { - return nil, err + return nil, fmt.Errorf("could not map attestation at index %v: %v", i, err) } block.Attestations[i] = attestation } for i, Deposit := range body.Deposits { deposit, err := MapDeposit(Deposit) if err != nil { - return nil, err + return nil, fmt.Errorf("could not map deposit at index %v: %v", i, err) } block.Deposits[i] = deposit } for i, signedVoluntaryExit := range body.VoluntaryExits { signedVoluntaryExit, err := MapSignedVoluntaryExit(signedVoluntaryExit) if err != nil { - return nil, err + return nil, fmt.Errorf("could not map signed voluntary exit at index %v: %v", i, err) } block.VoluntaryExits[i] = signedVoluntaryExit } @@ -154,11 +156,11 @@ func MapProposerSlashing(slashing *ethpb.ProposerSlashing) (*ProposerSlashing, e } signedHeader1, err := MapSignedBeaconBlockHeader(slashing.Header_1) if err != nil { - return nil, err + return nil, errors.Wrap(err, "could not map signed header 1") } signedHeader2, err := MapSignedBeaconBlockHeader(slashing.Header_2) if err != nil { - return nil, err + return nil, errors.Wrap(err, "could not map signed header 2") } return &ProposerSlashing{ SignedHeader_1: signedHeader1, @@ -198,11 +200,11 @@ func MapAttesterSlashing(slashing *ethpb.AttesterSlashing) (*AttesterSlashing, e } attestation1, err := MapIndexedAttestation(slashing.Attestation_1) if err != nil { - return nil, err + return nil, errors.Wrap(err, "could not map attestation 1") } attestation2, err := MapIndexedAttestation(slashing.Attestation_2) if err != nil { - return nil, err + return nil, errors.Wrap(err, "could not map attestation 2") } return &AttesterSlashing{ Attestation_1: attestation1, @@ -221,7 +223,7 @@ func MapIndexedAttestation(attestation *ethpb.IndexedAttestation) (*IndexedAttes } attestationData, err := MapAttestationData(attestation.Data) if err != nil { - return nil, err + return nil, errors.Wrap(err, "could not map attestation data to IndexedAttestation") } return &IndexedAttestation{ AttestingIndices: attestingIndices, @@ -279,7 +281,7 @@ func MapBeaconBlockAltair(block *ethpb.BeaconBlockAltair) (*BeaconBlockAltair, e } body, err := MapBeaconBlockBodyAltair(block.Body) if err != nil { - return nil, err + return nil, errors.Wrap(err, "could not map beacon block body for altair") } return &BeaconBlockAltair{ Slot: fmt.Sprint(block.Slot), @@ -320,28 +322,28 @@ func MapBeaconBlockBodyAltair(body *ethpb.BeaconBlockBodyAltair) (*BeaconBlockBo for i, slashing := range body.ProposerSlashings { proposer, err := MapProposerSlashing(slashing) if err != nil { - return nil, err + return nil, fmt.Errorf("could not map proposer slashing at index %v: %v", i, err) } block.ProposerSlashings[i] = proposer } for i, slashing := range body.AttesterSlashings { attesterSlashing, err := MapAttesterSlashing(slashing) if err != nil { - return nil, err + return nil, fmt.Errorf("could not map attester slashing at index %v: %v", i, err) } block.AttesterSlashings[i] = attesterSlashing } for i, attestation := range body.Attestations { attestation, err := MapAttestation(attestation) if err != nil { - return nil, err + return nil, fmt.Errorf("could not map attestation at index %v: %v", i, err) } block.Attestations[i] = attestation } for i, deposit := range body.Deposits { deposit, err := MapDeposit(deposit) if err != nil { - return nil, err + return nil, fmt.Errorf("could not map deposit at index %v: %v", i, err) } block.Deposits[i] = deposit } @@ -349,7 +351,7 @@ func MapBeaconBlockBodyAltair(body *ethpb.BeaconBlockBodyAltair) (*BeaconBlockBo exit, err := MapSignedVoluntaryExit(exit) if err != nil { - return nil, err + return nil, fmt.Errorf("could not map signed voluntary exit at index %v: %v", i, err) } block.VoluntaryExits[i] = exit } From 5a0a40ce56ca0746c8d964878b6fbca342921adf Mon Sep 17 00:00:00 2001 From: james-prysm <90280386+james-prysm@users.noreply.github.com> Date: Tue, 11 Jan 2022 13:25:46 -0600 Subject: [PATCH 09/19] Update validator/keymanager/remote-web3signer/client_test.go Co-authored-by: Raul Jordan --- validator/keymanager/remote-web3signer/client_test.go | 1 - 1 file changed, 1 deletion(-) diff --git a/validator/keymanager/remote-web3signer/client_test.go b/validator/keymanager/remote-web3signer/client_test.go index 9f3c78a4ed51..5712037fc033 100644 --- a/validator/keymanager/remote-web3signer/client_test.go +++ b/validator/keymanager/remote-web3signer/client_test.go @@ -9,7 +9,6 @@ import ( "testing" v1 "github.com/prysmaticlabs/prysm/validator/keymanager/remote-web3signer/v1" - "github.com/stretchr/testify/assert" ) From 112d4b0d2ef62269d5a80f323fcae04a9a538ce7 Mon Sep 17 00:00:00 2001 From: james-prysm <90280386+james-prysm@users.noreply.github.com> Date: Tue, 11 Jan 2022 13:25:53 -0600 Subject: [PATCH 10/19] Update validator/keymanager/remote-web3signer/keymanager.go Co-authored-by: Raul Jordan --- validator/keymanager/remote-web3signer/keymanager.go | 1 - 1 file changed, 1 deletion(-) diff --git a/validator/keymanager/remote-web3signer/keymanager.go b/validator/keymanager/remote-web3signer/keymanager.go index b596eb2ad227..7b2953282efc 100644 --- a/validator/keymanager/remote-web3signer/keymanager.go +++ b/validator/keymanager/remote-web3signer/keymanager.go @@ -5,7 +5,6 @@ import ( "fmt" v1 "github.com/prysmaticlabs/prysm/validator/keymanager/remote-web3signer/v1" - "github.com/ethereum/go-ethereum/common/hexutil" "github.com/pkg/errors" "github.com/prysmaticlabs/prysm/async/event" From 148ab0a174ad2775c137c836accb7f9d96cad907 Mon Sep 17 00:00:00 2001 From: james-prysm <90280386+james-prysm@users.noreply.github.com> Date: Tue, 11 Jan 2022 13:26:00 -0600 Subject: [PATCH 11/19] Update validator/keymanager/remote-web3signer/v1/custom_mappers.go Co-authored-by: Raul Jordan --- validator/keymanager/remote-web3signer/v1/custom_mappers.go | 1 - 1 file changed, 1 deletion(-) diff --git a/validator/keymanager/remote-web3signer/v1/custom_mappers.go b/validator/keymanager/remote-web3signer/v1/custom_mappers.go index a4c03e7920bd..e18454a2d35a 100644 --- a/validator/keymanager/remote-web3signer/v1/custom_mappers.go +++ b/validator/keymanager/remote-web3signer/v1/custom_mappers.go @@ -4,7 +4,6 @@ import ( "fmt" "github.com/pkg/errors" - "github.com/ethereum/go-ethereum/common/hexutil" ethpb "github.com/prysmaticlabs/prysm/proto/prysm/v1alpha1" ) From 655ca36c6ee5eaa5ee9cd2b332f620ef83b8a46c Mon Sep 17 00:00:00 2001 From: james-prysm <90280386+james-prysm@users.noreply.github.com> Date: Tue, 11 Jan 2022 13:26:14 -0600 Subject: [PATCH 12/19] Update validator/keymanager/remote-web3signer/v1/custom_mappers.go Co-authored-by: Raul Jordan --- validator/keymanager/remote-web3signer/v1/custom_mappers.go | 1 - 1 file changed, 1 deletion(-) diff --git a/validator/keymanager/remote-web3signer/v1/custom_mappers.go b/validator/keymanager/remote-web3signer/v1/custom_mappers.go index e18454a2d35a..2c4779df3e55 100644 --- a/validator/keymanager/remote-web3signer/v1/custom_mappers.go +++ b/validator/keymanager/remote-web3signer/v1/custom_mappers.go @@ -8,7 +8,6 @@ import ( ethpb "github.com/prysmaticlabs/prysm/proto/prysm/v1alpha1" ) -// All Mappings represent version 1.0 of the Web3Signer specs i.e. /api/v1/eth2 // MapForkInfo maps the eth2.ForkInfo proto to the Web3Signer spec. func MapForkInfo(from *ethpb.Fork, genesisValidatorsRoot []byte) (*ForkInfo, error) { From 638a626df2090bab02126d88265a5b0fd28ac35d Mon Sep 17 00:00:00 2001 From: James He Date: Tue, 11 Jan 2022 14:42:45 -0500 Subject: [PATCH 13/19] fixing comments --- .../keymanager/remote-web3signer/v1/web3signer_types.go | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/validator/keymanager/remote-web3signer/v1/web3signer_types.go b/validator/keymanager/remote-web3signer/v1/web3signer_types.go index 63f5fc9b8428..b22a5ca3f222 100644 --- a/validator/keymanager/remote-web3signer/v1/web3signer_types.go +++ b/validator/keymanager/remote-web3signer/v1/web3signer_types.go @@ -1,6 +1,6 @@ package v1 -/* Web3Signer Specs are found on the following link: https://consensys.github.io/web3signer/web3signer-eth2.html */ +/* Web3Signer Specs are found on conensys website: google Consensys' Web3Signer API specification*/ // All Types represent version 1.0 of the Web3Signer specs i.e. /api/v1/eth2 // AggregationSlotSignRequest is a request object for web3signer sign api. @@ -53,11 +53,6 @@ type BlockV2SignRequest struct { // DepositSignRequest Not currently supported by Prysm. // DepositSignRequest is a request object for web3signer sign api. -//type DepositSignRequest struct { -// Type string `json:"type"` -// SigningRoot string `json:"signingRoot"` -// Deposit *Deposit `json:"deposit"` -//} // RandaoRevealSignRequest is a request object for web3signer sign api. type RandaoRevealSignRequest struct { From f6d86f4104f6e159cc882c7133c68154204466ff Mon Sep 17 00:00:00 2001 From: James He Date: Tue, 11 Jan 2022 15:02:51 -0500 Subject: [PATCH 14/19] fixing comments --- validator/keymanager/remote-web3signer/README.md | 4 ++-- .../keymanager/remote-web3signer/v1/custom_mappers.go | 9 ++++----- .../keymanager/remote-web3signer/v1/web3signer_types.go | 5 ++--- 3 files changed, 8 insertions(+), 10 deletions(-) diff --git a/validator/keymanager/remote-web3signer/README.md b/validator/keymanager/remote-web3signer/README.md index 82697b29cf23..a9086ab74c4a 100644 --- a/validator/keymanager/remote-web3signer/README.md +++ b/validator/keymanager/remote-web3signer/README.md @@ -1,8 +1,8 @@ # Web3Signer Web3Signer is a popular remote signer tool by Consensys to allow users to store validation keys outside the validation -client and signed without the vc knowing the private keys. documentation on the Web3signer tool can be found -here https://consensys.github.io/web3signer/web3signer-eth2.html and https://docs.web3signer.consensys.net/en/latest/ +client and signed without the vc knowing the private keys. Web3Signer Specs are found by +searching `Consensys' Web3Signer API specification` issue: https://github.com/prysmaticlabs/prysm/issues/9994 diff --git a/validator/keymanager/remote-web3signer/v1/custom_mappers.go b/validator/keymanager/remote-web3signer/v1/custom_mappers.go index 2c4779df3e55..5e4dcbedfc38 100644 --- a/validator/keymanager/remote-web3signer/v1/custom_mappers.go +++ b/validator/keymanager/remote-web3signer/v1/custom_mappers.go @@ -3,12 +3,11 @@ package v1 import ( "fmt" - "github.com/pkg/errors" "github.com/ethereum/go-ethereum/common/hexutil" + "github.com/pkg/errors" ethpb "github.com/prysmaticlabs/prysm/proto/prysm/v1alpha1" ) - // MapForkInfo maps the eth2.ForkInfo proto to the Web3Signer spec. func MapForkInfo(from *ethpb.Fork, genesisValidatorsRoot []byte) (*ForkInfo, error) { if from == nil { @@ -52,7 +51,7 @@ func MapAttestation(attestation *ethpb.Attestation) (*Attestation, error) { } return &Attestation{ Data: data, - AggregationBits: hexutil.Encode(attestation.AggregationBits), + AggregationBits: hexutil.Encode(attestation.AggregationBits.Bytes()), Signature: hexutil.Encode(attestation.Signature), }, nil } @@ -313,7 +312,7 @@ func MapBeaconBlockBodyAltair(body *ethpb.BeaconBlockBodyAltair) (*BeaconBlockBo Deposits: make([]*Deposit, len(body.Deposits)), VoluntaryExits: make([]*SignedVoluntaryExit, len(body.VoluntaryExits)), SyncAggregate: &SyncAggregate{ - SyncCommitteeBits: hexutil.Encode(body.SyncAggregate.SyncCommitteeBits), + SyncCommitteeBits: hexutil.Encode(body.SyncAggregate.SyncCommitteeBits.Bytes()), SyncCommitteeSignature: hexutil.Encode(body.SyncAggregate.SyncCommitteeSignature), }, } @@ -390,7 +389,7 @@ func MapContributionAndProof(contribution *ethpb.ContributionAndProof) (*Contrib Slot: fmt.Sprint(contribution.Contribution.Slot), BeaconBlockRoot: hexutil.Encode(contribution.Contribution.BlockRoot), SubcommitteeIndex: fmt.Sprint(contribution.Contribution.SubcommitteeIndex), - AggregationBits: hexutil.Encode(contribution.Contribution.AggregationBits), + AggregationBits: hexutil.Encode(contribution.Contribution.AggregationBits.Bytes()), Signature: hexutil.Encode(contribution.Contribution.Signature), }, }, nil diff --git a/validator/keymanager/remote-web3signer/v1/web3signer_types.go b/validator/keymanager/remote-web3signer/v1/web3signer_types.go index b22a5ca3f222..15c492b48eb4 100644 --- a/validator/keymanager/remote-web3signer/v1/web3signer_types.go +++ b/validator/keymanager/remote-web3signer/v1/web3signer_types.go @@ -1,8 +1,7 @@ +// Package v1 defines mappings of types as defined by the web3signer official specification for its v1 version i.e. /api/v1/eth2 +/* Web3Signer Specs are found by searching Consensys' Web3Signer API specification*/ package v1 -/* Web3Signer Specs are found on conensys website: google Consensys' Web3Signer API specification*/ -// All Types represent version 1.0 of the Web3Signer specs i.e. /api/v1/eth2 - // AggregationSlotSignRequest is a request object for web3signer sign api. type AggregationSlotSignRequest struct { Type string `json:"type"` From cd325f90fb134c057f7f8acceb9cdd62a2dacefc Mon Sep 17 00:00:00 2001 From: James He Date: Tue, 11 Jan 2022 15:22:00 -0500 Subject: [PATCH 15/19] adding more error checks --- .../remote-web3signer/v1/custom_mappers.go | 24 +++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/validator/keymanager/remote-web3signer/v1/custom_mappers.go b/validator/keymanager/remote-web3signer/v1/custom_mappers.go index 5e4dcbedfc38..ae1c0a3b2e6c 100644 --- a/validator/keymanager/remote-web3signer/v1/custom_mappers.go +++ b/validator/keymanager/remote-web3signer/v1/custom_mappers.go @@ -45,6 +45,9 @@ func MapAttestation(attestation *ethpb.Attestation) (*Attestation, error) { if attestation == nil { return nil, fmt.Errorf("attestation is nil") } + if attestation.AggregationBits == nil { + return nil, fmt.Errorf("aggregation bits in attestation is nil") + } data, err := MapAttestationData(attestation.Data) if err != nil { return nil, err @@ -94,6 +97,9 @@ func MapBeaconBlockBody(body *ethpb.BeaconBlockBody) (*BeaconBlockBody, error) { if body == nil { return nil, fmt.Errorf("beacon block body is nil") } + if body.Eth1Data == nil { + return nil, fmt.Errorf("eth1 data in Beacon Block Body is nil") + } block := &BeaconBlockBody{ RandaoReveal: hexutil.Encode(body.RandaoReveal), Eth1Data: &Eth1Data{ @@ -170,6 +176,9 @@ func MapSignedBeaconBlockHeader(signedHeader *ethpb.SignedBeaconBlockHeader) (*S if signedHeader == nil { return nil, fmt.Errorf("signed beacon block header is nil") } + if signedHeader.Header == nil { + return nil, fmt.Errorf("signed beacon block header message is nil") + } return &SignedBeaconBlockHeader{ Message: &BeaconBlockHeader{ Slot: fmt.Sprint(signedHeader.Header.Slot), @@ -260,6 +269,9 @@ func MapSignedVoluntaryExit(signedVoluntaryExit *ethpb.SignedVoluntaryExit) (*Si if signedVoluntaryExit == nil { return nil, fmt.Errorf("signed voluntary exit is nil") } + if signedVoluntaryExit.Exit == nil { + return nil, fmt.Errorf("exit in signed voluntary exit is nil") + } return &SignedVoluntaryExit{ Message: &VoluntaryExit{ Epoch: fmt.Sprint(signedVoluntaryExit.Exit.Epoch), @@ -297,6 +309,12 @@ func MapBeaconBlockBodyAltair(body *ethpb.BeaconBlockBodyAltair) (*BeaconBlockBo if body == nil { return nil, fmt.Errorf("beacon block body altair is nil") } + if body.SyncAggregate == nil { + return nil, fmt.Errorf("sync aggregate in beacon block body altair is nil") + } + if body.SyncAggregate.SyncCommitteeBits == nil { + return nil, fmt.Errorf("sync committee bits in sync aggregate in beacon block body altair is nil") + } block := &BeaconBlockBodyAltair{ RandaoReveal: hexutil.Encode(body.RandaoReveal), @@ -382,6 +400,12 @@ func MapContributionAndProof(contribution *ethpb.ContributionAndProof) (*Contrib if contribution == nil { return nil, fmt.Errorf("contribution and proof is nil") } + if contribution.Contribution == nil { + return nil, fmt.Errorf("contribution in ContributionAndProof is nil") + } + if contribution.Contribution.AggregationBits == nil { + return nil, fmt.Errorf("aggregation bits in ContributionAndProof is nil") + } return &ContributionAndProof{ AggregatorIndex: fmt.Sprint(contribution.AggregatorIndex), SelectionProof: hexutil.Encode(contribution.SelectionProof), From 147b93815a62ef42c2aa0054ec2308a84da6c0c7 Mon Sep 17 00:00:00 2001 From: Raul Jordan Date: Tue, 11 Jan 2022 15:41:23 -0500 Subject: [PATCH 16/19] Update validator/keymanager/remote-web3signer/client.go --- validator/keymanager/remote-web3signer/client.go | 1 - 1 file changed, 1 deletion(-) diff --git a/validator/keymanager/remote-web3signer/client.go b/validator/keymanager/remote-web3signer/client.go index eeb54e855c14..9168a35dfd59 100644 --- a/validator/keymanager/remote-web3signer/client.go +++ b/validator/keymanager/remote-web3signer/client.go @@ -11,7 +11,6 @@ import ( "time" v1 "github.com/prysmaticlabs/prysm/validator/keymanager/remote-web3signer/v1" - "github.com/ethereum/go-ethereum/common/hexutil" "github.com/pkg/errors" "github.com/prysmaticlabs/prysm/crypto/bls" From c52f4110a4fadf4d5a1057944bbea86a15dbb30f Mon Sep 17 00:00:00 2001 From: Raul Jordan Date: Tue, 11 Jan 2022 14:42:36 -0600 Subject: [PATCH 17/19] fmt and bazel build --- .../keymanager/remote-web3signer/BUILD.bazel | 2 ++ .../keymanager/remote-web3signer/client.go | 2 +- .../remote-web3signer/keymanager.go | 2 +- .../remote-web3signer/v1/BUILD.bazel | 31 +++++++++++++++++++ .../v1/custom_mappers_test.go | 12 +++---- .../keymanager/remote-web3signer/v1/mocks.go | 16 +++++----- 6 files changed, 49 insertions(+), 16 deletions(-) create mode 100644 validator/keymanager/remote-web3signer/v1/BUILD.bazel diff --git a/validator/keymanager/remote-web3signer/BUILD.bazel b/validator/keymanager/remote-web3signer/BUILD.bazel index dd09380d7d71..dee432a03721 100644 --- a/validator/keymanager/remote-web3signer/BUILD.bazel +++ b/validator/keymanager/remote-web3signer/BUILD.bazel @@ -17,6 +17,7 @@ go_library( "//crypto/bls:go_default_library", "//encoding/bytesutil:go_default_library", "//proto/prysm/v1alpha1/validator-client:go_default_library", + "//validator/keymanager/remote-web3signer/v1:go_default_library", "@com_github_ethereum_go_ethereum//common/hexutil:go_default_library", "@com_github_pkg_errors//:go_default_library", "@com_github_sirupsen_logrus//:go_default_library", @@ -35,6 +36,7 @@ go_test( "//encoding/bytesutil:go_default_library", "//proto/prysm/v1alpha1:go_default_library", "//proto/prysm/v1alpha1/validator-client:go_default_library", + "//validator/keymanager/remote-web3signer/v1:go_default_library", "@com_github_ethereum_go_ethereum//common/hexutil:go_default_library", "@com_github_stretchr_testify//assert:go_default_library", ], diff --git a/validator/keymanager/remote-web3signer/client.go b/validator/keymanager/remote-web3signer/client.go index 9168a35dfd59..1a1e589a0140 100644 --- a/validator/keymanager/remote-web3signer/client.go +++ b/validator/keymanager/remote-web3signer/client.go @@ -10,11 +10,11 @@ import ( "net/url" "time" - v1 "github.com/prysmaticlabs/prysm/validator/keymanager/remote-web3signer/v1" "github.com/ethereum/go-ethereum/common/hexutil" "github.com/pkg/errors" "github.com/prysmaticlabs/prysm/crypto/bls" "github.com/prysmaticlabs/prysm/encoding/bytesutil" + v1 "github.com/prysmaticlabs/prysm/validator/keymanager/remote-web3signer/v1" ) const ( diff --git a/validator/keymanager/remote-web3signer/keymanager.go b/validator/keymanager/remote-web3signer/keymanager.go index 7b2953282efc..324771387a39 100644 --- a/validator/keymanager/remote-web3signer/keymanager.go +++ b/validator/keymanager/remote-web3signer/keymanager.go @@ -4,12 +4,12 @@ import ( "context" "fmt" - v1 "github.com/prysmaticlabs/prysm/validator/keymanager/remote-web3signer/v1" "github.com/ethereum/go-ethereum/common/hexutil" "github.com/pkg/errors" "github.com/prysmaticlabs/prysm/async/event" "github.com/prysmaticlabs/prysm/crypto/bls" validatorpb "github.com/prysmaticlabs/prysm/proto/prysm/v1alpha1/validator-client" + v1 "github.com/prysmaticlabs/prysm/validator/keymanager/remote-web3signer/v1" ) // SetupConfig includes configuration values for initializing. diff --git a/validator/keymanager/remote-web3signer/v1/BUILD.bazel b/validator/keymanager/remote-web3signer/v1/BUILD.bazel new file mode 100644 index 000000000000..b917134ce5f5 --- /dev/null +++ b/validator/keymanager/remote-web3signer/v1/BUILD.bazel @@ -0,0 +1,31 @@ +load("@prysm//tools/go:def.bzl", "go_library", "go_test") + +go_library( + name = "go_default_library", + srcs = [ + "custom_mappers.go", + "mocks.go", + "web3signer_types.go", + ], + importpath = "github.com/prysmaticlabs/prysm/validator/keymanager/remote-web3signer/v1", + visibility = ["//visibility:public"], + deps = [ + "//config/fieldparams:go_default_library", + "//proto/prysm/v1alpha1:go_default_library", + "@com_github_ethereum_go_ethereum//common/hexutil:go_default_library", + "@com_github_pkg_errors//:go_default_library", + "@com_github_prysmaticlabs_go_bitfield//:go_default_library", + ], +) + +go_test( + name = "go_default_test", + srcs = ["custom_mappers_test.go"], + embed = [":go_default_library"], + deps = [ + "//config/fieldparams:go_default_library", + "//proto/prysm/v1alpha1:go_default_library", + "//testing/util:go_default_library", + "@com_github_ethereum_go_ethereum//common/hexutil:go_default_library", + ], +) diff --git a/validator/keymanager/remote-web3signer/v1/custom_mappers_test.go b/validator/keymanager/remote-web3signer/v1/custom_mappers_test.go index d685dc12ea29..31f8aeafd604 100644 --- a/validator/keymanager/remote-web3signer/v1/custom_mappers_test.go +++ b/validator/keymanager/remote-web3signer/v1/custom_mappers_test.go @@ -216,7 +216,7 @@ func TestMapBeaconBlockAltair(t *testing.T) { }, }, AttesterSlashings: []*ethpb.AttesterSlashing{ - ðpb.AttesterSlashing{ + { Attestation_1: ðpb.IndexedAttestation{ AttestingIndices: []uint64{0, 1, 2}, Data: util.NewAttestation().Data, @@ -233,7 +233,7 @@ func TestMapBeaconBlockAltair(t *testing.T) { util.NewAttestation(), }, Deposits: []*ethpb.Deposit{ - ðpb.Deposit{ + { Proof: [][]byte{[]byte("A")}, Data: ðpb.Deposit_Data{ PublicKey: make([]byte, fieldparams.BLSPubkeyLength), @@ -244,7 +244,7 @@ func TestMapBeaconBlockAltair(t *testing.T) { }, }, VoluntaryExits: []*ethpb.SignedVoluntaryExit{ - ðpb.SignedVoluntaryExit{ + { Exit: ðpb.VoluntaryExit{ Epoch: 0, ValidatorIndex: 0, @@ -323,7 +323,7 @@ func TestMapBeaconBlockBody(t *testing.T) { }, }, AttesterSlashings: []*ethpb.AttesterSlashing{ - ðpb.AttesterSlashing{ + { Attestation_1: ðpb.IndexedAttestation{ AttestingIndices: []uint64{0, 1, 2}, Data: util.NewAttestation().Data, @@ -340,7 +340,7 @@ func TestMapBeaconBlockBody(t *testing.T) { util.NewAttestation(), }, Deposits: []*ethpb.Deposit{ - ðpb.Deposit{ + { Proof: [][]byte{[]byte("A")}, Data: ðpb.Deposit_Data{ PublicKey: make([]byte, fieldparams.BLSPubkeyLength), @@ -351,7 +351,7 @@ func TestMapBeaconBlockBody(t *testing.T) { }, }, VoluntaryExits: []*ethpb.SignedVoluntaryExit{ - ðpb.SignedVoluntaryExit{ + { Exit: ðpb.VoluntaryExit{ Epoch: 0, ValidatorIndex: 0, diff --git a/validator/keymanager/remote-web3signer/v1/mocks.go b/validator/keymanager/remote-web3signer/v1/mocks.go index a3af5dd85f21..e459958a9803 100644 --- a/validator/keymanager/remote-web3signer/v1/mocks.go +++ b/validator/keymanager/remote-web3signer/v1/mocks.go @@ -75,7 +75,7 @@ func MockBeaconBlockAltair() *BeaconBlockAltair { }, Graffiti: hexutil.Encode(make([]byte, 32)), ProposerSlashings: []*ProposerSlashing{ - &ProposerSlashing{ + { SignedHeader_1: &SignedBeaconBlockHeader{ Message: &BeaconBlockHeader{ Slot: "0", @@ -99,7 +99,7 @@ func MockBeaconBlockAltair() *BeaconBlockAltair { }, }, AttesterSlashings: []*AttesterSlashing{ - &AttesterSlashing{ + { Attestation_1: MockIndexedAttestation(), Attestation_2: MockIndexedAttestation(), }, @@ -108,7 +108,7 @@ func MockBeaconBlockAltair() *BeaconBlockAltair { MockAttestation(), }, Deposits: []*Deposit{ - &Deposit{ + { Proof: []string{"0x41"}, Data: &DepositData{ PublicKey: hexutil.Encode(make([]byte, fieldparams.BLSPubkeyLength)), @@ -119,7 +119,7 @@ func MockBeaconBlockAltair() *BeaconBlockAltair { }, }, VoluntaryExits: []*SignedVoluntaryExit{ - &SignedVoluntaryExit{ + { Message: &VoluntaryExit{ Epoch: "0", ValidatorIndex: "0", @@ -145,7 +145,7 @@ func MockBeaconBlockBody() *BeaconBlockBody { }, Graffiti: hexutil.Encode(make([]byte, 32)), ProposerSlashings: []*ProposerSlashing{ - &ProposerSlashing{ + { SignedHeader_1: &SignedBeaconBlockHeader{ Message: &BeaconBlockHeader{ Slot: "0", @@ -169,7 +169,7 @@ func MockBeaconBlockBody() *BeaconBlockBody { }, }, AttesterSlashings: []*AttesterSlashing{ - &AttesterSlashing{ + { Attestation_1: MockIndexedAttestation(), Attestation_2: MockIndexedAttestation(), }, @@ -178,7 +178,7 @@ func MockBeaconBlockBody() *BeaconBlockBody { MockAttestation(), }, Deposits: []*Deposit{ - &Deposit{ + { Proof: []string{"0x41"}, Data: &DepositData{ PublicKey: hexutil.Encode(make([]byte, fieldparams.BLSPubkeyLength)), @@ -189,7 +189,7 @@ func MockBeaconBlockBody() *BeaconBlockBody { }, }, VoluntaryExits: []*SignedVoluntaryExit{ - &SignedVoluntaryExit{ + { Message: &VoluntaryExit{ Epoch: "0", ValidatorIndex: "0", From 0e96c61078b37a8e73019889e77ecb4bb6d5137b Mon Sep 17 00:00:00 2001 From: James He Date: Tue, 11 Jan 2022 16:15:31 -0500 Subject: [PATCH 18/19] fixing unit tests --- .../keymanager/remote-web3signer/v1/custom_mappers_test.go | 5 +++-- validator/keymanager/remote-web3signer/v1/mocks.go | 6 +++--- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/validator/keymanager/remote-web3signer/v1/custom_mappers_test.go b/validator/keymanager/remote-web3signer/v1/custom_mappers_test.go index 31f8aeafd604..34a965e6f22d 100644 --- a/validator/keymanager/remote-web3signer/v1/custom_mappers_test.go +++ b/validator/keymanager/remote-web3signer/v1/custom_mappers_test.go @@ -5,6 +5,7 @@ import ( "testing" "github.com/ethereum/go-ethereum/common/hexutil" + "github.com/prysmaticlabs/go-bitfield" fieldparams "github.com/prysmaticlabs/prysm/config/fieldparams" "github.com/prysmaticlabs/prysm/testing/util" @@ -254,7 +255,7 @@ func TestMapBeaconBlockAltair(t *testing.T) { }, SyncAggregate: ðpb.SyncAggregate{ SyncCommitteeSignature: make([]byte, fieldparams.BLSSignatureLength), - SyncCommitteeBits: make([]byte, 64), + SyncCommitteeBits: bitfield.NewBitvector512(), }, }, }, @@ -398,7 +399,7 @@ func TestMapContributionAndProof(t *testing.T) { Slot: 0, BlockRoot: make([]byte, fieldparams.RootLength), SubcommitteeIndex: 0, - AggregationBits: make([]byte, 64), + AggregationBits: bitfield.NewBitvector128(), Signature: make([]byte, fieldparams.BLSSignatureLength), }, SelectionProof: make([]byte, fieldparams.BLSSignatureLength), diff --git a/validator/keymanager/remote-web3signer/v1/mocks.go b/validator/keymanager/remote-web3signer/v1/mocks.go index e459958a9803..2007bb0a5427 100644 --- a/validator/keymanager/remote-web3signer/v1/mocks.go +++ b/validator/keymanager/remote-web3signer/v1/mocks.go @@ -22,7 +22,7 @@ func MockForkInfo() *ForkInfo { // MockAttestation is a mock implementation of the Attestation. func MockAttestation() *Attestation { return &Attestation{ - AggregationBits: hexutil.Encode(bitfield.Bitlist{0b1101}), + AggregationBits: hexutil.Encode(bitfield.Bitlist{0b1101}.Bytes()), Data: &AttestationData{ Slot: "0", Index: "0", @@ -129,7 +129,7 @@ func MockBeaconBlockAltair() *BeaconBlockAltair { }, SyncAggregate: &SyncAggregate{ SyncCommitteeSignature: hexutil.Encode(make([]byte, fieldparams.BLSSignatureLength)), - SyncCommitteeBits: hexutil.Encode(make([]byte, 64)), + SyncCommitteeBits: hexutil.Encode(bitfield.NewBitvector512().Bytes()), }, }, } @@ -207,7 +207,7 @@ func MockContributionAndProof() *ContributionAndProof { Slot: "0", BeaconBlockRoot: hexutil.Encode(make([]byte, fieldparams.RootLength)), SubcommitteeIndex: "0", - AggregationBits: hexutil.Encode(make([]byte, 64)), + AggregationBits: hexutil.Encode(bitfield.NewBitvector128().Bytes()), Signature: hexutil.Encode(make([]byte, fieldparams.BLSSignatureLength)), }, SelectionProof: hexutil.Encode(make([]byte, fieldparams.BLSSignatureLength)), From 846d3cd01cf237bff2692926386836ae3a99ceaa Mon Sep 17 00:00:00 2001 From: Raul Jordan Date: Tue, 11 Jan 2022 15:30:50 -0600 Subject: [PATCH 19/19] lint needed --- validator/keymanager/remote-web3signer/v1/BUILD.bazel | 1 + 1 file changed, 1 insertion(+) diff --git a/validator/keymanager/remote-web3signer/v1/BUILD.bazel b/validator/keymanager/remote-web3signer/v1/BUILD.bazel index b917134ce5f5..623b77d40e74 100644 --- a/validator/keymanager/remote-web3signer/v1/BUILD.bazel +++ b/validator/keymanager/remote-web3signer/v1/BUILD.bazel @@ -27,5 +27,6 @@ go_test( "//proto/prysm/v1alpha1:go_default_library", "//testing/util:go_default_library", "@com_github_ethereum_go_ethereum//common/hexutil:go_default_library", + "@com_github_prysmaticlabs_go_bitfield//:go_default_library", ], )