Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Deduct gas fee from zkauth address #1279

Merged
merged 4 commits into from
Mar 18, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 60 additions & 2 deletions x/zkauth/ante/zkauth.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package ante

import (
"fmt"

"github.com/Finschia/finschia-sdk/crypto/types"
sdk "github.com/Finschia/finschia-sdk/types"
sdkerrors "github.com/Finschia/finschia-sdk/types/errors"
Expand Down Expand Up @@ -143,7 +145,7 @@
}

func (zdf ZKAuthDeductFeeDecorator) AnteHandle(ctx sdk.Context, tx sdk.Tx, simulate bool, next sdk.AnteHandler) (newCtx sdk.Context, err error) {
isZKAuthTx, _, _, err := getZKAuthInfoFromTx(tx)
isZKAuthTx, zkMsgs, _, err := getZKAuthInfoFromTx(tx)
if err != nil {
return ctx, err
}
Expand All @@ -152,7 +154,63 @@
return zdf.dfd.AnteHandle(ctx, tx, simulate, next)
}

// Case of zkauth msg, does nothing in this case
// Almost no different from the implementation of zdf.dfd.AnteHandle, uses ZKauthAddress for feePayer.
feeTx, ok := tx.(sdk.FeeTx)
if !ok {
return ctx, sdkerrors.Wrap(sdkerrors.ErrTxDecode, "Tx must be a FeeTx")
}

Check warning on line 161 in x/zkauth/ante/zkauth.go

View check run for this annotation

Codecov / codecov/patch

x/zkauth/ante/zkauth.go#L160-L161

Added lines #L160 - L161 were not covered by tests

if addr := zdf.ak.GetModuleAddress(authtypes.FeeCollectorName); addr.Empty() {
return ctx, fmt.Errorf("fee collector module account (%s) has not been set", authtypes.FeeCollectorName)
shiki-tak marked this conversation as resolved.
Show resolved Hide resolved
}

Check warning on line 165 in x/zkauth/ante/zkauth.go

View check run for this annotation

Codecov / codecov/patch

x/zkauth/ante/zkauth.go#L164-L165

Added lines #L164 - L165 were not covered by tests

fee := feeTx.GetFee()
signature := zkMsgs[0].GetZkAuthSignature()
shiki-tak marked this conversation as resolved.
Show resolved Hide resolved
feePayer, err := signature.GetZkAuthInputs().AccAddress()
if err != nil {
return ctx, err
}

Check warning on line 172 in x/zkauth/ante/zkauth.go

View check run for this annotation

Codecov / codecov/patch

x/zkauth/ante/zkauth.go#L171-L172

Added lines #L171 - L172 were not covered by tests
feeGranter := feeTx.FeeGranter()

deductFeesFrom := feePayer

// if feegranter set deduct fee from feegranter account.
// this works with only when feegrant enabled.
if feeGranter != nil {
if zdf.feegrantKeeper == nil {
return ctx, sdkerrors.Wrap(sdkerrors.ErrInvalidRequest, "fee grants are not enabled")
} else if !feeGranter.Equals(feePayer) {
err := zdf.feegrantKeeper.UseGrantedFees(ctx, feeGranter, feePayer, fee, tx.GetMsgs())
if err != nil {
return ctx, sdkerrors.Wrapf(err, "%s not allowed to pay fees from %s", feeGranter, feePayer)
}

Check warning on line 186 in x/zkauth/ante/zkauth.go

View check run for this annotation

Codecov / codecov/patch

x/zkauth/ante/zkauth.go#L180-L186

Added lines #L180 - L186 were not covered by tests
}

deductFeesFrom = feeGranter

Check warning on line 189 in x/zkauth/ante/zkauth.go

View check run for this annotation

Codecov / codecov/patch

x/zkauth/ante/zkauth.go#L189

Added line #L189 was not covered by tests
}

deductFeesFromAcc := zdf.ak.GetAccount(ctx, deductFeesFrom)
if deductFeesFromAcc == nil {
return ctx, sdkerrors.Wrapf(sdkerrors.ErrUnknownAddress, "fee payer address: %s does not exist", deductFeesFrom)
}

Check warning on line 195 in x/zkauth/ante/zkauth.go

View check run for this annotation

Codecov / codecov/patch

x/zkauth/ante/zkauth.go#L194-L195

