-
Notifications
You must be signed in to change notification settings - Fork 3
/
app.go
151 lines (117 loc) · 3.71 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
package app
import (
"fmt"
"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/params"
abci "github.com/tendermint/tendermint/abci/types"
"github.com/tendermint/tendermint/libs/common"
"github.com/tendermint/tendermint/libs/db"
"github.com/tendermint/tendermint/libs/log"
"tic_tac_toe/x/tic_tac_toe"
)
const (
appName = "tic_tac_toe"
)
type App struct {
*baseapp.BaseApp
cdc *codec.Codec
logger log.Logger
// Storage keys
keyMain *sdk.KVStoreKey
paramsKeeper params.Keeper
accountKeeper auth.AccountKeeper
feeCollectionKeeper auth.FeeCollectionKeeper
keeper tic_tac_toe.Keeper
}
func NewApp(logger log.Logger, db db.DB) *App {
cdc := MakeDefaultCodec()
base := baseapp.NewBaseApp(appName, logger, db, auth.DefaultTxDecoder(cdc))
app := &App{
BaseApp: base,
cdc: cdc,
logger: logger,
keyMain: sdk.NewKVStoreKey("main"),
}
keyParams := sdk.NewKVStoreKey("params")
tkeyParams := sdk.NewTransientStoreKey("transient_params")
app.paramsKeeper = params.NewKeeper(
app.cdc,
keyParams,
tkeyParams,
)
keyAccount := sdk.NewKVStoreKey("acc")
// Uses default account struct
app.accountKeeper = auth.NewAccountKeeper(
app.cdc,
keyAccount,
app.paramsKeeper.Subspace(auth.DefaultParamspace),
auth.ProtoBaseAccount,
)
keyFeeCollection := sdk.NewKVStoreKey("fee_collection")
app.feeCollectionKeeper = auth.NewFeeCollectionKeeper(cdc, keyFeeCollection)
keyTicTacToe := sdk.NewKVStoreKey("tictactoe")
app.keeper = tic_tac_toe.NewKeeper(cdc, keyTicTacToe, app.accountKeeper)
app.Router().
AddRoute("tictactoe", tic_tac_toe.NewHandler(app.keeper))
app.QueryRouter().
AddRoute(auth.QuerierRoute, auth.NewQuerier(app.accountKeeper)).
AddRoute("tictactoe", tic_tac_toe.NewQuerier(app.keeper))
app.MountStores(
app.keyMain,
keyParams,
tkeyParams,
keyAccount,
keyTicTacToe,
keyFeeCollection,
)
app.SetInitChainer(app.initChainer)
if err := app.LoadLatestVersion(app.keyMain); err != nil {
common.Exit(err.Error())
}
return app
}
func (app *App) initChainer(ctx sdk.Context, req abci.RequestInitChain) abci.ResponseInitChain {
// The initial state as stored in genesis file
genesisStateJSON := req.AppStateBytes
genesisState := new(GenesisState)
if err := app.cdc.UnmarshalJSON(genesisStateJSON, genesisState); err != nil {
panic(fmt.Sprintf("Failed to unmarshal genesis state: %s", err))
}
if err := auth.ValidateGenesis(genesisState.AuthState); err != nil {
panic(fmt.Sprintf("Invalid genesis auth state: %s", err))
}
auth.InitGenesis(ctx, app.accountKeeper, app.feeCollectionKeeper, genesisState.AuthState)
// Setting up initial accounts
accounts := make(map[string]bool)
for _, initialAccount := range genesisState.Accounts {
addrStr := initialAccount.Address.String()
if _, exists := accounts[addrStr]; exists {
panic(fmt.Sprintf("Duplicate account %s in genesis", addrStr))
}
accounts[addrStr] = true
initialAccount.AccountNumber = app.accountKeeper.GetNextAccountNumber(ctx)
app.accountKeeper.SetAccount(ctx, initialAccount)
}
initResponse := abci.ResponseInitChain{
Validators: genesisState.Validators,
}
return initResponse
}
// Uses go-amino which is a fork of protobuf3
// Here the codec implementation is injected into different modules
func MakeDefaultCodec() *codec.Codec {
var cdc = codec.New()
tic_tac_toe.RegisterCodec(cdc)
auth.RegisterCodec(cdc)
sdk.RegisterCodec(cdc)
codec.RegisterCrypto(cdc)
return cdc
}
type GenesisState struct {
AuthState auth.GenesisState `json:"auth"`
Accounts []*auth.BaseAccount `json:"accounts"`
Validators []abci.ValidatorUpdate `json:"validators"`
}