-
Notifications
You must be signed in to change notification settings - Fork 179
/
transactionSequenceNum.go
89 lines (70 loc) · 2.2 KB
/
transactionSequenceNum.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
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/model/flow"
"github.com/onflow/flow-go/module/trace"
)
type TransactionSequenceNumberChecker struct{}
func (c TransactionSequenceNumberChecker) CheckAndIncrementSequenceNumber(
tracer tracing.TracerSpan,
proc *TransactionProcedure,
txnState storage.Transaction,
) error {
// TODO(Janez): verification is part of inclusion fees, not execution fees.
var err error
txnState.RunWithAllLimitsDisabled(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.Transaction,
) 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
var accountKey flow.AccountPublicKey
accountKey, err = accounts.GetPublicKey(proposalKey.Address, proposalKey.KeyIndex)
if err != nil {
return errors.NewInvalidProposalSignatureError(proposalKey, err)
}
if accountKey.Revoked {
return errors.NewInvalidProposalSignatureError(
proposalKey,
fmt.Errorf("proposal key has been revoked"))
}
// Note that proposal key verification happens at the txVerifier and not here.
valid := accountKey.SeqNumber == proposalKey.SequenceNumber
if !valid {
return errors.NewInvalidProposalSeqNumberError(proposalKey, accountKey.SeqNumber)
}
accountKey.SeqNumber++
_, err = accounts.SetPublicKey(proposalKey.Address, proposalKey.KeyIndex, accountKey)
if err != nil {
restartError := txnState.RestartNestedTransaction(nestedTxnId)
if restartError != nil {
panic(restartError)
}
return err
}
return nil
}