Write dispatcher, change SetOption arguments
This commit is contained in:
+27
-6
@@ -1,6 +1,9 @@
|
||||
package app
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
abci "github.com/tendermint/abci/types"
|
||||
"github.com/tendermint/basecoin"
|
||||
eyes "github.com/tendermint/merkleeyes/client"
|
||||
@@ -15,8 +18,8 @@ import (
|
||||
)
|
||||
|
||||
const (
|
||||
PluginNameBase = "base"
|
||||
ChainKey = "base/chain_id"
|
||||
ModuleNameBase = "base"
|
||||
ChainKey = "chain_id"
|
||||
)
|
||||
|
||||
type Basecoin struct {
|
||||
@@ -66,18 +69,26 @@ func (app *Basecoin) Info() abci.ResponseInfo {
|
||||
|
||||
// ABCI::SetOption
|
||||
func (app *Basecoin) SetOption(key string, value string) string {
|
||||
if key == ChainKey {
|
||||
app.state.SetChainID(value)
|
||||
return "Success"
|
||||
module, prefix := splitKey(key)
|
||||
if module == ModuleNameBase {
|
||||
return app.setBaseOption(prefix, value)
|
||||
}
|
||||
|
||||
log, err := app.handler.SetOption(app.logger, app.state, key, value)
|
||||
log, err := app.handler.SetOption(app.logger, app.state, module, prefix, value)
|
||||
if err == nil {
|
||||
return log
|
||||
}
|
||||
return "Error: " + err.Error()
|
||||
}
|
||||
|
||||
func (app *Basecoin) setBaseOption(key, value string) string {
|
||||
if key == ChainKey {
|
||||
app.state.SetChainID(value)
|
||||
return "Success"
|
||||
}
|
||||
return fmt.Sprintf("Error: unknown base option: %s", key)
|
||||
}
|
||||
|
||||
// ABCI::DeliverTx
|
||||
func (app *Basecoin) DeliverTx(txBytes []byte) abci.Result {
|
||||
tx, err := basecoin.LoadTx(txBytes)
|
||||
@@ -178,3 +189,13 @@ func (app *Basecoin) EndBlock(height uint64) (res abci.ResponseEndBlock) {
|
||||
// }
|
||||
return
|
||||
}
|
||||
|
||||
// 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
|
||||
}
|
||||
|
||||
+19
-3
@@ -58,7 +58,7 @@ func (at *appTest) getTx(seq int, coins types.Coins) basecoin.Tx {
|
||||
func (at *appTest) acc2app(acc types.Account) {
|
||||
accBytes, err := json.Marshal(acc)
|
||||
require.Nil(at.t, err)
|
||||
res := at.app.SetOption("base/account", string(accBytes))
|
||||
res := at.app.SetOption("coin/account", string(accBytes))
|
||||
require.EqualValues(at.t, res, "Success")
|
||||
}
|
||||
|
||||
@@ -137,7 +137,7 @@ func TestSetOption(t *testing.T) {
|
||||
accIn := types.MakeAcc("input0").Account
|
||||
accsInBytes, err := json.Marshal(accIn)
|
||||
assert.Nil(err)
|
||||
res = app.SetOption("base/account", string(accsInBytes))
|
||||
res = app.SetOption("coin/account", string(accsInBytes))
|
||||
require.EqualValues(res, "Success")
|
||||
|
||||
// make sure it is set correctly, with some balance
|
||||
@@ -165,7 +165,7 @@ func TestSetOption(t *testing.T) {
|
||||
}
|
||||
]
|
||||
}`
|
||||
res = app.SetOption("base/account", unsortAcc)
|
||||
res = app.SetOption("coin/account", unsortAcc)
|
||||
require.EqualValues(res, "Success")
|
||||
|
||||
coins, err = getAddr(unsortAddr, app.state)
|
||||
@@ -234,3 +234,19 @@ func TestQuery(t *testing.T) {
|
||||
})
|
||||
assert.NotEqual(resQueryPreCommit, resQueryPostCommit, "Query should change before/after commit")
|
||||
}
|
||||
|
||||
func TestSplitKey(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
prefix, suffix := splitKey("foo/bar")
|
||||
assert.EqualValues("foo", prefix)
|
||||
assert.EqualValues("bar", suffix)
|
||||
|
||||
prefix, suffix = splitKey("foobar")
|
||||
assert.EqualValues("base", prefix)
|
||||
assert.EqualValues("foobar", suffix)
|
||||
|
||||
prefix, suffix = splitKey("some/complex/issue")
|
||||
assert.EqualValues("some", prefix)
|
||||
assert.EqualValues("complex/issue", suffix)
|
||||
|
||||
}
|
||||
|
||||
+1
-1
@@ -19,7 +19,7 @@ func (app *Basecoin) LoadGenesis(path string) error {
|
||||
|
||||
// set accounts
|
||||
for _, acct := range genDoc.AppOptions.Accounts {
|
||||
_ = app.SetOption("base/account", string(acct))
|
||||
_ = app.SetOption("coin/account", string(acct))
|
||||
}
|
||||
|
||||
// set plugin options
|
||||
|
||||
Reference in New Issue
Block a user