Skip to content

Commit

Permalink
refactor: update change from v0.31.x to v0.33.x
Browse files Browse the repository at this point in the history
  • Loading branch information
hoank101 committed Dec 25, 2023
1 parent 94cbfa7 commit e2bc85f
Show file tree
Hide file tree
Showing 18 changed files with 55 additions and 18 deletions.
2 changes: 1 addition & 1 deletion x/wasm/client/cli/gov_tx_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ func TestParseCodeInfoFlags(t *testing.T) {
wasmBin, err := os.ReadFile("../../keeper/testdata/hackatom.wasm.gzip")
require.NoError(t, err)

checksumStr := "beb3de5e9b93b52e514c74ce87ccddb594b9bcd33b7f1af1bb6da63fc883917b"
checksumStr := "5ca46abb8e9b1b754a5c906f9c0f4eec9121ee09e3cee55ea0faba54763706e2"

specs := map[string]struct {
args []string
Expand Down
8 changes: 4 additions & 4 deletions x/wasm/client/cli/tx_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,22 +75,22 @@ func TestParseVerificationFlags(t *testing.T) {
"gov store zipped": {
srcPath: "../../keeper/testdata/hackatom.wasm.gzip",
args: []string{
"--instantiate-everybody=true", "--code-hash=beb3de5e9b93b52e514c74ce87ccddb594b9bcd33b7f1af1bb6da63fc883917b",
"--instantiate-everybody=true", "--code-hash=5ca46abb8e9b1b754a5c906f9c0f4eec9121ee09e3cee55ea0faba54763706e2",
"--code-source-url=https://example.com", "--builder=cosmwasm/workspace-optimizer:0.12.11",
},
expBuilder: "cosmwasm/workspace-optimizer:0.12.11",
expSource: "https://example.com",
expCodeHash: "beb3de5e9b93b52e514c74ce87ccddb594b9bcd33b7f1af1bb6da63fc883917b",
expCodeHash: "5ca46abb8e9b1b754a5c906f9c0f4eec9121ee09e3cee55ea0faba54763706e2",
},
"gov store raw": {
srcPath: "../../keeper/testdata/hackatom.wasm",
args: []string{
"--instantiate-everybody=true", "--code-hash=beb3de5e9b93b52e514c74ce87ccddb594b9bcd33b7f1af1bb6da63fc883917b",
"--instantiate-everybody=true", "--code-hash=5ca46abb8e9b1b754a5c906f9c0f4eec9121ee09e3cee55ea0faba54763706e2",
"--code-source-url=https://example.com", "--builder=cosmwasm/workspace-optimizer:0.12.11",
},
expBuilder: "cosmwasm/workspace-optimizer:0.12.11",
expSource: "https://example.com",
expCodeHash: "beb3de5e9b93b52e514c74ce87ccddb594b9bcd33b7f1af1bb6da63fc883917b",
expCodeHash: "5ca46abb8e9b1b754a5c906f9c0f4eec9121ee09e3cee55ea0faba54763706e2",
},
"gov store checksum mismatch": {
srcPath: "../../keeper/testdata/hackatom.wasm",
Expand Down
6 changes: 3 additions & 3 deletions x/wasm/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -191,12 +191,12 @@ func (k Keeper) storeCodeInfo(ctx sdk.Context, codeID uint64, codeInfo types.Cod
func (k Keeper) importCode(ctx sdk.Context, codeID uint64, codeInfo types.CodeInfo, wasmCode []byte) error {
if ioutils.IsGzip(wasmCode) {
var err error
wasmCode, err = ioutils.Uncompress(wasmCode, int64(types.MaxWasmSize))
wasmCode, err = ioutils.Uncompress(wasmCode, math.MaxInt64)
if err != nil {
return errorsmod.Wrap(types.ErrCreateFailed, err.Error())
}
}
newCodeHash, err := k.wasmVM.Create(wasmCode)
newCodeHash, err := k.wasmVM.StoreCodeUnchecked(wasmCode)
if err != nil {
return errorsmod.Wrap(types.ErrCreateFailed, err.Error())
}
Expand Down Expand Up @@ -977,7 +977,7 @@ func (k Keeper) runtimeGasForContract(ctx sdk.Context) uint64 {
if meter.IsOutOfGas() {
return 0
}
if meter.Limit() == math.MaxUint64 { // infinite gas meter with limit=math.MaxUint64 and not out of gas
if meter.Limit() == math.MaxUint64 { // infinite gas meter with limit=0 and not out of gas
return math.MaxUint64
}

Expand Down
4 changes: 2 additions & 2 deletions x/wasm/keeper/keeper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ func TestCreateSuccess(t *testing.T) {
require.NoError(t, err)
require.Equal(t, hackatomWasm, storedCode)
// and events emitted
codeHash := strings.ToLower("beb3de5e9b93b52e514c74ce87ccddb594b9bcd33b7f1af1bb6da63fc883917b")
codeHash := strings.ToLower("5ca46abb8e9b1b754a5c906f9c0f4eec9121ee09e3cee55ea0faba54763706e2")
exp := sdk.Events{sdk.NewEvent("store_code", sdk.NewAttribute("code_checksum", codeHash), sdk.NewAttribute("code_id", "1"))}
assert.Equal(t, exp, em.Events())
}
Expand Down Expand Up @@ -416,7 +416,7 @@ func TestInstantiate(t *testing.T) {

gasAfter := ctx.GasMeter().GasConsumed()
if types.EnableGasVerification {
require.Equal(t, uint64(0x1b5bc), gasAfter-gasBefore)
require.Equal(t, uint64(0x1b5bd), gasAfter-gasBefore)
}

// ensure it is stored properly
Expand Down
4 changes: 2 additions & 2 deletions x/wasm/keeper/proposal_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func TestStoreCodeProposal(t *testing.T) {
require.NoError(t, err)
gzippedWasmCode, err := os.ReadFile("./testdata/hackatom.wasm.gzip")
require.NoError(t, err)
checksum, err := hex.DecodeString("beb3de5e9b93b52e514c74ce87ccddb594b9bcd33b7f1af1bb6da63fc883917b")
checksum, err := hex.DecodeString("5ca46abb8e9b1b754a5c906f9c0f4eec9121ee09e3cee55ea0faba54763706e2")
require.NoError(t, err)

specs := map[string]struct {
Expand Down Expand Up @@ -319,7 +319,7 @@ func TestStoreAndInstantiateContractProposal(t *testing.T) {
wasmCode, err := os.ReadFile("./testdata/hackatom.wasm")
require.NoError(t, err)

checksum, err := hex.DecodeString("beb3de5e9b93b52e514c74ce87ccddb594b9bcd33b7f1af1bb6da63fc883917b")
checksum, err := hex.DecodeString("5ca46abb8e9b1b754a5c906f9c0f4eec9121ee09e3cee55ea0faba54763706e2")
require.NoError(t, err)

var (
Expand Down
10 changes: 5 additions & 5 deletions x/wasm/keeper/recurse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,8 @@ func TestGasCostOnQuery(t *testing.T) {
// Note: about 100 SDK gas (10k wasmer gas) for each round of sha256
GasWork50 uint64 = 64_218 // this is a little shy of 50k gas - to keep an eye on the limit

GasReturnUnhashed uint64 = 32
GasReturnHashed uint64 = 27
GasReturnUnhashed uint64 = 29
GasReturnHashed uint64 = 24
)

cases := map[string]struct {
Expand Down Expand Up @@ -208,9 +208,9 @@ func TestLimitRecursiveQueryGas(t *testing.T) {

const (
// Note: about 100 SDK gas (10k wasmer gas) for each round of sha256
GasWork2k uint64 = 77_206 // = NewContractInstanceCosts + x // we have 6x gas used in cpu than in the instance
GasWork2k uint64 = 77_161 // = NewContractInstanceCosts + x // we have 6x gas used in cpu than in the instance
// This is overhead for calling into a sub-contract
GasReturnHashed uint64 = 27
GasReturnHashed uint64 = 25
)

cases := map[string]struct {
Expand Down Expand Up @@ -261,7 +261,7 @@ func TestLimitRecursiveQueryGas(t *testing.T) {
expectQueriesFromContract: 10,
expectOutOfGas: false,
expectError: "query wasm contract failed", // Error we get from the contract instance doing the failing query, not wasmd
expectedGas: 10*(GasWork2k+GasReturnHashed) - 247,
expectedGas: 10*(GasWork2k+GasReturnHashed) - 229,
},
}

Expand Down
1 change: 1 addition & 0 deletions x/wasm/keeper/relay.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ func (k Keeper) OnRecvPacket(
}
// note submessage reply results can overwrite the `Acknowledgement` data
return k.handleContractResponse(ctx, contractAddr, contractInfo.IBCPortID, res.Ok.Messages, res.Ok.Attributes, res.Ok.Acknowledgement, res.Ok.Events)

}

// OnAckPacket calls the contract to handle the "acknowledgement" data which can contain success or failure of a packet
Expand Down
1 change: 1 addition & 0 deletions x/wasm/keeper/test_common.go
Original file line number Diff line number Diff line change
Expand Up @@ -658,6 +658,7 @@ func InstantiateHackatomExampleContract(t testing.TB, ctx sdk.Context, keepers T
adminAddr := contract.CreatorAddr
label := "demo contract to query"
contractAddr, _, err := keepers.ContractKeeper.Instantiate(ctx, contract.CodeID, contract.CreatorAddr, adminAddr, initMsgBz, label, initialAmount)
fmt.Println(err)
require.NoError(t, err)
return HackatomExampleInstance{
ExampleContract: contract,
Expand Down
Binary file modified x/wasm/keeper/testdata/cyberpunk.wasm
Binary file not shown.
Binary file modified x/wasm/keeper/testdata/hackatom.wasm
Binary file not shown.
Binary file modified x/wasm/keeper/testdata/hackatom.wasm.gzip
Binary file not shown.
Binary file modified x/wasm/keeper/testdata/ibc_reflect.wasm
Binary file not shown.
Binary file modified x/wasm/keeper/testdata/ibc_reflect_send.wasm
Binary file not shown.
Binary file modified x/wasm/keeper/testdata/reflect.wasm
Binary file not shown.
Binary file modified x/wasm/keeper/testdata/staking.wasm
Binary file not shown.
2 changes: 1 addition & 1 deletion x/wasm/keeper/testdata/version.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
v1.3.0
burner.wasm: v1.2.0
ibc-reflect.wasm: custom build, see: https://github.com/CosmWasm/cosmwasm/pull/1690
ibc-reflect.wasm: custom build, see: https://github.com/CosmWasm/cosmwasm/pull/1690
4 changes: 4 additions & 0 deletions x/wasm/keeper/wasmtesting/mock_engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,10 @@ func SelfCallingInstMockWasmer(executeCalled *bool) *MockWasmer {
anyCodeID := bytes.Repeat([]byte{0x1}, 32)
return anyCodeID, nil
},
StoreCodeFn: func(code wasmvm.WasmCode) (wasmvm.Checksum, error) {
anyCodeID := bytes.Repeat([]byte{0x1}, 32)
return anyCodeID, nil
},
InstantiateFn: func(codeID wasmvm.Checksum, env wasmvmtypes.Env, info wasmvmtypes.MessageInfo, initMsg []byte, store wasmvm.KVStore, goapi wasmvm.GoAPI, querier wasmvm.Querier, gasMeter wasmvm.GasMeter, gasLimit uint64, deserCost wasmvmtypes.UFraction) (*wasmvmtypes.Response, uint64, error) {
return &wasmvmtypes.Response{
Messages: []wasmvmtypes.SubMsg{
Expand Down
31 changes: 31 additions & 0 deletions x/wasm/types/events.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
package types

import (
"fmt"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/ibc-go/v6/modules/core/exported"
)

const (
// WasmModuleEventType is stored with any contract TX that returns non empty EventAttributes
WasmModuleEventType = "wasm"
Expand All @@ -17,8 +23,31 @@ const (
EventTypeGovContractResult = "gov_contract_result"
EventTypeUpdateContractAdmin = "update_contract_admin"
EventTypeUpdateCodeAccessConfig = "update_code_access_config"
EventTypePacketRecv = "ibc_packet_received"
)

// EmitAcknowledgementEvent emits an event signalling a successful or failed acknowledgement and including the error
// details if any.
func EmitAcknowledgementEvent(ctx sdk.Context, contractAddr sdk.AccAddress, ack exported.Acknowledgement, err error) {
success := err == nil && (ack == nil || ack.Success())
attributes := []sdk.Attribute{
sdk.NewAttribute(sdk.AttributeKeyModule, ModuleName),
sdk.NewAttribute(AttributeKeyContractAddr, contractAddr.String()),
sdk.NewAttribute(AttributeKeyAckSuccess, fmt.Sprintf("%t", success)),
}

if err != nil {
attributes = append(attributes, sdk.NewAttribute(AttributeKeyAckError, err.Error()))
}

ctx.EventManager().EmitEvent(
sdk.NewEvent(
EventTypePacketRecv,
attributes...,
),
)
}

// event attributes returned from contract execution
const (
AttributeReservedPrefix = "_"
Expand All @@ -31,4 +60,6 @@ const (
AttributeKeyNewAdmin = "new_admin_address"
AttributeKeyCodePermission = "code_permission"
AttributeKeyAuthorizedAddresses = "authorized_addresses"
AttributeKeyAckSuccess = "success"
AttributeKeyAckError = "error"
)

0 comments on commit e2bc85f

Please sign in to comment.