Add My Module
下面是我具体添加mymodule的代码片段,具体细节见注释。
1
// Extended ABCI application
2
type GaiaApp struct {
3
*bam.BaseApp
4
cdc *wire.Codec
5
6
// keys to access the substores
7
...
8
// 1.添加自己的key
9
keyMymodule *sdk.KVStoreKey
10
11
// Manage getting and setting accounts
12
...
13
// 2.添加自己的keeper
14
mymoduleKeeper mymodule.Keeper
15
}
16
17
func NewGaiaApp(logger log.Logger, db dbm.DB) *GaiaApp {
18
cdc := MakeCodec()
19
20
// create your application object
21
var app = &GaiaApp{
22
...
23
// 3.初始化自己的key
24
keyMymodule: sdk.NewKVStoreKey("mymodule"),
25
}
26
27
// define the accountMapper
28
...
29
30
// add handlers
31
...
32
// 4.初始化自己的keeper
33
app.mymoduleKeeper = mymodule.NewKeeper(app.cdc, app.keyMymodule, app.coinKeeper,app.RegisterCodespace(mymodule.DefaultCodespace))
34
// register message routes
35
...
36
// 5.添加自己的module的handler
37
app.Router().AddRoute("mymodule",mymodule.NewHandler(app.mymoduleKeeper))
38
39
// initialize BaseApp
40
// 6.初始化区块链的时刻调用的handler
41
app.SetInitChainer(app.initChainer)
42
// 7.设置每次beginblock时刻调用的handler
43
app.SetBeginBlocker(gov.NewBeginBlocker(app.govKeeper))
44
// 8.设置每次endblock时刻调用的handler
45
app.SetEndBlocker(stake.NewEndBlocker(app.stakeKeeper))
46
// 9.添加自己的key到StoresIAVLtree中
47
app.MountStoresIAVL(app.keyMain, app.keyAccount, app.keyIBC, app.keyStake, app.keyGov,app.keyMymodule)
48
// 10.设置每次消息都会调用的handler
49
app.SetAnteHandler(auth.NewAnteHandler(app.accountMapper, stake.FeeHandler))
50
...
51
return app
52
}
53
54
// custom tx codec
55
func MakeCodec() *wire.Codec {
56
var cdc = wire.NewCodec()
57
...
58
// 11.注册自己的Wire
59
mymodule.RegisterWire(cdc)
60
return cdc
61
}
Copied!
Last modified 3yr ago
Copy link