forked from cosmos/cosmos-sdk
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
cleaned up UBD unit test and incremented consensus version
- Loading branch information
Showing
4 changed files
with
136 additions
and
78 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
// This file contains utility testing functions from SDK 47 that are required for the UBD migration unit test | ||
package v3 | ||
|
||
import ( | ||
"bytes" | ||
"encoding/hex" | ||
"fmt" | ||
"strconv" | ||
|
||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
) | ||
|
||
// 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, _ := AccAddressFromHexUnsafe(buffer.String()) | ||
bech := res.String() | ||
addr, _ := CheckHexConversion(buffer.String(), bech) | ||
|
||
addresses = append(addresses, addr) | ||
buffer.Reset() | ||
} | ||
|
||
return addresses | ||
} | ||
|
||
func CheckHexConversion(addr string, bech string) (sdk.AccAddress, error) { | ||
res, err := 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 | ||
} | ||
|
||
// ConvertAddrsToValAddrs converts the provided addresses to ValAddress. | ||
func ConvertAddrsToValAddrs(addrs []sdk.AccAddress) []sdk.ValAddress { | ||
valAddrs := make([]sdk.ValAddress, len(addrs)) | ||
|
||
for i, addr := range addrs { | ||
valAddrs[i] = sdk.ValAddress(addr) | ||
} | ||
|
||
return valAddrs | ||
} | ||
|
||
// AccAddressFromHexUnsafe creates an AccAddress from a HEX-encoded string. | ||
// | ||
// Note, this function is considered unsafe as it may produce an AccAddress from | ||
// otherwise invalid input, such as a transaction hash. Please use | ||
// AccAddressFromBech32. | ||
func AccAddressFromHexUnsafe(address string) (addr sdk.AccAddress, err error) { | ||
bz, err := addressBytesFromHexString(address) | ||
return sdk.AccAddress(bz), err | ||
} | ||
|
||
func addressBytesFromHexString(address string) ([]byte, error) { | ||
if len(address) == 0 { | ||
return nil, fmt.Errorf("empty hex address") | ||
} | ||
|
||
return hex.DecodeString(address) | ||
} |
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