Skip to content

Commit

Permalink
add plugin_options and fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ebuchman committed Mar 14, 2017
1 parent 03ea00f commit 0943329
Show file tree
Hide file tree
Showing 10 changed files with 101 additions and 48 deletions.
42 changes: 41 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,52 @@
# Changelog

## 0.3.0 (March 13, 2017)
## 0.3.0 (March 14, 2017)

BREAKING CHANGES:

- Remove `--data` flag and use `BCHOME` to set the home directory (defaults to `~/.basecoin`)
- Remove `--in-proc` flag and start Tendermint in-process by default (expect Tendermint files in $BCHOME/tendermint).
To start just the ABCI app/server, use `basecoin start --without-tendermint`.
- Consolidate genesis files so the Basecoin genesis is an object under `app_options` in Tendermint genesis. For instance:

```
{
"app_hash": "",
"chain_id": "foo_bar_chain",
"genesis_time": "0001-01-01T00:00:00.000Z",
"validators": [
{
"amount": 10,
"name": "",
"pub_key": [
1,
"7B90EA87E7DC0C7145C8C48C08992BE271C7234134343E8A8E8008E617DE7B30"
]
}
],
"app_options": {
"accounts": [{
"pub_key": {
"type": "ed25519",
"data": "6880db93598e283a67c4d88fc67a8858aa2de70f713fe94a5109e29c137100c2"
},
"coins": [
{
"denom": "blank",
"amount": 12345
},
{
"denom": "ETH",
"amount": 654321
}
]
}],
"plugin_options": ["plugin1/key1", "value1", "plugin1/key2", "value2"]
}
}
```

Note the array of key-value pairs is now under `app_options.plugin_options` while the `app_options` themselves are well formed.

FEATURES:

