prysm-pulse/validator/client/sync_committee.go
james-prysm 8da8855ad5
Web3Signer: Sign Method Implementation (#10084)
* breaking up changes from cli pr

* reverting some changes, adding in changes from remote-web3signer

* adding raul's change

* adding fork info to signing calls

* fixing imports

* gaz

* fixing gofmt

* removing unneeded comment

* Update validator/client/aggregate.go

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

* Update validator/client/sync_committee.go

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

* addressing comments

* revert proto changes

* proto changes

* reserve

* reserve

* switching to passing slot from passing fork, using slot to find the fork

* removing unneeded check

* fixing missed unit test

* optional

* optional

* gaz

* improving some definitions with constants

* improving some definitions with constants

* rem opt

* rem

* gaz

* moving mocks to its own folder

* adding in bazel field to fix

* fixing type check error

* fixing build

* fixing strict imports

* fixing dependencies

* changing bazel build

* changing bazel build

* changing bazel build

* removing testing only dependency

* removing dependency on testing util package

* update bazel build

* Update checktags_test.go

* Update active_balance.go

* Update sync_committee_minimal.go

* Update sync_committee_mainnet.go

* Update active_balance_disabled.go

* Update committee.go

* Update committee_disabled.go

* Update sync_committee.pb.gw.go

* Update powchain.pb.gw.go

* Update proposer_indices.go

* Update proposer_indices_disabled.go

* Update sync_committee.go

* Update mainnet.go

* Update p2p_messages.pb.gw.go

* Update finalized_block_root_container.pb.gw.go

* Update beacon_block.pb.gw.go

* Update attestation.pb.gw.go

* Update secret_key_test.go

* Update beacon_state.pb.gw.go

* Update version.pb.gw.go

* Update sync_committee.pb.gw.go

* Update sync_committee_disabled.go

* Update mainnet_test.go

* Update minimal.go

* Update signature_test.go

* Update gocast.go

* Update cgo_symbolizer.go

* Update validator.pb.gw.go

* Update beacon_state.pb.gw.go

* Update signature.go

* Update public_key_test.go

* Update minimal_test.go

* Update checktags_test.go

* Update bls_benchmark_test.go

* Update public_key.go

* Update secret_key.go

* Update aliases.go

* Update init.go

* Update stub.go

* Update journald_linux.go

* Update attestation.pb.gw.go

* Update config_utils_develop.go

* Update stub.go

* Update stub.go

* Update beacon_block.pb.gw.go

* Update validator.pb.gw.go

* Update node.pb.gw.go

* Update config_utils_prod.go

* Update journald.go

* Update beacon_block.pb.gw.go

* Update beacon_chain.pb.gw.go

* Update beacon_state.pb.gw.go

* Update events.pb.gw.go

* Update validator/keymanager/remote-web3signer/keymanager.go

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

* addressing comments from review

* updating length of comment

* Update validator/keymanager/remote-web3signer/keymanager.go

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

* Update stub.go

revert changes

* Update validator/client/aggregate_test.go

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

* addressing final comments

* fixing gofmt

Co-authored-by: Raul Jordan <raul@prysmaticlabs.com>
Co-authored-by: Preston Van Loon <preston@prysmaticlabs.com>
2022-01-18 14:31:58 -06:00

245 lines
8.4 KiB
Go

package client
import (
"context"
"fmt"
emptypb "github.com/golang/protobuf/ptypes/empty"
types "github.com/prysmaticlabs/eth2-types"
"github.com/prysmaticlabs/prysm/beacon-chain/core/altair"
"github.com/prysmaticlabs/prysm/beacon-chain/core/signing"
fieldparams "github.com/prysmaticlabs/prysm/config/fieldparams"
"github.com/prysmaticlabs/prysm/config/params"
"github.com/prysmaticlabs/prysm/encoding/bytesutil"
"github.com/prysmaticlabs/prysm/monitoring/tracing"
ethpb "github.com/prysmaticlabs/prysm/proto/prysm/v1alpha1"
validatorpb "github.com/prysmaticlabs/prysm/proto/prysm/v1alpha1/validator-client"
"github.com/prysmaticlabs/prysm/time/slots"
"github.com/sirupsen/logrus"
"go.opencensus.io/trace"
)
// SubmitSyncCommitteeMessage submits the sync committee message to the beacon chain.
func (v *validator) SubmitSyncCommitteeMessage(ctx context.Context, slot types.Slot, pubKey [fieldparams.BLSPubkeyLength]byte) {
ctx, span := trace.StartSpan(ctx, "validator.SubmitSyncCommitteeMessage")
defer span.End()
span.AddAttributes(trace.StringAttribute("validator", fmt.Sprintf("%#x", pubKey)))
v.waitOneThirdOrValidBlock(ctx, slot)
res, err := v.validatorClient.GetSyncMessageBlockRoot(ctx, &emptypb.Empty{})
if err != nil {
log.WithError(err).Error("Could not request sync message block root to sign")
tracing.AnnotateError(span, err)
return
}
duty, err := v.duty(pubKey)
if err != nil {
log.WithError(err).Error("Could not fetch validator assignment")
return
}
d, err := v.domainData(ctx, slots.ToEpoch(slot), params.BeaconConfig().DomainSyncCommittee[:])
if err != nil {
log.WithError(err).Error("Could not get sync committee domain data")
return
}
sszRoot := types.SSZBytes(res.Root)
r, err := signing.ComputeSigningRoot(&sszRoot, d.SignatureDomain)
if err != nil {
log.WithError(err).Error("Could not get sync committee message signing root")
return
}
sig, err := v.keyManager.Sign(ctx, &validatorpb.SignRequest{
PublicKey: pubKey[:],
SigningRoot: r[:],
SignatureDomain: d.SignatureDomain,
Object: &validatorpb.SignRequest_SyncMessageBlockRoot{
SyncMessageBlockRoot: res.Root,
},
SigningSlot: slot,
})
if err != nil {
log.WithError(err).Error("Could not sign sync committee message")
return
}
msg := &ethpb.SyncCommitteeMessage{
Slot: slot,
BlockRoot: res.Root,
ValidatorIndex: duty.ValidatorIndex,
Signature: sig.Marshal(),
}
if _, err := v.validatorClient.SubmitSyncMessage(ctx, msg); err != nil {
log.WithError(err).Error("Could not submit sync committee message")
return
}
log.WithFields(logrus.Fields{
"slot": msg.Slot,
"blockRoot": fmt.Sprintf("%#x", bytesutil.Trunc(msg.BlockRoot)),
"validatorIndex": msg.ValidatorIndex,
}).Info("Submitted new sync message")
}
// SubmitSignedContributionAndProof submits the signed sync committee contribution and proof to the beacon chain.
func (v *validator) SubmitSignedContributionAndProof(ctx context.Context, slot types.Slot, pubKey [fieldparams.BLSPubkeyLength]byte) {
ctx, span := trace.StartSpan(ctx, "validator.SubmitSignedContributionAndProof")
defer span.End()
span.AddAttributes(trace.StringAttribute("validator", fmt.Sprintf("%#x", pubKey)))
duty, err := v.duty(pubKey)
if err != nil {
log.Errorf("Could not fetch validator assignment: %v", err)
return
}
indexRes, err := v.validatorClient.GetSyncSubcommitteeIndex(ctx, &ethpb.SyncSubcommitteeIndexRequest{
PublicKey: pubKey[:],
Slot: slot,
})
if err != nil {
log.Errorf("Could not get sync subcommittee index: %v", err)
return
}
if len(indexRes.Indices) == 0 {
log.Debug("Empty subcommittee index list, do nothing")
return
}
selectionProofs, err := v.selectionProofs(ctx, slot, pubKey, indexRes)
if err != nil {
log.Errorf("Could not get selection proofs: %v", err)
return
}
v.waitToSlotTwoThirds(ctx, slot)
for i, comIdx := range indexRes.Indices {
isAggregator, err := altair.IsSyncCommitteeAggregator(selectionProofs[i])
if err != nil {
log.Errorf("Could check in aggregator: %v", err)
return
}
if !isAggregator {
continue
}
subCommitteeSize := params.BeaconConfig().SyncCommitteeSize / params.BeaconConfig().SyncCommitteeSubnetCount
subnet := uint64(comIdx) / subCommitteeSize
contribution, err := v.validatorClient.GetSyncCommitteeContribution(ctx, &ethpb.SyncCommitteeContributionRequest{
Slot: slot,
PublicKey: pubKey[:],
SubnetId: subnet,
})
if err != nil {
log.Errorf("Could not get sync committee contribution: %v", err)
return
}
if contribution.AggregationBits.Count() == 0 {
log.WithFields(logrus.Fields{
"slot": slot,
"pubkey": pubKey,
"subnet": subnet,
}).Warn("Sync contribution for validator has no bits set.")
continue
}
contributionAndProof := &ethpb.ContributionAndProof{
AggregatorIndex: duty.ValidatorIndex,
Contribution: contribution,
SelectionProof: selectionProofs[i],
}
sig, err := v.signContributionAndProof(ctx, pubKey, contributionAndProof, slot)
if err != nil {
log.Errorf("Could not sign contribution and proof: %v", err)
return
}
if _, err := v.validatorClient.SubmitSignedContributionAndProof(ctx, &ethpb.SignedContributionAndProof{
Message: contributionAndProof,
Signature: sig,
}); err != nil {
log.Errorf("Could not submit signed contribution and proof: %v", err)
return
}
log.WithFields(logrus.Fields{
"slot": contributionAndProof.Contribution.Slot,
"blockRoot": fmt.Sprintf("%#x", bytesutil.Trunc(contributionAndProof.Contribution.BlockRoot)),
"subcommitteeIndex": contributionAndProof.Contribution.SubcommitteeIndex,
"aggregatorIndex": contributionAndProof.AggregatorIndex,
"bitsCount": contributionAndProof.Contribution.AggregationBits.Count(),
}).Info("Submitted new sync contribution and proof")
}
}
// Signs and returns selection proofs per validator for slot and pub key.
func (v *validator) selectionProofs(ctx context.Context, slot types.Slot, pubKey [fieldparams.BLSPubkeyLength]byte, indexRes *ethpb.SyncSubcommitteeIndexResponse) ([][]byte, error) {
selectionProofs := make([][]byte, len(indexRes.Indices))
cfg := params.BeaconConfig()
size := cfg.SyncCommitteeSize
subCount := cfg.SyncCommitteeSubnetCount
for i, index := range indexRes.Indices {
subSize := size / subCount
subnet := uint64(index) / subSize
selectionProof, err := v.signSyncSelectionData(ctx, pubKey, subnet, slot)
if err != nil {
return nil, err
}
selectionProofs[i] = selectionProof
}
return selectionProofs, nil
}
// Signs input slot with domain sync committee selection proof. This is used to create the signature for sync committee selection.
func (v *validator) signSyncSelectionData(ctx context.Context, pubKey [fieldparams.BLSPubkeyLength]byte, index uint64, slot types.Slot) (signature []byte, err error) {
domain, err := v.domainData(ctx, slots.ToEpoch(slot), params.BeaconConfig().DomainSyncCommitteeSelectionProof[:])
if err != nil {
return nil, err
}
data := &ethpb.SyncAggregatorSelectionData{
Slot: slot,
SubcommitteeIndex: index,
}
root, err := signing.ComputeSigningRoot(data, domain.SignatureDomain)
if err != nil {
return nil, err
}
sig, err := v.keyManager.Sign(ctx, &validatorpb.SignRequest{
PublicKey: pubKey[:],
SigningRoot: root[:],
SignatureDomain: domain.SignatureDomain,
Object: &validatorpb.SignRequest_SyncAggregatorSelectionData{SyncAggregatorSelectionData: data},
SigningSlot: slot,
})
if err != nil {
return nil, err
}
return sig.Marshal(), nil
}
// This returns the signature of validator signing over sync committee contribution and proof object.
func (v *validator) signContributionAndProof(ctx context.Context, pubKey [fieldparams.BLSPubkeyLength]byte, c *ethpb.ContributionAndProof, slot types.Slot) ([]byte, error) {
d, err := v.domainData(ctx, slots.ToEpoch(c.Contribution.Slot), params.BeaconConfig().DomainContributionAndProof[:])
if err != nil {
return nil, err
}
root, err := signing.ComputeSigningRoot(c, d.SignatureDomain)
if err != nil {
return nil, err
}
sig, err := v.keyManager.Sign(ctx, &validatorpb.SignRequest{
PublicKey: pubKey[:],
SigningRoot: root[:],
SignatureDomain: d.SignatureDomain,
Object: &validatorpb.SignRequest_ContributionAndProof{ContributionAndProof: c},
SigningSlot: slot,
})
if err != nil {
return nil, err
}
return sig.Marshal(), nil
}