Commit bbbba0f1 authored by pengjun's avatar pengjun

add retrieve rpc-test

parent bf75f36f
......@@ -5,10 +5,7 @@ set -o pipefail
MAIN_HTTP=""
CASE_ERR=""
START_TX=""
CONTINUE_TX=""
QUIT_TX=""
PLAY_TX=""
GAME_ID=""
#color
RED='\033[1;31m'
......@@ -16,7 +13,7 @@ GRE='\033[1;32m'
NOC='\033[0m'
echo_rst() {
if [ "$2" == true ]; then
if [ "$2" == 0 ]; then
echo -e "${GRE}$1 ok${NOC}"
else
echo -e "${RED}$1 fail${NOC}"
......@@ -25,74 +22,156 @@ echo_rst() {
}
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
}
pokerbull_PlayRawTx() {
tx=$(curl -ksd '{"method":"Chain33.CreateTransaction","params":[{"execer":"pokerbull","actionName":"Play","payload":{"gameID":"'$START_TX'", "value":"1000000000", "round":1}}]}' ${MAIN_HTTP} | jq -r ".result")
tx=$(curl -ksd '{"method":"Chain33.CreateTransaction","params":[{"execer":"pokerbull","actionName":"Play","payload":{"gameId":"pokerbull-abc", "value":"1000000000", "round":1}}]}' ${MAIN_HTTP} | jq -r ".result")
PLAY_TX=$tx
data=$(curl -ksd '{"method":"Chain33.DecodeRawTransaction","params":[{"txHex":"'"$tx"'"}]}' ${MAIN_HTTP} | jq -r ".result.txs[0]")
ok=$(jq '(.execer == "Play")' <<<"$data")
ok=$(jq '(.execer == "pokerbull")' <<<"$data")
[ "$ok" == true ]
rst=$?
echo_rst "$FUNCNAME" "$rst"
echo_rst "$FUNCNAME" "$?"
signrawtx "$tx" "CC38546E9E659D15E6B4893F0AB32A06D103931A8230B0BDE71459D2B27D6944" "play"
}
pokerbull_QuitRawTx() {
tx=$(curl -ksd '{"method":"Chain33.CreateTransaction","params":[{"execer":"pokerbull","actionName":"Quit","payload":{"gameID":"'$START_TX'"}}]}' ${MAIN_HTTP} | jq -r ".result")
tx=$(curl -ksd '{"method":"Chain33.CreateTransaction","params":[{"execer":"pokerbull","actionName":"Quit","payload":{"gameId":"'$GAME_ID'"}}]}' ${MAIN_HTTP} | jq -r ".result")
QUIT_TX=$tx
data=$(curl -ksd '{"method":"Chain33.DecodeRawTransaction","params":[{"txHex":"'"$tx"'"}]}' ${MAIN_HTTP} | jq -r ".result.txs[0]")
ok=$(jq '(.execer == "Quit")' <<<"$data")
ok=$(jq '(.execer == "pokerbull")' <<<"$data")
[ "$ok" == true ]
rst=$?
echo_rst "$FUNCNAME" "$rst"
echo_rst "$FUNCNAME" "$?"
signrawtx "$tx" "CC38546E9E659D15E6B4893F0AB32A06D103931A8230B0BDE71459D2B27D6944" "quit"
}
pokerbull_ContinueRawTx() {
tx=$(curl -ksd '{"method":"Chain33.CreateTransaction","params":[{"execer":"pokerbull","actionName":"Continue","payload":{"gameID":"'$START_TX'"}}]}' ${MAIN_HTTP} | jq -r ".result")
tx=$(curl -ksd '{"method":"Chain33.CreateTransaction","params":[{"execer":"pokerbull","actionName":"Continue","payload":{"gameId":"'$GAME_ID'"}}]}' ${MAIN_HTTP} | jq -r ".result")
CONTINUE_TX=$tx
data=$(curl -ksd '{"method":"Chain33.DecodeRawTransaction","params":[{"txHex":"'"$tx"'"}]}' ${MAIN_HTTP} | jq -r ".result.txs[0]")
ok=$(jq '(.execer == "Continue")' <<<"$data")
ok=$(jq '(.execer == "pokerbull")' <<<"$data")
[ "$ok" == true ]
rst=$?
echo_rst "$FUNCNAME" "$rst"
echo_rst "$FUNCNAME" "$?"
signrawtx "$tx" "0x9c451df9e5cb05b88b28729aeaaeb3169a2414097401fcb4c79c1971df734588" "continue"
}
pokerbull_StartRawTx() {
tx=$(curl -ksd '{"method":"Chain33.CreateTransaction","params":[{"execer":"pokerbull","actionName":"Start","payload":{"value":"1000000000", "playerNum":"2"}}]}' ${MAIN_HTTP} | jq -r ".result")
START_TX=$tx
data=$(curl -ksd '{"method":"Chain33.DecodeRawTransaction","params":[{"txHex":"'"$tx"'"}]}' ${MAIN_HTTP} | jq -r ".result.txs[0]")
ok=$(jq '(.execer == "Start")' <<<"$data")
ok=$(jq '(.execer == "pokerbull")' <<<"$data")
[ "$ok" == true ]
rst=$?
echo_rst "$FUNCNAME" "$rst"
}
echo_rst "$FUNCNAME" "$?"
Chain33_SendRawTx() {
local req='"method":"Chain33.SignRawTx", "params":[{"data":"'"$1"'", "addr":"'"$2"'", "expire":"3600"}]'
signrawtx "$tx" "CC38546E9E659D15E6B4893F0AB32A06D103931A8230B0BDE71459D2B27D6944" "start"
}
# echo "#request: $req"
resp=$(curl -ksd "{$req}" "${MAIN_HTTP}")
pokerbull_QueryResult() {
data=$(curl -ksd '{"method":"Chain33.Query","params":[{"execer":"pokerbull","funcName":"QueryGameByID","payload":{"gameId":"'$GAME_ID'"}}]}' ${MAIN_HTTP} | jq -r ".result")
ok=$(jq '(.game.gameId == "'$GAME_ID'")' <<<"$data")
# echo "#response: $resp"
ok=$(jq '(.error|not)' <<<"$resp")
[ "$ok" == true ]
echo_rst "$FUNCNAME" "$?"
local reqSend='"method":"Chain33.SendTransaction", "params":[{"data":"'"$resp"'"}]'
data=$(curl -ksd '{"method":"Chain33.Query","params":[{"execer":"pokerbull","funcName":"QueryGameByAddr","payload":{"addr":"14KEKbYtKKQm4wMthSK9J4La4nAiidGozt"}}]}' ${MAIN_HTTP} | jq -r ".result")
[ "$data" != null ]
echo_rst "$FUNCNAME" "$?"
# echo "#request: $req"
respSend=$(curl -ksd "{$req}" "${MAIN_HTTP}")
data=$(curl -ksd '{"method":"Chain33.Query","params":[{"execer":"pokerbull","funcName":"QueryGameByStatus","payload":{"status":"3"}}]}' ${MAIN_HTTP} | jq -r ".result")
[ "$data" != null ]
echo_rst "$FUNCNAME" "$?"
}
# echo "#response: $resp"
okSend=$(jq '(.error|not)' <<<"$respSend")
[ "$okSend" == true ]
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() {
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
}
sendTx() {
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
}
Chain33_SendToAddress() {
......@@ -100,30 +179,25 @@ Chain33_SendToAddress() {
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 '(.result.hash)' <<<"$resp")
echo "hash=$hash"
# query_tx "$hash"
}
init() {
ispara=$(echo '"'"${MAIN_HTTP}"'"' | jq '.|contains("8901")')
echo "ipara=$ispara"
local relay_addr=""
if [ "$ispara" == true ]; then
pokerbull_addr=$(curl -ksd '{"method":"Chain33.ConvertExectoAddr","params":[{"execname":"user.p.para.pokerbull"}]}' ${MAIN_HTTP} | jq -r ".result")
else
chain33_ImportPrivkey "${MAIN_HTTP}" "0x9c451df9e5cb05b88b28729aeaaeb3169a2414097401fcb4c79c1971df734588" "1E5saiXVb9mW8wcWUUZjsHJPZs5GmdzuSY"
pokerbull_addr=$(curl -ksd '{"method":"Chain33.ConvertExectoAddr","params":[{"execname":"pokerbull"}]}' ${MAIN_HTTP} | jq -r ".result")
fi
echo "pokerbulladdr=$pokerbull_addr"
from="12qyocayNF7Lv6C9qW4avxs2E7U41fKSfv"
from="14KEKbYtKKQm4wMthSK9J4La4nAiidGozt"
Chain33_SendToAddress "$from" "$pokerbull_addr" 10000000000
from="1E5saiXVb9mW8wcWUUZjsHJPZs5GmdzuSY"
......@@ -133,11 +207,14 @@ init() {
function run_test() {
pokerbull_StartRawTx
Chain33_SendRawTx "$START_TX" "12qyocayNF7Lv6C9qW4avxs2E7U41fKSfv"
##pokerbull_ContinueRawTx
##pokerbull_QuitRawTx
##pokerbull_PlayRawTx
##pokerbull_QueryResult
pokerbull_ContinueRawTx
pokerbull_QuitRawTx
pokerbull_PlayRawTx
pokerbull_QueryResult
}
function main() {
......
all:
chmod +x ./build.sh
./build.sh $(OUT) $(FLAG)
\ No newline at end of file
#!/bin/sh
strpwd=$(pwd)
strcmd=${strpwd##*dapp/}
strapp=${strcmd%/cmd*}
OUT_DIR="${1}/$strapp"
#FLAG=$2
mkdir -p "${OUT_DIR}"
cp ./build/* "${OUT_DIR}"
OUT_TESTDIR="${1}/dapptest/$strapp"
mkdir -p "${OUT_TESTDIR}"
cp ./build/test-rpc.sh "${OUT_TESTDIR}"
#!/usr/bin/env bash
# shellcheck disable=SC2128
set -e
set -o pipefail
MAIN_HTTP=""
CASE_ERR=""
#color
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
}
retrieve_Backup() {
tx=$(curl -ksd '{"method":"retrieve.CreateRawRetrieveBackupTx","params":[{"backupAddr":"'$1'","defaultAddr":"'$2'","delayPeriod":$3}]}' ${MAIN_HTTP} | jq -r ".result")
data=$(curl -ksd '{"method":"Chain33.DecodeRawTransaction","params":[{"txHex":"'"$tx"'"}]}' ${MAIN_HTTP} | jq -r ".result.txs[0]")
ok=$(jq '(.execer == "retrieve")' <<<"$data")
[ "$ok" == true ]
echo_rst "$FUNCNAME" "$?"
signrawtx "$tx" "CC38546E9E659D15E6B4893F0AB32A06D103931A8230B0BDE71459D2B27D6944"
}
retrieve_Prepare() {
tx=$(curl -ksd '{"method":"retrieve.CreateRawRetrievePrepareTx","params":[{"backupAddr":"'$1'","defaultAddr":"'$2'"}]}' ${MAIN_HTTP} | jq -r ".result")
data=$(curl -ksd '{"method":"Chain33.DecodeRawTransaction","params":[{"txHex":"'"$tx"'"}]}' ${MAIN_HTTP} | jq -r ".result.txs[0]")
ok=$(jq '(.execer == "retrieve")' <<<"$data")
[ "$ok" == true ]
echo_rst "$FUNCNAME" "$?"
signrawtx "$tx" "CC38546E9E659D15E6B4893F0AB32A06D103931A8230B0BDE71459D2B27D6944"
}
retrieve_Perform() {
tx=$(curl -ksd '{"method":"retrieve.CreateRawRetrievePerformTx","params":[{"backupAddr":"'$1'","defaultAddr":"'$2'"}]}' ${MAIN_HTTP} | jq -r ".result")
data=$(curl -ksd '{"method":"Chain33.DecodeRawTransaction","params":[{"txHex":"'"$tx"'"}]}' ${MAIN_HTTP} | jq -r ".result.txs[0]")
ok=$(jq '(.execer == "retrieve")' <<<"$data")
[ "$ok" == true ]
echo_rst "$FUNCNAME" "$?"
signrawtx "$tx" "CC38546E9E659D15E6B4893F0AB32A06D103931A8230B0BDE71459D2B27D6944"
}
retrieve_Cancel() {
tx=$(curl -ksd '{"method":"retrieve.CreateRawRetrieveCancelTx","params":[{"backupAddr":"'$1'","defaultAddr":"'$2'"}]}' ${MAIN_HTTP} | jq -r ".result")
data=$(curl -ksd '{"method":"Chain33.DecodeRawTransaction","params":[{"txHex":"'"$tx"'"}]}' ${MAIN_HTTP} | jq -r ".result.txs[0]")
ok=$(jq '(.execer == "retrieve")' <<<"$data")
[ "$ok" == true ]
echo_rst "$FUNCNAME" "$?"
signrawtx "$tx" "CC38546E9E659D15E6B4893F0AB32A06D103931A8230B0BDE71459D2B27D6944"
}
retrieve_QueryResult() {
# data=$(curl -ksd '{"method":"Chain33.Query","params":[{"execer":"pokerbull","funcName":"QueryGameByID","payload":{"gameId":"'$GAME_ID'"}}]}' ${MAIN_HTTP} | jq -r ".result")
# ok=$(jq '(.game.gameId == "'$GAME_ID'")' <<<"$data")
#
# [ "$ok" == true ]
# echo_rst "$FUNCNAME" "$?"
#
# data=$(curl -ksd '{"method":"Chain33.Query","params":[{"execer":"pokerbull","funcName":"QueryGameByAddr","payload":{"addr":"14KEKbYtKKQm4wMthSK9J4La4nAiidGozt"}}]}' ${MAIN_HTTP} | jq -r ".result")
# [ "$data" != null ]
# echo_rst "$FUNCNAME" "$?"
#
# data=$(curl -ksd '{"method":"Chain33.Query","params":[{"execer":"pokerbull","funcName":"QueryGameByStatus","payload":{"status":"3"}}]}' ${MAIN_HTTP} | jq -r ".result")
# [ "$data" != null ]
echo_rst "$FUNCNAME" "$?"
}
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() {
txHex="$1"
priKey="$2"
type="$3"
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 '(.result.hash)' <<<"$resp")
echo "hash=$hash"
# query_tx "$hash"
}
init() {
ispara=$(echo '"'"${MAIN_HTTP}"'"' | jq '.|contains("8901")')
echo "ipara=$ispara"
if [ "$ispara" == true ]; then
retrieve_addr=$(curl -ksd '{"method":"Chain33.ConvertExectoAddr","params":[{"execname":"user.p.para.retrieve"}]}' ${MAIN_HTTP} | jq -r ".result")
else
#chain33_ImportPrivkey "${MAIN_HTTP}" "0x9c451df9e5cb05b88b28729aeaaeb3169a2414097401fcb4c79c1971df734588" "1E5saiXVb9mW8wcWUUZjsHJPZs5GmdzuSY"
retrieve_addr=$(curl -ksd '{"method":"Chain33.ConvertExectoAddr","params":[{"execname":"retrieve"}]}' ${MAIN_HTTP} | jq -r ".result")
fi
echo "retrieveaddr=$retrieve_addr"
from="14KEKbYtKKQm4wMthSK9J4La4nAiidGozt"
Chain33_SendToAddress "$from" "$retrieve_addr" 10000000000
from="1E5saiXVb9mW8wcWUUZjsHJPZs5GmdzuSY"
Chain33_SendToAddress "$from" "$retrieve_addr" 10000000000
block_wait 1
}
function run_test() {
retrieve_Backup
retrieve_Prepare
retrieve_Perform
retrieve_Cancel
retrieve_QueryResult
}
function main() {
MAIN_HTTP="$1"
echo "=========== # retrieve rpc test ============="
echo "ip=$MAIN_HTTP"
init
run_test
if [ -n "$CASE_ERR" ]; then
echo -e "${RED}=============retrieve Rpc Test Fail=============${NOC}"
exit 1
else
echo -e "${GRE}=============retrieve Rpc Test Pass==============${NOC}"
fi
}
main "$1"
......@@ -2,7 +2,7 @@
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package commands
package cmd
import (
"fmt"
......
......@@ -6,7 +6,7 @@ package retrieve
import (
"github.com/33cn/chain33/pluginmgr"
"github.com/33cn/plugin/plugin/dapp/retrieve/commands"
"github.com/33cn/plugin/plugin/dapp/retrieve/cmd"
"github.com/33cn/plugin/plugin/dapp/retrieve/executor"
"github.com/33cn/plugin/plugin/dapp/retrieve/rpc"
"github.com/33cn/plugin/plugin/dapp/retrieve/types"
......@@ -17,7 +17,7 @@ func init() {
Name: types.RetrieveX,
ExecName: executor.GetName(),
Exec: executor.Init,
Cmd: commands.RetrieveCmd,
Cmd: cmd.RetrieveCmd,
RPC: rpc.Init,
})
}
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