-
Notifications
You must be signed in to change notification settings - Fork 3.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
R4R [Staging PR 2/3] genesis generalization #4128
Closed
rigelrozanski
wants to merge
19
commits into
rigel/genesis-generalization
from
rigel/genesis-generalization2
Closed
Changes from 6 commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
a22a9f7
basic refactors cmd/gaia/init
rigelrozanski 75a9a15
working
rigelrozanski 26dae66
Merge branch 'rigel/genesis-generalization' into rigel/genesis-genera…
rigelrozanski 1b805dd
working
rigelrozanski 8e8345c
Merge branch 'rigel/genesis-generalization' into rigel/genesis-genera…
rigelrozanski 2573df8
add AppModuleBasic
rigelrozanski 9386263
moduleBasicManager / non-test code compiles
rigelrozanski a21406e
working attempting to get tests passing
rigelrozanski cf671d4
make test passes
rigelrozanski a2c8c28
sim random genesis fix
rigelrozanski c8a86bd
export bug
rigelrozanski cceb887
...
rigelrozanski d2de398
Merge branch 'rigel/genesis-generalization' into rigel/genesis-genera…
rigelrozanski 93fa001
lint fixes
rigelrozanski 62a2b69
Merge branch 'rigel/genesis-generalization' into rigel/genesis-genera…
rigelrozanski 84a0167
Merge branch 'rigel/genesis-generalization' into rigel/genesis-genera…
rigelrozanski 988da1b
cli test fix
rigelrozanski f387889
Merge branch 'rigel/genesis-generalization' into rigel/genesis-genera…
rigelrozanski 19e9edb
Merge branch 'rigel/genesis-generalization' into rigel/genesis-genera…
rigelrozanski File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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 |
---|---|---|
|
@@ -4,58 +4,30 @@ import ( | |
"encoding/json" | ||
"errors" | ||
"fmt" | ||
"io/ioutil" | ||
"os" | ||
"path/filepath" | ||
"sort" | ||
"strings" | ||
"time" | ||
|
||
tmtypes "github.com/tendermint/tendermint/types" | ||
|
||
"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" | ||
) | ||
|
||
// XXX should use map for all module data | ||
|
||
// State to Unmarshal | ||
type GenesisState struct { | ||
Accounts []GenesisAccount `json:"accounts"` | ||
AuthData auth.GenesisState `json:"auth"` | ||
BankData bank.GenesisState `json:"bank"` | ||
StakingData staking.GenesisState `json:"staking"` | ||
MintData mint.GenesisState `json:"mint"` | ||
DistrData distr.GenesisState `json:"distr"` | ||
GovData gov.GenesisState `json:"gov"` | ||
CrisisData crisis.GenesisState `json:"crisis"` | ||
SlashingData slashing.GenesisState `json:"slashing"` | ||
GenTxs []json.RawMessage `json:"gentxs"` | ||
Accounts []GenesisAccount `json:"accounts"` | ||
Modules map[string]json.RawMessage `json:"modules"` | ||
GenTxs []json.RawMessage `json:"gentxs"` | ||
} | ||
|
||
func NewGenesisState(accounts []GenesisAccount, authData auth.GenesisState, | ||
bankData bank.GenesisState, stakingData staking.GenesisState, mintData mint.GenesisState, | ||
distrData distr.GenesisState, govData gov.GenesisState, crisisData crisis.GenesisState, | ||
slashingData slashing.GenesisState) GenesisState { | ||
|
||
// NewGenesisState creates a new GenesisState object | ||
func NewGenesisState(accounts []GenesisAccount, modules map[string]json.RawMessage, genTxs []json.RawMessage) GenesisState { | ||
return GenesisState{ | ||
Accounts: accounts, | ||
AuthData: authData, | ||
BankData: bankData, | ||
StakingData: stakingData, | ||
MintData: mintData, | ||
DistrData: distrData, | ||
GovData: govData, | ||
CrisisData: crisisData, | ||
SlashingData: slashingData, | ||
Accounts: accounts, | ||
Modules: modules, | ||
GenTxs: genTxs, | ||
} | ||
} | ||
|
||
|
@@ -203,55 +175,25 @@ func GaiaAppGenStateJSON(cdc *codec.Codec, genDoc tmtypes.GenesisDoc, appGenTxs | |
|
||
// NewDefaultGenesisState generates the default state for gaia. | ||
func NewDefaultGenesisState() GenesisState { | ||
return GenesisState{ | ||
Accounts: nil, | ||
AuthData: auth.DefaultGenesisState(), | ||
BankData: bank.DefaultGenesisState(), | ||
StakingData: staking.DefaultGenesisState(), | ||
MintData: mint.DefaultGenesisState(), | ||
DistrData: distr.DefaultGenesisState(), | ||
GovData: gov.DefaultGenesisState(), | ||
CrisisData: crisis.DefaultGenesisState(), | ||
SlashingData: slashing.DefaultGenesisState(), | ||
GenTxs: nil, | ||
} | ||
} | ||
|
||
// GaiaValidateGenesisState ensures that the genesis state obeys the expected invariants | ||
// TODO: Ensure all state machine parameters are in genesis (#1704) | ||
func GaiaValidateGenesisState(genesisState GenesisState) error { | ||
if err := validateGenesisStateAccounts(genesisState.Accounts); err != nil { | ||
return err | ||
} | ||
|
||
// skip stakingData validation as genesis is created from txs | ||
if len(genesisState.GenTxs) > 0 { | ||
return nil | ||
moduleGS := make(map[string]json.RawMessage) | ||
for _, mb := range moduleBasics { | ||
moduleGS[mb.Name()] = mb.DefaultGenesisState(cdc) | ||
} | ||
|
||
if err := auth.ValidateGenesis(genesisState.AuthData); err != nil { | ||
return err | ||
} | ||
if err := bank.ValidateGenesis(genesisState.BankData); 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 := crisis.ValidateGenesis(genesisState.CrisisData); err != nil { | ||
return err | ||
return GenesisState{ | ||
Accounts: nil, | ||
Modules: moduleGS, | ||
//AuthData: auth.DefaultGenesisState(), | ||
//BankData: bank.DefaultGenesisState(), | ||
//StakingData: staking.DefaultGenesisState(), | ||
//MintData: mint.DefaultGenesisState(), | ||
//DistrData: distr.DefaultGenesisState(), | ||
//GovData: gov.DefaultGenesisState(), | ||
//CrisisData: crisis.DefaultGenesisState(), | ||
//SlashingData: slashing.DefaultGenesisState(), | ||
GenTxs: nil, | ||
} | ||
|
||
return slashing.ValidateGenesis(genesisState.SlashingData) | ||
} | ||
|
||
// validateGenesisStateAccounts performs validation of genesis accounts. It | ||
|
@@ -288,103 +230,3 @@ func validateGenesisStateAccounts(accs []GenesisAccount) error { | |
|
||
return nil | ||
} | ||
|
||
// 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 appGenTxs, persistentPeers, err | ||
} | ||
|
||
// 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] | ||
addrMap[acc.Address.String()] = 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") { | ||
continue | ||
} | ||
|
||
// get the genStdTx | ||
var jsonRawTx []byte | ||
if jsonRawTx, err = ioutil.ReadFile(filename); err != nil { | ||
return appGenTxs, persistentPeers, err | ||
} | ||
var genStdTx auth.StdTx | ||
if err = cdc.UnmarshalJSON(jsonRawTx, &genStdTx); err != nil { | ||
return appGenTxs, persistentPeers, err | ||
} | ||
appGenTxs = append(appGenTxs, genStdTx) | ||
|
||
// 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 { | ||
|
||
return appGenTxs, persistentPeers, errors.New( | ||
"each genesis transaction must provide a single genesis message") | ||
} | ||
|
||
msg := msgs[0].(staking.MsgCreateValidator) | ||
// validate delegator and validator addresses and funds against the accounts in the state | ||
delAddr := msg.DelegatorAddress.String() | ||
valAddr := sdk.AccAddress(msg.ValidatorAddress).String() | ||
|
||
delAcc, delOk := addrMap[delAddr] | ||
_, valOk := addrMap[valAddr] | ||
|
||
accsNotInGenesis := []string{} | ||
if !delOk { | ||
accsNotInGenesis = append(accsNotInGenesis, delAddr) | ||
} | ||
if !valOk { | ||
accsNotInGenesis = append(accsNotInGenesis, valAddr) | ||
} | ||
if len(accsNotInGenesis) != 0 { | ||
return appGenTxs, persistentPeers, fmt.Errorf( | ||
"account(s) %v not in genesis.json: %+v", strings.Join(accsNotInGenesis, " "), addrMap) | ||
} | ||
|
||
if delAcc.Coins.AmountOf(msg.Value.Denom).LT(msg.Value.Amount) { | ||
return appGenTxs, persistentPeers, fmt.Errorf( | ||
"insufficient fund for delegation %v: %v < %v", | ||
delAcc.Address, delAcc.Coins.AmountOf(msg.Value.Denom), msg.Value.Amount, | ||
) | ||
} | ||
|
||
// exclude itself from persistent peers | ||
if msg.Description.Moniker != moniker { | ||
addressesIPs = append(addressesIPs, nodeAddrIP) | ||
} | ||
} | ||
|
||
sort.Strings(addressesIPs) | ||
persistentPeers = strings.Join(addressesIPs, ",") | ||
|
||
return appGenTxs, persistentPeers, nil | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ineffectual assignment to
moduleBasics
(fromineffassign
)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good to know that this thing actually works :)