Skip to content

Commit

Permalink
feat: add sender to revert context (#2919)
Browse files Browse the repository at this point in the history
* e2e tests and modifications for authenticated call

* extend test with sender check and revert case

* separate tests into separate files

* cleanup

* withdraw and call support and tests

* bump protocol contracts

* split tests into separate files

* small cleanup

* fmt

* generate

* lint

* changelog

* PR  comments

* fix case in proto

* bump vote inbound gas limit in zetaclient

* fix test

* generate

* fixing tests

* call options non empty

* generate

* test fix

* rename gateway caller

* pr comments rename tests

* PR comment

* generate

* tests

* add sender in test contract

* extend e2e tests

* generate

* changelog

* PR comment

* generate

* update tests fixes

* tests fixes

* fix

* test fix

* gas limit fixes

* PR comment fix

* fix bad merge
  • Loading branch information
skosito authored Sep 27, 2024
1 parent 3568cd4 commit 3eeb78a
Show file tree
Hide file tree
Showing 20 changed files with 90 additions and 21 deletions.
1 change: 1 addition & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
* [2907](https://github.com/zeta-chain/node/pull/2907) - derive Bitcoin tss address by chain id and added more Signet static info
* [2911](https://github.com/zeta-chain/node/pull/2911) - add chain static information for btc testnet4
* [2904](https://github.com/zeta-chain/node/pull/2904) - integrate authenticated calls smart contract functionality into protocol
* [2919](https://github.com/zeta-chain/node/pull/2919) - add inbound sender to revert context

### Refactor

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package e2etests
import (
"math/big"

"github.com/ethereum/go-ethereum/accounts/abi/bind"
"github.com/stretchr/testify/require"
"github.com/zeta-chain/protocol-contracts/v2/pkg/gatewayevm.sol"

Expand Down Expand Up @@ -38,4 +39,12 @@ func TestV2ERC20DepositAndCallRevertWithCall(r *runner.E2ERunner, args []string)

// check the payload was received on the contract
r.AssertTestDAppEVMCalled(true, payloadMessageDepositOnRevertERC20, big.NewInt(0))

// check expected sender was used
senderForMsg, err := r.TestDAppV2EVM.SenderWithMessage(
&bind.CallOpts{},
[]byte(payloadMessageDepositOnRevertERC20),
)
require.NoError(r, err)
require.Equal(r, r.EVMAuth.From, senderForMsg)
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package e2etests
import (
"math/big"

"github.com/ethereum/go-ethereum/accounts/abi/bind"
"github.com/stretchr/testify/require"
"github.com/zeta-chain/protocol-contracts/v2/pkg/gatewayzevm.sol"

Expand Down Expand Up @@ -43,4 +44,12 @@ func TestV2ERC20WithdrawAndCallRevertWithCall(r *runner.E2ERunner, args []string
require.Equal(r, crosschaintypes.CctxStatus_Reverted, cctx.CctxStatus.Status)

r.AssertTestDAppZEVMCalled(true, payloadMessageWithdrawOnRevertERC20, big.NewInt(0))

// check expected sender was used
senderForMsg, err := r.TestDAppV2ZEVM.SenderWithMessage(
&bind.CallOpts{},
[]byte(payloadMessageWithdrawOnRevertERC20),
)
require.NoError(r, err)
require.Equal(r, r.ZEVMAuth.From, senderForMsg)
}
9 changes: 9 additions & 0 deletions e2e/e2etests/test_v2_eth_deposit_and_call_revert_with_call.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package e2etests
import (
"math/big"

"github.com/ethereum/go-ethereum/accounts/abi/bind"
"github.com/stretchr/testify/require"
"github.com/zeta-chain/protocol-contracts/v2/pkg/gatewayevm.sol"

Expand Down Expand Up @@ -38,4 +39,12 @@ func TestV2ETHDepositAndCallRevertWithCall(r *runner.E2ERunner, args []string) {

// check the payload was received on the contract
r.AssertTestDAppEVMCalled(true, payloadMessageDepositOnRevertETH, big.NewInt(0))

// check expected sender was used
senderForMsg, err := r.TestDAppV2EVM.SenderWithMessage(
&bind.CallOpts{},
[]byte(payloadMessageDepositOnRevertETH),
)
require.NoError(r, err)
require.Equal(r, r.EVMAuth.From, senderForMsg)
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package e2etests
import (
"math/big"

"github.com/ethereum/go-ethereum/accounts/abi/bind"
"github.com/stretchr/testify/require"
"github.com/zeta-chain/protocol-contracts/v2/pkg/gatewayzevm.sol"

Expand Down Expand Up @@ -42,4 +43,12 @@ func TestV2ETHWithdrawAndCallRevertWithCall(r *runner.E2ERunner, args []string)
require.Equal(r, crosschaintypes.CctxStatus_Reverted, cctx.CctxStatus.Status)

r.AssertTestDAppZEVMCalled(true, payloadMessageWithdrawOnRevertETH, big.NewInt(0))

// check expected sender was used
senderForMsg, err := r.TestDAppV2ZEVM.SenderWithMessage(
&bind.CallOpts{},
[]byte(payloadMessageWithdrawOnRevertETH),
)
require.NoError(r, err)
require.Equal(r, r.ZEVMAuth.From, senderForMsg)
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ require (
github.com/stretchr/testify v1.9.0
github.com/zeta-chain/ethermint v0.0.0-20240909234716-2fad916e7179
github.com/zeta-chain/keystone/keys v0.0.0-20240826165841-3874f358c138
github.com/zeta-chain/protocol-contracts v1.0.2-athens3.0.20240920151810-cabe34920d52
github.com/zeta-chain/protocol-contracts v1.0.2-athens3.0.20240924201108-3a274ce7bad0
gitlab.com/thorchain/tss/go-tss v1.6.5
go.nhat.io/grpcmock v0.25.0
golang.org/x/crypto v0.23.0
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -4182,8 +4182,8 @@ github.com/zeta-chain/go-tss v0.0.0-20240916173049-89fee4b0ae7f h1:XqUvw9a3EnDa2
github.com/zeta-chain/go-tss v0.0.0-20240916173049-89fee4b0ae7f/go.mod h1:B1FDE6kHs8hozKSX1/iXgCdvlFbS6+FeAupoBHDK0Cc=
github.com/zeta-chain/keystone/keys v0.0.0-20240826165841-3874f358c138 h1:vck/FcIIpFOvpBUm0NO17jbEtmSz/W/a5Y4jRuSJl6I=
github.com/zeta-chain/keystone/keys v0.0.0-20240826165841-3874f358c138/go.mod h1:U494OsZTWsU75hqoriZgMdSsgSGP1mUL1jX+wN/Aez8=
github.com/zeta-chain/protocol-contracts v1.0.2-athens3.0.20240920151810-cabe34920d52 h1:DlSY9awQteXVmvY0lPD4Or83iuL4P5KwSGky+n4mYio=
github.com/zeta-chain/protocol-contracts v1.0.2-athens3.0.20240920151810-cabe34920d52/go.mod h1:SjT7QirtJE8stnAe1SlNOanxtfSfijJm3MGJ+Ax7w7w=
github.com/zeta-chain/protocol-contracts v1.0.2-athens3.0.20240924201108-3a274ce7bad0 h1:GbfO2dyjSAWqBH0xDIttwPB2fE5A+zw0UUbEoW3S3wU=
github.com/zeta-chain/protocol-contracts v1.0.2-athens3.0.20240924201108-3a274ce7bad0/go.mod h1:SjT7QirtJE8stnAe1SlNOanxtfSfijJm3MGJ+Ax7w7w=
github.com/zeta-chain/tss-lib v0.0.0-20240916163010-2e6b438bd901 h1:9whtN5fjYHfk4yXIuAsYP2EHxImwDWDVUOnZJ2pfL3w=
github.com/zeta-chain/tss-lib v0.0.0-20240916163010-2e6b438bd901/go.mod h1:d2iTC62s9JwKiCMPhcDDXbIZmuzAyJ4lwso0H5QyRbk=
github.com/zondax/hid v0.9.1/go.mod h1:l5wttcP0jwtdLjqjMMWFVEE7d1zO0jvSPA9OPZxWpEM=
Expand Down
5 changes: 5 additions & 0 deletions pkg/contracts/testdappv2/TestDAppV2.abi
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,11 @@
"inputs": [
{
"components": [
{
"internalType": "address",
"name": "sender",
"type": "address"
},
{
"internalType": "address",
"name": "asset",
Expand Down
2 changes: 1 addition & 1 deletion pkg/contracts/testdappv2/TestDAppV2.bin

Large diffs are not rendered by default.

17 changes: 9 additions & 8 deletions pkg/contracts/testdappv2/TestDAppV2.go

Large diffs are not rendered by default.

7 changes: 6 additions & 1 deletion pkg/contracts/testdappv2/TestDAppV2.json

Large diffs are not rendered by default.

3 changes: 3 additions & 0 deletions pkg/contracts/testdappv2/TestDAppV2.sol
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,12 @@ contract TestDAppV2 {
}

/// @notice Struct containing revert context passed to onRevert.
/// @param sender Address of account that initiated smart contract call.
/// @param asset Address of asset, empty if it's gas token.
/// @param amount Amount specified with the transaction.
/// @param revertMessage Arbitrary data sent back in onRevert.
struct RevertContext {
address sender;
address asset;
uint64 amount;
bytes revertMessage;
Expand Down Expand Up @@ -100,6 +102,7 @@ contract TestDAppV2 {
function onRevert(RevertContext calldata revertContext) external {
setCalledWithMessage(string(revertContext.revertMessage));
setAmountWithMessage(string(revertContext.revertMessage), 0);
senderWithMessage[revertContext.revertMessage] = revertContext.sender;
}

function setExpectedOnCallSender(address _expectedOnCallSender) external {
Expand Down
10 changes: 5 additions & 5 deletions testutil/keeper/mocks/crosschain/fungible.go

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

1 change: 1 addition & 0 deletions x/crosschain/keeper/cctx_orchestrator_validate_outbound.go
Original file line number Diff line number Diff line change
Expand Up @@ -341,6 +341,7 @@ func (k Keeper) processFailedOutboundV2(ctx sdk.Context, cctx *types.CrossChainT
// process the revert on ZEVM
if err := k.fungibleKeeper.ProcessV2RevertDeposit(
ctx,
cctx.InboundParams.Sender,
cctx.GetCurrentOutboundParam().Amount.BigInt(),
chainID,
cctx.InboundParams.CoinType,
Expand Down
6 changes: 6 additions & 0 deletions x/crosschain/keeper/gas_payment.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,12 @@ func (k Keeper) PayGasNativeAndUpdateCctx(
return cosmoserrors.Wrap(types.ErrCannotFindGasParams, err.Error())
}

// with V2 protocol, reverts on connected chains can eventually call a onRevert function which can require a higher gas limit
if cctx.ProtocolContractVersion == types.ProtocolContractVersion_V2 && cctx.RevertOptions.CallOnRevert &&
!cctx.RevertOptions.RevertGasLimit.IsZero() {
gas.GasLimit = cctx.RevertOptions.RevertGasLimit
}

// calculate the final gas fee
outTxGasFee := gas.GasLimit.Mul(gas.GasPrice).Add(gas.ProtocolFlatFee)

Expand Down
1 change: 1 addition & 0 deletions x/crosschain/types/expected_keepers.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,7 @@ type FungibleKeeper interface {
) (*evmtypes.MsgEthereumTxResponse, bool, error)
ProcessV2RevertDeposit(
ctx sdk.Context,
inboundSender string,
amount *big.Int,
chainID int64,
coinType coin.CoinType,
Expand Down
4 changes: 3 additions & 1 deletion x/fungible/keeper/v2_deposits.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ func (k Keeper) ProcessV2Deposit(
// ProcessV2RevertDeposit handles a revert deposit from an inbound tx with protocol version 2
func (k Keeper) ProcessV2RevertDeposit(
ctx sdk.Context,
inboundSender string,
amount *big.Int,
chainID int64,
coinType coin.CoinType,
Expand All @@ -74,7 +75,7 @@ func (k Keeper) ProcessV2RevertDeposit(

if callOnRevert {
// no asset, call simple revert
_, err := k.CallExecuteRevert(ctx, zrc20Addr, amount, revertAddress, revertMessage)
_, err := k.CallExecuteRevert(ctx, inboundSender, zrc20Addr, amount, revertAddress, revertMessage)
return err
} else {
// no asset, no call, do nothing
Expand All @@ -87,6 +88,7 @@ func (k Keeper) ProcessV2RevertDeposit(
// revert with a ZRC20 asset
_, err := k.CallDepositAndRevert(
ctx,
inboundSender,
zrc20Addr,
amount,
revertAddress,
Expand Down
4 changes: 4 additions & 0 deletions x/fungible/keeper/v2_evm.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,7 @@ func (k Keeper) CallExecute(
// )
func (k Keeper) CallExecuteRevert(
ctx sdk.Context,
inboundSender string,
zrc20 common.Address,
amount *big.Int,
target common.Address,
Expand Down Expand Up @@ -184,6 +185,7 @@ func (k Keeper) CallExecuteRevert(
"executeRevert",
target,
revert.RevertContext{
Sender: common.HexToAddress(inboundSender),
Asset: zrc20,
Amount: amount.Uint64(),
RevertMessage: message,
Expand All @@ -203,6 +205,7 @@ func (k Keeper) CallExecuteRevert(
// )
func (k Keeper) CallDepositAndRevert(
ctx sdk.Context,
inboundSender string,
zrc20 common.Address,
amount *big.Int,
target common.Address,
Expand Down Expand Up @@ -236,6 +239,7 @@ func (k Keeper) CallDepositAndRevert(
amount,
target,
revert.RevertContext{
Sender: common.HexToAddress(inboundSender),
Asset: zrc20,
Amount: amount.Uint64(),
RevertMessage: message,
Expand Down
4 changes: 4 additions & 0 deletions zetaclient/chains/evm/signer/v2_sign.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ func (signer *Signer) signGatewayExecute(
// bytes calldata data
func (signer *Signer) signGatewayExecuteRevert(
ctx context.Context,
inboundSender string,
txData *OutboundData,
) (*ethtypes.Transaction, error) {
gatewayABI, err := gatewayevm.GatewayEVMMetaData.GetAbi()
Expand All @@ -78,6 +79,7 @@ func (signer *Signer) signGatewayExecuteRevert(
txData.to,
txData.message,
revert.RevertContext{
Sender: common.HexToAddress(inboundSender),
Asset: txData.asset,
Amount: txData.amount.Uint64(),
RevertMessage: txData.revertOptions.RevertMessage,
Expand Down Expand Up @@ -182,6 +184,7 @@ func (signer *Signer) signERC20CustodyWithdrawAndCall(
// bytes calldata data
func (signer *Signer) signERC20CustodyWithdrawRevert(
ctx context.Context,
inboundSender string,
txData *OutboundData,
) (*ethtypes.Transaction, error) {
erc20CustodyV2ABI, err := erc20custodyv2.ERC20CustodyMetaData.GetAbi()
Expand All @@ -196,6 +199,7 @@ func (signer *Signer) signERC20CustodyWithdrawRevert(
txData.amount,
txData.message,
revert.RevertContext{
Sender: common.HexToAddress(inboundSender),
Asset: txData.asset,
Amount: txData.amount.Uint64(),
RevertMessage: txData.revertOptions.RevertMessage,
Expand Down
4 changes: 2 additions & 2 deletions zetaclient/chains/evm/signer/v2_signer.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@ func (signer *Signer) SignOutboundFromCCTXV2(
// no-asset call simply hash msg.value == 0
return signer.signGatewayExecute(ctx, cctx.InboundParams.Sender, outboundData)
case evm.OutboundTypeGasWithdrawRevertAndCallOnRevert:
return signer.signGatewayExecuteRevert(ctx, outboundData)
return signer.signGatewayExecuteRevert(ctx, cctx.InboundParams.Sender, outboundData)
case evm.OutboundTypeERC20WithdrawRevertAndCallOnRevert:
return signer.signERC20CustodyWithdrawRevert(ctx, outboundData)
return signer.signERC20CustodyWithdrawRevert(ctx, cctx.InboundParams.Sender, outboundData)
}
return nil, fmt.Errorf("unsupported outbound type %d", outboundType)
}

0 comments on commit 3eeb78a

Please sign in to comment.