Skip to content

Commit eaf48e3

Browse files
committed
test fixdata
1 parent dd3cea2 commit eaf48e3

File tree

7 files changed

+259
-75
lines changed

7 files changed

+259
-75
lines changed
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
local config = import 'default.jsonnet';
2+
3+
config {
4+
'cronos_777-1'+: {
5+
'app-config'+: {
6+
'app-db-backend': 'rocksdb',
7+
'iavl-lazy-loading':: super['iavl-lazy-loading'],
8+
},
9+
validators: [super.validators[0] {
10+
'app-config'+: {
11+
store: {
12+
streamers: ['versiondb'],
13+
},
14+
memiavl:: super.memiavl,
15+
versiondb:: super.versiondb,
16+
},
17+
}] + super.validators[1:],
18+
genesis+: {
19+
consensus_params: {
20+
block: {
21+
max_bytes: '1048576',
22+
max_gas: '81500000',
23+
},
24+
},
25+
app_state+: {
26+
gov+: {
27+
params+: {
28+
expedited_voting_period:: super['expedited_voting_period'],
29+
},
30+
},
31+
},
32+
},
33+
},
34+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
let
2+
pkgs = import ../../nix { };
3+
fetchFlake =
4+
repo: rev:
5+
(pkgs.flake-compat {
6+
src = {
7+
outPath = builtins.fetchTarball "https://github.com/${repo}/archive/${rev}.tar.gz";
8+
inherit rev;
9+
shortRev = builtins.substring 0 7 rev;
10+
};
11+
}).defaultNix;
12+
# release/v1.1.0
13+
releasedGenesis =
14+
(fetchFlake "crypto-org-chain/cronos" "526bc803c2f43fd4aadc05a4e16936c3c8e81f29").default;
15+
# v1.2.2 with skip flush
16+
released_1 =
17+
(fetchFlake "mmsqe/cronos" "bd9eaa1cbb535b407061e94488d325263edf0bfa").default;
18+
current = pkgs.callPackage ../../. { };
19+
in
20+
pkgs.linkFarm "upgrade-test-package" [
21+
{
22+
name = "genesis";
23+
path = releasedGenesis;
24+
}
25+
{
26+
name = "v1.2";
27+
path = released_1;
28+
}
29+
{
30+
name = "v1.3";
31+
path = current;
32+
}
33+
]

integration_tests/cosmoscli.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1794,6 +1794,11 @@ def changeset_ingest_versiondb_sst(self, versiondb_dir, sst_dir, **kwargs):
17941794
**kwargs,
17951795
).decode()
17961796

