-
Notifications
You must be signed in to change notification settings - Fork 3.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' into bez/12189-vesting-base-fix
- Loading branch information
Showing
17 changed files
with
469 additions
and
256 deletions.
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
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,96 @@ | ||
package sims | ||
|
||
import ( | ||
"bytes" | ||
"fmt" | ||
"strconv" | ||
|
||
"cosmossdk.io/math" | ||
"github.com/cosmos/cosmos-sdk/crypto/keys/ed25519" | ||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
bankkeeper "github.com/cosmos/cosmos-sdk/x/bank/keeper" | ||
minttypes "github.com/cosmos/cosmos-sdk/x/mint/types" | ||
stakingkeeper "github.com/cosmos/cosmos-sdk/x/staking/keeper" | ||
) | ||
|
||
type GenerateAccountStrategy func(int) []sdk.AccAddress | ||
|
||
// AddTestAddrsIncremental constructs and returns accNum amount of accounts with an initial balance of accAmt in random order | ||
func AddTestAddrsIncremental(bankKeeper bankkeeper.Keeper, stakingKeeper *stakingkeeper.Keeper, ctx sdk.Context, accNum int, accAmt math.Int) []sdk.AccAddress { | ||
return addTestAddrs(bankKeeper, stakingKeeper, ctx, accNum, accAmt, CreateIncrementalAccounts) | ||
} | ||
|
||
func addTestAddrs(bankKeeper bankkeeper.Keeper, stakingKeeper *stakingkeeper.Keeper, ctx sdk.Context, accNum int, accAmt math.Int, strategy GenerateAccountStrategy) []sdk.AccAddress { | ||
testAddrs := strategy(accNum) | ||
initCoins := sdk.NewCoins(sdk.NewCoin(stakingKeeper.BondDenom(ctx), accAmt)) | ||
|
||
for _, addr := range testAddrs { | ||
initAccountWithCoins(bankKeeper, ctx, addr, initCoins) | ||
} | ||
|
||
return testAddrs | ||
} | ||
|
||
func initAccountWithCoins(bankKeeper bankkeeper.Keeper, ctx sdk.Context, addr sdk.AccAddress, coins sdk.Coins) { | ||
if err := bankKeeper.MintCoins(ctx, minttypes.ModuleName, coins); err != nil { | ||
panic(err) | ||
} | ||
|
||
if err := bankKeeper.SendCoinsFromModuleToAccount(ctx, minttypes.ModuleName, addr, coins); err != nil { | ||
panic(err) | ||
} | ||
} | ||
|
||
// createIncrementalAccounts is a strategy used by addTestAddrs() in order to generated addresses in ascending order. | ||
func CreateIncrementalAccounts(accNum int) []sdk.AccAddress { | ||
var addresses []sdk.AccAddress | ||
var buffer bytes.Buffer | ||
|
||
// start at 100 so we can make up to 999 test addresses with valid test addresses | ||
for i := 100; i < (accNum + 100); i++ { | ||
numString := strconv.Itoa(i) | ||
buffer.WriteString("A58856F0FD53BF058B4909A21AEC019107BA6") // base address string | ||
|
||
buffer.WriteString(numString) // adding on final two digits to make addresses unique | ||
res, _ := sdk.AccAddressFromHexUnsafe(buffer.String()) | ||
bech := res.String() | ||
addr, _ := TestAddr(buffer.String(), bech) | ||
|
||
addresses = append(addresses, addr) | ||
buffer.Reset() | ||
} | ||
|
||
return addresses | ||
} | ||
|
||
// CreateRandomAccounts is a strategy used by addTestAddrs() in order to generated addresses in random order. | ||
func CreateRandomAccounts(accNum int) []sdk.AccAddress { | ||
testAddrs := make([]sdk.AccAddress, accNum) | ||
for i := 0; i < accNum; i++ { | ||
pk := ed25519.GenPrivKey().PubKey() | ||
testAddrs[i] = sdk.AccAddress(pk.Address()) | ||
} | ||
|
||
return testAddrs | ||
} | ||
|
||
func TestAddr(addr string, bech string) (sdk.AccAddress, error) { | ||
res, err := sdk.AccAddressFromHexUnsafe(addr) | ||
if err != nil { | ||
return nil, err | ||
} | ||
bechexpected := res.String() | ||
if bech != bechexpected { | ||
return nil, fmt.Errorf("bech encoding doesn't match reference") | ||
} | ||
|
||
bechres, err := sdk.AccAddressFromBech32(bech) | ||
if err != nil { | ||
return nil, err | ||
} | ||
if !bytes.Equal(bechres, res) { | ||
return nil, err | ||
} | ||
|
||
return res, nil | ||
} |
Oops, something went wrong.