Skip to content

Commit

Permalink
Merge pull request #534 from iov-one/ethans-gconf-comments
Browse files Browse the repository at this point in the history
Ethans gconf comments
  • Loading branch information
husio authored Apr 18, 2019
2 parents 40f4e60 + 0d493ac commit 3f3b091
Show file tree
Hide file tree
Showing 18 changed files with 693 additions and 123 deletions.
15 changes: 10 additions & 5 deletions cmd/bcpd/app/app_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -240,13 +240,18 @@ func withWalletAppState(t testing.TB, accounts []*account) string {
Address weave.Address `json:"address"`
Coins coin.Coins `json:"coins"`
}
type conf struct {
Cash cash.Configuration `json:"cash"`
}
state := struct {
Cash []wallet `json:"cash"`
CashConf cash.Configuration `json:"cashconf"`
Cash []wallet `json:"cash"`
Conf conf `json:"conf"`
}{
CashConf: cash.Configuration{
CollectorAddress: fromHex(t, "fe1132f9ed1fb1c2e9c09ff297b619654387bb4a"),
MinimalFee: coin.Coin{}, // no fee
Conf: conf{
Cash: cash.Configuration{
CollectorAddress: fromHex(t, "fe1132f9ed1fb1c2e9c09ff297b619654387bb4a"),
MinimalFee: coin.Coin{}, // no fee
},
},
}

Expand Down
11 changes: 6 additions & 5 deletions cmd/bnsd/app/app_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@ package app_test

import (
"encoding/hex"
"github.com/iov-one/weave"
"github.com/iov-one/weave/weavetest"
"github.com/tendermint/tendermint/libs/common"
"fmt"
"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,14 +45,14 @@ 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
toHex("sigs:") + addr.String(), // sender sequence incremented
toHex("cash:") + feeDistAddr.String(), // fee destination
}
fmt.Printf("%v\n", wantKeys)
for _, want := range wantKeys {
var found bool
for i := 0; i < len(dres.Tags) && !found; i++ {
Expand Down
8 changes: 5 additions & 3 deletions cmd/bnsd/app/testdata/fixtures/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,11 @@ func appStateGenesis(keyAddress weave.Address) []byte {
},
},
},
"cashconf": cash.Configuration{
CollectorAddress: weave.Condition("dist/revenue/0000000000000001").Address(),
MinimalFee: coin.NewCoin(0, 10000000, "FRNK"),
"conf": dict{
"cash": cash.Configuration{
CollectorAddress: weave.Condition("dist/revenue/0000000000000001").Address(),
MinimalFee: coin.NewCoin(0, 10000000, "FRNK"),
},
},
"currencies": []interface{}{
dict{
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},
},
},
"cashconf": cash.Configuration{
CollectorAddress: weave.NewAddress([]byte("fake-collector-address")),
MinimalFee: 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),
},
},
"cashconf": cash.Configuration{
CollectorAddress: weave.Condition("dist/revenue/0000000000000001").Address(),
MinimalFee: antiSpamFee,
"conf": dict{
"cash": cash.Configuration{
CollectorAddress: weave.Condition("dist/revenue/0000000000000001").Address(),
MinimalFee: antiSpamFee,
},
},
"msgfee": []interface{}{
dict{
Expand Down
8 changes: 5 additions & 3 deletions examples/mycoind/app/app_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,11 @@ func testInitChain(t *testing.T, myApp app.BaseApp, addr string) {
},
},
},
"cashconf": cash.Configuration{
CollectorAddress: fromHex(t, "3b11c732b8fc1f09beb34031302fe2ab347c5c14"),
MinimalFee: 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 Down
8 changes: 5 additions & 3 deletions examples/mycoind/app/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,11 @@ func GenInitOptions(args []string) (json.RawMessage, error) {
},
},
},
"cashconf": cash.Configuration{
CollectorAddress: collectorAddr,
MinimalFee: coin.Coin{Whole: 0}, // no fee
"conf": dict{
"cash": cash.Configuration{
CollectorAddress: collectorAddr,
MinimalFee: coin.Coin{Whole: 0}, // no fee
},
},
})
}
Expand Down
61 changes: 40 additions & 21 deletions gconf/gconf.go
Original file line number Diff line number Diff line change
@@ -1,18 +1,23 @@
package gconf

