Skip to content
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

Redo gconf #503

Merged
merged 23 commits into from
Apr 18, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@

## HEAD

- `gconf` package was reimplemented from scratch
- `x/cash` is using new `gconf` package for configuration. New genesis path is
ethanfrey marked this conversation as resolved.
Show resolved Hide resolved
used. To update genesis file, replace "gconf": { "cash:xyz": "foo" }
with "conf": { "cash": { "xyz": "foo" } }
- Tests were cleaned up and no use testify or convey packages. A new package
`weavetest/assert` contains test helpers
- Simplify transaction message unpacking with `weave.LoadMsg`
Expand Down
39 changes: 28 additions & 11 deletions cmd/bcpd/app/app_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,16 @@ func toHex(s string) string {
return strings.ToUpper(h)
}

func fromHex(t testing.TB, s string) weave.Address {
t.Helper()

h, err := hex.DecodeString(s)
if err != nil {
t.Fatalf("cannot decode hex representation: %s", err)
}
return h
}

func TestQuery(t *testing.T) {
chainID := "test-net-22"
mainAccount := &account{pk: crypto.GenPrivKeyEd25519()}
Expand Down Expand Up @@ -185,7 +195,7 @@ func (a *account) address() []byte {

// newTestApp creates a new app with a wallet for each account
// coins and tokens are the same across all accounts and calls
func newTestApp(t require.TestingT, chainID string, accounts []*account) app.BaseApp {
func newTestApp(t testing.TB, chainID string, accounts []*account) app.BaseApp {
// no minimum fee, in-memory data-store
abciApp, err := GenerateApp(opts)
require.NoError(t, err)
Expand All @@ -205,7 +215,7 @@ func newTestApp(t require.TestingT, chainID string, accounts []*account) app.Bas
return myApp
}

func newMultisigTestApp(t require.TestingT, chainID string, contracts []*contract) app.BaseApp {
func newMultisigTestApp(t testing.TB, chainID string, contracts []*contract) app.BaseApp {
// no minimum fee, in-memory data-store
abciApp, err := GenerateApp(opts)
require.NoError(t, err)
Expand All @@ -225,18 +235,23 @@ func newMultisigTestApp(t require.TestingT, chainID string, contracts []*contrac
return myApp
}

func withWalletAppState(t require.TestingT, accounts []*account) string {
func withWalletAppState(t testing.TB, accounts []*account) string {
type wallet struct {
Address weave.Address `json:"address"`
Coins coin.Coins `json:"coins"`
}
type conf struct {
Cash cash.Configuration `json:"cash"`
}
state := struct {
Cash []wallet `json:"cash"`
Gconf map[string]interface{} `json:"gconf"`
Cash []wallet `json:"cash"`
Conf conf `json:"conf"`
}{
Gconf: map[string]interface{}{
cash.GconfCollectorAddress: "66616b652d636f6c6c6563746f722d61646472657373",
cash.GconfMinimalFee: coin.Coin{}, // no fee
Conf: conf{
Cash: cash.Configuration{
CollectorAddress: fromHex(t, "fe1132f9ed1fb1c2e9c09ff297b619654387bb4a"),
MinimalFee: coin.Coin{}, // no fee
},
},
}

Expand Down Expand Up @@ -278,7 +293,7 @@ func (c *contract) signers() []*account {
return c.accountSigs[:c.threshold]
}

func appStateGenesis(t require.TestingT, contracts []*contract) string {
func appStateGenesis(t testing.TB, contracts []*contract) string {
type dt map[string]interface{}
type arr []interface{}

Expand All @@ -300,8 +315,10 @@ func appStateGenesis(t require.TestingT, contracts []*contract) string {
dt{"ticker": "ETH", "name": "Smells like ethereum"},
dt{"ticker": "FRNK", "name": "Frankie"},
},
"gconf": dt{
"cash:collector_address": "66616b652d636f6c6c6563746f722d61646472657373",
"conf": dt{
"cash": cash.Configuration{
CollectorAddress: fromHex(t, "fe1132f9ed1fb1c2e9c09ff297b619654387bb4a"),
},
},
}

Expand Down
2 changes: 0 additions & 2 deletions cmd/bcpd/app/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (
"github.com/iov-one/weave/coin"
"github.com/iov-one/weave/commands/server"
"github.com/iov-one/weave/crypto"
"github.com/iov-one/weave/gconf"
"github.com/iov-one/weave/x/cash"
"github.com/iov-one/weave/x/currency"
"github.com/iov-one/weave/x/distribution"
Expand Down Expand Up @@ -83,7 +82,6 @@ func GenerateApp(options *server.Options) (abci.Application, error) {
return nil, err
}
application.WithInit(app.ChainInitializers(
&gconf.Initializer{},
&multisig.Initializer{},
&cash.Initializer{},
&currency.Initializer{},
Expand Down
17 changes: 10 additions & 7 deletions cmd/bnsd/app/app_performance_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package app

import (
"encoding/hex"
"io/ioutil"
"os"
"testing"
Expand All @@ -22,9 +21,11 @@ func BenchmarkBnsdEmptyBlock(b *testing.B) {

type dict map[string]interface{}
genesis := dict{
"gconf": map[string]interface{}{
cash.GconfCollectorAddress: hex.EncodeToString(aliceAddr),
cash.GconfMinimalFee: coin.Coin{}, // no fee
"conf": dict{
"cash": cash.Configuration{
CollectorAddress: aliceAddr,
MinimalFee: coin.Coin{}, // no fee
},
},
}

Expand Down Expand Up @@ -78,9 +79,11 @@ func BenchmarkBNSDSendToken(b *testing.B) {
"name": "Main token of this chain",
},
},
"gconf": dict{
cash.GconfCollectorAddress: hex.EncodeToString(carol),
cash.GconfMinimalFee: fee,
"conf": dict{
"cash": cash.Configuration{
CollectorAddress: carol,
MinimalFee: fee,
},
},
}
}
Expand Down
9 changes: 4 additions & 5 deletions cmd/bnsd/app/app_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ package app_test

import (
"encoding/hex"
"github.com/iov-one/weave"
"github.com/iov-one/weave/weavetest"
"github.com/tendermint/tendermint/libs/common"
"strings"
"testing"

"github.com/iov-one/weave"
"github.com/tendermint/tendermint/libs/common"

weaveApp "github.com/iov-one/weave/app"
"github.com/iov-one/weave/cmd/bnsd/app"
"github.com/iov-one/weave/cmd/bnsd/app/testdata/fixtures"
Expand Down Expand Up @@ -44,8 +44,7 @@ func TestApp(t *testing.T) {

// ensure 4 keys for all accounts that are modified by a transaction
require.Equal(t, 4, len(dres.Tags), tagsAsString(dres.Tags))
feeDistAddr := weave.NewCondition("dist", "revenue", weavetest.SequenceID(1)).
Address()
feeDistAddr := weave.Condition("dist/revenue/0000000000000001").Address()
wantKeys := []string{
toHex("cash:") + addr.String(), // sender balance decreased
toHex("cash:") + addr2.String(), // receiver balance increased
Expand Down
2 changes: 0 additions & 2 deletions cmd/bnsd/app/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import (
"github.com/iov-one/weave/coin"
"github.com/iov-one/weave/commands/server"
"github.com/iov-one/weave/crypto"
"github.com/iov-one/weave/gconf"
"github.com/iov-one/weave/orm"
"github.com/iov-one/weave/x/cash"
"github.com/iov-one/weave/x/currency"
Expand Down Expand Up @@ -99,7 +98,6 @@ func GenerateApp(options *server.Options) (abci.Application, error) {
// DecorateApp adds initializers and Logger to an Application
func DecorateApp(application app.BaseApp, logger log.Logger) app.BaseApp {
application.WithInit(app.ChainInitializers(
&gconf.Initializer{},
&multisig.Initializer{},
&cash.Initializer{},
&currency.Initializer{},
Expand Down
20 changes: 11 additions & 9 deletions cmd/bnsd/app/testdata/fixtures/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ import (
"math/rand"
"time"

"github.com/iov-one/weave/coin"
"github.com/iov-one/weave"
"github.com/iov-one/weave/commands/server"
"github.com/iov-one/weave/cmd/bnsd/app"
"github.com/iov-one/weave/coin"
"github.com/iov-one/weave/commands/server"
"github.com/iov-one/weave/crypto"
"github.com/iov-one/weave/x/cash"
abci "github.com/tendermint/tendermint/abci/types"
Expand Down Expand Up @@ -37,10 +37,10 @@ func NewApp() *AppFixture {

func (f AppFixture) Build() abci.Application {
opts := &server.Options{
MinFee: coin.Coin{},
Home:"",
MinFee: coin.Coin{},
Home: "",
Logger: log.NewNopLogger(),
Debug: true,
Debug: true,
}
myApp, err := app.GenerateApp(opts)
if err != nil {
Expand Down Expand Up @@ -76,6 +76,12 @@ func appStateGenesis(keyAddress weave.Address) []byte {
},
},
},
"conf": dict{
"cash": cash.Configuration{
CollectorAddress: weave.Condition("dist/revenue/0000000000000001").Address(),
MinimalFee: coin.NewCoin(0, 10000000, "FRNK"),
},
},
"currencies": []interface{}{
dict{
"ticker": "FRNK",
Expand Down Expand Up @@ -117,10 +123,6 @@ func appStateGenesis(keyAddress weave.Address) []byte {
"timeout": time.Now().Add(10000 * time.Hour),
},
},
"gconf": map[string]interface{}{
cash.GconfCollectorAddress: "cond:dist/revenue/0000000000000001",
cash.GconfMinimalFee: "0.01 FRNK",
},
"msgfee": []interface{}{
dict{
"msg_path": "distribution/newrevenue",
Expand Down
8 changes: 5 additions & 3 deletions cmd/bnsd/client/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,9 +91,11 @@ func initGenesis(filename string, addr weave.Address) error {
"coins": coin.Coins{&initBalance},
},
},
"gconf": map[string]interface{}{
cash.GconfCollectorAddress: "fake-collector-address",
cash.GconfMinimalFee: coin.Coin{}, // no fee
"conf": map[string]interface{}{
"cash": cash.Configuration{
CollectorAddress: weave.NewAddress([]byte("fake-collector-address")),
MinimalFee: coin.Coin{}, // no fee
},
},
})
if err != nil {
Expand Down
10 changes: 6 additions & 4 deletions cmd/bnsd/scenarios/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import (
cfg "github.com/tendermint/tendermint/config"
"github.com/tendermint/tendermint/libs/log"
nm "github.com/tendermint/tendermint/node"
"github.com/tendermint/tendermint/rpc/test"
rpctest "github.com/tendermint/tendermint/rpc/test"
tm "github.com/tendermint/tendermint/types"
)

Expand Down Expand Up @@ -194,9 +194,11 @@ func initGenesis(filename string, addr weave.Address) (*tm.GenesisDoc, error) {
"timeout": time.Now().Add(10000 * time.Hour),
},
},
"gconf": map[string]interface{}{
cash.GconfCollectorAddress: "cond:dist/revenue/0000000000000001",
cash.GconfMinimalFee: antiSpamFee,
"conf": dict{
"cash": cash.Configuration{
CollectorAddress: weave.Condition("dist/revenue/0000000000000001").Address(),
MinimalFee: antiSpamFee,
},
},
"msgfee": []interface{}{
dict{
Expand Down
12 changes: 11 additions & 1 deletion conditions.go
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,16 @@ func (a *Address) UnmarshalJSON(raw []byte) error {
}
}

// Clone provides an independent copy of an address.
func (a Address) Clone() Address {
if a == nil {
return nil
}
cpy := make(Address, len(a))
copy(cpy, a)
return cpy
}

// String returns a human readable string.
// Currently hex, may move to bech32
func (a Address) String() string {
Expand All @@ -208,7 +218,7 @@ func (a Address) Validate() error {
return errors.ErrEmpty
}
if len(a) != AddressLength {
return errors.Wrapf(errors.ErrInvalidInput, "address: %v", a)
return errors.Wrapf(errors.ErrInvalidInput, "invalid address length: %v", a)
}
return nil
}
Expand Down
18 changes: 15 additions & 3 deletions examples/mycoind/app/app_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package app

import (
"bytes"
"encoding/hex"
"encoding/json"
"testing"

Expand Down Expand Up @@ -38,9 +39,11 @@ func testInitChain(t *testing.T, myApp app.BaseApp, addr string) {
},
},
},
"gconf": dict{
cash.GconfCollectorAddress: "66616b652d636f6c6c6563746f722d61646472657373",
cash.GconfMinimalFee: coin.Coin{Whole: 0}, // no fee
"conf": dict{
"cash": cash.Configuration{
CollectorAddress: fromHex(t, "3b11c732b8fc1f09beb34031302fe2ab347c5c14"),
MinimalFee: coin.Coin{Whole: 0}, // no fee
},
},
})
if err != nil {
Expand All @@ -55,6 +58,15 @@ func testInitChain(t *testing.T, myApp app.BaseApp, addr string) {

}

func fromHex(t testing.TB, s string) weave.Address {
t.Helper()
raw, err := hex.DecodeString(s)
if err != nil {
t.Fatalf("cannot decode hex encoded address %q: %s", s, err)
}
return raw
}

// testCommit will commit at height h and return new hash
func testCommit(t *testing.T, myApp app.BaseApp, h int64) []byte {
// Commit first block, make sure non-nil hash
Expand Down
16 changes: 11 additions & 5 deletions examples/mycoind/app/init.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package app

import (
"encoding/hex"
"encoding/json"
"fmt"
"path/filepath"
Expand All @@ -10,7 +11,7 @@ import (
"github.com/iov-one/weave/coin"
"github.com/iov-one/weave/commands/server"
"github.com/iov-one/weave/crypto"
"github.com/iov-one/weave/gconf"
"github.com/iov-one/weave/errors"
"github.com/iov-one/weave/x/cash"
abci "github.com/tendermint/tendermint/abci/types"
)
Expand Down Expand Up @@ -43,6 +44,10 @@ func GenInitOptions(args []string) (json.RawMessage, error) {
dict map[string]interface{}
array []interface{}
)
collectorAddr, err := hex.DecodeString("3b11c732b8fc1f09beb34031302fe2ab347c5c14")
if err != nil {
return nil, errors.Wrap(err, "cannot hex decode collector address")
}
return json.Marshal(dict{
"cash": array{
dict{
Expand All @@ -55,9 +60,11 @@ func GenInitOptions(args []string) (json.RawMessage, error) {
},
},
},
"gconf": dict{
cash.GconfCollectorAddress: "66616b652d636f6c6c6563746f722d61646472657373",
cash.GconfMinimalFee: coin.Coin{Whole: 0}, // no fee
"conf": dict{
"cash": cash.Configuration{
CollectorAddress: collectorAddr,
MinimalFee: coin.Coin{Whole: 0}, // no fee
},
},
})
}
Expand All @@ -76,7 +83,6 @@ func GenerateApp(options *server.Options) (abci.Application, error) {
return nil, err
}
application.WithInit(app.ChainInitializers(
&gconf.Initializer{},
&cash.Initializer{},
))

Expand Down
Loading