refactor!: Rename AccAddressFromHex (#11888)
## Description Closes: #11881 --- ### Author Checklist *All items are required. Please add a note to the item if the item is not applicable and please add links to any relevant follow up issues.* I have... - [ ] included the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title - [ ] added `!` to the type prefix if API or client breaking change - [ ] targeted the correct branch (see [PR Targeting](https://github.com/cosmos/cosmos-sdk/blob/main/CONTRIBUTING.md#pr-targeting)) - [ ] provided a link to the relevant issue or specification - [ ] followed the guidelines for [building modules](https://github.com/cosmos/cosmos-sdk/blob/main/docs/building-modules) - [ ] included the necessary unit and integration [tests](https://github.com/cosmos/cosmos-sdk/blob/main/CONTRIBUTING.md#testing) - [ ] added a changelog entry to `CHANGELOG.md` - [ ] included comments for [documenting Go code](https://blog.golang.org/godoc) - [ ] updated the relevant documentation or specification - [ ] reviewed "Files changed" and left comments if necessary - [ ] confirmed all CI checks have passed ### Reviewers Checklist *All items are required. Please add a note if the item is not applicable and please add your handle next to the items reviewed if you only reviewed selected items.* I have... - [ ] confirmed the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title - [ ] confirmed `!` in the type prefix if API or client breaking change - [ ] confirmed all author checklist items have been addressed - [ ] reviewed state machine logic - [ ] reviewed API design and naming - [ ] reviewed documentation is accurate - [ ] reviewed tests and test coverage - [ ] manually tested (if applicable)
This commit is contained in:
parent
c83dc20e8a
commit
8ebd28c9f3
|
@ -89,6 +89,7 @@ Ref: https://keepachangelog.com/en/1.0.0/
|
|||
|
||||
### API Breaking Changes
|
||||
|
||||
* (types ) [#11881](https://github.com/cosmos/cosmos-sdk/issues/11881) Rename `AccAddressFromHex` to `AccAddressFromHexUnsafe`.
|
||||
* (types) [#11788](https://github.com/cosmos/cosmos-sdk/pull/11788) The `Int` and `Uint` types have been moved to their own dedicated module, `math`. Aliases are kept in the SDK's root `types` package, however, it is encouraged to utilize the new `math` module. As a result, the `Int#ToDec` API has been removed.
|
||||
* (grpc) [\#11642](https://github.com/cosmos/cosmos-sdk/pull/11642) The `RegisterTendermintService` method in the `tmservice` package now requires a `abciQueryFn` query function parameter.
|
||||
* [\#11496](https://github.com/cosmos/cosmos-sdk/pull/11496) Refactor abstractions for snapshot and pruning; snapshot intervals eventually pruned; unit tests.
|
||||
|
|
|
@ -309,7 +309,7 @@ func createIncrementalAccounts(accNum int) []sdk.AccAddress {
|
|||
buffer.WriteString("A58856F0FD53BF058B4909A21AEC019107BA6") // base address string
|
||||
|
||||
buffer.WriteString(numString) // adding on final two digits to make addresses unique
|
||||
res, _ := sdk.AccAddressFromHex(buffer.String())
|
||||
res, _ := sdk.AccAddressFromHexUnsafe(buffer.String())
|
||||
bech := res.String()
|
||||
addr, _ := TestAddr(buffer.String(), bech)
|
||||
|
||||
|
@ -377,7 +377,7 @@ func ConvertAddrsToValAddrs(addrs []sdk.AccAddress) []sdk.ValAddress {
|
|||
}
|
||||
|
||||
func TestAddr(addr string, bech string) (sdk.AccAddress, error) {
|
||||
res, err := sdk.AccAddressFromHex(addr)
|
||||
res, err := sdk.AccAddressFromHexUnsafe(addr)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
|
|
@ -85,6 +85,11 @@ var (
|
|||
valAddrCache *simplelru.LRU
|
||||
)
|
||||
|
||||
// sentinel errors
|
||||
var (
|
||||
ErrEmptyHexAddress = errors.New("decoding address from hex string failed: empty address")
|
||||
)
|
||||
|
||||
func init() {
|
||||
var err error
|
||||
// in total the cache size is 61k entries. Key is 32 bytes and value is around 50-70 bytes.
|
||||
|
@ -124,8 +129,12 @@ var _ Address = ConsAddress{}
|
|||
// When marshaled to a string or JSON, it uses Bech32.
|
||||
type AccAddress []byte
|
||||
|
||||
// AccAddressFromHex creates an AccAddress from a hex string.
|
||||
func AccAddressFromHex(address string) (addr AccAddress, err error) {
|
||||
// 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 AccAddress, err error) {
|
||||
bz, err := addressBytesFromHexString(address)
|
||||
return AccAddress(bz), err
|
||||
}
|
||||
|
@ -642,7 +651,7 @@ func GetFromBech32(bech32str, prefix string) ([]byte, error) {
|
|||
|
||||
func addressBytesFromHexString(address string) ([]byte, error) {
|
||||
if len(address) == 0 {
|
||||
return nil, errors.New("decoding Bech32 address failed: must provide an address")
|
||||
return nil, ErrEmptyHexAddress
|
||||
}
|
||||
|
||||
return hex.DecodeString(address)
|
||||
|
|
|
@ -2,7 +2,6 @@ package types_test
|
|||
|
||||
import (
|
||||
"encoding/binary"
|
||||
"fmt"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
|
@ -33,19 +32,22 @@ func addressStringCaller(require *require.Assertions, prefix byte, max uint32, c
|
|||
}
|
||||
|
||||
func (s *addressTestSuite) TestAddressRace() {
|
||||
fmt.Println("starting test")
|
||||
if testing.Short() {
|
||||
s.T().Skip("AddressRace test is not short")
|
||||
}
|
||||
|
||||
workers := 4
|
||||
done := make(chan bool, workers)
|
||||
cancel := make(chan bool)
|
||||
|
||||
for i := byte(1); i <= 2; i++ { // workes which will loop in first 100 addresses
|
||||
go addressStringCaller(s.Require(), i, 100, cancel, done)
|
||||
}
|
||||
|
||||
for i := byte(1); i <= 2; i++ { // workes which will generate 1e6 new addresses
|
||||
go addressStringCaller(s.Require(), i, 1000000, cancel, done)
|
||||
}
|
||||
|
||||
<-time.After(time.Millisecond * 30)
|
||||
close(cancel)
|
||||
|
||||
|
|
|
@ -105,13 +105,13 @@ func (s *addressTestSuite) TestRandBech32AccAddrConsistency() {
|
|||
s.Require().Equal(acc, res)
|
||||
|
||||
str = hex.EncodeToString(acc)
|
||||
res, err = types.AccAddressFromHex(str)
|
||||
res, err = types.AccAddressFromHexUnsafe(str)
|
||||
s.Require().Nil(err)
|
||||
s.Require().Equal(acc, res)
|
||||
}
|
||||
|
||||
for _, str := range invalidStrs {
|
||||
_, err := types.AccAddressFromHex(str)
|
||||
_, err := types.AccAddressFromHexUnsafe(str)
|
||||
s.Require().NotNil(err)
|
||||
|
||||
_, err = types.AccAddressFromBech32(str)
|
||||
|
@ -121,8 +121,8 @@ func (s *addressTestSuite) TestRandBech32AccAddrConsistency() {
|
|||
s.Require().NotNil(err)
|
||||
}
|
||||
|
||||
_, err := types.AccAddressFromHex("")
|
||||
s.Require().Equal("decoding Bech32 address failed: must provide an address", err.Error())
|
||||
_, err := types.AccAddressFromHexUnsafe("")
|
||||
s.Require().Equal(types.ErrEmptyHexAddress, err)
|
||||
}
|
||||
|
||||
func (s *addressTestSuite) TestValAddr() {
|
||||
|
@ -163,7 +163,7 @@ func (s *addressTestSuite) TestValAddr() {
|
|||
|
||||
// test empty string
|
||||
_, err := types.ValAddressFromHex("")
|
||||
s.Require().Equal("decoding Bech32 address failed: must provide an address", err.Error())
|
||||
s.Require().Equal(types.ErrEmptyHexAddress, err)
|
||||
}
|
||||
|
||||
func (s *addressTestSuite) TestConsAddress() {
|
||||
|
@ -203,7 +203,7 @@ func (s *addressTestSuite) TestConsAddress() {
|
|||
|
||||
// test empty string
|
||||
_, err := types.ConsAddressFromHex("")
|
||||
s.Require().Equal("decoding Bech32 address failed: must provide an address", err.Error())
|
||||
s.Require().Equal(types.ErrEmptyHexAddress, err)
|
||||
}
|
||||
|
||||
const letterBytes = "abcdefghijklmnopqrstuvwxyz"
|
||||
|
|
|
@ -139,7 +139,7 @@ func makeMultiSignCmd() func(cmd *cobra.Command, args []string) (err error) {
|
|||
|
||||
err = signing.VerifySignature(sig.PubKey, signingData, sig.Data, txCfg.SignModeHandler(), txBuilder.GetTx())
|
||||
if err != nil {
|
||||
addr, _ := sdk.AccAddressFromHex(sig.PubKey.Address().String())
|
||||
addr, _ := sdk.AccAddressFromHexUnsafe(sig.PubKey.Address().String())
|
||||
return fmt.Errorf("couldn't verify signature for address %s", addr)
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue