Skip to content

Commit

Permalink
Relaxed address strength restrictions for legacy 20 byte addresses (#772
Browse files Browse the repository at this point in the history
)

* Use the length of contractAddr to support variable length addresses

* Add contract address len to index

* Add changelog entry

* Add tests to assure old address length is still supported

Co-authored-by: Carlton Hanna <[email protected]>
  • Loading branch information
iramiller and channa-figure authored Mar 3, 2022
1 parent c3aeb1b commit e03c7f4
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 4 deletions.
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@

## [Unreleased](https://github.com/CosmWasm/wasmd/tree/HEAD)

[Full Changelog](https://github.com/CosmWasm/wasmd/compare/v0.23.0...HEAD)
**Fixed bugs + Api Breaking:**
- Add support for old contract addresses of length 20 [\#758](https://github.com/CosmWasm/wasmd/issues/758)

[Full Changelog](https://github.com/CosmWasm/wasmd/compare/v0.23.0...HEAD)

## [v0.23.0](https://github.com/CosmWasm/wasmd/tree/v0.23.0) (2022-01-28)

Expand Down
6 changes: 4 additions & 2 deletions x/wasm/types/keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,8 @@ func GetContractStorePrefix(addr sdk.AccAddress) []byte {
func GetContractByCreatedSecondaryIndexKey(contractAddr sdk.AccAddress, c ContractCodeHistoryEntry) []byte {
prefix := GetContractByCodeIDSecondaryIndexPrefix(c.CodeID)
prefixLen := len(prefix)
r := make([]byte, prefixLen+AbsoluteTxPositionLen+ContractAddrLen)
contractAddrLen := len(contractAddr)
r := make([]byte, prefixLen+AbsoluteTxPositionLen+contractAddrLen)
copy(r[0:], prefix)
copy(r[prefixLen:], c.Updated.Bytes())
copy(r[prefixLen+AbsoluteTxPositionLen:], contractAddr)
Expand Down Expand Up @@ -87,7 +88,8 @@ func GetContractCodeHistoryElementKey(contractAddr sdk.AccAddress, pos uint64) [
// GetContractCodeHistoryElementPrefix returns the key prefix for a contract code history entry: `<prefix><contractAddr>`
func GetContractCodeHistoryElementPrefix(contractAddr sdk.AccAddress) []byte {
prefixLen := len(ContractCodeHistoryElementPrefix)
r := make([]byte, prefixLen+ContractAddrLen)
contractAddrLen := len(contractAddr)
r := make([]byte, prefixLen+contractAddrLen)
copy(r[0:], ContractCodeHistoryElementPrefix)
copy(r[prefixLen:], contractAddr)
return r
Expand Down
37 changes: 36 additions & 1 deletion x/wasm/types/keys_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,19 +27,54 @@ func TestGetContractByCodeIDSecondaryIndexPrefix(t *testing.T) {
}
}

func TestGetContractCodeHistoryElementPrefix(t *testing.T) {

// test that contract addresses of 20 length are still supported
addr := bytes.Repeat([]byte{4}, 20)
got := GetContractCodeHistoryElementPrefix(addr)
exp := []byte{5, // prefix
4, 4, 4, 4, 4, 4, 4, 4, 4, 4, // address 20 bytes
4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
}
assert.Equal(t, exp, got)

addr = bytes.Repeat([]byte{4}, ContractAddrLen)
got = GetContractCodeHistoryElementPrefix(addr)
exp = []byte{5, // prefix
4, 4, 4, 4, 4, 4, 4, 4, 4, 4, // address 32 bytes
4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
4, 4,
}
assert.Equal(t, exp, got)
}

func TestGetContractByCreatedSecondaryIndexKey(t *testing.T) {
e := ContractCodeHistoryEntry{
CodeID: 1,
Updated: &AbsoluteTxPosition{2 + 1<<(8*7), 3 + 1<<(8*7)},
}
addr := bytes.Repeat([]byte{4}, ContractAddrLen)

// test that contract addresses of 20 length are still supported
addr := bytes.Repeat([]byte{4}, 20)
got := GetContractByCreatedSecondaryIndexKey(addr, e)
exp := []byte{6, // prefix
0, 0, 0, 0, 0, 0, 0, 1, // codeID
1, 0, 0, 0, 0, 0, 0, 2, // height
1, 0, 0, 0, 0, 0, 0, 3, // index
4, 4, 4, 4, 4, 4, 4, 4, 4, 4, // address 32 bytes
4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
}
assert.Equal(t, exp, got)

addr = bytes.Repeat([]byte{4}, ContractAddrLen)
got = GetContractByCreatedSecondaryIndexKey(addr, e)
exp = []byte{6, // prefix
0, 0, 0, 0, 0, 0, 0, 1, // codeID
1, 0, 0, 0, 0, 0, 0, 2, // height
1, 0, 0, 0, 0, 0, 0, 3, // index
4, 4, 4, 4, 4, 4, 4, 4, 4, 4, // address 32 bytes
4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
4, 4,
}
Expand Down

0 comments on commit e03c7f4

Please sign in to comment.