Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add signature aggregation helpers #711

Merged
merged 9 commits into from
Jul 7, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions params/protocol_params.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,3 +169,7 @@ var (
MinimumDifficulty = big.NewInt(131072) // The minimum that the difficulty may ever be.
DurationLimit = big.NewInt(13) // The decision boundary on the blocktime duration used to determine whether difficulty should go up or not.
)

aaronbuchwald marked this conversation as resolved.
Show resolved Hide resolved
const (
WarpQuorumDenominator uint64 = 100
)
5 changes: 4 additions & 1 deletion plugin/evm/vm.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ import (
"github.com/ava-labs/subnet-evm/sync/client/stats"
"github.com/ava-labs/subnet-evm/trie"
"github.com/ava-labs/subnet-evm/warp"
"github.com/ava-labs/subnet-evm/warp/aggregator"
warpValidators "github.com/ava-labs/subnet-evm/warp/validators"

// Force-load tracer engine to trigger registration
//
Expand Down Expand Up @@ -822,7 +824,8 @@ func (vm *VM) CreateHandlers(context.Context) (map[string]*commonEng.HTTPHandler
}

if vm.config.WarpAPIEnabled {
if err := handler.RegisterName("warp", &warp.WarpAPI{Backend: vm.warpBackend}); err != nil {
warpAggregator := aggregator.NewAggregator(vm.ctx.SubnetID, warpValidators.NewState(vm.ctx), &aggregator.NetworkSigner{Client: vm.client})
if err := handler.RegisterName("warp", warp.NewWarpAPI(vm.warpBackend, warpAggregator)); err != nil {
return nil, err
}
enabledAPIs = append(enabledAPIs, "warp")
Expand Down
135 changes: 135 additions & 0 deletions warp/aggregator/aggregation_job.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
// (c) 2023, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.

package aggregator

import (
"context"
"fmt"
"sync"

"github.com/ava-labs/avalanchego/ids"
"github.com/ava-labs/avalanchego/snow/validators"
"github.com/ava-labs/avalanchego/utils/crypto/bls"
"github.com/ava-labs/avalanchego/utils/set"
avalancheWarp "github.com/ava-labs/avalanchego/vms/platformvm/warp"
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/ethereum/go-ethereum/log"
)

type signatureAggregationJob struct {
aaronbuchwald marked this conversation as resolved.
Show resolved Hide resolved
// SignatureBackend is assumed to be thread-safe and may be used by multiple signature aggregation jobs concurrently
client SignatureBackend
height uint64
subnetID ids.ID

quorumNum uint64 // Minimum threshold at which to bother returning the resulting signature
aaronbuchwald marked this conversation as resolved.
Show resolved Hide resolved
cancelQuorumNum uint64 // Threshold at which to cancel further signature fetching
quorumDen uint64 // Denominator to use when checking if we've reached the threshold
state validators.State
msg *avalancheWarp.UnsignedMessage
}

type AggregateSignatureResult struct {
SignatureWeight uint64
TotalWeight uint64
Message *avalancheWarp.Message
}

func NewSignatureAggregationJob(
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why don't we just take aggregator as an input here?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point, this can be internal as well

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changed to internal, but looking at this, it may pass around a few parameters, but passing the aggregator as an input creates a dependency on the aggregator for the signature aggregation job tests that can be avoided otherwise, so going to leave this as is.

client SignatureBackend,
height uint64,
subnetID ids.ID,
quorumNum uint64,
cancelQuorumNum uint64,
aaronbuchwald marked this conversation as resolved.
Show resolved Hide resolved
quorumDen uint64,
state validators.State,
msg *avalancheWarp.UnsignedMessage,
) *signatureAggregationJob {
return &signatureAggregationJob{
client: client,
height: height,
subnetID: subnetID,
quorumNum: quorumNum,
cancelQuorumNum: cancelQuorumNum,
quorumDen: quorumDen,
state: state,
msg: msg,
}
}

// Execute aggregates signatures for the requested message
func (a *signatureAggregationJob) Execute(ctx context.Context) (*AggregateSignatureResult, error) {
validators, totalWeight, err := avalancheWarp.GetCanonicalValidatorSet(ctx, a.state, a.height, a.subnetID)
if err != nil {
return nil, fmt.Errorf("failed to get validator set: %w", err)
}
signatureJobs := make([]*signatureJob, 0, len(validators))
for _, validator := range validators {
signatureJobs = append(signatureJobs, newSignatureJob(a.client, validator, a.msg))
}

// signatureLock is used to access any of the signature attributes in the goroutines created below
signatureLock := sync.Mutex{}
blsSignatures := make([]*bls.Signature, 0, len(signatureJobs))
bitSet := set.NewBits()
signatureWeight := uint64(0)

// Create a child context to cancel signature fetching if we reach [cancelQuorumNum] threshold
signatureFetchCtx, signatureFetchCancel := context.WithCancel(ctx)
defer signatureFetchCancel()

wg := sync.WaitGroup{}
for i, signatureJob := range signatureJobs {
i := i
signatureJob := signatureJob
wg.Add(1)
go func() {
defer wg.Done()
log.Info("Fetching warp signature", "nodeID", signatureJob.nodeID, "index", i)
blsSignature, err := signatureJob.Execute(signatureFetchCtx)
if err != nil {
log.Info("Failed to fetch signature at index %d: %s", i, signatureJob)
return
}
log.Info("Retrieved warp signature", "nodeID", signatureJob.nodeID, "index", i, "signature", hexutil.Bytes(bls.SignatureToBytes(blsSignature)))
// Add the signature and check if we've reached the requested threshold
signatureLock.Lock()
defer signatureLock.Unlock()

blsSignatures = append(blsSignatures, blsSignature)
bitSet.Add(i)
log.Info("Updated weight", "totalWeight", signatureWeight+signatureJob.weight, "addedWeight", signatureJob.weight)
signatureWeight += signatureJob.weight
// If the signature weight meets the requested threshold, cancel signature fetching
if err := avalancheWarp.VerifyWeight(signatureWeight, totalWeight, a.cancelQuorumNum, a.quorumDen); err == nil {
log.Info("Verify weight passed, exiting aggregation early", "cancelQuorumNum", a.cancelQuorumNum, "totalWeight", totalWeight, "signatureWeight", signatureWeight)
signatureFetchCancel()
}
}()
}
wg.Wait()

// If I failed to fetch sufficient signature stake, return an error
if err := avalancheWarp.VerifyWeight(signatureWeight, totalWeight, a.quorumNum, a.quorumDen); err != nil {
return nil, fmt.Errorf("failed to aggregate signature: %w", err)
}
// Otherwise, return the aggregate signature
aggregateSignature, err := bls.AggregateSignatures(blsSignatures)
if err != nil {
return nil, fmt.Errorf("failed to aggregate BLS signatures: %w", err)
}
warpSignature := &avalancheWarp.BitSetSignature{
Signers: bitSet.Bytes(),
}
copy(warpSignature.Signature[:], bls.SignatureToBytes(aggregateSignature))
msg, err := avalancheWarp.NewMessage(a.msg, warpSignature)
if err != nil {
return nil, fmt.Errorf("failed to construct warp message: %w", err)
}
return &AggregateSignatureResult{
Message: msg,
SignatureWeight: signatureWeight,
TotalWeight: totalWeight,
}, nil
}
Loading