Skip to content

Commit

Permalink
bug fix in Get method of ColoredBalances (iotaledger#1089)
Browse files Browse the repository at this point in the history
* bug fix in Get method of ColoredBalances

* move test to color_test.go

* import sorting
  • Loading branch information
Evaldas Drasutis authored Mar 18, 2021
1 parent acffd28 commit 3fc4169
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 2 deletions.
7 changes: 5 additions & 2 deletions packages/ledgerstate/color.go
Original file line number Diff line number Diff line change
Expand Up @@ -175,8 +175,11 @@ func ColoredBalancesFromMarshalUtil(marshalUtil *marshalutil.MarshalUtil) (color
// Get returns the balance of the given Color and a boolean value indicating if the requested Color existed.
func (c *ColoredBalances) Get(color Color) (uint64, bool) {
balance, exists := c.balances.Get(color)

return balance.(uint64), exists
ret, ok := balance.(uint64)
if !ok {
return 0, false
}
return ret, exists
}

// ForEach calls the consumer for each element in the collection and aborts the iteration if the consumer returns false.
Expand Down
19 changes: 19 additions & 0 deletions packages/ledgerstate/color_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"golang.org/x/crypto/blake2b"
)

func TestNewColoredBalances(t *testing.T) {
Expand Down Expand Up @@ -67,3 +68,21 @@ func TestColoredBalances_String(t *testing.T) {

assert.Equal(t, "ColoredBalances {\n IOTA: 100\n MINT: 120\n}", coloredBalances.String())
}

func TestNonExistentColor(t *testing.T) {
h := blake2b.Sum256([]byte("dummy"))
color, _, err := ColorFromBytes(h[:])
require.NoError(t, err)
m := map[Color]uint64{color: 5}

cb := NewColoredBalances(m)
amount, ok := cb.Get(color)
require.EqualValues(t, 5, amount)
require.True(t, ok)

require.NotPanics(t, func() {
amount, ok = cb.Get(ColorIOTA)
})
require.False(t, ok)
require.EqualValues(t, 0, amount)
}

0 comments on commit 3fc4169

Please sign in to comment.