-
Notifications
You must be signed in to change notification settings - Fork 175
/
transactionVerifier.go
391 lines (326 loc) · 9.58 KB
/
transactionVerifier.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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
package fvm
import (
"context"
"fmt"
"sync"
"go.opentelemetry.io/otel/attribute"
"github.com/onflow/flow-go/fvm/crypto"
"github.com/onflow/flow-go/fvm/environment"
"github.com/onflow/flow-go/fvm/errors"
"github.com/onflow/flow-go/fvm/storage"
"github.com/onflow/flow-go/fvm/tracing"
"github.com/onflow/flow-go/model/flow"
"github.com/onflow/flow-go/module/trace"
)
type signatureType struct {
message []byte
errorBuilder func(flow.TransactionSignature, error) errors.CodedError
aggregateWeights map[flow.Address]int
}
type signatureEntry struct {
flow.TransactionSignature
signatureType
}
// signatureContinatuion is an internal/helper struct, accessible only by
// TransactionVerifier, used to keep track of the signature verification
// continuation state.
type signatureContinuation struct {
// signatureEntry is the initial input.
signatureEntry
// accountKey is set by getAccountKeys().
accountKey flow.AccountPublicKey
// invokedVerify and verifyErr are set by verifyAccountSignatures(). Note
// that verifyAccountSignatures() is always called after getAccountKeys()
// (i.e., accountKey is always initialized by the time
// verifyAccountSignatures is called).
invokedVerify bool
verifyErr errors.CodedError
}
func (entry *signatureContinuation) newError(err error) errors.CodedError {
return entry.errorBuilder(entry.TransactionSignature, err)
}
func (entry *signatureContinuation) matches(
proposalKey flow.ProposalKey,
) bool {
return entry.Address == proposalKey.Address &&
entry.KeyIndex == proposalKey.KeyIndex
}
func (entry *signatureContinuation) verify() errors.CodedError {
if entry.invokedVerify {
return entry.verifyErr
}
entry.invokedVerify = true
valid, err := crypto.VerifySignatureFromTransaction(
entry.Signature,
entry.message,
entry.accountKey.PublicKey,
entry.accountKey.HashAlgo,
)
if err != nil {
entry.verifyErr = entry.newError(err)
} else if !valid {
entry.verifyErr = entry.newError(fmt.Errorf("signature is not valid"))
}
return entry.verifyErr
}
func newSignatureEntries(
payloadSignatures []flow.TransactionSignature,
payloadMessage []byte,
envelopeSignatures []flow.TransactionSignature,
envelopeMessage []byte,
) (
[]*signatureContinuation,
map[flow.Address]int,
map[flow.Address]int,
error,
) {
payloadWeights := make(map[flow.Address]int, len(payloadSignatures))
envelopeWeights := make(map[flow.Address]int, len(envelopeSignatures))
type pair struct {
signatureType
signatures []flow.TransactionSignature
}
list := []pair{
{
signatureType{
payloadMessage,
errors.NewInvalidPayloadSignatureError,
payloadWeights,
},
payloadSignatures,
},
{
signatureType{
envelopeMessage,
errors.NewInvalidEnvelopeSignatureError,
envelopeWeights,
},
envelopeSignatures,
},
}
numSignatures := len(payloadSignatures) + len(envelopeSignatures)
signatures := make([]*signatureContinuation, 0, numSignatures)
type uniqueKey struct {
address flow.Address
index uint64
}
duplicate := make(map[uniqueKey]struct{}, numSignatures)
for _, group := range list {
for _, signature := range group.signatures {
entry := &signatureContinuation{
signatureEntry: signatureEntry{
TransactionSignature: signature,
signatureType: group.signatureType,
},
}
key := uniqueKey{
address: signature.Address,
index: signature.KeyIndex,
}
_, ok := duplicate[key]
if ok {
return nil, nil, nil, entry.newError(
fmt.Errorf("duplicate signatures are provided for the same key"))
}
duplicate[key] = struct{}{}
signatures = append(signatures, entry)
}
}
return signatures, payloadWeights, envelopeWeights, nil
}
// TransactionVerifier verifies the content of the transaction by
// checking there is no double signature
// all signatures are valid
// all accounts provides enoguh weights
//
// if KeyWeightThreshold is set to a negative number, signature verification is skipped
type TransactionVerifier struct {
VerificationConcurrency int
}
func (v *TransactionVerifier) CheckAuthorization(
tracer tracing.TracerSpan,
proc *TransactionProcedure,
txnState storage.TransactionPreparer,
keyWeightThreshold int,
) error {
// TODO(Janez): verification is part of inclusion fees, not execution fees.
var err error
txnState.RunWithAllLimitsDisabled(func() {
err = v.verifyTransaction(tracer, proc, txnState, keyWeightThreshold)
})
if err != nil {
return fmt.Errorf("transaction verification failed: %w", err)
}
return nil
}
// verifyTransaction verifies the transaction from the given procedure,
// and check the Authorizers have enough weights.
func (v *TransactionVerifier) verifyTransaction(
tracer tracing.TracerSpan,
proc *TransactionProcedure,
txnState storage.TransactionPreparer,
keyWeightThreshold int,
) error {
span := tracer.StartChildSpan(trace.FVMVerifyTransaction)
span.SetAttributes(
attribute.String("transaction.ID", proc.ID.String()),
)
defer span.End()
tx := proc.Transaction
if tx.Payer == flow.EmptyAddress {
return errors.NewInvalidAddressErrorf(tx.Payer, "payer address is invalid")
}
signatures, payloadWeights, envelopeWeights, err := newSignatureEntries(
tx.PayloadSignatures,
tx.PayloadMessage(),
tx.EnvelopeSignatures,
tx.EnvelopeMessage())
if err != nil {
return err
}
accounts := environment.NewAccounts(txnState)
if keyWeightThreshold < 0 {
return nil
}
err = v.getAccountKeys(txnState, accounts, signatures, tx.ProposalKey)
if err != nil {
return errors.NewInvalidProposalSignatureError(tx.ProposalKey, err)
}
err = v.verifyAccountSignatures(signatures)
if err != nil {
return errors.NewInvalidProposalSignatureError(tx.ProposalKey, err)
}
for _, addr := range tx.Authorizers {
// Skip this authorizer if it is also the payer. In the case where an account is
// both a PAYER as well as an AUTHORIZER or PROPOSER, that account is required
// to sign only the envelope.
if addr == tx.Payer {
continue
}
// hasSufficientKeyWeight
if !v.hasSufficientKeyWeight(payloadWeights, addr, keyWeightThreshold) {
return errors.NewAccountAuthorizationErrorf(
addr,
"authorizer account does not have sufficient signatures (%d < %d)",
payloadWeights[addr],
keyWeightThreshold)
}
}
if !v.hasSufficientKeyWeight(envelopeWeights, tx.Payer, keyWeightThreshold) {
// TODO change this to payer error (needed for fees)
return errors.NewAccountAuthorizationErrorf(
tx.Payer,
"payer account does not have sufficient signatures (%d < %d)",
envelopeWeights[tx.Payer],
keyWeightThreshold)
}
return nil
}
// getAccountKeys gets the signatures' account keys and populate the account
// keys into the signature continuation structs.
func (v *TransactionVerifier) getAccountKeys(
txnState storage.TransactionPreparer,
accounts environment.Accounts,
signatures []*signatureContinuation,
proposalKey flow.ProposalKey,
) error {
foundProposalSignature := false
for _, signature := range signatures {
accountKey, err := accounts.GetPublicKey(
signature.Address,
signature.KeyIndex)
if err != nil {
return signature.newError(err)
}
if accountKey.Revoked {
return signature.newError(
fmt.Errorf("account key has been revoked"))
}
signature.accountKey = accountKey
if !foundProposalSignature && signature.matches(proposalKey) {
foundProposalSignature = true
}
}
if !foundProposalSignature {
return fmt.Errorf(
"either the payload or the envelope should provide proposal " +
"signatures")
}
return nil
}
// verifyAccountSignatures verifies the given signature continuations and
// aggregate the valid signatures' weights.
func (v *TransactionVerifier) verifyAccountSignatures(
signatures []*signatureContinuation,
) error {
toVerifyChan := make(chan *signatureContinuation, len(signatures))
verifiedChan := make(chan *signatureContinuation, len(signatures))
verificationConcurrency := v.VerificationConcurrency
if len(signatures) < verificationConcurrency {
verificationConcurrency = len(signatures)
}
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
wg := sync.WaitGroup{}
wg.Add(verificationConcurrency)
for i := 0; i < verificationConcurrency; i++ {
go func() {
defer wg.Done()
for entry := range toVerifyChan {
err := entry.verify()
verifiedChan <- entry
if err != nil {
// Signal to other workers to early exit
cancel()
return
}
select {
case <-ctx.Done():
// Another worker has error-ed out.
return
default:
// continue
}
}
}()
}
for _, entry := range signatures {
toVerifyChan <- entry
}
close(toVerifyChan)
foundError := false
for i := 0; i < len(signatures); i++ {
entry := <-verifiedChan
if !entry.invokedVerify {
// This is a programming error.
return fmt.Errorf("signatureContinuation.verify not called")
}
if entry.verifyErr != nil {
// Unfortunately, we cannot return the first error we received
// from the verifiedChan since the entries may be out of order,
// which could lead to non-deterministic error output.
foundError = true
break
}
entry.aggregateWeights[entry.Address] += entry.accountKey.Weight
}
if !foundError {
return nil
}
// We need to wait for all workers to finish in order to deterministically
// return the first error with respect to the signatures slice.
wg.Wait()
for _, entry := range signatures {
if entry.verifyErr != nil {
return entry.verifyErr
}
}
panic("Should never reach here")
}
func (v *TransactionVerifier) hasSufficientKeyWeight(
weights map[flow.Address]int,
address flow.Address,
keyWeightThreshold int,
) bool {
return weights[address] >= keyWeightThreshold
}