cosmos-sdk/types/coin.go

832 lines
21 KiB
Go
Raw Normal View History

2018-01-10 20:11:44 -08:00
package types
2016-04-01 15:19:07 -07:00
import (
"encoding/json"
2016-04-01 15:19:07 -07:00
"fmt"
2018-01-06 12:53:31 -08:00
"regexp"
"sort"
2016-04-01 15:19:07 -07:00
"strings"
)
//-----------------------------------------------------------------------------
// Coin
// NewCoin returns a new coin with a denomination and amount. It will panic if
// the amount is negative or if the denomination is invalid.
2018-07-30 17:09:50 -07:00
func NewCoin(denom string, amount Int) Coin {
coin := Coin{
Denom: denom,
2018-07-10 17:36:50 -07:00
Amount: amount,
}
if err := coin.Validate(); err != nil {
panic(err)
}
return coin
2016-04-01 15:19:07 -07:00
}
// NewInt64Coin returns a new coin with a denomination and amount. It will panic
// if the amount is negative.
2018-07-30 17:09:50 -07:00
func NewInt64Coin(denom string, amount int64) Coin {
return NewCoin(denom, NewInt(amount))
}
2017-07-12 09:54:07 -07:00
// String provides a human-readable representation of a coin
2016-04-01 15:19:07 -07:00
func (coin Coin) String() string {
perf: Improve the speed of coins.String() (backport #10076) (#10850) * perf: Improve the speed of coins.String() (#10076) <!-- The default pull request template is for types feat, fix, or refactor. For other templates, add one of the following parameters to the url: - template=docs.md - template=other.md --> ## Description Speedup coins.String() and coin.String() In my local benchmarks, this >2x improves the time for balances with 1 coin, and further improves speed for strings with many balances. I did not benchmark on the two coin usecase --- ### Author Checklist *All items are required. Please add a note to the item if the item is not applicable and please add links to any relevant follow up issues.* I have... - [x] included the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title - [x] added `!` to the type prefix if API or client breaking change - [x] targeted the correct branch (see [PR Targeting](https://github.com/cosmos/cosmos-sdk/blob/master/CONTRIBUTING.md#pr-targeting)) - [x] provided a link to the relevant issue or specification - n/a - [x] followed the guidelines for [building modules](https://github.com/cosmos/cosmos-sdk/blob/master/docs/building-modules) - [x] included the necessary unit and integration [tests](https://github.com/cosmos/cosmos-sdk/blob/master/CONTRIBUTING.md#testing) - covered by existing - [ ] added a changelog entry to `CHANGELOG.md` - [x] included comments for [documenting Go code](https://blog.golang.org/godoc) - seems sufficiently clear - [x] updated the relevant documentation or specification - [x] reviewed "Files changed" and left comments if necessary - [ ] confirmed all CI checks have passed ### Reviewers Checklist *All items are required. Please add a note if the item is not applicable and please add your handle next to the items reviewed if you only reviewed selected items.* I have... - [ ] confirmed the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title - [ ] confirmed `!` in the type prefix if API or client breaking change - [ ] confirmed all author checklist items have been addressed - [ ] reviewed state machine logic - [ ] reviewed API design and naming - [ ] reviewed documentation is accurate - [ ] reviewed tests and test coverage - [ ] manually tested (if applicable) (cherry picked from commit 744c85b8fa616648cc969d4765c159590f2b28d2) # Conflicts: # CHANGELOG.md * changelog Co-authored-by: Dev Ojha <ValarDragon@users.noreply.github.com> Co-authored-by: Federico Kunze Küllmer <federico.kunze94@gmail.com>
2021-12-30 14:54:33 -08:00
return fmt.Sprintf("%v%s", coin.Amount, coin.Denom)
2017-03-28 13:32:55 -07:00
}
// Validate returns an error if the Coin has a negative amount or if
// the denom is invalid.
func (coin Coin) Validate() error {
if err := ValidateDenom(coin.Denom); err != nil {
return err
}
if coin.Amount.IsNegative() {
return fmt.Errorf("negative coin amount: %v", coin.Amount)
}
return nil
}
// IsValid returns true if the Coin has a non-negative amount and the denom is valid.
func (coin Coin) IsValid() bool {
return coin.Validate() == nil
}
2017-07-12 09:54:07 -07:00
// IsZero returns if this represents no money
func (coin Coin) IsZero() bool {
return coin.Amount.IsZero()
2017-07-12 09:54:07 -07:00
}
// IsGTE returns true if they are the same type and the receiver is
// an equal or greater value
func (coin Coin) IsGTE(other Coin) bool {
if coin.Denom != other.Denom {
panic(fmt.Sprintf("invalid coin denominations; %s, %s", coin.Denom, other.Denom))
}
return !coin.Amount.LT(other.Amount)
2018-03-25 10:35:45 -07:00
}
// IsLT returns true if they are the same type and the receiver is
// a smaller value
func (coin Coin) IsLT(other Coin) bool {
if coin.Denom != other.Denom {
panic(fmt.Sprintf("invalid coin denominations; %s, %s", coin.Denom, other.Denom))
}
return coin.Amount.LT(other.Amount)
}
2018-03-25 10:35:45 -07:00
// IsEqual returns true if the two sets of Coins have the same value
func (coin Coin) IsEqual(other Coin) bool {
if coin.Denom != other.Denom {
panic(fmt.Sprintf("invalid coin denominations; %s, %s", coin.Denom, other.Denom))
}
return coin.Amount.Equal(other.Amount)
2018-03-25 10:35:45 -07:00
}
// Add adds amounts of two coins with same denom. If the coins differ in denom then
// it panics.
func (coin Coin) Add(coinB Coin) Coin {
if coin.Denom != coinB.Denom {
panic(fmt.Sprintf("invalid coin denominations; %s, %s", coin.Denom, coinB.Denom))
2018-03-25 10:35:45 -07:00
}
return Coin{coin.Denom, coin.Amount.Add(coinB.Amount)}
2018-03-25 10:35:45 -07:00
}
// AddAmount adds an amount to the Coin.
func (coin Coin) AddAmount(amount Int) Coin {
return Coin{coin.Denom, coin.Amount.Add(amount)}
}
// Sub subtracts amounts of two coins with same denom. If the coins differ in denom
// then it panics.
func (coin Coin) Sub(coinB Coin) Coin {
if coin.Denom != coinB.Denom {
panic(fmt.Sprintf("invalid coin denominations; %s, %s", coin.Denom, coinB.Denom))
}
res := Coin{coin.Denom, coin.Amount.Sub(coinB.Amount)}
if res.IsNegative() {
panic("negative coin amount")
2018-03-25 10:35:45 -07:00
}
return res
}
// SubAmount subtracts an amount from the Coin.
func (coin Coin) SubAmount(amount Int) Coin {
res := Coin{coin.Denom, coin.Amount.Sub(amount)}
if res.IsNegative() {
panic("negative coin amount")
}
return res
}
// IsPositive returns true if coin amount is positive.
//
// TODO: Remove once unsigned integers are used.
func (coin Coin) IsPositive() bool {
return coin.Amount.Sign() == 1
}
// IsNegative returns true if the coin amount is negative and false otherwise.
//
// TODO: Remove once unsigned integers are used.
func (coin Coin) IsNegative() bool {
return coin.Amount.Sign() == -1
2017-07-12 09:54:07 -07:00
}
fix: null guard for tx fee amounts (backport #10327) (#10342) * fix: null guard for tx fee amounts (#10327) ## Description It is possible to submit a TX with a fees object containing a Coin with a nil amount. This results in a rather cryptic redacted panic response when the basic validation checks fee Coins for negative amounts. This PR adds an additional check for nil to provide a friendlier error message. --- ### Author Checklist *All items are required. Please add a note to the item if the item is not applicable and please add links to any relevant follow up issues.* I have... - [x] included the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title - [ ] added `!` to the type prefix if API or client breaking change - [x] targeted the correct branch (see [PR Targeting](https://github.com/cosmos/cosmos-sdk/blob/master/CONTRIBUTING.md#pr-targeting)) - [x] provided a link to the relevant issue or specification (note: No issue exists) - [x] followed the guidelines for [building modules](https://github.com/cosmos/cosmos-sdk/blob/master/docs/building-modules) - [x] included the necessary unit and integration [tests](https://github.com/cosmos/cosmos-sdk/blob/master/CONTRIBUTING.md#testing) (note: First PR against the SDK so please comment with what needs to be done) - [x] added a changelog entry to `CHANGELOG.md` - [x] included comments for [documenting Go code](https://blog.golang.org/godoc) - [ ] updated the relevant documentation or specification - [x] reviewed "Files changed" and left comments if necessary - [x] confirmed all CI checks have passed ### Reviewers Checklist *All items are required. Please add a note if the item is not applicable and please add your handle next to the items reviewed if you only reviewed selected items.* I have... - [ ] confirmed the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title - [ ] confirmed `!` in the type prefix if API or client breaking change - [ ] confirmed all author checklist items have been addressed - [ ] reviewed state machine logic - [ ] reviewed API design and naming - [ ] reviewed documentation is accurate - [ ] reviewed tests and test coverage - [ ] manually tested (if applicable) (cherry picked from commit 96e162b8a3939c7999c0c12c39d196698594306c) # Conflicts: # CHANGELOG.md * solve conflicts * move sections Co-authored-by: Alex Megalokonomos <alex.megalokonomos@tendermint.com> Co-authored-by: marbar3778 <marbar3778@yahoo.com>
2021-10-12 03:47:54 -07:00
// IsNil returns true if the coin amount is nil and false otherwise.
func (coin Coin) IsNil() bool {
return coin.Amount.i == nil
}
//-----------------------------------------------------------------------------
2017-12-21 03:26:40 -08:00
// Coins
2016-04-01 15:19:07 -07:00
2017-07-06 05:59:45 -07:00
// Coins is a set of Coin, one per currency
2016-04-01 15:19:07 -07:00
type Coins []Coin
// NewCoins constructs a new coin set. The provided coins will be sanitized by removing
// zero coins and sorting the coin set. A panic will occur if the coin set is not valid.
2019-03-07 16:55:08 -08:00
func NewCoins(coins ...Coin) Coins {
newCoins := sanitizeCoins(coins)
if err := newCoins.Validate(); err != nil {
panic(fmt.Errorf("invalid coin set %s: %w", newCoins, err))
2019-03-07 16:55:08 -08:00
}
return newCoins
}
func sanitizeCoins(coins []Coin) Coins {
newCoins := removeZeroCoins(coins)
if len(newCoins) == 0 {
return Coins{}
}
return newCoins.Sort()
}
type coinsJSON Coins
// MarshalJSON implements a custom JSON marshaller for the Coins type to allow
// nil Coins to be encoded as an empty array.
func (coins Coins) MarshalJSON() ([]byte, error) {
if coins == nil {
return json.Marshal(coinsJSON(Coins{}))
}
return json.Marshal(coinsJSON(coins))
}
func (coins Coins) String() string {
if len(coins) == 0 {
return ""
perf: Improve the speed of coins.String() (backport #10076) (#10850) * perf: Improve the speed of coins.String() (#10076) <!-- The default pull request template is for types feat, fix, or refactor. For other templates, add one of the following parameters to the url: - template=docs.md - template=other.md --> ## Description Speedup coins.String() and coin.String() In my local benchmarks, this >2x improves the time for balances with 1 coin, and further improves speed for strings with many balances. I did not benchmark on the two coin usecase --- ### Author Checklist *All items are required. Please add a note to the item if the item is not applicable and please add links to any relevant follow up issues.* I have... - [x] included the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title - [x] added `!` to the type prefix if API or client breaking change - [x] targeted the correct branch (see [PR Targeting](https://github.com/cosmos/cosmos-sdk/blob/master/CONTRIBUTING.md#pr-targeting)) - [x] provided a link to the relevant issue or specification - n/a - [x] followed the guidelines for [building modules](https://github.com/cosmos/cosmos-sdk/blob/master/docs/building-modules) - [x] included the necessary unit and integration [tests](https://github.com/cosmos/cosmos-sdk/blob/master/CONTRIBUTING.md#testing) - covered by existing - [ ] added a changelog entry to `CHANGELOG.md` - [x] included comments for [documenting Go code](https://blog.golang.org/godoc) - seems sufficiently clear - [x] updated the relevant documentation or specification - [x] reviewed "Files changed" and left comments if necessary - [ ] confirmed all CI checks have passed ### Reviewers Checklist *All items are required. Please add a note if the item is not applicable and please add your handle next to the items reviewed if you only reviewed selected items.* I have... - [ ] confirmed the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title - [ ] confirmed `!` in the type prefix if API or client breaking change - [ ] confirmed all author checklist items have been addressed - [ ] reviewed state machine logic - [ ] reviewed API design and naming - [ ] reviewed documentation is accurate - [ ] reviewed tests and test coverage - [ ] manually tested (if applicable) (cherry picked from commit 744c85b8fa616648cc969d4765c159590f2b28d2) # Conflicts: # CHANGELOG.md * changelog Co-authored-by: Dev Ojha <ValarDragon@users.noreply.github.com> Co-authored-by: Federico Kunze Küllmer <federico.kunze94@gmail.com>
2021-12-30 14:54:33 -08:00
} else if len(coins) == 1 {
return coins[0].String()
}
perf: Improve the speed of coins.String() (backport #10076) (#10850) * perf: Improve the speed of coins.String() (#10076) <!-- The default pull request template is for types feat, fix, or refactor. For other templates, add one of the following parameters to the url: - template=docs.md - template=other.md --> ## Description Speedup coins.String() and coin.String() In my local benchmarks, this >2x improves the time for balances with 1 coin, and further improves speed for strings with many balances. I did not benchmark on the two coin usecase --- ### Author Checklist *All items are required. Please add a note to the item if the item is not applicable and please add links to any relevant follow up issues.* I have... - [x] included the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title - [x] added `!` to the type prefix if API or client breaking change - [x] targeted the correct branch (see [PR Targeting](https://github.com/cosmos/cosmos-sdk/blob/master/CONTRIBUTING.md#pr-targeting)) - [x] provided a link to the relevant issue or specification - n/a - [x] followed the guidelines for [building modules](https://github.com/cosmos/cosmos-sdk/blob/master/docs/building-modules) - [x] included the necessary unit and integration [tests](https://github.com/cosmos/cosmos-sdk/blob/master/CONTRIBUTING.md#testing) - covered by existing - [ ] added a changelog entry to `CHANGELOG.md` - [x] included comments for [documenting Go code](https://blog.golang.org/godoc) - seems sufficiently clear - [x] updated the relevant documentation or specification - [x] reviewed "Files changed" and left comments if necessary - [ ] confirmed all CI checks have passed ### Reviewers Checklist *All items are required. Please add a note if the item is not applicable and please add your handle next to the items reviewed if you only reviewed selected items.* I have... - [ ] confirmed the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title - [ ] confirmed `!` in the type prefix if API or client breaking change - [ ] confirmed all author checklist items have been addressed - [ ] reviewed state machine logic - [ ] reviewed API design and naming - [ ] reviewed documentation is accurate - [ ] reviewed tests and test coverage - [ ] manually tested (if applicable) (cherry picked from commit 744c85b8fa616648cc969d4765c159590f2b28d2) # Conflicts: # CHANGELOG.md * changelog Co-authored-by: Dev Ojha <ValarDragon@users.noreply.github.com> Co-authored-by: Federico Kunze Küllmer <federico.kunze94@gmail.com>
2021-12-30 14:54:33 -08:00
// Build the string with a string builder
var out strings.Builder
for _, coin := range coins[:len(coins)-1] {
out.WriteString(coin.String())
out.WriteByte(',')
}
perf: Improve the speed of coins.String() (backport #10076) (#10850) * perf: Improve the speed of coins.String() (#10076) <!-- The default pull request template is for types feat, fix, or refactor. For other templates, add one of the following parameters to the url: - template=docs.md - template=other.md --> ## Description Speedup coins.String() and coin.String() In my local benchmarks, this >2x improves the time for balances with 1 coin, and further improves speed for strings with many balances. I did not benchmark on the two coin usecase --- ### Author Checklist *All items are required. Please add a note to the item if the item is not applicable and please add links to any relevant follow up issues.* I have... - [x] included the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title - [x] added `!` to the type prefix if API or client breaking change - [x] targeted the correct branch (see [PR Targeting](https://github.com/cosmos/cosmos-sdk/blob/master/CONTRIBUTING.md#pr-targeting)) - [x] provided a link to the relevant issue or specification - n/a - [x] followed the guidelines for [building modules](https://github.com/cosmos/cosmos-sdk/blob/master/docs/building-modules) - [x] included the necessary unit and integration [tests](https://github.com/cosmos/cosmos-sdk/blob/master/CONTRIBUTING.md#testing) - covered by existing - [ ] added a changelog entry to `CHANGELOG.md` - [x] included comments for [documenting Go code](https://blog.golang.org/godoc) - seems sufficiently clear - [x] updated the relevant documentation or specification - [x] reviewed "Files changed" and left comments if necessary - [ ] confirmed all CI checks have passed ### Reviewers Checklist *All items are required. Please add a note if the item is not applicable and please add your handle next to the items reviewed if you only reviewed selected items.* I have... - [ ] confirmed the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title - [ ] confirmed `!` in the type prefix if API or client breaking change - [ ] confirmed all author checklist items have been addressed - [ ] reviewed state machine logic - [ ] reviewed API design and naming - [ ] reviewed documentation is accurate - [ ] reviewed tests and test coverage - [ ] manually tested (if applicable) (cherry picked from commit 744c85b8fa616648cc969d4765c159590f2b28d2) # Conflicts: # CHANGELOG.md * changelog Co-authored-by: Dev Ojha <ValarDragon@users.noreply.github.com> Co-authored-by: Federico Kunze Küllmer <federico.kunze94@gmail.com>
2021-12-30 14:54:33 -08:00
out.WriteString(coins[len(coins)-1].String())
return out.String()
}
// Validate checks that the Coins are sorted, have positive amount, with a valid and unique
// denomination (i.e no duplicates). Otherwise, it returns an error.
func (coins Coins) Validate() error {
2016-04-01 15:19:07 -07:00
switch len(coins) {
case 0:
return nil
2016-04-01 15:19:07 -07:00
case 1:
2019-12-10 08:48:57 -08:00
if err := ValidateDenom(coins[0].Denom); err != nil {
return err
}
if !coins[0].IsPositive() {
return fmt.Errorf("coin %s amount is not positive", coins[0])
}
return nil
2019-01-02 11:14:12 -08:00
default:
2019-01-02 13:19:48 -08:00
// check single coin case
if err := (Coins{coins[0]}).Validate(); err != nil {
return err
}
2019-01-02 13:19:48 -08:00
2016-04-01 15:19:07 -07:00
lowDenom := coins[0].Denom
seenDenoms := make(map[string]bool)
seenDenoms[lowDenom] = true
2016-04-01 15:19:07 -07:00
for _, coin := range coins[1:] {
if seenDenoms[coin.Denom] {
return fmt.Errorf("duplicate denomination %s", coin.Denom)
}
if err := ValidateDenom(coin.Denom); err != nil {
return err
}
2016-04-01 15:19:07 -07:00
if coin.Denom <= lowDenom {
return fmt.Errorf("denomination %s is not sorted", coin.Denom)
2016-04-01 15:19:07 -07:00
}
if !coin.IsPositive() {
return fmt.Errorf("coin %s amount is not positive", coin.Denom)
2016-04-01 15:19:07 -07:00
}
2017-01-31 03:24:49 -08:00
// we compare each coin against the last denom
lowDenom = coin.Denom
seenDenoms[coin.Denom] = true
2016-04-01 15:19:07 -07:00
}
return nil
2016-04-01 15:19:07 -07:00
}
}
func (coins Coins) isSorted() bool {
for i := 1; i < len(coins); i++ {
if coins[i-1].Denom > coins[i].Denom {
return false
}
}
return true
}
// IsValid calls Validate and returns true when the Coins are sorted, have positive amount, with a
// valid and unique denomination (i.e no duplicates).
func (coins Coins) IsValid() bool {
return coins.Validate() == nil
}
// Add adds two sets of coins.
//
// e.g.
// {2A} + {A, 2B} = {3A, 2B}
// {2A} + {0B} = {2A}
//
// NOTE: Add operates under the invariant that coins are sorted by
// denominations.
//
// CONTRACT: Add will never return Coins where one Coin has a non-positive
// amount. In otherwords, IsValid will always return true.
// The function panics if `coins` or `coinsB` are not sorted (ascending).
func (coins Coins) Add(coinsB ...Coin) Coins {
return coins.safeAdd(coinsB)
}
// safeAdd will perform addition of two coins sets. If both coin sets are
// empty, then an empty set is returned. If only a single set is empty, the
// other set is returned. Otherwise, the coins are compared in order of their
// denomination and addition only occurs when the denominations match, otherwise
// the coin is simply added to the sum assuming it's not zero.
// The function panics if `coins` or `coinsB` are not sorted (ascending).
func (coins Coins) safeAdd(coinsB Coins) Coins {
// probably the best way will be to make Coins and interface and hide the structure
// definition (type alias)
if !coins.isSorted() {
panic("Coins (self) must be sorted")
}
if !coinsB.isSorted() {
panic("Wrong argument: coins must be sorted")
}
2018-04-07 00:02:00 -07:00
sum := ([]Coin)(nil)
2016-04-01 15:19:07 -07:00
indexA, indexB := 0, 0
2017-07-06 20:37:45 -07:00
lenA, lenB := len(coins), len(coinsB)
2016-04-01 15:19:07 -07:00
for {
if indexA == lenA {
if indexB == lenB {
// return nil coins if both sets are empty
2016-04-01 15:19:07 -07:00
return sum
}
// return set B (excluding zero coins) if set A is empty
return append(sum, removeZeroCoins(coinsB[indexB:])...)
2016-04-01 15:19:07 -07:00
} else if indexB == lenB {
// return set A (excluding zero coins) if set B is empty
return append(sum, removeZeroCoins(coins[indexA:])...)
2016-04-01 15:19:07 -07:00
}
2017-07-06 20:37:45 -07:00
coinA, coinB := coins[indexA], coinsB[indexB]
2016-04-01 15:19:07 -07:00
switch strings.Compare(coinA.Denom, coinB.Denom) {
case -1: // coin A denom < coin B denom
if !coinA.IsZero() {
sum = append(sum, coinA)
}
2017-07-06 05:59:45 -07:00
indexA++
case 0: // coin A denom == coin B denom
res := coinA.Add(coinB)
if !res.IsZero() {
sum = append(sum, res)
2016-04-01 15:19:07 -07:00
}
2017-07-06 05:59:45 -07:00
indexA++
indexB++
case 1: // coin A denom > coin B denom
if !coinB.IsZero() {
sum = append(sum, coinB)
}
2017-07-06 05:59:45 -07:00
indexB++
2016-04-01 15:19:07 -07:00
}
}
}
2019-03-12 08:02:18 -07:00
// DenomsSubsetOf returns true if receiver's denom set
// is subset of coinsB's denoms.
2019-03-11 07:26:46 -07:00
func (coins Coins) DenomsSubsetOf(coinsB Coins) bool {
2019-03-07 16:23:12 -08:00
// more denoms in B than in receiver
2019-03-11 07:26:46 -07:00
if len(coins) > len(coinsB) {
2019-03-07 16:23:12 -08:00
return false
}
2019-03-11 07:26:46 -07:00
for _, coin := range coins {
if coinsB.AmountOf(coin.Denom).IsZero() {
2019-03-07 16:23:12 -08:00
return false
}
}
return true
}
// Sub subtracts a set of coins from another.
//
// e.g.
// {2A, 3B} - {A} = {A, 3B}
// {2A} - {0B} = {2A}
// {A, B} - {A} = {B}
//
// CONTRACT: Sub will never return Coins where one Coin has a non-positive
// amount. In otherwords, IsValid will always return true.
func (coins Coins) Sub(coinsB Coins) Coins {
diff, hasNeg := coins.SafeSub(coinsB)
if hasNeg {
panic("negative coin amount")
2016-04-01 15:19:07 -07:00
}
return diff
2016-04-01 15:19:07 -07:00
}
// SafeSub performs the same arithmetic as Sub but returns a boolean if any
// negative coin amount was returned.
// The function panics if `coins` or `coinsB` are not sorted (ascending).
func (coins Coins) SafeSub(coinsB Coins) (Coins, bool) {
diff := coins.safeAdd(coinsB.negative())
return diff, diff.IsAnyNegative()
2016-04-01 15:19:07 -07:00
}
feat: min and max operators on coins (backport #11200) (#11249) * feat: min and max operators on coins (#11200) ## Description Closes: #10995 Adds `Min()` and `Max()` operations on `sdk.Coins` for per-denom minimum and maximum. Replaced an example of manual low-level construction of `Coins` with higher-level operators. Upcoming enhancements to vesting accounts make heavy use of this pattern. --- ### Author Checklist *All items are required. Please add a note to the item if the item is not applicable and please add links to any relevant follow up issues.* I have... - [X] included the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title - [X] added `!` to the type prefix if API or client breaking change - [X] targeted the correct branch (see [PR Targeting](https://github.com/cosmos/cosmos-sdk/blob/master/CONTRIBUTING.md#pr-targeting)) - [X] provided a link to the relevant issue or specification - [X] followed the guidelines for [building modules](https://github.com/cosmos/cosmos-sdk/blob/master/docs/building-modules) - [X] included the necessary unit and integration [tests](https://github.com/cosmos/cosmos-sdk/blob/master/CONTRIBUTING.md#testing) - [X] added a changelog entry to `CHANGELOG.md` - [X] included comments for [documenting Go code](https://blog.golang.org/godoc) - [X] updated the relevant documentation or specification - [X] reviewed "Files changed" and left comments if necessary - [ ] confirmed all CI checks have passed ### Reviewers Checklist *All items are required. Please add a note if the item is not applicable and please add your handle next to the items reviewed if you only reviewed selected items.* I have... - [ ] confirmed the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title - [ ] confirmed `!` in the type prefix if API or client breaking change - [ ] confirmed all author checklist items have been addressed - [ ] reviewed state machine logic - [ ] reviewed API design and naming - [ ] reviewed documentation is accurate - [ ] reviewed tests and test coverage - [ ] manually tested (if applicable) (cherry picked from commit afbb0bd1941f7ad36e086913153af02eb6a68f5a) # Conflicts: # CHANGELOG.md * fix conflicts Co-authored-by: Jim Larson <32469398+JimLarson@users.noreply.github.com> Co-authored-by: marbar3778 <marbar3778@yahoo.com>
2022-02-23 15:51:55 -08:00
// Max takes two valid Coins inputs and returns a valid Coins result
// where for every denom D, AmountOf(D) of the result is the maximum
// of AmountOf(D) of the inputs. Note that the result might be not
// be equal to either input. For any valid Coins a, b, and c, the
// following are always true:
// a.IsAllLTE(a.Max(b))
// b.IsAllLTE(a.Max(b))
// a.IsAllLTE(c) && b.IsAllLTE(c) == a.Max(b).IsAllLTE(c)
// a.Add(b...).IsEqual(a.Min(b).Add(a.Max(b)...))
//
// E.g.
// {1A, 3B, 2C}.Max({4A, 2B, 2C} == {4A, 3B, 2C})
// {2A, 3B}.Max({1B, 4C}) == {2A, 3B, 4C}
// {1A, 2B}.Max({}) == {1A, 2B}
func (coins Coins) Max(coinsB Coins) Coins {
max := make([]Coin, 0)
indexA, indexB := 0, 0
for indexA < len(coins) && indexB < len(coinsB) {
coinA, coinB := coins[indexA], coinsB[indexB]
switch strings.Compare(coinA.Denom, coinB.Denom) {
case -1: // denom missing from coinsB
max = append(max, coinA)
indexA++
case 0: // same denom in both
maxCoin := coinA
if coinB.Amount.GT(maxCoin.Amount) {
maxCoin = coinB
}
max = append(max, maxCoin)
indexA++
indexB++
case 1: // denom missing from coinsA
max = append(max, coinB)
indexB++
}
}
for ; indexA < len(coins); indexA++ {
max = append(max, coins[indexA])
}
for ; indexB < len(coinsB); indexB++ {
max = append(max, coinsB[indexB])
}
return NewCoins(max...)
}
// Min takes two valid Coins inputs and returns a valid Coins result
// where for every denom D, AmountOf(D) of the result is the minimum
// of AmountOf(D) of the inputs. Note that the result might be not
// be equal to either input. For any valid Coins a, b, and c, the
// following are always true:
// a.Min(b).IsAllLTE(a)
// a.Min(b).IsAllLTE(b)
// c.IsAllLTE(a) && c.IsAllLTE(b) == c.IsAllLTE(a.Min(b))
// a.Add(b...).IsEqual(a.Min(b).Add(a.Max(b)...))
//
// E.g.
// {1A, 3B, 2C}.Min({4A, 2B, 2C} == {1A, 2B, 2C})
// {2A, 3B}.Min({1B, 4C}) == {1B}
// {1A, 2B}.Min({3C}) == empty
//
// See also DecCoins.Intersect().
func (coins Coins) Min(coinsB Coins) Coins {
min := make([]Coin, 0)
for indexA, indexB := 0, 0; indexA < len(coins) && indexB < len(coinsB); {
coinA, coinB := coins[indexA], coinsB[indexB]
switch strings.Compare(coinA.Denom, coinB.Denom) {
case -1: // denom missing from coinsB
indexA++
case 0: // same denom in both
minCoin := coinA
if coinB.Amount.LT(minCoin.Amount) {
minCoin = coinB
}
if !minCoin.IsZero() {
min = append(min, minCoin)
}
indexA++
indexB++
case 1: // denom missing from coins
indexB++
}
}
return NewCoins(min...)
}
2019-03-12 08:02:18 -07:00
// IsAllGT returns true if for every denom in coinsB,
// the denom is present at a greater amount in coins.
func (coins Coins) IsAllGT(coinsB Coins) bool {
2019-03-07 16:23:12 -08:00
if len(coins) == 0 {
return false
}
2019-03-07 16:23:12 -08:00
if len(coinsB) == 0 {
return true
}
2019-03-11 07:26:46 -07:00
if !coinsB.DenomsSubsetOf(coins) {
2019-03-07 16:23:12 -08:00
return false
}
for _, coinB := range coinsB {
amountA, amountB := coins.AmountOf(coinB.Denom), coinB.Amount
if !amountA.GT(amountB) {
return false
}
}
return true
}
// IsAllGTE returns false if for any denom in coinsB,
// the denom is present at a smaller amount in coins;
// else returns true.
func (coins Coins) IsAllGTE(coinsB Coins) bool {
if len(coinsB) == 0 {
2016-04-01 15:19:07 -07:00
return true
}
if len(coins) == 0 {
return false
}
for _, coinB := range coinsB {
if coinB.Amount.GT(coins.AmountOf(coinB.Denom)) {
return false
}
}
return true
2016-04-01 15:19:07 -07:00
}
// IsAllLT returns True iff for every denom in coins, the denom is present at
// a smaller amount in coinsB.
func (coins Coins) IsAllLT(coinsB Coins) bool {
return coinsB.IsAllGT(coins)
}
// IsAllLTE returns true iff for every denom in coins, the denom is present at
// a smaller or equal amount in coinsB.
func (coins Coins) IsAllLTE(coinsB Coins) bool {
return coinsB.IsAllGTE(coins)
}
2019-05-07 10:10:35 -07:00
// IsAnyGT returns true iff for any denom in coins, the denom is present at a
// greater amount in coinsB.
//
// e.g.
// {2A, 3B}.IsAnyGT{A} = true
// {2A, 3B}.IsAnyGT{5C} = false
// {}.IsAnyGT{5C} = false
// {2A, 3B}.IsAnyGT{} = false
func (coins Coins) IsAnyGT(coinsB Coins) bool {
if len(coinsB) == 0 {
return false
}
for _, coin := range coins {
amt := coinsB.AmountOf(coin.Denom)
if coin.Amount.GT(amt) && !amt.IsZero() {
return true
}
}
return false
}
// IsAnyGTE returns true iff coins contains at least one denom that is present
// at a greater or equal amount in coinsB; it returns false otherwise.
//
// NOTE: IsAnyGTE operates under the invariant that both coin sets are sorted
// by denominations and there exists no zero coins.
func (coins Coins) IsAnyGTE(coinsB Coins) bool {
if len(coinsB) == 0 {
return false
}
for _, coin := range coins {
amt := coinsB.AmountOf(coin.Denom)
2019-02-12 07:22:04 -08:00
if coin.Amount.GTE(amt) && !amt.IsZero() {
return true
}
}
return false
}
// IsZero returns true if there are no coins or all coins are zero.
2016-04-01 15:19:07 -07:00
func (coins Coins) IsZero() bool {
2018-03-17 13:20:24 -07:00
for _, coin := range coins {
if !coin.IsZero() {
return false
}
}
return true
2016-04-01 15:19:07 -07:00
}
2017-07-06 05:59:45 -07:00
// IsEqual returns true if the two sets of Coins have the same value
2017-07-06 20:37:45 -07:00
func (coins Coins) IsEqual(coinsB Coins) bool {
if len(coins) != len(coinsB) {
2016-04-01 15:19:07 -07:00
return false
}
coins = coins.Sort()
coinsB = coinsB.Sort()
2017-07-06 20:37:45 -07:00
for i := 0; i < len(coins); i++ {
if !coins[i].IsEqual(coinsB[i]) {
2016-04-01 15:19:07 -07:00
return false
}
}
2016-04-01 15:19:07 -07:00
return true
}
// Empty returns true if there are no coins and false otherwise.
func (coins Coins) Empty() bool {
return len(coins) == 0
}
// AmountOf returns the amount of a denom from coins
func (coins Coins) AmountOf(denom string) Int {
mustValidateDenom(denom)
return coins.AmountOfNoDenomValidation(denom)
}
// AmountOfNoDenomValidation returns the amount of a denom from coins
// without validating the denomination.
func (coins Coins) AmountOfNoDenomValidation(denom string) Int {
switch len(coins) {
case 0:
return ZeroInt()
case 1:
coin := coins[0]
if coin.Denom == denom {
return coin.Amount
}
return ZeroInt()
default:
// Binary search the amount of coins remaining
midIdx := len(coins) / 2 // 2:1, 3:1, 4:2
coin := coins[midIdx]
2019-08-19 09:06:27 -07:00
switch {
case denom < coin.Denom:
return coins[:midIdx].AmountOfNoDenomValidation(denom)
2019-08-19 09:06:27 -07:00
case denom == coin.Denom:
return coin.Amount
2019-08-19 09:06:27 -07:00
default:
return coins[midIdx+1:].AmountOfNoDenomValidation(denom)
}
}
}
// GetDenomByIndex returns the Denom of the certain coin to make the findDup generic
func (coins Coins) GetDenomByIndex(i int) string {
return coins[i].Denom
}
// IsAllPositive returns true if there is at least one coin and all currencies
// have a positive value.
func (coins Coins) IsAllPositive() bool {
2016-04-01 15:19:07 -07:00
if len(coins) == 0 {
return false
}
2018-03-25 10:35:45 -07:00
for _, coin := range coins {
if !coin.IsPositive() {
2016-04-01 15:19:07 -07:00
return false
}
}
2016-04-01 15:19:07 -07:00
return true
}
// IsAnyNegative returns true if there is at least one coin whose amount
// is negative; returns false otherwise. It returns false if the coin set
// is empty too.
//
// TODO: Remove once unsigned integers are used.
func (coins Coins) IsAnyNegative() bool {
2018-03-25 10:35:45 -07:00
for _, coin := range coins {
if coin.IsNegative() {
return true
}
}
return false
}
fix: null guard for tx fee amounts (backport #10327) (#10342) * fix: null guard for tx fee amounts (#10327) ## Description It is possible to submit a TX with a fees object containing a Coin with a nil amount. This results in a rather cryptic redacted panic response when the basic validation checks fee Coins for negative amounts. This PR adds an additional check for nil to provide a friendlier error message. --- ### Author Checklist *All items are required. Please add a note to the item if the item is not applicable and please add links to any relevant follow up issues.* I have... - [x] included the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title - [ ] added `!` to the type prefix if API or client breaking change - [x] targeted the correct branch (see [PR Targeting](https://github.com/cosmos/cosmos-sdk/blob/master/CONTRIBUTING.md#pr-targeting)) - [x] provided a link to the relevant issue or specification (note: No issue exists) - [x] followed the guidelines for [building modules](https://github.com/cosmos/cosmos-sdk/blob/master/docs/building-modules) - [x] included the necessary unit and integration [tests](https://github.com/cosmos/cosmos-sdk/blob/master/CONTRIBUTING.md#testing) (note: First PR against the SDK so please comment with what needs to be done) - [x] added a changelog entry to `CHANGELOG.md` - [x] included comments for [documenting Go code](https://blog.golang.org/godoc) - [ ] updated the relevant documentation or specification - [x] reviewed "Files changed" and left comments if necessary - [x] confirmed all CI checks have passed ### Reviewers Checklist *All items are required. Please add a note if the item is not applicable and please add your handle next to the items reviewed if you only reviewed selected items.* I have... - [ ] confirmed the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title - [ ] confirmed `!` in the type prefix if API or client breaking change - [ ] confirmed all author checklist items have been addressed - [ ] reviewed state machine logic - [ ] reviewed API design and naming - [ ] reviewed documentation is accurate - [ ] reviewed tests and test coverage - [ ] manually tested (if applicable) (cherry picked from commit 96e162b8a3939c7999c0c12c39d196698594306c) # Conflicts: # CHANGELOG.md * solve conflicts * move sections Co-authored-by: Alex Megalokonomos <alex.megalokonomos@tendermint.com> Co-authored-by: marbar3778 <marbar3778@yahoo.com>
2021-10-12 03:47:54 -07:00
// IsAnyNil returns true if there is at least one coin whose amount
// is nil; returns false otherwise. It returns false if the coin set
// is empty too.
func (coins Coins) IsAnyNil() bool {
for _, coin := range coins {
if coin.IsNil() {
return true
}
}
return false
}
// negative returns a set of coins with all amount negative.
//
// TODO: Remove once unsigned integers are used.
func (coins Coins) negative() Coins {
res := make([]Coin, 0, len(coins))
for _, coin := range coins {
res = append(res, Coin{
Denom: coin.Denom,
Amount: coin.Amount.Neg(),
})
}
return res
}
// removeZeroCoins removes all zero coins from the given coin set in-place.
func removeZeroCoins(coins Coins) Coins {
perf: Only do memory allocation when zero coin is found (backport #10339) (#10361) * perf: Only do memory allocation when zero coin is found (#10339) ## Description Closes: #10333 Added a loop that checks for zero coins before making any memory allocations. If no zero coins are found, just return the `coins` slice as is. If a zero coin is found, then allocate the new array, stop looking for the first zero, and restart the loop to "remove" the 0's. --- ### Author Checklist *All items are required. Please add a note to the item if the item is not applicable and please add links to any relevant follow up issues.* I have... - [x] included the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title - [ ] added `!` to the type prefix if API or client breaking change (Not Applicable) - [x] targeted the correct branch (see [PR Targeting](https://github.com/cosmos/cosmos-sdk/blob/master/CONTRIBUTING.md#pr-targeting)) - [x] provided a link to the relevant issue or specification - [ ] followed the guidelines for [building modules](https://github.com/cosmos/cosmos-sdk/blob/master/docs/building-modules) (Not Applicable) - [ ] included the necessary unit and integration [tests](https://github.com/cosmos/cosmos-sdk/blob/master/CONTRIBUTING.md#testing) (Not Applicable) - [x] added a changelog entry to `CHANGELOG.md` - [ ] included comments for [documenting Go code](https://blog.golang.org/godoc) (Not Applicable) - [ ] updated the relevant documentation or specification (Not Applicable) - [x] reviewed "Files changed" and left comments if necessary - [ ] confirmed all CI checks have passed ### Reviewers Checklist *All items are required. Please add a note if the item is not applicable and please add your handle next to the items reviewed if you only reviewed selected items.* I have... - [ ] confirmed the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title - [ ] confirmed `!` in the type prefix if API or client breaking change - [ ] confirmed all author checklist items have been addressed - [ ] reviewed state machine logic - [ ] reviewed API design and naming - [ ] reviewed documentation is accurate - [ ] reviewed tests and test coverage - [ ] manually tested (if applicable) (cherry picked from commit f00e7a4e15be6299166aa30045efbadb7d7c8423) # Conflicts: # CHANGELOG.md * fix conflict Co-authored-by: Jake Waggoner <32466459+waggonerjake@users.noreply.github.com> Co-authored-by: marbar3778 <marbar3778@yahoo.com>
2021-10-14 02:43:08 -07:00
for i := 0; i < len(coins); i++ {
if coins[i].IsZero() {
break
} else if i == len(coins)-1 {
return coins
}
}
var result []Coin
if len(coins) > 0 {
result = make([]Coin, 0, len(coins)-1)
}
for _, coin := range coins {
if !coin.IsZero() {
result = append(result, coin)
2018-01-25 16:30:49 -08:00
}
}
return result
2018-01-25 16:30:49 -08:00
}
//-----------------------------------------------------------------------------
2017-12-21 03:26:40 -08:00
// Sort interface
// Len implements sort.Interface for Coins
func (coins Coins) Len() int { return len(coins) }
// Less implements sort.Interface for Coins
2017-07-06 05:59:45 -07:00
func (coins Coins) Less(i, j int) bool { return coins[i].Denom < coins[j].Denom }
// Swap implements sort.Interface for Coins
func (coins Coins) Swap(i, j int) { coins[i], coins[j] = coins[j], coins[i] }
2017-07-06 05:59:45 -07:00
var _ sort.Interface = Coins{}
// Sort is a helper function to sort the set of coins in-place
2018-04-05 04:55:10 -07:00
func (coins Coins) Sort() Coins {
sort.Sort(coins)
return coins
}
2017-12-25 00:57:07 -08:00
//-----------------------------------------------------------------------------
2018-01-06 12:53:31 -08:00
// Parsing
var (
// Denominations can be 3 ~ 128 characters long and support letters, followed by either
// a letter, a number or a separator ('/').
reDnmString = `[a-zA-Z][a-zA-Z0-9/-]{2,127}`
reDecAmt = `[[:digit:]]+(?:\.[[:digit:]]+)?|\.[[:digit:]]+`
reSpc = `[[:space:]]*`
reDnm *regexp.Regexp
reDecCoin *regexp.Regexp
2018-01-06 12:53:31 -08:00
)
func init() {
SetCoinDenomRegex(DefaultCoinDenomRegex)
}
// DefaultCoinDenomRegex returns the default regex string
func DefaultCoinDenomRegex() string {
return reDnmString
}
// coinDenomRegex returns the current regex string and can be overwritten for custom validation
var coinDenomRegex = DefaultCoinDenomRegex
// SetCoinDenomRegex allows for coin's custom validation by overriding the regular
// expression string used for denom validation.
func SetCoinDenomRegex(reFn func() string) {
coinDenomRegex = reFn
reDnm = regexp.MustCompile(fmt.Sprintf(`^%s$`, coinDenomRegex()))
reDecCoin = regexp.MustCompile(fmt.Sprintf(`^(%s)%s(%s)$`, reDecAmt, reSpc, coinDenomRegex()))
}
// ValidateDenom is the default validation function for Coin.Denom.
2019-12-10 08:48:57 -08:00
func ValidateDenom(denom string) error {
if !reDnm.MatchString(denom) {
return fmt.Errorf("invalid denom: %s", denom)
}
return nil
}
func mustValidateDenom(denom string) {
2019-12-10 08:48:57 -08:00
if err := ValidateDenom(denom); err != nil {
panic(err)
}
}
// ParseCoinNormalized parses and normalize a cli input for one coin type, returning errors if invalid or on an empty string
// as well.
// Expected format: "{amount}{denomination}"
func ParseCoinNormalized(coinStr string) (coin Coin, err error) {
decCoin, err := ParseDecCoin(coinStr)
if err != nil {
return Coin{}, err
}
coin, _ = NormalizeDecCoin(decCoin).TruncateDecimal()
return coin, nil
2018-01-06 12:53:31 -08:00
}
// ParseCoinsNormalized will parse out a list of coins separated by commas, and normalize them by converting to the smallest
// unit. If the parsing is successful, the provided coins will be sanitized by removing zero coins and sorting the coin
// set. Lastly a validation of the coin set is executed. If the check passes, ParseCoinsNormalized will return the
// sanitized coins.
// Otherwise, it will return an error.
// If an empty string is provided to ParseCoinsNormalized, it returns nil Coins.
// ParseCoinsNormalized supports decimal coins as inputs, and truncate them to int after converted to the smallest unit.
// Expected format: "{amount0}{denomination},...,{amountN}{denominationN}"
func ParseCoinsNormalized(coinStr string) (Coins, error) {
coins, err := ParseDecCoins(coinStr)
if err != nil {
return Coins{}, err
2018-01-06 12:53:31 -08:00
}
return NormalizeCoins(coins), nil
2018-01-06 12:53:31 -08:00
}