github.com/lino-network/lino@v0.6.11/app/genesis.go (about) 1 package app 2 3 import ( 4 "encoding/hex" 5 "encoding/json" 6 "errors" 7 "fmt" 8 "strings" 9 "time" 10 11 wire "github.com/cosmos/cosmos-sdk/codec" 12 "github.com/lino-network/lino/param" 13 "github.com/lino-network/lino/types" 14 crypto "github.com/tendermint/tendermint/crypto" 15 "github.com/tendermint/tendermint/crypto/secp256k1" 16 tmtypes "github.com/tendermint/tendermint/types" 17 ) 18 19 type GenesisPool struct { 20 Name types.PoolName `json:"name"` 21 Amount types.Coin `json:"amount"` 22 } 23 24 type GenesisPools struct { 25 Pools []GenesisPool `json:"pools"` 26 Total types.Coin `json:"total"` 27 } 28 29 func (g GenesisPools) IsValid() error { 30 poolMap := make(map[types.PoolName]bool) 31 poolMap[types.InflationDeveloperPool] = true 32 poolMap[types.InflationValidatorPool] = true 33 poolMap[types.InflationConsumptionPool] = true 34 poolMap[types.AccountVestingPool] = true 35 poolMap[types.VoteStakeInPool] = true 36 poolMap[types.VoteStakeReturnPool] = true 37 poolMap[types.VoteFrictionPool] = true 38 poolMap[types.DevIDAReservePool] = true 39 40 // checks 41 if len(g.Pools) != len(poolMap) { 42 return fmt.Errorf("expected number of pools: %d, actual: %d", len(poolMap), len(g.Pools)) 43 } 44 45 total := types.NewCoinFromInt64(0) 46 for _, pool := range g.Pools { 47 if !poolMap[pool.Name] { 48 return fmt.Errorf("unknown pool: %s", pool.Name) 49 } 50 total = total.Plus(pool.Amount) 51 } 52 if !total.IsEqual(g.Total) { 53 return fmt.Errorf("expected total: %s, actual: %s", g.Total, total) 54 } 55 return nil 56 } 57 58 func (g GenesisPools) ReservePool() types.Coin { 59 for _, pool := range g.Pools { 60 if pool.Name == types.DevIDAReservePool { 61 return pool.Amount 62 } 63 } 64 panic("reserve pool not found in genesis file") 65 } 66 67 // genesis state for blockchain 68 type GenesisState struct { 69 LoadPrevStates bool `json:"load_prev_states"` 70 GenesisPools GenesisPools `json:"genesis_pools"` 71 InitCoinPrice types.MiniDollar `json:"init_coin_price"` 72 Accounts []GenesisAccount `json:"accounts"` 73 Developers []GenesisAppDeveloper `json:"developers"` 74 GenesisParam GenesisParam `json:"genesis_param"` 75 } 76 77 // genesis account will get coin to the address and register user 78 // if genesis account is validator, it will be added to validator list automatically 79 type GenesisAccount struct { 80 Name string `json:"name"` 81 Coin types.Coin `json:"coin"` 82 TxKey crypto.PubKey `json:"tx_key"` 83 SignKey crypto.PubKey `json:"sign_key"` 84 IsValidator bool `json:"is_validator"` 85 ValPubKey crypto.PubKey `json:"validator_pub_key"` 86 } 87 88 // GenesisAppDeveloper - register developer in genesis phase 89 type GenesisAppDeveloper struct { 90 Name string `json:"name"` 91 Website string `json:"web_site"` 92 Description string `json:"description"` 93 AppMetaData string `json:"app_meta_data"` 94 } 95 96 // GenesisParam - genesis parameters 97 type GenesisParam struct { 98 InitFromConfig bool `json:"init_from_config"` 99 param.GlobalAllocationParam 100 param.VoteParam 101 param.ProposalParam 102 param.DeveloperParam 103 param.ValidatorParam 104 param.CoinDayParam 105 param.BandwidthParam 106 param.AccountParam 107 param.PostParam 108 param.ReputationParam 109 param.PriceParam 110 } 111 112 // LinoBlockchainGenTx - init genesis account 113 func LinoBlockchainGenTx(cdc *wire.Codec, pk crypto.PubKey) ( 114 appGenTx, cliPrint json.RawMessage, validator tmtypes.GenesisValidator, err error) { 115 txKey := secp256k1.GenPrivKey() 116 signKey := secp256k1.GenPrivKey() 117 118 fmt.Println("tx private key is:", strings.ToUpper(hex.EncodeToString(txKey.Bytes()))) 119 fmt.Println("sign private key is:", strings.ToUpper(hex.EncodeToString(signKey.Bytes()))) 120 121 totalCoin := types.NewCoinFromInt64(100000000 * types.Decimals) 122 genesisAcc := GenesisAccount{ 123 Name: "lino", 124 Coin: totalCoin, 125 TxKey: txKey.PubKey(), 126 SignKey: signKey.PubKey(), 127 IsValidator: true, 128 ValPubKey: pk, 129 } 130 131 var bz []byte 132 bz, err = wire.MarshalJSONIndent(cdc, genesisAcc) 133 if err != nil { 134 return 135 } 136 appGenTx = json.RawMessage(bz) 137 138 validator = tmtypes.GenesisValidator{ 139 PubKey: pk, 140 Power: 200000, 141 } 142 return 143 } 144 145 // LinoBlockchainGenState - default genesis file 146 func LinoBlockchainGenState(cdc *wire.Codec, appGenTxs []json.RawMessage) (appState json.RawMessage, err error) { 147 if len(appGenTxs) == 0 { 148 err = errors.New("must provide at least genesis transaction") 149 return 150 } 151 152 // totalLino := "10000000000" 153 genesisState := GenesisState{ 154 LoadPrevStates: false, 155 GenesisPools: GenesisPools{ 156 Pools: []GenesisPool{ 157 {Name: types.InflationDeveloperPool}, 158 {Name: types.InflationValidatorPool}, 159 {Name: types.InflationConsumptionPool}, 160 {Name: types.VoteStakeInPool}, 161 {Name: types.VoteStakeReturnPool}, 162 {Name: types.VoteFrictionPool}, 163 { 164 Name: types.DevIDAReservePool, 165 Amount: types.MustLinoToCoin("2000000000"), 166 }, 167 { 168 Name: types.AccountVestingPool, 169 Amount: types.MustLinoToCoin("8000000000"), 170 }, 171 }, 172 Total: types.MustLinoToCoin("10000000000"), 173 }, 174 InitCoinPrice: types.NewMiniDollar(1200), 175 Accounts: []GenesisAccount{}, 176 Developers: []GenesisAppDeveloper{}, 177 GenesisParam: GenesisParam{ 178 true, 179 param.GlobalAllocationParam{ 180 GlobalGrowthRate: types.NewDecFromRat(98, 1000), 181 ContentCreatorAllocation: types.NewDecFromRat(10, 100), 182 DeveloperAllocation: types.NewDecFromRat(70, 100), 183 ValidatorAllocation: types.NewDecFromRat(20, 100), 184 }, 185 param.VoteParam{ 186 MinStakeIn: types.NewCoinFromInt64(1000 * types.Decimals), 187 VoterCoinReturnIntervalSec: int64(7 * 24 * 3600), 188 VoterCoinReturnTimes: int64(7), 189 DelegatorCoinReturnIntervalSec: int64(7 * 24 * 3600), 190 DelegatorCoinReturnTimes: int64(7), 191 }, 192 param.ProposalParam{ 193 ContentCensorshipDecideSec: int64(24 * 7 * 3600), 194 ContentCensorshipPassRatio: types.NewDecFromRat(50, 100), 195 ContentCensorshipPassVotes: types.NewCoinFromInt64(10000 * types.Decimals), 196 ContentCensorshipMinDeposit: types.NewCoinFromInt64(100 * types.Decimals), 197 198 ChangeParamDecideSec: int64(24 * 7 * 3600), 199 ChangeParamPassRatio: types.NewDecFromRat(70, 100), 200 ChangeParamPassVotes: types.NewCoinFromInt64(1000000 * types.Decimals), 201 ChangeParamMinDeposit: types.NewCoinFromInt64(100000 * types.Decimals), 202 203 ProtocolUpgradeDecideSec: int64(24 * 7 * 3600), 204 ProtocolUpgradePassRatio: types.NewDecFromRat(80, 100), 205 ProtocolUpgradePassVotes: types.NewCoinFromInt64(10000000 * types.Decimals), 206 ProtocolUpgradeMinDeposit: types.NewCoinFromInt64(1000000 * types.Decimals), 207 }, 208 param.DeveloperParam{ 209 DeveloperMinDeposit: types.NewCoinFromInt64(1000000 * types.Decimals), 210 DeveloperCoinReturnIntervalSec: int64(7 * 24 * 3600), 211 DeveloperCoinReturnTimes: int64(7), 212 }, 213 param.ValidatorParam{ 214 ValidatorMinDeposit: types.NewCoinFromInt64(200000 * types.Decimals), 215 ValidatorCoinReturnIntervalSec: int64(7 * 24 * 3600), 216 ValidatorCoinReturnTimes: int64(7), 217 PenaltyMissCommit: types.NewCoinFromInt64(200 * types.Decimals), 218 PenaltyByzantine: types.NewCoinFromInt64(1000 * types.Decimals), 219 AbsentCommitLimitation: int64(600), // 30min 220 OncallSize: int64(22), 221 StandbySize: int64(7), 222 ValidatorRevokePendingSec: int64(7 * 24 * 3600), 223 OncallInflationWeight: int64(2), 224 StandbyInflationWeight: int64(1), 225 MaxVotedValidators: int64(3), 226 SlashLimitation: int64(5), 227 }, 228 param.CoinDayParam{ 229 SecondsToRecoverCoinDay: int64(7 * 24 * 3600), 230 }, 231 param.BandwidthParam{ 232 SecondsToRecoverBandwidth: int64(7 * 24 * 3600), 233 CapacityUsagePerTransaction: types.NewCoinFromInt64(1 * types.Decimals), 234 VirtualCoin: types.NewCoinFromInt64(1 * types.Decimals), 235 GeneralMsgQuotaRatio: types.NewDecFromRat(20, 100), 236 GeneralMsgEMAFactor: types.NewDecFromRat(1, 10), 237 AppMsgQuotaRatio: types.NewDecFromRat(80, 100), 238 AppMsgEMAFactor: types.NewDecFromRat(1, 10), 239 ExpectedMaxMPS: types.NewDecFromRat(300, 1), 240 MsgFeeFactorA: types.NewDecFromRat(6, 1), 241 MsgFeeFactorB: types.NewDecFromRat(10, 1), 242 MaxMPSDecayRate: types.NewDecFromRat(99, 100), 243 AppBandwidthPoolSize: types.NewDecFromRat(10, 1), 244 AppVacancyFactor: types.NewDecFromRat(69, 100), 245 AppPunishmentFactor: types.NewDecFromRat(14, 5), 246 }, 247 param.AccountParam{ 248 MinimumBalance: types.NewCoinFromInt64(0), 249 RegisterFee: types.NewCoinFromInt64(1 * types.Decimals), 250 FirstDepositFullCoinDayLimit: types.NewCoinFromInt64(1 * types.Decimals), 251 MaxNumFrozenMoney: 10, 252 }, 253 param.PostParam{ 254 ReportOrUpvoteIntervalSec: 24 * 3600, 255 PostIntervalSec: 600, 256 MaxReportReputation: types.NewCoinFromInt64(100 * types.Decimals), 257 }, 258 param.ReputationParam{ 259 BestContentIndexN: 200, 260 UserMaxN: 50, 261 }, 262 param.PriceParam{ 263 TestnetMode: true, 264 UpdateEverySec: int64(time.Hour.Seconds()), 265 FeedEverySec: int64((10 * time.Minute).Seconds()), 266 HistoryMaxLen: 71, 267 PenaltyMissFeed: types.NewCoinFromInt64(10000 * types.Decimals), 268 }, 269 }, 270 } 271 272 for _, genesisAccRaw := range appGenTxs { 273 var genesisAcc GenesisAccount 274 err = cdc.UnmarshalJSON(genesisAccRaw, &genesisAcc) 275 if err != nil { 276 return 277 } 278 genesisState.Accounts = append(genesisState.Accounts, genesisAcc) 279 } 280 genesisAppDeveloper := GenesisAppDeveloper{ 281 Name: "linoapp", 282 Website: "https://lino.network/", 283 Description: "", 284 AppMetaData: "", 285 } 286 genesisState.Developers = append(genesisState.Developers, genesisAppDeveloper) 287 288 appState, err = wire.MarshalJSONIndent(cdc, genesisState) 289 return 290 }