Skip to content

Commit

Permalink
Merge pull request #80 from binance-chain/release/0.25.0-binance.9
Browse files Browse the repository at this point in the history
Release v0.25.0 binance.9
  • Loading branch information
rickyyangz authored Mar 9, 2019
2 parents ce8925b + ef098b7 commit be0e7ed
Show file tree
Hide file tree
Showing 9 changed files with 58 additions and 23 deletions.
3 changes: 1 addition & 2 deletions store/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ func (m List) Push(value interface{}) {
// CONTRACT: No writes may happen within a domain while iterating over it.
func (m List) Iterate(ptr interface{}, fn func(uint64) bool) {
iter := sdk.KVStorePrefixIterator(m.store, []byte{0x01})
defer iter.Close()
for ; iter.Valid(); iter.Next() {
v := iter.Value()
m.cdc.MustUnmarshalBinaryLengthPrefixed(v, ptr)
Expand All @@ -96,8 +97,6 @@ func (m List) Iterate(ptr interface{}, fn func(uint64) bool) {
break
}
}

iter.Close()
}

func subspace(prefix []byte) (start, end []byte) {
Expand Down
6 changes: 6 additions & 0 deletions types/address.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@ func AccAddressFromHex(address string) (addr AccAddress, err error) {
return nil, err
}

if len(bz) != AddrLen {
return nil, fmt.Errorf("invalid address length, %s", address)
}
return AccAddress(bz), nil
}

Expand All @@ -61,6 +64,9 @@ func AccAddressFromBech32(address string) (addr AccAddress, err error) {
return nil, err
}

if len(bz) != AddrLen {
return nil, fmt.Errorf("invalid address length, %s", address)
}
return AccAddress(bz), nil
}

