-
Notifications
You must be signed in to change notification settings - Fork 3.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
stargate: balance and metadata validation #8421
Merged
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
package types | ||
|
||
import ( | ||
"bytes" | ||
"encoding/json" | ||
fmt "fmt" | ||
"sort" | ||
|
||
"github.com/cosmos/cosmos-sdk/codec" | ||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
"github.com/cosmos/cosmos-sdk/x/bank/exported" | ||
) | ||
|
||
var _ exported.GenesisBalance = (*Balance)(nil) | ||
|
||
// GetAddress returns the account address of the Balance object. | ||
func (b Balance) GetAddress() sdk.AccAddress { | ||
addr, _ := sdk.AccAddressFromBech32(b.Address) | ||
return addr | ||
} | ||
|
||
// GetCoins returns the account coins of the Balance object. | ||
func (b Balance) GetCoins() sdk.Coins { | ||
return b.Coins | ||
} | ||
|
||
// Validate checks for address and coins correctness. | ||
func (b Balance) Validate() error { | ||
_, err := sdk.AccAddressFromBech32(b.Address) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if len(b.Coins) == 0 { | ||
return fmt.Errorf("empty or nil coins for address %s", b.Address) | ||
} | ||
|
||
seenDenoms := make(map[string]bool) | ||
|
||
// NOTE: we perform a custom validation since the coins.Validate function | ||
// errors on zero balance coins | ||
for _, coin := range b.Coins { | ||
if seenDenoms[coin.Denom] { | ||
return fmt.Errorf("duplicate denomination %s", coin.Denom) | ||
} | ||
|
||
if err := sdk.ValidateDenom(coin.Denom); err != nil { | ||
return err | ||
} | ||
|
||
if coin.IsNegative() { | ||
return fmt.Errorf("coin %s amount is cannot be negative", coin.Denom) | ||
} | ||
Comment on lines
+47
to
+53
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We should put the cheaper call before to avoid wasted CPU cycles and from look at the code flow, most likely coins will be negative than to be within this code path and have invalid denominations. |
||
|
||
seenDenoms[coin.Denom] = true | ||
} | ||
|
||
// sort the coins post validation | ||
b.Coins = b.Coins.Sort() | ||
|
||
return nil | ||
} | ||
|
||
// SanitizeGenesisBalances sorts addresses and coin sets. | ||
func SanitizeGenesisBalances(balances []Balance) []Balance { | ||
sort.Slice(balances, func(i, j int) bool { | ||
addr1, _ := sdk.AccAddressFromBech32(balances[i].Address) | ||
addr2, _ := sdk.AccAddressFromBech32(balances[j].Address) | ||
return bytes.Compare(addr1.Bytes(), addr2.Bytes()) < 0 | ||
}) | ||
|
||
for _, balance := range balances { | ||
balance.Coins = balance.Coins.Sort() | ||
} | ||
|
||
return balances | ||
} | ||
|
||
// GenesisBalancesIterator implements genesis account iteration. | ||
type GenesisBalancesIterator struct{} | ||
|
||
// IterateGenesisBalances iterates over all the genesis balances found in | ||
// appGenesis and invokes a callback on each genesis account. If any call | ||
// returns true, iteration stops. | ||
func (GenesisBalancesIterator) IterateGenesisBalances( | ||
cdc codec.JSONMarshaler, appState map[string]json.RawMessage, cb func(exported.GenesisBalance) (stop bool), | ||
) { | ||
for _, balance := range GetGenesisStateFromAppState(cdc, appState).Balances { | ||
if cb(balance) { | ||
break | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
package types | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
|
||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
) | ||
|
||
func TestBalanceValidate(t *testing.T) { | ||
|
||
testCases := []struct { | ||
name string | ||
balance Balance | ||
expErr bool | ||
}{ | ||
{ | ||
"valid balance", | ||
Balance{ | ||
Address: "cosmos1yq8lgssgxlx9smjhes6ryjasmqmd3ts2559g0t", | ||
Coins: sdk.Coins{sdk.NewInt64Coin("uatom", 1)}, | ||
}, | ||
false, | ||
}, | ||
{"empty balance", Balance{}, true}, | ||
{ | ||
"nil balance coins", | ||
Balance{ | ||
Address: "cosmos1yq8lgssgxlx9smjhes6ryjasmqmd3ts2559g0t", | ||
}, | ||
true, | ||
}, | ||
{ | ||
"dup coins", | ||
Balance{ | ||
Address: "cosmos1yq8lgssgxlx9smjhes6ryjasmqmd3ts2559g0t", | ||
Coins: sdk.Coins{ | ||
sdk.NewInt64Coin("uatom", 1), | ||
sdk.NewInt64Coin("uatom", 1), | ||
}, | ||
}, | ||
true, | ||
}, | ||
{ | ||
"invalid coin denom", | ||
Balance{ | ||
Address: "cosmos1yq8lgssgxlx9smjhes6ryjasmqmd3ts2559g0t", | ||
Coins: sdk.Coins{ | ||
sdk.Coin{Denom: "", Amount: sdk.OneInt()}, | ||
}, | ||
}, | ||
true, | ||
}, | ||
{ | ||
"negative coin", | ||
Balance{ | ||
Address: "cosmos1yq8lgssgxlx9smjhes6ryjasmqmd3ts2559g0t", | ||
Coins: sdk.Coins{ | ||
sdk.Coin{Denom: "uatom", Amount: sdk.NewInt(-1)}, | ||
}, | ||
}, | ||
true, | ||
}, | ||
} | ||
|
||
for _, tc := range testCases { | ||
tc := tc | ||
t.Run(tc.name, func(t *testing.T) { | ||
|
||
err := tc.balance.Validate() | ||
|
||
if tc.expErr { | ||
require.Error(t, err) | ||
} else { | ||
require.NoError(t, err) | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why are we ignoring the error from here? Feels like we should be panicking instead.