-
Notifications
You must be signed in to change notification settings - Fork 3.5k
/
config.go
109 lines (93 loc) · 3.08 KB
/
config.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
package tx
import (
"fmt"
txconfigv1 "cosmossdk.io/api/cosmos/tx/config/v1"
"cosmossdk.io/core/appmodule"
"cosmossdk.io/depinject"
"github.com/cosmos/cosmos-sdk/baseapp"
"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/codec"
"github.com/cosmos/cosmos-sdk/runtime"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/auth/ante"
"github.com/cosmos/cosmos-sdk/x/auth/posthandler"
"github.com/cosmos/cosmos-sdk/x/auth/tx"
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
feegrantkeeper "github.com/cosmos/cosmos-sdk/x/feegrant/keeper"
)
func init() {
appmodule.Register(&txconfigv1.Config{},
appmodule.Provide(ProvideModule),
)
}
type TxInputs struct {
depinject.In
Config *txconfigv1.Config
ProtoCodecMarshaler codec.ProtoCodecMarshaler
AccountKeeper ante.AccountKeeper `optional:"true"`
BankKeeper authtypes.BankKeeper `optional:"true"`
FeeGrantKeeper feegrantkeeper.Keeper `optional:"true"`
}
type TxOutputs struct {
depinject.Out
TxConfig client.TxConfig
BaseAppOption runtime.BaseAppOption
}
func ProvideModule(in TxInputs) TxOutputs {
txConfig := tx.NewTxConfig(in.ProtoCodecMarshaler, tx.DefaultSignModes)
baseAppOption := func(app *baseapp.BaseApp) {
// AnteHandlers
if !in.Config.SkipAnteHandler {
anteHandler, err := newAnteHandler(txConfig, in)
if err != nil {
panic(err)
}
app.SetAnteHandler(anteHandler)
}
// PostHandlers
if !in.Config.SkipPostHandler {
// In v0.46, the SDK introduces _postHandlers_. PostHandlers are like
// antehandlers, but are run _after_ the `runMsgs` execution. They are also
// defined as a chain, and have the same signature as antehandlers.
//
// In baseapp, postHandlers are run in the same store branch as `runMsgs`,
// meaning that both `runMsgs` and `postHandler` state will be committed if
// both are successful, and both will be reverted if any of the two fails.
//
// The SDK exposes a default empty postHandlers chain.
//
// Please note that changing any of the anteHandler or postHandler chain is
// likely to be a state-machine breaking change, which needs a coordinated
// upgrade.
postHandler, err := posthandler.NewPostHandler(
posthandler.HandlerOptions{},
)
if err != nil {
panic(err)
}
app.SetPostHandler(postHandler)
}
// TxDecoder/TxEncoder
app.SetTxDecoder(txConfig.TxDecoder())
app.SetTxEncoder(txConfig.TxEncoder())
}
return TxOutputs{TxConfig: txConfig, BaseAppOption: baseAppOption}
}
func newAnteHandler(txConfig client.TxConfig, in TxInputs) (sdk.AnteHandler, error) {
if in.BankKeeper == nil {
return nil, fmt.Errorf("both AccountKeeper and BankKeeper are required")
}
anteHandler, err := ante.NewAnteHandler(
ante.HandlerOptions{
AccountKeeper: in.AccountKeeper,
BankKeeper: in.BankKeeper,
SignModeHandler: txConfig.SignModeHandler(),
FeegrantKeeper: in.FeeGrantKeeper,
SigGasConsumer: ante.DefaultSigVerificationGasConsumer,
},
)
if err != nil {
return nil, fmt.Errorf("failed to create ante handler: %w", err)
}
return anteHandler, nil
}