Moved content of txs package to sit next to the handlers

This commit is contained in:
Ethan Frey
2017-07-06 16:33:38 +02:00
parent a047e210fa
commit b757467f7b
18 changed files with 169 additions and 159 deletions
+3 -4
View File
@@ -5,12 +5,11 @@ import (
"github.com/tendermint/basecoin/errors"
"github.com/tendermint/basecoin/stack"
"github.com/tendermint/basecoin/state"
"github.com/tendermint/basecoin/txs"
)
//nolint
const (
NameChain = "chan"
NameChain = "chain"
)
// Chain enforces that this tx was bound to the named chain
@@ -43,9 +42,9 @@ func (c Chain) DeliverTx(ctx basecoin.Context, store state.KVStore, tx basecoin.
return next.DeliverTx(ctx, store, stx)
}
// checkChain makes sure the tx is a txs.Chain and
// checkChain makes sure the tx is a Chain Tx and is on the proper chain
func (c Chain) checkChain(chainID string, tx basecoin.Tx) (basecoin.Tx, error) {
ctx, ok := tx.Unwrap().(*txs.Chain)
ctx, ok := tx.Unwrap().(ChainTx)
if !ok {
return tx, errors.ErrNoChain()
}
+3 -4
View File
@@ -11,7 +11,6 @@ import (
"github.com/tendermint/basecoin"
"github.com/tendermint/basecoin/stack"
"github.com/tendermint/basecoin/state"
"github.com/tendermint/basecoin/txs"
)
func TestChain(t *testing.T) {
@@ -19,14 +18,14 @@ func TestChain(t *testing.T) {
msg := "got it"
chainID := "my-chain"
raw := txs.NewRaw([]byte{1, 2, 3, 4})
raw := stack.NewRawTx([]byte{1, 2, 3, 4})
cases := []struct {
tx basecoin.Tx
valid bool
errorMsg string
}{
{txs.NewChain(chainID, raw), true, ""},
{txs.NewChain("someone-else", raw), false, "someone-else"},
{NewChainTx(chainID, raw), true, ""},
{NewChainTx("someone-else", raw), false, "someone-else"},
{raw, false, "No chain id provided"},
}
+2 -3
View File
@@ -9,7 +9,6 @@ import (
"github.com/tendermint/basecoin"
"github.com/tendermint/basecoin/stack"
"github.com/tendermint/basecoin/state"
"github.com/tendermint/basecoin/txs"
)
//nolint
@@ -31,7 +30,7 @@ var _ stack.Middleware = Multiplexer{}
// CheckTx splits the input tx and checks them all - fulfills Middlware interface
func (Multiplexer) CheckTx(ctx basecoin.Context, store state.KVStore, tx basecoin.Tx, next basecoin.Checker) (res basecoin.Result, err error) {
if mtx, ok := tx.Unwrap().(*txs.MultiTx); ok {
if mtx, ok := tx.Unwrap().(*MultiTx); ok {
return runAll(ctx, store, mtx.Txs, next.CheckTx)
}
return next.CheckTx(ctx, store, tx)
@@ -39,7 +38,7 @@ func (Multiplexer) CheckTx(ctx basecoin.Context, store state.KVStore, tx basecoi
// DeliverTx splits the input tx and checks them all - fulfills Middlware interface
func (Multiplexer) DeliverTx(ctx basecoin.Context, store state.KVStore, tx basecoin.Tx, next basecoin.Deliver) (res basecoin.Result, err error) {
if mtx, ok := tx.Unwrap().(*txs.MultiTx); ok {
if mtx, ok := tx.Unwrap().(*MultiTx); ok {
return runAll(ctx, store, mtx.Txs, next.DeliverTx)
}
return next.DeliverTx(ctx, store, tx)
+66
View File
@@ -0,0 +1,66 @@
package base
import "github.com/tendermint/basecoin"
// nolint
const (
// for utils...
ByteMultiTx = 0x2
ByteChainTx = 0x3
)
//nolint
const (
TypeMultiTx = NameMultiplexer + "/tx"
TypeChainTx = NameChain + "/tx"
)
func init() {
basecoin.TxMapper.
RegisterImplementation(MultiTx{}, TypeMultiTx, ByteMultiTx).
RegisterImplementation(ChainTx{}, TypeChainTx, ByteChainTx)
}
/**** MultiTx ******/
type MultiTx struct {
Txs []basecoin.Tx `json:"txs"`
}
func NewMultiTx(txs ...basecoin.Tx) basecoin.Tx {
return (MultiTx{Txs: txs}).Wrap()
}
func (mt MultiTx) Wrap() basecoin.Tx {
return basecoin.Tx{mt}
}
func (mt MultiTx) ValidateBasic() error {
for _, t := range mt.Txs {
err := t.ValidateBasic()
if err != nil {
return err
}
}
return nil
}
/*** ChainTx ****/
// ChainTx locks this tx to one chainTx, wrap with this before signing
type ChainTx struct {
Tx basecoin.Tx `json:"tx"`
ChainID string `json:"chain_id"`
}
func NewChainTx(chainID string, tx basecoin.Tx) basecoin.Tx {
return (ChainTx{Tx: tx, ChainID: chainID}).Wrap()
}
func (c ChainTx) Wrap() basecoin.Tx {
return basecoin.Tx{c}
}
func (c ChainTx) ValidateBasic() error {
// TODO: more checks? chainID?
return c.Tx.ValidateBasic()
}
+51
View File
@@ -0,0 +1,51 @@
package base
import (
"strconv"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/tendermint/basecoin/stack"
"github.com/tendermint/go-wire/data"
"github.com/tendermint/basecoin"
)
func TestEncoding(t *testing.T) {
assert := assert.New(t)
require := require.New(t)
raw := stack.NewRawTx([]byte{0x34, 0xa7})
raw2 := stack.NewRawTx([]byte{0x73, 0x86, 0x22})
cases := []struct {
Tx basecoin.Tx
}{
{raw},
{NewMultiTx(raw, raw2)},
{NewChainTx("foobar", raw)},
}
for idx, tc := range cases {
i := strconv.Itoa(idx)
tx := tc.Tx
// test json in and out
js, err := data.ToJSON(tx)
require.Nil(err, i)
var jtx basecoin.Tx
err = data.FromJSON(js, &jtx)
require.Nil(err, i)
assert.Equal(tx, jtx, i)
// test wire in and out
bin, err := data.ToWire(tx)
require.Nil(err, i)
var wtx basecoin.Tx
err = data.FromWire(bin, &wtx)
require.Nil(err, i)
assert.Equal(tx, wtx, i)
}
}