Expand Down
4 changes: 2 additions & 2 deletions x/bank/msgs.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ type Input struct {

// ValidateBasic - validate transaction input
func (in Input) ValidateBasic() sdk.Error {
if len(in.Address) == 0 {
if len(in.Address) != sdk.AddrLen {
return sdk.ErrInvalidAddress(in.Address.String())
}
if !in.Coins.IsValid() {
Expand Down Expand Up @@ -128,7 +128,7 @@ type Output struct {

// ValidateBasic - validate transaction output
func (out Output) ValidateBasic() sdk.Error {
if len(out.Address) == 0 {
if len(out.Address) != sdk.AddrLen {
return sdk.ErrInvalidAddress(out.Address.String())
}
if !out.Coins.IsValid() {
Expand Down
46 changes: 38 additions & 8 deletions x/bank/msgs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import (

"github.com/stretchr/testify/require"

"github.com/tendermint/tendermint/crypto/secp256k1"

sdk "github.com/cosmos/cosmos-sdk/types"
)

Expand All @@ -26,8 +28,10 @@ func TestMsgSendRoute(t *testing.T) {
}

func TestInputValidation(t *testing.T) {
addr1 := sdk.AccAddress([]byte{1, 2})
addr2 := sdk.AccAddress([]byte{7, 8})
addr1 := sdk.AccAddress(secp256k1.GenPrivKey().PubKey().Address())
addr2 := sdk.AccAddress(secp256k1.GenPrivKey().PubKey().Address())
shortAddr := sdk.AccAddress([]byte{1, 2})
longAddr := sdk.AccAddress(append(secp256k1.GenPrivKey().PubKey().Address(), 1, 2))
someCoins := sdk.Coins{sdk.NewCoin("atom", 123)}
multiCoins := sdk.Coins{sdk.NewCoin("atom", 123), sdk.NewCoin("eth", 20)}

Expand All @@ -49,6 +53,8 @@ func TestInputValidation(t *testing.T) {
{true, NewInput(addr2, multiCoins)},

{false, NewInput(emptyAddr, someCoins)}, // empty address
{false, NewInput(shortAddr, someCoins)}, // invalid address length
{false, NewInput(longAddr, someCoins)}, // invalid address length
{false, NewInput(addr1, emptyCoins)}, // invalid coins
{false, NewInput(addr1, emptyCoins2)}, // invalid coins
{false, NewInput(addr1, someEmptyCoins)}, // invalid coins
Expand All @@ -68,8 +74,10 @@ func TestInputValidation(t *testing.T) {
}

func TestOutputValidation(t *testing.T) {
addr1 := sdk.AccAddress([]byte{1, 2})
addr2 := sdk.AccAddress([]byte{7, 8})
addr1 := sdk.AccAddress(secp256k1.GenPrivKey().PubKey().Address())
addr2 := sdk.AccAddress(secp256k1.GenPrivKey().PubKey().Address())
shortAddr := sdk.AccAddress([]byte{1, 2})
longAddr := sdk.AccAddress(append(secp256k1.GenPrivKey().PubKey().Address(), 1, 2))
someCoins := sdk.Coins{sdk.NewCoin("atom", 123)}
multiCoins := sdk.Coins{sdk.NewCoin("atom", 123), sdk.NewCoin("eth", 20)}

Expand All @@ -90,7 +98,10 @@ func TestOutputValidation(t *testing.T) {
{true, NewOutput(addr2, someCoins)},
{true, NewOutput(addr2, multiCoins)},

{false, NewOutput(emptyAddr, someCoins)}, // empty address
{false, NewOutput(emptyAddr, someCoins)}, // empty address
{false, NewOutput(shortAddr, someCoins)}, // invalid address length
{false, NewOutput(longAddr, someCoins)}, // invalid address length

{false, NewOutput(addr1, emptyCoins)}, // invalid coins
{false, NewOutput(addr1, emptyCoins2)}, // invalid coins
{false, NewOutput(addr1, someEmptyCoins)}, // invalid coins
Expand All @@ -110,8 +121,10 @@ func TestOutputValidation(t *testing.T) {
}

func TestMsgSendValidation(t *testing.T) {
addr1 := sdk.AccAddress([]byte{1, 2})
addr2 := sdk.AccAddress([]byte{7, 8})
addr1 := sdk.AccAddress(secp256k1.GenPrivKey().PubKey().Address())
addr2 := sdk.AccAddress(secp256k1.GenPrivKey().PubKey().Address())
shortAddr := sdk.AccAddress([]byte{1, 2})
longAddr := sdk.AccAddress(append(secp256k1.GenPrivKey().PubKey().Address(), 1, 2))
atom123 := sdk.Coins{sdk.NewCoin("atom", 123)}
atom124 := sdk.Coins{sdk.NewCoin("atom", 124)}
eth123 := sdk.Coins{sdk.NewCoin("eth", 123)}
Expand All @@ -135,11 +148,28 @@ func TestMsgSendValidation(t *testing.T) {
{false, MsgSend{Outputs: []Output{output1}}}, // just output
{false, MsgSend{
Inputs: []Input{NewInput(emptyAddr, atom123)}, // invalid input
Outputs: []Output{output1}}},
Outputs: []Output{output1}},
},
{false, MsgSend{
Inputs: []Input{NewInput(shortAddr, atom123)}, // invalid input
Outputs: []Output{output1}},
},
{false, MsgSend{
Inputs: []Input{NewInput(longAddr, atom123)}, // invalid input
Outputs: []Output{output1}},
},
{false, MsgSend{
Inputs: []Input{input1},
Outputs: []Output{{emptyAddr, atom123}}}, // invalid output
},
{false, MsgSend{
Inputs: []Input{input1},
Outputs: []Output{{shortAddr, atom123}}}, // invalid output
},
{false, MsgSend{
Inputs: []Input{input1},
Outputs: []Output{{longAddr, atom123}}}, // invalid output
},
{false, MsgSend{
Inputs: []Input{input1},
Outputs: []Output{output2}}, // amounts dont match
Expand Down
4 changes: 2 additions & 2 deletions x/gov/client/cli/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ func GetCmdSubmitProposal(cdc *codec.Codec) *cobra.Command {
Long: strings.TrimSpace(`
Submit a proposal along with an initial deposit. Proposal title, description, type and deposit can be given directly or through a proposal JSON file. For example:
$ gaiacli gov submit-proposal --proposal="path/to/proposal.json"
$ CLI gov submit-proposal --proposal="path/to/proposal.json"
where proposal.json contains:
Expand All @@ -74,7 +74,7 @@ where proposal.json contains:
is equivalent to
$ gaiacli gov submit-proposal --title="Test Proposal" --description="My awesome proposal" --type="Text" --deposit="1000:test"
$ CLI gov submit-proposal --title="Test Proposal" --description="My awesome proposal" --type="Text" --deposit="1000:test"
`),
RunE: func(cmd *cobra.Command, args []string) error {
proposal, err := parseSubmitProposalFlags()
Expand Down
8 changes: 2 additions & 6 deletions x/gov/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -445,7 +445,7 @@ func (keeper Keeper) GetDeposits(ctx sdk.Context, proposalID int64) sdk.Iterator
func (keeper Keeper) RefundDeposits(ctx sdk.Context, proposalID int64) {
store := ctx.KVStore(keeper.storeKey)
depositsIterator := keeper.GetDeposits(ctx, proposalID)

defer depositsIterator.Close()
for ; depositsIterator.Valid(); depositsIterator.Next() {
deposit := &Deposit{}
keeper.cdc.MustUnmarshalBinaryLengthPrefixed(depositsIterator.Value(), deposit)
Expand All @@ -458,20 +458,16 @@ func (keeper Keeper) RefundDeposits(ctx sdk.Context, proposalID int64) {
keeper.pool.AddAddrs([]sdk.AccAddress{deposit.Depositer})
store.Delete(depositsIterator.Key())
}

depositsIterator.Close()
}

// Deletes all the deposits on a specific proposal without refunding them
func (keeper Keeper) DeleteDeposits(ctx sdk.Context, proposalID int64) {
store := ctx.KVStore(keeper.storeKey)
depositsIterator := keeper.GetDeposits(ctx, proposalID)

defer depositsIterator.Close()
for ; depositsIterator.Valid(); depositsIterator.Next() {
store.Delete(depositsIterator.Key())
}

depositsIterator.Close()
}

// DistributeDeposits distributes deposits to proposer
Expand Down
2 changes: 2 additions & 0 deletions x/gov/queryable.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ func queryDeposits(ctx sdk.Context, path []string, req abci.RequestQuery, keeper

var deposits []Deposit
depositsIterator := keeper.GetDeposits(ctx, params.ProposalID)
defer depositsIterator.Close()
for ; depositsIterator.Valid(); depositsIterator.Next() {
deposit := Deposit{}
keeper.cdc.MustUnmarshalBinaryLengthPrefixed(depositsIterator.Value(), &deposit)
Expand Down Expand Up @@ -154,6 +155,7 @@ func queryVotes(ctx sdk.Context, path []string, req abci.RequestQuery, keeper Ke

var votes []Vote
votesIterator := keeper.GetVotes(ctx, params.ProposalID)
defer votesIterator.Close()
for ; votesIterator.Valid(); votesIterator.Next() {
vote := Vote{}
keeper.cdc.MustUnmarshalBinaryLengthPrefixed(votesIterator.Value(), &vote)
Expand Down
6 changes: 3 additions & 3 deletions x/stake/keeper/sdk_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ var _ sdk.ValidatorSet = Keeper{}
func (k Keeper) IterateValidators(ctx sdk.Context, fn func(index int64, validator sdk.Validator) (stop bool)) {
store := ctx.KVStore(k.storeKey)
iterator := sdk.KVStorePrefixIterator(store, ValidatorsKey)
defer iterator.Close()
i := int64(0)
for ; iterator.Valid(); iterator.Next() {
addr := iterator.Key()[1:]
Expand All @@ -24,13 +25,13 @@ func (k Keeper) IterateValidators(ctx sdk.Context, fn func(index int64, validato
}
i++
}
iterator.Close()
}

// iterate through the active validator set and perform the provided function
func (k Keeper) IterateValidatorsBonded(ctx sdk.Context, fn func(index int64, validator sdk.Validator) (stop bool)) {
store := ctx.KVStore(k.storeKey)
iterator := sdk.KVStorePrefixIterator(store, LastValidatorPowerKey)
defer iterator.Close()
i := int64(0)
for ; iterator.Valid(); iterator.Next() {
address := AddressFromLastValidatorPowerKey(iterator.Key())
Expand All @@ -45,7 +46,6 @@ func (k Keeper) IterateValidatorsBonded(ctx sdk.Context, fn func(index int64, va
}
i++
}
iterator.Close()
}

// get the sdk.validator for a particular address
Expand Down Expand Up @@ -113,6 +113,7 @@ func (k Keeper) IterateDelegations(ctx sdk.Context, delAddr sdk.AccAddress,
store := ctx.KVStore(k.storeKey)
delegatorPrefixKey := GetDelegationsKey(delAddr)
iterator := sdk.KVStorePrefixIterator(store, delegatorPrefixKey) //smallest to largest
defer iterator.Close()
for i := int64(0); iterator.Valid(); iterator.Next() {
del := types.MustUnmarshalDelegation(k.cdc, iterator.Key(), iterator.Value())
stop := fn(i, del)
Expand All @@ -121,5 +122,4 @@ func (k Keeper) IterateDelegations(ctx sdk.Context, delAddr sdk.AccAddress,
}
i++
}
iterator.Close()
}
2 changes: 2 additions & 0 deletions x/stake/keeper/val_state_change.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ func (k Keeper) ApplyAndReturnValidatorSetUpdates(ctx sdk.Context) (updates []ab

// Iterate over validators, highest power to lowest.
iterator := sdk.KVStoreReversePrefixIterator(store, ValidatorsByPowerIndexKey)
defer iterator.Close()
count := 0
for ; iterator.Valid() && count < int(maxValidators); iterator.Next() {

Expand Down Expand Up @@ -256,6 +257,7 @@ func (k Keeper) getLastValidatorsByAddr(ctx sdk.Context) validatorsByAddr {
last := make(validatorsByAddr)
store := ctx.KVStore(k.storeKey)
iterator := sdk.KVStorePrefixIterator(store, LastValidatorPowerKey)
defer iterator.Close()
for ; iterator.Valid(); iterator.Next() {
var operator [sdk.AddrLen]byte
copy(operator[:], iterator.Key()[1:])
Expand Down

0 comments on commit be0e7ed

Please sign in to comment.