diff --git a/CHANGELOG.md b/CHANGELOG.md index 727ff86ede..64fe73ebaf 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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) diff --git a/x/wasm/types/keys.go b/x/wasm/types/keys.go index 982bf1a069..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+ContractAddrLen) + contractAddrLen := len(contractAddr) + r := make([]byte, prefixLen+contractAddrLen) copy(r[0:], ContractCodeHistoryElementPrefix) copy(r[prefixLen:], contractAddr) return r 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, }