Introduce the concept of TxLayer

This commit is contained in:
Ethan Frey 2017-05-18 17:11:26 +02:00
parent 207cf6b64e
commit 6029f5c986
4 changed files with 91 additions and 17 deletions

18
tx.go
View File

@ -3,4 +3,22 @@ package basecoin
// TODO: add some common functionality here...
// +gen wrapper:"Tx"
type TxInner interface {
Wrap() Tx
}
// TxLayer provides a standard way to deal with "middleware" tx,
// That add context to an embedded tx.
type TxLayer interface {
TxInner
Next() Tx
}
func (t Tx) IsLayer() bool {
_, ok := t.Unwrap().(TxLayer)
return ok
}
func (t Tx) GetLayer() TxLayer {
l, _ := t.Unwrap().(TxLayer)
return l
}

35
tx_test.go Normal file
View File

@ -0,0 +1,35 @@
package basecoin
import (
"testing"
"github.com/stretchr/testify/assert"
)
// define a Demo struct that implements TxLayer
type Demo struct{}
var _ TxLayer = Demo{}
func (d Demo) Next() Tx { return Tx{} }
func (d Demo) Wrap() Tx { return Tx{d} }
// define a Fake struct that doesn't implement TxLayer
type Fake struct{}
func (f Fake) Wrap() Tx { return Tx{f} }
// Make sure the layer
func TestLayer(t *testing.T) {
assert := assert.New(t)
// a fake tx, just don't use it...
nl := Fake{}.Wrap()
assert.False(nl.IsLayer())
assert.Nil(nl.GetLayer())
// a tx containing a TxLayer should respond properly
l := Demo{}.Wrap()
assert.True(l.IsLayer())
assert.NotNil(l.GetLayer())
}

View File

@ -30,29 +30,50 @@ const (
func init() {
basecoin.TxMapper.
RegisterImplementation(data.Bytes{}, TypeRaw, ByteRaw).
RegisterImplementation(Raw{}, TypeRaw, ByteRaw).
RegisterImplementation(&Fee{}, TypeFees, ByteFees)
}
// WrapBytes converts data.Bytes into a Tx, so we
// can just pass raw bytes and display in hex in json
func WrapBytes(d []byte) basecoin.Tx {
return basecoin.Tx{data.Bytes(d)}
// Raw just contains bytes that can be hex-ified
type Raw struct {
data.Bytes
}
/**** One Sig ****/
func (r Raw) Wrap() basecoin.Tx {
return basecoin.Tx{r}
}
// OneSig lets us wrap arbitrary data with a go-crypto signature
func NewRaw(d []byte) Raw {
return Raw{data.Bytes(d)}
}
/**** Fee ****/
// Fee attaches a fee payment to the embedded tx
type Fee struct {
Tx basecoin.Tx `json:"tx"`
Fee types.Coin `json:"fee"`
Tx basecoin.Tx `json:"tx"`
Fee types.Coin `json:"fee"`
Payer data.Bytes `json:"payer"` // the address who pays the fee
// Gas types.Coin `json:"gas"` // ?????
}
func NewFee(tx basecoin.Tx, fee types.Coin) *Fee {
return &Fee{Tx: tx, Fee: fee}
func NewFee(tx basecoin.Tx, fee types.Coin, addr []byte) *Fee {
return &Fee{Tx: tx, Fee: fee, Payer: addr}
}
func (f *Fee) Wrap() basecoin.Tx {
return basecoin.Tx{f}
}
/**** MultiTx ******/
type MultiTx struct {
Txs []basecoin.Tx `json:"txs"`
}
func NewMultiTx(txs ...basecoin.Tx) *MultiTx {
return &MultiTx{Txs: txs}
}
func (mt *MultiTx) Wrap() basecoin.Tx {
return basecoin.Tx{mt}
}

View File

@ -6,13 +6,13 @@ import (
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/tendermint/basecoin"
crypto "github.com/tendermint/go-crypto"
keys "github.com/tendermint/go-crypto/keys"
"github.com/tendermint/go-crypto/keys/cryptostore"
"github.com/tendermint/go-crypto/keys/storage/memstorage"
wire "github.com/tendermint/go-wire"
"github.com/tendermint/go-wire/data"
"github.com/tendermint/basecoin"
)
func checkSignBytes(t *testing.T, bytes []byte, expected string) {
@ -22,9 +22,9 @@ func checkSignBytes(t *testing.T, bytes []byte, expected string) {
require.Nil(t, err)
// now make sure this tx is data.Bytes with the info we want
byt, ok := preTx.Unwrap().(data.Bytes)
raw, ok := preTx.Unwrap().(Raw)
require.True(t, ok)
assert.Equal(t, expected, string(byt))
assert.Equal(t, expected, string(raw.Bytes))
}
func TestOneSig(t *testing.T) {
@ -54,7 +54,7 @@ func TestOneSig(t *testing.T) {
}
for _, tc := range cases {
inner := WrapBytes([]byte(tc.data))
inner := NewRaw([]byte(tc.data)).Wrap()
tx := NewSig(inner)
// unsigned version
_, err = tx.Signers()
@ -119,7 +119,7 @@ func TestMultiSig(t *testing.T) {
}
for _, tc := range cases {
inner := WrapBytes([]byte(tc.data))
inner := NewRaw([]byte(tc.data)).Wrap()
tx := NewMulti(inner)
// unsigned version
_, err = tx.Signers()