-
Notifications
You must be signed in to change notification settings - Fork 86
/
genesis.go
316 lines (289 loc) · 10 KB
/
genesis.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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
package app
import (
"encoding/json"
"fmt"
"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/crisis"
distr "github.com/cosmos/cosmos-sdk/x/distribution"
"github.com/cosmos/cosmos-sdk/x/gov"
"github.com/cosmos/cosmos-sdk/x/mint"
"github.com/cosmos/cosmos-sdk/x/slashing"
"github.com/cosmos/cosmos-sdk/x/staking"
"github.com/cosmos/cosmos-sdk/x/staking/types"
"github.com/cosmos/cosmos-sdk/x/supply"
"github.com/cybercongress/go-cyber/types/coin"
"github.com/cybercongress/go-cyber/x/bandwidth"
"github.com/cybercongress/go-cyber/x/rank"
"github.com/pkg/errors"
"github.com/tendermint/go-amino"
tmtypes "github.com/tendermint/tendermint/types"
"io/ioutil"
"time"
)
const (
defaultUnbondingTime = 60 * 60 * 24 * 3 * 7 * time.Second // 3 weeks
)
// State to Unmarshal
type GenesisState struct {
Accounts []GenesisAccount `json:"accounts"`
AuthData auth.GenesisState `json:"auth"`
BankData bank.GenesisState `json:"bank"`
DistrData distr.GenesisState `json:"distribution"`
MintData mint.GenesisState `json:"mint"`
StakingData staking.GenesisState `json:"staking"`
Pool staking.Pool `json:"pool"`
SupplyData supply.GenesisState `json:"supply"`
SlashingData slashing.GenesisState `json:"slashing"`
GovData gov.GenesisState `json:"gov"`
BandwidthData bandwidth.GenesisState `json:"bandwidth"`
RankData rank.GenesisState `json:"rank"`
GenTxs []json.RawMessage `json:"gentxs"`
Crisis crisis.GenesisState `json:"crisis"`
}
func (gs *GenesisState) GetAddresses() []sdk.AccAddress {
addresses := make([]sdk.AccAddress, 0, len(gs.Accounts))
for _, acc := range gs.Accounts {
addresses = append(addresses, acc.Address)
}
return addresses
}
func NewGenesisState(
accounts []GenesisAccount, authData auth.GenesisState,
stakingData staking.GenesisState, pool staking.Pool,
mintData mint.GenesisState, distrData distr.GenesisState,
govData gov.GenesisState, supplyData supply.GenesisState,
slashingData slashing.GenesisState, bandwidthData bandwidth.GenesisState,
rankData rank.GenesisState, crisisData crisis.GenesisState,
) GenesisState {
return GenesisState{
Accounts: accounts,
AuthData: authData,
StakingData: stakingData,
Pool: pool,
SupplyData: supplyData,
MintData: mintData,
DistrData: distrData,
SlashingData: slashingData,
GovData: govData,
BandwidthData: bandwidthData,
RankData: rankData,
Crisis: crisisData,
}
}
type GenesisAccount struct {
Address sdk.AccAddress `json:"address" yaml:"address"`
Coins sdk.Coins `json:"coins" yaml:"coins"`
Sequence uint64 `json:"sequence_number" yaml:"sequence_number"`
AccountNumber uint64 `json:"account_number" yaml:"account_number"`
OriginalVesting sdk.Coins `json:"original_vesting" yaml:"original_vesting"`
DelegatedFree sdk.Coins `json:"delegated_free" yaml:"delegated_free"`
DelegatedVesting sdk.Coins `json:"delegated_vesting" yaml:"delegated_vesting"`
StartTime int64 `json:"start_time" yaml:"start_time"`
EndTime int64 `json:"end_time" yaml:"end_time"`
ModuleName string `json:"module_name" yaml:"module_name"`
ModulePermissions []string `json:"module_permissions" yaml:"module_permissions"`
}
// convert GenesisAccount to auth.BaseAccount
func (ga *GenesisAccount) ToAccount() auth.Account {
acc := auth.NewBaseAccount(ga.Address, ga.Coins.Sort(), nil, ga.AccountNumber, ga.Sequence)
// vesting accounts
if !ga.OriginalVesting.IsZero() {
baseVestingAcc := auth.NewBaseVestingAccount(
acc, ga.OriginalVesting, ga.DelegatedFree,
ga.DelegatedVesting, ga.EndTime,
)
switch {
case ga.StartTime != 0 && ga.EndTime != 0:
return auth.NewContinuousVestingAccountRaw(baseVestingAcc, ga.StartTime)
case ga.EndTime != 0:
return auth.NewDelayedVestingAccountRaw(baseVestingAcc)
default:
panic(fmt.Sprintf("invalid genesis vesting account: %+v", ga))
}
}
// module accounts
if ga.ModuleName != "" {
return supply.NewModuleAccount(acc, ga.ModuleName, ga.ModulePermissions...)
}
return acc
}
// NewDefaultGenesisState generates the default state for cyberd.
func NewDefaultGenesisState() GenesisState {
return GenesisState{
Accounts: nil,
AuthData: auth.GenesisState{
Params: auth.Params{
MaxMemoCharacters: 256,
TxSigLimit: 10,
},
},
BankData: bank.GenesisState{
SendEnabled: true,
},
MintData: mint.GenesisState{
Minter: mint.Minter{
Inflation: sdk.NewDecWithPrec(3, 2),
AnnualProvisions: sdk.NewDec(0),
},
Params: mint.Params{
MintDenom: coin.CYB,
InflationRateChange: sdk.NewDecWithPrec(10, 2),
InflationMax: sdk.NewDecWithPrec(15, 2),
InflationMin: sdk.NewDecWithPrec(1, 2),
GoalBonded: sdk.NewDecWithPrec(88, 2),
BlocksPerYear: uint64(60 * 60 * 8766 / 5), // assuming 5 second block times
},
},
StakingData: staking.GenesisState{
Params: types.Params{
UnbondingTime: defaultUnbondingTime,
MaxValidators: 7,
MaxEntries: 7,
BondDenom: coin.CYB,
},
},
Pool: staking.Pool{
NotBondedTokens: sdk.ZeroInt(),
BondedTokens: sdk.ZeroInt(),
},
SupplyData: supply.GenesisState{
Supply: sdk.NewCoins(),
},
SlashingData: slashing.GenesisState{
Params: slashing.Params{
MaxEvidenceAge: defaultUnbondingTime,
SignedBlocksWindow: 60 * 4, // ~20min
DowntimeJailDuration: 0,
MinSignedPerWindow: sdk.NewDecWithPrec(80, 2), // 80%
SlashFractionDoubleSign: sdk.NewDecWithPrec(5, 2), // 5%
SlashFractionDowntime: sdk.NewDec(5).Quo(sdk.NewDec(10000)), // 0.05%
},
},
DistrData: distr.GenesisState{
FeePool: distr.InitialFeePool(),
CommunityTax: sdk.NewDecWithPrec(10, 2), // 10%
BaseProposerReward: sdk.NewDecWithPrec(1, 2), // 1%
BonusProposerReward: sdk.NewDecWithPrec(5, 2), // 5%
WithdrawAddrEnabled: true,
PreviousProposer: nil,
},
GovData: gov.GenesisState{
StartingProposalID: 1,
DepositParams: gov.DepositParams{
MinDeposit: sdk.Coins{coin.NewCybCoin(500 * coin.Giga)},
MaxDepositPeriod: 7200 * time.Second, // 2 hours
},
VotingParams: gov.VotingParams{
VotingPeriod: 7200 * time.Second, // 2 hours
},
TallyParams: gov.TallyParams{
Quorum: sdk.NewDecWithPrec(334, 3),
Threshold: sdk.NewDecWithPrec(5, 1),
Veto: sdk.NewDecWithPrec(334, 3),
},
},
BandwidthData: bandwidth.DefaultGenesisState(),
RankData: rank.DefaultGenesisState(),
GenTxs: []json.RawMessage{},
Crisis: crisis.GenesisState{ ConstantFee: sdk.NewCoin(coin.CYB, sdk.NewInt(1000)) },
}
}
// Create the core parameters for genesis initialization for cyberd
// note that the pubkey input is this machines pubkey
func CyberdAppGenState(cdc *codec.Codec, genDoc tmtypes.GenesisDoc, appGenTxs []json.RawMessage) (
genesisState GenesisState, err error) {
if err = cdc.UnmarshalJSON(genDoc.AppState, &genesisState); err != nil {
return genesisState, err
}
// if there are no gen txs to be processed, return the default empty state
if len(appGenTxs) == 0 {
return genesisState, errors.New("there must be at least one genesis tx")
}
for i, genTx := range appGenTxs {
var tx auth.StdTx
if err := cdc.UnmarshalJSON(genTx, &tx); err != nil {
return genesisState, err
}
msgs := tx.GetMsgs()
if len(msgs) != 1 {
return genesisState, errors.New(
"must provide genesis StdTx with exactly 1 CreateValidator message")
}
if _, ok := msgs[0].(staking.MsgCreateValidator); !ok {
return genesisState, fmt.Errorf(
"genesis transaction %v does not contain a MsgCreateValidator", i)
}
}
genesisState.GenTxs = appGenTxs
return genesisState, nil
}
//todo should be here?
func CyberdAppGenStateJSON(cdc *codec.Codec, genDoc tmtypes.GenesisDoc, appGenTxs []json.RawMessage) (
appState json.RawMessage, err error) {
// create the final app state
genesisState, err := CyberdAppGenState(cdc, genDoc, appGenTxs)
if err != nil {
return nil, err
}
return codec.MarshalJSONIndent(cdc, genesisState)
}
// validateGenesisState ensures that the genesis state obeys the expected invariants
func validateGenesisState(genesisState GenesisState) error {
if err := validateGenesisStateAccounts(genesisState.Accounts); err != nil {
return err
}
if err := staking.ValidateGenesis(genesisState.StakingData); err != nil {
return err
}
if err := mint.ValidateGenesis(genesisState.MintData); err != nil {
return err
}
if err := distr.ValidateGenesis(genesisState.DistrData); err != nil {
return err
}
if err := gov.ValidateGenesis(genesisState.GovData); err != nil {
return err
}
if err := bank.ValidateGenesis(genesisState.BankData); err != nil {
return err
}
if err := mint.ValidateGenesis(genesisState.MintData); err != nil {
return err
}
if err := supply.ValidateGenesis(genesisState.SupplyData); err != nil {
return err
}
if err := bandwidth.ValidateGenesis(genesisState.BandwidthData); err != nil {
return err
}
if err := rank.ValidateGenesis(genesisState.RankData); err != nil {
return err
}
return staking.ValidateGenesis(genesisState.StakingData)
}
// Ensures that there are no duplicate accounts in the genesis state,
func validateGenesisStateAccounts(accs []GenesisAccount) (err error) {
addrMap := make(map[string]bool, len(accs))
for i := 0; i < len(accs); i++ {
acc := accs[i]
strAddr := string(acc.Address)
if _, ok := addrMap[strAddr]; ok {
return fmt.Errorf("duplicate account in genesis state: Address %v", acc.Address.String())
}
addrMap[strAddr] = true
}
return
}
func LoadGenesisDoc(cdc *amino.Codec, genFile string) (genDoc tmtypes.GenesisDoc, err error) {
genContents, err := ioutil.ReadFile(genFile)
if err != nil {
return genDoc, err
}
if err := cdc.UnmarshalJSON(genContents, &genDoc); err != nil {
return genDoc, err
}
return genDoc, err
}