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:
Aleksandr Bezobchuk
2022-05-06 22:09:36 +00:00
committed by GitHub
parent c83dc20e8a
commit 8ebd28c9f3
6 changed files with 26 additions and 14 deletions
+12 -3
View File
@@ -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)
+4 -2
View File
@@ -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)
+6 -6
View File
@@ -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"