Skip to content

Commit

Permalink
fix: fixed nayduck test state_sync_fail.py for nightly build (#9320)
Browse files Browse the repository at this point in the history
In #9274 I introduced simple nightshade V2 layout and added it to the nightly build. This broke the nayduck test state_sync_fail.py. Here is the fix for it. 

The test performs resharding and then checks some postconditions. It broke because it attempted to reshard from V0 shard layout to V2 shard layout. This doesn't work because ShardLayout contains shard split map that only makes sense when resharding from a shard layout version to the immediate next. 

The fix is to check what is the protocol version supported in the binary and depending on it reshard from V0 to V1 or from V1 to V2.
  • Loading branch information
wacban authored Jul 17, 2023
1 parent 0ec75de commit 975aebb
Show file tree
Hide file tree
Showing 2 changed files with 127 additions and 11 deletions.
20 changes: 20 additions & 0 deletions pytest/lib/cluster.py
Original file line number Diff line number Diff line change
Expand Up @@ -946,3 +946,23 @@ def load_config():
else:
logger.info(f"Use default config {config}")
return config


# Returns the protocol version of the binary.
def get_binary_protocol_version(config) -> typing.Optional[int]:
binary_name = config.get('binary_name', 'neard')
near_root = config.get('near_root')
binary_path = os.path.join(near_root, binary_name)

# Get the protocol version of the binary
# The --version output looks like this:
# neard (release trunk) (build 1.1.0-3884-ge93793a61-modified) (rustc 1.71.0) (protocol 137) (db 37)
out = subprocess.check_output([binary_path, "--version"], text=True)
out = out.replace('(', '')
out = out.replace(')', '')
tokens = out.split()
n = len(tokens)
for i in range(n):
if tokens[i] == "protocol" and i + 1 < n:
return int(tokens[i + 1])
return None
118 changes: 107 additions & 11 deletions pytest/tests/sanity/state_sync_fail.py
Original file line number Diff line number Diff line change
@@ -1,29 +1,124 @@
#!/usr/bin/env python3
# Spins up a node, wait until sharding is upgrade
# and spins up another node
# check that the node can't be started because it cannot state sync to the epoch
# after the sharding upgrade

# Spins up a node, waits until sharding is upgraded and spins up another node.
# Check that the node can't be started because it cannot state sync to the epoch
# after the sharding upgrade.

# Depending on the version of the binary (default or nightly) it will perform
# resharding from V0 (1 shard) to V1 (4 shards) or from V1 (4 shards) to V2 (5
# shards).

import sys, time
import pathlib

sys.path.append(str(pathlib.Path(__file__).resolve().parents[2] / 'lib'))

from cluster import init_cluster, spin_up_node, load_config
from cluster import init_cluster, spin_up_node, load_config, get_binary_protocol_version
from configured_logger import logger
import requests
import utils

EPOCH_LENGTH = 10
START_AT_BLOCK = int(EPOCH_LENGTH * 2.5)

V1_PROTOCOL_VERSION = 48
V2_PROTOCOL_VERSION = 135

V0_SHARD_LAYOUT = {"V0": {"num_shards": 1, "version": 0}}
V1_SHARD_LAYOUT = {
"V1": {
"boundary_accounts": [
"aurora", "aurora-0", "kkuuue2akv_1630967379.near"
],
"shards_split_map": [[0, 1, 2, 3]],
"to_parent_shard_map": [0, 0, 0, 0],
"version": 1
}
}


def append_shard_layout_config_changes(
binary_protocol_version,
genesis_config_changes,
):
if binary_protocol_version >= V2_PROTOCOL_VERSION:
logger.info("Testing migration from V1 to V2.")
# Set the initial protocol version to a version just before V2.
genesis_config_changes.append([
"protocol_version",
V2_PROTOCOL_VERSION - 1,
])
genesis_config_changes.append([
"shard_layout",
V1_SHARD_LAYOUT,
])
genesis_config_changes.append([
"num_block_producer_seats_per_shard",
[1, 1, 1, 1],
])
genesis_config_changes.append([
"avg_hidden_validator_seats_per_shard",
[0, 0, 0, 0],
])
print(genesis_config_changes)
return

if binary_protocol_version >= V1_PROTOCOL_VERSION:
logger.info("Testing migration from V0 to V1.")
# Set the initial protocol version to a version just before V1.
genesis_config_changes.append([
"protocol_version",
V1_PROTOCOL_VERSION - 1,
])
genesis_config_changes.append([
"shard_layout",
V0_SHARD_LAYOUT,
])
genesis_config_changes.append([
"num_block_producer_seats_per_shard",
[100],
])
genesis_config_changes.append([
"avg_hidden_validator_seats_per_shard",
[0],
])
print(genesis_config_changes)
return

assert False


def get_genesis_config_changes(binary_protocol_version):
genesis_config_changes = [
["min_gas_price", 0],
["max_inflation_rate", [0, 1]],
["epoch_length", EPOCH_LENGTH],
["use_production_config", True],
["block_producer_kickout_threshold", 80],
]

append_shard_layout_config_changes(
binary_protocol_version,
genesis_config_changes,
)

print(genesis_config_changes)

return genesis_config_changes


config = load_config()

binary_protocol_version = get_binary_protocol_version(config)
assert binary_protocol_version is not None

near_root, node_dirs = init_cluster(
2, 1, 1, config,
[["min_gas_price", 0], ["max_inflation_rate", [0, 1]],
["epoch_length", EPOCH_LENGTH], ["protocol_version", 47],
["use_production_config", True], ["block_producer_kickout_threshold", 80]],
{
num_nodes=2,
num_observers=1,
num_shards=4,
config=config,
genesis_config_changes=get_genesis_config_changes(binary_protocol_version),
client_config_changes={
0: {
"tracked_shards": [0],
"state_sync_enabled": True,
Expand All @@ -42,7 +137,8 @@
"state_sync_enabled": True,
"store.state_snapshot_enabled": True,
}
})
},
)

started = time.time()

Expand Down

0 comments on commit 975aebb

Please sign in to comment.