Skip to content

Commit

Permalink
do contract validation before setting before send hook
Browse files Browse the repository at this point in the history
  • Loading branch information
beer-1 committed Jan 14, 2025
1 parent a8c1dab commit cd9e13c
Show file tree
Hide file tree
Showing 3 changed files with 104 additions and 64 deletions.
23 changes: 23 additions & 0 deletions x/tokenfactory/keeper/before_send.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package keeper
import (
"context"
"encoding/json"
"strings"

storetypes "cosmossdk.io/store/types"

Expand All @@ -24,6 +25,28 @@ func (k Keeper) setBeforeSendHook(ctx context.Context, denom string, cosmwasmAdd
// delete the store for denom prefix store when cosmwasm address is nil
if cosmwasmAddress == "" {
return k.DenomHookAddr.Remove(ctx, denom)
} else {
// if a contract is being set, call the contract using cache context
// to test if the contract is an existing, valid contract.
cacheCtx, _ := sdk.UnwrapSDKContext(ctx).CacheContext()

cwAddr, err := sdk.AccAddressFromBech32(cosmwasmAddress)
if err != nil {
return err
}

tempMsg := types.TrackBeforeSendSudoMsg{
TrackBeforeSend: types.TrackBeforeSendMsg{},
}
msgBz, err := json.Marshal(tempMsg)
if err != nil {
return err
}
_, err = k.contractKeeper.Sudo(cacheCtx, cwAddr, msgBz)

if err != nil && strings.Contains(err.Error(), "no such contract") {
return err
}
}

_, err = k.ac.StringToBytes(cosmwasmAddress)
Expand Down
29 changes: 23 additions & 6 deletions x/tokenfactory/keeper/before_send_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,12 +147,13 @@ func TestBeforeSendHook(t *testing.T) {
// to properly test if we are gas metering trackBeforeSend properly.
func TestInfiniteTrackBeforeSend(t *testing.T) {
for _, tc := range []struct {
name string
wasmFile string
tokenToSend sdk.Coins
useFactoryDenom bool
blockBeforeSend bool
expectedError bool
name string
wasmFile string
tokenToSend sdk.Coins
useFactoryDenom bool
blockBeforeSend bool
expectedError bool
useInvalidContract bool
}{
{
name: "sending tokenfactory denom from module to module with infinite contract should panic",
Expand All @@ -176,6 +177,13 @@ func TestInfiniteTrackBeforeSend(t *testing.T) {
wasmFile: "./testdata/no100.wasm",
useFactoryDenom: true,
},
{
name: "Try using invalid contract",
wasmFile: "./testdata/no100.wasm",
useFactoryDenom: true,
useInvalidContract: true,
expectedError: true,
},
} {
t.Run(fmt.Sprintf("Case %s", tc.name), func(t *testing.T) {
ctx, input := createDefaultTestInput(t)
Expand Down Expand Up @@ -211,9 +219,18 @@ func TestInfiniteTrackBeforeSend(t *testing.T) {
input.Faucet.Fund(ctx, input.AccountKeeper.GetModuleAccount(ctx, authtypes.Minter).GetAddress(), tokenToSend...)
}

if tc.useInvalidContract {
cosmwasmAddress = make(sdk.AccAddress, 32)
}

// set beforesend hook to the new denom
// we register infinite loop contract here to test if we are gas metering properly
_, err = msgServer.SetBeforeSendHook(ctx, types.NewMsgSetBeforeSendHook(addrs[0].String(), factoryDenom, cosmwasmAddress.String()))
if tc.useInvalidContract {
require.Error(t, err, "test: %v", tc.name)
return
}

require.NoError(t, err, "test: %v", tc.name)

if tc.blockBeforeSend {
Expand Down
116 changes: 58 additions & 58 deletions x/tokenfactory/types/tx.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit cd9e13c

Please sign in to comment.