-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathapp_starter.go
297 lines (243 loc) · 8.17 KB
/
app_starter.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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
package starter
import (
"encoding/json"
"io"
"os"
abci "github.com/tendermint/tendermint/abci/types"
cmn "github.com/tendermint/tendermint/libs/common"
"github.com/tendermint/tendermint/libs/log"
pvm "github.com/tendermint/tendermint/privval"
tmtypes "github.com/tendermint/tendermint/types"
dbm "github.com/tendermint/tm-db"
bam "github.com/cosmos/cosmos-sdk/baseapp"
"github.com/cosmos/cosmos-sdk/codec"
"github.com/cosmos/cosmos-sdk/server"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/module"
"github.com/cosmos/cosmos-sdk/x/auth"
"github.com/cosmos/cosmos-sdk/x/bank"
"github.com/cosmos/cosmos-sdk/x/genaccounts"
"github.com/cosmos/cosmos-sdk/x/genutil"
"github.com/cosmos/cosmos-sdk/x/params"
"github.com/cosmos/cosmos-sdk/x/supply"
)
// nolint
var (
ModuleBasics module.BasicManager
Cdc *codec.Codec
DefaultCLIHome = os.ExpandEnv("$HOME/.tcli")
DefaultNodeHome = os.ExpandEnv("$HOME/.tcd")
maccPerms = map[string][]string{
auth.FeeCollectorName: nil,
}
)
//AppStarter is a drop in to make simple real world blockchains
func init() {
ModuleBasics = module.NewBasicManager(
genaccounts.AppModuleBasic{},
auth.AppModuleBasic{},
bank.AppModuleBasic{},
params.AppModuleBasic{},
supply.AppModuleBasic{},
)
}
// AppStarter is a basic app
type AppStarter struct {
*bam.BaseApp // AppStarter extends BaseApp
// Keys to access the substores
keyMain *sdk.KVStoreKey
keyAccount *sdk.KVStoreKey
keySupply *sdk.KVStoreKey
keyParams *sdk.KVStoreKey
tkeyParams *sdk.TransientStoreKey
// Keepers
accountKeeper auth.AccountKeeper
bankKeeper bank.Keeper
supplyKeeper supply.Keeper
paramsKeeper params.Keeper
Cdc *codec.Codec
Mm *module.Manager
}
// AppStarter implements abci.Application
var _ abci.Application = AppStarter{}
// MakeCodec registers the structs for encoding in amino
func MakeCodec() *codec.Codec {
cdc := codec.New()
ModuleBasics.RegisterCodec(cdc)
sdk.RegisterCodec(cdc)
codec.RegisterCrypto(cdc)
Cdc = cdc
return cdc
}
// InitChainer is called by Tendermint to start the chain.
func (app *AppStarter) InitChainer(ctx sdk.Context, req abci.RequestInitChain) abci.ResponseInitChain {
config := server.NewDefaultContext().Config
config.SetRoot(DefaultNodeHome)
server.UpgradeOldPrivValFile(config)
_, _, err := genutil.InitializeNodeValidatorFiles(config)
if err != nil {
panic(err)
}
/*
---------------------NOTICE-----------------------
vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
The code below is a hack to override the
functionality of tendermint. It is done here like
this so that you can focus on building fun custom
modules instead of all the necessary plumbing
required when building a production-ready app with
proof-of-stake. Don't worry about it now but
PLEASE NOTE that this is NOT BEST PRACTICE!
vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
---------------------------------------------------
*/
privValidator := pvm.LoadOrGenFilePV(
config.PrivValidatorKeyFile(), config.PrivValidatorStateFile())
valPubKey := tmtypes.TM2PB.PubKey(privValidator.GetPubKey())
update := abci.ValidatorUpdate{
PubKey: valPubKey,
Power: 100}
var genesisState GenesisState
err = app.Cdc.UnmarshalJSON(req.AppStateBytes, &genesisState)
if err != nil {
panic(err)
}
genesis := app.Mm.InitGenesis(ctx, genesisState)
genesis.Validators = append(genesis.Validators, update)
return genesis
}
// BeginBlocker runs before each block is committed.
func (app *AppStarter) BeginBlocker(ctx sdk.Context, req abci.RequestBeginBlock) abci.ResponseBeginBlock {
return app.Mm.BeginBlock(ctx, req)
}
// EndBlocker runs after each block is committed.
func (app *AppStarter) EndBlocker(ctx sdk.Context, req abci.RequestEndBlock) abci.ResponseEndBlock {
return app.Mm.EndBlock(ctx, req)
}
// LoadHeight loads the state at a given block height
func (app *AppStarter) LoadHeight(height int64) error {
return app.LoadVersion(height, app.keyMain)
}
// ExportAppStateAndValidators returns the Genesis and AppState for the apps modules
func (app *AppStarter) ExportAppStateAndValidators(forZeroHeight bool, jailWhiteList []string,
) (appState json.RawMessage, validators []tmtypes.GenesisValidator, err error) {
ctx := app.NewContext(true, abci.Header{Height: app.LastBlockHeight()})
genState := app.Mm.ExportGenesis(ctx)
appState, err = codec.MarshalJSONIndent(app.Cdc, genState)
if err != nil {
return nil, nil, err
}
return appState, validators, nil
}
// BuildModuleBasics adds more moduleBasics to the app
func BuildModuleBasics(moduleBasics ...module.AppModuleBasic) {
for _, mb := range moduleBasics {
ModuleBasics[mb.Name()] = mb
}
Cdc = MakeCodec()
}
// NewAppStarter created a basic app with bank, auth, supply and any other ModuleBasics passed to it
func NewAppStarter(appName string, logger log.Logger, db dbm.DB, moduleBasics ...module.AppModuleBasic) *AppStarter {
BuildModuleBasics(moduleBasics...)
Cdc = MakeCodec()
bApp := bam.NewBaseApp(appName, logger, db, auth.DefaultTxDecoder(Cdc))
var app = &AppStarter{
Cdc: Cdc,
BaseApp: bApp,
keyMain: sdk.NewKVStoreKey(bam.MainStoreKey),
keySupply: sdk.NewKVStoreKey(supply.StoreKey),
keyAccount: sdk.NewKVStoreKey(auth.StoreKey),
keyParams: sdk.NewKVStoreKey(params.StoreKey),
tkeyParams: sdk.NewTransientStoreKey(params.TStoreKey),
Mm: &module.Manager{},
}
app.paramsKeeper = params.NewKeeper(app.Cdc, app.keyParams, app.tkeyParams, params.DefaultCodespace)
authSubspace := app.paramsKeeper.Subspace(auth.DefaultParamspace)
bankSupspace := app.paramsKeeper.Subspace(bank.DefaultParamspace)
app.accountKeeper = auth.NewAccountKeeper(
app.Cdc,
app.keyAccount,
authSubspace,
auth.ProtoBaseAccount,
)
app.bankKeeper = bank.NewBaseKeeper(
app.accountKeeper,
bankSupspace,
bank.DefaultCodespace,
app.ModuleAccountAddrs(),
)
app.supplyKeeper = supply.NewKeeper(
app.Cdc,
app.keySupply,
app.accountKeeper,
app.bankKeeper,
maccPerms)
app.Mm = module.NewManager(
genaccounts.NewAppModule(app.accountKeeper),
auth.NewAppModule(app.accountKeeper),
bank.NewAppModule(app.bankKeeper, app.accountKeeper),
)
return app
}
// GenesisState holds the genesis state data for every module
type GenesisState map[string]json.RawMessage
// NewDefaultGenesisState populates a GenesisState with each module's default
func NewDefaultGenesisState() GenesisState {
return ModuleBasics.DefaultGenesis()
}
// GetCodec returns the app's codec
func (app *AppStarter) GetCodec() *codec.Codec {
return app.Cdc
}
// ModuleAccountAddrs returns all the app's module account addresses.
func (app *AppStarter) ModuleAccountAddrs() map[string]bool {
modAccAddrs := make(map[string]bool)
for acc := range maccPerms {
modAccAddrs[supply.NewModuleAddress(acc).String()] = true
}
return modAccAddrs
}
// InitializeStarter configures the app. NOTE ModuleBasics must be complete before calling this
func (app *AppStarter) InitializeStarter() {
app.Mm.SetOrderInitGenesis(
genaccounts.ModuleName,
auth.ModuleName,
bank.ModuleName,
)
app.Mm.RegisterRoutes(app.Router(), app.QueryRouter())
app.SetInitChainer(app.InitChainer)
app.SetBeginBlocker(app.BeginBlocker)
app.SetEndBlocker(app.EndBlocker)
app.SetAnteHandler(
auth.NewAnteHandler(
app.accountKeeper,
app.supplyKeeper,
auth.DefaultSigVerificationGasConsumer,
),
)
app.MountStores(
app.keyMain,
app.keyAccount,
app.keySupply,
app.keyParams,
app.tkeyParams,
)
err := app.LoadLatestVersion(app.keyMain)
if err != nil {
cmn.Exit(err.Error())
}
}
// NewAppCreator wraps and returns a function for instantiaing an app
func NewAppCreator(creator func(log.Logger, dbm.DB) abci.Application) server.AppCreator {
return func(logger log.Logger, db dbm.DB, traceStore io.Writer) abci.Application {
app := creator(logger, db)
return app
}
}
// NewAppExporter wraps and returns a function for exporting application state
func NewAppExporter(creator func(log.Logger, dbm.DB) abci.Application) server.AppExporter {
return func(logger log.Logger, db dbm.DB, traceStore io.Writer, height int64,
forZeroHeight bool, jailWhiteList []string) (json.RawMessage, []tmtypes.GenesisValidator, error) {
return nil, nil, nil
}
}