diff --git a/app/app_test.go b/app/app_test.go index 840dfce79..5d04d22db 100644 --- a/app/app_test.go +++ b/app/app_test.go @@ -14,7 +14,6 @@ import ( "github.com/tendermint/basecoin/stack" "github.com/tendermint/basecoin/state" "github.com/tendermint/basecoin/txs" - "github.com/tendermint/basecoin/types" wire "github.com/tendermint/go-wire" eyes "github.com/tendermint/merkleeyes/client" "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 -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}} out := []coin.TxOutput{{Address: at.acctOut.Actor(), Coins: coins}} 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 func (at *appTest) reset() { - at.acctIn = coin.NewAccountWithKey(types.Coins{{"mycoin", 7}}) - at.acctOut = coin.NewAccountWithKey(types.Coins{{"mycoin", 7}}) + at.acctIn = coin.NewAccountWithKey(coin.Coins{{"mycoin", 7}}) + at.acctOut = coin.NewAccountWithKey(coin.Coins{{"mycoin", 7}}) eyesCli := eyes.NewLocalClient("", 0) // logger := log.TestingLogger().With("module", "app"), @@ -82,18 +81,18 @@ func (at *appTest) reset() { 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) 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) return getBalance(actor, state) } // 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) initBalIn, err := getBalance(at.acctIn.Actor(), at.app.GetState()) @@ -135,7 +134,7 @@ func TestSetOption(t *testing.T) { assert.EqualValues(res, "Success") // make a nice account... - bal := types.Coins{{"atom", 77}, {"eth", 12}} + bal := coin.Coins{{"atom", 77}, {"eth", 12}} acct := coin.NewAccountWithKey(bal) res = app.SetOption("coin/account", acct.MakeOption()) require.EqualValues(res, "Success") @@ -148,7 +147,7 @@ func TestSetOption(t *testing.T) { // let's parse an account with badly sorted coins... unsortAddr, err := hex.DecodeString("C471FB670E44D219EE6DF2FC284BE38793ACBCE1") require.Nil(err) - unsortCoins := types.Coins{{"BTC", 789}, {"eth", 123}} + unsortCoins := coin.Coins{{"BTC", 789}, {"eth", 123}} unsortAcc := `{ "pub_key": { "type": "ed25519", @@ -190,23 +189,23 @@ func TestTx(t *testing.T) { at := newAppTest(t) //Bad Balance - at.acctIn.Coins = types.Coins{{"mycoin", 2}} + at.acctIn.Coins = coin.Coins{{"mycoin", 2}} 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) - 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(diffIn.IsZero()) assert.True(diffOut.IsZero()) //Regular CheckTx 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) //Regular DeliverTx at.reset() - amt := types.Coins{{"mycoin", 3}} + amt := coin.Coins{{"mycoin", 3}} 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.Equal(amt.Negative(), diffIn) @@ -217,7 +216,7 @@ func TestQuery(t *testing.T) { assert := assert.New(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) resQueryPreCommit := at.app.Query(abci.RequestQuery{ diff --git a/app/genesis_test.go b/app/genesis_test.go index 74947f779..1d99b38eb 100644 --- a/app/genesis_test.go +++ b/app/genesis_test.go @@ -7,7 +7,7 @@ import ( "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" - "github.com/tendermint/basecoin/types" + "github.com/tendermint/basecoin/modules/coin" eyescli "github.com/tendermint/merkleeyes/client" cmn "github.com/tendermint/tmlibs/common" "github.com/tendermint/tmlibs/log" @@ -68,17 +68,17 @@ func TestLoadGenesisAccountAddress(t *testing.T) { addr string exists bool hasPubkey bool - coins types.Coins + coins coin.Coins }{ // 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) - {"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) {"1234ABCDD18E8EFE3FFC4B0506BF9BF8E5B0D9E9", false, false, nil}, // this is given addr {"700BEC5ED18E8EFE3FFC4B0506BF9BF8E5B0D9E9", false, false, nil}, // this is addr of the given pubkey // 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 { diff --git a/cmd/basecli/commands/cmds.go b/cmd/basecli/commands/cmds.go index 5f64fc1a3..b877b7dbd 100644 --- a/cmd/basecli/commands/cmds.go +++ b/cmd/basecli/commands/cmds.go @@ -16,7 +16,6 @@ import ( "github.com/tendermint/basecoin/modules/coin" "github.com/tendermint/basecoin/stack" "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 // tx.Gas = viper.GetInt64(FlagGas) - amountCoins, err := btypes.ParseCoins(viper.GetString(FlagAmount)) + amountCoins, err := coin.ParseCoins(viper.GetString(FlagAmount)) if err != nil { return tx, err } diff --git a/cmd/basecoin/commands/relay.go b/cmd/basecoin/commands/relay.go index 13bdeb9dc..a177acec8 100644 --- a/cmd/basecoin/commands/relay.go +++ b/cmd/basecoin/commands/relay.go @@ -253,9 +253,9 @@ package commands // ibc.IBCTx `json:"unwrap"` // }{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{ // Gas: 0, // Fee: smallCoins, diff --git a/docs/guide/counter/cmd/countercli/commands/counter.go b/docs/guide/counter/cmd/countercli/commands/counter.go index 81d95e4b4..c1e5662bd 100644 --- a/docs/guide/counter/cmd/countercli/commands/counter.go +++ b/docs/guide/counter/cmd/countercli/commands/counter.go @@ -9,8 +9,8 @@ import ( txcmd "github.com/tendermint/light-client/commands/txs" "github.com/tendermint/basecoin/docs/guide/counter/plugins/counter" + "github.com/tendermint/basecoin/modules/coin" "github.com/tendermint/basecoin/txs" - btypes "github.com/tendermint/basecoin/types" ) //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) { - feeCoins, err := btypes.ParseCoins(viper.GetString(FlagCountFee)) + feeCoins, err := coin.ParseCoins(viper.GetString(FlagCountFee)) if err != nil { return tx, err } diff --git a/docs/guide/counter/plugins/counter/counter.go b/docs/guide/counter/plugins/counter/counter.go index 355594c43..dffa4a610 100644 --- a/docs/guide/counter/plugins/counter/counter.go +++ b/docs/guide/counter/plugins/counter/counter.go @@ -11,7 +11,6 @@ import ( "github.com/tendermint/basecoin/modules/coin" "github.com/tendermint/basecoin/stack" "github.com/tendermint/basecoin/state" - "github.com/tendermint/basecoin/types" ) // Tx @@ -32,13 +31,13 @@ func init() { // Tx - struct for all counter transactions type Tx struct { - Valid bool `json:"valid"` - Fee types.Coins `json:"fee"` - Sequence int `json:"sequence"` + Valid bool `json:"valid"` + Fee coin.Coins `json:"fee"` + Sequence int `json:"sequence"` } // 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{ Valid: valid, Fee: fee, @@ -183,8 +182,8 @@ func StoreActor() basecoin.Actor { // State - state of the counter applicaton type State struct { - Counter int `json:"counter"` - TotalFees types.Coins `json:"total_fees"` + Counter int `json:"counter"` + TotalFees coin.Coins `json:"total_fees"` } // StateKey - store key for the counter state diff --git a/docs/guide/counter/plugins/counter/counter_test.go b/docs/guide/counter/plugins/counter/counter_test.go index f839002a1..6be6e8ca2 100644 --- a/docs/guide/counter/plugins/counter/counter_test.go +++ b/docs/guide/counter/plugins/counter/counter_test.go @@ -10,7 +10,6 @@ import ( "github.com/tendermint/basecoin/app" "github.com/tendermint/basecoin/modules/coin" "github.com/tendermint/basecoin/txs" - "github.com/tendermint/basecoin/types" "github.com/tendermint/go-wire" eyescli "github.com/tendermint/merkleeyes/client" "github.com/tendermint/tmlibs/log" @@ -34,13 +33,13 @@ func TestCounterPlugin(t *testing.T) { bcApp.SetOption("base/chain_id", chainID) // Account initialization - bal := types.Coins{{"", 1000}, {"gold", 1000}} + bal := coin.Coins{{"", 1000}, {"gold", 1000}} acct := coin.NewAccountWithKey(bal) log := bcApp.SetOption("coin/account", acct.MakeOption()) require.Equal(t, "Success", log) // 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 = txs.NewChain(chainID, tx) stx := txs.NewSig(tx) @@ -58,10 +57,10 @@ func TestCounterPlugin(t *testing.T) { assert.True(res.IsErr(), res.String()) // 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()) // 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()) } diff --git a/types/coin.go b/modules/coin/coin.go similarity index 71% rename from types/coin.go rename to modules/coin/coin.go index 7460a8e8a..c7f1a1856 100644 --- a/types/coin.go +++ b/modules/coin/coin.go @@ -1,4 +1,4 @@ -package types +package coin import ( "fmt" @@ -10,6 +10,7 @@ import ( "github.com/pkg/errors" ) +// Coin hold some amount of one currency type Coin struct { Denom string `json:"denom"` Amount int64 `json:"amount"` @@ -25,6 +26,8 @@ var reAmt = regexp.MustCompile("(\\d+)") 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) { 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 func (coins Coins) String() string { @@ -59,6 +63,8 @@ func (coins Coins) String() string { 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) { // empty string is empty list... if len(str) == 0 { @@ -85,7 +91,7 @@ func ParseCoins(str string) (Coins, error) { 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 { switch len(coins) { case 0: @@ -108,6 +114,8 @@ func (coins Coins) IsValid() bool { } } +// Plus combines to sets of coins +// // TODO: handle empty coins! // Currently appends an empty coin ... func (coinsA Coins) Plus(coinsB Coins) Coins { @@ -118,9 +126,8 @@ func (coinsA Coins) Plus(coinsB Coins) Coins { if indexA == lenA { if indexB == lenB { return sum - } else { - return append(sum, coinsB[indexB:]...) } + return append(sum, coinsB[indexB:]...) } else if indexB == lenB { return append(sum, coinsA[indexA:]...) } @@ -128,7 +135,7 @@ func (coinsA Coins) Plus(coinsB Coins) Coins { switch strings.Compare(coinA.Denom, coinB.Denom) { case -1: sum = append(sum, coinA) - indexA += 1 + indexA++ case 0: if coinA.Amount+coinB.Amount == 0 { // ignore 0 sum coin type @@ -138,16 +145,17 @@ func (coinsA Coins) Plus(coinsB Coins) Coins { Amount: coinA.Amount + coinB.Amount, }) } - indexA += 1 - indexB += 1 + indexA++ + indexB++ case 1: sum = append(sum, coinB) - indexB += 1 + indexB++ } } return sum } +// Negative returns a set of coins with all amount negative func (coins Coins) Negative() Coins { res := make([]Coin, 0, len(coins)) for _, coin := range coins { @@ -159,10 +167,14 @@ func (coins Coins) Negative() Coins { return res } +// Minus subtracts a set of coins from another (adds the inverse) func (coinsA Coins) Minus(coinsB Coins) Coins { 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 { diff := coinsA.Minus(coinsB) if len(diff) == 0 { @@ -171,10 +183,12 @@ func (coinsA Coins) IsGTE(coinsB Coins) bool { return diff.IsNonnegative() } +// IsZero returns true if there are no coins func (coins Coins) IsZero() bool { return len(coins) == 0 } +// IsEqual returns true if the two sets of Coins have the same value func (coinsA Coins) IsEqual(coinsB Coins) bool { if len(coinsA) != len(coinsB) { return false @@ -187,6 +201,8 @@ func (coinsA Coins) IsEqual(coinsB Coins) bool { return true } +// IsPositive returns true if there is at least one coin, and all +// currencies have a positive value func (coins Coins) IsPositive() bool { if len(coins) == 0 { return false @@ -199,6 +215,8 @@ func (coins Coins) IsPositive() bool { 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 { if len(coins) == 0 { return true @@ -213,7 +231,12 @@ func (coins Coins) IsNonnegative() bool { /*** Implement Sort interface ***/ -func (c Coins) Len() int { return len(c) } -func (c Coins) Less(i, j int) bool { return c[i].Denom < c[j].Denom } -func (c Coins) Swap(i, j int) { c[i], c[j] = c[j], c[i] } -func (c Coins) Sort() { sort.Sort(c) } +//nolint +func (coins Coins) Len() int { return len(coins) } +func (coins Coins) Less(i, j int) bool { return coins[i].Denom < coins[j].Denom } +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) } diff --git a/types/coin_test.go b/modules/coin/coin_test.go similarity index 99% rename from types/coin_test.go rename to modules/coin/coin_test.go index bf651c50d..0cb2b4f21 100644 --- a/types/coin_test.go +++ b/modules/coin/coin_test.go @@ -1,4 +1,4 @@ -package types +package coin import ( "testing" diff --git a/modules/coin/genesis.go b/modules/coin/genesis.go index 4b26fa1e6..06bcd9c1c 100644 --- a/modules/coin/genesis.go +++ b/modules/coin/genesis.go @@ -7,8 +7,6 @@ import ( crypto "github.com/tendermint/go-crypto" "github.com/tendermint/go-wire/data" - - "github.com/tendermint/basecoin/types" ) /**** 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) PubKey crypto.PubKey `json:"pub_key"` // May be nil, if not known. Sequence int `json:"sequence"` - Balance types.Coins `json:"coins"` + Balance Coins `json:"coins"` } // ToAccount - GenesisAccount struct to a basecoin Account diff --git a/modules/coin/handler_test.go b/modules/coin/handler_test.go index 38a9fa2cf..48eb91ff3 100644 --- a/modules/coin/handler_test.go +++ b/modules/coin/handler_test.go @@ -13,7 +13,6 @@ import ( "github.com/tendermint/basecoin" "github.com/tendermint/basecoin/stack" "github.com/tendermint/basecoin/state" - "github.com/tendermint/basecoin/types" ) // 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 addr1 := basecoin.Actor{App: "coin", Address: []byte{1, 2}} addr2 := basecoin.Actor{App: "role", Address: []byte{7, 8}} - someCoins := types.Coins{{"atom", 123}} - doubleCoins := types.Coins{{"atom", 246}} - minusCoins := types.Coins{{"eth", -34}} + someCoins := Coins{{"atom", 123}} + doubleCoins := Coins{{"atom", 246}} + minusCoins := Coins{{"eth", -34}} cases := []struct { valid bool @@ -93,15 +92,15 @@ func TestDeliverTx(t *testing.T) { addr2 := basecoin.Actor{App: "role", Address: []byte{7, 8}} addr3 := basecoin.Actor{App: "coin", Address: []byte{6, 5, 4, 3}} - someCoins := types.Coins{{"atom", 123}} - moreCoins := types.Coins{{"atom", 6487}} + someCoins := Coins{{"atom", 123}} + moreCoins := Coins{{"atom", 6487}} diffCoins := moreCoins.Minus(someCoins) - otherCoins := types.Coins{{"eth", 11}} + otherCoins := Coins{{"eth", 11}} mixedCoins := someCoins.Plus(otherCoins) type money struct { addr basecoin.Actor - coins types.Coins + coins Coins } cases := []struct { @@ -175,13 +174,13 @@ func TestSetOption(t *testing.T) { addr := pk.PubKey().Address() actor := basecoin.Actor{App: stack.NameSigs, Address: addr} - someCoins := types.Coins{{"atom", 123}} - otherCoins := types.Coins{{"eth", 11}} + someCoins := Coins{{"atom", 123}} + otherCoins := Coins{{"eth", 11}} mixedCoins := someCoins.Plus(otherCoins) type money struct { addr basecoin.Actor - coins types.Coins + coins Coins } cases := []struct { diff --git a/modules/coin/helper.go b/modules/coin/helper.go index 684affa36..d15333982 100644 --- a/modules/coin/helper.go +++ b/modules/coin/helper.go @@ -1,11 +1,11 @@ package coin import ( - "github.com/tendermint/basecoin" - "github.com/tendermint/basecoin/stack" - "github.com/tendermint/basecoin/types" crypto "github.com/tendermint/go-crypto" "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 @@ -17,7 +17,7 @@ type AccountWithKey struct { // NewAccountWithKey creates an account with the given balance // and a random private key -func NewAccountWithKey(coins types.Coins) *AccountWithKey { +func NewAccountWithKey(coins Coins) *AccountWithKey { return &AccountWithKey{ Key: crypto.GenPrivKeyEd25519().Wrap(), Account: Account{Coins: coins}, diff --git a/modules/coin/store.go b/modules/coin/store.go index e779fa4db..01032cf1d 100644 --- a/modules/coin/store.go +++ b/modules/coin/store.go @@ -8,7 +8,6 @@ import ( "github.com/tendermint/basecoin" "github.com/tendermint/basecoin/errors" "github.com/tendermint/basecoin/state" - "github.com/tendermint/basecoin/types" ) // 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 -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) return acct.Coins, err } // 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) if err != nil { 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. // // 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)) // we can increase an empty account... if IsNoAccountErr(err) && coins.IsPositive() { @@ -98,8 +97,8 @@ func (a Accountant) MakeKey(addr basecoin.Actor) []byte { // Account - coin account structure type Account struct { - Coins types.Coins `json:"coins"` - Sequence int `json:"sequence"` + Coins Coins `json:"coins"` + Sequence int `json:"sequence"` } func loadAccount(store state.KVStore, key []byte) (acct Account, err error) { diff --git a/modules/coin/tx.go b/modules/coin/tx.go index c6b9c66a7..147f0ec84 100644 --- a/modules/coin/tx.go +++ b/modules/coin/tx.go @@ -4,7 +4,6 @@ import ( "fmt" "github.com/tendermint/basecoin" - "github.com/tendermint/basecoin/types" ) func init() { @@ -22,7 +21,7 @@ const ( // TxInput - expected coin movement outputs, used with SendTx type TxInput struct { 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 } @@ -52,7 +51,7 @@ func (txIn TxInput) String() string { } // 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{ Address: addr, 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 type TxOutput struct { Address basecoin.Actor `json:"address"` - Coins types.Coins `json:"coins"` + Coins Coins `json:"coins"` } // ValidateBasic - validate transaction output @@ -92,7 +91,7 @@ func (txOut TxOutput) String() string { } // 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{ Address: addr, Coins: coins, @@ -127,7 +126,7 @@ func (tx SendTx) ValidateBasic() error { return ErrNoOutputs() } // make sure all inputs and outputs are individually valid - var totalIn, totalOut types.Coins + var totalIn, totalOut Coins for _, in := range tx.Inputs { if err := in.ValidateBasic(); err != nil { return err diff --git a/modules/coin/tx_test.go b/modules/coin/tx_test.go index 1fb9bf0b4..9aa0699ed 100644 --- a/modules/coin/tx_test.go +++ b/modules/coin/tx_test.go @@ -5,9 +5,10 @@ import ( "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" - "github.com/tendermint/basecoin" - "github.com/tendermint/basecoin/types" + "github.com/tendermint/go-wire/data" + + "github.com/tendermint/basecoin" ) // these are some constructs for the test cases @@ -24,22 +25,22 @@ var actors = []struct { } var ( - zeroCoin = types.Coin{"zeros", 0} - plusCoin = types.Coin{"plus", 23} - negCoin = types.Coin{"neg", -42} + zeroCoin = Coin{"zeros", 0} + plusCoin = Coin{"plus", 23} + negCoin = Coin{"neg", -42} ) var coins = []struct { - coins types.Coins + coins Coins valid bool }{ - {types.Coins{}, false}, - {types.Coins{zeroCoin}, false}, - {types.Coins{plusCoin}, true}, - {types.Coins{negCoin}, false}, - {types.Coins{plusCoin, plusCoin}, false}, - {types.Coins{plusCoin, zeroCoin}, false}, - {types.Coins{negCoin, plusCoin}, false}, + {Coins{}, false}, + {Coins{zeroCoin}, false}, + {Coins{plusCoin}, true}, + {Coins{negCoin}, false}, + {Coins{plusCoin, plusCoin}, false}, + {Coins{plusCoin, zeroCoin}, false}, + {Coins{negCoin, plusCoin}, false}, } func TestTxValidateInput(t *testing.T) { @@ -94,12 +95,12 @@ func TestTxValidateTx(t *testing.T) { addr3 := basecoin.Actor{App: "role", Address: []byte{7, 8}} noAddr := basecoin.Actor{} - noCoins := types.Coins{} - someCoins := types.Coins{{"atom", 123}} - moreCoins := types.Coins{{"atom", 124}} - otherCoins := types.Coins{{"btc", 15}} + noCoins := Coins{} + someCoins := Coins{{"atom", 123}} + moreCoins := Coins{{"atom", 124}} + otherCoins := Coins{{"btc", 15}} bothCoins := someCoins.Plus(otherCoins) - minusCoins := types.Coins{{"eth", -34}} + minusCoins := Coins{{"eth", -34}} // cases: all valid (one), all valid (multi) // 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}} addr2 := basecoin.Actor{App: "coin", Address: []byte{3, 4}} - someCoins := types.Coins{{"atom", 123}} + someCoins := Coins{{"atom", 123}} send := NewSendTx( []TxInput{NewTxInput(addr1, someCoins, 2)}, diff --git a/modules/fee/handler.go b/modules/fee/handler.go index 25fb4f2b0..d202ebab4 100644 --- a/modules/fee/handler.go +++ b/modules/fee/handler.go @@ -3,9 +3,9 @@ package fee import ( "github.com/tendermint/basecoin" "github.com/tendermint/basecoin/errors" + "github.com/tendermint/basecoin/modules/coin" "github.com/tendermint/basecoin/stack" "github.com/tendermint/basecoin/state" - "github.com/tendermint/basecoin/types" ) // NameFee - namespace for the fee module @@ -14,17 +14,17 @@ const NameFee = "fee" // AccountChecker - interface used by SimpleFeeHandler type AccountChecker interface { // 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 // 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 type SimpleFeeHandler struct { AccountChecker - MinFee types.Coins + MinFee coin.Coins stack.PassOption } @@ -44,7 +44,7 @@ func (h SimpleFeeHandler) CheckTx(ctx basecoin.Context, store state.KVStore, tx return res, errors.ErrInvalidFormat(tx) } - fees := types.Coins{feeTx.Fee} + fees := coin.Coins{feeTx.Fee} if !fees.IsGTE(h.MinFee) { return res, ErrInsufficientFees() } @@ -68,7 +68,7 @@ func (h SimpleFeeHandler) DeliverTx(ctx basecoin.Context, store state.KVStore, t return res, errors.ErrInvalidFormat(tx) } - fees := types.Coins{feeTx.Fee} + fees := coin.Coins{feeTx.Fee} if !fees.IsGTE(h.MinFee) { return res, ErrInsufficientFees() } diff --git a/modules/fee/tx.go b/modules/fee/tx.go index 8f51c48e7..13aac2209 100644 --- a/modules/fee/tx.go +++ b/modules/fee/tx.go @@ -2,9 +2,10 @@ package fee import ( "github.com/tendermint/basecoin" - "github.com/tendermint/basecoin/types" + "github.com/tendermint/basecoin/modules/coin" ) +// nolint const ( ByteFees = 0x20 TypeFees = "fee" @@ -20,12 +21,12 @@ func init() { // Fee attaches a fee payment to the embedded tx type Fee struct { 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 - // 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() } diff --git a/plugins/ibc/ibc.go b/plugins/ibc/ibc.go index 3084ef7ca..822638584 100644 --- a/plugins/ibc/ibc.go +++ b/plugins/ibc/ibc.go @@ -147,7 +147,7 @@ package ibc // type CoinsPayload struct { // Address []byte -// Coins types.Coins +// Coins coin.Coins // } // func (p CoinsPayload) Type() string { diff --git a/plugins/ibc/ibc_test.go b/plugins/ibc/ibc_test.go index ffd464843..2950633c5 100644 --- a/plugins/ibc/ibc_test.go +++ b/plugins/ibc/ibc_test.go @@ -106,7 +106,7 @@ package ibc // store.SetLogging() // Log all activity // ibcPlugin := New() -// ctx := types.NewCallContext(nil, nil, types.Coins{}) +// ctx := types.NewCallContext(nil, nil, coin.Coins{}) // registerChain(t, ibcPlugin, store, ctx, "test_chain", testGenesisDoc) // } @@ -121,7 +121,7 @@ package ibc // store.SetLogging() // Log all activity // ibcPlugin := New() -// ctx := types.NewCallContext(nil, nil, types.Coins{}) +// ctx := types.NewCallContext(nil, nil, coin.Coins{}) // chainID_1 := "test_chain" // genDoc_1, _ := genGenesisDoc(chainID_1, 4) @@ -158,7 +158,7 @@ package ibc // store.SetLogging() // Log all activity // ibcPlugin := New() -// ctx := types.NewCallContext(nil, nil, types.Coins{}) +// ctx := types.NewCallContext(nil, nil, coin.Coins{}) // chainID_1 := "test_chain" // genDoc_1, _ := genGenesisDoc(chainID_1, 4) @@ -191,7 +191,7 @@ package ibc // store.SetLogging() // Log all activity // ibcPlugin := New() -// ctx := types.NewCallContext(nil, nil, types.Coins{}) +// ctx := types.NewCallContext(nil, nil, coin.Coins{}) // chainID_1 := "test_chain" // genDoc_1, privAccs_1 := genGenesisDoc(chainID_1, 4) @@ -259,8 +259,8 @@ package ibc // store.SetLogging() // Log all activity // ibcPlugin := New() -// coins := types.Coins{ -// types.Coin{ +// coins := coin.Coins{ +// coin.Coin{ // Denom: "mycoin", // Amount: 100, // }, @@ -277,8 +277,8 @@ package ibc // // send coins to this addr on the other chain // destinationAddr := []byte("some address") -// coinsBad := types.Coins{types.Coin{"mycoin", 200}} -// coinsGood := types.Coins{types.Coin{"mycoin", 1}} +// coinsBad := coin.Coins{coin.Coin{"mycoin", 200}} +// coinsGood := coin.Coins{coin.Coin{"mycoin", 1}} // // Try to send too many coins // packet := NewPacket("test_chain", "dst_chain", 0, CoinsPayload{ @@ -358,7 +358,7 @@ package ibc // store.SetLogging() // Log all activity // ibcPlugin := New() -// ctx := types.NewCallContext(nil, nil, types.Coins{}) +// ctx := types.NewCallContext(nil, nil, coin.Coins{}) // chainID_1 := "test_chain" // genDoc_1, privAccs_1 := genGenesisDoc(chainID_1, 4) @@ -396,7 +396,7 @@ package ibc // store.SetLogging() // Log all activity // ibcPlugin := New() -// ctx := types.NewCallContext(nil, nil, types.Coins{}) +// ctx := types.NewCallContext(nil, nil, coin.Coins{}) // chainID_1 := "test_chain" // genDoc_1, privAccs_1 := genGenesisDoc(chainID_1, 4) diff --git a/tests/tendermint/main.go b/tests/tendermint/main.go index fbe4c2077..32ca659e5 100644 --- a/tests/tendermint/main.go +++ b/tests/tendermint/main.go @@ -52,14 +52,14 @@ func main() {} // types.TxInput{ // Address: root.Account.PubKey.Address(), // PubKey: root.Account.PubKey, // TODO is this needed? -// Coins: types.Coins{{"", 1000002}}, +// Coins: coin.Coins{{"", 1000002}}, // Sequence: sequence, // }, // }, // Outputs: []types.TxOutput{ // types.TxOutput{ // Address: privAccount.Account.PubKey.Address(), -// Coins: types.Coins{{"", 1000000}}, +// Coins: coin.Coins{{"", 1000000}}, // }, // }, // } @@ -106,14 +106,14 @@ func main() {} // types.TxInput{ // Address: privAccountA.Account.PubKey.Address(), // PubKey: privAccountA.Account.PubKey, -// Coins: types.Coins{{"", 3}}, +// Coins: coin.Coins{{"", 3}}, // Sequence: privAccountASequence + 1, // }, // }, // Outputs: []types.TxOutput{ // types.TxOutput{ // Address: privAccountB.Account.PubKey.Address(), -// Coins: types.Coins{{"", 1}}, +// Coins: coin.Coins{{"", 1}}, // }, // }, // } diff --git a/tests/tmsp/tmsp_test.go b/tests/tmsp/tmsp_test.go index f08b62600..5dca8e07b 100644 --- a/tests/tmsp/tmsp_test.go +++ b/tests/tmsp/tmsp_test.go @@ -28,7 +28,7 @@ package tmsp_test // // Seed Basecoin with account // test1Acc := test1PrivAcc.Account -// test1Acc.Balance = types.Coins{{"", 1000}} +// test1Acc.Balance = coin.Coins{{"", 1000}} // accOpt, err := json.Marshal(test1Acc) // require.Nil(t, err) // bcApp.SetOption("base/account", string(accOpt)) @@ -36,14 +36,14 @@ package tmsp_test // // Construct a SendTx signature // tx := &types.SendTx{ // Gas: 0, -// Fee: types.Coin{"", 0}, +// Fee: coin.Coin{"", 0}, // Inputs: []types.TxInput{ -// types.NewTxInput(test1PrivAcc.Account.PubKey, types.Coins{{"", 1}}, 1), +// types.NewTxInput(test1PrivAcc.Account.PubKey, coin.Coins{{"", 1}}, 1), // }, // Outputs: []types.TxOutput{ // types.TxOutput{ // Address: test2PrivAcc.Account.PubKey.Address(), -// Coins: types.Coins{{"", 1}}, +// Coins: coin.Coins{{"", 1}}, // }, // }, // } @@ -72,7 +72,7 @@ package tmsp_test // // Get the test account // test1PrivAcc := types.PrivAccountFromSecret("test1") // test1Acc := test1PrivAcc.Account -// test1Acc.Balance = types.Coins{{"", 1 << 53}} +// test1Acc.Balance = coin.Coins{{"", 1 << 53}} // accOpt, err := json.Marshal(test1Acc) // require.Nil(t, err) // bcApp.SetOption("base/account", string(accOpt)) @@ -88,14 +88,14 @@ package tmsp_test // tx := &types.SendTx{ // Gas: 2, -// Fee: types.Coin{"", 2}, +// Fee: coin.Coin{"", 2}, // Inputs: []types.TxInput{ -// types.NewTxInput(test1Acc.PubKey, types.Coins{{"", 1000002}}, sequence), +// types.NewTxInput(test1Acc.PubKey, coin.Coins{{"", 1000002}}, sequence), // }, // Outputs: []types.TxOutput{ // types.TxOutput{ // Address: privAccount.Account.PubKey.Address(), -// Coins: types.Coins{{"", 1000000}}, +// Coins: coin.Coins{{"", 1000000}}, // }, // }, // } @@ -133,14 +133,14 @@ package tmsp_test // tx := &types.SendTx{ // Gas: 2, -// Fee: types.Coin{"", 2}, +// Fee: coin.Coin{"", 2}, // Inputs: []types.TxInput{ -// types.NewTxInput(privAccountA.PubKey, types.Coins{{"", 3}}, privAccountASequence+1), +// types.NewTxInput(privAccountA.PubKey, coin.Coins{{"", 3}}, privAccountASequence+1), // }, // Outputs: []types.TxOutput{ // types.TxOutput{ // Address: privAccountB.PubKey.Address(), -// Coins: types.Coins{{"", 1}}, +// Coins: coin.Coins{{"", 1}}, // }, // }, // }