Relaxed address strength restrictions for legacy 20 byte addresses (#772)

* 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 <channa@figure.com>
This commit is contained in:
Ira Miller
2022-03-03 10:16:02 -07:00
committed by GitHub
parent c3aeb1b6f9
commit e03c7f4f88
3 changed files with 43 additions and 4 deletions

View File

@@ -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)

View File

@@ -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)
@@ -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

View File

@@ -27,12 +27,36 @@ 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
@@ -40,6 +64,17 @@ func TestGetContractByCreatedSecondaryIndexKey(t *testing.T) {
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,
}