forked from CyberMiles/travis
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
30 changed files
with
1,973 additions
and
55 deletions.
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
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 |
---|---|---|
@@ -0,0 +1,113 @@ | ||
package genesis | ||
|
||
import ( | ||
"encoding/json" | ||
|
||
sdk "github.com/cosmos/cosmos-sdk" | ||
"github.com/pkg/errors" | ||
|
||
cmn "github.com/tendermint/tmlibs/common" | ||
) | ||
|
||
// KeyDelimiter is used to separate module and key in | ||
// the options | ||
const KeyDelimiter = "/" | ||
|
||
// Option just holds module/key/value triples from | ||
// parsing the genesis file | ||
type Option struct { | ||
Module string | ||
Key string | ||
Value string | ||
} | ||
|
||
// InitStater is anything that can handle app options | ||
// from genesis file. Setting the merkle store, config options, | ||
// or anything else | ||
type InitStater interface { | ||
InitState(module, key, value string) error | ||
} | ||
|
||
// Load parses the genesis file and sets the initial | ||
// state based on that | ||
func Load(app InitStater, filePath string) error { | ||
opts, err := GetOptions(filePath) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
// execute all the genesis init options | ||
// abort on any error | ||
for _, opt := range opts { | ||
err = app.InitState(opt.Module, opt.Key, opt.Value) | ||
if err != nil { | ||
return err | ||
} | ||
} | ||
return nil | ||
} | ||
|
||
// GetOptions parses the genesis file in a format | ||
// that can easily be handed into InitStaters | ||
func GetOptions(path string) ([]Option, error) { | ||
genDoc, err := load(path) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
opts := genDoc.AppOptions | ||
validators := genDoc.Validators | ||
cnt := 1 + len(opts.Accounts) + len(validators) | ||
res := make([]Option, 0, cnt) | ||
res = append(res, Option{sdk.ModuleNameBase, sdk.ChainKey, genDoc.ChainID}) | ||
|
||
// set accounts | ||
//for _, validator := range validators { | ||
// res = append(res, Option{"coin", "validator", string(validator)}) | ||
//} | ||
res = append(res, Option{"stake", "validators", string(validators)}) | ||
|
||
// set accounts | ||
for _, acct := range opts.Accounts { | ||
res = append(res, Option{"coin", "account", string(acct)}) | ||
} | ||
|
||
return res, nil | ||
} | ||
|
||
type keyValue struct { | ||
Key string `json:"key"` | ||
Value string `json:"value"` | ||
} | ||
|
||
// FullDoc - includes tendermint (in the json, we ignore here) | ||
type FullDoc struct { | ||
ChainID string `json:"chain_id"` | ||
AppOptions *Doc `json:"app_options"` | ||
Validators json.RawMessage `json:"validators"` | ||
} | ||
|
||
// Doc - All genesis values | ||
type Doc struct { | ||
Accounts []json.RawMessage `json:"accounts"` | ||
} | ||
|
||
func load(filePath string) (*FullDoc, error) { | ||
bytes, err := cmn.ReadFile(filePath) | ||
if err != nil { | ||
return nil, errors.Wrap(err, "loading genesis file") | ||
} | ||
|
||
// the basecoin genesis go-wire/data :) | ||
genDoc := new(FullDoc) | ||
err = json.Unmarshal(bytes, genDoc) | ||
if err != nil { | ||
return nil, errors.Wrap(err, "unmarshaling genesis file") | ||
} | ||
|
||
if genDoc.AppOptions == nil { | ||
genDoc.AppOptions = new(Doc) | ||
} | ||
|
||
return genDoc, nil | ||
} |
Oops, something went wrong.