Integrate dispatcher into app, and fix tests

This commit is contained in:
Ethan Frey 2017-07-04 12:46:57 +02:00
parent fcab8ac901
commit 473451f020
7 changed files with 75 additions and 8 deletions

View File

@ -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!

View File

@ -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")

View File

@ -15,7 +15,7 @@ func init() {
// we reserve the 0x20-0x3f range for standard modules
const (
ByteSend = 0x20
TypeSend = "send"
TypeSend = NameCoin + "/send"
)
//-----------------------------------------------------------------------------

View File

@ -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)

View File

@ -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 $?

22
tx.go
View File

@ -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
}

View File

@ -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)
}
}