Skip to content

Commit

Permalink
refactor!: Rename AccAddressFromHex (#11888)
Browse files Browse the repository at this point in the history
## 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)
  • Loading branch information
alexanderbez authored May 6, 2022
1 parent c83dc20 commit 8ebd28c
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 14 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
4 changes: 2 additions & 2 deletions simapp/test_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -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
}
Expand Down
15 changes: 12 additions & 3 deletions types/address.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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)
Expand Down
6 changes: 4 additions & 2 deletions types/address_race_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package types_test

import (
"encoding/binary"
"fmt"
"testing"
"time"

Expand Down Expand Up @@ -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)

Expand Down
12 changes: 6 additions & 6 deletions types/address_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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() {
Expand Down Expand Up @@ -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() {
Expand Down Expand Up @@ -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"
Expand Down
2 changes: 1 addition & 1 deletion x/auth/client/cli/tx_multisign.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down

0 comments on commit 8ebd28c

Please sign in to comment.