-
Notifications
You must be signed in to change notification settings - Fork 208
Expand file tree
/
Copy pathtransactionSequenceNum.go
More file actions
89 lines (71 loc) · 2.24 KB
/
transactionSequenceNum.go
File metadata and controls
89 lines (71 loc) · 2.24 KB
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
package fvm
import (
"fmt"
"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/module/trace"
)
type TransactionSequenceNumberChecker struct{}
func (c TransactionSequenceNumberChecker) CheckAndIncrementSequenceNumber(
tracer tracing.TracerSpan,
proc *TransactionProcedure,
txnState storage.TransactionPreparer,
) error {
// TODO(Janez): verification is part of inclusion fees, not execution fees.
var err error
txnState.RunWithMeteringDisabled(func() {
err = c.checkAndIncrementSequenceNumber(tracer, proc, txnState)
})
if err != nil {
return fmt.Errorf("checking sequence number failed: %w", err)
}
return nil
}
func (c TransactionSequenceNumberChecker) checkAndIncrementSequenceNumber(
tracer tracing.TracerSpan,
proc *TransactionProcedure,
txnState storage.TransactionPreparer,
) error {
defer tracer.StartChildSpan(trace.FVMSeqNumCheckTransaction).End()
nestedTxnId, err := txnState.BeginNestedTransaction()
if err != nil {
return err
}
defer func() {
_, commitError := txnState.CommitNestedTransaction(nestedTxnId)
if commitError != nil {
panic(commitError)
}
}()
accounts := environment.NewAccounts(txnState)
proposalKey := proc.Transaction.ProposalKey
revoked, err := accounts.GetAccountPublicKeyRevokedStatus(proposalKey.Address, proposalKey.KeyIndex)
if err != nil {
return errors.NewInvalidProposalSignatureError(proposalKey, err)
}
if revoked {
return errors.NewInvalidProposalSignatureError(
proposalKey,
fmt.Errorf("proposal key has been revoked"))
}
seqNumber, err := accounts.GetAccountPublicKeySequenceNumber(proposalKey.Address, proposalKey.KeyIndex)
if err != nil {
return err
}
// Note that proposal key verification happens at the txVerifier and not here.
valid := seqNumber == proposalKey.SequenceNumber
if !valid {
return errors.NewInvalidProposalSeqNumberError(proposalKey, seqNumber)
}
err = accounts.IncrementAccountPublicKeySequenceNumber(proposalKey.Address, proposalKey.KeyIndex)
if err != nil {
restartError := txnState.RestartNestedTransaction(nestedTxnId)
if restartError != nil {
panic(restartError)
}
return err
}
return nil
}