forked from swishlabsco/cosmos-ethereum-bridge
/
app.go
270 lines (223 loc) · 8.74 KB
/
app.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
package app
import (
"github.com/tendermint/tendermint/libs/log"
"github.com/cosmos/cosmos-sdk/simapp"
"github.com/cosmos/cosmos-sdk/codec"
"github.com/cosmos/cosmos-sdk/types/module"
"github.com/cosmos/cosmos-sdk/x/auth"
"github.com/cosmos/cosmos-sdk/x/auth/ante"
"github.com/cosmos/cosmos-sdk/x/genutil"
tmjson "github.com/tendermint/tendermint/libs/json"
authtype "github.com/cosmos/cosmos-sdk/x/auth/types"
authsims "github.com/cosmos/cosmos-sdk/x/auth/simulation"
authkeeper "github.com/cosmos/cosmos-sdk/x/auth/keeper"
"github.com/cosmos/cosmos-sdk/x/bank"
banktype "github.com/cosmos/cosmos-sdk/x/bank/types"
bankkeeper "github.com/cosmos/cosmos-sdk/x/bank/keeper"
"github.com/cosmos/cosmos-sdk/x/params"
paramstype "github.com/cosmos/cosmos-sdk/x/params/types"
paramskeeper "github.com/cosmos/cosmos-sdk/x/params/keeper"
"github.com/cosmos/cosmos-sdk/x/staking"
stakingtype "github.com/cosmos/cosmos-sdk/x/staking/types"
stakingkeeper "github.com/cosmos/cosmos-sdk/x/staking/keeper"
"github.com/pumpkinzomb/cosmos-ethereum-bridge/x/ethbridge"
"github.com/pumpkinzomb/cosmos-ethereum-bridge/x/oracle"
bam "github.com/cosmos/cosmos-sdk/baseapp"
sdk "github.com/cosmos/cosmos-sdk/types"
abci "github.com/tendermint/tendermint/abci/types"
tmos "github.com/tendermint/tendermint/libs/os"
dbm "github.com/tendermint/tm-db"
)
const (
appName = "ethereum-bridge"
)
var (
// DefaultNodeHome default home directories for the application daemon
DefaultNodeHome string
// ModuleBasics defines the module BasicManager is in charge of setting up basic,
// non-dependant module elements, such as codec registration
// and genesis verification.
ModuleBasics = module.NewBasicManager(
auth.AppModuleBasic{},
genutil.AppModuleBasic{},
bank.AppModuleBasic{},
staking.AppModuleBasic{},
params.AppModuleBasic{},
)
// module account permissions
maccPerms = map[string][]string{
authtype.FeeCollectorName: nil,
stakingtype.BondedPoolName: {authtype.Burner, authtype.Staking},
stakingtype.NotBondedPoolName: {authtype.Burner, authtype.Staking},
}
)
type ethereumBridgeApp struct {
*bam.BaseApp
appCodec codec.Marshaler
legacyAmino *codec.LegacyAmino
cdc codec.Marshaler
// keys to access the substores
keys map[string]*sdk.KVStoreKey
tkeys map[string]*sdk.TransientStoreKey
accountKeeper authkeeper.AccountKeeper
bankKeeper bankkeeper.Keeper
stakingKeeper stakingkeeper.Keeper
paramsKeeper paramskeeper.Keeper
oracleKeeper oracle.Keeper
// the module manager
mm *module.Manager
}
// NewEthereumBridgeApp is a constructor function for ethereumBridgeApp
func NewEthereumBridgeApp(logger log.Logger, db dbm.DB) *ethereumBridgeApp {
// First define the top level codec that will be shared by the different modules
encodingConfig := simapp.MakeTestEncodingConfig()
appCodec := encodingConfig.Marshaler
legacyAmino := encodingConfig.Amino
// BaseApp handles interactions with Tendermint through the ABCI protocol
bApp := bam.NewBaseApp(appName, logger, db, encodingConfig.TxConfig.TxDecoder())
keys := sdk.NewKVStoreKeys(
authtype.StoreKey,
banktype.StoreKey,
stakingtype.StoreKey,
paramstype.StoreKey,
oracle.StoreKey,
)
tkeys := sdk.NewTransientStoreKeys(paramstype.TStoreKey)
// Here you initialize your application with the store keys it requires
var app = ðereumBridgeApp{
BaseApp: bApp,
appCodec: appCodec,
legacyAmino: legacyAmino,
cdc: appCodec,
keys: keys,
tkeys: tkeys,
}
// The ParamsKeeper handles parameter storage for the application
app.paramsKeeper = initParamsKeeper(appCodec, legacyAmino, keys[paramstype.StoreKey], tkeys[paramstype.TStoreKey])
// The AccountKeeper handles address -> account lookups
app.accountKeeper = authkeeper.NewAccountKeeper(
appCodec,
keys[authtype.StoreKey],
app.GetSubspace(authtype.ModuleName),
authtype.ProtoBaseAccount,
maccPerms,
)
// The BankKeeper allows you perform sdk.Coins interactions
app.bankKeeper = bankkeeper.NewBaseKeeper(
appCodec,
keys[banktype.StoreKey],
app.accountKeeper,
app.GetSubspace(banktype.ModuleName),
app.ModuleAccountAddrs(),
)
app.stakingKeeper = stakingkeeper.NewKeeper(
appCodec,
keys[stakingtype.StoreKey],
app.accountKeeper,
app.bankKeeper,
app.GetSubspace(stakingtype.ModuleName),
)
// The OracleKeeper is the Keeper from the oracle module
// It handles interactions with the oracle store
oracleKeeper, oracleErr := oracle.NewKeeper(
app.stakingKeeper,
keys[oracle.StoreKey],
legacyAmino,
app.GetSubspace(oracle.ModuleName),
oracle.DefaultConsensusNeeded,
)
if oracleErr != nil {
tmos.Exit(oracleErr.Error())
}
app.oracleKeeper = oracleKeeper
// NOTE: Any module instantiated in the module manager that is later modified
// must be passed by reference here.
app.mm = module.NewManager(
genutil.NewAppModule(
app.accountKeeper, app.stakingKeeper, app.BaseApp.DeliverTx,
encodingConfig.TxConfig,
),
auth.NewAppModule(appCodec, app.accountKeeper, authsims.RandomGenesisAccounts),
bank.NewAppModule(appCodec, app.bankKeeper, app.accountKeeper),
staking.NewAppModule(appCodec, app.stakingKeeper, app.accountKeeper, app.bankKeeper),
params.NewAppModule(app.paramsKeeper),
)
// The AnteHandler handles signature verification and transaction pre-processing
// The app.Router is the main transaction router where each module registers its routes
// Register the bank route here
app.Router().
AddRoute(sdk.NewRoute(banktype.RouterKey, bank.NewHandler(app.bankKeeper))).
AddRoute(sdk.NewRoute(stakingtype.RouterKey, staking.NewHandler(app.stakingKeeper))).
AddRoute(sdk.NewRoute(ethbridge.RouterKey, ethbridge.NewHandler(app.oracleKeeper, app.bankKeeper, app.cdc, ethbridge.DefaultCodespace)))
// The app.QueryRouter is the main query router where each module registers its routes
app.QueryRouter().
AddRoute(authtype.QuerierRoute, authkeeper.NewQuerier(app.accountKeeper, app.legacyAmino)).
AddRoute(stakingtype.QuerierRoute, stakingkeeper.NewQuerier(app.stakingKeeper, app.legacyAmino)).
AddRoute(ethbridge.QuerierRoute, ethbridge.NewQuerier(app.oracleKeeper, app.legacyAmino, ethbridge.DefaultCodespace))
// The initChainer handles translating the genesis.json file into initial state for the network
app.SetInitChainer(app.InitChainer)
app.SetBeginBlocker(app.BeginBlocker)
app.SetAnteHandler(
ante.NewAnteHandler(
app.accountKeeper,
app.bankKeeper,
ante.DefaultSigVerificationGasConsumer,
encodingConfig.TxConfig.SignModeHandler(),
),
)
app.SetEndBlocker(app.EndBlocker)
if err := app.LoadLatestVersion(); err != nil {
tmos.Exit(err.Error())
}
return app
}
// MakeCodecs constructs the *std.Codec and *codec.LegacyAmino instances used by
// simapp. It is useful for tests and clients who do not want to construct the
// full simapp
func MakeCodecs() (codec.Marshaler, *codec.LegacyAmino) {
config := simapp.MakeTestEncodingConfig()
return config.Marshaler, config.Amino
}
// BeginBlocker application updates every begin block
func (app *ethereumBridgeApp) BeginBlocker(ctx sdk.Context, req abci.RequestBeginBlock) abci.ResponseBeginBlock {
return app.mm.BeginBlock(ctx, req)
}
// EndBlocker application updates every end block
func (app *ethereumBridgeApp) EndBlocker(ctx sdk.Context, req abci.RequestEndBlock) abci.ResponseEndBlock {
return app.mm.EndBlock(ctx, req)
}
// InitChainer application update at chain initialization
func (app *ethereumBridgeApp) InitChainer(ctx sdk.Context, req abci.RequestInitChain) abci.ResponseInitChain {
var genesisState GenesisState
if err := tmjson.Unmarshal(req.AppStateBytes, &genesisState); err != nil {
panic(err)
}
return app.mm.InitGenesis(ctx, app.appCodec, genesisState)
}
// load a particular height
func (app *ethereumBridgeApp) LoadHeight(height int64) error {
return app.LoadVersion(height)
}
// GetSubspace returns a param subspace for a given module name.
//
// NOTE: This is solely to be used for testing purposes.
func (app *ethereumBridgeApp) GetSubspace(moduleName string) paramstype.Subspace {
subspace, _ := app.paramsKeeper.GetSubspace(moduleName)
return subspace
}
// ModuleAccountAddrs returns all the app's module account addresses.
func (app *ethereumBridgeApp) ModuleAccountAddrs() map[string]bool {
modAccAddrs := make(map[string]bool)
for acc := range maccPerms {
modAccAddrs[authtype.NewModuleAddress(acc).String()] = true
}
return modAccAddrs
}
// initParamsKeeper init params keeper and its subspaces
func initParamsKeeper(appCodec codec.BinaryMarshaler, legacyAmino *codec.LegacyAmino, key, tkey sdk.StoreKey) paramskeeper.Keeper {
paramsKeeper := paramskeeper.NewKeeper(appCodec, legacyAmino, key, tkey)
paramsKeeper.Subspace(authtype.ModuleName)
paramsKeeper.Subspace(banktype.ModuleName)
paramsKeeper.Subspace(stakingtype.ModuleName)
return paramsKeeper
}