-
Notifications
You must be signed in to change notification settings - Fork 127
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #552 from HaoyangLiu/feature/deps-0.26.0
Runable iris prototype
- Loading branch information
Showing
16 changed files
with
938 additions
and
431 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,6 +9,7 @@ import ( | |
"path/filepath" | ||
"sort" | ||
"strings" | ||
"time" | ||
|
||
"github.com/cosmos/cosmos-sdk/codec" | ||
"github.com/cosmos/cosmos-sdk/server" | ||
|
@@ -19,14 +20,15 @@ import ( | |
"github.com/cosmos/cosmos-sdk/x/slashing" | ||
"github.com/cosmos/cosmos-sdk/x/stake" | ||
"github.com/irisnet/irishub/modules/gov" | ||
"github.com/irisnet/irishub/modules/iservice" | ||
"github.com/irisnet/irishub/modules/upgrade" | ||
"github.com/irisnet/irishub/types" | ||
"time" | ||
"github.com/irisnet/irishub/modules/iservice" | ||
tmtypes "github.com/tendermint/tendermint/types" | ||
) | ||
|
||
var ( | ||
Denom = "iris" | ||
StakeDenom = Denom + "-" + types.Atto | ||
FeeAmt = int64(100) | ||
IrisCt = types.NewDefaultCoinType(Denom) | ||
FreeFermionVal, _ = IrisCt.ConvertToMinCoin(fmt.Sprintf("%d%s", FeeAmt, Denom)) | ||
|
@@ -42,6 +44,7 @@ const ( | |
// State to Unmarshal | ||
type GenesisState struct { | ||
Accounts []GenesisAccount `json:"accounts"` | ||
AuthData auth.GenesisState `json:"auth"` | ||
StakeData stake.GenesisState `json:"stake"` | ||
MintData mint.GenesisState `json:"mint"` | ||
DistrData distr.GenesisState `json:"distr"` | ||
|
@@ -52,11 +55,12 @@ type GenesisState struct { | |
GenTxs []json.RawMessage `json:"gentxs"` | ||
} | ||
|
||
func NewGenesisState(accounts []GenesisAccount, stakeData stake.GenesisState, mintData mint.GenesisState, | ||
func NewGenesisState(accounts []GenesisAccount, authData auth.GenesisState, stakeData stake.GenesisState, mintData mint.GenesisState, | ||
distrData distr.GenesisState, govData gov.GenesisState, upgradeData upgrade.GenesisState, slashingData slashing.GenesisState) GenesisState { | ||
|
||
return GenesisState{ | ||
Accounts: accounts, | ||
AuthData: authData, | ||
StakeData: stakeData, | ||
MintData: mintData, | ||
DistrData: distrData, | ||
|
@@ -68,29 +72,51 @@ func NewGenesisState(accounts []GenesisAccount, stakeData stake.GenesisState, mi | |
|
||
// GenesisAccount doesn't need pubkey or sequence | ||
type GenesisAccount struct { | ||
Address sdk.AccAddress `json:"address"` | ||
Coins sdk.Coins `json:"coins"` | ||
Address sdk.AccAddress `json:"address"` | ||
Coins sdk.Coins `json:"coins"` | ||
Sequence int64 `json:"sequence_number"` | ||
AccountNumber int64 `json:"account_number"` | ||
} | ||
|
||
func NewGenesisAccount(acc *auth.BaseAccount) GenesisAccount { | ||
return GenesisAccount{ | ||
Address: acc.Address, | ||
Coins: acc.Coins, | ||
Address: acc.Address, | ||
Coins: acc.Coins, | ||
AccountNumber: acc.AccountNumber, | ||
Sequence: acc.Sequence, | ||
} | ||
} | ||
|
||
func NewGenesisAccountI(acc auth.Account) GenesisAccount { | ||
return GenesisAccount{ | ||
Address: acc.GetAddress(), | ||
Coins: acc.GetCoins(), | ||
Address: acc.GetAddress(), | ||
Coins: acc.GetCoins(), | ||
AccountNumber: acc.GetAccountNumber(), | ||
Sequence: acc.GetSequence(), | ||
} | ||
} | ||
|
||
// convert GenesisAccount to auth.BaseAccount | ||
func (ga *GenesisAccount) ToAccount() (acc *auth.BaseAccount) { | ||
return &auth.BaseAccount{ | ||
Address: ga.Address, | ||
Coins: ga.Coins.Sort(), | ||
Address: ga.Address, | ||
Coins: ga.Coins.Sort(), | ||
AccountNumber: ga.AccountNumber, | ||
Sequence: ga.Sequence, | ||
} | ||
} | ||
|
||
// NewDefaultGenesisState generates the default state for gaia. | ||
func NewDefaultGenesisState() GenesisState { | ||
return GenesisState{ | ||
Accounts: nil, | ||
StakeData: createStakeGenesisState(), | ||
MintData: createMintGenesisState(), | ||
DistrData: distr.DefaultGenesisState(), | ||
GovData: gov.DefaultGenesisState(), | ||
UpgradeData: upgrade.DefaultGenesisState(), | ||
SlashingData: slashing.DefaultGenesisState(), | ||
GenTxs: nil, | ||
} | ||
} | ||
|
||
|
@@ -103,65 +129,53 @@ func IrisAppInit() server.AppInit { | |
|
||
// Create the core parameters for genesis initialization for iris | ||
// note that the pubkey input is this machines pubkey | ||
func IrisAppGenState(cdc *codec.Codec, appGenTxs []json.RawMessage) (genesisState GenesisState, err error) { | ||
if len(appGenTxs) == 0 { | ||
err = errors.New("must provide at least genesis transaction") | ||
return | ||
func IrisAppGenState(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 | ||
} | ||
|
||
// start with the default staking genesis state | ||
stakeData := createGenesisState() | ||
slashingData := slashing.DefaultGenesisState() | ||
|
||
// get genesis flag account information | ||
genaccs := make([]GenesisAccount, len(appGenTxs)) | ||
// 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, appGenTx := range appGenTxs { | ||
stakeData := genesisState.StakeData | ||
for i, genTx := range appGenTxs { | ||
var tx auth.StdTx | ||
err = cdc.UnmarshalJSON(appGenTx, &tx) | ||
if err != nil { | ||
return | ||
if err := cdc.UnmarshalJSON(genTx, &tx); err != nil { | ||
return genesisState, err | ||
} | ||
msgs := tx.GetMsgs() | ||
if len(msgs) != 1 { | ||
err = errors.New("must provide genesis StdTx with exactly 1 CreateValidator message") | ||
return | ||
return genesisState, errors.New( | ||
"must provide genesis StdTx with exactly 1 CreateValidator message") | ||
} | ||
if _, ok := msgs[0].(stake.MsgCreateValidator); !ok { | ||
return genesisState, fmt.Errorf( | ||
"Genesis transaction %v does not contain a MsgCreateValidator", i) | ||
} | ||
msg := msgs[0].(stake.MsgCreateValidator) | ||
|
||
// create the genesis account, give'm few iris token and a buncha token with there name | ||
genaccs[i] = genesisAccountFromMsgCreateValidator(msg, FreeFermionAcc.Amount) | ||
stakeData.Pool.LooseTokens = stakeData.Pool.LooseTokens.Add(sdk.NewDecFromInt(FreeFermionAcc.Amount)) // increase the supply | ||
} | ||
|
||
// create the final app state | ||
genesisState = GenesisState{ | ||
Accounts: genaccs, | ||
StakeData: stakeData, | ||
MintData: mint.GenesisState{ | ||
Minter: mint.InitialMinter(), | ||
Params: mint.Params{ | ||
MintDenom: "iris-atto", | ||
InflationRateChange: sdk.NewDecWithPrec(13, 2), | ||
InflationMax: sdk.NewDecWithPrec(20, 2), | ||
InflationMin: sdk.NewDecWithPrec(7, 2), | ||
GoalBonded: sdk.NewDecWithPrec(67, 2), | ||
}, | ||
}, | ||
DistrData: distr.DefaultGenesisState(), | ||
GovData: gov.DefaultGenesisState(), | ||
UpgradeData: upgrade.DefaultGenesisState(), | ||
SlashingData: slashingData, | ||
IserviceData: iservice.DefaultGenesisState(), | ||
GenTxs: appGenTxs, | ||
for _, acc := range genesisState.Accounts { | ||
// create the genesis account, give'm few iris-atto and a buncha token with there name | ||
for _, coin := range acc.Coins { | ||
if coin.Denom == StakeDenom { | ||
stakeData.Pool.LooseTokens = stakeData.Pool.LooseTokens. | ||
Add(sdk.NewDecFromInt(coin.Amount)) // increase the supply | ||
} | ||
} | ||
} | ||
return | ||
genesisState.StakeData = stakeData | ||
genesisState.GenTxs = appGenTxs | ||
genesisState.UpgradeData = genesisState.UpgradeData | ||
return genesisState, nil | ||
} | ||
|
||
func genesisAccountFromMsgCreateValidator(msg stake.MsgCreateValidator, amount sdk.Int) GenesisAccount { | ||
accAuth := auth.NewBaseAccountWithAddress(sdk.AccAddress(msg.ValidatorAddr)) | ||
accAuth.Coins = []sdk.Coin{ | ||
{"iris-atto", amount}, | ||
{StakeDenom, amount}, | ||
} | ||
return NewGenesisAccount(&accAuth) | ||
} | ||
|
@@ -197,28 +211,45 @@ func validateGenesisStateAccounts(accs []GenesisAccount) (err error) { | |
} | ||
|
||
// IrisAppGenState but with JSON | ||
func IrisAppGenStateJSON(cdc *codec.Codec, appGenTxs []json.RawMessage) (appState json.RawMessage, err error) { | ||
func IrisAppGenStateJSON(cdc *codec.Codec, genDoc tmtypes.GenesisDoc, appGenTxs []json.RawMessage) ( | ||
appState json.RawMessage, err error) { | ||
|
||
// create the final app state | ||
genesisState, err := IrisAppGenState(cdc, appGenTxs) | ||
genesisState, err := IrisAppGenState(cdc, genDoc, appGenTxs) | ||
if err != nil { | ||
return nil, err | ||
} | ||
appState, err = codec.MarshalJSONIndent(cdc, genesisState) | ||
return | ||
} | ||
|
||
// CollectStdTxs processes and validates application's genesis StdTxs and returns the list of validators, | ||
// appGenTxs, and persistent peers required to generate genesis.json. | ||
func CollectStdTxs(moniker string, genTxsDir string, cdc *codec.Codec) ( | ||
// CollectStdTxs processes and validates application's genesis StdTxs and returns | ||
// the list of appGenTxs, and persistent peers required to generate genesis.json. | ||
func CollectStdTxs(cdc *codec.Codec, moniker string, genTxsDir string, genDoc tmtypes.GenesisDoc) ( | ||
appGenTxs []auth.StdTx, persistentPeers string, err error) { | ||
|
||
var fos []os.FileInfo | ||
fos, err = ioutil.ReadDir(genTxsDir) | ||
if err != nil { | ||
return | ||
return appGenTxs, persistentPeers, err | ||
} | ||
|
||
var addresses []string | ||
// prepare a map of all accounts in genesis state to then validate | ||
// against the validators addresses | ||
var appState GenesisState | ||
if err := cdc.UnmarshalJSON(genDoc.AppState, &appState); err != nil { | ||
return appGenTxs, persistentPeers, err | ||
} | ||
addrMap := make(map[string]GenesisAccount, len(appState.Accounts)) | ||
for i := 0; i < len(appState.Accounts); i++ { | ||
acc := appState.Accounts[i] | ||
strAddr := string(acc.Address) | ||
addrMap[strAddr] = acc | ||
} | ||
|
||
// addresses and IPs (and port) validator server info | ||
var addressesIPs []string | ||
|
||
for _, fo := range fos { | ||
filename := filepath.Join(genTxsDir, fo.Name()) | ||
if !fo.IsDir() && (filepath.Ext(filename) != ".json") { | ||
|
@@ -227,41 +258,55 @@ func CollectStdTxs(moniker string, genTxsDir string, cdc *codec.Codec) ( | |
|
||
// get the genStdTx | ||
var jsonRawTx []byte | ||
jsonRawTx, err = ioutil.ReadFile(filename) | ||
if err != nil { | ||
return | ||
if jsonRawTx, err = ioutil.ReadFile(filename); err != nil { | ||
return appGenTxs, persistentPeers, err | ||
} | ||
var genStdTx auth.StdTx | ||
err = cdc.UnmarshalJSON(jsonRawTx, &genStdTx) | ||
if err != nil { | ||
return | ||
if err = cdc.UnmarshalJSON(jsonRawTx, &genStdTx); err != nil { | ||
return appGenTxs, persistentPeers, err | ||
} | ||
appGenTxs = append(appGenTxs, genStdTx) | ||
|
||
nodeAddr := genStdTx.GetMemo() | ||
if len(nodeAddr) == 0 { | ||
err = fmt.Errorf("couldn't find node's address in %s", fo.Name()) | ||
return | ||
// the memo flag is used to store | ||
// the ip and node-id, for example this may be: | ||
// "[email protected]:26656" | ||
nodeAddrIP := genStdTx.GetMemo() | ||
if len(nodeAddrIP) == 0 { | ||
return appGenTxs, persistentPeers, fmt.Errorf( | ||
"couldn't find node's address and IP in %s", fo.Name()) | ||
} | ||
|
||
// genesis transactions must be single-message | ||
msgs := genStdTx.GetMsgs() | ||
if len(msgs) != 1 { | ||
err = errors.New("each genesis transaction must provide a single genesis message") | ||
return | ||
|
||
return appGenTxs, persistentPeers, errors.New( | ||
"each genesis transaction must provide a single genesis message") | ||
} | ||
|
||
// validate the validator address and funds against the accounts in the state | ||
msg := msgs[0].(stake.MsgCreateValidator) | ||
addr := string(sdk.AccAddress(msg.ValidatorAddr)) | ||
acc, ok := addrMap[addr] | ||
if !ok { | ||
return appGenTxs, persistentPeers, fmt.Errorf( | ||
"account %v not in genesis.json: %+v", addr, addrMap) | ||
} | ||
if acc.Coins.AmountOf(msg.Delegation.Denom).LT(msg.Delegation.Amount) { | ||
err = fmt.Errorf("insufficient fund for the delegation: %s < %s", | ||
acc.Coins.AmountOf(msg.Delegation.Denom), msg.Delegation.Amount) | ||
} | ||
|
||
// exclude itself from persistent peers | ||
if msg.Description.Moniker != moniker { | ||
addresses = append(addresses, nodeAddr) | ||
addressesIPs = append(addressesIPs, nodeAddrIP) | ||
} | ||
} | ||
|
||
sort.Strings(addresses) | ||
persistentPeers = strings.Join(addresses, ",") | ||
sort.Strings(addressesIPs) | ||
persistentPeers = strings.Join(addressesIPs, ",") | ||
|
||
return | ||
return appGenTxs, persistentPeers, nil | ||
} | ||
|
||
func NewDefaultGenesisAccount(addr sdk.AccAddress) GenesisAccount { | ||
|
@@ -272,7 +317,7 @@ func NewDefaultGenesisAccount(addr sdk.AccAddress) GenesisAccount { | |
return NewGenesisAccount(&accAuth) | ||
} | ||
|
||
func createGenesisState() stake.GenesisState { | ||
func createStakeGenesisState() stake.GenesisState { | ||
return stake.GenesisState{ | ||
Pool: stake.Pool{ | ||
LooseTokens: sdk.ZeroDec(), | ||
|
@@ -281,7 +326,20 @@ func createGenesisState() stake.GenesisState { | |
Params: stake.Params{ | ||
UnbondingTime: defaultUnbondingTime, | ||
MaxValidators: 100, | ||
BondDenom: Denom + "-" + types.Atto, | ||
BondDenom: StakeDenom, | ||
}, | ||
} | ||
} | ||
|
||
func createMintGenesisState() mint.GenesisState { | ||
return mint.GenesisState{ | ||
Minter: mint.InitialMinter(), | ||
Params: mint.Params{ | ||
MintDenom: StakeDenom, | ||
InflationRateChange: sdk.NewDecWithPrec(13, 2), | ||
InflationMax: sdk.NewDecWithPrec(20, 2), | ||
InflationMin: sdk.NewDecWithPrec(7, 2), | ||
GoalBonded: sdk.NewDecWithPrec(67, 2), | ||
}, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.