Skip to content

Commit

Permalink
Fix comments
Browse files Browse the repository at this point in the history
  • Loading branch information
pinosu committed Sep 17, 2024
1 parent 46d721c commit 40c7669
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 23 deletions.
15 changes: 7 additions & 8 deletions x/wasm/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,7 @@ func (k Keeper) instantiate(
return nil, nil, types.ErrNoSuchCodeFn(codeID).Wrapf("code id %d", codeID)
}

sdkCtx, discount := k.checkDiscountEligibility(sdkCtx, string(codeInfo.CodeHash), k.IsPinnedCode(sdkCtx, codeID))
sdkCtx, discount := k.checkDiscountEligibility(sdkCtx, codeInfo.CodeHash, k.IsPinnedCode(sdkCtx, codeID))
setupCost := k.gasRegister.SetupContractCost(discount, len(initMsg))

sdkCtx.GasMeter().ConsumeGas(setupCost, "Loading CosmWasm module: instantiate")
Expand Down Expand Up @@ -399,7 +399,7 @@ func (k Keeper) execute(ctx context.Context, contractAddress, caller sdk.AccAddr
return nil, err
}

sdkCtx, discount := k.checkDiscountEligibility(sdkCtx, string(codeInfo.CodeHash), k.IsPinnedCode(ctx, contractInfo.CodeID))
sdkCtx, discount := k.checkDiscountEligibility(sdkCtx, codeInfo.CodeHash, k.IsPinnedCode(ctx, contractInfo.CodeID))
setupCost := k.gasRegister.SetupContractCost(discount, len(msg))

