Skip to content

Commit

Permalink
Test: Add bats test for epoch_length_in_dummy param
Browse files Browse the repository at this point in the history
Signed-off-by: Eval EXEC <[email protected]>
  • Loading branch information
eval-exec committed Aug 11, 2023
1 parent 6e572ef commit 1cb06af
Show file tree
Hide file tree
Showing 2 changed files with 128 additions and 1 deletion.
9 changes: 8 additions & 1 deletion util/app-config/src/tests/cli_test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ trap cleanup EXIT

cp target/release/ckb ${CKB_BATS_TESTBED}
cp util/app-config/src/tests/*.bats ${CKB_BATS_TESTBED}
cp -r util/app-config/src/tests/later_bats_job ${CKB_BATS_TESTBED}
cp util/app-config/src/tests/*.sh ${CKB_BATS_TESTBED}

if [ ! -d "/tmp/ckb_bats_assets/" ]; then
Expand Down Expand Up @@ -47,9 +48,15 @@ export TMP_DIR=${CKB_BATS_TESTBED}/tmp_dir
mkdir ${TMP_DIR}

for bats_cases in *.bats; do
bats "$bats_cases"
bats --verbose-run --print-output-on-failure --show-output-of-passing-tests "$bats_cases"
ret=$?
if [ "$ret" -ne "0" ]; then
exit "$ret"
fi
done

bats --verbose-run --print-output-on-failure --show-output-of-passing-tests ./later_bats_job/change_epoch.bats
ret=$?
if [ "$ret" -ne "0" ]; then
exit "$ret"
fi
120 changes: 120 additions & 0 deletions util/app-config/src/tests/later_bats_job/change_epoch.bats
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
#!/usr/bin/env bats
set -e

bats_load_library 'bats-assert'
bats_load_library 'bats-support'

NUMBER_OFFSET=0
NUMBER_BITS=24
NUMBER_MAXIMUM_VALUE=$((1 << NUMBER_BITS))
NUMBER_MASK=$((NUMBER_MAXIMUM_VALUE - 1))

INDEX_OFFSET=$((NUMBER_BITS))
INDEX_BITS=16
INDEX_MAXIMUM_VALUE=$((1 << INDEX_BITS))
INDEX_MASK=$((INDEX_MAXIMUM_VALUE - 1))

LENGTH_OFFSET=$((NUMBER_BITS + INDEX_BITS))
LENGTH_BITS=16
LENGTH_MAXIMUM_VALUE=$((1 << LENGTH_BITS))
LENGTH_MASK=$((LENGTH_MAXIMUM_VALUE - 1))

function extract_epoch_number() {
local value=$1
echo $(( (value >> NUMBER_OFFSET) & NUMBER_MASK ))
}

function extract_epoch_index() {
local value=$1
echo $(( (value >> INDEX_OFFSET) & INDEX_MASK ))
}

function extract_epoch_length() {
local value=$1
echo $(( (value >> LENGTH_OFFSET) & LENGTH_MASK ))
}

function tip_header_epoch() {
curl -s -X POST http://127.0.0.1:8114 \
-H 'Content-Type: application/json' \
-d '{ "id": 42, "jsonrpc": "2.0", "method": "get_tip_header", "params": [ ] }' \
| jq .result.epoch | xargs -I{} printf "%d\n" {}
}

function tip_header_number() {
curl -s -X POST http://127.0.0.1:8114 \
-H 'Content-Type: application/json' \
-d '{ "id": 42, "jsonrpc": "2.0", "method": "get_tip_header", "params": [ ] }' \
| jq .result.number | xargs -I{} printf "%d\n" {}
}

function block_kill() {
kill $1
while kill -0 $1; do
echo "waiting for $1 to exit"
sleep 1
done
}

function ckb_change_epoch_length_for_dumm_mode { #@test
ckb run -C ${CKB_DIRNAME} &> /dev/null &

CKB_NODE_PID=$!
sleep 5


TIP_EPOCH=$(tip_header_epoch)

TIP_EPOCH_NUMBER=$(extract_epoch_number ${TIP_EPOCH})
TIP_EPOCH_INDEX=$(extract_epoch_index ${TIP_EPOCH})
TIP_EPOCH_LENGTH=$(extract_epoch_length ${TIP_EPOCH})
TIP_NUMBER=$(tip_header_number)

echo tip_number is ${TIP_NUMBER}
echo tip_epoch_number is ${TIP_EPOCH_NUMBER}, tip_epoch_index is ${TIP_EPOCH_INDEX}, tip_epoch_length is ${TIP_EPOCH_LENGTH}

kill ${CKB_NODE_PID}

block_kill ${CKB_NODE_PID}

wget https://raw.githubusercontent.com/nervosnetwork/ckb/develop/resource/specs/mainnet.toml

ckb init -c dev --import-spec mainnet.toml --force

sed -i 's/Eaglesong/Dummy/g' specs/dev.toml
sed -i '/genesis_epoch_length = 1743/a permanent_difficulty_in_dummy = true\epoch_duration_target = 80\n' specs/dev.toml

sed -i 's/poll_interval = 1000/poll_interval = 1/g' ckb-miner.toml
sed -i 's/value = 5000/value = 1/g' ckb-miner.toml

sed -i 's/# \[block_assembler\]/\[block_assembler\]/g' ckb.toml
sed -i 's/# code_hash =/code_hash =/g' ckb.toml
sed -i 's/# args = "ckb-cli util blake2b --prefix-160 <compressed-pubkey>"/args = "0xc8328aabcd9b9e8e64fbc566c4385c3bdeb219d7"/g' ckb.toml
sed -i 's/# hash_type =/hash_type =/g' ckb.toml
sed -i 's/# message = "A 0x-prefixed hex string"/message = "0x"/g' ckb.toml



ckb run --skip-spec-check --overwrite-spec -C ${CKB_DIRNAME} &> /dev/null &
CKB_NODE_PID=$!

ckb miner -C ${CKB_DIRNAME} &> /dev/null &
CKB_MINER_PID=$!

sleep 5

while [ $(tip_header_number) -lt $(( ${TIP_NUMBER} + ${TIP_EPOCH_LENGTH} )) ]; do
echo waiting for tip_number to be $(( ${TIP_NUMBER} + ${TIP_EPOCH_LENGTH} ))
sleep 1
done

echo latest tip_header_number is $(tip_header_number)
echo latest tip_header_epoch length is $(extract_epoch_length $(tip_header_epoch))
echo latest tip_header_epoch number is $(extract_epoch_number $(tip_header_epoch))

# we specified epoch_duration_target = 80 in dev.toml, so the epoch length should be 10
assert [ $(extract_epoch_length $(tip_header_epoch)) -eq 10 ]

block_kill ${CKB_NODE_PID}
block_kill ${CKB_MINER_PID}
}

0 comments on commit 1cb06af

Please sign in to comment.