Commit 52f1e829 authored by mdj33's avatar mdj33

relay multi coins support

parent 98d25c0a
...@@ -4,14 +4,14 @@ strpwd=$(pwd) ...@@ -4,14 +4,14 @@ strpwd=$(pwd)
strcmd=${strpwd##*dapp/} strcmd=${strpwd##*dapp/}
strapp=${strcmd%/cmd*} strapp=${strcmd%/cmd*}
#OUT_DIR="${1}/$strapp" OUT_DIR="${1}/$strapp"
#SRC_RELAYD=github.com/33cn/plugin/plugin/dapp/relay/cmd/relayd SRC_RELAYD=github.com/33cn/plugin/plugin/dapp/relay/cmd/relayd
#FLAG=$2 FLAG=$2
#
## shellcheck disable=SC2086,1072 # shellcheck disable=SC2086,1072
#go build -i ${FLAG} -v -o "${OUT_DIR}/relayd" "${SRC_RELAYD}" go build -i ${FLAG} -v -o "${OUT_DIR}/relayd" "${SRC_RELAYD}"
#cp ./relayd/relayd.toml "${OUT_DIR}/relayd.toml" cp ./relayd/relayd.toml "${OUT_DIR}/relayd.toml"
#cp ./build/* "${OUT_DIR}" cp ./build/* "${OUT_DIR}"
OUT_TESTDIR="${1}/dapptest/$strapp" OUT_TESTDIR="${1}/dapptest/$strapp"
mkdir -p "${OUT_TESTDIR}" mkdir -p "${OUT_TESTDIR}"
......
...@@ -93,8 +93,28 @@ function wait_btc_height() { ...@@ -93,8 +93,28 @@ function wait_btc_height() {
} }
function relay_test() { function relay_test() {
local sell_addr="1G5Cjy8LuQex2fuYv3gzb7B8MxAnxLEqt3"
local sell_priv="22968d29c6de695381a8719ef7bf00e2edb6cce500bb59a4fc73c41887610962"
local acct_addr="1EZKahMRfoMiKp1BewjWrQWoaJ9kmC4hum"
local acct_priv="ec9162ea5fc2f473ab8240619a0a0f495ba9e9e5d4d9c434b8794a68280236c4"
local buy_addr="1BafoGyuC3X6Sx5EhcVuHHfDgMNyjQGc5x"
local buy_priv="0xd04015639faa5bf740db756d8934003c4134865320e7eae65775be6cf30ff56f"
echo "================relayd test========================" echo "================relayd test========================"
echo "=========== # transfer to acct ============="
hash=$(${1} send coins transfer -a 1200 -t "$sell_addr" -n "transfer to sell" -k 12qyocayNF7Lv6C9qW4avxs2E7U41fKSfv)
echo "${hash}"
hash=$(${1} send coins transfer -a 500 -t "$acct_addr" -n "transfer to sell" -k 12qyocayNF7Lv6C9qW4avxs2E7U41fKSfv)
echo "${hash}"
hash=$(${1} send coins transfer -a 200 -t "$buy_addr" -n "transfer to buy" -k 12qyocayNF7Lv6C9qW4avxs2E7U41fKSfv)
echo "${hash}"
block_wait "${1}" 2 block_wait "${1}" 2
acct1=$(${1} account balance -a "$sell_addr" | jq -r ".balance")
acct2=$(${1} account balance -a "$acct_addr" | jq -r ".balance")
if [ "${acct1}" == "0.0000" ] || [ "${acct2}" == "0.0000" ]; then
echo "wrong relay addr balance, should not be zero"
exit 1
fi
times=100 times=100
while true; do while true; do
...@@ -122,149 +142,90 @@ function relay_test() { ...@@ -122,149 +142,90 @@ function relay_test() {
btcrcv_addr=$(${BTC_CTL} --rpcuser=root --rpcpass=1314 --simnet --wallet getaccountaddress "${newacct}") btcrcv_addr=$(${BTC_CTL} --rpcuser=root --rpcpass=1314 --simnet --wallet getaccountaddress "${newacct}")
echo "btcrcvaddr=${btcrcv_addr}" echo "btcrcvaddr=${btcrcv_addr}"
echo "=========== # get real BTY buy account ============="
real_buy_addr=$(${1} account list | jq -r '.wallets[] | select(.label=="node award") | .acc.addr')
echo "realbuyaddr=${real_buy_addr}"
echo "=========== # transfer to relay =============" echo "=========== # transfer to relay ============="
hash=$(${1} send coins transfer -a 1000 -t 1rhRgzbz264eyJu7Ac63wepsm9TsEpwXM -n "transfer to relay" -k 12qyocayNF7Lv6C9qW4avxs2E7U41fKSfv) hash=$(${1} send coins transfer -a 1000 -t 1rhRgzbz264eyJu7Ac63wepsm9TsEpwXM -n "transfer to relay" -k "$sell_priv")
echo "${hash}" echo "${hash}"
hash=$(${1} send coins transfer -a 1000 -t 14KEKbYtKKQm4wMthSK9J4La4nAiidGozt -n "transfer to accept addr" -k 12qyocayNF7Lv6C9qW4avxs2E7U41fKSfv) hash=$(${1} send coins transfer -a 300 -t 1rhRgzbz264eyJu7Ac63wepsm9TsEpwXM -n "send to relay" -k "$acct_priv")
echo "${hash}" echo "${hash}"
hash=$(${1} send coins transfer -a 200 -t "${real_buy_addr}" -n "transfer to accept addr" -k 12qyocayNF7Lv6C9qW4avxs2E7U41fKSfv) hash=$(${1} send coins transfer -a 100 -t 1rhRgzbz264eyJu7Ac63wepsm9TsEpwXM -n "send to relay" -k "${buy_priv}")
echo "${hash}" echo "${hash}"
block_wait "${1}" 1 block_wait "${1}" 1
before=$(${1} account balance -a 12qyocayNF7Lv6C9qW4avxs2E7U41fKSfv -e relay | jq -r ".balance") before=$(${1} account balance -a "$sell_addr" -e relay | jq -r ".balance")
if [ "${before}" == "0.0000" ]; then if [ "${before}" == "0.0000" ]; then
echo "wrong relay addr balance, should not be zero" echo "wrong relay addr balance, should not be zero"
exit 1 exit 1
fi fi
before=$(${1} account balance -a 14KEKbYtKKQm4wMthSK9J4La4nAiidGozt -e coins | jq -r ".balance")
if [ "${before}" == "0.0000" ]; then
echo "wrong accept addr balance, should not be zero"
exit 1
fi
before=$(${1} account balance -a "${real_buy_addr}" -e coins | jq -r ".balance")
if [ "${before}" == "0.0000" ]; then
echo "wrong real accept addr balance, should not be zero"
exit 1
fi
echo "=========== # create buy order =============" echo "=========== # create buy order ============="
buy_hash=$(${1} send relay create -m 2.99 -o 0 -c BTC -a 1Am9UTGfdnxabvcywYG2hvzr6qK8T3oUZT -b 200 -k 12qyocayNF7Lv6C9qW4avxs2E7U41fKSfv) buy_hash=$(${1} send relay create -m 2.99 -o 0 -c BTC -a 1Am9UTGfdnxabvcywYG2hvzr6qK8T3oUZT -b 200 -k "$sell_priv")
echo "${buy_hash}" echo "${buy_hash}"
echo "=========== # create sell order =============" echo "=========== # create sell order ============="
sell_hash=$(${1} send relay create -m 2.99 -o 1 -c BTC -a 2Am9UTGfdnxabvcywYG2hvzr6qK8T3oUZT -b 200 -k 12qyocayNF7Lv6C9qW4avxs2E7U41fKSfv) sell_hash=$(${1} send relay create -m 2.99 -o 1 -c BTC -a 2Am9UTGfdnxabvcywYG2hvzr6qK8T3oUZT -b 200 -k "$sell_priv")
echo "${sell_hash}" echo "${sell_hash}"
echo "=========== # create real buy order =============" echo "=========== # create real buy order ============="
realbuy_hash=$(${1} send relay create -m 10 -o 0 -c BTC -a "${btcrcv_addr}" -b 200 -k 12qyocayNF7Lv6C9qW4avxs2E7U41fKSfv) realbuy_hash=$(${1} send relay create -m 10 -o 0 -c BTC -a "${btcrcv_addr}" -b 200 -k "$sell_priv")
echo "${realbuy_hash}" echo "${realbuy_hash}"
echo "=========== # transfer to relay ============="
hash=$(${1} send coins transfer -a 300 -t 1rhRgzbz264eyJu7Ac63wepsm9TsEpwXM -n "send to relay" -k 14KEKbYtKKQm4wMthSK9J4La4nAiidGozt)
echo "${hash}"
hash=$(${1} send coins transfer -a 100 -t 1rhRgzbz264eyJu7Ac63wepsm9TsEpwXM -n "send to relay" -k "${real_buy_addr}")
echo "${hash}"
block_wait "${1}" 1 block_wait "${1}" 1
coinaddr=$(${1} tx query -s "${buy_hash}" | jq -r ".receipt.logs[2].log.coinAddr") # coinaddr=$(${1} tx query -s "${buy_hash}" | jq -r ".receipt.logs[2].log.xAddr")
if [ "${coinaddr}" != "1Am9UTGfdnxabvcywYG2hvzr6qK8T3oUZT" ]; then # if [ "${coinaddr}" != "1Am9UTGfdnxabvcywYG2hvzr6qK8T3oUZT" ]; then
${1} tx query -s "${buy_hash}" # ${1} tx query -s "${buy_hash}"
echo "wrong create order to coinaddr" # echo "wrong create order to coinaddr"
exit 1 # exit 1
fi # fi
buy_id=$(${1} tx query -s "${buy_hash}" | jq -r ".receipt.logs[2].log.orderId") # buy_id=$(${1} tx query -s "${buy_hash}" | jq -r ".receipt.logs[2].log.orderId")
if [ -z "${buy_id}" ]; then # if [ -z "${buy_id}" ]; then
echo "wrong buy id" # echo "wrong buy id"
exit 1 # exit 1
fi # fi
oper=$(${1} tx query -s "${buy_hash}" | jq -r ".receipt.logs[2].log.coinOperation")
if [ "${oper}" != "buy" ]; then
echo "wrong buy operation"
exit 1
fi
status=$(${1} tx query -s "${sell_hash}" | jq -r ".receipt.logs[2].log.curStatus") status=$(${1} tx query -s "${sell_hash}" | jq -r ".receipt.logs[2].log.curStatus")
if [ "${status}" != "pending" ]; then if [ "${status}" != "pending" ]; then
echo "wrong create sell order status" echo "wrong create sell order status"
exit 1 exit 1
fi fi
sell_id=$(${1} tx query -s "${sell_hash}" | jq -r ".receipt.logs[2].log.orderId")
if [ -z "${sell_id}" ]; then ${1} relay status -s 1
echo "wrong sell id" num=$(${1} relay status -s 1 | jq -sr '.|length')
exit 1 if [ "${num}" != 3 ]; then
fi echo "wrong create orders num"
oper=$(${1} tx query -s "${sell_hash}" | jq -r ".receipt.logs[2].log.coinOperation")
if [ "${oper}" != "sell" ]; then
echo "wrong sell operation"
exit 1
fi
realbuy_id=$(${1} tx query -s "${realbuy_hash}" | jq -r ".receipt.logs[2].log.orderId")
if [ -z "${realbuy_id}" ]; then
echo "wrong realbuy_id "
exit 1
fi
before=$(${1} account balance -a 14KEKbYtKKQm4wMthSK9J4La4nAiidGozt -e relay | jq -r ".balance")
if [ "${before}" == "0.0000" ]; then
echo "wrong relay balance, should not be zero"
exit 1
fi
before=$(${1} account balance -a "${real_buy_addr}" -e relay | jq -r ".balance")
if [ "${before}" != "100.0000" ]; then
echo "wrong relay real buy balance, should be 100"
exit 1 exit 1
fi fi
id=$(${1} relay status -s 1 | jq -sr '.[] | select(.coinoperation=="buy")| select(.coinaddr=="1Am9UTGfdnxabvcywYG2hvzr6qK8T3oUZT") |.orderid') id=$(${1} relay status -s 1 | jq -sr '.[] | select(.coinoperation==0)| select(.coinaddr=="1Am9UTGfdnxabvcywYG2hvzr6qK8T3oUZT") |.orderid')
if [ "${id}" != "${buy_id}" ]; then if [ "${id}" != "${buy_hash}" ]; then
echo "wrong relay status buy order id" echo "wrong relay status buy order id"
exit 1 exit 1
fi fi
id=$(${1} relay status -s 1 | jq -sr '.[] | select(.coinoperation=="buy")| select(.coinamount=="10.0000") |.orderid') id=$(${1} relay status -s 1 | jq -sr '.[] | select(.coinoperation==0)| select(.coinamount=="10.0000") |.orderid')
if [ "${id}" != "${realbuy_id}" ]; then if [ "${id}" != "${realbuy_hash}" ]; then
echo "wrong relay status real buy order id" echo "wrong relay status real buy order id"
exit 1 exit 1
fi fi
id=$(${1} relay status -s 1 | jq -sr '.[] | select(.coinoperation=="sell")|.orderid') id=$(${1} relay status -s 1 | jq -sr '.[] | select(.coinoperation==1)|.orderid')
if [ "${id}" != "${sell_id}" ]; then if [ "${id}" != "${sell_hash}" ]; then
echo "wrong relay status sell order id" echo "wrong relay status sell order id"
exit 1 exit 1
fi fi
echo "=========== # accept buy order =============" echo "=========== # accept buy order ============="
buy_hash=$(${1} send relay accept -o "${buy_id}" -a 1Am9UTGfdnxabvcywYG2hvzr6qK8T3oUZT -k 14KEKbYtKKQm4wMthSK9J4La4nAiidGozt) acct_buy_hash=$(${1} send relay accept -o "${buy_hash}" -a 1Am9UTGfdnxabvcywYG2hvzr6qK8T3oUZT -k "$acct_priv")
echo "${buy_hash}" echo "${acct_buy_hash}"
echo "=========== # accept real buy order =============" echo "=========== # accept real buy order ============="
realbuy_hash=$(${1} send relay accept -o "${realbuy_id}" -a 1Am9UTGfdnxabvcywYG2hvzr6qK8T3oUZT -k "${real_buy_addr}") acct_realbuy_hash=$(${1} send relay accept -o "${realbuy_hash}" -a 1Am9UTGfdnxabvcywYG2hvzr6qK8T3oUZT -k "${buy_priv}")
echo "${realbuy_hash}" echo "${acct_realbuy_hash}"
echo "=========== # accept sell order =============" echo "=========== # accept sell order ============="
sell_hash=$(${1} send relay accept -o "${sell_id}" -a 1Am9UTGfdnxabvcywYG2hvzr6qK8T3oUZT -k 14KEKbYtKKQm4wMthSK9J4La4nAiidGozt) acct_sell_hash=$(${1} send relay accept -o "${sell_hash}" -a 1Am9UTGfdnxabvcywYG2hvzr6qK8T3oUZT -k "$acct_priv")
echo "${sell_hash}" echo "${acct_sell_hash}"
block_wait "${1}" 1 block_wait "${1}" 1
frozen=$(${1} tx query -s "${buy_hash}" | jq -r ".receipt.logs[1].log.current.frozen") ${1} relay status -s 2
if [ "${frozen}" != "10000000000" ]; then num=$(${1} relay status -s 2 | jq -sr '.|length')
echo "wrong buy frozen account, should be 100" if [ "${num}" != 3 ]; then
${1} tx query -s "${buy_hash}" echo "wrong accept orders num"
exit 1
fi
id=$(${1} relay status -s 2 | jq -sr '.[] | select(.coinoperation=="buy") | select(.coinaddr=="1Am9UTGfdnxabvcywYG2hvzr6qK8T3oUZT") |.orderid')
if [ "${id}" != "${buy_id}" ]; then
echo "wrong relay status buy order id"
exit 1
fi
id=$(${1} relay status -s 2 | jq -sr '.[] | select(.coinoperation=="buy")| select(.coinamount=="10.0000")|.orderid')
if [ "${id}" != "${realbuy_id}" ]; then
echo "wrong relay status real buy order id"
exit 1
fi
id=$(${1} relay status -s 2 | jq -sr '.[] | select(.coinoperation=="sell")|.orderid')
if [ "${id}" != "${sell_id}" ]; then
echo "wrong relay status sell order id"
exit 1 exit 1
fi fi
...@@ -286,7 +247,7 @@ function relay_test() { ...@@ -286,7 +247,7 @@ function relay_test() {
wait_btc_height "${1}" $((current + 80 + 4)) wait_btc_height "${1}" $((current + 80 + 4))
echo "=========== # unlock buy order ===========" echo "=========== # unlock buy order ==========="
acceptHeight=$(${1} tx query -s "${buy_hash}" | jq -r ".receipt.logs[2].log.coinHeight") acceptHeight=$(${1} tx query -s "${acct_buy_hash}" | jq -r ".receipt.logs[2].log.xHeight")
if [ "${acceptHeight}" -lt "${btc_cur_height}" ]; then if [ "${acceptHeight}" -lt "${btc_cur_height}" ]; then
echo "accept height less previous height" echo "accept height less previous height"
exit 1 exit 1
...@@ -294,32 +255,32 @@ function relay_test() { ...@@ -294,32 +255,32 @@ function relay_test() {
wait_btc_height "${1}" $((acceptHeight + 72)) wait_btc_height "${1}" $((acceptHeight + 72))
revoke_hash=$(${1} send relay revoke -a 0 -t 1 -i "${buy_id}" -k 14KEKbYtKKQm4wMthSK9J4La4nAiidGozt) revoke_hash=$(${1} send relay revoke -a 0 -t 1 -i "${buy_hash}" -k "$acct_priv")
echo "${revoke_hash}" echo "${revoke_hash}"
echo "=========== # confirm real buy order =============" echo "=========== # confirm real buy order ============="
confirm_hash=$(${1} send relay confirm -t "${btc_tx_hash}" -o "${realbuy_id}" -k "${real_buy_addr}") confirm_hash=$(${1} send relay confirm -t "${btc_tx_hash}" -o "${realbuy_hash}" -k "${buy_priv}")
echo "${confirm_hash}" echo "${confirm_hash}"
echo "=========== # confirm sell order =============" echo "=========== # confirm sell order ============="
confirm_hash=$(${1} send relay confirm -t 6359f0868171b1d194cbee1af2f16ea598ae8fad666d9b012c8ed2b79a236ec4 -o "${sell_id}" -k 12qyocayNF7Lv6C9qW4avxs2E7U41fKSfv) confirm_hash=$(${1} send relay confirm -t 6359f0868171b1d194cbee1af2f16ea598ae8fad666d9b012c8ed2b79a236ec4 -o "${sell_hash}" -k "$sell_priv")
echo "${confirm_hash}" echo "${confirm_hash}"
block_wait "${1}" 1 block_wait "${1}" 1
echo "${revoke_hash}" echo "${revoke_hash}"
${1} tx query -s "${revoke_hash}" ${1} tx query -s "${revoke_hash}"
id=$(${1} relay status -s 1 | jq -sr '.[] | select(.coinoperation=="buy")|.orderid') id=$(${1} relay status -s 1 | jq -sr '.[] | select(.coinoperation==0)|.orderid')
if [ "${id}" != "${buy_id}" ]; then if [ "${id}" != "${buy_hash}" ]; then
echo "wrong relay pending status unlock buy order id" echo "wrong relay pending status unlock buy order id"
exit 1 exit 1
fi fi
id=$(${1} relay status -s 3 | jq -sr '.[] | select(.coinoperation=="buy")|.orderid') id=$(${1} relay status -s 3 | jq -sr '.[] | select(.coinoperation==0)|.orderid')
if [ "${id}" != "${realbuy_id}" ]; then if [ "${id}" != "${realbuy_hash}" ]; then
echo "wrong relay status confirming real buy order id" echo "wrong relay status confirming real buy order id"
exit 1 exit 1
fi fi
id=$(${1} relay status -s 3 | jq -sr '.[] | select(.coinoperation=="sell")|.orderid') id=$(${1} relay status -s 3 | jq -sr '.[] | select(.coinoperation==1)|.orderid')
if [ "${id}" != "${sell_id}" ]; then if [ "${id}" != "${sell_hash}" ]; then
echo "wrong relay status confirming sell order id" echo "wrong relay status confirming sell order id"
exit 1 exit 1
fi fi
...@@ -330,7 +291,7 @@ function relay_test() { ...@@ -330,7 +291,7 @@ function relay_test() {
wait_btc_height "${1}" $((current + 300)) wait_btc_height "${1}" $((current + 300))
echo "=========== # unlock sell order ===" echo "=========== # unlock sell order ==="
confirmHeight=$(${1} tx query -s "${confirm_hash}" | jq -r ".receipt.logs[1].log.coinHeight") confirmHeight=$(${1} tx query -s "${confirm_hash}" | jq -r ".receipt.logs[1].log.xHeight")
if [ "${confirmHeight}" -lt "${btc_cur_height}" ]; then if [ "${confirmHeight}" -lt "${btc_cur_height}" ]; then
echo "wrong confirm height" echo "wrong confirm height"
exit 1 exit 1
...@@ -338,23 +299,21 @@ function relay_test() { ...@@ -338,23 +299,21 @@ function relay_test() {
wait_btc_height "${1}" $((confirmHeight + 288)) wait_btc_height "${1}" $((confirmHeight + 288))
revoke_hash=$(${1} send relay revoke -a 0 -t 0 -i "${sell_id}" -k 12qyocayNF7Lv6C9qW4avxs2E7U41fKSfv) revoke_hash=$(${1} send relay revoke -a 0 -t 0 -i "${sell_hash}" -k "$sell_priv")
echo "${revoke_hash}" echo "${revoke_hash}"
echo "=========== # test cancel create order ===" echo "=========== # test cancel create order ==="
cancel_hash=$(${1} send relay create -m 2.99 -o 0 -c BTC -a 1Am9UTGfdnxabvcywYG2hvzr6qK8T3oUZT -b 200 -k 14KEKbYtKKQm4wMthSK9J4La4nAiidGozt) ${1} account balance -a "$acct_addr"
cancel_hash=$(${1} send relay create -m 2.99 -o 0 -c BTC -a 1Am9UTGfdnxabvcywYG2hvzr6qK8T3oUZT -b 20 -k "$acct_priv")
echo "${cancel_hash}" echo "${cancel_hash}"
block_wait "${1}" 1 block_wait "${1}" 1
echo "${revoke_hash}" echo "${revoke_hash}"
${1} tx query -s "${revoke_hash}" ${1} tx query -s "${revoke_hash}"
echo "${cancel_hash}"
${1} tx query -s "${cancel_hash}"
cancel_id=$(${1} tx query -s "${cancel_hash}" | jq -r ".receipt.logs[2].log.orderId") id=$(${1} relay status -s 1 | jq -sr '.[] | select(.coinoperation==1)| select(.address=="'"$sell_addr"'") | .orderid')
if [ -z "${cancel_id}" ]; then if [ "${id}" != "${sell_hash}" ]; then
echo "wrong buy id"
exit 1
fi
id=$(${1} relay status -s 1 | jq -sr '.[] | select(.coinoperation=="sell")| select(.address=="12qyocayNF7Lv6C9qW4avxs2E7U41fKSfv") | .orderid')
if [ "${id}" != "${sell_id}" ]; then
echo "wrong relay revoke order id " echo "wrong relay revoke order id "
exit 1 exit 1
fi fi
...@@ -367,8 +326,8 @@ function relay_test() { ...@@ -367,8 +326,8 @@ function relay_test() {
echo "=========== # check finish order =============" echo "=========== # check finish order ============="
local count=30 local count=30
while true; do while true; do
id=$(${1} relay status -s 4 | jq -sr '.[] | select(.coinoperation=="buy")|.orderid') id=$(${1} relay status -s 4 | jq -sr '.[] | select(.coinoperation==0)|.orderid')
if [ "${id}" == "${realbuy_id}" ]; then if [ "${id}" == "${realbuy_hash}" ]; then
break break
fi fi
block_wait "${1}" 1 block_wait "${1}" 1
...@@ -379,17 +338,16 @@ function relay_test() { ...@@ -379,17 +338,16 @@ function relay_test() {
fi fi
done done
before=$(${1} account balance -a "${real_buy_addr}" -e relay | jq -r ".balance") before=$(${1} account balance -a "${buy_addr}" -e relay | jq -r ".balance")
if [ "${before}" != "300.0000" ]; then if [ "${before}" != "300.0000" ]; then
echo "wrong relay real buy addr balance, should be 300" echo "wrong relay real buy addr balance, should be 300"
exit 1 exit 1
fi fi
echo "=========== # cancel order =============" echo "=========== # cancel order ============="
hash=$(${1} send relay revoke -a 1 -t 0 -i "${cancel_id}" -k 14KEKbYtKKQm4wMthSK9J4La4nAiidGozt) hash=$(${1} send relay revoke -a 1 -t 0 -i "${cancel_hash}" -k "$acct_priv")
echo "${hash}" echo "${hash}"
block_wait "${1}" 1 block_wait "${1}" 1
echo "${hash}"
${1} tx query -s "${hash}" ${1} tx query -s "${hash}"
status=$(${1} relay status -s 5 | jq -r ".status") status=$(${1} relay status -s 5 | jq -r ".status")
...@@ -397,8 +355,8 @@ function relay_test() { ...@@ -397,8 +355,8 @@ function relay_test() {
echo "wrong relay order pending status" echo "wrong relay order pending status"
exit 1 exit 1
fi fi
id=$(${1} relay status -s 5 | jq -sr '.[] | select(.coinoperation=="buy")|.orderid') id=$(${1} relay status -s 5 | jq -sr '.[] | select(.coinoperation==0)|.orderid')
if [ "${id}" != "${cancel_id}" ]; then if [ "${id}" != "${cancel_hash}" ]; then
echo "wrong relay status cancel order id" echo "wrong relay status cancel order id"
exit 1 exit 1
fi fi
......
...@@ -10,8 +10,8 @@ syncSetup = 100 ...@@ -10,8 +10,8 @@ syncSetup = 100
syncSetupCount = 10 syncSetupCount = 10
[auth] [auth]
# test private key # test private key 1G5Cjy8LuQex2fuYv3gzb7B8MxAnxLEqt3
privateKey = "4257D8692EF7FE13C68B65D6A52F03933DB2FA5CE8FAF210B5B8B80C721CED01" privateKey = "22968d29c6de695381a8719ef7bf00e2edb6cce500bb59a4fc73c41887610962"
publicKey = "" publicKey = ""
address = "" address = ""
......
...@@ -344,7 +344,7 @@ func (r *Relayd) dealOrder() { ...@@ -344,7 +344,7 @@ func (r *Relayd) dealOrder() {
for _, value := range result.GetOrders() { for _, value := range result.GetOrders() {
// TODO save db ??? // TODO save db ???
tx, err := r.btcClient.GetTransaction(value.CoinTxHash) tx, err := r.btcClient.GetTransaction(value.XTxHash)
if err != nil { if err != nil {
log.Error("dealOrder", "dealOrder GetTransaction error: ", err) log.Error("dealOrder", "dealOrder GetTransaction error: ", err)
continue continue
......
...@@ -302,12 +302,12 @@ func parseRelayOrders(res ty.ReplyRelayOrders) { ...@@ -302,12 +302,12 @@ func parseRelayOrders(res ty.ReplyRelayOrders) {
show.OrderID = order.Id show.OrderID = order.Id
show.Status = order.Status.String() show.Status = order.Status.String()
show.Creator = order.CreaterAddr show.Creator = order.CreaterAddr
show.CoinOperation = ty.RelayOrderOperation[order.CoinOperation] show.CoinOperation = order.Operation
show.Amount = strconv.FormatFloat(float64(order.Amount)/float64(types.Coin), 'f', 4, 64) show.Amount = strconv.FormatFloat(float64(order.LocalCoinAmount)/float64(types.Coin), 'f', 4, 64)
show.Coin = order.Coin show.Coin = order.XCoin
show.CoinAddr = order.CoinAddr show.CoinAddr = order.XAddr
show.CoinAmount = strconv.FormatFloat(float64(order.CoinAmount)/float64(types.Coin), 'f', 4, 64) show.CoinAmount = strconv.FormatFloat(float64(order.XAmount)/float64(types.Coin), 'f', 4, 64)
show.CoinWaits = order.CoinWaits show.CoinWaits = order.XBlockWaits
show.CreateTime = order.CreateTime show.CreateTime = order.CreateTime
show.AcceptAddr = order.AcceptAddr show.AcceptAddr = order.AcceptAddr
show.AcceptTime = order.AcceptTime show.AcceptTime = order.AcceptTime
...@@ -389,11 +389,11 @@ func relayOrder(cmd *cobra.Command, args []string) { ...@@ -389,11 +389,11 @@ func relayOrder(cmd *cobra.Command, args []string) {
params := &ty.RelayCreate{ params := &ty.RelayCreate{
Operation: oper, Operation: oper,
Amount: coinUInt64 * 1e4, XAmount: coinUInt64 * 1e4,
Coin: coin, XCoin: coin,
Addr: coinaddr, XAddr: coinaddr,
CoinWaits: coinwait, XBlockWaits: coinwait,
BtyAmount: btyUInt64 * 1e4, LocalCoinAmount: btyUInt64 * 1e4,
} }
var res string var res string
...@@ -435,8 +435,8 @@ func relayAccept(cmd *cobra.Command, args []string) { ...@@ -435,8 +435,8 @@ func relayAccept(cmd *cobra.Command, args []string) {
params := &ty.RelayAccept{ params := &ty.RelayAccept{
OrderId: orderID, OrderId: orderID,
CoinAddr: coinaddr, XAddr: coinaddr,
CoinWaits: coinwait, XBlockWaits: coinwait,
} }
var res string var res string
ctx := jsonclient.NewRPCCtx(rpcLaddr, "relay.CreateRawRelayAcceptTx", params, &res) ctx := jsonclient.NewRPCCtx(rpcLaddr, "relay.CreateRawRelayAcceptTx", params, &res)
......
...@@ -9,7 +9,7 @@ type relayOrder2Show struct { ...@@ -9,7 +9,7 @@ type relayOrder2Show struct {
Status string `json:"status"` Status string `json:"status"`
Creator string `json:"address"` Creator string `json:"address"`
Amount string `json:"amount"` Amount string `json:"amount"`
CoinOperation string `json:"coinoperation"` CoinOperation uint32 `json:"coinoperation"`
Coin string `json:"coin"` Coin string `json:"coin"`
CoinAmount string `json:"coinamount"` CoinAmount string `json:"coinamount"`
CoinAddr string `json:"coinaddr"` CoinAddr string `json:"coinaddr"`
...@@ -21,4 +21,6 @@ type relayOrder2Show struct { ...@@ -21,4 +21,6 @@ type relayOrder2Show struct {
FinishTime int64 `json:"finishtime"` FinishTime int64 `json:"finishtime"`
FinishTxHash string `json:"finishtxhash"` FinishTxHash string `json:"finishtxhash"`
Height int64 `json:"height"` Height int64 `json:"height"`
LocalCoinExec string `json:"localCoinExec"`
LocalCoinSym string `json:"localCoinSym"`
} }
...@@ -7,6 +7,8 @@ package executor ...@@ -7,6 +7,8 @@ package executor
import ( import (
"fmt" "fmt"
"strings"
ty "github.com/33cn/plugin/plugin/dapp/relay/types" ty "github.com/33cn/plugin/plugin/dapp/relay/types"
) )
...@@ -46,25 +48,25 @@ func calcBtcHeaderKeyHeightList(height int64) []byte { ...@@ -46,25 +48,25 @@ func calcBtcHeaderKeyHeightList(height int64) []byte {
func calcOrderKeyStatus(order *ty.RelayOrder, status int32) []byte { func calcOrderKeyStatus(order *ty.RelayOrder, status int32) []byte {
key := fmt.Sprintf(relayOrderSCAIH+"%d:%s:%s:%s:%d", key := fmt.Sprintf(relayOrderSCAIH+"%d:%s:%s:%s:%d",
status, order.Coin, order.CreaterAddr, order.Id, order.Height) status, order.XCoin, order.CreaterAddr, order.Id, order.Height)
return []byte(key) return []byte(key)
} }
func calcOrderKeyCoin(order *ty.RelayOrder, status int32) []byte { func calcOrderKeyCoin(order *ty.RelayOrder, status int32) []byte {
key := fmt.Sprintf(relayOrderCSAIH+"%s:%d:%s:%s:%d", key := fmt.Sprintf(relayOrderCSAIH+"%s:%d:%s:%s:%d",
order.Coin, status, order.CreaterAddr, order.Id, order.Height) order.XCoin, status, order.CreaterAddr, order.Id, order.Height)
return []byte(key) return []byte(key)
} }
func calcOrderKeyAddrStatus(order *ty.RelayOrder, status int32) []byte { func calcOrderKeyAddrStatus(order *ty.RelayOrder, status int32) []byte {
key := fmt.Sprintf(relayOrderASCIH+"%s:%d:%s:%s:%d", key := fmt.Sprintf(relayOrderASCIH+"%s:%d:%s:%s:%d",
order.CreaterAddr, status, order.Coin, order.Id, order.Height) order.CreaterAddr, status, order.XCoin, order.Id, order.Height)
return []byte(key) return []byte(key)
} }
func calcOrderKeyAddrCoin(order *ty.RelayOrder, status int32) []byte { func calcOrderKeyAddrCoin(order *ty.RelayOrder, status int32) []byte {
key := fmt.Sprintf(relayOrderACSIH+"%s:%s:%d:%s:%d", key := fmt.Sprintf(relayOrderACSIH+"%s:%s:%d:%s:%d",
order.CreaterAddr, order.Coin, status, order.Id, order.Height) order.CreaterAddr, order.XCoin, status, order.Id, order.Height)
return []byte(key) return []byte(key)
} }
...@@ -90,7 +92,7 @@ func calcOrderPrefixAddr(addr string) []byte { ...@@ -90,7 +92,7 @@ func calcOrderPrefixAddr(addr string) []byte {
func calcAcceptKeyAddr(order *ty.RelayOrder, status int32) []byte { func calcAcceptKeyAddr(order *ty.RelayOrder, status int32) []byte {
if order.AcceptAddr != "" { if order.AcceptAddr != "" {
return []byte(fmt.Sprintf(relayBuyOrderACSIH+"%s:%s:%d:%s:%d", return []byte(fmt.Sprintf(relayBuyOrderACSIH+"%s:%s:%d:%s:%d",
order.AcceptAddr, order.Coin, status, order.Id, order.Height)) order.AcceptAddr, order.XCoin, status, order.Id, order.Height))
} }
return nil return nil
...@@ -111,3 +113,9 @@ func calcRelayOrderID(hash string) string { ...@@ -111,3 +113,9 @@ func calcRelayOrderID(hash string) string {
func calcCoinHash(hash string) string { func calcCoinHash(hash string) string {
return coinHashPrefix + hash return coinHashPrefix + hash
} }
func getRealTxHashID(id string) string {
ids := strings.Split(id, "-")
return ids[len(ids)-1]
}
...@@ -133,6 +133,10 @@ func (r *relay) getRelayOrderReply(OrderIDs [][]byte) (types.Message, error) { ...@@ -133,6 +133,10 @@ func (r *relay) getRelayOrderReply(OrderIDs [][]byte) (types.Message, error) {
orderIDGot[string(orderID)] = true orderIDGot[string(orderID)] = true
} }
} }
//get remove mavl-xx- prefix
for _, order := range reply.Relayorders {
order.Id = getRealTxHashID(order.Id)
}
return &reply, nil return &reply, nil
} }
...@@ -142,7 +146,7 @@ func insertOrderDescending(toBeInserted *ty.RelayOrder, orders []*ty.RelayOrder) ...@@ -142,7 +146,7 @@ func insertOrderDescending(toBeInserted *ty.RelayOrder, orders []*ty.RelayOrder)
} else { } else {
index := len(orders) index := len(orders)
for i, element := range orders { for i, element := range orders {
if toBeInserted.Amount >= element.Amount { if toBeInserted.LocalCoinAmount >= element.LocalCoinAmount {
index = i index = i
break break
} }
......
...@@ -124,10 +124,10 @@ func (s *suiteRelay) testExecDelLocal(tx *types.Transaction, receipt *types.Rece ...@@ -124,10 +124,10 @@ func (s *suiteRelay) testExecDelLocal(tx *types.Transaction, receipt *types.Rece
func (s *suiteRelay) TestExec_1() { func (s *suiteRelay) TestExec_1() {
order := &ty.RelayCreate{ order := &ty.RelayCreate{
Operation: ty.RelayOrderSell, Operation: ty.RelayOrderSell,
Coin: "BTC", XCoin: "BTC",
Amount: 0.299 * 1e8, XAmount: 0.299 * 1e8,
Addr: addrBtc, XAddr: addrBtc,
BtyAmount: 200 * 1e8, LocalCoinAmount: 200 * 1e8,
} }
sell := &ty.RelayAction{ sell := &ty.RelayAction{
...@@ -156,8 +156,8 @@ func (s *suiteRelay) TestExec_1() { ...@@ -156,8 +156,8 @@ func (s *suiteRelay) TestExec_1() {
var log ty.ReceiptRelayLog var log ty.ReceiptRelayLog
types.Decode(receipt.Logs[len(receipt.Logs)-1].Log, &log) types.Decode(receipt.Logs[len(receipt.Logs)-1].Log, &log)
s.Equal("200.0000", log.TxAmount) s.Equal("200.0000", log.LocalCoinAmount)
s.Equal(uint64(10), log.CoinHeight) s.Equal(uint64(10), log.XHeight)
s.orderID = log.OrderId s.orderID = log.OrderId
...@@ -170,7 +170,7 @@ func (s *suiteRelay) TestExec_1() { ...@@ -170,7 +170,7 @@ func (s *suiteRelay) TestExec_1() {
func (s *suiteRelay) TestExec_2() { func (s *suiteRelay) TestExec_2() {
order := &ty.RelayAccept{ order := &ty.RelayAccept{
OrderId: s.orderID, OrderId: s.orderID,
CoinAddr: addrBtc, XAddr: addrBtc,
} }
sell := &ty.RelayAction{ sell := &ty.RelayAction{
...@@ -197,8 +197,8 @@ func (s *suiteRelay) TestExec_2() { ...@@ -197,8 +197,8 @@ func (s *suiteRelay) TestExec_2() {
var log ty.ReceiptRelayLog var log ty.ReceiptRelayLog
types.Decode(receipt.Logs[len(receipt.Logs)-1].Log, &log) types.Decode(receipt.Logs[len(receipt.Logs)-1].Log, &log)
s.Equal("200.0000", log.TxAmount) s.Equal("200.0000", log.LocalCoinAmount)
s.Equal(uint64(20), log.CoinHeight) s.Equal(uint64(20), log.XHeight)
s.Equal(ty.RelayOrderStatus_locking.String(), log.CurStatus) s.Equal(ty.RelayOrderStatus_locking.String(), log.CurStatus)
} }
...@@ -233,8 +233,8 @@ func (s *suiteRelay) TestExec_3() { ...@@ -233,8 +233,8 @@ func (s *suiteRelay) TestExec_3() {
var log ty.ReceiptRelayLog var log ty.ReceiptRelayLog
types.Decode(receipt.Logs[len(receipt.Logs)-1].Log, &log) types.Decode(receipt.Logs[len(receipt.Logs)-1].Log, &log)
s.Equal("200.0000", log.TxAmount) s.Equal("200.0000", log.LocalCoinAmount)
s.Equal(uint64(30), log.CoinHeight) s.Equal(uint64(30), log.XHeight)
s.Equal(ty.RelayOrderStatus_confirming.String(), log.CurStatus) s.Equal(ty.RelayOrderStatus_confirming.String(), log.CurStatus)
} }
......
...@@ -197,7 +197,7 @@ func (b *btcStore) getMerkleRootFromHeader(blockhash string) (string, error) { ...@@ -197,7 +197,7 @@ func (b *btcStore) getMerkleRootFromHeader(blockhash string) (string, error) {
func (b *btcStore) verifyBtcTx(verify *ty.RelayVerify, order *ty.RelayOrder) error { func (b *btcStore) verifyBtcTx(verify *ty.RelayVerify, order *ty.RelayOrder) error {
var foundtx bool var foundtx bool
for _, outtx := range verify.GetTx().GetVout() { for _, outtx := range verify.GetTx().GetVout() {
if outtx.Address == order.CoinAddr && outtx.Value >= order.CoinAmount { if outtx.Address == order.XAddr && outtx.Value >= order.XAmount {
foundtx = true foundtx = true
} }
} }
...@@ -220,7 +220,7 @@ func (b *btcStore) verifyBtcTx(verify *ty.RelayVerify, order *ty.RelayOrder) err ...@@ -220,7 +220,7 @@ func (b *btcStore) verifyBtcTx(verify *ty.RelayVerify, order *ty.RelayOrder) err
return err return err
} }
if verify.Tx.BlockHeight+uint64(order.CoinWaits) > uint64(height) { if verify.Tx.BlockHeight+uint64(order.XBlockWaits) > uint64(height) {
return ty.ErrRelayWaitBlocksErr return ty.ErrRelayWaitBlocksErr
} }
......
...@@ -197,8 +197,8 @@ func (s *suiteBtcStore) TestGetMerkleRootFromHeader() { ...@@ -197,8 +197,8 @@ func (s *suiteBtcStore) TestGetMerkleRootFromHeader() {
func (s *suiteBtcStore) TestVerifyBtcTx() { func (s *suiteBtcStore) TestVerifyBtcTx() {
order := &ty.RelayOrder{ order := &ty.RelayOrder{
CoinAddr: "1Am9UTGfdnxabvcywYG2hvzr6qK8T3oUZT", XAddr: "1Am9UTGfdnxabvcywYG2hvzr6qK8T3oUZT",
CoinAmount: 29900000, XAmount: 29900000,
AcceptTime: 100, AcceptTime: 100,
ConfirmTime: 200, ConfirmTime: 200,
} }
......
...@@ -16,6 +16,8 @@ import ( ...@@ -16,6 +16,8 @@ import (
"github.com/33cn/chain33/system/dapp" "github.com/33cn/chain33/system/dapp"
"github.com/33cn/chain33/types" "github.com/33cn/chain33/types"
ty "github.com/33cn/plugin/plugin/dapp/relay/types" ty "github.com/33cn/plugin/plugin/dapp/relay/types"
token "github.com/33cn/plugin/plugin/dapp/token/types"
"github.com/pkg/errors"
) )
const ( const (
...@@ -46,8 +48,8 @@ func (r *relayLog) getKVSet() (kvSet []*types.KeyValue) { ...@@ -46,8 +48,8 @@ func (r *relayLog) getKVSet() (kvSet []*types.KeyValue) {
key := []byte(r.Id) key := []byte(r.Id)
kvSet = append(kvSet, &types.KeyValue{Key: key, Value: value}) kvSet = append(kvSet, &types.KeyValue{Key: key, Value: value})
if r.CoinTxHash != "" { if r.XTxHash != "" {
key = []byte(calcCoinHash(r.CoinTxHash)) key = []byte(calcCoinHash(r.XTxHash))
kvSet = append(kvSet, &types.KeyValue{Key: key, Value: value}) kvSet = append(kvSet, &types.KeyValue{Key: key, Value: value})
} }
...@@ -62,19 +64,21 @@ func (r *relayLog) receiptLog(relayLogType int32) *types.ReceiptLog { ...@@ -62,19 +64,21 @@ func (r *relayLog) receiptLog(relayLogType int32) *types.ReceiptLog {
CurStatus: r.Status.String(), CurStatus: r.Status.String(),
PreStatus: r.PreStatus.String(), PreStatus: r.PreStatus.String(),
CreaterAddr: r.CreaterAddr, CreaterAddr: r.CreaterAddr,
TxAmount: strconv.FormatFloat(float64(r.Amount)/float64(types.Coin), 'f', 4, 64), LocalCoinAmount: strconv.FormatFloat(float64(r.LocalCoinAmount)/float64(types.Coin), 'f', 4, 64),
CoinOperation: ty.RelayOrderOperation[r.CoinOperation], CoinOperation: r.Operation,
Coin: r.Coin, XCoin: r.XCoin,
CoinAmount: strconv.FormatFloat(float64(r.CoinAmount)/float64(types.Coin), 'f', 4, 64), XAmount: strconv.FormatFloat(float64(r.XAmount)/float64(types.Coin), 'f', 4, 64),
CoinAddr: r.CoinAddr, XAddr: r.XAddr,
CoinTxHash: r.CoinTxHash, XTxHash: r.XTxHash,
CoinWaits: r.CoinWaits, XBlockWaits: r.XBlockWaits,
CreateTime: r.CreateTime, CreateTime: r.CreateTime,
AcceptAddr: r.AcceptAddr, AcceptAddr: r.AcceptAddr,
AcceptTime: r.AcceptTime, AcceptTime: r.AcceptTime,
ConfirmTime: r.ConfirmTime, ConfirmTime: r.ConfirmTime,
FinishTime: r.FinishTime, FinishTime: r.FinishTime,
CoinHeight: r.CoinHeight, XHeight: r.XHeight,
LocalCoinExec: r.LocalCoinExec,
LocalCoinSymbol: r.LocalCoinSymbol,
} }
log.Log = types.Encode(receipt) log.Log = types.Encode(receipt)
...@@ -102,8 +106,8 @@ func newRelayDB(r *relay, tx *types.Transaction) *relayDB { ...@@ -102,8 +106,8 @@ func newRelayDB(r *relay, tx *types.Transaction) *relayDB {
fromAddr, r.GetBlockTime(), r.GetHeight(), dapp.ExecAddress(string(tx.Execer)), btc, r.GetAPI()} fromAddr, r.GetBlockTime(), r.GetHeight(), dapp.ExecAddress(string(tx.Execer)), btc, r.GetAPI()}
} }
func (action *relayDB) getOrderByID(orderID []byte) (*ty.RelayOrder, error) { func (action *relayDB) getOrderByID(orderID string) (*ty.RelayOrder, error) {
value, err := action.db.Get(orderID) value, err := action.db.Get([]byte(calcRelayOrderID(orderID)))
if err != nil { if err != nil {
return nil, err return nil, err
} }
...@@ -128,22 +132,39 @@ func (action *relayDB) getOrderByCoinHash(hash []byte) (*ty.RelayOrder, error) { ...@@ -128,22 +132,39 @@ func (action *relayDB) getOrderByCoinHash(hash []byte) (*ty.RelayOrder, error) {
return &order, nil return &order, nil
} }
func (action *relayDB) createAccount(exec, symbol string) (*account.DB, error) {
var accDB *account.DB
cfg := action.api.GetConfig()
if symbol == "" {
accDB = account.NewCoinsAccount(cfg)
accDB.SetDB(action.db)
return accDB, nil
}
if exec == "" {
exec = token.TokenX
}
return account.NewAccountDB(cfg, exec, symbol, action.db)
}
func (action *relayDB) create(order *ty.RelayCreate) (*types.Receipt, error) { func (action *relayDB) create(order *ty.RelayCreate) (*types.Receipt, error) {
var receipt *types.Receipt var receipt *types.Receipt
var err error var err error
var coinAddr string
var coinWaits uint32 accDb, err := action.createAccount(order.LocalCoinExec, order.LocalCoinSymbol)
if err != nil {
return nil, errors.Wrapf(err, "relay create,exec=%s,sym=%s", order.LocalCoinExec, order.LocalCoinSymbol)
}
if order.Operation == ty.RelayOrderBuy { if order.Operation == ty.RelayOrderBuy {
receipt, err = action.coinsAccount.ExecFrozen(action.fromAddr, action.execAddr, int64(order.BtyAmount)) receipt, err = accDb.ExecFrozen(action.fromAddr, action.execAddr, int64(order.LocalCoinAmount))
if err != nil { if err != nil {
relaylog.Error("account.ExecFrozen relay ", "addrFrom", action.fromAddr, "execAddr", action.execAddr, "amount", order.BtyAmount) relaylog.Error("account.ExecFrozen relay ", "addrFrom", action.fromAddr, "execAddr", action.execAddr, "amount", order.LocalCoinAmount)
return nil, err return nil, err
} }
coinAddr = order.Addr
coinWaits = order.CoinWaits
} else { } else {
receipt, err = action.coinsAccount.ExecFrozen(action.fromAddr, action.execAddr, int64(lockBtyAmount)) receipt, err = accDb.ExecFrozen(action.fromAddr, action.execAddr, int64(lockBtyAmount))
if err != nil { if err != nil {
relaylog.Error("account.ExecFrozen relay ", "addrFrom", action.fromAddr, "execAddr", action.execAddr, "amount", lockBtyAmount) relaylog.Error("account.ExecFrozen relay ", "addrFrom", action.fromAddr, "execAddr", action.execAddr, "amount", lockBtyAmount)
return nil, err return nil, err
...@@ -156,22 +177,24 @@ func (action *relayDB) create(order *ty.RelayCreate) (*types.Receipt, error) { ...@@ -156,22 +177,24 @@ func (action *relayDB) create(order *ty.RelayCreate) (*types.Receipt, error) {
Id: calcRelayOrderID(common.ToHex(action.txHash)), Id: calcRelayOrderID(common.ToHex(action.txHash)),
Status: ty.RelayOrderStatus_pending, Status: ty.RelayOrderStatus_pending,
PreStatus: ty.RelayOrderStatus_init, PreStatus: ty.RelayOrderStatus_init,
Amount: order.BtyAmount, Operation: order.Operation,
LocalCoinAmount: order.LocalCoinAmount,
CreaterAddr: action.fromAddr, CreaterAddr: action.fromAddr,
CoinOperation: order.Operation, XCoin: order.XCoin,
Coin: order.Coin, XAmount: order.XAmount,
CoinAmount: order.Amount, XAddr: order.XAddr,
CoinAddr: coinAddr, XBlockWaits: order.XBlockWaits,
CoinWaits: coinWaits,
CreateTime: action.blockTime, CreateTime: action.blockTime,
Height: action.height, Height: action.height,
LocalCoinExec: order.LocalCoinExec,
LocalCoinSymbol: order.LocalCoinSymbol,
} }
height, err := action.btc.getLastBtcHeadHeight() height, err := action.btc.getLastBtcHeadHeight()
if err != nil { if err != nil {
return nil, err return nil, err
} }
uOrder.CoinHeight = uint64(height) uOrder.XHeight = uint64(height)
logs = append(logs, receipt.Logs...) logs = append(logs, receipt.Logs...)
kv = append(kv, receipt.KV...) kv = append(kv, receipt.KV...)
...@@ -193,8 +216,8 @@ func (action *relayDB) checkRevokeOrder(order *ty.RelayOrder) error { ...@@ -193,8 +216,8 @@ func (action *relayDB) checkRevokeOrder(order *ty.RelayOrder) error {
return err return err
} }
if nowBtcHeight > 0 && order.CoinHeight > 0 && nowBtcHeight > int64(order.CoinHeight) { if nowBtcHeight > 0 && order.XHeight > 0 && nowBtcHeight > int64(order.XHeight) {
subHeight = nowBtcHeight - int64(order.CoinHeight) subHeight = nowBtcHeight - int64(order.XHeight)
} }
if order.Status == ty.RelayOrderStatus_locking { if order.Status == ty.RelayOrderStatus_locking {
...@@ -218,8 +241,7 @@ func (action *relayDB) checkRevokeOrder(order *ty.RelayOrder) error { ...@@ -218,8 +241,7 @@ func (action *relayDB) checkRevokeOrder(order *ty.RelayOrder) error {
} }
func (action *relayDB) revokeCreate(revoke *ty.RelayRevoke) (*types.Receipt, error) { func (action *relayDB) revokeCreate(revoke *ty.RelayRevoke) (*types.Receipt, error) {
orderID := []byte(revoke.OrderId) order, err := action.getOrderByID(revoke.OrderId)
order, err := action.getOrderByID(orderID)
if err != nil { if err != nil {
return nil, ty.ErrRelayOrderNotExist return nil, ty.ErrRelayOrderNotExist
} }
...@@ -248,22 +270,27 @@ func (action *relayDB) revokeCreate(revoke *ty.RelayRevoke) (*types.Receipt, err ...@@ -248,22 +270,27 @@ func (action *relayDB) revokeCreate(revoke *ty.RelayRevoke) (*types.Receipt, err
return nil, ty.ErrRelayReturnAddr return nil, ty.ErrRelayReturnAddr
} }
accDb, err := action.createAccount(order.LocalCoinExec, order.LocalCoinSymbol)
if err != nil {
return nil, errors.Wrapf(err, "relay revokeCreate,exec=%s,sym=%s", order.LocalCoinExec, order.LocalCoinSymbol)
}
var receipt *types.Receipt var receipt *types.Receipt
var receiptTransfer *types.Receipt var receiptTransfer *types.Receipt
if order.CoinOperation == ty.RelayOrderBuy { if order.Operation == ty.RelayOrderBuy {
receipt, err = action.coinsAccount.ExecActive(order.CreaterAddr, action.execAddr, int64(order.Amount)) receipt, err = accDb.ExecActive(order.CreaterAddr, action.execAddr, int64(order.LocalCoinAmount))
if err != nil { if err != nil {
relaylog.Error("revoke create", "addrFrom", order.CreaterAddr, "execAddr", action.execAddr, "amount", order.Amount) relaylog.Error("revoke create", "addrFrom", order.CreaterAddr, "execAddr", action.execAddr, "amount", order.LocalCoinAmount)
return nil, err return nil, err
} }
} else if order.Status != ty.RelayOrderStatus_pending { } else if order.Status != ty.RelayOrderStatus_pending {
receipt, err = action.coinsAccount.ExecActive(order.AcceptAddr, action.execAddr, int64(order.Amount)) receipt, err = accDb.ExecActive(order.AcceptAddr, action.execAddr, int64(order.LocalCoinAmount))
if err != nil { if err != nil {
relaylog.Error("revoke create", "addrFrom", order.AcceptAddr, "execAddr", action.execAddr, "amount", order.Amount) relaylog.Error("revoke create", "addrFrom", order.AcceptAddr, "execAddr", action.execAddr, "amount", order.LocalCoinAmount)
return nil, err return nil, err
} }
receiptTransfer, err = action.coinsAccount.ExecTransferFrozen(order.CreaterAddr, order.AcceptAddr, action.execAddr, int64(lockBtyAmount)) receiptTransfer, err = accDb.ExecTransferFrozen(order.CreaterAddr, order.AcceptAddr, action.execAddr, int64(lockBtyAmount))
if err != nil { if err != nil {
relaylog.Error("revokeAccept", "from", order.AcceptAddr, "to", order.CreaterAddr, "execAddr", action.execAddr, "amount", lockBtyAmount) relaylog.Error("revokeAccept", "from", order.AcceptAddr, "to", order.CreaterAddr, "execAddr", action.execAddr, "amount", lockBtyAmount)
return nil, err return nil, err
...@@ -296,8 +323,7 @@ func (action *relayDB) revokeCreate(revoke *ty.RelayRevoke) (*types.Receipt, err ...@@ -296,8 +323,7 @@ func (action *relayDB) revokeCreate(revoke *ty.RelayRevoke) (*types.Receipt, err
} }
func (action *relayDB) accept(accept *ty.RelayAccept) (*types.Receipt, error) { func (action *relayDB) accept(accept *ty.RelayAccept) (*types.Receipt, error) {
orderID := []byte(accept.OrderId) order, err := action.getOrderByID(accept.OrderId)
order, err := action.getOrderByID(orderID)
if err != nil { if err != nil {
return nil, ty.ErrRelayOrderNotExist return nil, ty.ErrRelayOrderNotExist
} }
...@@ -309,27 +335,31 @@ func (action *relayDB) accept(accept *ty.RelayAccept) (*types.Receipt, error) { ...@@ -309,27 +335,31 @@ func (action *relayDB) accept(accept *ty.RelayAccept) (*types.Receipt, error) {
return nil, ty.ErrRelayOrderSoldout return nil, ty.ErrRelayOrderSoldout
} }
var receipt *types.Receipt accDb, err := action.createAccount(order.LocalCoinExec, order.LocalCoinSymbol)
if err != nil {
return nil, errors.Wrapf(err, "relay accept,exec=%s,sym=%s", order.LocalCoinExec, order.LocalCoinSymbol)
}
if order.CoinOperation == ty.RelayOrderBuy { var receipt *types.Receipt
receipt, err = action.coinsAccount.ExecFrozen(action.fromAddr, action.execAddr, int64(lockBtyAmount)) if order.Operation == ty.RelayOrderBuy {
receipt, err = accDb.ExecFrozen(action.fromAddr, action.execAddr, int64(lockBtyAmount))
if err != nil { if err != nil {
relaylog.Error("relay accept frozen fail ", "addrFrom", action.fromAddr, "execAddr", action.execAddr, "amount", lockBtyAmount) relaylog.Error("relay accept frozen fail ", "addrFrom", action.fromAddr, "execAddr", action.execAddr, "amount", lockBtyAmount)
return nil, err return nil, err
} }
} else { } else {
if accept.CoinAddr == "" { if accept.XAddr == "" {
relaylog.Error("accept, for sell operation, coinAddr needed") relaylog.Error("accept, for sell operation, coinAddr needed")
return nil, ty.ErrRelayOrderParamErr return nil, ty.ErrRelayOrderParamErr
} }
order.CoinAddr = accept.CoinAddr order.XAddr = accept.XAddr
order.CoinWaits = accept.CoinWaits order.XBlockWaits = accept.XBlockWaits
receipt, err = action.coinsAccount.ExecFrozen(action.fromAddr, action.execAddr, int64(order.Amount)) receipt, err = accDb.ExecFrozen(action.fromAddr, action.execAddr, int64(order.LocalCoinAmount))
if err != nil { if err != nil {
relaylog.Error("relay accept frozen fail", "addrFrom", action.fromAddr, "execAddr", action.execAddr, "amount", order.Amount) relaylog.Error("relay accept frozen fail", "addrFrom", action.fromAddr, "execAddr", action.execAddr, "amount", order.LocalCoinAmount)
return nil, err return nil, err
} }
} }
...@@ -343,7 +373,7 @@ func (action *relayDB) accept(accept *ty.RelayAccept) (*types.Receipt, error) { ...@@ -343,7 +373,7 @@ func (action *relayDB) accept(accept *ty.RelayAccept) (*types.Receipt, error) {
if err != nil { if err != nil {
return nil, err return nil, err
} }
order.CoinHeight = uint64(height) order.XHeight = uint64(height)
var logs []*types.ReceiptLog var logs []*types.ReceiptLog
var kv []*types.KeyValue var kv []*types.KeyValue
...@@ -370,8 +400,7 @@ func (action *relayDB) relayRevoke(revoke *ty.RelayRevoke) (*types.Receipt, erro ...@@ -370,8 +400,7 @@ func (action *relayDB) relayRevoke(revoke *ty.RelayRevoke) (*types.Receipt, erro
} }
func (action *relayDB) revokeAccept(revoke *ty.RelayRevoke) (*types.Receipt, error) { func (action *relayDB) revokeAccept(revoke *ty.RelayRevoke) (*types.Receipt, error) {
orderIDByte := []byte(revoke.OrderId) order, err := action.getOrderByID(revoke.OrderId)
order, err := action.getOrderByID(orderIDByte)
if err != nil { if err != nil {
return nil, ty.ErrRelayOrderNotExist return nil, ty.ErrRelayOrderNotExist
} }
...@@ -392,19 +421,24 @@ func (action *relayDB) revokeAccept(revoke *ty.RelayRevoke) (*types.Receipt, err ...@@ -392,19 +421,24 @@ func (action *relayDB) revokeAccept(revoke *ty.RelayRevoke) (*types.Receipt, err
return nil, ty.ErrRelayReturnAddr return nil, ty.ErrRelayReturnAddr
} }
accDb, err := action.createAccount(order.LocalCoinExec, order.LocalCoinSymbol)
if err != nil {
return nil, errors.Wrapf(err, "relay revokeAccept,exec=%s,sym=%s", order.LocalCoinExec, order.LocalCoinSymbol)
}
var receipt *types.Receipt var receipt *types.Receipt
if order.CoinOperation == ty.RelayOrderSell { if order.Operation == ty.RelayOrderSell {
receipt, err = action.coinsAccount.ExecActive(order.AcceptAddr, action.execAddr, int64(order.Amount)) receipt, err = accDb.ExecActive(order.AcceptAddr, action.execAddr, int64(order.LocalCoinAmount))
if err != nil { if err != nil {
relaylog.Error("revokeAccept", "addrFrom", order.AcceptAddr, "execAddr", action.execAddr, "amount", order.Amount) relaylog.Error("revokeAccept", "addrFrom", order.AcceptAddr, "execAddr", action.execAddr, "amount", order.LocalCoinAmount)
return nil, err return nil, err
} }
order.CoinAddr = "" order.XAddr = ""
} }
var receiptTransfer *types.Receipt var receiptTransfer *types.Receipt
if order.CoinOperation == ty.RelayOrderBuy { if order.Operation == ty.RelayOrderBuy {
receiptTransfer, err = action.coinsAccount.ExecTransferFrozen(order.AcceptAddr, order.CreaterAddr, action.execAddr, int64(lockBtyAmount)) receiptTransfer, err = accDb.ExecTransferFrozen(order.AcceptAddr, order.CreaterAddr, action.execAddr, int64(lockBtyAmount))
if err != nil { if err != nil {
relaylog.Error("revokeAccept", "from", order.AcceptAddr, "to", order.CreaterAddr, "execAddr", action.execAddr, "amount", lockBtyAmount) relaylog.Error("revokeAccept", "from", order.AcceptAddr, "to", order.CreaterAddr, "execAddr", action.execAddr, "amount", lockBtyAmount)
return nil, err return nil, err
...@@ -435,8 +469,7 @@ func (action *relayDB) revokeAccept(revoke *ty.RelayRevoke) (*types.Receipt, err ...@@ -435,8 +469,7 @@ func (action *relayDB) revokeAccept(revoke *ty.RelayRevoke) (*types.Receipt, err
} }
func (action *relayDB) confirmTx(confirm *ty.RelayConfirmTx) (*types.Receipt, error) { func (action *relayDB) confirmTx(confirm *ty.RelayConfirmTx) (*types.Receipt, error) {
orderID := []byte(confirm.OrderId) order, err := action.getOrderByID(confirm.OrderId)
order, err := action.getOrderByID(orderID)
if err != nil { if err != nil {
return nil, ty.ErrRelayOrderNotExist return nil, ty.ErrRelayOrderNotExist
} }
...@@ -461,7 +494,7 @@ func (action *relayDB) confirmTx(confirm *ty.RelayConfirmTx) (*types.Receipt, er ...@@ -461,7 +494,7 @@ func (action *relayDB) confirmTx(confirm *ty.RelayConfirmTx) (*types.Receipt, er
} }
var confirmAddr string var confirmAddr string
if order.CoinOperation == ty.RelayOrderBuy { if order.Operation == ty.RelayOrderBuy {
confirmAddr = order.AcceptAddr confirmAddr = order.AcceptAddr
} else { } else {
confirmAddr = order.CreaterAddr confirmAddr = order.CreaterAddr
...@@ -473,13 +506,13 @@ func (action *relayDB) confirmTx(confirm *ty.RelayConfirmTx) (*types.Receipt, er ...@@ -473,13 +506,13 @@ func (action *relayDB) confirmTx(confirm *ty.RelayConfirmTx) (*types.Receipt, er
order.PreStatus = order.Status order.PreStatus = order.Status
order.Status = ty.RelayOrderStatus_confirming order.Status = ty.RelayOrderStatus_confirming
order.ConfirmTime = action.blockTime order.ConfirmTime = action.blockTime
order.CoinTxHash = confirm.TxHash order.XTxHash = confirm.TxHash
height, err := action.btc.getLastBtcHeadHeight() height, err := action.btc.getLastBtcHeadHeight()
if err != nil { if err != nil {
relaylog.Error("confirmTx Get Last BTC", "orderid", confirm.OrderId) relaylog.Error("confirmTx Get Last BTC", "orderid", confirm.OrderId)
return nil, err return nil, err
} }
order.CoinHeight = uint64(height) order.XHeight = uint64(height)
var logs []*types.ReceiptLog var logs []*types.ReceiptLog
var kv []*types.KeyValue var kv []*types.KeyValue
...@@ -495,8 +528,7 @@ func (action *relayDB) confirmTx(confirm *ty.RelayConfirmTx) (*types.Receipt, er ...@@ -495,8 +528,7 @@ func (action *relayDB) confirmTx(confirm *ty.RelayConfirmTx) (*types.Receipt, er
} }
func (action *relayDB) verifyTx(verify *ty.RelayVerify) (*types.Receipt, error) { func (action *relayDB) verifyTx(verify *ty.RelayVerify) (*types.Receipt, error) {
orderID := []byte(verify.OrderId) order, err := action.getOrderByID(verify.OrderId)
order, err := action.getOrderByID(orderID)
if err != nil { if err != nil {
return nil, ty.ErrRelayOrderNotExist return nil, ty.ErrRelayOrderNotExist
} }
...@@ -516,16 +548,21 @@ func (action *relayDB) verifyTx(verify *ty.RelayVerify) (*types.Receipt, error) ...@@ -516,16 +548,21 @@ func (action *relayDB) verifyTx(verify *ty.RelayVerify) (*types.Receipt, error)
return nil, err return nil, err
} }
accDb, err := action.createAccount(order.LocalCoinExec, order.LocalCoinSymbol)
if err != nil {
return nil, errors.Wrapf(err, "relay revokeAccept,exec=%s,sym=%s", order.LocalCoinExec, order.LocalCoinSymbol)
}
var receipt *types.Receipt var receipt *types.Receipt
if order.CoinOperation == ty.RelayOrderBuy { if order.Operation == ty.RelayOrderBuy {
receipt, err = action.coinsAccount.ExecTransferFrozen(order.CreaterAddr, order.AcceptAddr, action.execAddr, int64(order.Amount)) receipt, err = accDb.ExecTransferFrozen(order.CreaterAddr, order.AcceptAddr, action.execAddr, int64(order.LocalCoinAmount))
if err != nil { if err != nil {
relaylog.Error("verify buy transfer fail", "error", err.Error()) relaylog.Error("verify buy transfer fail", "error", err.Error())
return nil, err return nil, err
} }
} else { } else {
receipt, err = action.coinsAccount.ExecTransferFrozen(order.AcceptAddr, order.CreaterAddr, action.execAddr, int64(order.Amount)) receipt, err = accDb.ExecTransferFrozen(order.AcceptAddr, order.CreaterAddr, action.execAddr, int64(order.LocalCoinAmount))
if err != nil { if err != nil {
relaylog.Error("verify sell transfer fail", "error", err.Error()) relaylog.Error("verify sell transfer fail", "error", err.Error())
return nil, err return nil, err
...@@ -533,15 +570,15 @@ func (action *relayDB) verifyTx(verify *ty.RelayVerify) (*types.Receipt, error) ...@@ -533,15 +570,15 @@ func (action *relayDB) verifyTx(verify *ty.RelayVerify) (*types.Receipt, error)
} }
var receiptTransfer *types.Receipt var receiptTransfer *types.Receipt
if order.CoinOperation == ty.RelayOrderBuy { if order.Operation == ty.RelayOrderBuy {
receiptTransfer, err = action.coinsAccount.ExecActive(order.AcceptAddr, action.execAddr, int64(lockBtyAmount)) receiptTransfer, err = accDb.ExecActive(order.AcceptAddr, action.execAddr, int64(lockBtyAmount))
if err != nil { if err != nil {
relaylog.Error("verify exec active", "from", order.AcceptAddr, "amount", lockBtyAmount) relaylog.Error("verify exec active", "from", order.AcceptAddr, "amount", lockBtyAmount)
return nil, err return nil, err
} }
} else { } else {
receiptTransfer, err = action.coinsAccount.ExecActive(order.CreaterAddr, action.execAddr, int64(lockBtyAmount)) receiptTransfer, err = accDb.ExecActive(order.CreaterAddr, action.execAddr, int64(lockBtyAmount))
if err != nil { if err != nil {
relaylog.Error("verify exec active", "from", order.CreaterAddr, "amount", lockBtyAmount) relaylog.Error("verify exec active", "from", order.CreaterAddr, "amount", lockBtyAmount)
return nil, err return nil, err
...@@ -568,82 +605,6 @@ func (action *relayDB) verifyTx(verify *ty.RelayVerify) (*types.Receipt, error) ...@@ -568,82 +605,6 @@ func (action *relayDB) verifyTx(verify *ty.RelayVerify) (*types.Receipt, error)
} }
//之前用作cli verify, 检查不严格,现在弃用
func (action *relayDB) verifyCmdTx(verify *ty.RelayVerifyCli) (*types.Receipt, error) {
orderID := []byte(verify.OrderId)
order, err := action.getOrderByID(orderID)
if err != nil {
return nil, ty.ErrRelayOrderNotExist
}
if order.Status == ty.RelayOrderStatus_finished {
return nil, ty.ErrRelayOrderSoldout
}
if order.Status == ty.RelayOrderStatus_canceled {
return nil, ty.ErrRelayOrderRevoked
}
if order.Status == ty.RelayOrderStatus_pending || order.Status == ty.RelayOrderStatus_locking {
return nil, ty.ErrRelayOrderOnSell
}
var receipt *types.Receipt
err = action.btc.verifyCmdBtcTx(verify)
if err != nil {
return nil, err
}
if order.CoinOperation == ty.RelayOrderBuy {
receipt, err = action.coinsAccount.ExecTransferFrozen(order.CreaterAddr, order.AcceptAddr, action.execAddr, int64(order.Amount))
} else {
receipt, err = action.coinsAccount.ExecTransferFrozen(order.AcceptAddr, order.CreaterAddr, action.execAddr, int64(order.Amount))
}
if err != nil {
relaylog.Error("relay verify tx transfer fail", "error", err.Error())
return nil, err
}
var receiptTransfer *types.Receipt
if order.CoinOperation == ty.RelayOrderBuy {
receiptTransfer, err = action.coinsAccount.ExecActive(order.AcceptAddr, action.execAddr, int64(lockBtyAmount))
if err != nil {
relaylog.Error("verify exec active", "from", order.AcceptAddr, "amount", lockBtyAmount)
return nil, err
}
} else {
receiptTransfer, err = action.coinsAccount.ExecActive(order.CreaterAddr, action.execAddr, int64(lockBtyAmount))
if err != nil {
relaylog.Error("verify exec active", "from", order.CreaterAddr, "amount", lockBtyAmount)
return nil, err
}
}
order.PreStatus = order.Status
order.Status = ty.RelayOrderStatus_finished
order.FinishTime = action.blockTime
relayLog := newRelayLog(order)
orderKV := relayLog.save(action.db)
if err != nil {
return nil, err
}
var logs []*types.ReceiptLog
var kv []*types.KeyValue
logs = append(logs, receipt.Logs...)
logs = append(logs, receiptTransfer.Logs...)
logs = append(logs, relayLog.receiptLog(ty.TyLogRelayFinishTx))
kv = append(kv, receipt.KV...)
kv = append(kv, receiptTransfer.KV...)
kv = append(kv, orderKV...)
return &types.Receipt{Ty: types.ExecOk, KV: kv, Logs: logs}, nil
}
func saveBtcLastHead(db dbm.KV, head *ty.RelayLastRcvBtcHeader) (set []*types.KeyValue) { func saveBtcLastHead(db dbm.KV, head *ty.RelayLastRcvBtcHeader) (set []*types.KeyValue) {
if head == nil || head.Header == nil { if head == nil || head.Header == nil {
return nil return nil
......
...@@ -35,7 +35,7 @@ func TestRunSuiteRelayLog(t *testing.T) { ...@@ -35,7 +35,7 @@ func TestRunSuiteRelayLog(t *testing.T) {
func (s *suiteRelayLog) SetupSuite() { func (s *suiteRelayLog) SetupSuite() {
order := &ty.RelayOrder{ order := &ty.RelayOrder{
Id: "123456", Id: "123456",
CoinTxHash: "aabbccddee", XTxHash: "aabbccddee",
} }
s.db = new(mocks.KV) s.db = new(mocks.KV)
...@@ -46,7 +46,7 @@ func (s *suiteRelayLog) TestSave() { ...@@ -46,7 +46,7 @@ func (s *suiteRelayLog) TestSave() {
kvSet := []*types.KeyValue{} kvSet := []*types.KeyValue{}
value := types.Encode(&s.log.RelayOrder) value := types.Encode(&s.log.RelayOrder)
keyID := []byte(s.log.Id) keyID := []byte(s.log.Id)
keyCoinTxHash := []byte(calcCoinHash(s.log.CoinTxHash)) keyCoinTxHash := []byte(calcCoinHash(s.log.XTxHash))
kvSet = append(kvSet, &types.KeyValue{Key: keyID, Value: value}) kvSet = append(kvSet, &types.KeyValue{Key: keyID, Value: value})
kvSet = append(kvSet, &types.KeyValue{Key: keyCoinTxHash, Value: value}) kvSet = append(kvSet, &types.KeyValue{Key: keyCoinTxHash, Value: value})
...@@ -62,7 +62,7 @@ func (s *suiteRelayLog) TestGetKVSet() { ...@@ -62,7 +62,7 @@ func (s *suiteRelayLog) TestGetKVSet() {
kvSet := []*types.KeyValue{} kvSet := []*types.KeyValue{}
value := types.Encode(&s.log.RelayOrder) value := types.Encode(&s.log.RelayOrder)
keyID := []byte(s.log.Id) keyID := []byte(s.log.Id)
keyCoinTxHash := []byte(calcCoinHash(s.log.CoinTxHash)) keyCoinTxHash := []byte(calcCoinHash(s.log.XTxHash))
kvSet = append(kvSet, &types.KeyValue{Key: keyID, Value: value}) kvSet = append(kvSet, &types.KeyValue{Key: keyID, Value: value})
kvSet = append(kvSet, &types.KeyValue{Key: keyCoinTxHash, Value: value}) kvSet = append(kvSet, &types.KeyValue{Key: keyCoinTxHash, Value: value})
...@@ -167,10 +167,10 @@ func (s *suiteRelayDB) SetupSuite() { ...@@ -167,10 +167,10 @@ func (s *suiteRelayDB) SetupSuite() {
func (s *suiteRelayDB) TestRelayCreate_1() { func (s *suiteRelayDB) TestRelayCreate_1() {
order := &ty.RelayCreate{ order := &ty.RelayCreate{
Operation: ty.RelayOrderBuy, Operation: ty.RelayOrderBuy,
Coin: "BTC", XCoin: "BTC",
Amount: 10 * 1e8, XAmount: 10 * 1e8,
Addr: addrBtc, XAddr: addrBtc,
BtyAmount: 200 * 1e8, LocalCoinAmount: 200 * 1e8,
} }
tx := &types.Transaction{} tx := &types.Transaction{}
...@@ -193,8 +193,8 @@ func (s *suiteRelayDB) TestRelayCreate_1() { ...@@ -193,8 +193,8 @@ func (s *suiteRelayDB) TestRelayCreate_1() {
var log ty.ReceiptRelayLog var log ty.ReceiptRelayLog
types.Decode(receipt.Logs[len(receipt.Logs)-1].Log, &log) types.Decode(receipt.Logs[len(receipt.Logs)-1].Log, &log)
s.Equal("200.0000", log.TxAmount) s.Equal("200.0000", log.LocalCoinAmount)
s.Equal(uint64(10), log.CoinHeight) s.Equal(uint64(10), log.XHeight)
s.orderID = log.OrderId s.orderID = log.OrderId
} }
...@@ -303,10 +303,10 @@ func (s *suiteAccept) setupAccount() { ...@@ -303,10 +303,10 @@ func (s *suiteAccept) setupAccount() {
func (s *suiteAccept) setupRelayCreate() { func (s *suiteAccept) setupRelayCreate() {
order := &ty.RelayCreate{ order := &ty.RelayCreate{
Operation: ty.RelayOrderBuy, Operation: ty.RelayOrderBuy,
Coin: "BTC", XCoin: "BTC",
Amount: 10 * 1e8, XAmount: 10 * 1e8,
Addr: addrBtc, XAddr: addrBtc,
BtyAmount: 200 * 1e8, LocalCoinAmount: 200 * 1e8,
} }
tx := &types.Transaction{} tx := &types.Transaction{}
...@@ -329,8 +329,8 @@ func (s *suiteAccept) setupRelayCreate() { ...@@ -329,8 +329,8 @@ func (s *suiteAccept) setupRelayCreate() {
var log ty.ReceiptRelayLog var log ty.ReceiptRelayLog
types.Decode(receipt.Logs[len(receipt.Logs)-1].Log, &log) types.Decode(receipt.Logs[len(receipt.Logs)-1].Log, &log)
s.Equal("200.0000", log.TxAmount) s.Equal("200.0000", log.LocalCoinAmount)
s.Equal(uint64(10), log.CoinHeight) s.Equal(uint64(10), log.XHeight)
s.orderID = log.OrderId s.orderID = log.OrderId
} }
...@@ -358,7 +358,7 @@ func (s *suiteAccept) TestRelayAccept() { ...@@ -358,7 +358,7 @@ func (s *suiteAccept) TestRelayAccept() {
order := &ty.RelayAccept{ order := &ty.RelayAccept{
OrderId: s.orderID, OrderId: s.orderID,
CoinAddr: "BTC", XAddr: "BTC",
} }
tx := &types.Transaction{} tx := &types.Transaction{}
...@@ -380,8 +380,8 @@ func (s *suiteAccept) TestRelayAccept() { ...@@ -380,8 +380,8 @@ func (s *suiteAccept) TestRelayAccept() {
var log ty.ReceiptRelayLog var log ty.ReceiptRelayLog
types.Decode(receipt.Logs[len(receipt.Logs)-1].Log, &log) types.Decode(receipt.Logs[len(receipt.Logs)-1].Log, &log)
s.Equal("200.0000", log.TxAmount) s.Equal("200.0000", log.LocalCoinAmount)
s.Equal(uint64(20), log.CoinHeight) s.Equal(uint64(20), log.XHeight)
s.Equal(ty.RelayOrderStatus_locking.String(), log.CurStatus) s.Equal(ty.RelayOrderStatus_locking.String(), log.CurStatus)
} }
...@@ -452,7 +452,7 @@ func (s *suiteAccept) TestRevokeAccept_3() { ...@@ -452,7 +452,7 @@ func (s *suiteAccept) TestRevokeAccept_3() {
var log ty.ReceiptRelayLog var log ty.ReceiptRelayLog
types.Decode(receipt.Logs[len(receipt.Logs)-1].Log, &log) types.Decode(receipt.Logs[len(receipt.Logs)-1].Log, &log)
s.Equal(uint64(20), log.CoinHeight) s.Equal(uint64(20), log.XHeight)
s.Equal(ty.RelayOrderStatus_pending.String(), log.CurStatus) s.Equal(ty.RelayOrderStatus_pending.String(), log.CurStatus)
} }
...@@ -508,10 +508,10 @@ func (s *suiteConfirm) setupAccount() { ...@@ -508,10 +508,10 @@ func (s *suiteConfirm) setupAccount() {
func (s *suiteConfirm) setupRelayCreate() { func (s *suiteConfirm) setupRelayCreate() {
order := &ty.RelayCreate{ order := &ty.RelayCreate{
Operation: ty.RelayOrderBuy, Operation: ty.RelayOrderBuy,
Coin: "BTC", XCoin: "BTC",
Amount: 10 * 1e8, XAmount: 10 * 1e8,
Addr: addrBtc, XAddr: addrBtc,
BtyAmount: 200 * 1e8, LocalCoinAmount: 200 * 1e8,
} }
tx := &types.Transaction{} tx := &types.Transaction{}
...@@ -534,8 +534,8 @@ func (s *suiteConfirm) setupRelayCreate() { ...@@ -534,8 +534,8 @@ func (s *suiteConfirm) setupRelayCreate() {
var log ty.ReceiptRelayLog var log ty.ReceiptRelayLog
types.Decode(receipt.Logs[len(receipt.Logs)-1].Log, &log) types.Decode(receipt.Logs[len(receipt.Logs)-1].Log, &log)
s.Equal("200.0000", log.TxAmount) s.Equal("200.0000", log.LocalCoinAmount)
s.Equal(uint64(10), log.CoinHeight) s.Equal(uint64(10), log.XHeight)
s.orderID = log.OrderId s.orderID = log.OrderId
} }
...@@ -564,7 +564,7 @@ func (s *suiteConfirm) setupAccept() { ...@@ -564,7 +564,7 @@ func (s *suiteConfirm) setupAccept() {
order := &ty.RelayAccept{ order := &ty.RelayAccept{
OrderId: s.orderID, OrderId: s.orderID,
CoinAddr: "BTC", XAddr: "BTC",
} }
tx := &types.Transaction{} tx := &types.Transaction{}
...@@ -586,8 +586,8 @@ func (s *suiteConfirm) setupAccept() { ...@@ -586,8 +586,8 @@ func (s *suiteConfirm) setupAccept() {
var log ty.ReceiptRelayLog var log ty.ReceiptRelayLog
types.Decode(receipt.Logs[len(receipt.Logs)-1].Log, &log) types.Decode(receipt.Logs[len(receipt.Logs)-1].Log, &log)
s.Equal("200.0000", log.TxAmount) s.Equal("200.0000", log.LocalCoinAmount)
s.Equal(uint64(20), log.CoinHeight) s.Equal(uint64(20), log.XHeight)
s.Equal(ty.RelayOrderStatus_locking.String(), log.CurStatus) s.Equal(ty.RelayOrderStatus_locking.String(), log.CurStatus)
} }
...@@ -637,8 +637,8 @@ func (s *suiteConfirm) TestConfirm_2() { ...@@ -637,8 +637,8 @@ func (s *suiteConfirm) TestConfirm_2() {
var log ty.ReceiptRelayLog var log ty.ReceiptRelayLog
types.Decode(receipt.Logs[len(receipt.Logs)-1].Log, &log) types.Decode(receipt.Logs[len(receipt.Logs)-1].Log, &log)
s.Equal("200.0000", log.TxAmount) s.Equal("200.0000", log.LocalCoinAmount)
s.Equal(uint64(30), log.CoinHeight) s.Equal(uint64(30), log.XHeight)
s.Equal(ty.RelayOrderStatus_confirming.String(), log.CurStatus) s.Equal(ty.RelayOrderStatus_confirming.String(), log.CurStatus)
} }
...@@ -686,7 +686,7 @@ func (s *suiteConfirm) TestRevokeConfirm_2() { ...@@ -686,7 +686,7 @@ func (s *suiteConfirm) TestRevokeConfirm_2() {
s.Nil(err) s.Nil(err)
var log ty.ReceiptRelayLog var log ty.ReceiptRelayLog
types.Decode(receipt.Logs[len(receipt.Logs)-1].Log, &log) types.Decode(receipt.Logs[len(receipt.Logs)-1].Log, &log)
s.Equal(uint64(30), log.CoinHeight) s.Equal(uint64(30), log.XHeight)
s.Equal(ty.RelayOrderStatus_pending.String(), log.CurStatus) s.Equal(ty.RelayOrderStatus_pending.String(), log.CurStatus)
} }
...@@ -743,10 +743,10 @@ func (s *suiteVerify) setupAccount() { ...@@ -743,10 +743,10 @@ func (s *suiteVerify) setupAccount() {
func (s *suiteVerify) setupRelayCreate() { func (s *suiteVerify) setupRelayCreate() {
order := &ty.RelayCreate{ order := &ty.RelayCreate{
Operation: ty.RelayOrderBuy, Operation: ty.RelayOrderBuy,
Coin: "BTC", XCoin: "BTC",
Amount: 0.299 * 1e8, XAmount: 0.299 * 1e8,
Addr: addrBtc, XAddr: addrBtc,
BtyAmount: 200 * 1e8, LocalCoinAmount: 200 * 1e8,
} }
tx := &types.Transaction{} tx := &types.Transaction{}
...@@ -769,8 +769,8 @@ func (s *suiteVerify) setupRelayCreate() { ...@@ -769,8 +769,8 @@ func (s *suiteVerify) setupRelayCreate() {
var log ty.ReceiptRelayLog var log ty.ReceiptRelayLog
types.Decode(receipt.Logs[len(receipt.Logs)-1].Log, &log) types.Decode(receipt.Logs[len(receipt.Logs)-1].Log, &log)
s.Equal("200.0000", log.TxAmount) s.Equal("200.0000", log.LocalCoinAmount)
s.Equal(uint64(10), log.CoinHeight) s.Equal(uint64(10), log.XHeight)
s.orderID = log.OrderId s.orderID = log.OrderId
} }
...@@ -779,7 +779,7 @@ func (s *suiteVerify) setupAccept() { ...@@ -779,7 +779,7 @@ func (s *suiteVerify) setupAccept() {
order := &ty.RelayAccept{ order := &ty.RelayAccept{
OrderId: s.orderID, OrderId: s.orderID,
CoinAddr: "BTC", XAddr: "BTC",
} }
tx := &types.Transaction{} tx := &types.Transaction{}
...@@ -801,8 +801,8 @@ func (s *suiteVerify) setupAccept() { ...@@ -801,8 +801,8 @@ func (s *suiteVerify) setupAccept() {
var log ty.ReceiptRelayLog var log ty.ReceiptRelayLog
types.Decode(receipt.Logs[len(receipt.Logs)-1].Log, &log) types.Decode(receipt.Logs[len(receipt.Logs)-1].Log, &log)
s.Equal("200.0000", log.TxAmount) s.Equal("200.0000", log.LocalCoinAmount)
s.Equal(uint64(20), log.CoinHeight) s.Equal(uint64(20), log.XHeight)
s.Equal(ty.RelayOrderStatus_locking.String(), log.CurStatus) s.Equal(ty.RelayOrderStatus_locking.String(), log.CurStatus)
} }
...@@ -833,8 +833,8 @@ func (s *suiteVerify) setupConfirm() { ...@@ -833,8 +833,8 @@ func (s *suiteVerify) setupConfirm() {
var log ty.ReceiptRelayLog var log ty.ReceiptRelayLog
types.Decode(receipt.Logs[len(receipt.Logs)-1].Log, &log) types.Decode(receipt.Logs[len(receipt.Logs)-1].Log, &log)
s.Equal("200.0000", log.TxAmount) s.Equal("200.0000", log.LocalCoinAmount)
s.Equal(uint64(30), log.CoinHeight) s.Equal(uint64(30), log.XHeight)
s.Equal(ty.RelayOrderStatus_confirming.String(), log.CurStatus) s.Equal(ty.RelayOrderStatus_confirming.String(), log.CurStatus)
} }
...@@ -923,8 +923,8 @@ func (s *suiteVerify) TestVerify() { ...@@ -923,8 +923,8 @@ func (s *suiteVerify) TestVerify() {
var log ty.ReceiptRelayLog var log ty.ReceiptRelayLog
types.Decode(receipt.Logs[len(receipt.Logs)-1].Log, &log) types.Decode(receipt.Logs[len(receipt.Logs)-1].Log, &log)
//s.Equal("200.0000",log.TxAmount) //s.Equal("200.0000",log.LocalCoinAmount)
//s.Equal(uint64(30),log.CoinHeight) //s.Equal(uint64(30),log.XHeight)
s.Equal(ty.RelayOrderStatus_finished.String(), log.CurStatus) s.Equal(ty.RelayOrderStatus_finished.String(), log.CurStatus)
} }
...@@ -980,10 +980,10 @@ func (s *suiteVerifyCli) setupAccount() { ...@@ -980,10 +980,10 @@ func (s *suiteVerifyCli) setupAccount() {
func (s *suiteVerifyCli) setupRelayCreate() { func (s *suiteVerifyCli) setupRelayCreate() {
order := &ty.RelayCreate{ order := &ty.RelayCreate{
Operation: ty.RelayOrderBuy, Operation: ty.RelayOrderBuy,
Coin: "BTC", XCoin: "BTC",
Amount: 0.299 * 1e8, XAmount: 0.299 * 1e8,
Addr: addrBtc, XAddr: addrBtc,
BtyAmount: 200 * 1e8, LocalCoinAmount: 200 * 1e8,
} }
tx := &types.Transaction{} tx := &types.Transaction{}
...@@ -1006,8 +1006,8 @@ func (s *suiteVerifyCli) setupRelayCreate() { ...@@ -1006,8 +1006,8 @@ func (s *suiteVerifyCli) setupRelayCreate() {
var log ty.ReceiptRelayLog var log ty.ReceiptRelayLog
types.Decode(receipt.Logs[len(receipt.Logs)-1].Log, &log) types.Decode(receipt.Logs[len(receipt.Logs)-1].Log, &log)
s.Equal("200.0000", log.TxAmount) s.Equal("200.0000", log.LocalCoinAmount)
s.Equal(uint64(10), log.CoinHeight) s.Equal(uint64(10), log.XHeight)
s.orderID = log.OrderId s.orderID = log.OrderId
} }
...@@ -1016,7 +1016,7 @@ func (s *suiteVerifyCli) setupAccept() { ...@@ -1016,7 +1016,7 @@ func (s *suiteVerifyCli) setupAccept() {
order := &ty.RelayAccept{ order := &ty.RelayAccept{
OrderId: s.orderID, OrderId: s.orderID,
CoinAddr: "BTC", XAddr: "BTC",
} }
tx := &types.Transaction{} tx := &types.Transaction{}
...@@ -1038,8 +1038,8 @@ func (s *suiteVerifyCli) setupAccept() { ...@@ -1038,8 +1038,8 @@ func (s *suiteVerifyCli) setupAccept() {
var log ty.ReceiptRelayLog var log ty.ReceiptRelayLog
types.Decode(receipt.Logs[len(receipt.Logs)-1].Log, &log) types.Decode(receipt.Logs[len(receipt.Logs)-1].Log, &log)
s.Equal("200.0000", log.TxAmount) s.Equal("200.0000", log.LocalCoinAmount)
s.Equal(uint64(20), log.CoinHeight) s.Equal(uint64(20), log.XHeight)
s.Equal(ty.RelayOrderStatus_locking.String(), log.CurStatus) s.Equal(ty.RelayOrderStatus_locking.String(), log.CurStatus)
} }
...@@ -1070,8 +1070,8 @@ func (s *suiteVerifyCli) setupConfirm() { ...@@ -1070,8 +1070,8 @@ func (s *suiteVerifyCli) setupConfirm() {
var log ty.ReceiptRelayLog var log ty.ReceiptRelayLog
types.Decode(receipt.Logs[len(receipt.Logs)-1].Log, &log) types.Decode(receipt.Logs[len(receipt.Logs)-1].Log, &log)
s.Equal("200.0000", log.TxAmount) s.Equal("200.0000", log.LocalCoinAmount)
s.Equal(uint64(30), log.CoinHeight) s.Equal(uint64(30), log.XHeight)
s.Equal(ty.RelayOrderStatus_confirming.String(), log.CurStatus) s.Equal(ty.RelayOrderStatus_confirming.String(), log.CurStatus)
} }
...@@ -1097,48 +1097,6 @@ func (s *suiteVerifyCli) SetupSuite() { ...@@ -1097,48 +1097,6 @@ func (s *suiteVerifyCli) SetupSuite() {
s.setupConfirm() s.setupConfirm()
} }
func (s *suiteVerifyCli) TestVerify() {
var head = &ty.BtcHeader{
Version: 1,
MerkleRoot: "f3e94742aca4b5ef85488dc37c06c3282295ffec960994b2c0d5ac2a25a95766",
}
headEnc := types.Encode(head)
s.kvdb.On("Get", mock.Anything).Return(headEnc, nil).Once()
order := &ty.RelayVerifyCli{
OrderId: s.orderID,
RawTx: "0100000001c33ebff2a709f13d9f9a7569ab16a32786af7d7e2de09265e41c61d078294ecf010000008a4730440220032d30df5ee6f57fa46cddb5eb8d0d9fe8de6b342d27942ae90a3231e0ba333e02203deee8060fdc70230a7f5b4ad7d7bc3e628cbe219a886b84269eaeb81e26b4fe014104ae31c31bf91278d99b8377a35bbce5b27d9fff15456839e919453fc7b3f721f0ba403ff96c9deeb680e5fd341c0fc3a7b90da4631ee39560639db462e9cb850fffffffff0240420f00000000001976a914b0dcbf97eabf4404e31d952477ce822dadbe7e1088acc060d211000000001976a9146b1281eec25ab4e1e0793ff4e08ab1abb3409cd988ac00000000",
TxIndex: 2,
MerkBranch: "e9a66845e05d5abc0ad04ec80f774a7e585c6e8db975962d069a522137b80c1d-ccdafb73d8dcd0173d5d5c3c9a0770d0b3953db889dab99ef05b1907518cb815",
BlockHash: "000000000003ba27aa200b1cecaad478d2b00432346c3f1f3986da1afd33e506",
}
tx := &types.Transaction{Execer: []byte(ty.RelayX)}
tx.To = s.addrRelay
tx.Sign(types.SECP256K1, privTo)
s.relay.SetEnv(40, 4000, 1)
s.relayDb = newRelayDB(s.relay, tx)
receipt, err := s.relayDb.verifyCmdTx(order)
s.Nil(err)
acc := s.relay.GetCoinsAccount()
account := acc.LoadExecAccount(addrFrom, s.addrRelay)
//s.Equal(int64(200*1e8),account.Balance)
s.Zero(account.Frozen)
account = acc.LoadExecAccount(addrTo, s.addrRelay)
s.Equal(int64(400*1e8), account.Balance)
s.Zero(account.Frozen)
var log ty.ReceiptRelayLog
types.Decode(receipt.Logs[len(receipt.Logs)-1].Log, &log)
s.Equal(ty.RelayOrderStatus_finished.String(), log.CurStatus)
}
func TestRunSuiteVerifyCli(t *testing.T) { func TestRunSuiteVerifyCli(t *testing.T) {
log := new(suiteVerifyCli) log := new(suiteVerifyCli)
suite.Run(t, log) suite.Run(t, log)
......
...@@ -28,13 +28,13 @@ message RelayOrder { ...@@ -28,13 +28,13 @@ message RelayOrder {
string id = 1; string id = 1;
RelayOrderStatus status = 2; RelayOrderStatus status = 2;
RelayOrderStatus preStatus = 3; RelayOrderStatus preStatus = 3;
uint64 amount = 4; uint64 localCoinAmount = 4;
string createrAddr = 5; string createrAddr = 5;
uint32 coinOperation = 6; uint32 operation = 6;
string coin = 7; string xCoin = 7;
uint64 coinAmount = 8; uint64 xAmount = 8;
string coinAddr = 9; string xAddr = 9;
string coinTxHash = 10; string xTxHash = 10;
int64 createTime = 11; int64 createTime = 11;
string acceptAddr = 12; string acceptAddr = 12;
int64 acceptTime = 13; int64 acceptTime = 13;
...@@ -42,23 +42,27 @@ message RelayOrder { ...@@ -42,23 +42,27 @@ message RelayOrder {
int64 finishTime = 15; int64 finishTime = 15;
string finishTxHash = 16; string finishTxHash = 16;
int64 height = 17; int64 height = 17;
uint64 coinHeight = 18; uint64 XHeight = 18;
uint32 coinWaits = 19; uint32 xBlockWaits = 19;
string localCoinSymbol = 20;
string localCoinExec = 21;
} }
message RelayCreate { message RelayCreate {
uint32 operation = 1; // 0: buy, 1: sell uint32 operation = 1; // 0: buy, 1: sell
string coin = 2; // outside coin BTC, ETH... string xCoin = 2; // outside cross coin BTC, ETH...
uint64 amount = 3; uint64 xAmount = 3;
string addr = 4; string xAddr = 4;
uint64 btyAmount = 5; uint64 localCoinAmount = 5;
uint32 coinWaits = 6; // the coin blocks to wait uint32 xBlockWaits = 6; // the coin blocks to wait
string localCoinSymbol = 7;
string localCoinExec = 8;
} }
message RelayAccept { message RelayAccept {
string orderId = 1; string orderId = 1;
string coinAddr = 2; // for sell coin case needed string xAddr = 2; // for sell coin case needed
uint32 coinWaits = 3; uint32 xBlockWaits = 3;
} }
message RelayRevoke { message RelayRevoke {
...@@ -154,21 +158,23 @@ message ReceiptRelayLog { ...@@ -154,21 +158,23 @@ message ReceiptRelayLog {
string orderId = 1; string orderId = 1;
string curStatus = 2; string curStatus = 2;
string preStatus = 3; string preStatus = 3;
string txAmount = 4; string localCoinAmount = 4;
string createrAddr = 5; string createrAddr = 5;
string coinOperation = 6; uint32 coinOperation = 6;
string coin = 7; string xCoin = 7;
string coinAmount = 8; string xAmount = 8;
string coinAddr = 9; string xAddr = 9;
string coinTxHash = 10; string xTxHash = 10;
int64 createTime = 11; int64 createTime = 11;
string acceptAddr = 12; string acceptAddr = 12;
int64 acceptTime = 13; int64 acceptTime = 13;
int64 confirmTime = 14; int64 confirmTime = 14;
int64 finishTime = 15; int64 finishTime = 15;
string finishTxHash = 16; string finishTxHash = 16;
uint64 coinHeight = 17; uint64 xHeight = 17;
uint32 coinWaits = 18; uint32 xBlockWaits = 18;
string localCoinSymbol = 19;
string localCoinExec = 20;
} }
message ReqRelayAddrCoins { message ReqRelayAddrCoins {
......
...@@ -42,12 +42,6 @@ const ( ...@@ -42,12 +42,6 @@ const (
RelayOrderSell RelayOrderSell
) )
// RelayOrderOperation buy or sell operation
var RelayOrderOperation = map[uint32]string{
RelayOrderBuy: "buy",
RelayOrderSell: "sell",
}
const ( const (
// RelayUnlock revoke order // RelayUnlock revoke order
RelayUnlock = iota RelayUnlock = iota
...@@ -171,7 +165,7 @@ func (r *RelayType) Amount(tx *types.Transaction) (int64, error) { ...@@ -171,7 +165,7 @@ func (r *RelayType) Amount(tx *types.Transaction) (int64, error) {
} }
relay := data.(*RelayAction) relay := data.(*RelayAction)
if RelayActionCreate == relay.Ty && relay.GetCreate() != nil { if RelayActionCreate == relay.Ty && relay.GetCreate() != nil {
return int64(relay.GetCreate().BtyAmount), nil return int64(relay.GetCreate().LocalCoinAmount), nil
} }
return 0, nil return 0, nil
} }
......
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