Skip to content

Commit cd9e13c

Browse files
committed
do contract validation before setting before send hook
1 parent a8c1dab commit cd9e13c

File tree

3 files changed

+104
-64
lines changed

3 files changed

+104
-64
lines changed

x/tokenfactory/keeper/before_send.go

+23
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package keeper
33
import (
44
"context"
55
"encoding/json"
6+
"strings"
67

78
storetypes "cosmossdk.io/store/types"
89

@@ -24,6 +25,28 @@ func (k Keeper) setBeforeSendHook(ctx context.Context, denom string, cosmwasmAdd
2425
// delete the store for denom prefix store when cosmwasm address is nil
2526
if cosmwasmAddress == "" {
2627
return k.DenomHookAddr.Remove(ctx, denom)
28+
} else {
29+
// if a contract is being set, call the contract using cache context
30+
// to test if the contract is an existing, valid contract.
31+
cacheCtx, _ := sdk.UnwrapSDKContext(ctx).CacheContext()
32+
33+
cwAddr, err := sdk.AccAddressFromBech32(cosmwasmAddress)
34+
if err != nil {
35+
return err
36+
}
37+
38+
tempMsg := types.TrackBeforeSendSudoMsg{
39+
TrackBeforeSend: types.TrackBeforeSendMsg{},
40+
}
41+
msgBz, err := json.Marshal(tempMsg)
42+
if err != nil {
43+
return err
44+
}
45+
_, err = k.contractKeeper.Sudo(cacheCtx, cwAddr, msgBz)
46+
47+
if err != nil && strings.Contains(err.Error(), "no such contract") {
48+
return err
49+
}
2750
}
2851

2952
_, err = k.ac.StringToBytes(cosmwasmAddress)

x/tokenfactory/keeper/before_send_test.go

+23-6
Original file line numberDiff line numberDiff line change
@@ -147,12 +147,13 @@ func TestBeforeSendHook(t *testing.T) {
147147
// to properly test if we are gas metering trackBeforeSend properly.
148148
func TestInfiniteTrackBeforeSend(t *testing.T) {
149149
for _, tc := range []struct {
150-
name string
151-
wasmFile string
152-
tokenToSend sdk.Coins
153-
useFactoryDenom bool
154-
blockBeforeSend bool
155-
expectedError bool
150+
name string
151+
wasmFile string
152+
tokenToSend sdk.Coins
153+
useFactoryDenom bool
154+
blockBeforeSend bool
155+
expectedError bool
156+
useInvalidContract bool
156157
}{
157158
{
158159
name: "sending tokenfactory denom from module to module with infinite contract should panic",
@@ -176,6 +177,13 @@ func TestInfiniteTrackBeforeSend(t *testing.T) {
176177
wasmFile: "./testdata/no100.wasm",
177178
useFactoryDenom: true,
178179
},
180+
{
181+
name: "Try using invalid contract",
182+
wasmFile: "./testdata/no100.wasm",
183+
useFactoryDenom: true,
184+
useInvalidContract: true,
185+
expectedError: true,
186+
},
179187
} {
180188
t.Run(fmt.Sprintf("Case %s", tc.name), func(t *testing.T) {
181189
ctx, input := createDefaultTestInput(t)
@@ -211,9 +219,18 @@ func TestInfiniteTrackBeforeSend(t *testing.T) {
211219
input.Faucet.Fund(ctx, input.AccountKeeper.GetModuleAccount(ctx, authtypes.Minter).GetAddress(), tokenToSend...)
212220
}
213221

222+
if tc.useInvalidContract {
223+
cosmwasmAddress = make(sdk.AccAddress, 32)
224+
}
225+
214226
// set beforesend hook to the new denom
215227
// we register infinite loop contract here to test if we are gas metering properly
216228
_, err = msgServer.SetBeforeSendHook(ctx, types.NewMsgSetBeforeSendHook(addrs[0].String(), factoryDenom, cosmwasmAddress.String()))
229+
if tc.useInvalidContract {
230+
require.Error(t, err, "test: %v", tc.name)
231+
return
232+
}
233+
217234
require.NoError(t, err, "test: %v", tc.name)
218235

219236
if tc.blockBeforeSend {

x/tokenfactory/types/tx.pb.go

+58-58
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)