forked from zigbee-alliance/distributed-compliance-ledger
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.go
283 lines (226 loc) · 8.35 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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
// Copyright 2020 DSR Corporation
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package app
import (
"encoding/json"
"os"
bam "github.com/cosmos/cosmos-sdk/baseapp"
"github.com/cosmos/cosmos-sdk/codec"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/module"
"github.com/cosmos/cosmos-sdk/version"
authutils "github.com/cosmos/cosmos-sdk/x/auth"
"github.com/cosmos/cosmos-sdk/x/params"
abci "github.com/tendermint/tendermint/abci/types"
cmn "github.com/tendermint/tendermint/libs/common"
"github.com/tendermint/tendermint/libs/log"
tmtypes "github.com/tendermint/tendermint/types"
dbm "github.com/tendermint/tm-db"
"github.com/zigbee-alliance/distributed-compliance-ledger/x/auth"
"github.com/zigbee-alliance/distributed-compliance-ledger/x/compliance"
"github.com/zigbee-alliance/distributed-compliance-ledger/x/compliancetest"
"github.com/zigbee-alliance/distributed-compliance-ledger/x/genutil"
"github.com/zigbee-alliance/distributed-compliance-ledger/x/modelinfo"
"github.com/zigbee-alliance/distributed-compliance-ledger/x/pki"
"github.com/zigbee-alliance/distributed-compliance-ledger/x/validator"
)
const appName = "dc-ledger"
var (
// default home directories for the application CLI.
DefaultCLIHome = os.ExpandEnv("$HOME/.dclcli")
// DefaultNodeHome sets the folder where the applcation data and configuration will be stored.
DefaultNodeHome = os.ExpandEnv("$HOME/.dcld")
)
// ModuleBasics is in charge of setting up basic module elemnets.
var ModuleBasics = module.NewBasicManager(
auth.AppModuleBasic{},
validator.AppModuleBasic{},
genutil.AppModuleBasic{},
modelinfo.AppModuleBasic{},
compliance.AppModuleBasic{},
compliancetest.AppModuleBasic{},
pki.AppModuleBasic{},
)
// MakeCodec generates the necessary codecs for Amino.
func MakeCodec() *codec.Codec {
cdc := codec.New()
ModuleBasics.RegisterCodec(cdc)
sdk.RegisterCodec(cdc)
codec.RegisterCrypto(cdc)
return cdc
}
type dcLedgerApp struct {
*bam.BaseApp
cdc *codec.Codec
// keys to access the substores
keys map[string]*sdk.KVStoreKey
tkeys map[string]*sdk.TransientStoreKey
// Keepers
authKeeper auth.Keeper
validatorKeeper validator.Keeper
modelinfoKeeper modelinfo.Keeper
pkiKeeper pki.Keeper
complianceKeeper compliance.Keeper
compliancetestKeeper compliancetest.Keeper
// Module Manager
mm *module.Manager
}
// NewDcLedgerApp is a constructor function for dcLedgerApp.
func NewDcLedgerApp(logger log.Logger, db dbm.DB, baseAppOptions ...func(*bam.BaseApp)) *dcLedgerApp {
// First define the top level codec that will be shared by the different modules
cdc := MakeCodec()
// BaseApp handles interactions with Tendermint through the ABCI protocol
bApp := bam.NewBaseApp(appName, logger, db, authutils.DefaultTxDecoder(cdc), baseAppOptions...)
bApp.SetAppVersion(version.Version)
keys := sdk.NewKVStoreKeys(bam.MainStoreKey, auth.StoreKey, validator.StoreKey,
modelinfo.StoreKey, compliance.StoreKey, compliancetest.StoreKey, pki.StoreKey)
tkeys := sdk.NewTransientStoreKeys(params.TStoreKey)
// Here you initialize your application with the store keys it requires.
app := &dcLedgerApp{
BaseApp: bApp,
cdc: cdc,
keys: keys,
tkeys: tkeys,
}
InitKeepers(app, keys)
InitModuleManager(app)
// The initChainer handles translating the genesis.json file into initial state for the network.
app.SetInitChainer(app.InitChainer)
app.SetBeginBlocker(app.BeginBlocker)
app.SetEndBlocker(app.EndBlocker)
// The AnteHandler handles signature verification and transaction pre-processing.
app.SetAnteHandler(
auth.NewAnteHandler(
app.authKeeper,
auth.DefaultSigVerificationGasConsumer,
),
)
// initialize stores
app.MountKVStores(keys)
app.MountTransientStores(tkeys)
err := app.LoadLatestVersion(app.keys[bam.MainStoreKey])
if err != nil {
cmn.Exit(err.Error())
}
return app
}
func InitModuleManager(app *dcLedgerApp) {
app.mm = module.NewManager(
genutil.NewAppModule(app.authKeeper, app.validatorKeeper, app.BaseApp.DeliverTx),
auth.NewAppModule(app.authKeeper),
validator.NewAppModule(app.validatorKeeper, app.authKeeper),
modelinfo.NewAppModule(app.modelinfoKeeper, app.authKeeper),
compliance.NewAppModule(app.complianceKeeper, app.modelinfoKeeper, app.compliancetestKeeper, app.authKeeper),
compliancetest.NewAppModule(app.compliancetestKeeper, app.authKeeper, app.modelinfoKeeper),
pki.NewAppModule(app.pkiKeeper, app.authKeeper),
)
app.mm.SetOrderBeginBlockers(validator.ModuleName)
app.mm.SetOrderEndBlockers(validator.ModuleName)
app.mm.SetOrderInitGenesis(
auth.ModuleName,
validator.ModuleName,
modelinfo.ModuleName,
compliance.ModuleName,
compliancetest.ModuleName,
pki.ModuleName,
genutil.ModuleName,
)
// register all module routes and module queriers
app.mm.RegisterRoutes(app.Router(), app.QueryRouter())
}
func InitKeepers(app *dcLedgerApp, keys map[string]*sdk.KVStoreKey) {
// The Validator keeper
app.validatorKeeper = MakeValidatorKeeper(keys, app)
// The ModelinfoKeeper keeper
app.modelinfoKeeper = MakeModelinfoKeeper(keys, app)
// The ComplianceKeeper keeper
app.complianceKeeper = MakeComplianceKeeper(keys, app)
// The CompliancetestKeeper keeper
app.compliancetestKeeper = MakeCompliancetestKeeper(keys, app)
// The PKI keeper
app.pkiKeeper = MakePkiKeeper(keys, app)
// The AuthKeeper keeper
app.authKeeper = MakeAuthKeeper(keys, app)
}
func MakeAuthKeeper(keys map[string]*sdk.KVStoreKey, app *dcLedgerApp) auth.Keeper {
return auth.NewKeeper(
keys[auth.StoreKey],
app.cdc,
)
}
func MakeModelinfoKeeper(keys map[string]*sdk.KVStoreKey, app *dcLedgerApp) modelinfo.Keeper {
return modelinfo.NewKeeper(
keys[modelinfo.StoreKey],
app.cdc,
)
}
func MakeComplianceKeeper(keys map[string]*sdk.KVStoreKey, app *dcLedgerApp) compliance.Keeper {
return compliance.NewKeeper(
keys[compliance.StoreKey],
app.cdc,
)
}
func MakeCompliancetestKeeper(keys map[string]*sdk.KVStoreKey, app *dcLedgerApp) compliancetest.Keeper {
return compliancetest.NewKeeper(
keys[compliancetest.StoreKey],
app.cdc,
)
}
func MakePkiKeeper(keys map[string]*sdk.KVStoreKey, app *dcLedgerApp) pki.Keeper {
return pki.NewKeeper(
keys[pki.StoreKey],
app.cdc,
)
}
func MakeValidatorKeeper(keys map[string]*sdk.KVStoreKey, app *dcLedgerApp) validator.Keeper {
return validator.NewKeeper(
keys[validator.StoreKey],
app.cdc,
)
}
// GenesisState represents chain state at the start of the chain. Any initial state (account balances) are stored here.
type GenesisState map[string]json.RawMessage
func NewDefaultGenesisState() GenesisState {
return ModuleBasics.DefaultGenesis()
}
func (app *dcLedgerApp) InitChainer(ctx sdk.Context, req abci.RequestInitChain) abci.ResponseInitChain {
var genesisState GenesisState
err := app.cdc.UnmarshalJSON(req.AppStateBytes, &genesisState)
if err != nil {
panic(err)
}
return app.mm.InitGenesis(ctx, genesisState)
}
func (app *dcLedgerApp) BeginBlocker(ctx sdk.Context, req abci.RequestBeginBlock) abci.ResponseBeginBlock {
return app.mm.BeginBlock(ctx, req)
}
func (app *dcLedgerApp) EndBlocker(ctx sdk.Context, req abci.RequestEndBlock) abci.ResponseEndBlock {
return app.mm.EndBlock(ctx, req)
}
func (app *dcLedgerApp) LoadHeight(height int64) error {
return app.LoadVersion(height, app.keys[bam.MainStoreKey])
}
//_________________________________________________________
func (app *dcLedgerApp) ExportAppStateAndValidators(forZeroHeight bool, jailWhiteList []string,
) (appState json.RawMessage, validators []tmtypes.GenesisValidator, err error) {
// as if they could withdraw from the start of the next block
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
}
validators = validator.WriteValidators(ctx, app.validatorKeeper)
return appState, validators, nil
}