Commit 80f2bde6 authored by shajiaiming's avatar shajiaiming

Merge branch 'feature/cross_chain' into 'master'

Feature/cross chain See merge request !111
parents 17908887 d524195e
...@@ -52,6 +52,7 @@ class CrossChainController extends BaseController ...@@ -52,6 +52,7 @@ class CrossChainController extends BaseController
$data['txhex'] = $val['tx']; $data['txhex'] = $val['tx'];
$data['transfer_url'] = $val['url']; $data['transfer_url'] = $val['url'];
$data['step'] = $val['step']; $data['step'] = $val['step'];
$data['amount'] = isset($val['amount']) ? $val['amount'] : 0;
$model->load($data, ''); $model->load($data, '');
$model->save(); $model->save();
} }
...@@ -123,7 +124,7 @@ class CrossChainController extends BaseController ...@@ -123,7 +124,7 @@ class CrossChainController extends BaseController
$code = -1; $code = -1;
$msg = 'fail'; $msg = 'fail';
$data = null; $data = null;
$step = 0; $step = $amount = 0;
$request = Yii::$app->request; $request = Yii::$app->request;
if (!$request->isGet) { if (!$request->isGet) {
$msg = '请求错误!'; $msg = '请求错误!';
...@@ -146,6 +147,7 @@ class CrossChainController extends BaseController ...@@ -146,6 +147,7 @@ class CrossChainController extends BaseController
sort($model); sort($model);
foreach ($model as $key => $val) { foreach ($model as $key => $val) {
$amount = $val['amount'];
//未交易 //未交易
if ('0' == $val['send_result'] && '0' == $val['query_result'] && '0' == $val['msg']) { if ('0' == $val['send_result'] && '0' == $val['query_result'] && '0' == $val['msg']) {
$step = $key + 1; $step = $key + 1;
...@@ -189,7 +191,8 @@ class CrossChainController extends BaseController ...@@ -189,7 +191,8 @@ class CrossChainController extends BaseController
doEnd : doEnd :
$data = [ $data = [
'step' => $step 'step' => $step,
'amount' => (int)$amount
]; ];
return ['code' => $code, 'msg' => $msg, 'data' => $data]; return ['code' => $code, 'msg' => $msg, 'data' => $data];
} }
......
This diff is collapsed.
...@@ -10,6 +10,9 @@ class CoinCrossChain extends BaseActiveRecord ...@@ -10,6 +10,9 @@ class CoinCrossChain extends BaseActiveRecord
const WITHDRAW = 0; const WITHDRAW = 0;
const RECHARGE = 1; const RECHARGE = 1;
const CROSS_CHAIN_TRANSFERING = 'cross_chain_transfering';
const CROSS_CHAIN_CACHE_FAIL = 'cross_chain_fail';
public static function getDb() public static function getDb()
{ {
return Yii::$app->get('p_sources'); return Yii::$app->get('p_sources');
...@@ -28,14 +31,14 @@ class CoinCrossChain extends BaseActiveRecord ...@@ -28,14 +31,14 @@ class CoinCrossChain extends BaseActiveRecord
{ {
return [ return [
[['is_with_draw', 'address', 'txhex', 'transfer_url'], 'required'], [['is_with_draw', 'address', 'txhex', 'transfer_url'], 'required'],
[['transfer_number', 'step', 'send_result', 'query_result', 'msg'], 'safe'] [['transfer_number', 'amount', 'step', 'send_result', 'query_result', 'msg'], 'safe']
]; ];
} }
public function scenarios() public function scenarios()
{ {
$scenarios = [ $scenarios = [
self:: SCENARIOS_CREATE => ['is_with_draw', 'address', 'txhex', 'transfer_url', 'transfer_number', 'step', 'send_result', 'query_result', 'msg'], self:: SCENARIOS_CREATE => ['is_with_draw', 'address', 'txhex', 'transfer_url', 'transfer_number', 'amount', 'step', 'send_result', 'query_result', 'msg'],
]; ];
return array_merge(parent:: scenarios(), $scenarios); return array_merge(parent:: scenarios(), $scenarios);
} }
......
...@@ -20,7 +20,7 @@ class Chain33Service ...@@ -20,7 +20,7 @@ class Chain33Service
public function __construct($parameter = []) public function __construct($parameter = [])
{ {
if(empty($parameter)){ if (empty($parameter)) {
$this->node_params = Yii::$app->params['chain33']; $this->node_params = Yii::$app->params['chain33'];
} else { } else {
$this->node_params = $parameter; $this->node_params = $parameter;
...@@ -31,8 +31,8 @@ class Chain33Service ...@@ -31,8 +31,8 @@ class Chain33Service
{ {
$config = $this->node_params; $config = $this->node_params;
$scheme = $config['scheme'] ?? 'http'; $scheme = $config['scheme'] ?? 'http';
$host = $config['host'] ?? '127.0.0.1'; $host = $config['host'] ?? '127.0.0.1';
$port = (string)$config['port'] ?? ''; $port = (string)$config['port'] ?? '';
if ($port) { if ($port) {
return $scheme . '://' . $host . ':' . $port; return $scheme . '://' . $host . ':' . $port;
} else { } else {
...@@ -44,9 +44,9 @@ class Chain33Service ...@@ -44,9 +44,9 @@ class Chain33Service
{ {
$data = [ $data = [
'jsonrpc' => '2.0', 'jsonrpc' => '2.0',
'id' => 0, 'id' => 0,
'method' => $method, 'method' => $method,
'params' => [], 'params' => [],
]; ];
if (!empty($params)) { if (!empty($params)) {
$data['params'][] = $params; $data['params'][] = $params;
...@@ -56,7 +56,11 @@ class Chain33Service ...@@ -56,7 +56,11 @@ class Chain33Service
public function send($params = [], $method = 'Chain33.Query') public function send($params = [], $method = 'Chain33.Query')
{ {
$ch = new Curl(); $timeout = [
'connect_timeout' => 50000,
'timeout_ms' => 40000
];
$ch = new Curl($timeout);
$jsonrpc = self::jsonRpcBuild($params, $method); $jsonrpc = self::jsonRpcBuild($params, $method);
$ch->setHeader('Content-Type', 'application/json'); $ch->setHeader('Content-Type', 'application/json');
$ch->setRawPostData($jsonrpc); $ch->setRawPostData($jsonrpc);
...@@ -124,7 +128,7 @@ class Chain33Service ...@@ -124,7 +128,7 @@ class Chain33Service
* 获取地址下的所有token资产 * 获取地址下的所有token资产
* *
* @param string|array $address * @param string|array $address
* @param string $symbol * @param string $symbol
* @return array * @return array
*/ */
public function getAccountTokenAssets($address, $symbol) public function getAccountTokenAssets($address, $symbol)
...@@ -133,8 +137,8 @@ class Chain33Service ...@@ -133,8 +137,8 @@ class Chain33Service
$address = [$address]; $address = [$address];
} }
$params = [ $params = [
"addresses" => $address, "addresses" => $address,
"execer" => "user.p.guodun.token", "execer" => "user.p.guodun.token",
"tokenSymbol" => $symbol "tokenSymbol" => $symbol
]; ];
return $this->send($params, 'token.GetTokenBalance'); return $this->send($params, 'token.GetTokenBalance');
...@@ -147,18 +151,18 @@ class Chain33Service ...@@ -147,18 +151,18 @@ class Chain33Service
* @param $to * @param $to
* @param $amount * @param $amount
* @param string $note * @param string $note
* @param bool $isToken * @param bool $isToken
* @param string $tokenSymbol * @param string $tokenSymbol
* @return array * @return array
*/ */
public function extractToken($from, $to, $amount, $note = '', $isToken = true, $tokenSymbol = '') public function extractToken($from, $to, $amount, $note = '', $isToken = true, $tokenSymbol = '')
{ {
$params = [ $params = [
"from" => $to, "from" => $to,
"to" => $from, "to" => $from,
"amount" => -$amount, "amount" => -$amount,
"note" => $note, "note" => $note,
"isToken" => $isToken ? true : false, "isToken" => $isToken ? true : false,
"tokenSymbol" => strtoupper($tokenSymbol) "tokenSymbol" => strtoupper($tokenSymbol)
]; ];
return $this->send($params, 'Chain33.SendToAddress'); return $this->send($params, 'Chain33.SendToAddress');
...@@ -171,18 +175,18 @@ class Chain33Service ...@@ -171,18 +175,18 @@ class Chain33Service
* @param $to * @param $to
* @param $amount * @param $amount
* @param string $note * @param string $note
* @param bool $isToken * @param bool $isToken
* @param string $tokenSymbol * @param string $tokenSymbol
* @return array * @return array
*/ */
public function transToken($from, $to, $amount, $note = '', $isToken = true, $tokenSymbol = '') public function transToken($from, $to, $amount, $note = '', $isToken = true, $tokenSymbol = '')
{ {
$params = [ $params = [
"from" => $from, "from" => $from,
"to" => $to, "to" => $to,
"amount" => $amount, "amount" => $amount,
"note" => $note, "note" => $note,
"isToken" => $isToken ? true : false, "isToken" => $isToken ? true : false,
"tokenSymbol" => strtoupper($tokenSymbol) "tokenSymbol" => strtoupper($tokenSymbol)
]; ];
return $this->send($params, 'Chain33.SendToAddress'); return $this->send($params, 'Chain33.SendToAddress');
...@@ -191,25 +195,25 @@ class Chain33Service ...@@ -191,25 +195,25 @@ class Chain33Service
public function createTokenRawTransaction($to, $amount, $isToken, $tokenSymbol, $fee, $note, $execer) public function createTokenRawTransaction($to, $amount, $isToken, $tokenSymbol, $fee, $note, $execer)
{ {
$params = [ $params = [
"to" => $to, "to" => $to,
"amount" => $amount, "amount" => $amount,
"isToken" => $isToken, "isToken" => $isToken,
"tokenSymbol" => $tokenSymbol, "tokenSymbol" => $tokenSymbol,
"fee" => $fee, "fee" => $fee,
"note" => $note, "note" => $note,
"execer" => $execer, "execer" => $execer,
]; ];
return $this->send($params, 'Chain33.CreateRawTransaction'); return $this->send($params, 'Chain33.CreateRawTransaction');
} }
public function createRawTransaction($to, $amount, $fee, $note,$execer) public function createRawTransaction($to, $amount, $fee, $note, $execer)
{ {
$params = [ $params = [
"to" => $to, "to" => $to,
"amount" => $amount, "amount" => $amount,
"fee" => $fee, "fee" => $fee,
"note" => $note, "note" => $note,
"execer" => $execer, "execer" => $execer,
]; ];
...@@ -274,11 +278,11 @@ class Chain33Service ...@@ -274,11 +278,11 @@ class Chain33Service
public function structureTrade($amount, $execName, $fee, $isToken = True, $to, $tokenSymbol = '') public function structureTrade($amount, $execName, $fee, $isToken = True, $to, $tokenSymbol = '')
{ {
$params = [ $params = [
"amount" => $amount, "amount" => $amount,
"execName" => $execName, "execName" => $execName,
"fee" => $fee, "fee" => $fee,
"isToken" => $isToken ? true : false, "isToken" => $isToken ? true : false,
"to" => $to, "to" => $to,
"tokenSymbol" => strtoupper($tokenSymbol) "tokenSymbol" => strtoupper($tokenSymbol)
]; ];
return $this->send($params, 'Chain33.CreateRawTransaction'); return $this->send($params, 'Chain33.CreateRawTransaction');
...@@ -287,9 +291,9 @@ class Chain33Service ...@@ -287,9 +291,9 @@ class Chain33Service
public function signTrade($addr, $expire = '2h', $txHex) public function signTrade($addr, $expire = '2h', $txHex)
{ {
$params = [ $params = [
"addr" => $addr, "addr" => $addr,
"expire" => $expire, "expire" => $expire,
"txHex" => $txHex "txHex" => $txHex
]; ];
return $this->send($params, 'Chain33.SignRawTx'); return $this->send($params, 'Chain33.SignRawTx');
} }
...@@ -297,11 +301,19 @@ class Chain33Service ...@@ -297,11 +301,19 @@ class Chain33Service
public function sendTrade($data) public function sendTrade($data)
{ {
$params = [ $params = [
"data" => $data, "data" => $data,
]; ];
return $this->send($params, 'Chain33.SendTransaction'); return $this->send($params, 'Chain33.SendTransaction');
} }
public function getHeight($params = [])
{
$params = [
"data" => 'user.p.game.',
];
return $this->send($params, 'paracross.GetHeight');
}
/** /**
* 获取最新的区块 * 获取最新的区块
*/ */
...@@ -329,12 +341,12 @@ class Chain33Service ...@@ -329,12 +341,12 @@ class Chain33Service
public function getTxByAddr($addr, $flag, $count, $direction, $height, $index) public function getTxByAddr($addr, $flag, $count, $direction, $height, $index)
{ {
$params = [ $params = [
'addr' => $addr, 'addr' => $addr,
'flag' => $flag, 'flag' => $flag,
'count' => $count, 'count' => $count,
'direction' => $direction, 'direction' => $direction,
'height' => $height, 'height' => $height,
'index' => $index 'index' => $index
]; ];
return $this->send($params, 'Chain33.GetTxByAddr'); return $this->send($params, 'Chain33.GetTxByAddr');
} }
...@@ -359,9 +371,9 @@ class Chain33Service ...@@ -359,9 +371,9 @@ class Chain33Service
public function unLockWallet($password) public function unLockWallet($password)
{ {
$params = [ $params = [
'passwd' => $password, 'passwd' => $password,
'walletorticket' => false, 'walletorticket' => false,
'timeout' => 30 'timeout' => 30
]; ];
return $this->send($params, 'Chain33.UnLock'); return $this->send($params, 'Chain33.UnLock');
} }
......
...@@ -14,71 +14,88 @@ class CrossChainController extends Controller ...@@ -14,71 +14,88 @@ class CrossChainController extends Controller
* *
* @return array * @return array
*/ */
public function actionAutoTransfer() public function actionAutoTransfer($step, $is_with_draw)
{ {
$model = CoinCrossChain::find()->where(['send_result' => "0"])->limit(20)->groupBy('transfer_number')->orderBy('id')->asArray()->all(); $redis = Yii::$app->redis;
$model = CoinCrossChain::find()->where(['send_result' => "0", 'step' => (int)$step, 'is_with_draw' => (int)$is_with_draw])->asArray()->all();
if (empty($model)) { if (empty($model)) {
echo date('Y-m-d H:i:s') . '暂无跨链交易计划' . PHP_EOL; echo date('Y-m-d H:i:s') . ' STEP: ' . $step . '暂无跨链交易计划' . PHP_EOL;
return 0; return 0;
} }
$current_time = time();
foreach ($model as $val) { foreach ($model as $val) {
$isExist = CoinCrossChain::find() if ($step > 1) {
->where(['transfer_number' => $val['transfer_number']]) $isExist = CoinCrossChain::find()
->andWhere(['<', 'id', (int)$val['id']]) ->where(['transfer_number' => $val['transfer_number']])
->orderBy('id desc') ->andWhere(['<', 'id', (int)$val['id']])
->asArray() ->orderBy('id desc')
->one(); ->asArray()
//上一步发送成功,未查询 ->one();
if ('success' != $isExist['query_result'] && !empty($isExist)) { //上一步发送成功,未查询
if ('success' != $isExist['query_result'] && !empty($isExist)) {
continue;
}
}
$cache_transfering_time = $redis->hget(CoinCrossChain::CROSS_CHAIN_TRANSFERING, $val['address'] . ':' . $val['is_with_draw'] . ':' . $val['id']);
if (true == $cache_transfering_time) {
continue; continue;
} }
$redis->hmset(CoinCrossChain::CROSS_CHAIN_TRANSFERING, $val['address'] . ':' . $val['is_with_draw'] . ':' . $val['id'], $current_time);
//go(function () use ($val) { go(function () use ($val, $redis) {
// \Co::sleep(0.5); \Co::sleep(0.5);
$transfer_url = $val['transfer_url']; $transfer_url = $val['transfer_url'];
$transfer_url = explode(':', $transfer_url); $transfer_url = explode(':', $transfer_url);
$node_params = [ $node_params = [
'scheme' => $transfer_url[0], 'scheme' => $transfer_url[0],
'host' => str_replace('//', '', $transfer_url[1]), 'host' => str_replace('//', '', $transfer_url[1]),
'port' => isset($transfer_url[2]) ? $transfer_url[2] : '' 'port' => isset($transfer_url[2]) ? $transfer_url[2] : '',
]; ];
$service = new Chain33Service($node_params); $service = new Chain33Service($node_params);
$sign_str = $val['txhex']; $sign_str = $val['txhex'];
$result = $service->sendTransaction($sign_str); $result = $service->sendTransaction($sign_str);
if (0 != $result['code']) { if (0 == $result['code']) {
$msg = $result['msg']; $send_result = $result['result'];
goto doEnd; $currentModel = CoinCrossChain::findOne($val['id']);
}
$send_result = $result['result'];
doEnd :
$currentModel = CoinCrossChain::findOne($val['id']);
if (isset($msg)) {
$currentModel->msg = $msg;
}
if (isset($send_result)) {
$currentModel->send_result = $send_result; $currentModel->send_result = $send_result;
$currentModel->msg = 0;
$currentModel->save();
$redis->hdel(CoinCrossChain::CROSS_CHAIN_TRANSFERING, $val['address'] . ':' . $val['is_with_draw'] . ':' . $val['id']);
} }
$currentModel->save();
//}); if (0 != $result['code'] && (false == strstr($result['msg'], 'ErrDupTx'))) {
$redis->hdel(CoinCrossChain::CROSS_CHAIN_TRANSFERING, $val['address'] . ':' . $val['is_with_draw'] . ':' . $val['id']);
}
});
} }
echo date('Y-m-d H:i:s') . '跨链交易成功' . PHP_EOL; echo date('Y-m-d H:i:s') . ' STEP: ' . $step . '跨链交易成功' . PHP_EOL;
return 0; return 0;
} }
public function actionQueryTransaction() public function actionQueryTransaction()
{ {
$model = CoinCrossChain::find()->where(['<>', 'send_result', '0'])->andWhere(['msg' => '0'])->limit(20)->asArray()->all(); $redis = Yii::$app->redis;
$model = CoinCrossChain::find()->where(['<>', 'send_result', '0'])->andWhere(['msg' => '0'])->asArray()->all();
if (empty($model)) { if (empty($model)) {
echo date('Y-m-d H:i:s') . '暂无跨链交易计划' . PHP_EOL; echo date('Y-m-d H:i:s') . '暂无跨链交易计划' . PHP_EOL;
return 0; return 0;
} }
//查询共识高度
$node_params = [
'scheme' => 'https',
'host' => 'jiedian1.bityuan.com',
'port' => 8801
];
$service = new Chain33Service($node_params);
$result = $service->getHeight();
$consensHeight = $result['result']['consensHeight'];
$current_time = time();
foreach ($model as $val) { foreach ($model as $val) {
$node_params = $val['transfer_url']; $node_params = $val['transfer_url'];
$node_params = explode(':', $node_params); $node_params = explode(':', $node_params);
...@@ -90,30 +107,53 @@ class CrossChainController extends Controller ...@@ -90,30 +107,53 @@ class CrossChainController extends Controller
$service = new Chain33Service($node_params); $service = new Chain33Service($node_params);
$send_result = $val['send_result']; $send_result = $val['send_result'];
$result = $service->QueryTransaction($send_result); $result = $service->QueryTransaction($send_result);
if (-1 == $result['code']) { $switch = true;
if('tx not exist' == $result['msg']) continue;
$msg = $result['msg'];
$query_result = $result['code'];
goto doEnd;
// echo date('Y-m-d H:i:s') . 'ID:' . $val['id'] . '查询错误' . PHP_EOL;
// continue;
}
if (isset($result['result']['actionName']) && 'unknown' == $result['result']['actionName']) { if (isset($result['result']['actionName']) && 'unknown' == $result['result']['actionName']) {
$redis->hdel(CoinCrossChain::CROSS_CHAIN_CACHE_FAIL, $val['address'] . ':' . $val['is_with_draw']);
$query_result = 'success'; $query_result = 'success';
$msg = 'success'; $msg = 'success';
$height = isset($result['result']['height']) ? $result['result']['height'] : 0;
goto doEnd;
} else if (isset($result['result']['receipt']['ty']) && 2 == $result['result']['receipt']['ty']) { } else if (isset($result['result']['receipt']['ty']) && 2 == $result['result']['receipt']['ty']) {
$redis->hdel(CoinCrossChain::CROSS_CHAIN_CACHE_FAIL, $val['address'] . ':' . $val['is_with_draw']);
$query_result = 'success'; $query_result = 'success';
$msg = 'success'; $msg = 'success';
$height = isset($result['result']['height']) ? $result['result']['height'] : 0;
goto doEnd;
} else { } else {
$query_result = 'fail'; $cache_error_time = $redis->hget(CoinCrossChain::CROSS_CHAIN_CACHE_FAIL, $val['address'] . ':' . $val['is_with_draw']);
foreach ($result['result']['receipt']['logs'] as $log) { if (false == $cache_error_time) {
if (is_array($log['log'])) continue; $redis->hmset(CoinCrossChain::CROSS_CHAIN_CACHE_FAIL, $val['address'] . ':' . $val['is_with_draw'], $current_time);
$msg = isset($log['log']) ? $log['log'] : '查询错误'; continue;
}
if (($current_time - $cache_error_time) < 60) {
continue;
}
$redis->hdel(CoinCrossChain::CROSS_CHAIN_CACHE_FAIL, $val['address'] . ':' . $val['is_with_draw']);
if (-1 == $result['code']) {
$msg = $result['msg'];
$query_result = $result['code'];
$switch = false;
goto doEnd;
} else {
$query_result = 'fail';
foreach ($result['result']['receipt']['logs'] as $log) {
if (is_array($log['log'])) continue;
$msg = isset($log['log']) ? $log['log'] : '查询错误';
}
$switch = false;
goto doEnd;
} }
} }
doEnd : doEnd :
if (CoinCrossChain::RECHARGE == $val['is_with_draw'] && $val['step'] == 2 && $switch && isset($height) && ($consensHeight < $height)) {
continue;
}
$currentModel = CoinCrossChain::findOne($val['id']); $currentModel = CoinCrossChain::findOne($val['id']);
$currentModel->query_result = $query_result; $currentModel->query_result = $query_result;
$currentModel->msg = $msg; $currentModel->msg = $msg;
...@@ -121,7 +161,6 @@ class CrossChainController extends Controller ...@@ -121,7 +161,6 @@ class CrossChainController extends Controller
$count = CoinCrossChain::find()->where(['transfer_number' => $val['transfer_number']])->andWhere(['query_result' => 'success'])->count(); $count = CoinCrossChain::find()->where(['transfer_number' => $val['transfer_number']])->andWhere(['query_result' => 'success'])->count();
if (3 == $count) { if (3 == $count) {
$redis = Yii::$app->redis;
$key = $val['address'] . '_' . $val['is_with_draw']; $key = $val['address'] . '_' . $val['is_with_draw'];
if ($redis->exists($key)) { if ($redis->exists($key)) {
$redis->del($key); $redis->del($key);
......
#!/bin/bash
while true; do php /var/www/html/token_pwallet/yii cross-chain/auto-transfer >> /var/www/html/auto-transfer.log 2>&1; sleep 15; done &
\ No newline at end of file
#!/bin/bash
while true; do php /var/www/html/token_pwallet/yii game-bet/bet-update >> /var/log/bet-update.log 2>&1; sleep 2; done &
\ No newline at end of file
#!/bin/bash
while true; do php /var/www/html/token_pwallet/yii game-bet/game-status >> /var/log/game-status.log 2>&1; sleep 5; done &
\ No newline at end of file
#!/bin/bash
while true; do php /var/www/html/token_pwallet/yii cross-chain/query-transaction >> /var/www/html/query-transaction.log 2>&1; sleep 5; done &
\ No newline at end of file
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