From 1330b115d664d0ba6aa1155795383accafb87ad4 Mon Sep 17 00:00:00 2001 From: Ethan Frey Date: Wed, 28 Jul 2021 00:58:31 +0200 Subject: [PATCH 1/6] Update hash algorithm --- x/wasm/keeper/keeper.go | 46 +++++++++++++++++++++++++++++++++++------ 1 file changed, 40 insertions(+), 6 deletions(-) diff --git a/x/wasm/keeper/keeper.go b/x/wasm/keeper/keeper.go index 8e7d9942fb..68e58b27cb 100644 --- a/x/wasm/keeper/keeper.go +++ b/x/wasm/keeper/keeper.go @@ -2,6 +2,7 @@ package keeper import ( "bytes" + "crypto/sha256" "encoding/binary" "fmt" "github.com/CosmWasm/wasmd/x/wasm/types" @@ -815,13 +816,46 @@ func (k Keeper) generateContractAddress(ctx sdk.Context, codeID uint64) sdk.AccA // BuildContractAddress builds an sdk account address for a contract. func BuildContractAddress(codeID, instanceID uint64) sdk.AccAddress { - if codeID > math.MaxUint32 || instanceID > math.MaxUint32 { - // NOTE: It is possible to get a duplicate address if either codeID or instanceID - // overflow 32 bits. This is highly improbable, but something that could be refactored. - panic(fmt.Sprintf("address uint32 reached: codeID: %d, instanceID: %d", codeID, instanceID)) + contractID := make([]byte, 16) + binary.BigEndian.PutUint64(contractID[:8], codeID) + binary.BigEndian.PutUint64(contractID[8:], instanceID) + return Module(types.ModuleName, contractID) +} + +// Hash and Module is taken from https://github.com/cosmos/cosmos-sdk/blob/v0.43.0-rc2/types/address/hash.go +// (PR #9088 included in Cosmos SDK 0.43 - can be swapped out for the sdk version when we upgrade) + +// Hash creates a new address from address type and key +func Hash(typ string, key []byte) []byte { + hasher := sha256.New() + _, err := hasher.Write([]byte(typ)) + // the error always nil, it's here only to satisfy the io.Writer interface + assertNil(err) + th := hasher.Sum(nil) + + hasher.Reset() + _, err = hasher.Write(th) + assertNil(err) + _, err = hasher.Write(key) + assertNil(err) + hashed := hasher.Sum(nil) + // 20 bytes to work with Cosmos SDK 0.42 (0.43 pushes for 32 bytes) + // TODO: remove truncate if we update to 0.43 before wasmd 1.0 + return hashed[:20] +} + +// Module is a specialized version of a composed address for modules. Each module account +// is constructed from a module name and module account key. +func Module(moduleName string, key []byte) []byte { + mKey := append([]byte(moduleName), 0) + return Hash("module", append(mKey, key...)) +} + +// Also from the 0.43 Cosmos SDK... sigh (sdkerrors.AssertNil) +func assertNil(err error) { + if err != nil { + panic(fmt.Errorf("logic error - this should never happen. %w", err)) } - contractID := codeID<<32 + instanceID - return addrFromUint64(contractID) } // GetNextCodeID reads the next sequence id used for storing wasm code. From b8b0f209b2b7e837adca1531a303399a4e598d37 Mon Sep 17 00:00:00 2001 From: Ethan Frey Date: Wed, 28 Jul 2021 00:58:46 +0200 Subject: [PATCH 2/6] Update tests (new default contract addr) --- x/wasm/keeper/genesis_test.go | 4 ++-- x/wasm/keeper/keeper_test.go | 23 ++++++++-------------- x/wasm/keeper/proposal_integration_test.go | 2 +- 3 files changed, 11 insertions(+), 18 deletions(-) diff --git a/x/wasm/keeper/genesis_test.go b/x/wasm/keeper/genesis_test.go index 3a9a292e5e..1da3d03200 100644 --- a/x/wasm/keeper/genesis_test.go +++ b/x/wasm/keeper/genesis_test.go @@ -474,7 +474,7 @@ func TestImportContractWithCodeHistoryReset(t *testing.T) { ], "contracts": [ { - "contract_address": "cosmos18vd8fpwxzck93qlwghaj6arh4p7c5n89uzcee5", + "contract_address": "cosmos14hj2tavq8fpesdwxxcu44rty3hh90vhuc53mp6", "contract_info": { "code_id": "1", "creator": "cosmos13x849jzd03vne42ynpj25hn8npjecxqrjghd8x", @@ -533,7 +533,7 @@ func TestImportContractWithCodeHistoryReset(t *testing.T) { assert.Equal(t, expCodeInfo, *gotCodeInfo) // verify contract - contractAddr, _ := sdk.AccAddressFromBech32("cosmos18vd8fpwxzck93qlwghaj6arh4p7c5n89uzcee5") + contractAddr, _ := sdk.AccAddressFromBech32("cosmos14hj2tavq8fpesdwxxcu44rty3hh90vhuc53mp6") gotContractInfo := keeper.GetContractInfo(ctx, contractAddr) require.NotNil(t, gotContractInfo) contractCreatorAddr := "cosmos13x849jzd03vne42ynpj25hn8npjecxqrjghd8x" diff --git a/x/wasm/keeper/keeper_test.go b/x/wasm/keeper/keeper_test.go index 337f70070d..ca402b39de 100644 --- a/x/wasm/keeper/keeper_test.go +++ b/x/wasm/keeper/keeper_test.go @@ -280,7 +280,7 @@ func TestInstantiate(t *testing.T) { // create with no balance is also legal gotContractAddr, _, err := keepers.ContractKeeper.Instantiate(ctx, codeID, creator, nil, initMsgBz, "demo contract 1", nil) require.NoError(t, err) - require.Equal(t, "cosmos18vd8fpwxzck93qlwghaj6arh4p7c5n89uzcee5", gotContractAddr.String()) + require.Equal(t, "cosmos14hj2tavq8fpesdwxxcu44rty3hh90vhuc53mp6", gotContractAddr.String()) gasAfter := ctx.GasMeter().GasConsumed() if types.EnableGasVerification { @@ -481,7 +481,7 @@ func TestExecute(t *testing.T) { addr, _, err := keepers.ContractKeeper.Instantiate(ctx, contractID, creator, nil, initMsgBz, "demo contract 3", deposit) require.NoError(t, err) - require.Equal(t, "cosmos18vd8fpwxzck93qlwghaj6arh4p7c5n89uzcee5", addr.String()) + require.Equal(t, "cosmos14hj2tavq8fpesdwxxcu44rty3hh90vhuc53mp6", addr.String()) // ensure bob doesn't exist bobAcct := accKeeper.GetAccount(ctx, bob) @@ -1163,7 +1163,7 @@ func TestSudo(t *testing.T) { addr, _, err := keepers.ContractKeeper.Instantiate(ctx, contractID, creator, nil, initMsgBz, "demo contract 3", deposit) require.NoError(t, err) - require.Equal(t, "cosmos18vd8fpwxzck93qlwghaj6arh4p7c5n89uzcee5", addr.String()) + require.Equal(t, "cosmos14hj2tavq8fpesdwxxcu44rty3hh90vhuc53mp6", addr.String()) // the community is broke _, _, community := keyPubAddr() @@ -1501,7 +1501,6 @@ func TestBuildContractAddress(t *testing.T) { specs := map[string]struct { srcCodeID uint64 srcInstanceID uint64 - expPanic bool }{ "both empty": {}, "both below max": { @@ -1512,23 +1511,17 @@ func TestBuildContractAddress(t *testing.T) { srcCodeID: math.MaxUint32, srcInstanceID: math.MaxUint32, }, - "codeID > max": { - srcCodeID: math.MaxUint32 + 1, - expPanic: true, + "codeID > max u32": { + srcCodeID: math.MaxUint32 + 1, + srcInstanceID: 17, }, - "instanceID > max": { + "instanceID > max u32": { + srcCodeID: 22, srcInstanceID: math.MaxUint32 + 1, - expPanic: true, }, } for name, spec := range specs { t.Run(name, func(t *testing.T) { - if spec.expPanic { - require.Panics(t, func() { - BuildContractAddress(spec.srcCodeID, spec.srcInstanceID) - }) - return - } gotAddr := BuildContractAddress(spec.srcCodeID, spec.srcInstanceID) require.NotNil(t, gotAddr) assert.Nil(t, sdk.VerifyAddressFormat(gotAddr)) diff --git a/x/wasm/keeper/proposal_integration_test.go b/x/wasm/keeper/proposal_integration_test.go index 3ab1e8da8b..317e63f6ed 100644 --- a/x/wasm/keeper/proposal_integration_test.go +++ b/x/wasm/keeper/proposal_integration_test.go @@ -98,7 +98,7 @@ func TestInstantiateProposal(t *testing.T) { require.NoError(t, err) // then - contractAddr, err := sdk.AccAddressFromBech32("cosmos18vd8fpwxzck93qlwghaj6arh4p7c5n89uzcee5") + contractAddr, err := sdk.AccAddressFromBech32("cosmos14hj2tavq8fpesdwxxcu44rty3hh90vhuc53mp6") require.NoError(t, err) cInfo := wasmKeeper.GetContractInfo(ctx, contractAddr) From 477c794913980beeeced77cc97f8ba5618398833 Mon Sep 17 00:00:00 2001 From: Ethan Frey Date: Wed, 28 Jul 2021 01:04:11 +0200 Subject: [PATCH 3/6] Almost all tests pass --- x/wasm/README.md | 14 +++++++------- x/wasm/client/cli/genesis_msg_test.go | 3 ++- x/wasm/client/proposal_handler_test.go | 6 +++--- x/wasm/module_test.go | 6 +++--- x/wasm/types/proposal_test.go | 12 ++++++------ x/wasm/types/test_fixtures.go | 8 ++++---- 6 files changed, 25 insertions(+), 24 deletions(-) diff --git a/x/wasm/README.md b/x/wasm/README.md index 3309b35a37..ad5fb1f54b 100644 --- a/x/wasm/README.md +++ b/x/wasm/README.md @@ -53,7 +53,7 @@ was sent: }, { "key": "_contract_address", - "value": "cosmos18vd8fpwxzck93qlwghaj6arh4p7c5n89uzcee5" + "value": "cosmos14hj2tavq8fpesdwxxcu44rty3hh90vhuc53mp6" } ] } @@ -70,7 +70,7 @@ provide a initial balance in the same `MsgInstantiateContract`. We see the follo "Attr": [ { "key": "recipient", - "value": "cosmos18vd8fpwxzck93qlwghaj6arh4p7c5n89uzcee5" + "value": "cosmos14hj2tavq8fpesdwxxcu44rty3hh90vhuc53mp6" }, { "key": "sender", @@ -97,7 +97,7 @@ Here is an example from the escrow contract successfully releasing funds to the "Attr": [ { "key": "_contract_address", - "value": "cosmos18vd8fpwxzck93qlwghaj6arh4p7c5n89uzcee5" + "value": "cosmos14hj2tavq8fpesdwxxcu44rty3hh90vhuc53mp6" }, { "key": "action", @@ -131,7 +131,7 @@ was executed (which always appears, while 2 is optional and has information as r "Attr": [ { "key": "recipient", - "value": "cosmos18vd8fpwxzck93qlwghaj6arh4p7c5n89uzcee5" + "value": "cosmos14hj2tavq8fpesdwxxcu44rty3hh90vhuc53mp6" }, { "key": "sender", @@ -148,7 +148,7 @@ was executed (which always appears, while 2 is optional and has information as r "Attr": [ { "key": "_contract_address", - "value": "cosmos18vd8fpwxzck93qlwghaj6arh4p7c5n89uzcee5" + "value": "cosmos14hj2tavq8fpesdwxxcu44rty3hh90vhuc53mp6" }, { "key": "action", @@ -169,7 +169,7 @@ was executed (which always appears, while 2 is optional and has information as r }, { "key": "sender", - "value": "cosmos18vd8fpwxzck93qlwghaj6arh4p7c5n89uzcee5" + "value": "cosmos14hj2tavq8fpesdwxxcu44rty3hh90vhuc53mp6" }, { "key": "amount", @@ -194,7 +194,7 @@ was executed (which always appears, while 2 is optional and has information as r }, { "key": "_contract_address", - "value": "cosmos18vd8fpwxzck93qlwghaj6arh4p7c5n89uzcee5" + "value": "cosmos14hj2tavq8fpesdwxxcu44rty3hh90vhuc53mp6" } ] } diff --git a/x/wasm/client/cli/genesis_msg_test.go b/x/wasm/client/cli/genesis_msg_test.go index e96dd03912..20bc417aad 100644 --- a/x/wasm/client/cli/genesis_msg_test.go +++ b/x/wasm/client/cli/genesis_msg_test.go @@ -315,7 +315,7 @@ func TestInstantiateContractCmd(t *testing.T) { } func TestExecuteContractCmd(t *testing.T) { - const firstContractAddress = "cosmos18vd8fpwxzck93qlwghaj6arh4p7c5n89uzcee5" + const firstContractAddress = "cosmos14hj2tavq8fpesdwxxcu44rty3hh90vhuc53mp6" minimalWasmGenesis := types.GenesisState{ Params: types.DefaultParams(), } @@ -396,6 +396,7 @@ func TestExecuteContractCmd(t *testing.T) { }, }, mutator: func(cmd *cobra.Command) { + // TODO: calculate desired address cmd.SetArgs([]string{"cosmos1weh0k0l6t6v4jkmkde8e90tzkw2c59g42ccl62", `{}`}) flagSet := cmd.Flags() flagSet.Set("run-as", myWellFundedAccount) diff --git a/x/wasm/client/proposal_handler_test.go b/x/wasm/client/proposal_handler_test.go index 755972b6a9..735dbd73e2 100644 --- a/x/wasm/client/proposal_handler_test.go +++ b/x/wasm/client/proposal_handler_test.go @@ -172,7 +172,7 @@ func TestGovRestHandlers(t *testing.T) { "title": "Test Proposal", "description": "My proposal", "type": "migrate", - "contract": "cosmos18vd8fpwxzck93qlwghaj6arh4p7c5n89uzcee5", + "contract": "cosmos14hj2tavq8fpesdwxxcu44rty3hh90vhuc53mp6", "code_id": "1", "msg": dict{"foo": "bar"}, "run_as": "cosmos100dejzacpanrldpjjwksjm62shqhyss44jf5xz", @@ -188,7 +188,7 @@ func TestGovRestHandlers(t *testing.T) { "title": "Test Proposal", "description": "My proposal", "type": "migrate", - "contract": "cosmos18vd8fpwxzck93qlwghaj6arh4p7c5n89uzcee5", + "contract": "cosmos14hj2tavq8fpesdwxxcu44rty3hh90vhuc53mp6", "new_admin": "cosmos100dejzacpanrldpjjwksjm62shqhyss44jf5xz", "deposit": []dict{{"denom": "ustake", "amount": "10"}}, "proposer": "cosmos1ve557a5g9yw2g2z57js3pdmcvd5my6g8ze20np", @@ -202,7 +202,7 @@ func TestGovRestHandlers(t *testing.T) { "title": "Test Proposal", "description": "My proposal", "type": "migrate", - "contract": "cosmos18vd8fpwxzck93qlwghaj6arh4p7c5n89uzcee5", + "contract": "cosmos14hj2tavq8fpesdwxxcu44rty3hh90vhuc53mp6", "deposit": []dict{{"denom": "ustake", "amount": "10"}}, "proposer": "cosmos1ve557a5g9yw2g2z57js3pdmcvd5my6g8ze20np", "base_req": aBaseReq, diff --git a/x/wasm/module_test.go b/x/wasm/module_test.go index 54c71e3654..14bcd825f9 100644 --- a/x/wasm/module_test.go +++ b/x/wasm/module_test.go @@ -177,7 +177,7 @@ func TestHandleInstantiate(t *testing.T) { require.NoError(t, err) contractBech32Addr := parseInitResponse(t, res.Data) - require.Equal(t, "cosmos18vd8fpwxzck93qlwghaj6arh4p7c5n89uzcee5", contractBech32Addr) + require.Equal(t, "cosmos14hj2tavq8fpesdwxxcu44rty3hh90vhuc53mp6", contractBech32Addr) // this should be standard x/wasm init event, nothing from contract require.Equal(t, 2, len(res.Events), prettyEvents(res.Events)) assert.Equal(t, "wasm", res.Events[0].Type) @@ -234,7 +234,7 @@ func TestHandleExecute(t *testing.T) { require.NoError(t, err) contractBech32Addr := parseInitResponse(t, res.Data) - require.Equal(t, "cosmos18vd8fpwxzck93qlwghaj6arh4p7c5n89uzcee5", contractBech32Addr) + require.Equal(t, "cosmos14hj2tavq8fpesdwxxcu44rty3hh90vhuc53mp6", contractBech32Addr) // this should be standard x/wasm init event, plus a bank send event (2), with no custom contract events require.Equal(t, 3, len(res.Events), prettyEvents(res.Events)) assert.Equal(t, "transfer", res.Events[0].Type) @@ -354,7 +354,7 @@ func TestHandleExecuteEscrow(t *testing.T) { res, err = h(data.ctx, &initCmd) require.NoError(t, err) contractBech32Addr := parseInitResponse(t, res.Data) - require.Equal(t, "cosmos18vd8fpwxzck93qlwghaj6arh4p7c5n89uzcee5", contractBech32Addr) + require.Equal(t, "cosmos14hj2tavq8fpesdwxxcu44rty3hh90vhuc53mp6", contractBech32Addr) handleMsg := map[string]interface{}{ "release": map[string]interface{}{}, diff --git a/x/wasm/types/proposal_test.go b/x/wasm/types/proposal_test.go index f7ce323540..15ea4fcd55 100644 --- a/x/wasm/types/proposal_test.go +++ b/x/wasm/types/proposal_test.go @@ -509,7 +509,7 @@ func TestProposalStrings(t *testing.T) { exp: `Migrate Contract Proposal: Title: Foo Description: Bar - Contract: cosmos18vd8fpwxzck93qlwghaj6arh4p7c5n89uzcee5 + Contract: cosmos14hj2tavq8fpesdwxxcu44rty3hh90vhuc53mp6 Code id: 1 Run as: cosmos1qyqszqgpqyqszqgpqyqszqgpqyqszqgpjnp7du Msg "{\"verifier\":\"cosmos1qyqszqgpqyqszqgpqyqszqgpqyqszqgpjnp7du\"}" @@ -520,7 +520,7 @@ func TestProposalStrings(t *testing.T) { exp: `Update Contract Admin Proposal: Title: Foo Description: Bar - Contract: cosmos18vd8fpwxzck93qlwghaj6arh4p7c5n89uzcee5 + Contract: cosmos14hj2tavq8fpesdwxxcu44rty3hh90vhuc53mp6 New Admin: cosmos1qyqszqgpqyqszqgpqyqszqgpqyqszqgpjnp7du `, }, @@ -529,7 +529,7 @@ func TestProposalStrings(t *testing.T) { exp: `Clear Contract Admin Proposal: Title: Foo Description: Bar - Contract: cosmos18vd8fpwxzck93qlwghaj6arh4p7c5n89uzcee5 + Contract: cosmos14hj2tavq8fpesdwxxcu44rty3hh90vhuc53mp6 `, }, "pin codes": { @@ -628,7 +628,7 @@ funds: [] src: MigrateContractProposalFixture(), exp: `title: Foo description: Bar -contract: cosmos18vd8fpwxzck93qlwghaj6arh4p7c5n89uzcee5 +contract: cosmos14hj2tavq8fpesdwxxcu44rty3hh90vhuc53mp6 code_id: 1 msg: '{"verifier":"cosmos1qyqszqgpqyqszqgpqyqszqgpqyqszqgpjnp7du"}' run_as: cosmos1qyqszqgpqyqszqgpqyqszqgpqyqszqgpjnp7du @@ -639,14 +639,14 @@ run_as: cosmos1qyqszqgpqyqszqgpqyqszqgpqyqszqgpjnp7du exp: `title: Foo description: Bar new_admin: cosmos1qyqszqgpqyqszqgpqyqszqgpqyqszqgpjnp7du -contract: cosmos18vd8fpwxzck93qlwghaj6arh4p7c5n89uzcee5 +contract: cosmos14hj2tavq8fpesdwxxcu44rty3hh90vhuc53mp6 `, }, "clear admin": { src: ClearAdminProposalFixture(), exp: `title: Foo description: Bar -contract: cosmos18vd8fpwxzck93qlwghaj6arh4p7c5n89uzcee5 +contract: cosmos14hj2tavq8fpesdwxxcu44rty3hh90vhuc53mp6 `, }, "pin codes": { diff --git a/x/wasm/types/test_fixtures.go b/x/wasm/types/test_fixtures.go index da0a5e52c1..a1492ea314 100644 --- a/x/wasm/types/test_fixtures.go +++ b/x/wasm/types/test_fixtures.go @@ -164,7 +164,7 @@ func MsgInstantiateContractFixture(mutators ...func(*MsgInstantiateContract)) *M func MsgExecuteContractFixture(mutators ...func(*MsgExecuteContract)) *MsgExecuteContract { const ( anyAddress = "cosmos1qyqszqgpqyqszqgpqyqszqgpqyqszqgpjnp7du" - firstContractAddress = "cosmos18vd8fpwxzck93qlwghaj6arh4p7c5n89uzcee5" + firstContractAddress = "cosmos14hj2tavq8fpesdwxxcu44rty3hh90vhuc53mp6" ) r := &MsgExecuteContract{ Sender: anyAddress, @@ -246,7 +246,7 @@ func MigrateContractProposalFixture(mutators ...func(p *MigrateContractProposal) panic(err) } const ( - contractAddr = "cosmos18vd8fpwxzck93qlwghaj6arh4p7c5n89uzcee5" + contractAddr = "cosmos14hj2tavq8fpesdwxxcu44rty3hh90vhuc53mp6" anyAddress = "cosmos1qyqszqgpqyqszqgpqyqszqgpqyqszqgpjnp7du" ) p := &MigrateContractProposal{ @@ -266,7 +266,7 @@ func MigrateContractProposalFixture(mutators ...func(p *MigrateContractProposal) func UpdateAdminProposalFixture(mutators ...func(p *UpdateAdminProposal)) *UpdateAdminProposal { const ( - contractAddr = "cosmos18vd8fpwxzck93qlwghaj6arh4p7c5n89uzcee5" + contractAddr = "cosmos14hj2tavq8fpesdwxxcu44rty3hh90vhuc53mp6" anyAddress = "cosmos1qyqszqgpqyqszqgpqyqszqgpqyqszqgpjnp7du" ) @@ -283,7 +283,7 @@ func UpdateAdminProposalFixture(mutators ...func(p *UpdateAdminProposal)) *Updat } func ClearAdminProposalFixture(mutators ...func(p *ClearAdminProposal)) *ClearAdminProposal { - const contractAddr = "cosmos18vd8fpwxzck93qlwghaj6arh4p7c5n89uzcee5" + const contractAddr = "cosmos14hj2tavq8fpesdwxxcu44rty3hh90vhuc53mp6" p := &ClearAdminProposal{ Title: "Foo", Description: "Bar", From 65f4b544fb3505125a29720e781dc0329ac5ea81 Mon Sep 17 00:00:00 2001 From: Ethan Frey Date: Wed, 28 Jul 2021 01:07:05 +0200 Subject: [PATCH 4/6] Update CHANGELOG --- CHANGELOG.md | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index a899fcf031..ac51900468 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,19 +2,25 @@ ## [Unreleased](https://github.com/CosmWasm/wasmd/tree/HEAD) +**Api Breaking:** + +- Move Proto version from `v1beta1` to `v1` for all cosmwasm.wasm.* types + [\#563](https://github.com/CosmWasm/wasmd/pull/563) +- Renamed InitMsg and MigrateMsg fields to Msg. This applies to protobuf Msg + and Proposals, as well as REST and CLI [\#563](https://github.com/CosmWasm/wasmd/pull/563) +- Removed source and builder fields from StoreCode and CodeInfo. They were rarely used. + [\#564](https://github.com/CosmWasm/wasmd/pull/564) +- Changed contract address derivation function. If you hardcoded the first contract + addresses anywhere (in scripts?), please update them. + [\#565](https://github.com/CosmWasm/wasmd/pull/565) + **Implemented Enhancements:** + - Reject invalid events/attributes returned from contracts [\#560](https://github.com/CosmWasm/wasmd/pull/560) - IBC Query methods from Wasm contracts only return OPEN channels [\#568](https://github.com/CosmWasm/wasmd/pull/568) [Full Changelog](https://github.com/CosmWasm/wasmd/compare/v0.17.0...HEAD) -**Api Breaking:** - -- Renamed InitMsg and MigrateMsg fields to Msg. This applies to protobuf Msg - and Proposals, as well as REST and CLI [\#563](https://github.com/CosmWasm/wasmd/pull/563) -- Move Proto version from `v1beta1` to `v1` for all cosmwasm.wasm.* types - [\#563](https://github.com/CosmWasm/wasmd/pull/563) - ## [v0.17.0](https://github.com/CosmWasm/wasmd/tree/v0.17.0) (2021-05-26) [Full Changelog](https://github.com/CosmWasm/wasmd/compare/v0.17.0...v0.16.0) From 92850081f0bd4b42d11fe2b9341d14e9170be188 Mon Sep 17 00:00:00 2001 From: Ethan Frey Date: Wed, 28 Jul 2021 01:18:48 +0200 Subject: [PATCH 5/6] Fix last tests --- x/wasm/client/cli/genesis_msg_test.go | 4 ++-- x/wasm/keeper/keeper_test.go | 17 ++++++++++++++++- 2 files changed, 18 insertions(+), 3 deletions(-) diff --git a/x/wasm/client/cli/genesis_msg_test.go b/x/wasm/client/cli/genesis_msg_test.go index 20bc417aad..61095e8c8d 100644 --- a/x/wasm/client/cli/genesis_msg_test.go +++ b/x/wasm/client/cli/genesis_msg_test.go @@ -396,8 +396,8 @@ func TestExecuteContractCmd(t *testing.T) { }, }, mutator: func(cmd *cobra.Command) { - // TODO: calculate desired address - cmd.SetArgs([]string{"cosmos1weh0k0l6t6v4jkmkde8e90tzkw2c59g42ccl62", `{}`}) + // See TestBuildContractAddress in keeper_test.go + cmd.SetArgs([]string{"cosmos1mujpjkwhut9yjw4xueyugc02evfv46y04aervg", `{}`}) flagSet := cmd.Flags() flagSet.Set("run-as", myWellFundedAccount) }, diff --git a/x/wasm/keeper/keeper_test.go b/x/wasm/keeper/keeper_test.go index ca402b39de..8bf0cdf5ac 100644 --- a/x/wasm/keeper/keeper_test.go +++ b/x/wasm/keeper/keeper_test.go @@ -1501,8 +1501,18 @@ func TestBuildContractAddress(t *testing.T) { specs := map[string]struct { srcCodeID uint64 srcInstanceID uint64 + expectedAddr string }{ - "both empty": {}, + "initial contract": { + srcCodeID: 1, + srcInstanceID: 1, + expectedAddr: "cosmos14hj2tavq8fpesdwxxcu44rty3hh90vhuc53mp6", + }, + "demo value": { + srcCodeID: 1, + srcInstanceID: 100, + expectedAddr: "cosmos1mujpjkwhut9yjw4xueyugc02evfv46y04aervg", + }, "both below max": { srcCodeID: math.MaxUint32 - 1, srcInstanceID: math.MaxUint32 - 1, @@ -1514,10 +1524,12 @@ func TestBuildContractAddress(t *testing.T) { "codeID > max u32": { srcCodeID: math.MaxUint32 + 1, srcInstanceID: 17, + expectedAddr: "cosmos1673hrexz4h6s0ft04l96ygq667djzh2nvy7fsu", }, "instanceID > max u32": { srcCodeID: 22, srcInstanceID: math.MaxUint32 + 1, + expectedAddr: "cosmos10q3pgfvmeyy0veekgtqhxujxkhz0vm9z65ckqh", }, } for name, spec := range specs { @@ -1525,6 +1537,9 @@ func TestBuildContractAddress(t *testing.T) { gotAddr := BuildContractAddress(spec.srcCodeID, spec.srcInstanceID) require.NotNil(t, gotAddr) assert.Nil(t, sdk.VerifyAddressFormat(gotAddr)) + if len(spec.expectedAddr) > 0 { + require.Equal(t, spec.expectedAddr, gotAddr.String()) + } }) } } From 6f720a4c883ba54099d6cf324d4aa15f54e68f9e Mon Sep 17 00:00:00 2001 From: Ethan Frey Date: Wed, 28 Jul 2021 10:33:33 +0200 Subject: [PATCH 6/6] PR Comment: move where truncating address length to 20 --- x/wasm/keeper/keeper.go | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/x/wasm/keeper/keeper.go b/x/wasm/keeper/keeper.go index 68e58b27cb..4666cc9f2e 100644 --- a/x/wasm/keeper/keeper.go +++ b/x/wasm/keeper/keeper.go @@ -819,7 +819,9 @@ func BuildContractAddress(codeID, instanceID uint64) sdk.AccAddress { contractID := make([]byte, 16) binary.BigEndian.PutUint64(contractID[:8], codeID) binary.BigEndian.PutUint64(contractID[8:], instanceID) - return Module(types.ModuleName, contractID) + // 20 bytes to work with Cosmos SDK 0.42 (0.43 pushes for 32 bytes) + // TODO: remove truncate if we update to 0.43 before wasmd 1.0 + return Module(types.ModuleName, contractID)[:20] } // Hash and Module is taken from https://github.com/cosmos/cosmos-sdk/blob/v0.43.0-rc2/types/address/hash.go @@ -838,10 +840,7 @@ func Hash(typ string, key []byte) []byte { assertNil(err) _, err = hasher.Write(key) assertNil(err) - hashed := hasher.Sum(nil) - // 20 bytes to work with Cosmos SDK 0.42 (0.43 pushes for 32 bytes) - // TODO: remove truncate if we update to 0.43 before wasmd 1.0 - return hashed[:20] + return hasher.Sum(nil) } // Module is a specialized version of a composed address for modules. Each module account