From 473451f020c5f22aee12ce7954aa7d84965a3a76 Mon Sep 17 00:00:00 2001 From: Ethan Frey Date: Tue, 4 Jul 2017 12:46:57 +0200 Subject: [PATCH] Integrate dispatcher into app, and fix tests --- app/app.go | 3 ++- app/app_test.go | 11 +++++++++-- modules/coin/tx.go | 2 +- stack/dispatcher.go | 13 ++++++++++--- tests/cli/common.sh | 2 +- tx.go | 22 ++++++++++++++++++++++ tx_test.go | 30 ++++++++++++++++++++++++++++++ 7 files changed, 75 insertions(+), 8 deletions(-) diff --git a/app/app.go b/app/app.go index d8c2f9b609..16d8f6b654 100644 --- a/app/app.go +++ b/app/app.go @@ -46,7 +46,8 @@ func NewBasecoin(h basecoin.Handler, eyesCli *eyes.Client, l log.Logger) *Baseco func DefaultHandler() basecoin.Handler { // use the default stack h := coin.NewHandler() - return stack.NewDefault().Use(h) + d := stack.NewDispatcher(stack.WrapHandler(h)) + return stack.NewDefault().Use(d) } // XXX For testing, not thread safe! diff --git a/app/app_test.go b/app/app_test.go index a86f290024..753efcf3f7 100644 --- a/app/app_test.go +++ b/app/app_test.go @@ -3,6 +3,7 @@ package app import ( "encoding/hex" "encoding/json" + "os" "testing" "github.com/stretchr/testify/assert" @@ -68,8 +69,14 @@ func (at *appTest) reset() { at.accOut = types.MakeAcc("output0") eyesCli := eyes.NewLocalClient("", 0) - at.app = NewBasecoin(DefaultHandler(), eyesCli, - log.TestingLogger().With("module", "app")) + // l := log.TestingLogger().With("module", "app"), + l := log.NewTMLogger(os.Stdout).With("module", "app") + l = log.NewTracingLogger(l) + at.app = NewBasecoin( + DefaultHandler(), + eyesCli, + l, + ) res := at.app.SetOption("base/chain_id", at.chainID) require.EqualValues(at.t, res, "Success") diff --git a/modules/coin/tx.go b/modules/coin/tx.go index 4087d7c945..c5f7370c01 100644 --- a/modules/coin/tx.go +++ b/modules/coin/tx.go @@ -15,7 +15,7 @@ func init() { // we reserve the 0x20-0x3f range for standard modules const ( ByteSend = 0x20 - TypeSend = "send" + TypeSend = NameCoin + "/send" ) //----------------------------------------------------------------------------- diff --git a/stack/dispatcher.go b/stack/dispatcher.go index 7c07266b37..6e177ca098 100644 --- a/stack/dispatcher.go +++ b/stack/dispatcher.go @@ -2,6 +2,7 @@ package stack import ( "fmt" + "strings" "github.com/tendermint/tmlibs/log" @@ -23,7 +24,9 @@ type Dispatcher struct { } func NewDispatcher(routes ...Dispatchable) *Dispatcher { - d := &Dispatcher{} + d := &Dispatcher{ + routes: map[string]Dispatchable{}, + } d.AddRoutes(routes...) return d } @@ -76,8 +79,12 @@ func (d *Dispatcher) SetOption(l log.Logger, store types.KVStore, module, key, v } func (d *Dispatcher) lookupTx(tx basecoin.Tx) (Dispatchable, error) { - // TODO - name := "foo" + kind, err := tx.GetKind() + if err != nil { + return nil, err + } + // grab everything before the / + name := strings.SplitN(kind, "/", 2)[0] r, ok := d.routes[name] if !ok { return nil, errors.ErrUnknownTxType(tx) diff --git a/tests/cli/common.sh b/tests/cli/common.sh index 7c3b886ea3..82a35849e2 100644 --- a/tests/cli/common.sh +++ b/tests/cli/common.sh @@ -163,7 +163,7 @@ checkSendTx() { CTX=$(echo $TX | jq .data.data.tx) assertEquals "type=chain" '"chain"' $(echo $CTX | jq .type) STX=$(echo $CTX | jq .data.tx) - assertEquals "type=send" '"send"' $(echo $STX | jq .type) + assertEquals "type=coin/send" '"coin/send"' $(echo $STX | jq .type) assertEquals "proper sender" "\"$3\"" $(echo $STX | jq .data.inputs[0].address.addr) assertEquals "proper out amount" "$4" $(echo $STX | jq .data.outputs[0].coins[0].amount) return $? diff --git a/tx.go b/tx.go index 8336f02b6c..2e8d5f2bd8 100644 --- a/tx.go +++ b/tx.go @@ -55,3 +55,25 @@ func (t Tx) GetLayer() TxLayer { l, _ := t.Unwrap().(TxLayer) return l } + +// env lets us parse an envelope and just grab the type +type env struct { + Kind string `json:"type"` +} + +// TODO: put this functionality into go-data in a cleaner and more efficient way +func (t Tx) GetKind() (string, error) { + // render as json + d, err := data.ToJSON(t) + if err != nil { + return "", err + } + // parse json + text := env{} + err = data.FromJSON(d, &text) + if err != nil { + return "", err + } + // grab the type we used in json + return text.Kind, nil +} diff --git a/tx_test.go b/tx_test.go index b766b39328..c54a26dba6 100644 --- a/tx_test.go +++ b/tx_test.go @@ -4,6 +4,20 @@ import ( "testing" "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" +) + +func init() { + TxMapper. + RegisterImplementation(Demo{}, TypeDemo, ByteDemo). + RegisterImplementation(Fake{}, TypeFake, ByteFake) +} + +const ( + ByteDemo = 0xF0 + TypeDemo = "test/demo" + ByteFake = 0xF1 + TypeFake = "test/fake" ) // define a Demo struct that implements TxLayer @@ -35,3 +49,19 @@ func TestLayer(t *testing.T) { assert.True(l.IsLayer()) assert.NotNil(l.GetLayer()) } + +func TestKind(t *testing.T) { + cases := []struct { + tx Tx + kind string + }{ + {Demo{}.Wrap(), TypeDemo}, + {Fake{}.Wrap(), TypeFake}, + } + + for _, tc := range cases { + kind, err := tc.tx.GetKind() + require.Nil(t, err, "%+v", err) + assert.Equal(t, tc.kind, kind) + } +}