Cost -> Fee

This commit is contained in:
Jae Kwon 2017-01-28 17:01:07 -08:00
parent 0b583ec97b
commit 1ff535d883
2 changed files with 17 additions and 17 deletions

View File

@ -10,12 +10,12 @@ import (
type CounterPluginState struct {
Counter int
TotalCost types.Coins
TotalFees types.Coins
}
type CounterTx struct {
Valid bool
Cost types.Coins
Fee types.Coins
}
//--------------------------------------------------------------------------------
@ -54,20 +54,20 @@ func (cp *CounterPlugin) RunTx(store types.KVStore, ctx types.CallContext, txByt
if !tx.Valid {
return abci.ErrInternalError.AppendLog("CounterTx.Valid must be true")
}
if !tx.Cost.IsValid() {
return abci.ErrInternalError.AppendLog("CounterTx.Cost is not sorted or has zero amounts")
if !tx.Fee.IsValid() {
return abci.ErrInternalError.AppendLog("CounterTx.Fee is not sorted or has zero amounts")
}
if !tx.Cost.IsNonnegative() {
return abci.ErrInternalError.AppendLog("CounterTx.Cost must be nonnegative")
if !tx.Fee.IsNonnegative() {
return abci.ErrInternalError.AppendLog("CounterTx.Fee must be nonnegative")
}
// Did the caller provide enough coins?
if !ctx.Coins.IsGTE(tx.Cost) {
return abci.ErrInsufficientFunds.AppendLog("CounterTx.Cost was not provided")
if !ctx.Coins.IsGTE(tx.Fee) {
return abci.ErrInsufficientFunds.AppendLog("CounterTx.Fee was not provided")
}
// TODO If there are any funds left over, return funds.
// e.g. !ctx.Coins.Minus(tx.Cost).IsZero()
// e.g. !ctx.Coins.Minus(tx.Fee).IsZero()
// ctx.CallerAccount is synced w/ store, so just modify that and store it.
// Load CounterPluginState
@ -82,7 +82,7 @@ func (cp *CounterPlugin) RunTx(store types.KVStore, ctx types.CallContext, txByt
// Update CounterPluginState
cpState.Counter += 1
cpState.TotalCost = cpState.TotalCost.Plus(tx.Cost)
cpState.TotalFees = cpState.TotalFees.Plus(tx.Fee)
// Save CounterPluginState
store.Set(cp.StateKey(), wire.BinaryBytes(cpState))

View File

@ -35,14 +35,14 @@ func TestCounterPlugin(t *testing.T) {
bcApp.SetOption("base/account", string(wire.JSONBytes(test1Acc)))
// Deliver a CounterTx
DeliverCounterTx := func(gas int64, fee types.Coin, inputCoins types.Coins, inputSequence int, cost types.Coins) abci.Result {
DeliverCounterTx := func(gas int64, fee types.Coin, inputCoins types.Coins, inputSequence int, appFee types.Coins) abci.Result {
// Construct an AppTx signature
tx := &types.AppTx{
Gas: gas,
Fee: fee,
Name: counterPluginName,
Input: types.NewTxInput(test1Acc.PubKey, inputCoins, inputSequence),
Data: wire.BinaryBytes(CounterTx{Valid: true, Cost: cost}),
Data: wire.BinaryBytes(CounterTx{Valid: true, Fee: appFee}),
}
// Sign request
@ -57,7 +57,7 @@ func TestCounterPlugin(t *testing.T) {
return bcApp.DeliverTx(txBytes)
}
// REF: DeliverCounterTx(gas, fee, inputCoins, inputSequence, cost) {
// REF: DeliverCounterTx(gas, fee, inputCoins, inputSequence, appFee) {
// Test a basic send, no fee
res := DeliverCounterTx(0, types.Coin{}, types.Coins{{"", 1}}, 1, types.Coins{})
@ -75,15 +75,15 @@ func TestCounterPlugin(t *testing.T) {
res = DeliverCounterTx(0, types.Coin{"", 2}, types.Coins{{"", 3}}, 3, types.Coins{})
assert.True(t, res.IsOK(), res.String())
// Test input equals fee+cost
// Test input equals fee+appFee
res = DeliverCounterTx(0, types.Coin{"", 1}, types.Coins{{"", 3}, {"gold", 1}}, 4, types.Coins{{"", 2}, {"gold", 1}})
assert.True(t, res.IsOK(), res.String())
// Test fee+cost prevented transaction, not enough ""
// Test fee+appFee prevented transaction, not enough ""
res = DeliverCounterTx(0, types.Coin{"", 1}, types.Coins{{"", 2}, {"gold", 1}}, 5, types.Coins{{"", 2}, {"gold", 1}})
assert.True(t, res.IsErr(), res.String())
// Test fee+cost prevented transaction, not enough "gold"
// Test fee+appFee prevented transaction, not enough "gold"
res = DeliverCounterTx(0, types.Coin{"", 1}, types.Coins{{"", 3}, {"gold", 1}}, 5, types.Coins{{"", 2}, {"gold", 2}})
assert.True(t, res.IsErr(), res.String())
@ -95,5 +95,5 @@ func TestCounterPlugin(t *testing.T) {
res = DeliverCounterTx(0, types.Coin{"", 1}, types.Coins{{"", 3}, {"gold", 2}}, 7, types.Coins{{"", 2}, {"gold", 1}})
assert.True(t, res.IsOK(), res.String())
// REF: DeliverCounterTx(gas, fee, inputCoins, inputSequence, cost) {
// REF: DeliverCounterTx(gas, fee, inputCoins, inputSequence, appFee) {
}