Skip to content

Commit

Permalink
[1658]: Unit tests on the indexedAddrAmts stuff.
Browse files Browse the repository at this point in the history
  • Loading branch information
SpicyLemon committed Sep 26, 2023
1 parent 088d28b commit 91af37a
Show file tree
Hide file tree
Showing 3 changed files with 615 additions and 8 deletions.
20 changes: 20 additions & 0 deletions x/exchange/fulfillment.go
Original file line number Diff line number Diff line change
Expand Up @@ -731,7 +731,13 @@ func newIndexedAddrAmts() *indexedAddrAmts {
}

// add adds the coins to the given address.
// Panics if a provided coin is invalid.
func (i *indexedAddrAmts) add(addr string, coins ...sdk.Coin) {
for _, coin := range coins {
if err := coin.Validate(); err != nil {
panic(fmt.Errorf("cannot index and add invalid coin amount %q", coin))
}
}
n, known := i.indexes[addr]
if !known {
n = len(i.addrs)
Expand All @@ -743,18 +749,32 @@ func (i *indexedAddrAmts) add(addr string, coins ...sdk.Coin) {
}

// getAsInputs returns all the entries as bank Inputs.
// Panics if this is nil, has no addrs, or has a negative coin amount.
func (i *indexedAddrAmts) getAsInputs() []banktypes.Input {
if i == nil || len(i.addrs) == 0 {
panic(errors.New("cannot get inputs from empty set"))
}
rv := make([]banktypes.Input, len(i.addrs))
for n, addr := range i.addrs {
if !i.amts[n].IsAllPositive() {
panic(fmt.Errorf("invalid indexed amount %q for address %q: cannot be zero or negative", addr, i.amts[n]))
}
rv[n] = banktypes.Input{Address: addr, Coins: i.amts[n]}
}
return rv
}

// getAsOutputs returns all the entries as bank Outputs.
// Panics if this is nil, has no addrs, or has a negative coin amount.
func (i *indexedAddrAmts) getAsOutputs() []banktypes.Output {
if i == nil || len(i.addrs) == 0 {
panic(errors.New("cannot get inputs from empty set"))
}
rv := make([]banktypes.Output, len(i.addrs))
for n, addr := range i.addrs {
if !i.amts[n].IsAllPositive() {
panic(fmt.Errorf("invalid indexed amount %q for address %q: cannot be zero or negative", addr, i.amts[n]))
}
rv[n] = banktypes.Output{Address: addr, Coins: i.amts[n]}
}
return rv
Expand Down
Loading

0 comments on commit 91af37a

Please sign in to comment.