Moved Coins from types -> modules/coin
This commit is contained in:
parent
49357a3574
commit
6983f61961
|
@ -14,7 +14,6 @@ import (
|
||||||
"github.com/tendermint/basecoin/stack"
|
"github.com/tendermint/basecoin/stack"
|
||||||
"github.com/tendermint/basecoin/state"
|
"github.com/tendermint/basecoin/state"
|
||||||
"github.com/tendermint/basecoin/txs"
|
"github.com/tendermint/basecoin/txs"
|
||||||
"github.com/tendermint/basecoin/types"
|
|
||||||
wire "github.com/tendermint/go-wire"
|
wire "github.com/tendermint/go-wire"
|
||||||
eyes "github.com/tendermint/merkleeyes/client"
|
eyes "github.com/tendermint/merkleeyes/client"
|
||||||
"github.com/tendermint/tmlibs/log"
|
"github.com/tendermint/tmlibs/log"
|
||||||
|
@ -41,7 +40,7 @@ func newAppTest(t *testing.T) *appTest {
|
||||||
}
|
}
|
||||||
|
|
||||||
// make a tx sending 5mycoin from each acctIn to acctOut
|
// make a tx sending 5mycoin from each acctIn to acctOut
|
||||||
func (at *appTest) getTx(seq int, coins types.Coins) basecoin.Tx {
|
func (at *appTest) getTx(seq int, coins coin.Coins) basecoin.Tx {
|
||||||
in := []coin.TxInput{{Address: at.acctIn.Actor(), Coins: coins, Sequence: seq}}
|
in := []coin.TxInput{{Address: at.acctIn.Actor(), Coins: coins, Sequence: seq}}
|
||||||
out := []coin.TxOutput{{Address: at.acctOut.Actor(), Coins: coins}}
|
out := []coin.TxOutput{{Address: at.acctOut.Actor(), Coins: coins}}
|
||||||
tx := coin.NewSendTx(in, out)
|
tx := coin.NewSendTx(in, out)
|
||||||
|
@ -59,8 +58,8 @@ func (at *appTest) initAccount(acct *coin.AccountWithKey) {
|
||||||
|
|
||||||
// reset the in and out accs to be one account each with 7mycoin
|
// reset the in and out accs to be one account each with 7mycoin
|
||||||
func (at *appTest) reset() {
|
func (at *appTest) reset() {
|
||||||
at.acctIn = coin.NewAccountWithKey(types.Coins{{"mycoin", 7}})
|
at.acctIn = coin.NewAccountWithKey(coin.Coins{{"mycoin", 7}})
|
||||||
at.acctOut = coin.NewAccountWithKey(types.Coins{{"mycoin", 7}})
|
at.acctOut = coin.NewAccountWithKey(coin.Coins{{"mycoin", 7}})
|
||||||
|
|
||||||
eyesCli := eyes.NewLocalClient("", 0)
|
eyesCli := eyes.NewLocalClient("", 0)
|
||||||
// logger := log.TestingLogger().With("module", "app"),
|
// logger := log.TestingLogger().With("module", "app"),
|
||||||
|
@ -82,18 +81,18 @@ func (at *appTest) reset() {
|
||||||
require.True(at.t, resabci.IsOK(), resabci)
|
require.True(at.t, resabci.IsOK(), resabci)
|
||||||
}
|
}
|
||||||
|
|
||||||
func getBalance(key basecoin.Actor, state state.KVStore) (types.Coins, error) {
|
func getBalance(key basecoin.Actor, state state.KVStore) (coin.Coins, error) {
|
||||||
acct, err := coin.NewAccountant("").GetAccount(state, key)
|
acct, err := coin.NewAccountant("").GetAccount(state, key)
|
||||||
return acct.Coins, err
|
return acct.Coins, err
|
||||||
}
|
}
|
||||||
|
|
||||||
func getAddr(addr []byte, state state.KVStore) (types.Coins, error) {
|
func getAddr(addr []byte, state state.KVStore) (coin.Coins, error) {
|
||||||
actor := stack.SigPerm(addr)
|
actor := stack.SigPerm(addr)
|
||||||
return getBalance(actor, state)
|
return getBalance(actor, state)
|
||||||
}
|
}
|
||||||
|
|
||||||
// returns the final balance and expected balance for input and output accounts
|
// returns the final balance and expected balance for input and output accounts
|
||||||
func (at *appTest) exec(t *testing.T, tx basecoin.Tx, checkTx bool) (res abci.Result, diffIn, diffOut types.Coins) {
|
func (at *appTest) exec(t *testing.T, tx basecoin.Tx, checkTx bool) (res abci.Result, diffIn, diffOut coin.Coins) {
|
||||||
require := require.New(t)
|
require := require.New(t)
|
||||||
|
|
||||||
initBalIn, err := getBalance(at.acctIn.Actor(), at.app.GetState())
|
initBalIn, err := getBalance(at.acctIn.Actor(), at.app.GetState())
|
||||||
|
@ -135,7 +134,7 @@ func TestSetOption(t *testing.T) {
|
||||||
assert.EqualValues(res, "Success")
|
assert.EqualValues(res, "Success")
|
||||||
|
|
||||||
// make a nice account...
|
// make a nice account...
|
||||||
bal := types.Coins{{"atom", 77}, {"eth", 12}}
|
bal := coin.Coins{{"atom", 77}, {"eth", 12}}
|
||||||
acct := coin.NewAccountWithKey(bal)
|
acct := coin.NewAccountWithKey(bal)
|
||||||
res = app.SetOption("coin/account", acct.MakeOption())
|
res = app.SetOption("coin/account", acct.MakeOption())
|
||||||
require.EqualValues(res, "Success")
|
require.EqualValues(res, "Success")
|
||||||
|
@ -148,7 +147,7 @@ func TestSetOption(t *testing.T) {
|
||||||
// let's parse an account with badly sorted coins...
|
// let's parse an account with badly sorted coins...
|
||||||
unsortAddr, err := hex.DecodeString("C471FB670E44D219EE6DF2FC284BE38793ACBCE1")
|
unsortAddr, err := hex.DecodeString("C471FB670E44D219EE6DF2FC284BE38793ACBCE1")
|
||||||
require.Nil(err)
|
require.Nil(err)
|
||||||
unsortCoins := types.Coins{{"BTC", 789}, {"eth", 123}}
|
unsortCoins := coin.Coins{{"BTC", 789}, {"eth", 123}}
|
||||||
unsortAcc := `{
|
unsortAcc := `{
|
||||||
"pub_key": {
|
"pub_key": {
|
||||||
"type": "ed25519",
|
"type": "ed25519",
|
||||||
|
@ -190,23 +189,23 @@ func TestTx(t *testing.T) {
|
||||||
at := newAppTest(t)
|
at := newAppTest(t)
|
||||||
|
|
||||||
//Bad Balance
|
//Bad Balance
|
||||||
at.acctIn.Coins = types.Coins{{"mycoin", 2}}
|
at.acctIn.Coins = coin.Coins{{"mycoin", 2}}
|
||||||
at.initAccount(at.acctIn)
|
at.initAccount(at.acctIn)
|
||||||
res, _, _ := at.exec(t, at.getTx(1, types.Coins{{"mycoin", 5}}), true)
|
res, _, _ := at.exec(t, at.getTx(1, coin.Coins{{"mycoin", 5}}), true)
|
||||||
assert.True(res.IsErr(), "ExecTx/Bad CheckTx: Expected error return from ExecTx, returned: %v", res)
|
assert.True(res.IsErr(), "ExecTx/Bad CheckTx: Expected error return from ExecTx, returned: %v", res)
|
||||||
res, diffIn, diffOut := at.exec(t, at.getTx(1, types.Coins{{"mycoin", 5}}), false)
|
res, diffIn, diffOut := at.exec(t, at.getTx(1, coin.Coins{{"mycoin", 5}}), false)
|
||||||
assert.True(res.IsErr(), "ExecTx/Bad DeliverTx: Expected error return from ExecTx, returned: %v", res)
|
assert.True(res.IsErr(), "ExecTx/Bad DeliverTx: Expected error return from ExecTx, returned: %v", res)
|
||||||
assert.True(diffIn.IsZero())
|
assert.True(diffIn.IsZero())
|
||||||
assert.True(diffOut.IsZero())
|
assert.True(diffOut.IsZero())
|
||||||
|
|
||||||
//Regular CheckTx
|
//Regular CheckTx
|
||||||
at.reset()
|
at.reset()
|
||||||
res, _, _ = at.exec(t, at.getTx(1, types.Coins{{"mycoin", 5}}), true)
|
res, _, _ = at.exec(t, at.getTx(1, coin.Coins{{"mycoin", 5}}), true)
|
||||||
assert.True(res.IsOK(), "ExecTx/Good CheckTx: Expected OK return from ExecTx, Error: %v", res)
|
assert.True(res.IsOK(), "ExecTx/Good CheckTx: Expected OK return from ExecTx, Error: %v", res)
|
||||||
|
|
||||||
//Regular DeliverTx
|
//Regular DeliverTx
|
||||||
at.reset()
|
at.reset()
|
||||||
amt := types.Coins{{"mycoin", 3}}
|
amt := coin.Coins{{"mycoin", 3}}
|
||||||
res, diffIn, diffOut = at.exec(t, at.getTx(1, amt), false)
|
res, diffIn, diffOut = at.exec(t, at.getTx(1, amt), false)
|
||||||
assert.True(res.IsOK(), "ExecTx/Good DeliverTx: Expected OK return from ExecTx, Error: %v", res)
|
assert.True(res.IsOK(), "ExecTx/Good DeliverTx: Expected OK return from ExecTx, Error: %v", res)
|
||||||
assert.Equal(amt.Negative(), diffIn)
|
assert.Equal(amt.Negative(), diffIn)
|
||||||
|
@ -217,7 +216,7 @@ func TestQuery(t *testing.T) {
|
||||||
assert := assert.New(t)
|
assert := assert.New(t)
|
||||||
at := newAppTest(t)
|
at := newAppTest(t)
|
||||||
|
|
||||||
res, _, _ := at.exec(t, at.getTx(1, types.Coins{{"mycoin", 5}}), false)
|
res, _, _ := at.exec(t, at.getTx(1, coin.Coins{{"mycoin", 5}}), false)
|
||||||
assert.True(res.IsOK(), "Commit, DeliverTx: Expected OK return from DeliverTx, Error: %v", res)
|
assert.True(res.IsOK(), "Commit, DeliverTx: Expected OK return from DeliverTx, Error: %v", res)
|
||||||
|
|
||||||
resQueryPreCommit := at.app.Query(abci.RequestQuery{
|
resQueryPreCommit := at.app.Query(abci.RequestQuery{
|
||||||
|
|
|
@ -7,7 +7,7 @@ import (
|
||||||
|
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
"github.com/stretchr/testify/require"
|
"github.com/stretchr/testify/require"
|
||||||
"github.com/tendermint/basecoin/types"
|
"github.com/tendermint/basecoin/modules/coin"
|
||||||
eyescli "github.com/tendermint/merkleeyes/client"
|
eyescli "github.com/tendermint/merkleeyes/client"
|
||||||
cmn "github.com/tendermint/tmlibs/common"
|
cmn "github.com/tendermint/tmlibs/common"
|
||||||
"github.com/tendermint/tmlibs/log"
|
"github.com/tendermint/tmlibs/log"
|
||||||
|
@ -68,17 +68,17 @@ func TestLoadGenesisAccountAddress(t *testing.T) {
|
||||||
addr string
|
addr string
|
||||||
exists bool
|
exists bool
|
||||||
hasPubkey bool
|
hasPubkey bool
|
||||||
coins types.Coins
|
coins coin.Coins
|
||||||
}{
|
}{
|
||||||
// this comes from a public key, should be stored proper (alice)
|
// this comes from a public key, should be stored proper (alice)
|
||||||
{"62035D628DE7543332544AA60D90D3693B6AD51B", true, true, types.Coins{{"one", 111}}},
|
{"62035D628DE7543332544AA60D90D3693B6AD51B", true, true, coin.Coins{{"one", 111}}},
|
||||||
// this comes from an address, should be stored proper (bob)
|
// this comes from an address, should be stored proper (bob)
|
||||||
{"C471FB670E44D219EE6DF2FC284BE38793ACBCE1", true, false, types.Coins{{"two", 222}}},
|
{"C471FB670E44D219EE6DF2FC284BE38793ACBCE1", true, false, coin.Coins{{"two", 222}}},
|
||||||
// this one had a mismatched address and pubkey, should not store under either (carl)
|
// this one had a mismatched address and pubkey, should not store under either (carl)
|
||||||
{"1234ABCDD18E8EFE3FFC4B0506BF9BF8E5B0D9E9", false, false, nil}, // this is given addr
|
{"1234ABCDD18E8EFE3FFC4B0506BF9BF8E5B0D9E9", false, false, nil}, // this is given addr
|
||||||
{"700BEC5ED18E8EFE3FFC4B0506BF9BF8E5B0D9E9", false, false, nil}, // this is addr of the given pubkey
|
{"700BEC5ED18E8EFE3FFC4B0506BF9BF8E5B0D9E9", false, false, nil}, // this is addr of the given pubkey
|
||||||
// this comes from a secp256k1 public key, should be stored proper (sam)
|
// this comes from a secp256k1 public key, should be stored proper (sam)
|
||||||
{"979F080B1DD046C452C2A8A250D18646C6B669D4", true, true, types.Coins{{"four", 444}}},
|
{"979F080B1DD046C452C2A8A250D18646C6B669D4", true, true, coin.Coins{{"four", 444}}},
|
||||||
}
|
}
|
||||||
|
|
||||||
for i, tc := range cases {
|
for i, tc := range cases {
|
||||||
|
|
|
@ -16,7 +16,6 @@ import (
|
||||||
"github.com/tendermint/basecoin/modules/coin"
|
"github.com/tendermint/basecoin/modules/coin"
|
||||||
"github.com/tendermint/basecoin/stack"
|
"github.com/tendermint/basecoin/stack"
|
||||||
"github.com/tendermint/basecoin/txs"
|
"github.com/tendermint/basecoin/txs"
|
||||||
btypes "github.com/tendermint/basecoin/types"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
//-------------------------
|
//-------------------------
|
||||||
|
@ -94,7 +93,7 @@ func readSendTxFlags() (tx basecoin.Tx, err error) {
|
||||||
// // set the gas
|
// // set the gas
|
||||||
// tx.Gas = viper.GetInt64(FlagGas)
|
// tx.Gas = viper.GetInt64(FlagGas)
|
||||||
|
|
||||||
amountCoins, err := btypes.ParseCoins(viper.GetString(FlagAmount))
|
amountCoins, err := coin.ParseCoins(viper.GetString(FlagAmount))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return tx, err
|
return tx, err
|
||||||
}
|
}
|
||||||
|
|
|
@ -253,9 +253,9 @@ package commands
|
||||||
// ibc.IBCTx `json:"unwrap"`
|
// ibc.IBCTx `json:"unwrap"`
|
||||||
// }{ibcTx}))
|
// }{ibcTx}))
|
||||||
|
|
||||||
// smallCoins := types.Coin{"mycoin", 1}
|
// smallCoins := coin.Coin{"mycoin", 1}
|
||||||
|
|
||||||
// input := types.NewTxInput(r.privKey.PubKey, types.Coins{smallCoins}, sequence)
|
// input := types.NewTxInput(r.privKey.PubKey, coin.Coins{smallCoins}, sequence)
|
||||||
// tx := &types.AppTx{
|
// tx := &types.AppTx{
|
||||||
// Gas: 0,
|
// Gas: 0,
|
||||||
// Fee: smallCoins,
|
// Fee: smallCoins,
|
||||||
|
|
|
@ -9,8 +9,8 @@ import (
|
||||||
txcmd "github.com/tendermint/light-client/commands/txs"
|
txcmd "github.com/tendermint/light-client/commands/txs"
|
||||||
|
|
||||||
"github.com/tendermint/basecoin/docs/guide/counter/plugins/counter"
|
"github.com/tendermint/basecoin/docs/guide/counter/plugins/counter"
|
||||||
|
"github.com/tendermint/basecoin/modules/coin"
|
||||||
"github.com/tendermint/basecoin/txs"
|
"github.com/tendermint/basecoin/txs"
|
||||||
btypes "github.com/tendermint/basecoin/types"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
//CounterTxCmd is the CLI command to execute the counter
|
//CounterTxCmd is the CLI command to execute the counter
|
||||||
|
@ -70,7 +70,7 @@ func counterTx(cmd *cobra.Command, args []string) error {
|
||||||
}
|
}
|
||||||
|
|
||||||
func readCounterTxFlags() (tx basecoin.Tx, err error) {
|
func readCounterTxFlags() (tx basecoin.Tx, err error) {
|
||||||
feeCoins, err := btypes.ParseCoins(viper.GetString(FlagCountFee))
|
feeCoins, err := coin.ParseCoins(viper.GetString(FlagCountFee))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return tx, err
|
return tx, err
|
||||||
}
|
}
|
||||||
|
|
|
@ -11,7 +11,6 @@ import (
|
||||||
"github.com/tendermint/basecoin/modules/coin"
|
"github.com/tendermint/basecoin/modules/coin"
|
||||||
"github.com/tendermint/basecoin/stack"
|
"github.com/tendermint/basecoin/stack"
|
||||||
"github.com/tendermint/basecoin/state"
|
"github.com/tendermint/basecoin/state"
|
||||||
"github.com/tendermint/basecoin/types"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// Tx
|
// Tx
|
||||||
|
@ -33,12 +32,12 @@ func init() {
|
||||||
// Tx - struct for all counter transactions
|
// Tx - struct for all counter transactions
|
||||||
type Tx struct {
|
type Tx struct {
|
||||||
Valid bool `json:"valid"`
|
Valid bool `json:"valid"`
|
||||||
Fee types.Coins `json:"fee"`
|
Fee coin.Coins `json:"fee"`
|
||||||
Sequence int `json:"sequence"`
|
Sequence int `json:"sequence"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewTx - return a new counter transaction struct wrapped as a basecoin transaction
|
// NewTx - return a new counter transaction struct wrapped as a basecoin transaction
|
||||||
func NewTx(valid bool, fee types.Coins, sequence int) basecoin.Tx {
|
func NewTx(valid bool, fee coin.Coins, sequence int) basecoin.Tx {
|
||||||
return Tx{
|
return Tx{
|
||||||
Valid: valid,
|
Valid: valid,
|
||||||
Fee: fee,
|
Fee: fee,
|
||||||
|
@ -184,7 +183,7 @@ func StoreActor() basecoin.Actor {
|
||||||
// State - state of the counter applicaton
|
// State - state of the counter applicaton
|
||||||
type State struct {
|
type State struct {
|
||||||
Counter int `json:"counter"`
|
Counter int `json:"counter"`
|
||||||
TotalFees types.Coins `json:"total_fees"`
|
TotalFees coin.Coins `json:"total_fees"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// StateKey - store key for the counter state
|
// StateKey - store key for the counter state
|
||||||
|
|
|
@ -10,7 +10,6 @@ import (
|
||||||
"github.com/tendermint/basecoin/app"
|
"github.com/tendermint/basecoin/app"
|
||||||
"github.com/tendermint/basecoin/modules/coin"
|
"github.com/tendermint/basecoin/modules/coin"
|
||||||
"github.com/tendermint/basecoin/txs"
|
"github.com/tendermint/basecoin/txs"
|
||||||
"github.com/tendermint/basecoin/types"
|
|
||||||
"github.com/tendermint/go-wire"
|
"github.com/tendermint/go-wire"
|
||||||
eyescli "github.com/tendermint/merkleeyes/client"
|
eyescli "github.com/tendermint/merkleeyes/client"
|
||||||
"github.com/tendermint/tmlibs/log"
|
"github.com/tendermint/tmlibs/log"
|
||||||
|
@ -34,13 +33,13 @@ func TestCounterPlugin(t *testing.T) {
|
||||||
bcApp.SetOption("base/chain_id", chainID)
|
bcApp.SetOption("base/chain_id", chainID)
|
||||||
|
|
||||||
// Account initialization
|
// Account initialization
|
||||||
bal := types.Coins{{"", 1000}, {"gold", 1000}}
|
bal := coin.Coins{{"", 1000}, {"gold", 1000}}
|
||||||
acct := coin.NewAccountWithKey(bal)
|
acct := coin.NewAccountWithKey(bal)
|
||||||
log := bcApp.SetOption("coin/account", acct.MakeOption())
|
log := bcApp.SetOption("coin/account", acct.MakeOption())
|
||||||
require.Equal(t, "Success", log)
|
require.Equal(t, "Success", log)
|
||||||
|
|
||||||
// Deliver a CounterTx
|
// Deliver a CounterTx
|
||||||
DeliverCounterTx := func(valid bool, counterFee types.Coins, inputSequence int) abci.Result {
|
DeliverCounterTx := func(valid bool, counterFee coin.Coins, inputSequence int) abci.Result {
|
||||||
tx := NewTx(valid, counterFee, inputSequence)
|
tx := NewTx(valid, counterFee, inputSequence)
|
||||||
tx = txs.NewChain(chainID, tx)
|
tx = txs.NewChain(chainID, tx)
|
||||||
stx := txs.NewSig(tx)
|
stx := txs.NewSig(tx)
|
||||||
|
@ -58,10 +57,10 @@ func TestCounterPlugin(t *testing.T) {
|
||||||
assert.True(res.IsErr(), res.String())
|
assert.True(res.IsErr(), res.String())
|
||||||
|
|
||||||
// Test the fee (increments sequence)
|
// Test the fee (increments sequence)
|
||||||
res = DeliverCounterTx(true, types.Coins{{"gold", 100}}, 1)
|
res = DeliverCounterTx(true, coin.Coins{{"gold", 100}}, 1)
|
||||||
assert.True(res.IsOK(), res.String())
|
assert.True(res.IsOK(), res.String())
|
||||||
|
|
||||||
// Test unsupported fee
|
// Test unsupported fee
|
||||||
res = DeliverCounterTx(true, types.Coins{{"silver", 100}}, 2)
|
res = DeliverCounterTx(true, coin.Coins{{"silver", 100}}, 2)
|
||||||
assert.True(res.IsErr(), res.String())
|
assert.True(res.IsErr(), res.String())
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
package types
|
package coin
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
@ -10,6 +10,7 @@ import (
|
||||||
"github.com/pkg/errors"
|
"github.com/pkg/errors"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// Coin hold some amount of one currency
|
||||||
type Coin struct {
|
type Coin struct {
|
||||||
Denom string `json:"denom"`
|
Denom string `json:"denom"`
|
||||||
Amount int64 `json:"amount"`
|
Amount int64 `json:"amount"`
|
||||||
|
@ -25,6 +26,8 @@ var reAmt = regexp.MustCompile("(\\d+)")
|
||||||
|
|
||||||
var reCoin = regexp.MustCompile("^([[:digit:]]+)[[:space:]]*([[:alpha:]]+)$")
|
var reCoin = regexp.MustCompile("^([[:digit:]]+)[[:space:]]*([[:alpha:]]+)$")
|
||||||
|
|
||||||
|
// ParseCoin parses a cli input for one coin type, returning errors if invalid.
|
||||||
|
// This returns an error on an empty string as well.
|
||||||
func ParseCoin(str string) (Coin, error) {
|
func ParseCoin(str string) (Coin, error) {
|
||||||
var coin Coin
|
var coin Coin
|
||||||
|
|
||||||
|
@ -45,6 +48,7 @@ func ParseCoin(str string) (Coin, error) {
|
||||||
|
|
||||||
//----------------------------------------
|
//----------------------------------------
|
||||||
|
|
||||||
|
// Coins is a set of Coin, one per currency
|
||||||
type Coins []Coin
|
type Coins []Coin
|
||||||
|
|
||||||
func (coins Coins) String() string {
|
func (coins Coins) String() string {
|
||||||
|
@ -59,6 +63,8 @@ func (coins Coins) String() string {
|
||||||
return out[:len(out)-1]
|
return out[:len(out)-1]
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ParseCoins will parse out a list of coins separated by commas.
|
||||||
|
// If nothing is provided, it returns an empty array
|
||||||
func ParseCoins(str string) (Coins, error) {
|
func ParseCoins(str string) (Coins, error) {
|
||||||
// empty string is empty list...
|
// empty string is empty list...
|
||||||
if len(str) == 0 {
|
if len(str) == 0 {
|
||||||
|
@ -85,7 +91,7 @@ func ParseCoins(str string) (Coins, error) {
|
||||||
return coins, nil
|
return coins, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Must be sorted, and not have 0 amounts
|
// IsValid asserts the Coins are sorted, and don't have 0 amounts
|
||||||
func (coins Coins) IsValid() bool {
|
func (coins Coins) IsValid() bool {
|
||||||
switch len(coins) {
|
switch len(coins) {
|
||||||
case 0:
|
case 0:
|
||||||
|
@ -108,6 +114,8 @@ func (coins Coins) IsValid() bool {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Plus combines to sets of coins
|
||||||
|
//
|
||||||
// TODO: handle empty coins!
|
// TODO: handle empty coins!
|
||||||
// Currently appends an empty coin ...
|
// Currently appends an empty coin ...
|
||||||
func (coinsA Coins) Plus(coinsB Coins) Coins {
|
func (coinsA Coins) Plus(coinsB Coins) Coins {
|
||||||
|
@ -118,9 +126,8 @@ func (coinsA Coins) Plus(coinsB Coins) Coins {
|
||||||
if indexA == lenA {
|
if indexA == lenA {
|
||||||
if indexB == lenB {
|
if indexB == lenB {
|
||||||
return sum
|
return sum
|
||||||
} else {
|
|
||||||
return append(sum, coinsB[indexB:]...)
|
|
||||||
}
|
}
|
||||||
|
return append(sum, coinsB[indexB:]...)
|
||||||
} else if indexB == lenB {
|
} else if indexB == lenB {
|
||||||
return append(sum, coinsA[indexA:]...)
|
return append(sum, coinsA[indexA:]...)
|
||||||
}
|
}
|
||||||
|
@ -128,7 +135,7 @@ func (coinsA Coins) Plus(coinsB Coins) Coins {
|
||||||
switch strings.Compare(coinA.Denom, coinB.Denom) {
|
switch strings.Compare(coinA.Denom, coinB.Denom) {
|
||||||
case -1:
|
case -1:
|
||||||
sum = append(sum, coinA)
|
sum = append(sum, coinA)
|
||||||
indexA += 1
|
indexA++
|
||||||
case 0:
|
case 0:
|
||||||
if coinA.Amount+coinB.Amount == 0 {
|
if coinA.Amount+coinB.Amount == 0 {
|
||||||
// ignore 0 sum coin type
|
// ignore 0 sum coin type
|
||||||
|
@ -138,16 +145,17 @@ func (coinsA Coins) Plus(coinsB Coins) Coins {
|
||||||
Amount: coinA.Amount + coinB.Amount,
|
Amount: coinA.Amount + coinB.Amount,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
indexA += 1
|
indexA++
|
||||||
indexB += 1
|
indexB++
|
||||||
case 1:
|
case 1:
|
||||||
sum = append(sum, coinB)
|
sum = append(sum, coinB)
|
||||||
indexB += 1
|
indexB++
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return sum
|
return sum
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Negative returns a set of coins with all amount negative
|
||||||
func (coins Coins) Negative() Coins {
|
func (coins Coins) Negative() Coins {
|
||||||
res := make([]Coin, 0, len(coins))
|
res := make([]Coin, 0, len(coins))
|
||||||
for _, coin := range coins {
|
for _, coin := range coins {
|
||||||
|
@ -159,10 +167,14 @@ func (coins Coins) Negative() Coins {
|
||||||
return res
|
return res
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Minus subtracts a set of coins from another (adds the inverse)
|
||||||
func (coinsA Coins) Minus(coinsB Coins) Coins {
|
func (coinsA Coins) Minus(coinsB Coins) Coins {
|
||||||
return coinsA.Plus(coinsB.Negative())
|
return coinsA.Plus(coinsB.Negative())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// IsGTE returns True iff coinsA is NonNegative(), and for every
|
||||||
|
// currency in coinsB, the currency is present at an equal or greater
|
||||||
|
// amount in coinsB
|
||||||
func (coinsA Coins) IsGTE(coinsB Coins) bool {
|
func (coinsA Coins) IsGTE(coinsB Coins) bool {
|
||||||
diff := coinsA.Minus(coinsB)
|
diff := coinsA.Minus(coinsB)
|
||||||
if len(diff) == 0 {
|
if len(diff) == 0 {
|
||||||
|
@ -171,10 +183,12 @@ func (coinsA Coins) IsGTE(coinsB Coins) bool {
|
||||||
return diff.IsNonnegative()
|
return diff.IsNonnegative()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// IsZero returns true if there are no coins
|
||||||
func (coins Coins) IsZero() bool {
|
func (coins Coins) IsZero() bool {
|
||||||
return len(coins) == 0
|
return len(coins) == 0
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// IsEqual returns true if the two sets of Coins have the same value
|
||||||
func (coinsA Coins) IsEqual(coinsB Coins) bool {
|
func (coinsA Coins) IsEqual(coinsB Coins) bool {
|
||||||
if len(coinsA) != len(coinsB) {
|
if len(coinsA) != len(coinsB) {
|
||||||
return false
|
return false
|
||||||
|
@ -187,6 +201,8 @@ func (coinsA Coins) IsEqual(coinsB Coins) bool {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// IsPositive returns true if there is at least one coin, and all
|
||||||
|
// currencies have a positive value
|
||||||
func (coins Coins) IsPositive() bool {
|
func (coins Coins) IsPositive() bool {
|
||||||
if len(coins) == 0 {
|
if len(coins) == 0 {
|
||||||
return false
|
return false
|
||||||
|
@ -199,6 +215,8 @@ func (coins Coins) IsPositive() bool {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// IsNonnegative returns true if there is no currency with a negative value
|
||||||
|
// (even no coins is true here)
|
||||||
func (coins Coins) IsNonnegative() bool {
|
func (coins Coins) IsNonnegative() bool {
|
||||||
if len(coins) == 0 {
|
if len(coins) == 0 {
|
||||||
return true
|
return true
|
||||||
|
@ -213,7 +231,12 @@ func (coins Coins) IsNonnegative() bool {
|
||||||
|
|
||||||
/*** Implement Sort interface ***/
|
/*** Implement Sort interface ***/
|
||||||
|
|
||||||
func (c Coins) Len() int { return len(c) }
|
//nolint
|
||||||
func (c Coins) Less(i, j int) bool { return c[i].Denom < c[j].Denom }
|
func (coins Coins) Len() int { return len(coins) }
|
||||||
func (c Coins) Swap(i, j int) { c[i], c[j] = c[j], c[i] }
|
func (coins Coins) Less(i, j int) bool { return coins[i].Denom < coins[j].Denom }
|
||||||
func (c Coins) Sort() { sort.Sort(c) }
|
func (coins Coins) Swap(i, j int) { coins[i], coins[j] = coins[j], coins[i] }
|
||||||
|
|
||||||
|
var _ sort.Interface = Coins{}
|
||||||
|
|
||||||
|
// Sort is a helper function to sort the set of coins inplace
|
||||||
|
func (coins Coins) Sort() { sort.Sort(coins) }
|
|
@ -1,4 +1,4 @@
|
||||||
package types
|
package coin
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"testing"
|
"testing"
|
|
@ -7,8 +7,6 @@ import (
|
||||||
|
|
||||||
crypto "github.com/tendermint/go-crypto"
|
crypto "github.com/tendermint/go-crypto"
|
||||||
"github.com/tendermint/go-wire/data"
|
"github.com/tendermint/go-wire/data"
|
||||||
|
|
||||||
"github.com/tendermint/basecoin/types"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
/**** code to parse accounts from genesis docs ***/
|
/**** code to parse accounts from genesis docs ***/
|
||||||
|
@ -19,7 +17,7 @@ type GenesisAccount struct {
|
||||||
// this from types.Account (don't know how to embed this properly)
|
// this from types.Account (don't know how to embed this properly)
|
||||||
PubKey crypto.PubKey `json:"pub_key"` // May be nil, if not known.
|
PubKey crypto.PubKey `json:"pub_key"` // May be nil, if not known.
|
||||||
Sequence int `json:"sequence"`
|
Sequence int `json:"sequence"`
|
||||||
Balance types.Coins `json:"coins"`
|
Balance Coins `json:"coins"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// ToAccount - GenesisAccount struct to a basecoin Account
|
// ToAccount - GenesisAccount struct to a basecoin Account
|
||||||
|
|
|
@ -13,7 +13,6 @@ import (
|
||||||
"github.com/tendermint/basecoin"
|
"github.com/tendermint/basecoin"
|
||||||
"github.com/tendermint/basecoin/stack"
|
"github.com/tendermint/basecoin/stack"
|
||||||
"github.com/tendermint/basecoin/state"
|
"github.com/tendermint/basecoin/state"
|
||||||
"github.com/tendermint/basecoin/types"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// this makes sure that txs are rejected with invalid data or permissions
|
// this makes sure that txs are rejected with invalid data or permissions
|
||||||
|
@ -23,9 +22,9 @@ func TestHandlerValidation(t *testing.T) {
|
||||||
// these are all valid, except for minusCoins
|
// these are all valid, except for minusCoins
|
||||||
addr1 := basecoin.Actor{App: "coin", Address: []byte{1, 2}}
|
addr1 := basecoin.Actor{App: "coin", Address: []byte{1, 2}}
|
||||||
addr2 := basecoin.Actor{App: "role", Address: []byte{7, 8}}
|
addr2 := basecoin.Actor{App: "role", Address: []byte{7, 8}}
|
||||||
someCoins := types.Coins{{"atom", 123}}
|
someCoins := Coins{{"atom", 123}}
|
||||||
doubleCoins := types.Coins{{"atom", 246}}
|
doubleCoins := Coins{{"atom", 246}}
|
||||||
minusCoins := types.Coins{{"eth", -34}}
|
minusCoins := Coins{{"eth", -34}}
|
||||||
|
|
||||||
cases := []struct {
|
cases := []struct {
|
||||||
valid bool
|
valid bool
|
||||||
|
@ -93,15 +92,15 @@ func TestDeliverTx(t *testing.T) {
|
||||||
addr2 := basecoin.Actor{App: "role", Address: []byte{7, 8}}
|
addr2 := basecoin.Actor{App: "role", Address: []byte{7, 8}}
|
||||||
addr3 := basecoin.Actor{App: "coin", Address: []byte{6, 5, 4, 3}}
|
addr3 := basecoin.Actor{App: "coin", Address: []byte{6, 5, 4, 3}}
|
||||||
|
|
||||||
someCoins := types.Coins{{"atom", 123}}
|
someCoins := Coins{{"atom", 123}}
|
||||||
moreCoins := types.Coins{{"atom", 6487}}
|
moreCoins := Coins{{"atom", 6487}}
|
||||||
diffCoins := moreCoins.Minus(someCoins)
|
diffCoins := moreCoins.Minus(someCoins)
|
||||||
otherCoins := types.Coins{{"eth", 11}}
|
otherCoins := Coins{{"eth", 11}}
|
||||||
mixedCoins := someCoins.Plus(otherCoins)
|
mixedCoins := someCoins.Plus(otherCoins)
|
||||||
|
|
||||||
type money struct {
|
type money struct {
|
||||||
addr basecoin.Actor
|
addr basecoin.Actor
|
||||||
coins types.Coins
|
coins Coins
|
||||||
}
|
}
|
||||||
|
|
||||||
cases := []struct {
|
cases := []struct {
|
||||||
|
@ -175,13 +174,13 @@ func TestSetOption(t *testing.T) {
|
||||||
addr := pk.PubKey().Address()
|
addr := pk.PubKey().Address()
|
||||||
actor := basecoin.Actor{App: stack.NameSigs, Address: addr}
|
actor := basecoin.Actor{App: stack.NameSigs, Address: addr}
|
||||||
|
|
||||||
someCoins := types.Coins{{"atom", 123}}
|
someCoins := Coins{{"atom", 123}}
|
||||||
otherCoins := types.Coins{{"eth", 11}}
|
otherCoins := Coins{{"eth", 11}}
|
||||||
mixedCoins := someCoins.Plus(otherCoins)
|
mixedCoins := someCoins.Plus(otherCoins)
|
||||||
|
|
||||||
type money struct {
|
type money struct {
|
||||||
addr basecoin.Actor
|
addr basecoin.Actor
|
||||||
coins types.Coins
|
coins Coins
|
||||||
}
|
}
|
||||||
|
|
||||||
cases := []struct {
|
cases := []struct {
|
||||||
|
|
|
@ -1,11 +1,11 @@
|
||||||
package coin
|
package coin
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/tendermint/basecoin"
|
|
||||||
"github.com/tendermint/basecoin/stack"
|
|
||||||
"github.com/tendermint/basecoin/types"
|
|
||||||
crypto "github.com/tendermint/go-crypto"
|
crypto "github.com/tendermint/go-crypto"
|
||||||
"github.com/tendermint/go-wire/data"
|
"github.com/tendermint/go-wire/data"
|
||||||
|
|
||||||
|
"github.com/tendermint/basecoin"
|
||||||
|
"github.com/tendermint/basecoin/stack"
|
||||||
)
|
)
|
||||||
|
|
||||||
// AccountWithKey is a helper for tests, that includes and account
|
// AccountWithKey is a helper for tests, that includes and account
|
||||||
|
@ -17,7 +17,7 @@ type AccountWithKey struct {
|
||||||
|
|
||||||
// NewAccountWithKey creates an account with the given balance
|
// NewAccountWithKey creates an account with the given balance
|
||||||
// and a random private key
|
// and a random private key
|
||||||
func NewAccountWithKey(coins types.Coins) *AccountWithKey {
|
func NewAccountWithKey(coins Coins) *AccountWithKey {
|
||||||
return &AccountWithKey{
|
return &AccountWithKey{
|
||||||
Key: crypto.GenPrivKeyEd25519().Wrap(),
|
Key: crypto.GenPrivKeyEd25519().Wrap(),
|
||||||
Account: Account{Coins: coins},
|
Account: Account{Coins: coins},
|
||||||
|
|
|
@ -8,7 +8,6 @@ import (
|
||||||
"github.com/tendermint/basecoin"
|
"github.com/tendermint/basecoin"
|
||||||
"github.com/tendermint/basecoin/errors"
|
"github.com/tendermint/basecoin/errors"
|
||||||
"github.com/tendermint/basecoin/state"
|
"github.com/tendermint/basecoin/state"
|
||||||
"github.com/tendermint/basecoin/types"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// Accountant - custom object to manage coins for the coin module
|
// Accountant - custom object to manage coins for the coin module
|
||||||
|
@ -39,13 +38,13 @@ func (a Accountant) GetAccount(store state.KVStore, addr basecoin.Actor) (Accoun
|
||||||
}
|
}
|
||||||
|
|
||||||
// CheckCoins makes sure there are funds, but doesn't change anything
|
// CheckCoins makes sure there are funds, but doesn't change anything
|
||||||
func (a Accountant) CheckCoins(store state.KVStore, addr basecoin.Actor, coins types.Coins, seq int) (types.Coins, error) {
|
func (a Accountant) CheckCoins(store state.KVStore, addr basecoin.Actor, coins Coins, seq int) (Coins, error) {
|
||||||
acct, err := a.updateCoins(store, addr, coins, seq)
|
acct, err := a.updateCoins(store, addr, coins, seq)
|
||||||
return acct.Coins, err
|
return acct.Coins, err
|
||||||
}
|
}
|
||||||
|
|
||||||
// ChangeCoins changes the money, returns error if it would be negative
|
// ChangeCoins changes the money, returns error if it would be negative
|
||||||
func (a Accountant) ChangeCoins(store state.KVStore, addr basecoin.Actor, coins types.Coins, seq int) (types.Coins, error) {
|
func (a Accountant) ChangeCoins(store state.KVStore, addr basecoin.Actor, coins Coins, seq int) (Coins, error) {
|
||||||
acct, err := a.updateCoins(store, addr, coins, seq)
|
acct, err := a.updateCoins(store, addr, coins, seq)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return acct.Coins, err
|
return acct.Coins, err
|
||||||
|
@ -58,7 +57,7 @@ func (a Accountant) ChangeCoins(store state.KVStore, addr basecoin.Actor, coins
|
||||||
// updateCoins will load the account, make all checks, and return the updated account.
|
// updateCoins will load the account, make all checks, and return the updated account.
|
||||||
//
|
//
|
||||||
// it doesn't save anything, that is up to you to decide (Check/Change Coins)
|
// it doesn't save anything, that is up to you to decide (Check/Change Coins)
|
||||||
func (a Accountant) updateCoins(store state.KVStore, addr basecoin.Actor, coins types.Coins, seq int) (acct Account, err error) {
|
func (a Accountant) updateCoins(store state.KVStore, addr basecoin.Actor, coins Coins, seq int) (acct Account, err error) {
|
||||||
acct, err = loadAccount(store, a.MakeKey(addr))
|
acct, err = loadAccount(store, a.MakeKey(addr))
|
||||||
// we can increase an empty account...
|
// we can increase an empty account...
|
||||||
if IsNoAccountErr(err) && coins.IsPositive() {
|
if IsNoAccountErr(err) && coins.IsPositive() {
|
||||||
|
@ -98,7 +97,7 @@ func (a Accountant) MakeKey(addr basecoin.Actor) []byte {
|
||||||
|
|
||||||
// Account - coin account structure
|
// Account - coin account structure
|
||||||
type Account struct {
|
type Account struct {
|
||||||
Coins types.Coins `json:"coins"`
|
Coins Coins `json:"coins"`
|
||||||
Sequence int `json:"sequence"`
|
Sequence int `json:"sequence"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -4,7 +4,6 @@ import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
|
||||||
"github.com/tendermint/basecoin"
|
"github.com/tendermint/basecoin"
|
||||||
"github.com/tendermint/basecoin/types"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
|
@ -22,7 +21,7 @@ const (
|
||||||
// TxInput - expected coin movement outputs, used with SendTx
|
// TxInput - expected coin movement outputs, used with SendTx
|
||||||
type TxInput struct {
|
type TxInput struct {
|
||||||
Address basecoin.Actor `json:"address"`
|
Address basecoin.Actor `json:"address"`
|
||||||
Coins types.Coins `json:"coins"`
|
Coins Coins `json:"coins"`
|
||||||
Sequence int `json:"sequence"` // Nonce: Must be 1 greater than the last committed TxInput
|
Sequence int `json:"sequence"` // Nonce: Must be 1 greater than the last committed TxInput
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -52,7 +51,7 @@ func (txIn TxInput) String() string {
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewTxInput - create a transaction input, used with SendTx
|
// NewTxInput - create a transaction input, used with SendTx
|
||||||
func NewTxInput(addr basecoin.Actor, coins types.Coins, sequence int) TxInput {
|
func NewTxInput(addr basecoin.Actor, coins Coins, sequence int) TxInput {
|
||||||
input := TxInput{
|
input := TxInput{
|
||||||
Address: addr,
|
Address: addr,
|
||||||
Coins: coins,
|
Coins: coins,
|
||||||
|
@ -66,7 +65,7 @@ func NewTxInput(addr basecoin.Actor, coins types.Coins, sequence int) TxInput {
|
||||||
// TxOutput - expected coin movement output, used with SendTx
|
// TxOutput - expected coin movement output, used with SendTx
|
||||||
type TxOutput struct {
|
type TxOutput struct {
|
||||||
Address basecoin.Actor `json:"address"`
|
Address basecoin.Actor `json:"address"`
|
||||||
Coins types.Coins `json:"coins"`
|
Coins Coins `json:"coins"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// ValidateBasic - validate transaction output
|
// ValidateBasic - validate transaction output
|
||||||
|
@ -92,7 +91,7 @@ func (txOut TxOutput) String() string {
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewTxOutput - create a transaction output, used with SendTx
|
// NewTxOutput - create a transaction output, used with SendTx
|
||||||
func NewTxOutput(addr basecoin.Actor, coins types.Coins) TxOutput {
|
func NewTxOutput(addr basecoin.Actor, coins Coins) TxOutput {
|
||||||
output := TxOutput{
|
output := TxOutput{
|
||||||
Address: addr,
|
Address: addr,
|
||||||
Coins: coins,
|
Coins: coins,
|
||||||
|
@ -127,7 +126,7 @@ func (tx SendTx) ValidateBasic() error {
|
||||||
return ErrNoOutputs()
|
return ErrNoOutputs()
|
||||||
}
|
}
|
||||||
// make sure all inputs and outputs are individually valid
|
// make sure all inputs and outputs are individually valid
|
||||||
var totalIn, totalOut types.Coins
|
var totalIn, totalOut Coins
|
||||||
for _, in := range tx.Inputs {
|
for _, in := range tx.Inputs {
|
||||||
if err := in.ValidateBasic(); err != nil {
|
if err := in.ValidateBasic(); err != nil {
|
||||||
return err
|
return err
|
||||||
|
|
|
@ -5,9 +5,10 @@ import (
|
||||||
|
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
"github.com/stretchr/testify/require"
|
"github.com/stretchr/testify/require"
|
||||||
"github.com/tendermint/basecoin"
|
|
||||||
"github.com/tendermint/basecoin/types"
|
|
||||||
"github.com/tendermint/go-wire/data"
|
"github.com/tendermint/go-wire/data"
|
||||||
|
|
||||||
|
"github.com/tendermint/basecoin"
|
||||||
)
|
)
|
||||||
|
|
||||||
// these are some constructs for the test cases
|
// these are some constructs for the test cases
|
||||||
|
@ -24,22 +25,22 @@ var actors = []struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
var (
|
var (
|
||||||
zeroCoin = types.Coin{"zeros", 0}
|
zeroCoin = Coin{"zeros", 0}
|
||||||
plusCoin = types.Coin{"plus", 23}
|
plusCoin = Coin{"plus", 23}
|
||||||
negCoin = types.Coin{"neg", -42}
|
negCoin = Coin{"neg", -42}
|
||||||
)
|
)
|
||||||
|
|
||||||
var coins = []struct {
|
var coins = []struct {
|
||||||
coins types.Coins
|
coins Coins
|
||||||
valid bool
|
valid bool
|
||||||
}{
|
}{
|
||||||
{types.Coins{}, false},
|
{Coins{}, false},
|
||||||
{types.Coins{zeroCoin}, false},
|
{Coins{zeroCoin}, false},
|
||||||
{types.Coins{plusCoin}, true},
|
{Coins{plusCoin}, true},
|
||||||
{types.Coins{negCoin}, false},
|
{Coins{negCoin}, false},
|
||||||
{types.Coins{plusCoin, plusCoin}, false},
|
{Coins{plusCoin, plusCoin}, false},
|
||||||
{types.Coins{plusCoin, zeroCoin}, false},
|
{Coins{plusCoin, zeroCoin}, false},
|
||||||
{types.Coins{negCoin, plusCoin}, false},
|
{Coins{negCoin, plusCoin}, false},
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestTxValidateInput(t *testing.T) {
|
func TestTxValidateInput(t *testing.T) {
|
||||||
|
@ -94,12 +95,12 @@ func TestTxValidateTx(t *testing.T) {
|
||||||
addr3 := basecoin.Actor{App: "role", Address: []byte{7, 8}}
|
addr3 := basecoin.Actor{App: "role", Address: []byte{7, 8}}
|
||||||
noAddr := basecoin.Actor{}
|
noAddr := basecoin.Actor{}
|
||||||
|
|
||||||
noCoins := types.Coins{}
|
noCoins := Coins{}
|
||||||
someCoins := types.Coins{{"atom", 123}}
|
someCoins := Coins{{"atom", 123}}
|
||||||
moreCoins := types.Coins{{"atom", 124}}
|
moreCoins := Coins{{"atom", 124}}
|
||||||
otherCoins := types.Coins{{"btc", 15}}
|
otherCoins := Coins{{"btc", 15}}
|
||||||
bothCoins := someCoins.Plus(otherCoins)
|
bothCoins := someCoins.Plus(otherCoins)
|
||||||
minusCoins := types.Coins{{"eth", -34}}
|
minusCoins := Coins{{"eth", -34}}
|
||||||
|
|
||||||
// cases: all valid (one), all valid (multi)
|
// cases: all valid (one), all valid (multi)
|
||||||
// no input, no outputs, invalid inputs, invalid outputs
|
// no input, no outputs, invalid inputs, invalid outputs
|
||||||
|
@ -181,7 +182,7 @@ func TestTxSerializeTx(t *testing.T) {
|
||||||
|
|
||||||
addr1 := basecoin.Actor{App: "coin", Address: []byte{1, 2}}
|
addr1 := basecoin.Actor{App: "coin", Address: []byte{1, 2}}
|
||||||
addr2 := basecoin.Actor{App: "coin", Address: []byte{3, 4}}
|
addr2 := basecoin.Actor{App: "coin", Address: []byte{3, 4}}
|
||||||
someCoins := types.Coins{{"atom", 123}}
|
someCoins := Coins{{"atom", 123}}
|
||||||
|
|
||||||
send := NewSendTx(
|
send := NewSendTx(
|
||||||
[]TxInput{NewTxInput(addr1, someCoins, 2)},
|
[]TxInput{NewTxInput(addr1, someCoins, 2)},
|
||||||
|
|
|
@ -3,9 +3,9 @@ package fee
|
||||||
import (
|
import (
|
||||||
"github.com/tendermint/basecoin"
|
"github.com/tendermint/basecoin"
|
||||||
"github.com/tendermint/basecoin/errors"
|
"github.com/tendermint/basecoin/errors"
|
||||||
|
"github.com/tendermint/basecoin/modules/coin"
|
||||||
"github.com/tendermint/basecoin/stack"
|
"github.com/tendermint/basecoin/stack"
|
||||||
"github.com/tendermint/basecoin/state"
|
"github.com/tendermint/basecoin/state"
|
||||||
"github.com/tendermint/basecoin/types"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// NameFee - namespace for the fee module
|
// NameFee - namespace for the fee module
|
||||||
|
@ -14,17 +14,17 @@ const NameFee = "fee"
|
||||||
// AccountChecker - interface used by SimpleFeeHandler
|
// AccountChecker - interface used by SimpleFeeHandler
|
||||||
type AccountChecker interface {
|
type AccountChecker interface {
|
||||||
// Get amount checks the current amount
|
// Get amount checks the current amount
|
||||||
GetAmount(store state.KVStore, addr basecoin.Actor) (types.Coins, error)
|
GetAmount(store state.KVStore, addr basecoin.Actor) (coin.Coins, error)
|
||||||
|
|
||||||
// ChangeAmount modifies the balance by the given amount and returns the new balance
|
// ChangeAmount modifies the balance by the given amount and returns the new balance
|
||||||
// always returns an error if leading to negative balance
|
// always returns an error if leading to negative balance
|
||||||
ChangeAmount(store state.KVStore, addr basecoin.Actor, coins types.Coins) (types.Coins, error)
|
ChangeAmount(store state.KVStore, addr basecoin.Actor, coins coin.Coins) (coin.Coins, error)
|
||||||
}
|
}
|
||||||
|
|
||||||
// SimpleFeeHandler - checker object for fee checking
|
// SimpleFeeHandler - checker object for fee checking
|
||||||
type SimpleFeeHandler struct {
|
type SimpleFeeHandler struct {
|
||||||
AccountChecker
|
AccountChecker
|
||||||
MinFee types.Coins
|
MinFee coin.Coins
|
||||||
stack.PassOption
|
stack.PassOption
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -44,7 +44,7 @@ func (h SimpleFeeHandler) CheckTx(ctx basecoin.Context, store state.KVStore, tx
|
||||||
return res, errors.ErrInvalidFormat(tx)
|
return res, errors.ErrInvalidFormat(tx)
|
||||||
}
|
}
|
||||||
|
|
||||||
fees := types.Coins{feeTx.Fee}
|
fees := coin.Coins{feeTx.Fee}
|
||||||
if !fees.IsGTE(h.MinFee) {
|
if !fees.IsGTE(h.MinFee) {
|
||||||
return res, ErrInsufficientFees()
|
return res, ErrInsufficientFees()
|
||||||
}
|
}
|
||||||
|
@ -68,7 +68,7 @@ func (h SimpleFeeHandler) DeliverTx(ctx basecoin.Context, store state.KVStore, t
|
||||||
return res, errors.ErrInvalidFormat(tx)
|
return res, errors.ErrInvalidFormat(tx)
|
||||||
}
|
}
|
||||||
|
|
||||||
fees := types.Coins{feeTx.Fee}
|
fees := coin.Coins{feeTx.Fee}
|
||||||
if !fees.IsGTE(h.MinFee) {
|
if !fees.IsGTE(h.MinFee) {
|
||||||
return res, ErrInsufficientFees()
|
return res, ErrInsufficientFees()
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,9 +2,10 @@ package fee
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/tendermint/basecoin"
|
"github.com/tendermint/basecoin"
|
||||||
"github.com/tendermint/basecoin/types"
|
"github.com/tendermint/basecoin/modules/coin"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// nolint
|
||||||
const (
|
const (
|
||||||
ByteFees = 0x20
|
ByteFees = 0x20
|
||||||
TypeFees = "fee"
|
TypeFees = "fee"
|
||||||
|
@ -20,12 +21,12 @@ func init() {
|
||||||
// Fee attaches a fee payment to the embedded tx
|
// Fee attaches a fee payment to the embedded tx
|
||||||
type Fee struct {
|
type Fee struct {
|
||||||
Tx basecoin.Tx `json:"tx"`
|
Tx basecoin.Tx `json:"tx"`
|
||||||
Fee types.Coin `json:"fee"`
|
Fee coin.Coin `json:"fee"`
|
||||||
Payer basecoin.Actor `json:"payer"` // the address who pays the fee
|
Payer basecoin.Actor `json:"payer"` // the address who pays the fee
|
||||||
// Gas types.Coin `json:"gas"` // ?????
|
// Gas coin.Coin `json:"gas"` // ?????
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewFee(tx basecoin.Tx, fee types.Coin, payer basecoin.Actor) basecoin.Tx {
|
func NewFee(tx basecoin.Tx, fee coin.Coin, payer basecoin.Actor) basecoin.Tx {
|
||||||
return (&Fee{Tx: tx, Fee: fee, Payer: payer}).Wrap()
|
return (&Fee{Tx: tx, Fee: fee, Payer: payer}).Wrap()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -147,7 +147,7 @@ package ibc
|
||||||
|
|
||||||
// type CoinsPayload struct {
|
// type CoinsPayload struct {
|
||||||
// Address []byte
|
// Address []byte
|
||||||
// Coins types.Coins
|
// Coins coin.Coins
|
||||||
// }
|
// }
|
||||||
|
|
||||||
// func (p CoinsPayload) Type() string {
|
// func (p CoinsPayload) Type() string {
|
||||||
|
|
|
@ -106,7 +106,7 @@ package ibc
|
||||||
// store.SetLogging() // Log all activity
|
// store.SetLogging() // Log all activity
|
||||||
|
|
||||||
// ibcPlugin := New()
|
// ibcPlugin := New()
|
||||||
// ctx := types.NewCallContext(nil, nil, types.Coins{})
|
// ctx := types.NewCallContext(nil, nil, coin.Coins{})
|
||||||
|
|
||||||
// registerChain(t, ibcPlugin, store, ctx, "test_chain", testGenesisDoc)
|
// registerChain(t, ibcPlugin, store, ctx, "test_chain", testGenesisDoc)
|
||||||
// }
|
// }
|
||||||
|
@ -121,7 +121,7 @@ package ibc
|
||||||
// store.SetLogging() // Log all activity
|
// store.SetLogging() // Log all activity
|
||||||
|
|
||||||
// ibcPlugin := New()
|
// ibcPlugin := New()
|
||||||
// ctx := types.NewCallContext(nil, nil, types.Coins{})
|
// ctx := types.NewCallContext(nil, nil, coin.Coins{})
|
||||||
|
|
||||||
// chainID_1 := "test_chain"
|
// chainID_1 := "test_chain"
|
||||||
// genDoc_1, _ := genGenesisDoc(chainID_1, 4)
|
// genDoc_1, _ := genGenesisDoc(chainID_1, 4)
|
||||||
|
@ -158,7 +158,7 @@ package ibc
|
||||||
// store.SetLogging() // Log all activity
|
// store.SetLogging() // Log all activity
|
||||||
|
|
||||||
// ibcPlugin := New()
|
// ibcPlugin := New()
|
||||||
// ctx := types.NewCallContext(nil, nil, types.Coins{})
|
// ctx := types.NewCallContext(nil, nil, coin.Coins{})
|
||||||
|
|
||||||
// chainID_1 := "test_chain"
|
// chainID_1 := "test_chain"
|
||||||
// genDoc_1, _ := genGenesisDoc(chainID_1, 4)
|
// genDoc_1, _ := genGenesisDoc(chainID_1, 4)
|
||||||
|
@ -191,7 +191,7 @@ package ibc
|
||||||
// store.SetLogging() // Log all activity
|
// store.SetLogging() // Log all activity
|
||||||
|
|
||||||
// ibcPlugin := New()
|
// ibcPlugin := New()
|
||||||
// ctx := types.NewCallContext(nil, nil, types.Coins{})
|
// ctx := types.NewCallContext(nil, nil, coin.Coins{})
|
||||||
|
|
||||||
// chainID_1 := "test_chain"
|
// chainID_1 := "test_chain"
|
||||||
// genDoc_1, privAccs_1 := genGenesisDoc(chainID_1, 4)
|
// genDoc_1, privAccs_1 := genGenesisDoc(chainID_1, 4)
|
||||||
|
@ -259,8 +259,8 @@ package ibc
|
||||||
// store.SetLogging() // Log all activity
|
// store.SetLogging() // Log all activity
|
||||||
|
|
||||||
// ibcPlugin := New()
|
// ibcPlugin := New()
|
||||||
// coins := types.Coins{
|
// coins := coin.Coins{
|
||||||
// types.Coin{
|
// coin.Coin{
|
||||||
// Denom: "mycoin",
|
// Denom: "mycoin",
|
||||||
// Amount: 100,
|
// Amount: 100,
|
||||||
// },
|
// },
|
||||||
|
@ -277,8 +277,8 @@ package ibc
|
||||||
|
|
||||||
// // send coins to this addr on the other chain
|
// // send coins to this addr on the other chain
|
||||||
// destinationAddr := []byte("some address")
|
// destinationAddr := []byte("some address")
|
||||||
// coinsBad := types.Coins{types.Coin{"mycoin", 200}}
|
// coinsBad := coin.Coins{coin.Coin{"mycoin", 200}}
|
||||||
// coinsGood := types.Coins{types.Coin{"mycoin", 1}}
|
// coinsGood := coin.Coins{coin.Coin{"mycoin", 1}}
|
||||||
|
|
||||||
// // Try to send too many coins
|
// // Try to send too many coins
|
||||||
// packet := NewPacket("test_chain", "dst_chain", 0, CoinsPayload{
|
// packet := NewPacket("test_chain", "dst_chain", 0, CoinsPayload{
|
||||||
|
@ -358,7 +358,7 @@ package ibc
|
||||||
// store.SetLogging() // Log all activity
|
// store.SetLogging() // Log all activity
|
||||||
|
|
||||||
// ibcPlugin := New()
|
// ibcPlugin := New()
|
||||||
// ctx := types.NewCallContext(nil, nil, types.Coins{})
|
// ctx := types.NewCallContext(nil, nil, coin.Coins{})
|
||||||
|
|
||||||
// chainID_1 := "test_chain"
|
// chainID_1 := "test_chain"
|
||||||
// genDoc_1, privAccs_1 := genGenesisDoc(chainID_1, 4)
|
// genDoc_1, privAccs_1 := genGenesisDoc(chainID_1, 4)
|
||||||
|
@ -396,7 +396,7 @@ package ibc
|
||||||
// store.SetLogging() // Log all activity
|
// store.SetLogging() // Log all activity
|
||||||
|
|
||||||
// ibcPlugin := New()
|
// ibcPlugin := New()
|
||||||
// ctx := types.NewCallContext(nil, nil, types.Coins{})
|
// ctx := types.NewCallContext(nil, nil, coin.Coins{})
|
||||||
|
|
||||||
// chainID_1 := "test_chain"
|
// chainID_1 := "test_chain"
|
||||||
// genDoc_1, privAccs_1 := genGenesisDoc(chainID_1, 4)
|
// genDoc_1, privAccs_1 := genGenesisDoc(chainID_1, 4)
|
||||||
|
|
|
@ -52,14 +52,14 @@ func main() {}
|
||||||
// types.TxInput{
|
// types.TxInput{
|
||||||
// Address: root.Account.PubKey.Address(),
|
// Address: root.Account.PubKey.Address(),
|
||||||
// PubKey: root.Account.PubKey, // TODO is this needed?
|
// PubKey: root.Account.PubKey, // TODO is this needed?
|
||||||
// Coins: types.Coins{{"", 1000002}},
|
// Coins: coin.Coins{{"", 1000002}},
|
||||||
// Sequence: sequence,
|
// Sequence: sequence,
|
||||||
// },
|
// },
|
||||||
// },
|
// },
|
||||||
// Outputs: []types.TxOutput{
|
// Outputs: []types.TxOutput{
|
||||||
// types.TxOutput{
|
// types.TxOutput{
|
||||||
// Address: privAccount.Account.PubKey.Address(),
|
// Address: privAccount.Account.PubKey.Address(),
|
||||||
// Coins: types.Coins{{"", 1000000}},
|
// Coins: coin.Coins{{"", 1000000}},
|
||||||
// },
|
// },
|
||||||
// },
|
// },
|
||||||
// }
|
// }
|
||||||
|
@ -106,14 +106,14 @@ func main() {}
|
||||||
// types.TxInput{
|
// types.TxInput{
|
||||||
// Address: privAccountA.Account.PubKey.Address(),
|
// Address: privAccountA.Account.PubKey.Address(),
|
||||||
// PubKey: privAccountA.Account.PubKey,
|
// PubKey: privAccountA.Account.PubKey,
|
||||||
// Coins: types.Coins{{"", 3}},
|
// Coins: coin.Coins{{"", 3}},
|
||||||
// Sequence: privAccountASequence + 1,
|
// Sequence: privAccountASequence + 1,
|
||||||
// },
|
// },
|
||||||
// },
|
// },
|
||||||
// Outputs: []types.TxOutput{
|
// Outputs: []types.TxOutput{
|
||||||
// types.TxOutput{
|
// types.TxOutput{
|
||||||
// Address: privAccountB.Account.PubKey.Address(),
|
// Address: privAccountB.Account.PubKey.Address(),
|
||||||
// Coins: types.Coins{{"", 1}},
|
// Coins: coin.Coins{{"", 1}},
|
||||||
// },
|
// },
|
||||||
// },
|
// },
|
||||||
// }
|
// }
|
||||||
|
|
|
@ -28,7 +28,7 @@ package tmsp_test
|
||||||
|
|
||||||
// // Seed Basecoin with account
|
// // Seed Basecoin with account
|
||||||
// test1Acc := test1PrivAcc.Account
|
// test1Acc := test1PrivAcc.Account
|
||||||
// test1Acc.Balance = types.Coins{{"", 1000}}
|
// test1Acc.Balance = coin.Coins{{"", 1000}}
|
||||||
// accOpt, err := json.Marshal(test1Acc)
|
// accOpt, err := json.Marshal(test1Acc)
|
||||||
// require.Nil(t, err)
|
// require.Nil(t, err)
|
||||||
// bcApp.SetOption("base/account", string(accOpt))
|
// bcApp.SetOption("base/account", string(accOpt))
|
||||||
|
@ -36,14 +36,14 @@ package tmsp_test
|
||||||
// // Construct a SendTx signature
|
// // Construct a SendTx signature
|
||||||
// tx := &types.SendTx{
|
// tx := &types.SendTx{
|
||||||
// Gas: 0,
|
// Gas: 0,
|
||||||
// Fee: types.Coin{"", 0},
|
// Fee: coin.Coin{"", 0},
|
||||||
// Inputs: []types.TxInput{
|
// Inputs: []types.TxInput{
|
||||||
// types.NewTxInput(test1PrivAcc.Account.PubKey, types.Coins{{"", 1}}, 1),
|
// types.NewTxInput(test1PrivAcc.Account.PubKey, coin.Coins{{"", 1}}, 1),
|
||||||
// },
|
// },
|
||||||
// Outputs: []types.TxOutput{
|
// Outputs: []types.TxOutput{
|
||||||
// types.TxOutput{
|
// types.TxOutput{
|
||||||
// Address: test2PrivAcc.Account.PubKey.Address(),
|
// Address: test2PrivAcc.Account.PubKey.Address(),
|
||||||
// Coins: types.Coins{{"", 1}},
|
// Coins: coin.Coins{{"", 1}},
|
||||||
// },
|
// },
|
||||||
// },
|
// },
|
||||||
// }
|
// }
|
||||||
|
@ -72,7 +72,7 @@ package tmsp_test
|
||||||
// // Get the test account
|
// // Get the test account
|
||||||
// test1PrivAcc := types.PrivAccountFromSecret("test1")
|
// test1PrivAcc := types.PrivAccountFromSecret("test1")
|
||||||
// test1Acc := test1PrivAcc.Account
|
// test1Acc := test1PrivAcc.Account
|
||||||
// test1Acc.Balance = types.Coins{{"", 1 << 53}}
|
// test1Acc.Balance = coin.Coins{{"", 1 << 53}}
|
||||||
// accOpt, err := json.Marshal(test1Acc)
|
// accOpt, err := json.Marshal(test1Acc)
|
||||||
// require.Nil(t, err)
|
// require.Nil(t, err)
|
||||||
// bcApp.SetOption("base/account", string(accOpt))
|
// bcApp.SetOption("base/account", string(accOpt))
|
||||||
|
@ -88,14 +88,14 @@ package tmsp_test
|
||||||
|
|
||||||
// tx := &types.SendTx{
|
// tx := &types.SendTx{
|
||||||
// Gas: 2,
|
// Gas: 2,
|
||||||
// Fee: types.Coin{"", 2},
|
// Fee: coin.Coin{"", 2},
|
||||||
// Inputs: []types.TxInput{
|
// Inputs: []types.TxInput{
|
||||||
// types.NewTxInput(test1Acc.PubKey, types.Coins{{"", 1000002}}, sequence),
|
// types.NewTxInput(test1Acc.PubKey, coin.Coins{{"", 1000002}}, sequence),
|
||||||
// },
|
// },
|
||||||
// Outputs: []types.TxOutput{
|
// Outputs: []types.TxOutput{
|
||||||
// types.TxOutput{
|
// types.TxOutput{
|
||||||
// Address: privAccount.Account.PubKey.Address(),
|
// Address: privAccount.Account.PubKey.Address(),
|
||||||
// Coins: types.Coins{{"", 1000000}},
|
// Coins: coin.Coins{{"", 1000000}},
|
||||||
// },
|
// },
|
||||||
// },
|
// },
|
||||||
// }
|
// }
|
||||||
|
@ -133,14 +133,14 @@ package tmsp_test
|
||||||
|
|
||||||
// tx := &types.SendTx{
|
// tx := &types.SendTx{
|
||||||
// Gas: 2,
|
// Gas: 2,
|
||||||
// Fee: types.Coin{"", 2},
|
// Fee: coin.Coin{"", 2},
|
||||||
// Inputs: []types.TxInput{
|
// Inputs: []types.TxInput{
|
||||||
// types.NewTxInput(privAccountA.PubKey, types.Coins{{"", 3}}, privAccountASequence+1),
|
// types.NewTxInput(privAccountA.PubKey, coin.Coins{{"", 3}}, privAccountASequence+1),
|
||||||
// },
|
// },
|
||||||
// Outputs: []types.TxOutput{
|
// Outputs: []types.TxOutput{
|
||||||
// types.TxOutput{
|
// types.TxOutput{
|
||||||
// Address: privAccountB.PubKey.Address(),
|
// Address: privAccountB.PubKey.Address(),
|
||||||
// Coins: types.Coins{{"", 1}},
|
// Coins: coin.Coins{{"", 1}},
|
||||||
// },
|
// },
|
||||||
// },
|
// },
|
||||||
// }
|
// }
|
||||||
|
|
Loading…
Reference in New Issue