Fee handler set by default, tested app level

This commit is contained in:
Ethan Frey 2017-07-12 20:38:54 +02:00
parent 454227393b
commit 36a453ea41
7 changed files with 47 additions and 12 deletions

View File

@ -14,6 +14,7 @@ import (
"github.com/tendermint/basecoin/modules/auth"
"github.com/tendermint/basecoin/modules/base"
"github.com/tendermint/basecoin/modules/coin"
"github.com/tendermint/basecoin/modules/fee"
"github.com/tendermint/basecoin/stack"
sm "github.com/tendermint/basecoin/state"
"github.com/tendermint/basecoin/version"
@ -52,7 +53,7 @@ func NewBasecoin(handler basecoin.Handler, eyesCli *eyes.Client, logger log.Logg
}
// DefaultHandler - placeholder to just handle sendtx
func DefaultHandler() basecoin.Handler {
func DefaultHandler(feeDenom string) basecoin.Handler {
// use the default stack
h := coin.NewHandler()
d := stack.NewDispatcher(stack.WrapHandler(h))
@ -61,6 +62,7 @@ func DefaultHandler() basecoin.Handler {
stack.Recovery{},
auth.Signatures{},
base.Chain{},
fee.NewSimpleFeeMiddleware(coin.Coin{feeDenom, 0}, fee.Bank),
).Use(d)
}

View File

@ -13,6 +13,7 @@ import (
"github.com/tendermint/basecoin/modules/auth"
"github.com/tendermint/basecoin/modules/base"
"github.com/tendermint/basecoin/modules/coin"
"github.com/tendermint/basecoin/modules/fee"
"github.com/tendermint/basecoin/stack"
"github.com/tendermint/basecoin/state"
wire "github.com/tendermint/go-wire"
@ -40,17 +41,33 @@ func newAppTest(t *testing.T) *appTest {
return at
}
// make a tx sending 5mycoin from each acctIn to acctOut
func (at *appTest) getTx(coins coin.Coins) basecoin.Tx {
// baseTx is the
func (at *appTest) baseTx(coins coin.Coins) basecoin.Tx {
in := []coin.TxInput{{Address: at.acctIn.Actor(), Coins: coins}}
out := []coin.TxOutput{{Address: at.acctOut.Actor(), Coins: coins}}
tx := coin.NewSendTx(in, out)
tx = base.NewChainTx(at.chainID, 0, tx)
return tx
}
func (at *appTest) signTx(tx basecoin.Tx) basecoin.Tx {
stx := auth.NewMulti(tx)
auth.Sign(stx, at.acctIn.Key)
return stx.Wrap()
}
func (at *appTest) getTx(coins coin.Coins) basecoin.Tx {
tx := at.baseTx(coins)
tx = base.NewChainTx(at.chainID, 0, tx)
return at.signTx(tx)
}
func (at *appTest) feeTx(coins coin.Coins, toll coin.Coin) basecoin.Tx {
tx := at.baseTx(coins)
tx = fee.NewFee(tx, toll, at.acctIn.Actor())
tx = base.NewChainTx(at.chainID, 0, tx)
return at.signTx(tx)
}
// set the account on the app through SetOption
func (at *appTest) initAccount(acct *coin.AccountWithKey) {
res := at.app.SetOption("coin/account", acct.MakeOption())
@ -67,7 +84,7 @@ func (at *appTest) reset() {
logger := log.NewTMLogger(os.Stdout).With("module", "app")
logger = log.NewTracingLogger(logger)
at.app = NewBasecoin(
DefaultHandler(),
DefaultHandler("mycoin"),
eyesCli,
logger,
)
@ -124,7 +141,7 @@ func TestSetOption(t *testing.T) {
eyesCli := eyes.NewLocalClient("", 0)
app := NewBasecoin(
DefaultHandler(),
DefaultHandler("atom"),
eyesCli,
log.TestingLogger().With("module", "app"),
)
@ -212,6 +229,17 @@ func TestTx(t *testing.T) {
assert.True(res.IsOK(), "ExecTx/Good DeliverTx: Expected OK return from ExecTx, Error: %v", res)
assert.Equal(amt.Negative(), diffIn)
assert.Equal(amt, diffOut)
//DeliverTx with fee.... 4 get to recipient, 1 extra taxed
at.reset()
amt = coin.Coins{{"mycoin", 4}}
toll := coin.Coin{"mycoin", 1}
res, diffIn, diffOut = at.exec(t, at.feeTx(amt, toll), false)
assert.True(res.IsOK(), "ExecTx/Good DeliverTx: Expected OK return from ExecTx, Error: %v", res)
payment := amt.Plus(coin.Coins{toll}).Negative()
assert.Equal(payment, diffIn)
assert.Equal(amt, diffOut)
}
func TestQuery(t *testing.T) {

View File

@ -18,7 +18,7 @@ const genesisAcctFilepath = "./testdata/genesis2.json"
func TestLoadGenesisDoNotFailIfAppOptionsAreMissing(t *testing.T) {
eyesCli := eyescli.NewLocalClient("", 0)
app := NewBasecoin(DefaultHandler(), eyesCli, log.TestingLogger())
app := NewBasecoin(DefaultHandler("mycoin"), eyesCli, log.TestingLogger())
err := app.LoadGenesis("./testdata/genesis3.json")
require.Nil(t, err, "%+v", err)
}
@ -27,7 +27,7 @@ func TestLoadGenesis(t *testing.T) {
assert, require := assert.New(t), require.New(t)
eyesCli := eyescli.NewLocalClient("", 0)
app := NewBasecoin(DefaultHandler(), eyesCli, log.TestingLogger())
app := NewBasecoin(DefaultHandler("mycoin"), eyesCli, log.TestingLogger())
err := app.LoadGenesis(genesisFilepath)
require.Nil(err, "%+v", err)
@ -56,7 +56,7 @@ func TestLoadGenesisAccountAddress(t *testing.T) {
assert, require := assert.New(t), require.New(t)
eyesCli := eyescli.NewLocalClient("", 0)
app := NewBasecoin(DefaultHandler(), eyesCli, log.TestingLogger())
app := NewBasecoin(DefaultHandler("mycoin"), eyesCli, log.TestingLogger())
err := app.LoadGenesis(genesisAcctFilepath)
require.Nil(err, "%+v", err)

View File

@ -11,7 +11,8 @@ import (
func main() {
rt := commands.RootCmd
commands.Handler = app.DefaultHandler()
// require all fees in mycoin - change this in your app!
commands.Handler = app.DefaultHandler("mycoin")
rt.AddCommand(
commands.InitCmd,

View File

@ -23,7 +23,7 @@ import (
// so it gets routed properly
const (
NameCounter = "cntr"
ByteTx = 0x21 //TODO What does this byte represent should use typebytes probably
ByteTx = 0x2F //TODO What does this byte represent should use typebytes probably
TypeTx = NameCounter + "/count"
)

View File

@ -11,6 +11,10 @@ import (
// NameFee - namespace for the fee module
const NameFee = "fee"
// Bank is a default location for the fees, but pass anything into
// the middleware constructor
var Bank = basecoin.Actor{App: NameFee, Address: []byte("bank")}
// SimpleFeeMiddleware - middleware for fee checking, constant amount
// It used modules.coin to move the money
type SimpleFeeMiddleware struct {

View File

@ -29,7 +29,7 @@ func TestFeeChecks(t *testing.T) {
pure := atoms(46657)
// these are some accounts
collector := basecoin.Actor{App: fee.NameFee, Address: []byte("bank")}
collector := basecoin.Actor{App: fee.NameFee, Address: []byte("mine")}
key1 := coin.NewAccountWithKey(mixed)
key2 := coin.NewAccountWithKey(pure)
act1, act2 := key1.Actor(), key2.Actor()