Skip to content

Commit

Permalink
fix: test environments inherit and modify local genesis.json
Browse files Browse the repository at this point in the history
Signed-off-by: Shunkichi Sato <49983831+s8sato@users.noreply.github.com>
  • Loading branch information
s8sato committed Oct 29, 2024
1 parent 4b23527 commit 53137d9
Show file tree
Hide file tree
Showing 8 changed files with 106 additions and 49 deletions.
2 changes: 1 addition & 1 deletion crates/iroha_genesis/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -433,7 +433,7 @@ impl TryFrom<WasmPath> for WasmSmartContract {
impl WasmPath {
/// Resolve `self` to `here/self`,
/// assuming `self` is an unresolved relative path to `here`.
/// Must be applied once.
/// In case `self` is absolute, it replaces `here` i.e. this method mutates nothing.
fn resolve(&mut self, here: impl AsRef<Path>) {
self.0 = here.as_ref().join(&self.0)
}
Expand Down
45 changes: 35 additions & 10 deletions crates/iroha_swarm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -200,10 +200,15 @@ mod tests {
init: true
command: |-
/bin/bash -c "
kagami genesis generate --executor ../config/executor.wasm --wasm-dir ../config/libs --genesis-public-key $$GENESIS_PUBLIC_KEY > /tmp/genesis.default.json
EXECUTOR_RELATIVE_PATH=$(jq -r '.executor' /config/genesis.json) && \\
EXECUTOR_ABSOLUTE_PATH=$(realpath \"/config/$$EXECUTOR_RELATIVE_PATH\") && \\
WASM_DIR_RELATIVE_PATH=$(jq -r '.wasm_dir' /config/genesis.json) && \\
WASM_DIR_ABSOLUTE_PATH=$(realpath \"/config/$$WASM_DIR_RELATIVE_PATH\") && \\
jq \\
--arg executor \"$$EXECUTOR_ABSOLUTE_PATH\" \\
--arg wasm_dir \"$$WASM_DIR_ABSOLUTE_PATH\" \\
--argjson topology \"$$TOPOLOGY\" \\
'.topology = $$topology' /tmp/genesis.default.json \\
'.executor = $$executor | .wasm_dir = $$wasm_dir | .topology = $$topology' /config/genesis.json \\
>/tmp/genesis.json && \\
kagami genesis sign /tmp/genesis.json \\
--public-key $$GENESIS_PUBLIC_KEY \\
Expand Down Expand Up @@ -250,10 +255,15 @@ mod tests {
init: true
command: |-
/bin/bash -c "
kagami genesis generate --executor ../config/executor.wasm --wasm-dir ../config/libs --genesis-public-key $$GENESIS_PUBLIC_KEY > /tmp/genesis.default.json
EXECUTOR_RELATIVE_PATH=$(jq -r '.executor' /config/genesis.json) && \\
EXECUTOR_ABSOLUTE_PATH=$(realpath \"/config/$$EXECUTOR_RELATIVE_PATH\") && \\
WASM_DIR_RELATIVE_PATH=$(jq -r '.wasm_dir' /config/genesis.json) && \\
WASM_DIR_ABSOLUTE_PATH=$(realpath \"/config/$$WASM_DIR_RELATIVE_PATH\") && \\
jq \\
--arg executor \"$$EXECUTOR_ABSOLUTE_PATH\" \\
--arg wasm_dir \"$$WASM_DIR_ABSOLUTE_PATH\" \\
--argjson topology \"$$TOPOLOGY\" \\
'.topology = $$topology' /tmp/genesis.default.json \\
'.executor = $$executor | .wasm_dir = $$wasm_dir | .topology = $$topology' /config/genesis.json \\
>/tmp/genesis.json && \\
kagami genesis sign /tmp/genesis.json \\
--public-key $$GENESIS_PUBLIC_KEY \\
Expand Down Expand Up @@ -300,10 +310,15 @@ mod tests {
init: true
command: |-
/bin/bash -c "
kagami genesis generate --executor ../config/executor.wasm --wasm-dir ../config/libs --genesis-public-key $$GENESIS_PUBLIC_KEY > /tmp/genesis.default.json
EXECUTOR_RELATIVE_PATH=$(jq -r '.executor' /config/genesis.json) && \\
EXECUTOR_ABSOLUTE_PATH=$(realpath \"/config/$$EXECUTOR_RELATIVE_PATH\") && \\
WASM_DIR_RELATIVE_PATH=$(jq -r '.wasm_dir' /config/genesis.json) && \\
WASM_DIR_ABSOLUTE_PATH=$(realpath \"/config/$$WASM_DIR_RELATIVE_PATH\") && \\
jq \\
--arg executor \"$$EXECUTOR_ABSOLUTE_PATH\" \\
--arg wasm_dir \"$$WASM_DIR_ABSOLUTE_PATH\" \\
--argjson topology \"$$TOPOLOGY\" \\
'.topology = $$topology' /tmp/genesis.default.json \\
'.executor = $$executor | .wasm_dir = $$wasm_dir | .topology = $$topology' /config/genesis.json \\
>/tmp/genesis.json && \\
kagami genesis sign /tmp/genesis.json \\
--public-key $$GENESIS_PUBLIC_KEY \\
Expand Down Expand Up @@ -408,10 +423,15 @@ mod tests {
start_period: 4s
command: |-
/bin/bash -c "
kagami genesis generate --executor ../config/executor.wasm --wasm-dir ../config/libs --genesis-public-key $$GENESIS_PUBLIC_KEY > /tmp/genesis.default.json
EXECUTOR_RELATIVE_PATH=$(jq -r '.executor' /config/genesis.json) && \\
EXECUTOR_ABSOLUTE_PATH=$(realpath \"/config/$$EXECUTOR_RELATIVE_PATH\") && \\
WASM_DIR_RELATIVE_PATH=$(jq -r '.wasm_dir' /config/genesis.json) && \\
WASM_DIR_ABSOLUTE_PATH=$(realpath \"/config/$$WASM_DIR_RELATIVE_PATH\") && \\
jq \\
--arg executor \"$$EXECUTOR_ABSOLUTE_PATH\" \\
--arg wasm_dir \"$$WASM_DIR_ABSOLUTE_PATH\" \\
--argjson topology \"$$TOPOLOGY\" \\
'.topology = $$topology' /tmp/genesis.default.json \\
'.executor = $$executor | .wasm_dir = $$wasm_dir | .topology = $$topology' /config/genesis.json \\
>/tmp/genesis.json && \\
kagami genesis sign /tmp/genesis.json \\
--public-key $$GENESIS_PUBLIC_KEY \\
Expand Down Expand Up @@ -461,10 +481,15 @@ mod tests {
start_period: 4s
command: |-
/bin/bash -c "
kagami genesis generate --executor ../config/executor.wasm --wasm-dir ../config/libs --genesis-public-key $$GENESIS_PUBLIC_KEY > /tmp/genesis.default.json
EXECUTOR_RELATIVE_PATH=$(jq -r '.executor' /config/genesis.json) && \\
EXECUTOR_ABSOLUTE_PATH=$(realpath \"/config/$$EXECUTOR_RELATIVE_PATH\") && \\
WASM_DIR_RELATIVE_PATH=$(jq -r '.wasm_dir' /config/genesis.json) && \\
WASM_DIR_ABSOLUTE_PATH=$(realpath \"/config/$$WASM_DIR_RELATIVE_PATH\") && \\
jq \\
--arg executor \"$$EXECUTOR_ABSOLUTE_PATH\" \\
--arg wasm_dir \"$$WASM_DIR_ABSOLUTE_PATH\" \\
--argjson topology \"$$TOPOLOGY\" \\
'.topology = $$topology' /tmp/genesis.default.json \\
'.executor = $$executor | .wasm_dir = $$wasm_dir | .topology = $$topology' /config/genesis.json \\
>/tmp/genesis.json && \\
kagami genesis sign /tmp/genesis.json \\
--public-key $$GENESIS_PUBLIC_KEY \\
Expand Down
30 changes: 22 additions & 8 deletions crates/iroha_swarm/src/schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -243,12 +243,14 @@ impl std::fmt::Display for ContainerFile<'_> {
}
}

const GENESIS_FILE: Filename = Filename("genesis.json");
const CONFIG_FILE: Filename = Filename("client.toml");
const GENESIS_SIGNED_SCALE: Filename = Filename("genesis.signed.scale");

const CONTAINER_CONFIG_DIR: ContainerPath = ContainerPath("/config/");
const CONTAINER_TMP_DIR: ContainerPath = ContainerPath("/tmp/");

const CONTAINER_GENESIS_CONFIG: ContainerFile = ContainerFile(CONTAINER_CONFIG_DIR, GENESIS_FILE);
const CONTAINER_CLIENT_CONFIG: ContainerFile = ContainerFile(CONTAINER_CONFIG_DIR, CONFIG_FILE);
const CONTAINER_SIGNED_GENESIS: ContainerFile =
ContainerFile(CONTAINER_TMP_DIR, GENESIS_SIGNED_SCALE);
Expand All @@ -261,7 +263,7 @@ struct ReadOnly;
struct PathMapping<'a>(HostFile<'a>, ContainerFile<'a>, ReadOnly);

/// Mapping between host and container paths.
type Volumes<'a> = [PathMapping<'a>; 1];
type Volumes<'a> = [PathMapping<'a>; 2];

/// Healthcheck parameters.
#[derive(Debug)]
Expand Down Expand Up @@ -326,10 +328,15 @@ where
struct SignAndSubmitGenesis;

const SIGN_AND_SUBMIT_GENESIS: &str = r#"/bin/bash -c "
kagami genesis generate --executor ../config/executor.wasm --wasm-dir ../config/libs --genesis-public-key $$GENESIS_PUBLIC_KEY > /tmp/genesis.default.json
EXECUTOR_RELATIVE_PATH=$(jq -r '.executor' /config/genesis.json) && \\
EXECUTOR_ABSOLUTE_PATH=$(realpath \"/config/$$EXECUTOR_RELATIVE_PATH\") && \\
WASM_DIR_RELATIVE_PATH=$(jq -r '.wasm_dir' /config/genesis.json) && \\
WASM_DIR_ABSOLUTE_PATH=$(realpath \"/config/$$WASM_DIR_RELATIVE_PATH\") && \\
jq \\
--arg executor \"$$EXECUTOR_ABSOLUTE_PATH\" \\
--arg wasm_dir \"$$WASM_DIR_ABSOLUTE_PATH\" \\
--argjson topology \"$$TOPOLOGY\" \\
'.topology = $$topology' /tmp/genesis.default.json \\
'.executor = $$executor | .wasm_dir = $$wasm_dir | .topology = $$topology' /config/genesis.json \\
>/tmp/genesis.json && \\
kagami genesis sign /tmp/genesis.json \\
--public-key $$GENESIS_PUBLIC_KEY \\
Expand Down Expand Up @@ -528,11 +535,18 @@ impl<'a> DockerCompose<'a> {
}: &'a PeerSettings,
) -> Self {
let image = ImageId(name);
let volumes = [PathMapping(
HostFile(config_dir, CONFIG_FILE),
CONTAINER_CLIENT_CONFIG,
ReadOnly,
)];
let volumes = [
PathMapping(
HostFile(config_dir, GENESIS_FILE),
CONTAINER_GENESIS_CONFIG,
ReadOnly,
),
PathMapping(
HostFile(config_dir, CONFIG_FILE),
CONTAINER_CLIENT_CONFIG,
ReadOnly,
),
];
Self {
services: build_dir.as_ref().map_or_else(
|| {
Expand Down
13 changes: 11 additions & 2 deletions defaults/docker-compose.local.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ services:
- 1337:1337
- 8080:8080
volumes:
- ./genesis.json:/config/genesis.json:ro
- ./client.toml:/config/client.toml:ro
init: true
healthcheck:
Expand All @@ -32,10 +33,15 @@ services:
start_period: 4s
command: |-
/bin/bash -c "
kagami genesis generate --executor ../config/executor.wasm --wasm-dir ../config/libs --genesis-public-key $$GENESIS_PUBLIC_KEY > /tmp/genesis.default.json
EXECUTOR_RELATIVE_PATH=$(jq -r '.executor' /config/genesis.json) && \\
EXECUTOR_ABSOLUTE_PATH=$(realpath \"/config/$$EXECUTOR_RELATIVE_PATH\") && \\
WASM_DIR_RELATIVE_PATH=$(jq -r '.wasm_dir' /config/genesis.json) && \\
WASM_DIR_ABSOLUTE_PATH=$(realpath \"/config/$$WASM_DIR_RELATIVE_PATH\") && \\
jq \\
--arg executor \"$$EXECUTOR_ABSOLUTE_PATH\" \\
--arg wasm_dir \"$$WASM_DIR_ABSOLUTE_PATH\" \\
--argjson topology \"$$TOPOLOGY\" \\
'.topology = $$topology' /tmp/genesis.default.json \\
'.executor = $$executor | .wasm_dir = $$wasm_dir | .topology = $$topology' /config/genesis.json \\
>/tmp/genesis.json && \\
kagami genesis sign /tmp/genesis.json \\
--public-key $$GENESIS_PUBLIC_KEY \\
Expand All @@ -61,6 +67,7 @@ services:
- 1338:1338
- 8081:8081
volumes:
- ./genesis.json:/config/genesis.json:ro
- ./client.toml:/config/client.toml:ro
init: true
healthcheck:
Expand All @@ -86,6 +93,7 @@ services:
- 1339:1339
- 8082:8082
volumes:
- ./genesis.json:/config/genesis.json:ro
- ./client.toml:/config/client.toml:ro
init: true
healthcheck:
Expand All @@ -111,6 +119,7 @@ services:
- 1340:1340
- 8083:8083
volumes:
- ./genesis.json:/config/genesis.json:ro
- ./client.toml:/config/client.toml:ro
init: true
healthcheck:
Expand Down
10 changes: 8 additions & 2 deletions defaults/docker-compose.single.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ services:
- 1337:1337
- 8080:8080
volumes:
- ./genesis.json:/config/genesis.json:ro
- ./client.toml:/config/client.toml:ro
init: true
healthcheck:
Expand All @@ -31,10 +32,15 @@ services:
start_period: 4s
command: |-
/bin/bash -c "
kagami genesis generate --executor ../config/executor.wasm --wasm-dir ../config/libs --genesis-public-key $$GENESIS_PUBLIC_KEY > /tmp/genesis.default.json
EXECUTOR_RELATIVE_PATH=$(jq -r '.executor' /config/genesis.json) && \\
EXECUTOR_ABSOLUTE_PATH=$(realpath \"/config/$$EXECUTOR_RELATIVE_PATH\") && \\
WASM_DIR_RELATIVE_PATH=$(jq -r '.wasm_dir' /config/genesis.json) && \\
WASM_DIR_ABSOLUTE_PATH=$(realpath \"/config/$$WASM_DIR_RELATIVE_PATH\") && \\
jq \\
--arg executor \"$$EXECUTOR_ABSOLUTE_PATH\" \\
--arg wasm_dir \"$$WASM_DIR_ABSOLUTE_PATH\" \\
--argjson topology \"$$TOPOLOGY\" \\
'.topology = $$topology' /tmp/genesis.default.json \\
'.executor = $$executor | .wasm_dir = $$wasm_dir | .topology = $$topology' /config/genesis.json \\
>/tmp/genesis.json && \\
kagami genesis sign /tmp/genesis.json \\
--public-key $$GENESIS_PUBLIC_KEY \\
Expand Down
13 changes: 11 additions & 2 deletions defaults/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ services:
- 1337:1337
- 8080:8080
volumes:
- ./genesis.json:/config/genesis.json:ro
- ./client.toml:/config/client.toml:ro
init: true
healthcheck:
Expand All @@ -30,10 +31,15 @@ services:
start_period: 4s
command: |-
/bin/bash -c "
kagami genesis generate --executor ../config/executor.wasm --wasm-dir ../config/libs --genesis-public-key $$GENESIS_PUBLIC_KEY > /tmp/genesis.default.json
EXECUTOR_RELATIVE_PATH=$(jq -r '.executor' /config/genesis.json) && \\
EXECUTOR_ABSOLUTE_PATH=$(realpath \"/config/$$EXECUTOR_RELATIVE_PATH\") && \\
WASM_DIR_RELATIVE_PATH=$(jq -r '.wasm_dir' /config/genesis.json) && \\
WASM_DIR_ABSOLUTE_PATH=$(realpath \"/config/$$WASM_DIR_RELATIVE_PATH\") && \\
jq \\
--arg executor \"$$EXECUTOR_ABSOLUTE_PATH\" \\
--arg wasm_dir \"$$WASM_DIR_ABSOLUTE_PATH\" \\
--argjson topology \"$$TOPOLOGY\" \\
'.topology = $$topology' /tmp/genesis.default.json \\
'.executor = $$executor | .wasm_dir = $$wasm_dir | .topology = $$topology' /config/genesis.json \\
>/tmp/genesis.json && \\
kagami genesis sign /tmp/genesis.json \\
--public-key $$GENESIS_PUBLIC_KEY \\
Expand All @@ -56,6 +62,7 @@ services:
- 1338:1338
- 8081:8081
volumes:
- ./genesis.json:/config/genesis.json:ro
- ./client.toml:/config/client.toml:ro
init: true
healthcheck:
Expand All @@ -78,6 +85,7 @@ services:
- 1339:1339
- 8082:8082
volumes:
- ./genesis.json:/config/genesis.json:ro
- ./client.toml:/config/client.toml:ro
init: true
healthcheck:
Expand All @@ -100,6 +108,7 @@ services:
- 1340:1340
- 8083:8083
volumes:
- ./genesis.json:/config/genesis.json:ro
- ./client.toml:/config/client.toml:ro
init: true
healthcheck:
Expand Down
8 changes: 4 additions & 4 deletions scripts/build_wasm.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
set -e;

DEFAULTS_DIR="defaults"
CRATES_DIR="wasm"
TARGET_DIR="wasm/target/prebuilt"
CARGO_DIR="wasm"
TARGET_DIR="$CARGO_DIR/target/prebuilt"

build() {
case $1 in
Expand All @@ -18,15 +18,15 @@ build() {
;;
"samples")
NAMES=($(
cargo metadata --no-deps --manifest-path "$CRATES_DIR/Cargo.toml" --format-version=1 |
cargo metadata --no-deps --manifest-path "$CARGO_DIR/Cargo.toml" --format-version=1 |
jq '.packages | map(select(.targets[].kind | contains(["cdylib"]))) | map(.manifest_path | split("/")) | map(select(.[-3] == "samples")) | map(.[-2]) | .[]' -r
))
esac

mkdir -p "$TARGET_DIR/$1"
for name in ${NAMES[@]}; do
out_file="$TARGET_DIR/$1/$name.wasm"
cargo run --bin iroha_wasm_builder -- build "$CRATES_DIR/$1/$name" --optimize --out-file "$out_file"
cargo run --bin iroha_wasm_builder -- build "$CARGO_DIR/$1/$name" --optimize --out-file "$out_file"
done
echo "info: WASM $1 build complete"
echo "artifacts written to $TARGET_DIR/$1/"
Expand Down
34 changes: 14 additions & 20 deletions scripts/test_env.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ def __init__(self, args: argparse.Namespace):
tomli_w.dump(shared_config, f)

copy_or_prompt_build_bin("irohad", args.root_dir, peers_dir)
generate_genesis_json_and_change_topology(args, genesis_path, genesis_public_key, trusted_peers)
copy_genesis_json_and_change_topology(args, genesis_path, trusted_peers)
sign_genesis_with_kagami(args, genesis_path, genesis_public_key, genesis_private_key)


Expand Down Expand Up @@ -194,28 +194,22 @@ def kagami_generate_key_pair(out_dir: pathlib.Path, seed: str = None):
# dict with `{ public_key: string, private_key: string }`
return json.loads(kagami.stdout)

def generate_genesis_json_and_change_topology(args: argparse.Namespace, genesis_path, genesis_public_key, topology):
genesis_dir_abs = genesis_path.parent.resolve()
executor_abs = (args.root_dir / SWARM_CONFIGS_DIRECTORY / "executor.wasm").resolve()
wasm_dir_abs = (args.root_dir / SWARM_CONFIGS_DIRECTORY / "libs").resolve()
executor_rel = executor_abs.relative_to(genesis_dir_abs, walk_up=True)
wasm_dir_rel = wasm_dir_abs.relative_to(genesis_dir_abs, walk_up=True)

command = [
args.out_dir / "kagami",
"genesis",
"generate",
"--executor", executor_rel,
"--wasm-dir", wasm_dir_rel,
"--genesis-public-key", genesis_public_key
]
kagami = subprocess.run(command, capture_output=True)
if kagami.returncode:
logging.error("Kagami failed to generate genesis.json")
def copy_genesis_json_and_change_topology(args: argparse.Namespace, genesis_path, topology):
source_path = args.root_dir / SWARM_CONFIGS_DIRECTORY / "genesis.json"
try:
with open(source_path, 'r') as f:
genesis = json.load(f)
except FileNotFoundError:
logging.error(f"genesis config not found at {source_path}")
sys.exit(1)

genesis = json.loads(kagami.stdout)
# Early resolution to absolute paths:
# When the resulting genesis.json parsed, the second resolution doesn't affect them
executor_abs = (args.root_dir / SWARM_CONFIGS_DIRECTORY / genesis["executor"]).resolve()
wasm_dir_abs = (args.root_dir / SWARM_CONFIGS_DIRECTORY / genesis["wasm_dir"]).resolve()

genesis["executor"] = str(executor_abs)
genesis["wasm_dir"] = str(wasm_dir_abs)
genesis["topology"] = topology

with open(genesis_path, 'w') as f:
Expand Down

0 comments on commit 53137d9

Please sign in to comment.