Added lines #L194 - L195 were not covered by tests

// deduct the fees
if !feeTx.GetFee().IsZero() {
err = authante.DeductFees(zdf.bankKeeper, ctx, deductFeesFromAcc, feeTx.GetFee())
if err != nil {
return ctx, err
}

Check warning on line 202 in x/zkauth/ante/zkauth.go

View check run for this annotation

Codecov / codecov/patch

x/zkauth/ante/zkauth.go#L201-L202

Added lines #L201 - L202 were not covered by tests
}

events := sdk.Events{
sdk.NewEvent(
sdk.EventTypeTx,
sdk.NewAttribute(sdk.AttributeKeyFee, fee.String()),
sdk.NewAttribute(sdk.AttributeKeyFeePayer, deductFeesFrom.String()),
),
}
ctx.EventManager().EmitEvents(events)

return next(ctx, tx, simulate)
}

Expand Down
7 changes: 7 additions & 0 deletions x/zkauth/ante/zkauth_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ func TestNewDecorators(t *testing.T) {
}
accounts, err := f.CreateTestAccounts(2)
require.NoError(t, err)
zkauthAddress, err := f.AddTestAccounts([]string{"link1g7ud63eqllj7zj4q7fkca5h7s223j78tyvr0e2cxuw4qyyaaf3usa64dqc"})
require.NoError(t, err)

// bank msg
subMsg := &banktype.MsgSend{
Expand All @@ -50,6 +52,7 @@ func TestNewDecorators(t *testing.T) {
err = f.TxBuilder.SetMsgs(msg)
require.NoError(t, err)

f.TxBuilder.SetFeeAmount(sdk.NewCoins(sdk.NewCoin("cony", sdk.NewInt(1000))))
ephPubKey, ok := new(big.Int).SetString("18948426102457371978524559226152399917062673825697601263047735920285791872240", 10)
require.True(t, ok)
pub := secp256k1.PubKey{Key: ephPubKey.Bytes()}
Expand All @@ -62,4 +65,8 @@ func TestNewDecorators(t *testing.T) {
})
require.NoError(t, err)
}

balance, err := f.BankKeeper.Balance(sdk.WrapSDKContext(f.Ctx), &banktype.QueryBalanceRequest{zkauthAddress[0].GetAddress().String(), "cony"})
require.NoError(t, err)
require.Equal(t, sdk.NewInt(9999000), balance.Balance.Amount)
}
7 changes: 4 additions & 3 deletions x/zkauth/testutil/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
authtypes "github.com/Finschia/finschia-sdk/x/auth/types"
banktypes "github.com/Finschia/finschia-sdk/x/bank/types"
bankpluskeeper "github.com/Finschia/finschia-sdk/x/bankplus/keeper"
feegrant "github.com/Finschia/finschia-sdk/x/feegrant"
feegrantkeeper "github.com/Finschia/finschia-sdk/x/feegrant/keeper"
minttypes "github.com/Finschia/finschia-sdk/x/mint/types"
"github.com/Finschia/finschia-sdk/x/zkauth/keeper"
Expand Down Expand Up @@ -92,12 +93,12 @@ func ZkAuthKeeper(t testing.TB) TestApp {

clientCtx := client.Context{}.WithTxConfig(encodingConfig.TxConfig)
authKeeper := authkeeper.NewAccountKeeper(
appCodec, app.GetKey(types.StoreKey), app.GetSubspace(authtypes.ModuleName),
appCodec, app.GetKey(authtypes.StoreKey), app.GetSubspace(authtypes.ModuleName),
authtypes.ProtoBaseAccount, maccPerms,
)
feeGrantKeeper := feegrantkeeper.NewKeeper(appCodec, app.GetKey(types.StoreKey), authKeeper)
feeGrantKeeper := feegrantkeeper.NewKeeper(appCodec, app.GetKey(feegrant.StoreKey), authKeeper)
bankKeeper := bankpluskeeper.NewBaseKeeper(
appCodec, app.GetKey(types.StoreKey), authKeeper, app.GetSubspace(banktypes.ModuleName), app.BlockedAddrs(), false)
appCodec, app.GetKey(banktypes.StoreKey), authKeeper, app.GetSubspace(banktypes.ModuleName), app.BlockedAddrs(), false)

testApp := TestApp{
Simapp: app,
Expand Down
Loading