-
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.
test: add tx priority checks to e2e tests (#2184)
- Loading branch information
Showing
9 changed files
with
255 additions
and
104 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
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
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,37 @@ | ||
package local | ||
|
||
import ( | ||
"path/filepath" | ||
|
||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
"github.com/spf13/cobra" | ||
"github.com/zeta-chain/zetacore/app" | ||
"github.com/zeta-chain/zetacore/e2e/config" | ||
) | ||
|
||
// GetConfig returns config from file from the command line flag | ||
func GetConfig(cmd *cobra.Command) (config.Config, error) { | ||
configFile, err := cmd.Flags().GetString(FlagConfigFile) | ||
if err != nil { | ||
return config.Config{}, err | ||
} | ||
|
||
// use default config if no config file is specified | ||
if configFile == "" { | ||
return config.DefaultConfig(), nil | ||
} | ||
|
||
configFile, err = filepath.Abs(configFile) | ||
if err != nil { | ||
return config.Config{}, err | ||
} | ||
|
||
return config.ReadConfig(configFile) | ||
} | ||
|
||
// setCosmosConfig set account prefix to zeta | ||
func setCosmosConfig() { | ||
cosmosConf := sdk.GetConfig() | ||
cosmosConf.SetBech32PrefixForAccount(app.Bech32PrefixAccAddr, app.Bech32PrefixAccPub) | ||
cosmosConf.Seal() | ||
} |
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,113 @@ | ||
package local | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"strings" | ||
"time" | ||
|
||
"github.com/cometbft/cometbft/abci/types" | ||
rpchttp "github.com/cometbft/cometbft/rpc/client/http" | ||
coretypes "github.com/cometbft/cometbft/rpc/core/types" | ||
"github.com/zeta-chain/zetacore/e2e/config" | ||
) | ||
|
||
// monitorTxPriorityInBlocks checks for transaction priorities in blocks and reports errors | ||
func monitorTxPriorityInBlocks(ctx context.Context, conf config.Config, errCh chan error) { | ||
rpcClient, err := rpchttp.New(conf.RPCs.ZetaCoreRPC, "/websocket") | ||
if err != nil { | ||
errCh <- err | ||
return | ||
} | ||
|
||
ticker := time.NewTicker(2 * time.Second) | ||
defer ticker.Stop() | ||
|
||
for { | ||
select { | ||
case <-ctx.Done(): | ||
errCh <- nil | ||
case <-ticker.C: | ||
processBlockTxs(ctx, rpcClient, errCh) | ||
} | ||
} | ||
} | ||
|
||
// processBlockTxs fetches the latest block and evaluates transaction priorities | ||
func processBlockTxs(ctx context.Context, rpc *rpchttp.HTTP, errCh chan error) { | ||
block, err := rpc.Block(ctx, nil) | ||
if err != nil { | ||
errCh <- err | ||
return | ||
} | ||
|
||
nonSystemTxFound := false | ||
for _, tx := range block.Block.Txs { | ||
txResult, err := rpc.Tx(ctx, tx.Hash(), false) | ||
if err != nil { | ||
continue | ||
} | ||
processTx(txResult, &nonSystemTxFound, errCh) | ||
} | ||
} | ||
|
||
// processTx handles the processing of each transaction and its events | ||
func processTx(txResult *coretypes.ResultTx, nonSystemTxFound *bool, errCh chan error) { | ||
for _, event := range txResult.TxResult.Events { | ||
for _, attr := range event.Attributes { | ||
// skip attrs with empty value | ||
if attr.Value == "\"\"" { | ||
continue | ||
} | ||
|
||
// skip internal events with msg_type_url key, because they are not representing sdk msgs | ||
if strings.Contains(attr.Value, ".internal.") { | ||
continue | ||
} | ||
switch attr.Key { | ||
// if attr key is msg_type_url, check if it's one of system txs, otherwise mark it as non system tx | ||
case "msg_type_url": | ||
if isMsgTypeURLSystemTx(attr) { | ||
// a non system tx has been found in the block before a system tx | ||
if *nonSystemTxFound { | ||
errCh <- errors.New("wrong tx priority, system tx not on top") | ||
return | ||
} | ||
} else { | ||
*nonSystemTxFound = true | ||
} | ||
// if attr key is action, check if tx is ethermint non system tx and if it is, mark it | ||
case "action": | ||
if isActionNonSystemTx(attr) { | ||
*nonSystemTxFound = true | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
func isMsgTypeURLSystemTx(attr types.EventAttribute) bool { | ||
// type urls in attr.Value are in double quotes, so it needs to be formatted like this | ||
systemTxsMsgTypeUrls := []string{ | ||
"\"/zetachain.zetacore.crosschain.MsgVoteOnObservedOutboundTx\"", | ||
"\"/zetachain.zetacore.crosschain.MsgVoteOnObservedInboundTx\"", | ||
"\"/zetachain.zetacore.crosschain.MsgVoteGasPrice\"", | ||
"\"/zetachain.zetacore.crosschain.MsgAddToOutTxTracker\"", | ||
"\"/zetachain.zetacore.crosschain.MsgAddToInTxTracker\"", | ||
"\"/zetachain.zetacore.observer.MsgVoteBlockHeader\"", | ||
"\"/zetachain.zetacore.observer.MsgVoteTSS\"", | ||
"\"/zetachain.zetacore.observer.MsgAddBlameVote\"", | ||
} | ||
|
||
for _, url := range systemTxsMsgTypeUrls { | ||
if url == attr.Value { | ||
return true | ||
} | ||
} | ||
|
||
return false | ||
} | ||
|
||
func isActionNonSystemTx(attr types.EventAttribute) bool { | ||
return attr.Value == "/ethermint.evm.v1.MsgEthereumTx" | ||
} |
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,43 @@ | ||
package local | ||
|
||
import ( | ||
ethcommon "github.com/ethereum/go-ethereum/common" | ||
zetae2econfig "github.com/zeta-chain/zetacore/cmd/zetae2e/config" | ||
"github.com/zeta-chain/zetacore/e2e/config" | ||
"github.com/zeta-chain/zetacore/e2e/runner" | ||
"github.com/zeta-chain/zetacore/e2e/utils" | ||
) | ||
|
||
// initTestRunner initializes a runner form tests | ||
// it creates a runner with an account and copy contracts from deployer runner | ||
func initTestRunner( | ||
name string, | ||
conf config.Config, | ||
deployerRunner *runner.E2ERunner, | ||
userAddress ethcommon.Address, | ||
userPrivKey string, | ||
logger *runner.Logger, | ||
) (*runner.E2ERunner, error) { | ||
// initialize runner for test | ||
testRunner, err := zetae2econfig.RunnerFromConfig( | ||
deployerRunner.Ctx, | ||
name, | ||
deployerRunner.CtxCancel, | ||
conf, | ||
userAddress, | ||
userPrivKey, | ||
utils.FungibleAdminName, | ||
FungibleAdminMnemonic, | ||
logger, | ||
) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
// copy contracts from deployer runner | ||
if err := testRunner.CopyAddressesFrom(deployerRunner); err != nil { | ||
return nil, err | ||
} | ||
|
||
return testRunner, nil | ||
} |
Oops, something went wrong.