Expand Down
2 changes: 1 addition & 1 deletion app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ func (app *Basecoin) SetOption(key string, value string) string {
if plugin == nil {
return "Invalid plugin name: " + pluginName
}
log.Info("SetOption on plugin", "plugin", pluginName, "key", key, "value", value)
log.Notice("SetOption on plugin", "plugin", pluginName, "key", key, "value", value)
return plugin.SetOption(app.state, key, value)
} else {
// Set option on basecoin
Expand Down
53 changes: 33 additions & 20 deletions app/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,32 +2,37 @@ package app

import (
"encoding/json"
"fmt"

"github.com/pkg/errors"
"github.com/tendermint/basecoin/types"
cmn "github.com/tendermint/go-common"
"github.com/tendermint/go-wire"
tmtypes "github.com/tendermint/tendermint/types"
//tmtypes "github.com/tendermint/tendermint/types"
)

func (app *Basecoin) LoadGenesis(path string) error {
tmDoc, appDoc, err := loadGenesis(path)
genDoc, err := loadGenesis(path)
if err != nil {
return err
}
fmt.Println("TMGendoc", tmDoc)
fmt.Println("AppGendoc", appDoc)

app.SetOption("base/chain_id", appDoc.ChainID)
for _, acc := range appDoc.Accounts {
// set chain_id
app.SetOption("base/chain_id", genDoc.ChainID)

// set accounts
for _, acc := range genDoc.AppOptions.Accounts {
accBytes, err := json.Marshal(acc)
if err != nil {
return err
}
r := app.SetOption("base/account", string(accBytes))
// TODO: SetOption returns an error
log.Notice("SetOption", "result", r)
log.Notice("Done setting Account via SetOption", "result", r)
}

// set plugin options
for _, kv := range genDoc.AppOptions.pluginOptions {
r := app.SetOption(kv.Key, kv.Value)
log.Notice("Done setting Plugin key-value pair via SetOption", "result", r, "k", kv.Key, "v", kv.Value)
}
return nil
}
Expand All @@ -39,32 +44,40 @@ type keyValue struct {

// includes tendermint (in the json, we ignore here)
type FullGenesisDoc struct {
ChainID string `json:"chain_id"`
AppOptions *GenesisDoc `json:"app_options"`
}

type GenesisDoc struct {
ChainID string `json:"chain_id"`
Accounts []types.Account `json:"accounts"`
Accounts []types.Account `json:"accounts"`
PluginOptions []json.RawMessage `json:"plugin_options"`

pluginOptions []keyValue // unmarshaled rawmessages
}

func loadGenesis(filePath string) (*tmtypes.GenesisDoc, *GenesisDoc, error) {
func loadGenesis(filePath string) (*FullGenesisDoc, error) {
bytes, err := cmn.ReadFile(filePath)
if err != nil {
return nil, nil, errors.Wrap(err, "loading genesis file")
return nil, errors.Wrap(err, "loading genesis file")
}

tmGenesis := new(tmtypes.GenesisDoc)
appGenesis := new(FullGenesisDoc)

// the tendermint genesis is go-wire
err = wire.ReadJSONBytes(bytes, tmGenesis)
// tmGenesis := new(tmtypes.GenesisDoc)
// err = wire.ReadJSONBytes(bytes, tmGenesis)

// the basecoin genesis go-data :)
err = json.Unmarshal(bytes, appGenesis)
genDoc := new(FullGenesisDoc)
err = json.Unmarshal(bytes, genDoc)
if err != nil {
return nil, errors.Wrap(err, "unmarshaling genesis file")
}

pluginOpts, err := parseGenesisList(genDoc.AppOptions.PluginOptions)
if err != nil {
return nil, nil, errors.Wrap(err, "unmarshaling genesis file")
return nil, err
}
return tmGenesis, appGenesis.AppOptions, nil
genDoc.AppOptions.pluginOptions = pluginOpts
return genDoc, nil
}

func parseGenesisList(kvz_ []json.RawMessage) (kvz []keyValue, err error) {
Expand Down
37 changes: 20 additions & 17 deletions app/testdata/genesis.json
Original file line number Diff line number Diff line change
@@ -1,19 +1,22 @@
[
"base/chainID", "foo_bar_chain",
"base/account", {
"pub_key": {
"type": "ed25519",
"data": "6880db93598e283a67c4d88fc67a8858aa2de70f713fe94a5109e29c137100c2"
},
"coins": [
{
"denom": "blank",
"amount": 12345
{
"chain_id": "foo_bar_chain",
"app_options": {
"accounts": [{
"pub_key": {
"type": "ed25519",
"data": "6880db93598e283a67c4d88fc67a8858aa2de70f713fe94a5109e29c137100c2"
},
{
"denom": "ETH",
"amount": 654321
}
]
"coins": [
{
"denom": "blank",
"amount": 12345
},
{
"denom": "ETH",
"amount": 654321
}
]
}],
"plugin_options": ["plugin1/key1", "value1", "plugin1/key2", "value2"]
}
]
}
3 changes: 1 addition & 2 deletions cmd/commands/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ const privValJSON = `{

const genesisJSON = `{
"app_hash": "",
"chain_id": "test-chain-Ppk1h3",
"chain_id": "test_chain_id",
"genesis_time": "0001-01-01T00:00:00.000Z",
"validators": [
{
Expand All @@ -82,7 +82,6 @@ const genesisJSON = `{
}
],
"app_options": {
"chain_id": "test_chain_id",
"accounts": [{
"pub_key": {
"type": "ed25519",
Expand Down
1 change: 0 additions & 1 deletion demo/data/chain1/genesis.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
}
],
"app_options": {
"chain_id": "test_chain_1",
"accounts": [
{
"pub_key": {
Expand Down
1 change: 0 additions & 1 deletion demo/data/chain2/genesis.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
}
],
"app_options": {
"chain_id": "test_chain_2",
"accounts": [
{
"pub_key": {
Expand Down
4 changes: 2 additions & 2 deletions docs/guide/basecoin-tool.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ Now we can make a `genesis.json` file and add an account with our public key:

```json
[
"base/chainID", "example-chain",
"base/chain_id", "example-chain",
"base/account", {
"pub_key": [1, "43AA6C88034F9EB8D2717CA4BBFCBA745EFF19B13EFCD6F339EDBAAAFCD2F7B3"],
"coins": [
Expand All @@ -95,7 +95,7 @@ Now we can make a `genesis.json` file and add an account with our public key:
```

Here we've granted ourselves `1000000000` units of the `gold` token.
Note that we've also set the `base/chainID` to be `example-chain`.
Note that we've also set the `base/chain_id` to be `example-chain`.
All transactions must therefore include the `--chain_id example-chain` in order to make sure they are valid for this chain.
Previously, we didn't need this flag because we were using the default chain ID ("test_chain_id").
Now that we're using a custom chain, we need to specify the chain explicitly on the command line.
Expand Down
2 changes: 1 addition & 1 deletion plugins/counter/counter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ func TestCounterPlugin(t *testing.T) {
eyesCli := eyescli.NewLocalClient("", 0)
chainID := "test_chain_id"
bcApp := app.NewBasecoin(eyesCli)
bcApp.SetOption("base/chainID", chainID)
bcApp.SetOption("base/chain_id", chainID)
// t.Log(bcApp.Info())

// Add Counter plugin
Expand Down
4 changes: 2 additions & 2 deletions tests/tmsp/tmsp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ func TestSendTx(t *testing.T) {
eyesCli := eyescli.NewLocalClient("", 0)
chainID := "test_chain_id"
bcApp := app.NewBasecoin(eyesCli)
bcApp.SetOption("base/chainID", chainID)
bcApp.SetOption("base/chain_id", chainID)
// t.Log(bcApp.Info())

test1PrivAcc := types.PrivAccountFromSecret("test1")
Expand Down Expand Up @@ -64,7 +64,7 @@ func TestSequence(t *testing.T) {
eyesCli := eyescli.NewLocalClient("", 0)
chainID := "test_chain_id"
bcApp := app.NewBasecoin(eyesCli)
bcApp.SetOption("base/chainID", chainID)
bcApp.SetOption("base/chain_id", chainID)
// t.Log(bcApp.Info())

// Get the test account
Expand Down

0 comments on commit 0943329

Please sign in to comment.