Skip to content

Commit

Permalink
add wallet for each nodes to simplify code
Browse files Browse the repository at this point in the history
  • Loading branch information
chenyukang committed Jun 11, 2024
1 parent a987e9b commit 0fed258
Show file tree
Hide file tree
Showing 9 changed files with 44 additions and 32 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/e2e.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ on: [push, pull_request]
name: E2E tests

jobs:
run-e2e-test:
e2e-testing:
strategy:
fail-fast: false
matrix:
Expand Down
4 changes: 3 additions & 1 deletion tests/deploy/deploy.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ set -xeuo pipefail
script_dir="$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" &>/dev/null && pwd)"
data_dir="$script_dir/node-data"
udt_init_dir="$script_dir/udt-init"
nodes_dir="$script_dir/../nodes"
cd "$script_dir" || exit 1

miner_key_file="$data_dir/specs/miner.key"
Expand Down Expand Up @@ -35,7 +36,7 @@ function deploy() {
GENESIS_TX0="$(ckb -C "$data_dir" list-hashes | sed -n 's/tx_hash = "\(.*\)"/\1/p' | head -1)"
sed "s/0x8f8c79eb6671709633fe6a46de93c0fedc9c1b8a6527a18d3983879542635c9f/$GENESIS_TX0/" "$TEMPLATE_FILE" >"$CONFIG_FILE"

ckb-cli deploy gen-txs --from-address ckt1qzda0cr08m85hc8jlnfp3zer7xulejywt49kt2rr0vthywaa50xwsqwgx292hnvmn68xf779vmzrshpmm6epn4c0cgwga \
ckb-cli deploy gen-txs --from-address $(cat "$nodes_dir/deployer/ckb-chain/wallet") \
--fee-rate 100000 --deployment-config "$CONFIG_FILE" --info-file "$INFO_FILE" --migration-dir "$MIGRATION_DIR"

ckb-cli deploy sign-txs --add-signatures --info-file "$INFO_FILE" --privkey-path "$miner_key_file" --output-format json | sed -n 's/: \("[^"]*"\)/: [\1]/p'
Expand All @@ -58,6 +59,7 @@ deploy_and_generate_blocks() {

run_udt_init() {
export $(xargs <".env")
export NODES_DIR="$nodes_dir"
(
cd "$udt_init_dir" || exit 1
cargo run -- "$@"
Expand Down
9 changes: 5 additions & 4 deletions tests/deploy/init-dev-chain.sh
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ check_deps ckb ckb-cli perl

script_dir="$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" &>/dev/null && pwd)"
data_dir="$script_dir/node-data"
nodes_dir="$script_dir/../nodes"

# If -f is used, we will remove old state data. Otherwise we will skip the initialization.
while getopts "f" opt; do
Expand Down Expand Up @@ -48,21 +49,21 @@ if ! [[ -d "$data_dir" ]]; then

# Transfer some money to the node 1.
# The address of node 1 can be seen with the following command:
# echo | HOME=/tmp ckb-cli account import --local-only --privkey-path "$script_dir/../nodes/1/ckb-chain/key"
ckb-cli wallet transfer --to-address ckt1qzda0cr08m85hc8jlnfp3zer7xulejywt49kt2rr0vthywaa50xwsqgx5lf4pczpamsfam48evs0c8nvwqqa59qapt46f --capacity 5000000000 --fee-rate 2000 --privkey-path "$script_dir/../nodes/deployer/ckb-chain/key"
# echo | HOME=/tmp ckb-cli account import --local-only --privkey-path "$$nodes_dir/1/ckb-chain/key"
ckb-cli wallet transfer --to-address $(cat "$nodes_dir/1/ckb-chain/wallet") --capacity 5000000000 --fee-rate 2000 --privkey-path "$nodes_dir/deployer/ckb-chain/key"

sleep 1
"$script_dir/generate-blocks.sh" 6
sleep 1

# Transfer some money to the node 2.
ckb-cli wallet transfer --to-address ckt1qzda0cr08m85hc8jlnfp3zer7xulejywt49kt2rr0vthywaa50xwsqt4vqqyehpxn47deg5l6eeqtkfrt5kfkfchkwv62 --capacity 5000000000 --fee-rate 2000 --privkey-path "$script_dir/../nodes/deployer/ckb-chain/key"
ckb-cli wallet transfer --to-address $(cat "$nodes_dir/2/ckb-chain/wallet") --capacity 5000000000 --fee-rate 2000 --privkey-path "$nodes_dir/deployer/ckb-chain/key"
sleep 1
"$script_dir/generate-blocks.sh" 6
sleep 1

# Transfer some money to the node 3.
ckb-cli wallet transfer --to-address ckt1qzda0cr08m85hc8jlnfp3zer7xulejywt49kt2rr0vthywaa50xwsqtrnd9f2lh5vlwlj23dedf7jje65cdj8qs7q4awr --capacity 5000000000 --fee-rate 2000 --privkey-path "$script_dir/../nodes/deployer/ckb-chain/key"
ckb-cli wallet transfer --to-address $(cat "$nodes_dir/3/ckb-chain/wallet") --capacity 5000000000 --fee-rate 2000 --privkey-path "$nodes_dir/deployer/ckb-chain/key"
sleep 1
# Generate a few blocks so that above transaction is confirmed.
echo "begin to generate blocks for wallet updating..."
Expand Down
55 changes: 30 additions & 25 deletions tests/deploy/udt-init/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,24 +78,22 @@ fn generate_configuration(
let mut configuration =
TransactionBuilderConfiguration::new_devnet().expect("new devnet configuration");

let sighash_handler = gen_dev_sighash_handler();
configuration.register_script_handler(Box::new(sighash_handler));
let sudt_handler = gen_dev_udt_handler();
configuration.register_script_handler(Box::new(sudt_handler));
configuration.register_script_handler(Box::new(gen_dev_sighash_handler()));
configuration.register_script_handler(Box::new(gen_dev_udt_handler()));
return Ok((network_info, configuration));
}

fn init_or_send_udt(
issuer_address: &str,
sender_info: &(&str, H256),
sender_info: &(String, H256),
receiver_address: Option<&str>,
sudt_amount: u128,
apply: bool,
) -> Result<(), Box<dyn StdErr>> {
let (network_info, configuration) = generate_configuration()?;

let issuer = Address::from_str(issuer_address)?;
let sender = Address::from_str(sender_info.0)?;
let sender = Address::from_str(&sender_info.0)?;
let receiver = if let Some(addr) = receiver_address {
Address::from_str(addr)?
} else {
Expand Down Expand Up @@ -174,53 +172,60 @@ fn generate_udt_type_script(address: &str) -> ckb_types::packed::Script {
res
}

fn main() -> Result<(), Box<dyn StdErr>> {
// The address is from
// ckb-cli account import --local-only --privkey-path tests/nodes/deployer/ckb-chain/key
let udt_owner = ("ckt1qzda0cr08m85hc8jlnfp3zer7xulejywt49kt2rr0vthywaa50xwsqwgx292hnvmn68xf779vmzrshpmm6epn4c0cgwga", h256!("0xd00c06bfd800d27397002dca6fb0993d5ba6399b4238b2f29ee9deb97593d2bc"));
fn get_nodes_info(node: &str) -> (String, H256) {
let nodes_dir = std::env::var("NODES_DIR").expect("env var");
let node_dir = format!("{}/{}", nodes_dir, node);
let wallet =
std::fs::read_to_string(format!("{}/ckb-chain/wallet", node_dir)).expect("read failed");
let key = std::fs::read_to_string(format!("{}/ckb-chain/key", node_dir)).expect("read failed");
eprintln!("node {}: wallet: {}", node, wallet);
(wallet, H256::from_str(&key.trim()).expect("parse hex"))
}

fn main() -> Result<(), Box<dyn StdErr>> {
let udt_owner = get_nodes_info("deployer");
let wallets = [
("ckt1qzda0cr08m85hc8jlnfp3zer7xulejywt49kt2rr0vthywaa50xwsqgx5lf4pczpamsfam48evs0c8nvwqqa59qapt46f", h256!("0xcccd5f7e693b60447623fb71a5983f15a426938c33699b1a81d1239cfa656cd1")),
("ckt1qzda0cr08m85hc8jlnfp3zer7xulejywt49kt2rr0vthywaa50xwsqt4vqqyehpxn47deg5l6eeqtkfrt5kfkfchkwv62", h256!("0x85af6ff21ea891dbb384b771e02317427e7b66e84b4516c03d74ca4fd5ad0500")),
("ckt1qzda0cr08m85hc8jlnfp3zer7xulejywt49kt2rr0vthywaa50xwsqtrnd9f2lh5vlwlj23dedf7jje65cdj8qs7q4awr", h256!("0xd00c06bfd800d27397002dca6fb0993d5ba6399b4238b2f29ee9deb975ffffff")),
get_nodes_info("1"),
get_nodes_info("2"),
get_nodes_info("3"),
];

init_or_send_udt(udt_owner.0, &udt_owner, None, 1000000000000, true).expect("init udt");
init_or_send_udt(&udt_owner.0, &udt_owner, None, 1000000000000, true).expect("init udt");
generate_blocks(4).expect("ok");

init_or_send_udt(
udt_owner.0,
&udt_owner.0,
&udt_owner,
Some(wallets[0].0),
Some(&wallets[0].0),
200000000000,
true,
)?;
generate_blocks(4).expect("ok");

init_or_send_udt(
udt_owner.0,
&udt_owner.0,
&udt_owner,
Some(wallets[1].0),
Some(&wallets[1].0),
200000000000,
true,
)?;
generate_blocks(4).expect("ok");

init_or_send_udt(
udt_owner.0,
&udt_owner.0,
&udt_owner,
Some(wallets[2].0),
Some(&wallets[2].0),
200000000000,
true,
)?;
generate_blocks(4).expect("ok");

check_account(udt_owner.0, udt_owner.0)?;
check_account(wallets[0].0, udt_owner.0)?;
check_account(wallets[1].0, udt_owner.0)?;
check_account(wallets[2].0, udt_owner.0)?;
check_account(&udt_owner.0, &udt_owner.0)?;
check_account(&wallets[0].0, &udt_owner.0)?;
check_account(&wallets[1].0, &udt_owner.0)?;
check_account(&wallets[2].0, &udt_owner.0)?;

let script = generate_udt_type_script(udt_owner.0);
let script = generate_udt_type_script(&udt_owner.0);
println!("initialized udt_type_script: {} ...", script);

Ok(())
Expand Down
1 change: 1 addition & 0 deletions tests/nodes/1/ckb-chain/wallet
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ckt1qzda0cr08m85hc8jlnfp3zer7xulejywt49kt2rr0vthywaa50xwsqgx5lf4pczpamsfam48evs0c8nvwqqa59qapt46f
2 changes: 1 addition & 1 deletion tests/nodes/2/ckb-chain/key
Original file line number Diff line number Diff line change
@@ -1 +1 @@
85af6ff21ea891dbb384b771e02317427e7b66e84b4516c03d74ca4fd5ad0500
85af6ff21ea891dbb384b771e02317427e7b66e84b4516c03d74ca4fd5ad0500
1 change: 1 addition & 0 deletions tests/nodes/2/ckb-chain/wallet
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ckt1qzda0cr08m85hc8jlnfp3zer7xulejywt49kt2rr0vthywaa50xwsqt4vqqyehpxn47deg5l6eeqtkfrt5kfkfchkwv62
1 change: 1 addition & 0 deletions tests/nodes/3/ckb-chain/wallet
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ckt1qzda0cr08m85hc8jlnfp3zer7xulejywt49kt2rr0vthywaa50xwsqtrnd9f2lh5vlwlj23dedf7jje65cdj8qs7q4awr
1 change: 1 addition & 0 deletions tests/nodes/deployer/ckb-chain/wallet
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ckt1qzda0cr08m85hc8jlnfp3zer7xulejywt49kt2rr0vthywaa50xwsqwgx292hnvmn68xf779vmzrshpmm6epn4c0cgwga

0 comments on commit 0fed258

Please sign in to comment.