import (
"reflect"

"github.com/iov-one/weave"
"github.com/iov-one/weave/errors"
)

// Store is a subset of weave.KVStore
type Store interface {
Get([]byte) []byte
Set([]byte, []byte)
}

func Save(db Store, src Marshaler) error {
key := []byte("configuration:" + pkgPath(src))
// Save will Validate the object, before writing it to a special "configuration"
// singleton for that package name.
func Save(db Store, pkg string, src ValidMarshaler) error {
key := []byte("_c:" + pkg)
if err := src.Validate(); err != nil {
return errors.Wrap(err, "saving gconf")
}
raw, err := src.Marshal()
if err != nil {
return errors.Wrapf(err, "marshal: key %q", key)
Expand All @@ -21,20 +26,24 @@ func Save(db Store, src Marshaler) error {
return nil
}

// Marshaler is implemented by object that can serialize itself to a binary
// representation. This interface is implemented by all protobuf messages.
type Marshaler interface {
// ValidMarshaler is implemented by object that can serialize itself to a binary
// representation. Marshal is implemented by all protobuf messages.
// You must add your own Validate method
//
// Note duplicate of code in x/persistent.go
type ValidMarshaler interface {
Marshal() ([]byte, error)
Validate() error
}

func Load(db Store, dst Unmarshaler) error {
key := []byte("configuration:" + pkgPath(dst))
func Load(db Store, pkg string, dst Unmarshaler) error {
key := []byte("_c:" + pkg)
raw := db.Get(key)
if raw == nil {
return errors.Wrapf(errors.ErrNotFound, "key %q", key)
}
if err := dst.Unmarshal(raw); err != nil {
return errors.Wrapf(err, "unmarhsal: key %q", key)
return errors.Wrapf(err, "unmarshal: key %q", key)
}
return nil
}
Expand All @@ -46,17 +55,27 @@ type Unmarshaler interface {
Unmarshal([]byte) error
}

// pkgPath returns the full package path that given structure belongs to. It
// returns an empty string of non structure types.
// Use full path instead of just the package name to avoid name collisions.
// Each package is expected to have only one configuration object.
func pkgPath(structure interface{}) string {
t := reflect.TypeOf(structure)
for t.Kind() == reflect.Ptr {
t = t.Elem()
type Configuration interface {
ValidMarshaler
Unmarshaler
}

// InitConfig will take opts["conf"][pkg], parse it into the given Configuration object
// validate it, and store under the proper key in the database
// Returns an error if anything goes wrong
func InitConfig(db Store, opts weave.Options, pkg string, conf Configuration) error {
var confOptions weave.Options
if err := opts.ReadOptions("conf", &confOptions); err != nil {
return errors.Wrap(err, "read conf")
}
if t.Kind() != reflect.Struct {
return ""
if confOptions[pkg] == nil {
return errors.Wrapf(errors.ErrInvalidInput, "no configuration for %s", pkg)
}
return t.PkgPath()
if err := confOptions.ReadOptions(pkg, conf); err != nil {
return errors.Wrapf(err, "read configuration for %s", pkg)
}
if err := Save(db, pkg, conf); err != nil {
return errors.Wrapf(err, "save configuration for %s", pkg)
}
return nil
}
16 changes: 6 additions & 10 deletions gconf/gconf_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,13 @@ import (
"github.com/iov-one/weave/store"
)

func TestPackageOf(t *testing.T) {
type Foo struct {
}
if name := pkgPath(&Foo{}); name != "github.com/iov-one/weave/gconf" {
t.Fatalf("unexpected name: %q", name)
}
}

func TestLoadSave(t *testing.T) {
db := store.MemStore()
c := configuration{raw: "foobar"}
if err := Save(db, &c); err != nil {
if err := Save(db, "gconf", &c); err != nil {
t.Fatalf("cannot save configuration: %s", err)
}
if err := Load(db, &c); err != nil {
if err := Load(db, "gconf", &c); err != nil {
t.Fatalf("cannot load configuration: %s", err)
}
}
Expand All @@ -45,3 +37,7 @@ func (c *configuration) Unmarshal(raw []byte) error {
}
return c.err
}

func (c *configuration) Validate() error {
return c.err
}
Loading

0 comments on commit 3f3b091

Please sign in to comment.