This repository has been archived by the owner on Nov 7, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.go
215 lines (168 loc) · 6.37 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
//
// Copyright 2018 Wireline, Inc.
//
package app
import (
"encoding/json"
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/x/auth"
"github.com/cosmos/cosmos-sdk/x/bank"
"github.com/cosmos/cosmos-sdk/x/stake"
abci "github.com/tendermint/tendermint/abci/types"
cmn "github.com/tendermint/tendermint/libs/common"
dbm "github.com/tendermint/tendermint/libs/db"
"github.com/tendermint/tendermint/libs/log"
tmtypes "github.com/tendermint/tendermint/types"
"github.com/wirelineio/registry/x/htlc"
"github.com/wirelineio/registry/x/multisig"
msighandler "github.com/wirelineio/registry/x/multisig/handlers"
"github.com/wirelineio/registry/x/registry"
"github.com/wirelineio/registry/x/utxo"
"github.com/wirelineio/registry/x/registry/gql"
)
const (
appName = "registry"
)
type registryApp struct {
*bam.BaseApp
cdc *codec.Codec
keyMain *sdk.KVStoreKey
keyAccount *sdk.KVStoreKey
keyFeeCollection *sdk.KVStoreKey
keyTxStore *sdk.KVStoreKey
keyHtlcStore *sdk.KVStoreKey
keyMultisigStore *sdk.KVStoreKey
keyAccUtxoStore *sdk.KVStoreKey
keyUtxoStore *sdk.KVStoreKey
keyRegStore *sdk.KVStoreKey
accountKeeper auth.AccountKeeper
bankKeeper bank.Keeper
feeCollectionKeeper auth.FeeCollectionKeeper
htlcKeeper htlc.Keeper
multisigKeeper msighandler.Keeper
utxoKeeper utxo.Keeper
regKeeper registry.Keeper
}
// NewRegistryApp is a constructor function for registryApp
func NewRegistryApp(logger log.Logger, db dbm.DB) *registryApp {
// 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, auth.DefaultTxDecoder(cdc))
// bApp.SetMinimumFees(sdk.Coins{sdk.NewInt64Coin("wire", 1)})
// Here you initialize your application with the store keys it requires
var app = ®istryApp{
BaseApp: bApp,
cdc: cdc,
keyMain: sdk.NewKVStoreKey("main"),
keyAccount: sdk.NewKVStoreKey("acc"),
keyFeeCollection: sdk.NewKVStoreKey("fee_collection"),
keyTxStore: sdk.NewKVStoreKey("tx"),
keyHtlcStore: sdk.NewKVStoreKey("htlc"),
keyMultisigStore: sdk.NewKVStoreKey("multisig"),
keyAccUtxoStore: sdk.NewKVStoreKey("acc_utxo"),
keyUtxoStore: sdk.NewKVStoreKey("utxo"),
keyRegStore: sdk.NewKVStoreKey("registry"),
}
// The AccountKeeper handles address -> account lookups
app.accountKeeper = auth.NewAccountKeeper(
app.cdc,
app.keyAccount,
auth.ProtoBaseAccount,
)
// The BankKeeper allows you perform sdk.Coins interactions
app.bankKeeper = bank.NewBaseKeeper(app.accountKeeper)
// The FeeCollectionKeeper collects transaction fees and renders them to the fee distribution module
app.feeCollectionKeeper = auth.NewFeeCollectionKeeper(cdc, app.keyFeeCollection)
app.htlcKeeper = htlc.NewKeeper(app.bankKeeper, app.keyHtlcStore, app.cdc)
app.multisigKeeper = msighandler.NewKeeper(app.bankKeeper, app.keyMultisigStore, app.cdc)
app.utxoKeeper = utxo.NewKeeper(app.accountKeeper, app.bankKeeper, app.keyAccUtxoStore, app.keyUtxoStore, app.keyTxStore, app.cdc)
app.regKeeper = registry.NewKeeper(app.accountKeeper, app.bankKeeper, app.keyRegStore, app.cdc)
// The AnteHandler handles signature verification and transaction pre-processing
app.SetAnteHandler(auth.NewAnteHandler(app.accountKeeper, app.feeCollectionKeeper))
// The app.Router is the main transaction router where each module registers its routes
// Register the bank and registry routes here
app.Router().
AddRoute("bank", bank.NewHandler(app.bankKeeper)).
AddRoute("htlc", htlc.NewHandler(app.htlcKeeper)).
AddRoute("multisig", msighandler.NewHandler(app.multisigKeeper)).
AddRoute("utxo", utxo.NewHandler(app.utxoKeeper)).
AddRoute("registry", registry.NewHandler(app.regKeeper))
// The app.QueryRouter is the main query router where each module registers its routes
app.QueryRouter().
AddRoute("multisig", msighandler.NewQuerier(app.multisigKeeper)).
AddRoute("utxo", registry.NewQuerier(app.regKeeper)).
AddRoute("registry", registry.NewQuerier(app.regKeeper))
// The initChainer handles translating the genesis.json file into initial state for the network
app.SetInitChainer(app.initChainer)
app.MountStores(
app.keyMain,
app.keyAccount,
app.keyTxStore,
app.keyFeeCollection,
app.keyHtlcStore,
app.keyMultisigStore,
app.keyAccUtxoStore,
app.keyUtxoStore,
app.keyRegStore,
)
err := app.LoadLatestVersion(app.keyMain)
if err != nil {
cmn.Exit(err.Error())
}
go gql.Server(app.BaseApp, app.cdc, app.regKeeper, app.accountKeeper)
return app
}
// GenesisState represents chain state at the start of the chain. Any initial state (account balances) are stored here.
type GenesisState struct {
Accounts []*auth.BaseAccount `json:"accounts"`
}
func (app *registryApp) initChainer(ctx sdk.Context, req abci.RequestInitChain) abci.ResponseInitChain {
stateJSON := req.AppStateBytes
genesisState := new(GenesisState)
err := app.cdc.UnmarshalJSON(stateJSON, genesisState)
if err != nil {
panic(err)
}
for _, acc := range genesisState.Accounts {
acc.AccountNumber = app.accountKeeper.GetNextAccountNumber(ctx)
app.accountKeeper.SetAccount(ctx, acc)
}
return abci.ResponseInitChain{}
}
// ExportAppStateAndValidators does the things
func (app *registryApp) ExportAppStateAndValidators() (appState json.RawMessage, validators []tmtypes.GenesisValidator, err error) {
ctx := app.NewContext(true, abci.Header{})
accounts := []*auth.BaseAccount{}
appendAccountsFn := func(acc auth.Account) bool {
account := &auth.BaseAccount{
Address: acc.GetAddress(),
Coins: acc.GetCoins(),
}
accounts = append(accounts, account)
return false
}
app.accountKeeper.IterateAccounts(ctx, appendAccountsFn)
genState := GenesisState{Accounts: accounts}
appState, err = codec.MarshalJSONIndent(app.cdc, genState)
if err != nil {
return nil, nil, err
}
return appState, validators, err
}
// MakeCodec generates the necessary codecs for Amino
func MakeCodec() *codec.Codec {
var cdc = codec.New()
auth.RegisterCodec(cdc)
bank.RegisterCodec(cdc)
stake.RegisterCodec(cdc)
sdk.RegisterCodec(cdc)
htlc.RegisterCodec(cdc)
multisig.RegisterCodec(cdc)
utxo.RegisterCodec(cdc)
registry.RegisterCodec(cdc)
codec.RegisterCrypto(cdc)
return cdc
}