-
Notifications
You must be signed in to change notification settings - Fork 109
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(
app.go
): disable vesting account creation to prevent contract a…
…ddress bricking (#923) * add disabled messages * add other vesting messages * option refactor * add vesting ante handler * implement test for vesting ante handler * lint issues * fix lint 2 * fix lint 3 --------- Co-authored-by: Tanmay <[email protected]>
- Loading branch information
Showing
6 changed files
with
192 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package ante_test | ||
|
||
import sdk "github.com/cosmos/cosmos-sdk/types" | ||
|
||
var _ sdk.AnteHandler = (&MockAnteHandler{}).AnteHandle | ||
|
||
// MockAnteHandler mocks an AnteHandler | ||
type MockAnteHandler struct { | ||
WasCalled bool | ||
CalledCtx sdk.Context | ||
} | ||
|
||
// AnteHandle implements AnteHandler | ||
func (mah *MockAnteHandler) AnteHandle(ctx sdk.Context, _ sdk.Tx, _ bool) (sdk.Context, error) { | ||
mah.WasCalled = true | ||
mah.CalledCtx = ctx | ||
return ctx, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package ante | ||
|
||
import ( | ||
errorsmod "cosmossdk.io/errors" | ||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors" | ||
vesting "github.com/cosmos/cosmos-sdk/x/auth/vesting/types" | ||
) | ||
|
||
var _ sdk.AnteDecorator = VestingAccountDecorator{} | ||
|
||
// VestingAccountDecorator blocks vesting messages from reaching the mempool | ||
type VestingAccountDecorator struct { | ||
disabledMsgTypeURLs []string | ||
} | ||
|
||
// NewVestingAccountDecorator creates a decorator to block vesting messages from reaching the mempool | ||
func NewVestingAccountDecorator() VestingAccountDecorator { | ||
return VestingAccountDecorator{ | ||
disabledMsgTypeURLs: []string{ | ||
sdk.MsgTypeURL(&vesting.MsgCreateVestingAccount{}), | ||
sdk.MsgTypeURL(&vesting.MsgCreatePermanentLockedAccount{}), | ||
sdk.MsgTypeURL(&vesting.MsgCreatePeriodicVestingAccount{}), | ||
}, | ||
} | ||
} | ||
|
||
// AnteHandle implements AnteDecorator | ||
func (vad VestingAccountDecorator) AnteHandle( | ||
ctx sdk.Context, | ||
tx sdk.Tx, | ||
simulate bool, | ||
next sdk.AnteHandler, | ||
) (newCtx sdk.Context, err error) { | ||
for _, msg := range tx.GetMsgs() { | ||
typeURL := sdk.MsgTypeURL(msg) | ||
|
||
for _, disabledTypeURL := range vad.disabledMsgTypeURLs { | ||
if typeURL == disabledTypeURL { | ||
return ctx, errorsmod.Wrapf( | ||
sdkerrors.ErrUnauthorized, | ||
"MsgTypeURL %s not supported", | ||
typeURL, | ||
) | ||
} | ||
} | ||
} | ||
|
||
return next(ctx, tx, simulate) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
package ante_test | ||
|
||
import ( | ||
"math/rand" | ||
"testing" | ||
"time" | ||
|
||
"github.com/cosmos/cosmos-sdk/simapp/helpers" | ||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
vesting "github.com/cosmos/cosmos-sdk/x/auth/vesting/types" | ||
banktypes "github.com/cosmos/cosmos-sdk/x/bank/types" | ||
"github.com/stretchr/testify/require" | ||
|
||
"github.com/zeta-chain/zetacore/app" | ||
"github.com/zeta-chain/zetacore/app/ante" | ||
"github.com/zeta-chain/zetacore/testutil/sample" | ||
) | ||
|
||
func TestVesting_AnteHandle(t *testing.T) { | ||
txConfig := app.MakeEncodingConfig().TxConfig | ||
|
||
testPrivKey, testAddress := sample.PrivKeyAddressPair() | ||
_, testAddress2 := sample.PrivKeyAddressPair() | ||
|
||
decorator := ante.NewVestingAccountDecorator() | ||
|
||
tests := []struct { | ||
name string | ||
msg sdk.Msg | ||
wantHasErr bool | ||
wantErr string | ||
}{ | ||
{ | ||
"MsgCreateVestingAccount", | ||
vesting.NewMsgCreateVestingAccount( | ||
testAddress, testAddress2, | ||
sdk.NewCoins(sdk.NewInt64Coin("azeta", 100_000_000)), | ||
time.Date(1998, 1, 1, 0, 0, 0, 0, time.UTC).Unix(), | ||
false, | ||
), | ||
true, | ||
"MsgTypeURL /cosmos.vesting.v1beta1.MsgCreateVestingAccount not supported", | ||
}, | ||
{ | ||
"MsgCreatePermanentLockedAccount", | ||
vesting.NewMsgCreatePermanentLockedAccount( | ||
testAddress, testAddress2, | ||
sdk.NewCoins(sdk.NewInt64Coin("azeta", 100_000_000)), | ||
), | ||
true, | ||
"MsgTypeURL /cosmos.vesting.v1beta1.MsgCreatePermanentLockedAccount not supported", | ||
}, | ||
{ | ||
"MsgCreatePeriodicVestingAccount", | ||
vesting.NewMsgCreatePeriodicVestingAccount( | ||
testAddress, testAddress2, | ||
time.Date(1998, 1, 1, 0, 0, 0, 0, time.UTC).Unix(), | ||
nil, | ||
), | ||
true, | ||
"MsgTypeURL /cosmos.vesting.v1beta1.MsgCreatePeriodicVestingAccount not supported", | ||
}, | ||
{ | ||
"Non blocked message", | ||
banktypes.NewMsgSend( | ||
testAddress, testAddress2, | ||
sdk.NewCoins(sdk.NewInt64Coin("azeta", 100_000_000)), | ||
), | ||
false, | ||
"", | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
tx, err := helpers.GenSignedMockTx( | ||
rand.New(rand.NewSource(time.Now().UnixNano())), | ||
txConfig, | ||
[]sdk.Msg{ | ||
tt.msg, | ||
}, | ||
sdk.NewCoins(), | ||
helpers.DefaultGenTxGas, | ||
"testing-chain-id", | ||
[]uint64{0}, | ||
[]uint64{0}, | ||
testPrivKey, | ||
) | ||
require.NoError(t, err) | ||
|
||
mmd := MockAnteHandler{} | ||
ctx := sdk.Context{}.WithIsCheckTx(true) | ||
|
||
_, err = decorator.AnteHandle(ctx, tx, false, mmd.AnteHandle) | ||
|
||
if tt.wantHasErr { | ||
require.Error(t, err) | ||
require.Contains(t, err.Error(), tt.wantErr) | ||
} else { | ||
require.NoError(t, err) | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters