github.com/fibonacci-chain/fbc@v0.0.0-20231124064014-c7636198c1e9/app/ante/AccountBlockedVerificationDecorator.go (about)

     1  package ante
     2  
     3  import (
     4  	sdk "github.com/fibonacci-chain/fbc/libs/cosmos-sdk/types"
     5  	sdkerrors "github.com/fibonacci-chain/fbc/libs/cosmos-sdk/types/errors"
     6  	evmtypes "github.com/fibonacci-chain/fbc/x/evm/types"
     7  )
     8  
     9  // AccountBlockedVerificationDecorator check whether signer is blocked.
    10  type AccountBlockedVerificationDecorator struct {
    11  	evmKeeper EVMKeeper
    12  }
    13  
    14  // NewAccountBlockedVerificationDecorator creates a new AccountBlockedVerificationDecorator instance
    15  func NewAccountBlockedVerificationDecorator(evmKeeper EVMKeeper) AccountBlockedVerificationDecorator {
    16  	return AccountBlockedVerificationDecorator{
    17  		evmKeeper: evmKeeper,
    18  	}
    19  }
    20  
    21  // AnteHandle check wether signer of tx(contains cosmos-tx and eth-tx) is blocked.
    22  func (abvd AccountBlockedVerificationDecorator) AnteHandle(ctx sdk.Context, tx sdk.Tx, simulate bool, next sdk.AnteHandler) (sdk.Context, error) {
    23  	// simulate means 'eth_call' or 'eth_estimateGas', when it means 'eth_estimateGas' we can not 'VerifySig'.so skip here
    24  	if simulate {
    25  		return next(ctx, tx, simulate)
    26  	}
    27  	pinAnte(ctx.AnteTracer(), "AccountBlockedVerificationDecorator")
    28  
    29  	var signers []sdk.AccAddress
    30  	if ethTx, ok := tx.(*evmtypes.MsgEthereumTx); ok {
    31  		signers = ethTx.GetSigners()
    32  	} else {
    33  		signers = tx.GetSigners()
    34  	}
    35  
    36  	currentGasMeter := ctx.GasMeter()
    37  	infGasMeter := sdk.GetReusableInfiniteGasMeter()
    38  	ctx.SetGasMeter(infGasMeter)
    39  
    40  	for _, signer := range signers {
    41  		//TODO it may be optimizate by cache blockedAddressList
    42  		if ok := abvd.evmKeeper.IsAddressBlocked(ctx, signer); ok {
    43  			ctx.SetGasMeter(currentGasMeter)
    44  			sdk.ReturnInfiniteGasMeter(infGasMeter)
    45  			return ctx, sdkerrors.Wrapf(sdkerrors.ErrUnknownRequest, "address: %s has been blocked", signer.String())
    46  		}
    47  	}
    48  	ctx.SetGasMeter(currentGasMeter)
    49  	sdk.ReturnInfiniteGasMeter(infGasMeter)
    50  	return next(ctx, tx, simulate)
    51  }