/
auth.go
71 lines (57 loc) · 2.24 KB
/
auth.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
package staking
import (
"context"
"fmt"
consensus "github.com/oasisprotocol/oasis-core/go/consensus/api"
"github.com/oasisprotocol/oasis-core/go/consensus/api/transaction"
"github.com/oasisprotocol/oasis-core/go/consensus/tendermint/api"
stakingState "github.com/oasisprotocol/oasis-core/go/consensus/tendermint/apps/staking/state"
staking "github.com/oasisprotocol/oasis-core/go/staking/api"
)
var _ api.TransactionAuthHandler = (*stakingApplication)(nil)
// Implements api.TransactionAuthHandler.
func (app *stakingApplication) GetSignerNonce(ctx context.Context, req *consensus.GetSignerNonceRequest) (uint64, error) {
q, err := app.QueryFactory().(*QueryFactory).QueryAt(ctx, req.Height)
if err != nil {
return 0, err
}
acct, err := q.Account(ctx, req.AccountAddress)
if err != nil {
return 0, err
}
return acct.General.Nonce, nil
}
// Implements api.TransactionAuthHandler.
func (app *stakingApplication) AuthenticateTx(ctx *api.Context, tx *transaction.Transaction) error {
return stakingState.AuthenticateAndPayFees(ctx, ctx.TxSigner(), tx.Nonce, tx.Fee)
}
// Implements api.TransactionAuthHandler.
func (app *stakingApplication) PostExecuteTx(ctx *api.Context, tx *transaction.Transaction) error {
if !ctx.IsCheckOnly() {
// Do not do anything outside CheckTx.
return nil
}
// The below fee and nonce updates are performed for CheckTx in case all other transaction
// checks passed and the transaction is ready to be included in the mempool. This should not be
// done earlier (e.g. in AuthenticateTx) as that could increment the nonce even for otherwise
// invalid transactions which will not be kept in the mempool (and so may be retried).
state := stakingState.NewMutableState(ctx.State())
fee := tx.Fee
if fee == nil {
fee = &transaction.Fee{}
}
addr := staking.NewAddress(ctx.TxSigner())
account, err := state.Account(ctx, addr)
if err != nil {
return fmt.Errorf("failed to fetch account state: %w", err)
}
// Deduct fee and increment the nonce.
if err := account.General.Balance.Sub(&fee.Amount); err != nil {
return transaction.ErrInsufficientFeeBalance
}
account.General.Nonce++
if err := state.SetAccount(ctx, addr, account); err != nil {
return fmt.Errorf("failed to set account: %w", err)
}
return nil
}