From a0dc5605f1a7f378bba5d26e96260f8b9fb4b383 Mon Sep 17 00:00:00 2001 From: Carlton Hanna Date: Tue, 15 Feb 2022 14:37:06 -0700 Subject: [PATCH 1/4] Use the length of contractAddr to support variable length addresses --- x/wasm/types/keys.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/x/wasm/types/keys.go b/x/wasm/types/keys.go index 982bf1a069..c2f5e84d12 100644 --- a/x/wasm/types/keys.go +++ b/x/wasm/types/keys.go @@ -87,7 +87,7 @@ func GetContractCodeHistoryElementKey(contractAddr sdk.AccAddress, pos uint64) [ // GetContractCodeHistoryElementPrefix returns the key prefix for a contract code history entry: `` func GetContractCodeHistoryElementPrefix(contractAddr sdk.AccAddress) []byte { prefixLen := len(ContractCodeHistoryElementPrefix) - r := make([]byte, prefixLen+ContractAddrLen) + r := make([]byte, prefixLen+len(contractAddr)) copy(r[0:], ContractCodeHistoryElementPrefix) copy(r[prefixLen:], contractAddr) return r From 773eef810d79d9fb47cce49e7f0cc042aed051eb Mon Sep 17 00:00:00 2001 From: Carlton Hanna Date: Tue, 15 Feb 2022 15:06:52 -0700 Subject: [PATCH 2/4] Add contract address len to index --- x/wasm/types/keys.go | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/x/wasm/types/keys.go b/x/wasm/types/keys.go index c2f5e84d12..fb636ef4f3 100644 --- a/x/wasm/types/keys.go +++ b/x/wasm/types/keys.go @@ -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: `` func GetContractCodeHistoryElementPrefix(contractAddr sdk.AccAddress) []byte { prefixLen := len(ContractCodeHistoryElementPrefix) - r := make([]byte, prefixLen+len(contractAddr)) + contractAddrLen := len(contractAddr) + r := make([]byte, prefixLen+contractAddrLen) copy(r[0:], ContractCodeHistoryElementPrefix) copy(r[prefixLen:], contractAddr) return r From c4a6940dafd5cebea46d33d0da83ce5697af3623 Mon Sep 17 00:00:00 2001 From: Carlton Hanna Date: Wed, 2 Mar 2022 09:07:12 -0700 Subject: [PATCH 3/4] Add changelog entry --- CHANGELOG.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 932685b43a..cd59a6f174 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,8 @@ ## [Unreleased](https://github.com/CosmWasm/wasmd/tree/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.22.0...HEAD) From ce034f0fd0ba4384f190fca32271dd8e0f7c2b46 Mon Sep 17 00:00:00 2001 From: Carlton Hanna Date: Wed, 2 Mar 2022 16:29:06 -0700 Subject: [PATCH 4/4] Add tests to assure old address length is still supported --- x/wasm/types/keys_test.go | 37 ++++++++++++++++++++++++++++++++++++- 1 file changed, 36 insertions(+), 1 deletion(-) diff --git a/x/wasm/types/keys_test.go b/x/wasm/types/keys_test.go index 614a73fd73..ece1cad10c 100644 --- a/x/wasm/types/keys_test.go +++ b/x/wasm/types/keys_test.go @@ -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, }