Pull out fees from base into own module

This commit is contained in:
Ethan Frey 2017-06-29 21:08:28 +02:00
parent aa0c246f64
commit 872c04280c
3 changed files with 49 additions and 39 deletions

View File

@ -1,10 +1,9 @@
package handlers
package fee
import (
"github.com/tendermint/basecoin"
"github.com/tendermint/basecoin/errors"
"github.com/tendermint/basecoin/stack"
"github.com/tendermint/basecoin/txs"
"github.com/tendermint/basecoin/types"
)
@ -35,7 +34,7 @@ var _ stack.Middleware = SimpleFeeHandler{}
// Yes, I know refactor a bit... really too late already
func (h SimpleFeeHandler) CheckTx(ctx basecoin.Context, store types.KVStore, tx basecoin.Tx, next basecoin.Checker) (res basecoin.Result, err error) {
feeTx, ok := tx.Unwrap().(*txs.Fee)
feeTx, ok := tx.Unwrap().(*Fee)
if !ok {
return res, errors.InvalidFormat()
}
@ -58,7 +57,7 @@ func (h SimpleFeeHandler) CheckTx(ctx basecoin.Context, store types.KVStore, tx
}
func (h SimpleFeeHandler) DeliverTx(ctx basecoin.Context, store types.KVStore, tx basecoin.Tx, next basecoin.Deliver) (res basecoin.Result, err error) {
feeTx, ok := tx.Unwrap().(*txs.Fee)
feeTx, ok := tx.Unwrap().(*Fee)
if !ok {
return res, errors.InvalidFormat()
}

43
modules/fee/tx.go Normal file
View File

@ -0,0 +1,43 @@
package fee
import (
"github.com/tendermint/basecoin"
"github.com/tendermint/basecoin/types"
)
const (
ByteFees = 0x20
TypeFees = "fee"
)
func init() {
basecoin.TxMapper.
RegisterImplementation(&Fee{}, TypeFees, ByteFees)
}
/**** Fee ****/
// Fee attaches a fee payment to the embedded tx
type Fee struct {
Tx basecoin.Tx `json:"tx"`
Fee types.Coin `json:"fee"`
Payer basecoin.Actor `json:"payer"` // the address who pays the fee
// Gas types.Coin `json:"gas"` // ?????
}
func NewFee(tx basecoin.Tx, fee types.Coin, payer basecoin.Actor) *Fee {
return &Fee{Tx: tx, Fee: fee, Payer: payer}
}
func (f *Fee) ValidateBasic() error {
// TODO: more checks
return f.Tx.ValidateBasic()
}
func (f *Fee) Wrap() basecoin.Tx {
return basecoin.Tx{f}
}
func (f *Fee) Next() basecoin.Tx {
return f.Tx
}

View File

@ -3,17 +3,14 @@ package txs
import (
"github.com/tendermint/basecoin"
"github.com/tendermint/basecoin/errors"
"github.com/tendermint/basecoin/types"
"github.com/tendermint/go-wire/data"
)
const (
// for utils...
ByteRaw = 0x1
// TODO: move fees into a module, multiplexer is standard
ByteFees = 0x2
ByteMulti = 0x3
ByteChain = 0x4
ByteRaw = 0x1
ByteMulti = 0x2
ByteChain = 0x3
// for signatures
ByteSig = 0x16
@ -23,7 +20,6 @@ const (
const (
// for utils...
TypeRaw = "raw"
TypeFees = "fee"
TypeMulti = "multi"
TypeChain = "chain"
@ -39,7 +35,6 @@ const (
func init() {
basecoin.TxMapper.
RegisterImplementation(Raw{}, TypeRaw, ByteRaw).
RegisterImplementation(&Fee{}, TypeFees, ByteFees).
RegisterImplementation(&MultiTx{}, TypeMulti, ByteMulti).
RegisterImplementation(&Chain{}, TypeChain, ByteChain)
}
@ -64,33 +59,6 @@ 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"`
Payer basecoin.Actor `json:"payer"` // the address who pays the fee
// Gas types.Coin `json:"gas"` // ?????
}
func NewFee(tx basecoin.Tx, fee types.Coin, payer basecoin.Actor) *Fee {
return &Fee{Tx: tx, Fee: fee, Payer: payer}
}
func (f *Fee) ValidateBasic() error {
// TODO: more checks
return f.Tx.ValidateBasic()
}
func (f *Fee) Wrap() basecoin.Tx {
return basecoin.Tx{f}
}
func (f *Fee) Next() basecoin.Tx {
return f.Tx
}
/**** MultiTx ******/
type MultiTx struct {
Txs []basecoin.Tx `json:"txs"`