-
Notifications
You must be signed in to change notification settings - Fork 139
/
aggregator.go
171 lines (148 loc) · 4.95 KB
/
aggregator.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
// (c) 2023, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package aggregator
import (
"context"
"fmt"
"github.com/ethereum/go-ethereum/log"
"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/ava-labs/coreth/precompile/contracts/warp"
)
type AggregateSignatureResult struct {
// Weight of validators included in the aggregate signature.
SignatureWeight uint64
// Total weight of all validators in the subnet.
TotalWeight uint64
// The message with the aggregate signature.
Message *avalancheWarp.Message
}
type signatureFetchResult struct {
sig *bls.Signature
index int
weight uint64
}
// Aggregator requests signatures from validators and
// aggregates them into a single signature.
type Aggregator struct {
validators []*avalancheWarp.Validator
totalWeight uint64
client SignatureGetter
}
// New returns a signature aggregator that will attempt to aggregate signatures from [validators].
func New(client SignatureGetter, validators []*avalancheWarp.Validator, totalWeight uint64) *Aggregator {
return &Aggregator{
client: client,
validators: validators,
totalWeight: totalWeight,
}
}
// Returns an aggregate signature over [unsignedMessage].
// The returned signature's weight exceeds the threshold given by [quorumNum].
func (a *Aggregator) AggregateSignatures(ctx context.Context, unsignedMessage *avalancheWarp.UnsignedMessage, quorumNum uint64) (*AggregateSignatureResult, error) {
// Create a child context to cancel signature fetching if we reach signature threshold.
signatureFetchCtx, signatureFetchCancel := context.WithCancel(ctx)
defer signatureFetchCancel()
// Fetch signatures from validators concurrently.
signatureFetchResultChan := make(chan *signatureFetchResult)
for i, validator := range a.validators {
var (
i = i
validator = validator
// TODO: update from a single nodeID to the original slice and use extra nodeIDs as backup.
nodeID = validator.NodeIDs[0]
)
go func() {
log.Debug("Fetching warp signature",
"nodeID", nodeID,
"index", i,
"msgID", unsignedMessage.ID(),
)
signature, err := a.client.GetSignature(signatureFetchCtx, nodeID, unsignedMessage)
if err != nil {
log.Debug("Failed to fetch warp signature",
"nodeID", nodeID,
"index", i,
"err", err,
"msgID", unsignedMessage.ID(),
)
signatureFetchResultChan <- nil
return
}
log.Debug("Retrieved warp signature",
"nodeID", nodeID,
"msgID", unsignedMessage.ID(),
"index", i,
)
if !bls.Verify(validator.PublicKey, signature, unsignedMessage.Bytes()) {
log.Debug("Failed to verify warp signature",
"nodeID", nodeID,
"index", i,
"msgID", unsignedMessage.ID(),
)
signatureFetchResultChan <- nil
return
}
signatureFetchResultChan <- &signatureFetchResult{
sig: signature,
index: i,
weight: validator.Weight,
}
}()
}
var (
signatures = make([]*bls.Signature, 0, len(a.validators))
signersBitset = set.NewBits()
signaturesWeight = uint64(0)
signaturesPassedThreshold = false
)
for i := 0; i < len(a.validators); i++ {
signatureFetchResult := <-signatureFetchResultChan
if signatureFetchResult == nil {
continue
}
signatures = append(signatures, signatureFetchResult.sig)
signersBitset.Add(signatureFetchResult.index)
signaturesWeight += signatureFetchResult.weight
log.Debug("Updated weight",
"totalWeight", signaturesWeight,
"addedWeight", signatureFetchResult.weight,
"msgID", unsignedMessage.ID(),
)
// If the signature weight meets the requested threshold, cancel signature fetching
if err := avalancheWarp.VerifyWeight(signaturesWeight, a.totalWeight, quorumNum, warp.WarpQuorumDenominator); err == nil {
log.Debug("Verify weight passed, exiting aggregation early",
"quorumNum", quorumNum,
"totalWeight", a.totalWeight,
"signatureWeight", signaturesWeight,
"msgID", unsignedMessage.ID(),
)
signatureFetchCancel()
signaturesPassedThreshold = true
break
}
}
// If I failed to fetch sufficient signature stake, return an error
if !signaturesPassedThreshold {
return nil, avalancheWarp.ErrInsufficientWeight
}
// Otherwise, return the aggregate signature
aggregateSignature, err := bls.AggregateSignatures(signatures)
if err != nil {
return nil, fmt.Errorf("failed to aggregate BLS signatures: %w", err)
}
warpSignature := &avalancheWarp.BitSetSignature{
Signers: signersBitset.Bytes(),
}
copy(warpSignature.Signature[:], bls.SignatureToBytes(aggregateSignature))
msg, err := avalancheWarp.NewMessage(unsignedMessage, warpSignature)
if err != nil {
return nil, fmt.Errorf("failed to construct warp message: %w", err)
}
return &AggregateSignatureResult{
Message: msg,
SignatureWeight: signaturesWeight,
TotalWeight: a.totalWeight,
}, nil
}