Skip to content

Commit

Permalink
Merge branch 'develop' into update-binary-names-upgrade-path-testing
Browse files Browse the repository at this point in the history
  • Loading branch information
lumtis authored Oct 30, 2023
2 parents ca5dd32 + ea9ba12 commit 0821530
Show file tree
Hide file tree
Showing 31 changed files with 657 additions and 185 deletions.
25 changes: 25 additions & 0 deletions testutil/keeper/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package keeper

import sdk "github.com/cosmos/cosmos-sdk/types"

const (
AccountAddressPrefix = "zeta"
)

var (
AccountPubKeyPrefix = AccountAddressPrefix + "pub"
ValidatorAddressPrefix = AccountAddressPrefix + "valoper"
ValidatorPubKeyPrefix = AccountAddressPrefix + "valoperpub"
ConsNodeAddressPrefix = AccountAddressPrefix + "valcons"
ConsNodePubKeyPrefix = AccountAddressPrefix + "valconspub"
)

func SetConfig(seal bool) {
config := sdk.GetConfig()
config.SetBech32PrefixForAccount(AccountAddressPrefix, AccountPubKeyPrefix)
config.SetBech32PrefixForValidator(ValidatorAddressPrefix, ValidatorPubKeyPrefix)
config.SetBech32PrefixForConsensusNode(ConsNodeAddressPrefix, ConsNodePubKeyPrefix)
if seal {
config.Seal()
}
}
1 change: 1 addition & 0 deletions testutil/keeper/crosschain.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ func CrosschainKeeperWithMocks(
t testing.TB,
mockOptions CrosschainMockOptions,
) (*keeper.Keeper, sdk.Context, SDKKeepers, ZetaKeepers) {
SetConfig(false)
storeKey := sdk.NewKVStoreKey(types.StoreKey)
memStoreKey := storetypes.NewMemoryStoreKey(types.MemStoreKey)

Expand Down
32 changes: 32 additions & 0 deletions testutil/sample/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@ import (
"math/big"

"github.com/cosmos/cosmos-sdk/crypto/keys/ed25519"
ethtypes "github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/ethclient"
"github.com/ethereum/go-ethereum/rlp"
"github.com/tendermint/tendermint/crypto/secp256k1"
"github.com/zeta-chain/zetacore/common"
"github.com/zeta-chain/zetacore/common/ethereum"
)

func Chain(chainID int64) *common.Chain {
Expand Down Expand Up @@ -42,3 +44,33 @@ func EthHeader() (headerRLP []byte, err error) {
headerRLP, _ = rlp.EncodeToBytes(block.Header())
return
}

func Proof() (txIndex int64, block *ethtypes.Block, header ethtypes.Header, headerRLP []byte, proof *common.Proof, tx *ethtypes.Transaction, err error) {
txIndex = int64(9)
url := "https://rpc.ankr.com/eth_goerli"
client, err := ethclient.Dial(url)
if err != nil {
return
}
bn := int64(9889649)
block, err = client.BlockByNumber(context.Background(), big.NewInt(bn))
if err != nil {
return
}
headerRLP, _ = rlp.EncodeToBytes(block.Header())
err = rlp.DecodeBytes(headerRLP, &header)
if err != nil {
return
}
tr := ethereum.NewTrie(block.Transactions())
var b []byte
ib := rlp.AppendUint64(b, uint64(txIndex))
p := ethereum.NewProof()
err = tr.Prove(ib, 0, p)
if err != nil {
return
}
proof = common.NewEthereumProof(p)
tx = block.Transactions()[txIndex]
return
}
18 changes: 12 additions & 6 deletions x/crosschain/keeper/gas_payment_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -226,12 +226,13 @@ func TestKeeper_PayGasNativeAndUpdateCctx(t *testing.T) {
k.GetAuthKeeper().GetModuleAccount(ctx, fungibletypes.ModuleName)
admin := sample.AccAddress()
setAdminPolicies(ctx, zk, admin)
fungibleMsgServer := fungiblekeeper.NewMsgServerImpl(*zk.FungibleKeeper)

// deploy gas coin and set fee params
chainID := getValidEthChainID(t)
deploySystemContracts(t, ctx, zk.FungibleKeeper, sdkk.EvmKeeper)
zrc20 := setupGasCoin(t, ctx, zk.FungibleKeeper, sdkk.EvmKeeper, chainID, "foobar", "foobar")
_, err := zk.FungibleKeeper.UpdateZRC20WithdrawFee(
_, err := fungibleMsgServer.UpdateZRC20WithdrawFee(
sdk.UnwrapSDKContext(ctx),
fungibletypes.NewMsgUpdateZRC20WithdrawFee(admin, zrc20.String(), sdk.NewUint(withdrawFee), math.Uint{}),
)
Expand Down Expand Up @@ -324,12 +325,13 @@ func TestKeeper_PayGasNativeAndUpdateCctx(t *testing.T) {
k.GetAuthKeeper().GetModuleAccount(ctx, fungibletypes.ModuleName)
admin := sample.AccAddress()
setAdminPolicies(ctx, zk, admin)
fungibleMsgServer := fungiblekeeper.NewMsgServerImpl(*zk.FungibleKeeper)

// deploy gas coin and set fee params
chainID := getValidEthChainID(t)
deploySystemContracts(t, ctx, zk.FungibleKeeper, sdkk.EvmKeeper)
zrc20 := setupGasCoin(t, ctx, zk.FungibleKeeper, sdkk.EvmKeeper, chainID, "foobar", "foobar")
_, err := zk.FungibleKeeper.UpdateZRC20WithdrawFee(
_, err := fungibleMsgServer.UpdateZRC20WithdrawFee(
sdk.UnwrapSDKContext(ctx),
fungibletypes.NewMsgUpdateZRC20WithdrawFee(admin, zrc20.String(), sdk.NewUint(withdrawFee), math.Uint{}),
)
Expand Down Expand Up @@ -367,6 +369,7 @@ func TestKeeper_PayGasInERC20AndUpdateCctx(t *testing.T) {
k.GetAuthKeeper().GetModuleAccount(ctx, fungibletypes.ModuleName)
admin := sample.AccAddress()
setAdminPolicies(ctx, zk, admin)
fungibleMsgServer := fungiblekeeper.NewMsgServerImpl(*zk.FungibleKeeper)

// deploy gas coin, erc20 and set fee params
chainID := getValidEthChainID(t)
Expand All @@ -383,7 +386,7 @@ func TestKeeper_PayGasInERC20AndUpdateCctx(t *testing.T) {
assetAddress,
"bar",
)
_, err := zk.FungibleKeeper.UpdateZRC20WithdrawFee(
_, err := fungibleMsgServer.UpdateZRC20WithdrawFee(
sdk.UnwrapSDKContext(ctx),
fungibletypes.NewMsgUpdateZRC20WithdrawFee(admin, gasZRC20.String(), sdk.NewUint(withdrawFee), math.Uint{}),
)
Expand Down Expand Up @@ -490,13 +493,14 @@ func TestKeeper_PayGasInERC20AndUpdateCctx(t *testing.T) {
k.GetAuthKeeper().GetModuleAccount(ctx, fungibletypes.ModuleName)
admin := sample.AccAddress()
setAdminPolicies(ctx, zk, admin)
fungibleMsgServer := fungiblekeeper.NewMsgServerImpl(*zk.FungibleKeeper)

// deploy gas coin, erc20 and set fee params
chainID := getValidEthChainID(t)
assetAddress := sample.EthAddress().String()
deploySystemContracts(t, ctx, zk.FungibleKeeper, sdkk.EvmKeeper)
gasZRC20 := setupGasCoin(t, ctx, zk.FungibleKeeper, sdkk.EvmKeeper, chainID, "foo", "foo")
_, err := zk.FungibleKeeper.UpdateZRC20WithdrawFee(
_, err := fungibleMsgServer.UpdateZRC20WithdrawFee(
sdk.UnwrapSDKContext(ctx),
fungibletypes.NewMsgUpdateZRC20WithdrawFee(admin, gasZRC20.String(), sdk.NewUint(withdrawFee), math.Uint{}),
)
Expand Down Expand Up @@ -534,6 +538,7 @@ func TestKeeper_PayGasInERC20AndUpdateCctx(t *testing.T) {
k.GetAuthKeeper().GetModuleAccount(ctx, fungibletypes.ModuleName)
admin := sample.AccAddress()
setAdminPolicies(ctx, zk, admin)
fungibleMsgServer := fungiblekeeper.NewMsgServerImpl(*zk.FungibleKeeper)

// deploy gas coin, erc20 and set fee params
chainID := getValidEthChainID(t)
Expand All @@ -550,7 +555,7 @@ func TestKeeper_PayGasInERC20AndUpdateCctx(t *testing.T) {
assetAddress,
"bar",
)
_, err := zk.FungibleKeeper.UpdateZRC20WithdrawFee(
_, err := fungibleMsgServer.UpdateZRC20WithdrawFee(
sdk.UnwrapSDKContext(ctx),
fungibletypes.NewMsgUpdateZRC20WithdrawFee(admin, gasZRC20.String(), sdk.NewUint(withdrawFee), math.Uint{}),
)
Expand Down Expand Up @@ -588,6 +593,7 @@ func TestKeeper_PayGasInERC20AndUpdateCctx(t *testing.T) {
k.GetAuthKeeper().GetModuleAccount(ctx, fungibletypes.ModuleName)
admin := sample.AccAddress()
setAdminPolicies(ctx, zk, admin)
fungibleMsgServer := fungiblekeeper.NewMsgServerImpl(*zk.FungibleKeeper)

// deploy gas coin, erc20 and set fee params
chainID := getValidEthChainID(t)
Expand All @@ -604,7 +610,7 @@ func TestKeeper_PayGasInERC20AndUpdateCctx(t *testing.T) {
assetAddress,
"bar",
)
_, err := zk.FungibleKeeper.UpdateZRC20WithdrawFee(
_, err := fungibleMsgServer.UpdateZRC20WithdrawFee(
sdk.UnwrapSDKContext(ctx),
fungibletypes.NewMsgUpdateZRC20WithdrawFee(admin, gasZRC20.String(), sdk.NewUint(withdrawFee), math.Uint{}),
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,7 @@ func (k msgServer) VoteOnObservedInboundTx(goCtx context.Context, msg *types.Msg
}
commit()
cctx.CctxStatus.ChangeStatus(types.CctxStatus_PendingOutbound, "")
k.RemoveInTxTrackerIfExists(ctx, cctx.InboundTxParams.SenderChainId, cctx.InboundTxParams.InboundTxObservedHash)
return &types.MsgVoteOnObservedInboundTxResponse{}, nil
}
}
70 changes: 10 additions & 60 deletions x/crosschain/keeper/msg_server_add_to_intx_tracker.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ import (

errorsmod "cosmossdk.io/errors"
sdk "github.com/cosmos/cosmos-sdk/types"
eth "github.com/ethereum/go-ethereum/common"
ethtypes "github.com/ethereum/go-ethereum/core/types"
"github.com/zeta-chain/zetacore/common"
"github.com/zeta-chain/zetacore/x/crosschain/types"
observertypes "github.com/zeta-chain/zetacore/x/observer/types"
Expand All @@ -29,11 +27,16 @@ func (k msgServer) AddToInTxTracker(goCtx context.Context, msg *types.MsgAddToIn
if !(isAdmin || isObserver) && msg.Proof != nil {
txBytes, err := k.VerifyProof(ctx, msg.Proof, msg.ChainId, msg.BlockHash, msg.TxIndex)
if err != nil {
return nil, types.ErrCannotVerifyProof.Wrapf(err.Error())
return nil, types.ErrProofVerificationFail.Wrapf(err.Error())
}
err = k.VerifyInTxBody(ctx, msg, txBytes)
if err != nil {
return nil, types.ErrCannotVerifyProof.Wrapf(err.Error())

if common.IsEVMChain(msg.ChainId) {
err = k.VerifyEVMInTxBody(ctx, msg, txBytes)
if err != nil {
return nil, types.ErrTxBodyVerificationFail.Wrapf(err.Error())
}
} else {
return nil, types.ErrTxBodyVerificationFail.Wrapf(fmt.Sprintf("cannot verify inTx body for chain %d", msg.ChainId))
}
isProven = true
}
Expand All @@ -43,63 +46,10 @@ func (k msgServer) AddToInTxTracker(goCtx context.Context, msg *types.MsgAddToIn
return nil, errorsmod.Wrap(observertypes.ErrNotAuthorized, fmt.Sprintf("Creator %s", msg.Creator))
}

k.Keeper.SetInTxTracker(ctx, types.InTxTracker{
k.SetInTxTracker(ctx, types.InTxTracker{
ChainId: msg.ChainId,
TxHash: msg.TxHash,
CoinType: msg.CoinType,
})
return &types.MsgAddToInTxTrackerResponse{}, nil
}

// https://github.com/zeta-chain/node/issues/1254
func (k Keeper) VerifyInTxBody(ctx sdk.Context, msg *types.MsgAddToInTxTracker, txBytes []byte) error {
// get core params and tss address
coreParams, found := k.zetaObserverKeeper.GetCoreParamsByChainID(ctx, msg.ChainId)
if !found {
return types.ErrUnsupportedChain.Wrapf("core params not found for chain %d", msg.ChainId)
}
tss, err := k.GetTssAddress(ctx, &types.QueryGetTssAddressRequest{})
if err != nil {
return err
}

// verify message against transaction body
if common.IsEVMChain(msg.ChainId) {
err = VerifyEVMInTxBody(coreParams, msg, txBytes, tss.Eth)
} else {
return fmt.Errorf("cannot verify inTx body for chain %d", msg.ChainId)
}
return err
}

func VerifyEVMInTxBody(coreParams *observertypes.CoreParams, msg *types.MsgAddToInTxTracker, txBytes []byte, tssEth string) error {
var txx ethtypes.Transaction
err := txx.UnmarshalBinary(txBytes)
if err != nil {
return err
}
tssAddr := eth.HexToAddress(tssEth)
if tssAddr == (eth.Address{}) {
return fmt.Errorf("tss address not found")
}

switch msg.CoinType {
case common.CoinType_Zeta:
if txx.To().Hex() != coreParams.ConnectorContractAddress {
return fmt.Errorf("receiver is not connector contract for coin type %s", msg.CoinType)
}
return nil
case common.CoinType_ERC20:
if txx.To().Hex() != coreParams.Erc20CustodyContractAddress {
return fmt.Errorf("receiver is not erc20Custory contract for coin type %s", msg.CoinType)
}
return nil
case common.CoinType_Gas:
if txx.To().Hex() != tssAddr.Hex() {
return fmt.Errorf("receiver is not tssAddress contract for coin type %s", msg.CoinType)
}
return nil
default:
return fmt.Errorf("coin type %s not supported", msg.CoinType)
}
}
Loading

0 comments on commit 0821530

Please sign in to comment.