Skip to content

Commit

Permalink
Problem: no unjailed test for slashing
Browse files Browse the repository at this point in the history
  • Loading branch information
mmsqe committed Jul 16, 2024
1 parent 60025aa commit c037871
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 2 deletions.
27 changes: 27 additions & 0 deletions integration_tests/configs/slashing.jsonnet
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
local config = import 'default.jsonnet';

config {
'cronos_777-1'+: {
validators: super.validators + [
{
coins: '1000000000000000000stake,10000000000000000000000basetcro',
staked: '1000000000000000000stake',
client_config: {
'broadcast-mode': 'sync',
},
}
for i in std.range(1, 2)
],
genesis+: {
app_state+: {
slashing+: {
params: {
signed_blocks_window: '10',
slash_fraction_downtime: '0.01',
downtime_jail_duration: '60s',
},
},
},
},
},
}
12 changes: 10 additions & 2 deletions integration_tests/cosmoscli.py
Original file line number Diff line number Diff line change
Expand Up @@ -590,8 +590,12 @@ def broadcast_tx_json(self, tx, **kwargs):
fp.flush()
return self.broadcast_tx(fp.name)

def unjail(self, addr):
return json.loads(
def unjail(self, addr, **kwargs):
default_kwargs = {
"gas_prices": DEFAULT_GAS_PRICE,
"gas": DEFAULT_GAS,
}
rsp = json.loads(
self.raw(
"tx",
"slashing",
Expand All @@ -602,8 +606,12 @@ def unjail(self, addr):
node=self.node_rpc,
keyring_backend="test",
chain_id=self.chain_id,
**(default_kwargs | kwargs),
)
)
if rsp["code"] == 0:
rsp = self.event_query_tx_for(rsp["txhash"])
return rsp

def create_validator(
self,
Expand Down
60 changes: 60 additions & 0 deletions integration_tests/test_slashing.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import datetime
from pathlib import Path

import pytest
from dateutil.parser import isoparse
from pystarport import ports

from .network import Cronos, setup_custom_cronos
from .utils import wait_for_block_time, wait_for_new_blocks, wait_for_port

pytestmark = pytest.mark.slow


@pytest.fixture(scope="module")
def cronos(request, tmp_path_factory):
"""start-cronos
params: enable_auto_deployment
"""
yield from setup_custom_cronos(
tmp_path_factory.mktemp("slashing"),
27100,
Path(__file__).parent / "configs/slashing.jsonnet",
)


def test_slashing(cronos: Cronos):
"stop node2, wait for non-live slashing"
cli = cronos.cosmos_cli()
cli_2 = cronos.cosmos_cli(i=2)
addr = cli_2.address("validator")
val_addr = cli_2.address("validator", bech="val")
tokens1 = int((cli.validator(val_addr))["tokens"])

print("tokens before slashing", tokens1)
print("stop and wait for 10 blocks")
print(cronos.supervisorctl("stop", "cronos_777-1-node2"))
wait_for_new_blocks(cli, 10)
print(cronos.supervisorctl("start", "cronos_777-1-node2"))
wait_for_port(ports.evmrpc_port(cronos.base_port(2)))

val = cli.validator(val_addr)
tokens2 = int(val["tokens"])
print("tokens after slashing", tokens2)
assert tokens2 == int(tokens1 * 0.99), "slash amount is not correct"

assert val["jailed"], "validator is jailed"

# try to unjail
rsp = cli_2.unjail(addr)
assert rsp["code"] == 4, "still jailed, can't be unjailed"

# wait for 60s and unjail again
wait_for_block_time(
cli, isoparse(val["unbonding_time"]) + datetime.timedelta(seconds=60)
)
rsp = cli_2.unjail(addr)
assert rsp["code"] == 0, f"unjail should success {rsp}"

wait_for_new_blocks(cli, 3)
assert len(cli.validators()) == 4

0 comments on commit c037871

Please sign in to comment.