1797+
def changeset_fixdata(self, versiondb_dir, dry_run=False):
1798+
return self.raw(
1799+
"changeset", "fixdata", versiondb_dir, "--dry-run" if dry_run else None
1800+
)
1801+
17971802
def restore_versiondb(self, height, format=3, **kwargs):
17981803
return self.raw(
17991804
"changeset",

integration_tests/network.py

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
import json
22
import os
3+
import shutil
34
import signal
5+
import stat
46
import subprocess
7+
from contextlib import contextmanager
58
from pathlib import Path
69

710
import tomlkit
@@ -10,7 +13,7 @@
1013
from web3.middleware import geth_poa_middleware
1114

1215
from .cosmoscli import CosmosCLI
13-
from .utils import supervisorctl, wait_for_port
16+
from .utils import post_init, supervisorctl, wait_for_port
1417

1518

1619
class Cronos:
@@ -191,3 +194,32 @@ def setup_custom_cronos(
191194
os.killpg(os.getpgid(proc.pid), signal.SIGTERM)
192195
# proc.terminate()
193196
proc.wait()
197+
198+
199+
def setup_upgrade_cronos(tmp_path_factory, port, nix_name, cfg_name):
200+
path = tmp_path_factory.mktemp("upgrade")
201+
configdir = Path(__file__).parent
202+
cmd = [
203+
"nix-build",
204+
configdir / f"configs/{nix_name}.nix",
205+
]
206+
print(*cmd)
207+
subprocess.run(cmd, check=True)
208+
209+
# copy the content so the new directory is writable.
210+
upgrades = path / "upgrades"
211+
shutil.copytree("./result", upgrades)
212+
mod = stat.S_IRWXU
213+
upgrades.chmod(mod)
214+
for d in upgrades.iterdir():
215+
d.chmod(mod)
216+
217+
# init with genesis binary
218+
with contextmanager(setup_custom_cronos)(
219+
path,
220+
port,
221+
configdir / f"configs/{cfg_name}.jsonnet",
222+
post_init=post_init,
223+
chain_binary=str(upgrades / "genesis/bin/cronosd"),
224+
) as cronos:
225+
yield cronos

integration_tests/test_upgrade.py

Lines changed: 2 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,18 @@
11
import json
2-
import shutil
3-
import stat
4-
import subprocess
5-
from contextlib import contextmanager
62
from datetime import datetime, timedelta
73
from pathlib import Path
84

95
import pytest
106
from pystarport import ports
11-
from pystarport.cluster import SUPERVISOR_CONFIG_FILE
127

138
from .cosmoscli import DEFAULT_GAS_PRICE
14-
from .network import Cronos, setup_custom_cronos
9+
from .network import Cronos, setup_upgrade_cronos
1510
from .utils import (
1611
ADDRS,
1712
CONTRACTS,
1813
KEYS,
1914
approve_proposal,
2015
deploy_contract,
21-
edit_ini_sections,
2216
get_consensus_params,
2317
get_send_enable,
2418
send_transaction,
@@ -33,75 +27,10 @@
3327

3428
@pytest.fixture(scope="module")
3529
def custom_cronos(tmp_path_factory):
36-
yield from setup_cronos_test(tmp_path_factory)
37-
38-
39-
def init_cosmovisor(home):
40-
"""
41-
build and setup cosmovisor directory structure in each node's home directory
42-
"""
43-
cosmovisor = home / "cosmovisor"
44-
cosmovisor.mkdir()
45-
(cosmovisor / "upgrades").symlink_to("../../../upgrades")
46-
(cosmovisor / "genesis").symlink_to("./upgrades/genesis")
47-
48-
49-
def post_init(path, base_port, config):
50-
"""
51-
prepare cosmovisor for each node
52-
"""
53-
chain_id = "cronos_777-1"
54-
data = path / chain_id
55-
cfg = json.loads((data / "config.json").read_text())
56-
for i, _ in enumerate(cfg["validators"]):
57-
home = data / f"node{i}"
58-
init_cosmovisor(home)
59-
60-
edit_ini_sections(
61-
chain_id,
62-
data / SUPERVISOR_CONFIG_FILE,
63-
lambda i, _: {
64-
"command": f"cosmovisor run start --home %(here)s/node{i}",
65-
"environment": (
66-
"DAEMON_NAME=cronosd,"
67-
"DAEMON_SHUTDOWN_GRACE=1m,"
68-
"UNSAFE_SKIP_BACKUP=true,"
69-
f"DAEMON_HOME=%(here)s/node{i}"
70-
),
71-
},
72-
)
73-
74-
75-
def setup_cronos_test(tmp_path_factory):
76-
path = tmp_path_factory.mktemp("upgrade")
7730
port = 26200
7831
nix_name = "upgrade-test-package"
7932
cfg_name = "cosmovisor"
80-
configdir = Path(__file__).parent
81-
cmd = [
82-
"nix-build",
83-
configdir / f"configs/{nix_name}.nix",
84-
]
85-
print(*cmd)
86-
subprocess.run(cmd, check=True)
87-
88-
# copy the content so the new directory is writable.
89-
upgrades = path / "upgrades"
90-
shutil.copytree("./result", upgrades)
91-
mod = stat.S_IRWXU
92-
upgrades.chmod(mod)
93-
for d in upgrades.iterdir():
94-
d.chmod(mod)
95-
96-
# init with genesis binary
97-
with contextmanager(setup_custom_cronos)(
98-
path,
99-
port,
100-
configdir / f"configs/{cfg_name}.jsonnet",
101-
post_init=post_init,
102-
chain_binary=str(upgrades / "genesis/bin/cronosd"),
103-
) as cronos:
104-
yield cronos
33+
yield from setup_upgrade_cronos(tmp_path_factory, port, nix_name, cfg_name)
10534

10635

10736
def exec(c, tmp_path_factory):
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
import time
2+
3+
import pytest
4+
import requests
5+
from pystarport import ports
6+
7+
from .network import setup_upgrade_cronos
8+
from .utils import (
9+
ADDRS,
10+
CONTRACTS,
11+
KEYS,
12+
deploy_contract,
13+
do_upgrade,
14+
sign_transaction,
15+
wait_for_new_blocks,
16+
wait_for_port,
17+
)
18+
19+
pytestmark = pytest.mark.upgrade
20+
21+
22+
@pytest.fixture(scope="module")
23+
def custom_cronos(tmp_path_factory):
24+
port = 27100
25+
nix_name = "upgrade-test-package-recent"
26+
cfg_name = "cosmovisor_recent"
27+
yield from setup_upgrade_cronos(tmp_path_factory, port, nix_name, cfg_name)
28+
29+
30+
def test_cosmovisor_upgrade(custom_cronos):
31+
c = custom_cronos
32+
do_upgrade(c, "v1.2", c.cosmos_cli().block_height() + 15)
33+
wait_for_port(ports.evmrpc_port(c.base_port(1)))
34+
w3 = c.node_w3(1)
35+
gas_price = w3.eth.gas_price
36+
erc20 = deploy_contract(
37+
w3,
38+
CONTRACTS["TestERC20A"],
39+
key=KEYS["validator"],
40+
gas_price=gas_price,
41+
)
42+
43+
def transfer():
44+
tx = erc20.functions.transfer(ADDRS["community"], 10).build_transaction(
45+
{
46+
"from": ADDRS["validator"],
47+
"gasPrice": gas_price,
48+
}
49+
)
50+
signed = sign_transaction(w3, tx, KEYS["validator"])
51+
txhash = w3.eth.send_raw_transaction(signed.rawTransaction)
52+
receipt = w3.eth.wait_for_transaction_receipt(txhash)
53+
return receipt
54+
55+
transfer()
56+
cli = do_upgrade(c, "v1.3", c.cosmos_cli().block_height() + 15)
57+
wait_for_port(ports.evmrpc_port(c.base_port(1)))
58+
receipt = transfer()
59+
print("mm-receipt", receipt)
60+
blk = receipt["blockNumber"]
61+
62+
def trace_blk(i):
63+
url = f"http://127.0.0.1:{ports.evmrpc_port(c.base_port(i))}"
64+
params = {
65+
"method": "debug_traceBlockByNumber",
66+
"params": [hex(blk)],
67+
"id": 1,
68+
"jsonrpc": "2.0",
69+
}
70+
rsp = requests.post(url, json=params)
71+
assert rsp.status_code == 200
72+
return rsp.json()["result"]
73+
74+
wait_for_new_blocks(cli, 1)
75+
b0 = trace_blk(0)
76+
b1 = trace_blk(1)
77+
assert b0 != b1, b0
78+
79+
c.supervisorctl("stop", "cronos_777-1-node0")
80+
time.sleep(3)
81+
cli.changeset_fixdata(f"{c.base_dir}/node0/data/versiondb")
82+
c.supervisorctl("start", "cronos_777-1-node0")
83+
wait_for_port(ports.evmrpc_port(c.base_port(0)))
84+
b0 = trace_blk(0)
85+
assert b0 == b1, b0

0 commit comments

Comments
 (0)