sdkCtx.GasMeter().ConsumeGas(setupCost, "Loading CosmWasm module: execute")
Expand Down Expand Up @@ -561,7 +561,7 @@ func (k Keeper) callMigrateEntrypoint(
msg []byte,
newCodeID uint64,
) (*wasmvmtypes.Response, error) {
sdkCtx, discount := k.checkDiscountEligibility(sdkCtx, string(newChecksum), k.IsPinnedCode(sdkCtx, newCodeID))
sdkCtx, discount := k.checkDiscountEligibility(sdkCtx, newChecksum, k.IsPinnedCode(sdkCtx, newCodeID))
setupCost := k.gasRegister.SetupContractCost(discount, len(msg))
sdkCtx.GasMeter().ConsumeGas(setupCost, "Loading CosmWasm module: migrate")

Expand Down Expand Up @@ -604,7 +604,7 @@ func (k Keeper) Sudo(ctx context.Context, contractAddress sdk.AccAddress, msg []
return nil, err
}
sdkCtx := sdk.UnwrapSDKContext(ctx)
sdkCtx, discount := k.checkDiscountEligibility(sdkCtx, string(codeInfo.CodeHash), k.IsPinnedCode(ctx, contractInfo.CodeID))
sdkCtx, discount := k.checkDiscountEligibility(sdkCtx, codeInfo.CodeHash, k.IsPinnedCode(ctx, contractInfo.CodeID))
setupCost := k.gasRegister.SetupContractCost(discount, len(msg))

sdkCtx.GasMeter().ConsumeGas(setupCost, "Loading CosmWasm module: sudo")
Expand Down Expand Up @@ -648,8 +648,7 @@ func (k Keeper) reply(ctx sdk.Context, contractAddress sdk.AccAddress, reply was
return nil, err
}

ctx, discount := k.checkDiscountEligibility(ctx, string(codeInfo.CodeHash), k.IsPinnedCode(ctx, contractInfo.CodeID))
replyCosts := k.gasRegister.ReplyCosts(discount, reply)
replyCosts := k.gasRegister.ReplyCosts(true, reply)
ctx.GasMeter().ConsumeGas(replyCosts, "Loading CosmWasm module: reply")

env := types.NewEnv(ctx, contractAddress)
Expand Down Expand Up @@ -842,7 +841,7 @@ func (k Keeper) QuerySmart(ctx context.Context, contractAddr sdk.AccAddress, req
return nil, err
}

sdkCtx, discount := k.checkDiscountEligibility(sdkCtx, string(codeInfo.CodeHash), k.IsPinnedCode(ctx, contractInfo.CodeID))
sdkCtx, discount := k.checkDiscountEligibility(sdkCtx, codeInfo.CodeHash, k.IsPinnedCode(ctx, contractInfo.CodeID))
setupCost := k.gasRegister.SetupContractCost(discount, len(req))
sdkCtx.GasMeter().ConsumeGas(setupCost, "Loading CosmWasm module: query")

Expand Down Expand Up @@ -1167,7 +1166,7 @@ func (k Keeper) IsPinnedCode(ctx context.Context, codeID uint64) bool {
return ok
}

func (k Keeper) checkDiscountEligibility(ctx sdk.Context, checksum string, isPinned bool) (sdk.Context, bool) {
func (k Keeper) checkDiscountEligibility(ctx sdk.Context, checksum []byte, isPinned bool) (sdk.Context, bool) {
sdkCtx := sdk.UnwrapSDKContext(ctx)
if isPinned {
return sdkCtx, true
Expand Down
2 changes: 1 addition & 1 deletion x/wasm/keeper/relay_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -427,7 +427,7 @@ func TestOnRecvPacket(t *testing.T) {
},
"submessage reply can overwrite ack data": {
contractAddr: example.Contract,
expContractGas: types.DefaultInstanceCost + myContractGas + storageCosts + 1_000,
expContractGas: types.DefaultInstanceCostDiscount + myContractGas + storageCosts,
contractResp: &wasmvmtypes.IBCReceiveResult{
Ok: &wasmvmtypes.IBCReceiveResponse{
Acknowledgement: []byte("myAck"),
Expand Down
10 changes: 5 additions & 5 deletions x/wasm/keeper/submsg_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -243,14 +243,14 @@ func TestDispatchSubMsgErrorHandling(t *testing.T) {
"send tokens": {
submsgID: 5,
msg: validBankSend,
resultAssertions: []assertion{assertReturnedEvents(0), assertGasUsed(110_000, 113_000)},
resultAssertions: []assertion{assertReturnedEvents(0), assertGasUsed(110_000, 112_000)},
},
"not enough tokens": {
submsgID: 6,
msg: invalidBankSend,
subMsgError: true,
// uses less gas than the send tokens (cost of bank transfer)
resultAssertions: []assertion{assertGasUsed(78_000, 82_000), assertErrorString("codespace: sdk, code: 5")},
resultAssertions: []assertion{assertGasUsed(78_000, 81_000), assertErrorString("codespace: sdk, code: 5")},
},
"out of gas panic with no gas limit": {
submsgID: 7,
Expand All @@ -263,23 +263,23 @@ func TestDispatchSubMsgErrorHandling(t *testing.T) {
msg: validBankSend,
gasLimit: &subGasLimit,
// uses same gas as call without limit (note we do not charge the 40k on reply)
resultAssertions: []assertion{assertReturnedEvents(0), assertGasUsed(110_000, 113_000)},
resultAssertions: []assertion{assertReturnedEvents(0), assertGasUsed(110_000, 112_000)},
},
"not enough tokens with limit": {
submsgID: 16,
msg: invalidBankSend,
subMsgError: true,
gasLimit: &subGasLimit,
// uses same gas as call without limit (note we do not charge the 40k on reply)
resultAssertions: []assertion{assertGasUsed(78_000, 82_000), assertErrorString("codespace: sdk, code: 5")},
resultAssertions: []assertion{assertGasUsed(78_000, 81_000), assertErrorString("codespace: sdk, code: 5")},
},
"out of gas caught with gas limit": {
submsgID: 17,
msg: infiniteLoop,
subMsgError: true,
gasLimit: &subGasLimit,
// uses all the subGasLimit, plus the 52k or so for the main contract
resultAssertions: []assertion{assertGasUsed(subGasLimit+75_000, subGasLimit+78_000), assertErrorString("codespace: sdk, code: 11")},
resultAssertions: []assertion{assertGasUsed(subGasLimit+75_000, subGasLimit+77_000), assertErrorString("codespace: sdk, code: 11")},
},
"instantiate contract gets address in data and events": {
submsgID: 21,
Expand Down
6 changes: 3 additions & 3 deletions x/wasm/types/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,11 +86,11 @@ func GasRegisterFromContext(ctx context.Context) (GasRegister, bool) {
}

// WithTxContracts stores the tx contracts into the context returned
func WithTxContracts(ctx sdk.Context, c TxContracts) sdk.Context {
if c == nil {
func WithTxContracts(ctx sdk.Context, tc TxContracts) sdk.Context {
if tc.GetContracts() == nil {
panic("tx contracts must not be nil")

Check warning on line 91 in x/wasm/types/context.go

View check run for this annotation

Codecov / codecov/patch

x/wasm/types/context.go#L89-L91

Added lines #L89 - L91 were not covered by tests
}
return ctx.WithValue(contextKeyTxContracts, c)
return ctx.WithValue(contextKeyTxContracts, tc)

Check warning on line 93 in x/wasm/types/context.go

View check run for this annotation

Codecov / codecov/patch

x/wasm/types/context.go#L93

Added line #L93 was not covered by tests
}

// TxContractsFromContext reads the tx contracts from the context
Expand Down
24 changes: 18 additions & 6 deletions x/wasm/types/types.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package types

import (
"encoding/hex"
"fmt"
"reflect"

Expand Down Expand Up @@ -430,17 +431,28 @@ func (a AccessConfig) AllAuthorizedAddresses() []string {
return []string{}
}

type TxContracts map[string]struct{}
type txContracts map[string]struct{}

type TxContracts struct {
contracts txContracts
}

func NewTxContracts() TxContracts {
return make(TxContracts, 0)
c := make(txContracts, 0)
return TxContracts{contracts: c}

Check warning on line 442 in x/wasm/types/types.go

View check run for this annotation

Codecov / codecov/patch

x/wasm/types/types.go#L440-L442

Added lines #L440 - L442 were not covered by tests
}

func (a TxContracts) AddContract(checksum string) {
a[checksum] = struct{}{}
func (tc TxContracts) AddContract(checksum []byte) {
hexHash := hex.EncodeToString(checksum)
tc.contracts[hexHash] = struct{}{}

Check warning on line 447 in x/wasm/types/types.go

View check run for this annotation

Codecov / codecov/patch

x/wasm/types/types.go#L445-L447

Added lines #L445 - L447 were not covered by tests
}

func (a TxContracts) Exists(checksum string) bool {
_, ok := a[checksum]
func (tc TxContracts) Exists(checksum []byte) bool {
hexHash := hex.EncodeToString(checksum)
_, ok := tc.contracts[hexHash]
return ok

Check warning on line 453 in x/wasm/types/types.go

View check run for this annotation

Codecov / codecov/patch

x/wasm/types/types.go#L450-L453

Added lines #L450 - L453 were not covered by tests
}

func (tc TxContracts) GetContracts() txContracts {
return tc.contracts

Check warning on line 457 in x/wasm/types/types.go

View check run for this annotation

Codecov / codecov/patch

x/wasm/types/types.go#L456-L457

Added lines #L456 - L457 were not covered by tests
}

0 comments on commit 40c7669

Please sign in to comment.