From 36a453ea412ea2fbca1c45e70d8398e5f4414c62 Mon Sep 17 00:00:00 2001 From: Ethan Frey Date: Wed, 12 Jul 2017 20:38:54 +0200 Subject: [PATCH] Fee handler set by default, tested app level --- app/app.go | 4 +- app/app_test.go | 38 ++++++++++++++++--- app/genesis_test.go | 6 +-- cmd/basecoin/main.go | 3 +- docs/guide/counter/plugins/counter/counter.go | 2 +- modules/fee/handler.go | 4 ++ modules/fee/handler_test.go | 2 +- 7 files changed, 47 insertions(+), 12 deletions(-) diff --git a/app/app.go b/app/app.go index e053408a48..836e8c66fa 100644 --- a/app/app.go +++ b/app/app.go @@ -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) } diff --git a/app/app_test.go b/app/app_test.go index ff9ed2221f..cbb446bb05 100644 --- a/app/app_test.go +++ b/app/app_test.go @@ -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) { diff --git a/app/genesis_test.go b/app/genesis_test.go index 1d99b38eb4..95e1d788e3 100644 --- a/app/genesis_test.go +++ b/app/genesis_test.go @@ -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) diff --git a/cmd/basecoin/main.go b/cmd/basecoin/main.go index 2185262b73..8b4f5764cd 100644 --- a/cmd/basecoin/main.go +++ b/cmd/basecoin/main.go @@ -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, diff --git a/docs/guide/counter/plugins/counter/counter.go b/docs/guide/counter/plugins/counter/counter.go index dd432822be..da30a798c3 100644 --- a/docs/guide/counter/plugins/counter/counter.go +++ b/docs/guide/counter/plugins/counter/counter.go @@ -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" ) diff --git a/modules/fee/handler.go b/modules/fee/handler.go index 64a64bf893..2ada56cb08 100644 --- a/modules/fee/handler.go +++ b/modules/fee/handler.go @@ -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 { diff --git a/modules/fee/handler_test.go b/modules/fee/handler_test.go index 9e0568de0b..15e28e1dfa 100644 --- a/modules/fee/handler_test.go +++ b/modules/fee/handler_test.go @@ -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()