diff --git a/tests/config/0gchain-init-genesis.sh b/tests/config/0gchain-init-genesis.sh new file mode 100644 index 0000000..bda0a7e --- /dev/null +++ b/tests/config/0gchain-init-genesis.sh @@ -0,0 +1,93 @@ +#!/bin/bash + +set -e + +BINARY=$(cd $(dirname ${BASH_SOURCE[0]})/../tmp; pwd)/0gchaind +ROOT_DIR=${1:-.} +NUM_NODES=${2:-3} +P2P_PORT_START=${3:-26656} +CHAIN_ID=zgchainpy_9000-777 + +# install jq if not unavailable +jq --version >/dev/null 2>&1 || sudo snap install jq -y + +mkdir -p $ROOT_DIR + +# Init configs +for ((i=0; i<$NUM_NODES; i++)) do + $BINARY init node$i --home $ROOT_DIR/node$i --chain-id $CHAIN_ID + + # Change genesis.json + GENESIS=$ROOT_DIR/node$i/config/genesis.json + TMP_GENESIS=$ROOT_DIR/node$i/config/tmp_genesis.json + + # Replace stake with neuron + sed -in-place='' 's/stake/ua0gi/g' "$GENESIS" + + # Replace the default evm denom of aphoton with neuron + sed -in-place='' 's/aphoton/neuron/g' "$GENESIS" + + cat $GENESIS | jq '.consensus_params.block.max_gas = "25000000"' >$TMP_GENESIS && mv $TMP_GENESIS $GENESIS + + # Zero out the total supply so it gets recalculated during InitGenesis + cat $GENESIS | jq '.app_state.bank.supply = []' >$TMP_GENESIS && mv $TMP_GENESIS $GENESIS + + # Disable fee market + cat $GENESIS | jq '.app_state.feemarket.params.no_base_fee = true' >$TMP_GENESIS && mv $TMP_GENESIS $GENESIS + + # Disable london fork + cat $GENESIS | jq '.app_state.evm.params.chain_config.london_block = null' >$TMP_GENESIS && mv $TMP_GENESIS $GENESIS + cat $GENESIS | jq '.app_state.evm.params.chain_config.arrow_glacier_block = null' >$TMP_GENESIS && mv $TMP_GENESIS $GENESIS + cat $GENESIS | jq '.app_state.evm.params.chain_config.gray_glacier_block = null' >$TMP_GENESIS && mv $TMP_GENESIS $GENESIS + cat $GENESIS | jq '.app_state.evm.params.chain_config.merge_netsplit_block = null' >$TMP_GENESIS && mv $TMP_GENESIS $GENESIS + cat $GENESIS | jq '.app_state.evm.params.chain_config.shanghai_block = null' >$TMP_GENESIS && mv $TMP_GENESIS $GENESIS + cat $GENESIS | jq '.app_state.evm.params.chain_config.cancun_block = null' >$TMP_GENESIS && mv $TMP_GENESIS $GENESIS + + # cat $GENESIS | jq '.app_state["staking"]["params"]["bond_denom"]="a0gi"' >$TMP_GENESIS && mv $TMP_GENESIS $GENESIS + # cat $GENESIS | jq '.app_state["gov"]["params"]["min_deposit"][0]["denom"]="a0gi"' >$TMP_GENESIS && mv $TMP_GENESIS $GENESIS + + cat "$GENESIS" | jq '.app_state["staking"]["params"]["max_validators"]=125' >"$TMP_GENESIS" && mv "$TMP_GENESIS" "$GENESIS" + cat "$GENESIS" | jq '.app_state["slashing"]["params"]["signed_blocks_window"]="1000"' >"$TMP_GENESIS" && mv "$TMP_GENESIS" "$GENESIS" + + cat "$GENESIS" | jq '.app_state["consensus_params"]["block"]["time_iota_ms"]="3000"' >"$TMP_GENESIS" && mv "$TMP_GENESIS" "$GENESIS" + + # Change app.toml + APP_TOML=$ROOT_DIR/node$i/config/app.toml + sed -i 's/minimum-gas-prices = "0ua0gi"/minimum-gas-prices = "1000000000neuron"/' $APP_TOML + sed -i '/\[grpc\]/,/^\[/ s/enable = true/enable = false/' $APP_TOML + sed -i '/\[grpc-web\]/,/^\[/ s/enable = true/enable = false/' $APP_TOML + sed -i '/\[json-rpc\]/,/^\[/ s/enable = false/enable = true/' $APP_TOML + + # Change config.toml + CONFIG_TOML=$ROOT_DIR/node$i/config/config.toml + sed -i '/seeds = /c\seeds = ""' $CONFIG_TOML + sed -i 's/addr_book_strict = true/addr_book_strict = false/' $CONFIG_TOML +done + +# Update persistent_peers in config.toml +for ((i=1; i<$NUM_NODES; i++)) do + PERSISTENT_NODES="" + for ((j=0; j<$i; j++)) do + if [[ $j -gt 0 ]]; then PERSISTENT_NODES=$PERSISTENT_NODES,; fi + NODE_ID=`$BINARY tendermint show-node-id --home $ROOT_DIR/node$j` + P2P_PORT=$(($P2P_PORT_START+$j)) + PERSISTENT_NODES=$PERSISTENT_NODES$NODE_ID@127.0.0.1:$P2P_PORT + done + sed -i "/persistent_peers = /c\persistent_peers = \"$PERSISTENT_NODES\"" $ROOT_DIR/node$i/config/config.toml +done + +# Create genesis with a single validator +$BINARY keys add val0 --keyring-backend test --home $ROOT_DIR/node0 +$BINARY add-genesis-account val0 15000000000000000000ua0gi --keyring-backend test --home $ROOT_DIR/node0 + +# add genesis account for tests, see GENESIS_PRIV_KEY and GENESIS_PRIV_KEY1 in node_config.py +$BINARY add-genesis-account 0g1l0j9dqdvd3fatfqywhm4y6avrln4jracmt6ztf 40000000000000000000ua0gi --home $ROOT_DIR/node0 +$BINARY add-genesis-account 0g1pemg6y3etj9tlhkl0vdwkrw36f74u2nl8sjw7g 40000000000000000000ua0gi --home $ROOT_DIR/node0 + +mkdir -p $ROOT_DIR/gentxs +$BINARY gentx val0 10000000000000000000ua0gi --keyring-backend test --home $ROOT_DIR/node0 --output-document $ROOT_DIR/gentxs/node0.json +$BINARY collect-gentxs --home $ROOT_DIR/node0 --gentx-dir $ROOT_DIR/gentxs +$BINARY validate-genesis --home $ROOT_DIR/node0 +for ((i=1; i<$NUM_NODES; i++)) do + cp $ROOT_DIR/node0/config/genesis.json $ROOT_DIR/node$i/config/genesis.json +done diff --git a/tests/config/evmos-init-genesis.sh b/tests/config/evmos-init-genesis.sh deleted file mode 100644 index 4a39518..0000000 --- a/tests/config/evmos-init-genesis.sh +++ /dev/null @@ -1,63 +0,0 @@ -#!/bin/bash - -set -e - -EVMOSD=$(cd $(dirname ${BASH_SOURCE[0]})/../tmp; pwd)/evmosd -ROOT_DIR=${1:-.} -NUM_NODES=${2:-3} -P2P_PORT_START=${3:-26656} -CHAIN_ID=evmospy_9000-777 - -# install jq if not unavailable -jq --version >/dev/null 2>&1 || sudo snap install jq -y - -mkdir -p $ROOT_DIR - -# Init configs -for ((i=0; i<$NUM_NODES; i++)) do - $EVMOSD init node$i --home $ROOT_DIR/node$i --chain-id $CHAIN_ID - - # Change parameter token denominations to aevmos - GENESIS=$ROOT_DIR/node$i/config/genesis.json - TMP_GENESIS=$ROOT_DIR/node$i/config/tmp_genesis.json - cat $GENESIS | jq '.app_state["staking"]["params"]["bond_denom"]="aevmos"' >$TMP_GENESIS && mv $TMP_GENESIS $GENESIS - cat $GENESIS | jq '.app_state["gov"]["params"]["min_deposit"][0]["denom"]="aevmos"' >$TMP_GENESIS && mv $TMP_GENESIS $GENESIS - - # Change app.toml - APP_TOML=$ROOT_DIR/node$i/config/app.toml - sed -i 's/minimum-gas-prices = "0aevmos"/minimum-gas-prices = "1aevmos"/' $APP_TOML - sed -i '/\[json-rpc\]/,/^\[/ s/enable = false/enable = true/' $APP_TOML - - # Change config.toml - CONFIG_TOML=$ROOT_DIR/node$i/config/config.toml - sed -i '/seeds = /c\seeds = ""' $CONFIG_TOML - sed -i 's/addr_book_strict = true/addr_book_strict = false/' $CONFIG_TOML -done - -# Update persistent_peers in config.toml -for ((i=1; i<$NUM_NODES; i++)) do - PERSISTENT_NODES="" - for ((j=0; j<$i; j++)) do - if [[ $j -gt 0 ]]; then PERSISTENT_NODES=$PERSISTENT_NODES,; fi - NODE_ID=`$EVMOSD tendermint show-node-id --home $ROOT_DIR/node$j` - P2P_PORT=$(($P2P_PORT_START+$j)) - PERSISTENT_NODES=$PERSISTENT_NODES$NODE_ID@127.0.0.1:$P2P_PORT - done - sed -i "/persistent_peers = /c\persistent_peers = \"$PERSISTENT_NODES\"" $ROOT_DIR/node$i/config/config.toml -done - -# Create genesis with a single validator -$EVMOSD keys add val0 --keyring-backend test --home $ROOT_DIR/node0 -$EVMOSD add-genesis-account val0 1000000000evmos --keyring-backend test --home $ROOT_DIR/node0 - -# add genesis account for tests, see GENESIS_PRIV_KEY and GENESIS_PRIV_KEY1 in node_config.py -$EVMOSD add-genesis-account evmos1l0j9dqdvd3fatfqywhm4y6avrln4jracaapkme 1000000000evmos --home $ROOT_DIR/node0 -$EVMOSD add-genesis-account evmos1pemg6y3etj9tlhkl0vdwkrw36f74u2nlpxf6wc 1000000000evmos --home $ROOT_DIR/node0 - -mkdir -p $ROOT_DIR/gentxs -$EVMOSD gentx val0 500000000evmos --keyring-backend test --home $ROOT_DIR/node0 --output-document $ROOT_DIR/gentxs/node0.json -$EVMOSD collect-gentxs --home $ROOT_DIR/node0 --gentx-dir $ROOT_DIR/gentxs -$EVMOSD validate-genesis --home $ROOT_DIR/node0 -for ((i=1; i<$NUM_NODES; i++)) do - cp $ROOT_DIR/node0/config/genesis.json $ROOT_DIR/node$i/config/genesis.json -done diff --git a/tests/test_framework/blockchain_node.py b/tests/test_framework/blockchain_node.py index 8f9f6cb..aa82290 100644 --- a/tests/test_framework/blockchain_node.py +++ b/tests/test_framework/blockchain_node.py @@ -27,7 +27,7 @@ from test_framework.contracts import load_contract_metadata class BlockChainNodeType(Enum): Conflux = 0 BSC = 1 - Evmos = 2 + ZG = 2 def block_time(self): if self == BlockChainNodeType.Conflux: @@ -363,4 +363,4 @@ class BlockchainNode(TestNode): w3.eth.wait_for_transaction_receipt(tx_hash) def start(self): - super().start(self.blockchain_node_type == BlockChainNodeType.BSC) + super().start(self.blockchain_node_type == BlockChainNodeType.BSC or self.blockchain_node_type == BlockChainNodeType.ZG) diff --git a/tests/test_framework/test_framework.py b/tests/test_framework/test_framework.py index d3d1520..06f8a33 100644 --- a/tests/test_framework/test_framework.py +++ b/tests/test_framework/test_framework.py @@ -18,7 +18,7 @@ from test_framework.contract_proxy import FlowContractProxy, MineContractProxy, from test_framework.zgs_node import ZgsNode from test_framework.blockchain_node import BlockChainNodeType from test_framework.conflux_node import ConfluxNode, connect_sample_nodes -from test_framework.evmos_node import EvmosNode, evmos_init_genesis +from test_framework.zg_node import ZGNode, zg_node_init_genesis from utility.utils import PortMin, is_windows_platform, wait_until from utility.build_binary import build_cli @@ -35,7 +35,7 @@ TEST_EXIT_FAILED = 1 class TestFramework: - def __init__(self, blockchain_node_type=BlockChainNodeType.Evmos): + def __init__(self, blockchain_node_type=BlockChainNodeType.ZG): if "http_proxy" in os.environ: del os.environ["http_proxy"] @@ -62,8 +62,8 @@ class TestFramework: self.__default_geth_binary__ = os.path.join( tests_dir, "tmp", "geth" + binary_ext ) - self.__default_evmos_binary__ = os.path.join( - tests_dir, "tmp", "evmosd" + binary_ext + self.__default_zg_binary__ = os.path.join( + tests_dir, "tmp", "0gchaind" + binary_ext ) self.__default_zgs_node_binary__ = os.path.join( root_dir, "target", "release", "zgs_node" + binary_ext @@ -73,9 +73,9 @@ class TestFramework: ) def __setup_blockchain_node(self): - if self.blockchain_node_type == BlockChainNodeType.Evmos: - evmos_init_genesis(self.blockchain_binary, self.root_dir, self.num_blockchain_nodes) - self.log.info("Evmos genesis initialized for %s nodes" % self.num_blockchain_nodes) + if self.blockchain_node_type == BlockChainNodeType.ZG: + zg_node_init_genesis(self.blockchain_binary, self.root_dir, self.num_blockchain_nodes) + self.log.info("0gchain genesis initialized for %s nodes" % self.num_blockchain_nodes) for i in range(self.num_blockchain_nodes): if i in self.blockchain_node_configs: @@ -103,8 +103,8 @@ class TestFramework: self.contract_path, self.log, ) - elif self.blockchain_node_type == BlockChainNodeType.Evmos: - node = EvmosNode( + elif self.blockchain_node_type == BlockChainNodeType.ZG: + node = ZGNode( i, self.root_dir, self.blockchain_binary, @@ -161,9 +161,9 @@ class TestFramework: # The default is `dev` mode with auto mining, so it's not guaranteed that blocks # can be synced in time for `sync_blocks` to pass. # sync_blocks(self.blockchain_nodes) - elif self.blockchain_node_type == BlockChainNodeType.Evmos: + elif self.blockchain_node_type == BlockChainNodeType.ZG: # wait for the first block - self.log.debug("Wait 3 seconds for evmos node to generate first block") + self.log.debug("Wait 3 seconds for 0gchain node to generate first block") time.sleep(3) for node in self.blockchain_nodes: wait_until(lambda: node.net_peerCount() == self.num_blockchain_nodes - 1) @@ -227,9 +227,9 @@ class TestFramework: ) parser.add_argument( - "--evmos-binary", - dest="evmos", - default=self.__default_evmos_binary__, + "--zg-binary", + dest="zg", + default=self.__default_zg_binary__, type=str, ) @@ -458,8 +458,8 @@ class TestFramework: self.blockchain_binary = os.path.abspath(self.options.conflux) elif self.blockchain_node_type == BlockChainNodeType.BSC: self.blockchain_binary = os.path.abspath(self.options.bsc) - elif self.blockchain_node_type == BlockChainNodeType.Evmos: - self.blockchain_binary = os.path.abspath(self.options.evmos) + elif self.blockchain_node_type == BlockChainNodeType.ZG: + self.blockchain_binary = os.path.abspath(self.options.zg) else: raise NotImplementedError diff --git a/tests/test_framework/evmos_node.py b/tests/test_framework/zg_node.py similarity index 58% rename from tests/test_framework/evmos_node.py rename to tests/test_framework/zg_node.py index ebd7340..89c2c41 100644 --- a/tests/test_framework/evmos_node.py +++ b/tests/test_framework/zg_node.py @@ -4,41 +4,41 @@ import tempfile from test_framework.blockchain_node import BlockChainNodeType, BlockchainNode from utility.utils import blockchain_rpc_port, arrange_port -from utility.build_binary import build_evmos +from utility.build_binary import build_zg -EVMOS_PORT_CATEGORY_WS = 0 -EVMOS_PORT_CATEGORY_P2P = 1 -EVMOS_PORT_CATEGORY_RPC = 2 -EVMOS_PORT_CATEGORY_PPROF = 3 +ZGNODE_PORT_CATEGORY_WS = 0 +ZGNODE_PORT_CATEGORY_P2P = 1 +ZGNODE_PORT_CATEGORY_RPC = 2 +ZGNODE_PORT_CATEGORY_PPROF = 3 -def evmos_init_genesis(binary: str, root_dir: str, num_nodes: int): +def zg_node_init_genesis(binary: str, root_dir: str, num_nodes: int): assert num_nodes > 0, "Invalid number of blockchain nodes: %s" % num_nodes if not os.path.exists(binary): - build_evmos(os.path.dirname(binary)) + build_zg(os.path.dirname(binary)) shell_script = os.path.join( os.path.dirname(os.path.realpath(__file__)), # test_framework folder - "..", "config", "evmos-init-genesis.sh" + "..", "config", "0gchain-init-genesis.sh" ) - evmosd_dir = os.path.join(root_dir, "evmosd") - os.mkdir(evmosd_dir) + zgchaind_dir = os.path.join(root_dir, "0gchaind") + os.mkdir(zgchaind_dir) - log_file = tempfile.NamedTemporaryFile(dir=evmosd_dir, delete=False, prefix="init_genesis_", suffix=".log") - p2p_port_start = arrange_port(EVMOS_PORT_CATEGORY_P2P, 0) + log_file = tempfile.NamedTemporaryFile(dir=zgchaind_dir, delete=False, prefix="init_genesis_", suffix=".log") + p2p_port_start = arrange_port(ZGNODE_PORT_CATEGORY_P2P, 0) ret = subprocess.run( - args=["bash", shell_script, evmosd_dir, str(num_nodes), str(p2p_port_start)], + args=["bash", shell_script, zgchaind_dir, str(num_nodes), str(p2p_port_start)], stdout=log_file, stderr=log_file, ) log_file.close() - assert ret.returncode == 0, "Failed to init evmos genesis, see more details in log file: %s" % log_file.name + assert ret.returncode == 0, "Failed to init 0gchain genesis, see more details in log file: %s" % log_file.name -class EvmosNode(BlockchainNode): +class ZGNode(BlockchainNode): def __init__( self, index, @@ -49,7 +49,7 @@ class EvmosNode(BlockchainNode): log, rpc_timeout=10, ): - data_dir = os.path.join(root_dir, "evmosd", "node" + str(index)) + data_dir = os.path.join(root_dir, "0gchaind", "node" + str(index)) rpc_url = "http://127.0.0.1:%s" % blockchain_rpc_port(index) super().__init__( @@ -60,7 +60,7 @@ class EvmosNode(BlockchainNode): {}, contract_path, log, - BlockChainNodeType.Evmos, + BlockChainNodeType.ZG, rpc_timeout, ) @@ -71,13 +71,13 @@ class EvmosNode(BlockchainNode): # overwrite json rpc http port: 8545 "--json-rpc.address", "127.0.0.1:%s" % blockchain_rpc_port(index), # overwrite json rpc ws port: 8546 - "--json-rpc.ws-address", "127.0.0.1:%s" % arrange_port(EVMOS_PORT_CATEGORY_WS, index), + "--json-rpc.ws-address", "127.0.0.1:%s" % arrange_port(ZGNODE_PORT_CATEGORY_WS, index), # overwrite p2p port: 26656 - "--p2p.laddr", "tcp://127.0.0.1:%s" % arrange_port(EVMOS_PORT_CATEGORY_P2P, index), + "--p2p.laddr", "tcp://127.0.0.1:%s" % arrange_port(ZGNODE_PORT_CATEGORY_P2P, index), # overwrite rpc port: 26657 - "--rpc.laddr", "tcp://127.0.0.1:%s" % arrange_port(EVMOS_PORT_CATEGORY_RPC, index), + "--rpc.laddr", "tcp://127.0.0.1:%s" % arrange_port(ZGNODE_PORT_CATEGORY_RPC, index), # overwrite pprof port: 6060 - "--rpc.pprof_laddr", "127.0.0.1:%s" % arrange_port(EVMOS_PORT_CATEGORY_PPROF, index), + "--rpc.pprof_laddr", "127.0.0.1:%s" % arrange_port(ZGNODE_PORT_CATEGORY_PPROF, index), ] for k, v in updated_config.items(): diff --git a/tests/utility/build_binary.py b/tests/utility/build_binary.py index 20c05fb..9fa8823 100644 --- a/tests/utility/build_binary.py +++ b/tests/utility/build_binary.py @@ -13,10 +13,10 @@ GITHUB_DOWNLOAD_URL="https://api.github.com/repos/0glabs/0g-storage-node/release CONFLUX_BINARY = "conflux.exe" if is_windows_platform() else "conflux" BSC_BINARY = "geth.exe" if is_windows_platform() else "geth" -EVMOS_BINARY = "evmosd.exe" if is_windows_platform() else "evmosd" +ZG_BINARY = "0gchaind.exe" if is_windows_platform() else "0gchaind" CLIENT_BINARY = "0g-storage-client.exe" if is_windows_platform() else "0g-storage-client" -EVMOS_GIT_REV = "2ef76f6c9bdd73cd15dabd7397492dbebc311f98" +ZG_GIT_REV = "7bc25a060fab9c17bc9942b6747cd07a668d3042" # v0.1.0 CLI_GIT_REV = "1d09ec4f0b9c27428b2357de46b66e8c231b74df" @unique @@ -60,13 +60,13 @@ def build_bsc(dir: str) -> BuildBinaryResult: return result -def build_evmos(dir: str) -> BuildBinaryResult: - # Download or build evmos binary if absent +def build_zg(dir: str) -> BuildBinaryResult: + # Download or build 0gchain binary if absent result = __download_from_github( dir=dir, - binary_name=EVMOS_BINARY, + binary_name=ZG_BINARY, github_url=GITHUB_DOWNLOAD_URL, - asset_name=__asset_name(EVMOS_BINARY, zip=True), + asset_name=__asset_name(ZG_BINARY, zip=True), ) if result == BuildBinaryResult.AlreadyExists or result == BuildBinaryResult.Installed: @@ -74,10 +74,10 @@ def build_evmos(dir: str) -> BuildBinaryResult: return __build_from_github( dir=dir, - binary_name=EVMOS_BINARY, - github_url="https://github.com/0glabs/0g-evmos.git", - git_rev=EVMOS_GIT_REV, - build_cmd="make install; cp $(go env GOPATH)/bin/evmosd .", + binary_name=ZG_BINARY, + github_url="https://github.com/0glabs/0g-chain.git", + git_rev=ZG_GIT_REV, + build_cmd="make install; cp $(go env GOPATH)/bin/0gchaind .", compiled_relative_path=[], ) diff --git a/tests/utility/run_all.py b/tests/utility/run_all.py index 1dd936c..9e68b73 100644 --- a/tests/utility/run_all.py +++ b/tests/utility/run_all.py @@ -6,7 +6,7 @@ import sys from concurrent.futures import ProcessPoolExecutor -from utility.build_binary import build_conflux, build_bsc, build_evmos, build_cli +from utility.build_binary import build_conflux, build_bsc, build_zg, build_cli DEFAULT_PORT_MIN = 11000 DEFAULT_PORT_MAX = 65535 @@ -62,7 +62,7 @@ def run_all(test_dir: str, test_subdirs: list[str]=[], slow_tests: set[str]={}, # Build blockchain binaries if absent build_conflux(tmp_dir) build_bsc(tmp_dir) - build_evmos(tmp_dir) + build_zg(tmp_dir) build_cli(tmp_dir) start_time = time.time()