Separate genesis parsing from basecoin
This commit is contained in:
parent
c1d36eeb21
commit
df0f3a22da
11
app/app.go
11
app/app.go
|
@ -20,7 +20,6 @@ import (
|
||||||
|
|
||||||
//nolint
|
//nolint
|
||||||
const (
|
const (
|
||||||
ModuleNameBase = "base"
|
|
||||||
ChainKey = "chain_id"
|
ChainKey = "chain_id"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -207,16 +206,6 @@ func pubKeyIndex(val *abci.Validator, list []*abci.Validator) int {
|
||||||
return -1
|
return -1
|
||||||
}
|
}
|
||||||
|
|
||||||
// Splits the string at the first '/'.
|
|
||||||
// if there are none, assign default module ("base").
|
|
||||||
func splitKey(key string) (string, string) {
|
|
||||||
if strings.Contains(key, "/") {
|
|
||||||
keyParts := strings.SplitN(key, "/", 2)
|
|
||||||
return keyParts[0], keyParts[1]
|
|
||||||
}
|
|
||||||
return ModuleNameBase, key
|
|
||||||
}
|
|
||||||
|
|
||||||
func loadState(dbName string, cacheSize int) (*sm.State, error) {
|
func loadState(dbName string, cacheSize int) (*sm.State, error) {
|
||||||
// memory backed case, just for testing
|
// memory backed case, just for testing
|
||||||
if dbName == "" {
|
if dbName == "" {
|
||||||
|
|
|
@ -100,8 +100,8 @@ func (at *appTest) feeTx(coins coin.Coins, toll coin.Coin, sequence uint32) sdk.
|
||||||
|
|
||||||
// set the account on the app through InitState
|
// set the account on the app through InitState
|
||||||
func (at *appTest) initAccount(acct *coin.AccountWithKey) {
|
func (at *appTest) initAccount(acct *coin.AccountWithKey) {
|
||||||
res := at.app.InitState("coin/account", acct.MakeOption())
|
_, err := at.app.InitState("coin", "account", acct.MakeOption())
|
||||||
require.EqualValues(at.t, res, "Success")
|
require.Nil(at.t, err, "%+v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
// reset the in and out accs to be one account each with 7mycoin
|
// reset the in and out accs to be one account each with 7mycoin
|
||||||
|
@ -122,8 +122,8 @@ func (at *appTest) reset() {
|
||||||
)
|
)
|
||||||
require.Nil(at.t, err, "%+v", err)
|
require.Nil(at.t, err, "%+v", err)
|
||||||
|
|
||||||
res := at.app.InitState("base/chain_id", at.chainID)
|
_, err = at.app.InitState("base", "chain_id", at.chainID)
|
||||||
require.EqualValues(at.t, res, "Success")
|
require.Nil(at.t, err, "%+v", err)
|
||||||
|
|
||||||
at.initAccount(at.acctIn)
|
at.initAccount(at.acctIn)
|
||||||
at.initAccount(at.acctOut)
|
at.initAccount(at.acctOut)
|
||||||
|
@ -183,15 +183,15 @@ func TestInitState(t *testing.T) {
|
||||||
|
|
||||||
//testing ChainID
|
//testing ChainID
|
||||||
chainID := "testChain"
|
chainID := "testChain"
|
||||||
res := app.InitState("base/chain_id", chainID)
|
_, err = app.InitState("base", "chain_id", chainID)
|
||||||
|
require.Nil(err, "%+v", err)
|
||||||
assert.EqualValues(app.GetChainID(), chainID)
|
assert.EqualValues(app.GetChainID(), chainID)
|
||||||
assert.EqualValues(res, "Success")
|
|
||||||
|
|
||||||
// make a nice account...
|
// make a nice account...
|
||||||
bal := coin.Coins{{"atom", 77}, {"eth", 12}}
|
bal := coin.Coins{{"atom", 77}, {"eth", 12}}
|
||||||
acct := coin.NewAccountWithKey(bal)
|
acct := coin.NewAccountWithKey(bal)
|
||||||
res = app.InitState("coin/account", acct.MakeOption())
|
_, err = app.InitState("coin", "account", acct.MakeOption())
|
||||||
require.EqualValues(res, "Success")
|
require.Nil(err, "%+v", err)
|
||||||
|
|
||||||
// make sure it is set correctly, with some balance
|
// make sure it is set correctly, with some balance
|
||||||
coins, err := getBalance(acct.Actor(), app.Append())
|
coins, err := getBalance(acct.Actor(), app.Append())
|
||||||
|
@ -218,23 +218,22 @@ func TestInitState(t *testing.T) {
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
}`
|
}`
|
||||||
res = app.InitState("coin/account", unsortAcc)
|
_, err = app.InitState("coin", "account", unsortAcc)
|
||||||
require.EqualValues(res, "Success")
|
require.Nil(err, "%+v", err)
|
||||||
|
|
||||||
coins, err = getAddr(unsortAddr, app.Append())
|
coins, err = getAddr(unsortAddr, app.Append())
|
||||||
require.Nil(err)
|
require.Nil(err)
|
||||||
assert.True(coins.IsValid())
|
assert.True(coins.IsValid())
|
||||||
assert.Equal(unsortCoins, coins)
|
assert.Equal(unsortCoins, coins)
|
||||||
|
|
||||||
res = app.InitState("base/dslfkgjdas", "")
|
_, err = app.InitState("base", "dslfkgjdas", "")
|
||||||
assert.NotEqual(res, "Success")
|
require.NotNil(err)
|
||||||
|
|
||||||
res = app.InitState("dslfkgjdas", "")
|
_, err = app.InitState("", "dslfkgjdas", "")
|
||||||
assert.NotEqual(res, "Success")
|
require.NotNil(err)
|
||||||
|
|
||||||
res = app.InitState("dslfkgjdas/szfdjzs", "")
|
|
||||||
assert.NotEqual(res, "Success")
|
|
||||||
|
|
||||||
|
_, err = app.InitState("dslfkgjdas", "szfdjzs", "")
|
||||||
|
require.NotNil(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Test CheckTx and DeliverTx with insufficient and sufficient balance
|
// Test CheckTx and DeliverTx with insufficient and sufficient balance
|
||||||
|
|
35
app/bc.go
35
app/bc.go
|
@ -47,23 +47,18 @@ func NewBasecoinTick(handler sdk.Handler, tick Ticker, dbName string, cacheSize
|
||||||
|
|
||||||
// InitState - used to setup state (was SetOption)
|
// InitState - used to setup state (was SetOption)
|
||||||
// to be used by InitChain later
|
// to be used by InitChain later
|
||||||
func (app *Basecoin) InitState(key string, value string) string {
|
func (app *Basecoin) InitState(module, key, value string) (string, error) {
|
||||||
module, key := splitKey(key)
|
|
||||||
state := app.Append()
|
state := app.Append()
|
||||||
|
|
||||||
if module == ModuleNameBase {
|
if module == ModuleNameBase {
|
||||||
if key == ChainKey {
|
if key == ChainKey {
|
||||||
app.info.SetChainID(state, value)
|
app.info.SetChainID(state, value)
|
||||||
return "Success"
|
return "Success", nil
|
||||||
}
|
}
|
||||||
return fmt.Sprintf("Error: unknown base option: %s", key)
|
return "", fmt.Errorf("unknown base option: %s", key)
|
||||||
}
|
}
|
||||||
|
|
||||||
log, err := app.handler.InitState(app.Logger(), state, module, key, value)
|
return app.handler.InitState(app.Logger(), state, module, key, value)
|
||||||
if err == nil {
|
|
||||||
return log
|
|
||||||
}
|
|
||||||
return "Error: " + err.Error()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// DeliverTx - ABCI
|
// DeliverTx - ABCI
|
||||||
|
@ -121,3 +116,25 @@ func (app *Basecoin) BeginBlock(req abci.RequestBeginBlock) {
|
||||||
app.AddValChange(diff)
|
app.AddValChange(diff)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// LoadGenesis parses the genesis file and sets the initial
|
||||||
|
// state based on that
|
||||||
|
func (app *Basecoin) LoadGenesis(filePath string) error {
|
||||||
|
init, err := GetInitialState(filePath)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
// execute all the genesis init options
|
||||||
|
// abort on any error
|
||||||
|
fmt.Printf("%#v\n", init)
|
||||||
|
for _, mkv := range init {
|
||||||
|
log, _ := app.InitState(mkv.Module, mkv.Key, mkv.Value)
|
||||||
|
// TODO: error out on bad options??
|
||||||
|
// if err != nil {
|
||||||
|
// return err
|
||||||
|
// }
|
||||||
|
app.Logger().Info(log)
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
|
@ -2,33 +2,55 @@ package app
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
|
"strings"
|
||||||
|
|
||||||
"github.com/pkg/errors"
|
"github.com/pkg/errors"
|
||||||
|
|
||||||
cmn "github.com/tendermint/tmlibs/common"
|
cmn "github.com/tendermint/tmlibs/common"
|
||||||
)
|
)
|
||||||
|
|
||||||
// LoadGenesis - Load the genesis file into memory
|
//nolint
|
||||||
func (app *Basecoin) LoadGenesis(path string) error {
|
const (
|
||||||
genDoc, err := loadGenesis(path)
|
ModuleNameBase = "base"
|
||||||
if err != nil {
|
)
|
||||||
return err
|
|
||||||
|
// InitState just holds module/key/value triples from
|
||||||
|
// parsing the genesis file
|
||||||
|
type InitState struct {
|
||||||
|
Module string
|
||||||
|
Key string
|
||||||
|
Value string
|
||||||
}
|
}
|
||||||
|
|
||||||
// set chain_id
|
// GetInitialState parses the genesis file in a format
|
||||||
app.InitState("base/chain_id", genDoc.ChainID)
|
// that can easily be handed into InitState modules
|
||||||
|
func GetInitialState(path string) ([]InitState, error) {
|
||||||
|
genDoc, err := loadGenesis(path)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
opts := genDoc.AppOptions
|
||||||
|
cnt := 1 + len(opts.Accounts) + len(opts.pluginOptions)
|
||||||
|
res := make([]InitState, cnt)
|
||||||
|
|
||||||
|
res[0] = InitState{ModuleNameBase, ChainKey, genDoc.ChainID}
|
||||||
|
i := 1
|
||||||
|
|
||||||
// set accounts
|
// set accounts
|
||||||
for _, acct := range genDoc.AppOptions.Accounts {
|
for _, acct := range opts.Accounts {
|
||||||
_ = app.InitState("coin/account", string(acct))
|
res[i] = InitState{"coin", "account", string(acct)}
|
||||||
|
i++
|
||||||
}
|
}
|
||||||
|
|
||||||
// set plugin options
|
// set plugin options
|
||||||
for _, kv := range genDoc.AppOptions.pluginOptions {
|
for _, kv := range opts.pluginOptions {
|
||||||
_ = app.InitState(kv.Key, kv.Value)
|
module, key := splitKey(kv.Key)
|
||||||
|
res[i] = InitState{module, key, kv.Value}
|
||||||
|
i++
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return res, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
type keyValue struct {
|
type keyValue struct {
|
||||||
|
@ -97,3 +119,13 @@ func parseGenesisList(kvzIn []json.RawMessage) (kvz []keyValue, err error) {
|
||||||
}
|
}
|
||||||
return kvz, nil
|
return kvz, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Splits the string at the first '/'.
|
||||||
|
// if there are none, assign default module ("base").
|
||||||
|
func splitKey(key string) (string, string) {
|
||||||
|
if strings.Contains(key, "/") {
|
||||||
|
keyParts := strings.SplitN(key, "/", 2)
|
||||||
|
return keyParts[0], keyParts[1]
|
||||||
|
}
|
||||||
|
return ModuleNameBase, key
|
||||||
|
}
|
||||||
|
|
|
@ -69,8 +69,8 @@ func NewBenchApp(h sdk.Handler, chainID string, n int,
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
res := app.InitState("base/chain_id", chainID)
|
_, err = app.InitState("base", "chain_id", chainID)
|
||||||
if res != "Success" {
|
if err != nil {
|
||||||
panic("cannot set chain")
|
panic("cannot set chain")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -79,8 +79,8 @@ func NewBenchApp(h sdk.Handler, chainID string, n int,
|
||||||
accts := make([]*coin.AccountWithKey, n)
|
accts := make([]*coin.AccountWithKey, n)
|
||||||
for i := 0; i < n; i++ {
|
for i := 0; i < n; i++ {
|
||||||
accts[i] = coin.NewAccountWithKey(money)
|
accts[i] = coin.NewAccountWithKey(money)
|
||||||
res := app.InitState("coin/account", accts[i].MakeOption())
|
_, err = app.InitState("coin", "account", accts[i].MakeOption())
|
||||||
if res != "Success" {
|
if err != nil {
|
||||||
panic("can't set account")
|
panic("can't set account")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -35,13 +35,14 @@ func TestCounterPlugin(t *testing.T) {
|
||||||
logger.With("module", "app"),
|
logger.With("module", "app"),
|
||||||
)
|
)
|
||||||
require.Nil(err, "%+v", err)
|
require.Nil(err, "%+v", err)
|
||||||
bcApp.InitState("base/chain_id", chainID)
|
_, err = bcApp.InitState("base", "chain_id", chainID)
|
||||||
|
require.Nil(err, "%+v", err)
|
||||||
|
|
||||||
// Account initialization
|
// Account initialization
|
||||||
bal := coin.Coins{{"", 1000}, {"gold", 1000}}
|
bal := coin.Coins{{"", 1000}, {"gold", 1000}}
|
||||||
acct := coin.NewAccountWithKey(bal)
|
acct := coin.NewAccountWithKey(bal)
|
||||||
log := bcApp.InitState("coin/account", acct.MakeOption())
|
_, err = bcApp.InitState("coin", "account", acct.MakeOption())
|
||||||
require.Equal("Success", log)
|
require.Nil(err, "%+v", err)
|
||||||
|
|
||||||
// Deliver a CounterTx
|
// Deliver a CounterTx
|
||||||
DeliverCounterTx := func(valid bool, counterFee coin.Coins, sequence uint32) abci.Result {
|
DeliverCounterTx := func(valid bool, counterFee coin.Coins, sequence uint32) abci.Result {
|
||||||
|
|
Loading…
Reference in New Issue