Commit 4d5579cb authored by pengjun's avatar pengjun

add dapp-test-common

parent 22daf0c7
#!/usr/bin/env bash
echo_rst() {
if [ "$2" == 0 ]; then
echo -e "${GRE}$1 ok${NOC}"
else
echo -e "${RED}$1 fail${NOC}"
CASE_ERR="FAIL"
fi
}
function block_wait() {
local req='"method":"Chain33.GetLastHeader","params":[]'
cur_height=$(curl -ksd "{$req}" ${MAIN_HTTP} | jq ".result.height")
expect=$((cur_height + ${1}))
local count=0
while true; do
new_height=$(curl -ksd "{$req}" ${MAIN_HTTP} | jq ".result.height")
if [ "${new_height}" -ge "${expect}" ]; then
break
fi
count=$((count + 1))
sleep 1
done
echo "wait new block $count s, cur height=$expect,old=$cur_height"
}
function query_tx() {
block_wait 1
local txhash="$1"
local req='"method":"Chain33.QueryTransaction","params":[{"hash":"'"$txhash"'"}]'
# echo "req=$req"
local times=10
while true; do
ret=$(curl -ksd "{$req}" ${MAIN_HTTP} | jq -r ".result.tx.hash")
echo "====query tx= ${1}, return=$ret "
if [ "${ret}" != "${1}" ]; then
block_wait 1
times=$((times - 1))
if [ $times -le 0 ]; then
echo "====query tx=$1 failed"
echo "req=$req"
curl -ksd "{$req}" ${MAIN_HTTP}
exit 1
fi
else
echo "====query tx=$1 success"
break
fi
done
}
sendTx() {
echo "send tx '$1' begin"
signedTx=$1
type=$2
local req='"method":"Chain33.SendTransaction","params":[{"token":"BTY","data":"'"$signedTx"'"}]'
resp=$(curl -ksd "{$req}" ${MAIN_HTTP})
err=$(jq '(.error)' <<<"$resp")
txhash=$(jq -r ".result" <<<"$resp")
if [ "$err" == null ]; then
query_tx "$txhash"
if [ "$type" == "start" ]; then
GAME_ID=$txhash
fi
else
echo "send tx error:$err"
fi
echo "send tx '$1' end"
}
Chain33_SendToAddress() {
echo "send '$3' from '$1' to '$2' begin"
local from="$1"
local to="$2"
local amount=$3
local req='"method":"Chain33.SendToAddress", "params":[{"from":"'"$from"'","to":"'"$to"'", "amount":'"$amount"', "note":"test\n"}]'
resp=$(curl -ksd "{$req}" "${MAIN_HTTP}")
ok=$(jq '(.error|not) and (.result.hash|length==66)' <<<"$resp")
[ "$ok" == true ]
echo_rst "$FUNCNAME" "$?"
hash=$(jq -r ".result.hash" <<<"$resp")
query_tx "$hash"
echo "send '$3' from '$1' to '$2' end"
}
chain33_ImportPrivkey() {
local pri=$2
local acc=$3
local req='"method":"Chain33.ImportPrivkey", "params":[{"privkey":"'"$pri"'", "label":"pokerbullimportkey1"}]'
echo "#request: $req"
resp=$(curl -ksd "{$req}" "$1")
echo "#response: $resp"
ok=$(jq '(.error|not) and (.result.label=="pokerbullimportkey1") and (.result.acc.addr == "'"$acc"'")' <<<"$resp")
[ "$ok" == true ]
echo_rst "$FUNCNAME" "$?"
}
signrawtx() {
echo "sign tx '$1' begin"
txHex="$1"
priKey="$2"
type="$3"
local req='"method":"Chain33.SignRawTx","params":[{"privkey":"'"$priKey"'","txHex":"'"$txHex"'","expire":"120s"}]'
signedTx=$(curl -ksd "{$req}" ${MAIN_HTTP} | jq -r ".result")
if [ "$signedTx" != null ]; then
sendTx "$signedTx" "$3"
else
echo "signedTx null error"
fi
echo "sign tx '$1' end"
}
\ No newline at end of file
......@@ -2,11 +2,13 @@
# shellcheck disable=SC2128
RPC_TESTFILE=test-rpc.sh
DAPP_TEST_COMMON=dapp-test-common.sh
function dapp_test_rpc() {
local ip=$1
echo "============ # dapp rpc test begin ============="
if [ -d dapptest ]; then
cp $DAPP_TEST_COMMON dapptest/
cd dapptest || return
dir=$(find . -maxdepth 1 -type d ! -name dapptest ! -name . | sed 's/^\.\///')
for app in $dir; do
......
......@@ -12,56 +12,7 @@ RED='\033[1;31m'
GRE='\033[1;32m'
NOC='\033[0m'
echo_rst() {
if [ "$2" == 0 ]; then
echo -e "${GRE}$1 ok${NOC}"
else
echo -e "${RED}$1 fail${NOC}"
CASE_ERR="FAIL"
fi
}
function block_wait() {
local req='"method":"Chain33.GetLastHeader","params":[]'
cur_height=$(curl -ksd "{$req}" ${MAIN_HTTP} | jq ".result.height")
expect=$((cur_height + ${1}))
local count=0
while true; do
new_height=$(curl -ksd "{$req}" ${MAIN_HTTP} | jq ".result.height")
if [ "${new_height}" -ge "${expect}" ]; then
break
fi
count=$((count + 1))
sleep 1
done
echo "wait new block $count s, cur height=$expect,old=$cur_height"
}
function query_tx() {
block_wait 1
local txhash="$1"
local req='"method":"Chain33.QueryTransaction","params":[{"hash":"'"$txhash"'"}]'
# echo "req=$req"
local times=10
while true; do
ret=$(curl -ksd "{$req}" ${MAIN_HTTP} | jq -r ".result.tx.hash")
echo "====query tx= ${1}, return=$ret "
if [ "${ret}" != "${1}" ]; then
block_wait 1
times=$((times - 1))
if [ $times -le 0 ]; then
echo "====query tx=$1 failed"
echo "req=$req"
curl -ksd "{$req}" ${MAIN_HTTP}
exit 1
fi
else
echo "====query tx=$1 success"
break
fi
done
}
source ../dapp-test-common.sh
pokerbull_PlayRawTx() {
echo "========== # pokerbull play tx begin =========="
......@@ -145,68 +96,6 @@ pokerbull_QueryResult() {
echo "========== # pokerbull query result end =========="
}
chain33_ImportPrivkey() {
local pri=$2
local acc=$3
local req='"method":"Chain33.ImportPrivkey", "params":[{"privkey":"'"$pri"'", "label":"pokerbullimportkey1"}]'
echo "#request: $req"
resp=$(curl -ksd "{$req}" "$1")
echo "#response: $resp"
ok=$(jq '(.error|not) and (.result.label=="pokerbullimportkey1") and (.result.acc.addr == "'"$acc"'")' <<<"$resp")
[ "$ok" == true ]
echo_rst "$FUNCNAME" "$?"
}
signrawtx() {
echo "sign tx '$1' begin"
txHex="$1"
priKey="$2"
type="$3"
local req='"method":"Chain33.SignRawTx","params":[{"privkey":"'"$priKey"'","txHex":"'"$txHex"'","expire":"120s"}]'
signedTx=$(curl -ksd "{$req}" ${MAIN_HTTP} | jq -r ".result")
if [ "$signedTx" != null ]; then
sendTx "$signedTx" "$3"
else
echo "signedTx null error"
fi
echo "sign tx '$1' end"
}
sendTx() {
echo "send tx '$1' begin"
signedTx=$1
type=$2
local req='"method":"Chain33.SendTransaction","params":[{"token":"BTY","data":"'"$signedTx"'"}]'
resp=$(curl -ksd "{$req}" ${MAIN_HTTP})
err=$(jq '(.error)' <<<"$resp")
txhash=$(jq -r ".result" <<<"$resp")
if [ "$err" == null ]; then
query_tx "$txhash"
if [ "$type" == "start" ]; then
GAME_ID=$txhash
fi
else
echo "send tx error:$err"
fi
echo "send tx '$1' end"
}
Chain33_SendToAddress() {
echo "send '$3' from '$1' to '$2' begin"
local from="$1"
local to="$2"
local amount=$3
local req='"method":"Chain33.SendToAddress", "params":[{"from":"'"$from"'","to":"'"$to"'", "amount":'"$amount"', "note":"test\n"}]'
resp=$(curl -ksd "{$req}" "${MAIN_HTTP}")
ok=$(jq '(.error|not) and (.result.hash|length==66)' <<<"$resp")
[ "$ok" == true ]
echo_rst "$FUNCNAME" "$?"
hash=$(jq -r ".result.hash" <<<"$resp")
query_tx "$hash"
echo "send '$3' from '$1' to '$2' end"
}
init() {
ispara=$(echo '"'"${MAIN_HTTP}"'"' | jq '.|contains("8901")')
echo "ipara=$ispara"
......@@ -237,6 +126,7 @@ function run_test() {
pokerbull_QueryResult
}
function main() {
MAIN_HTTP="$1"
echo "=========== # pokerbull rpc test ============="
......
......@@ -11,56 +11,7 @@ RED='\033[1;31m'
GRE='\033[1;32m'
NOC='\033[0m'
echo_rst() {
if [ "$2" == 0 ]; then
echo -e "${GRE}$1 ok${NOC}"
else
echo -e "${RED}$1 fail${NOC}"
CASE_ERR="FAIL"
fi
}
function block_wait() {
local req='"method":"Chain33.GetLastHeader","params":[]'
cur_height=$(curl -ksd "{$req}" ${MAIN_HTTP} | jq ".result.height")
expect=$((cur_height + ${1}))
local count=0
while true; do
new_height=$(curl -ksd "{$req}" ${MAIN_HTTP} | jq ".result.height")
if [ "${new_height}" -ge "${expect}" ]; then
break
fi
count=$((count + 1))
sleep 1
done
echo "wait new block $count s, cur height=$expect,old=$cur_height"
}
function query_tx() {
block_wait 1
local txhash="$1"
local req='"method":"Chain33.QueryTransaction","params":[{"hash":"'"$txhash"'"}]'
# echo "req=$req"
local times=10
while true; do
ret=$(curl -ksd "{$req}" ${MAIN_HTTP} | jq -r ".result.tx.hash")
echo "====query tx= ${1}, return=$ret "
if [ "${ret}" != "${1}" ]; then
block_wait 1
times=$((times - 1))
if [ $times -le 0 ]; then
echo "====query tx=$1 failed"
echo "req=$req"
curl -ksd "{$req}" ${MAIN_HTTP}
exit 1
fi
else
echo "====query tx=$1 success"
break
fi
done
}
source ../dapp-test-common.sh
retrieve_Backup() {
echo "========== # retrieve backup begin =========="
......@@ -152,65 +103,6 @@ retrieve_QueryResult() {
echo "========== # retrieve query result end =========="
}
chain33_ImportPrivkey() {
local pri=$2
local acc=$3
local req='"method":"Chain33.ImportPrivkey", "params":[{"privkey":"'"$pri"'", "label":"retrieveimportkey1"}]'
echo "#request: $req"
resp=$(curl -ksd "{$req}" "$1")
echo "#response: $resp"
ok=$(jq '(.error|not) and (.result.label=="retrieveimportkey1") and (.result.acc.addr == "'"$acc"'")' <<<"$resp")
[ "$ok" == true ]
echo_rst "$FUNCNAME" "$?"
}
signrawtx() {
txHex="$1"
priKey="$2"
local req='"method":"Chain33.SignRawTx","params":[{"privkey":"'"$priKey"'","txHex":"'"$txHex"'","expire":"120s"}]'
echo "#request SignRawTx: $req"
signedTx=$(curl -ksd "{$req}" ${MAIN_HTTP} | jq -r ".result")
echo "signedTx=$signedTx"
if [ "$signedTx" != null ]; then
sendTx "$signedTx"
else
echo "signedTx null error"
fi
}
sendTx() {
signedTx=$1
local req='"method":"Chain33.SendTransaction","params":[{"token":"BTY","data":"'"$signedTx"'"}]'
echo "#request sendTx: $req"
# curl -ksd "{$req}" ${MAIN_HTTP}
resp=$(curl -ksd "{$req}" ${MAIN_HTTP})
err=$(jq '(.error)' <<<"$resp")
txhash=$(jq -r ".result" <<<"$resp")
if [ "$err" == null ]; then
echo "tx hash: $txhash"
query_tx "$txhash"
else
echo "send tx error:$err"
fi
}
Chain33_SendToAddress() {
local from="$1"
local to="$2"
local amount=$3
local req='"method":"Chain33.SendToAddress", "params":[{"from":"'"$from"'","to":"'"$to"'", "amount":'"$amount"', "note":"test\n"}]'
echo "#request: $req"
resp=$(curl -ksd "{$req}" "${MAIN_HTTP}")
echo "#response: $resp"
ok=$(jq '(.error|not) and (.result.hash|length==66)' <<<"$resp")
[ "$ok" == true ]
echo_rst "$FUNCNAME" "$?"
hash=$(jq -r ".result.hash" <<<"$resp")
query_tx "$hash"
}
init() {
ispara=$(echo '"'"${MAIN_HTTP}"'"' | jq '.|contains("8901")')
echo "ipara=$ispara"
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment