Commit 9e44df21 authored by shajiaiming's avatar shajiaiming

Merge branch 'feature/cross_chain' into 'master'

Feature/cross chain See merge request !112
parents 80f2bde6 12df6a3f
......@@ -298,6 +298,15 @@ class Chain33Service
return $this->send($params, 'Chain33.SignRawTx');
}
public function getBlock2MainInfo($start, $end)
{
$params = [
"Start" => $start,
"End" => $end,
];
return $this->send($params, 'paracross.GetBlock2MainInfo');
}
public function sendTrade($data)
{
$params = [
......
......@@ -4,6 +4,7 @@ namespace console\controllers;
use common\business\Chain33Business;
use common\models\psources\CoinGameBet;
use common\service\chain33\Chain33Service;
use yii\console\Controller;
use Yii;
......@@ -17,48 +18,46 @@ class GameBetController extends Controller
public function actionGameStatus()
{
$nodes = \Yii::$app->params['chain_parallel']['wasm'];
if(empty($nodes)){
echo date('Y-m-d H:i:s') . '无节点'.PHP_EOL;
if (empty($nodes)) {
echo date('Y-m-d H:i:s') . '无节点' . PHP_EOL;
return 0;
}
foreach ($nodes as $key => $node) {
$service = new Chain33Business();
$result = $service->getGameStatus($node);
if (0 !== $result['code']) {
echo $key.':'.date('Y-m-d H:i:s') . $result['msg'].PHP_EOL;
echo $key . ':' . date('Y-m-d H:i:s') . $result['msg'] . PHP_EOL;
continue;
}
$queryResultItems = $result['result'] ?? [];
if (empty($queryResultItems)) {
echo $key.':'.date('Y-m-d H:i:s') . 'error'.PHP_EOL;
echo $key . ':' . date('Y-m-d H:i:s') . 'error' . PHP_EOL;
continue;
}
$resultJSON = json_decode($queryResultItems['queryResultItems'][0]['resultJSON'], true);
$current_round = $resultJSON['current_round'];
$current_height = $resultJSON['height'];
$cache_current_round = Yii::$app->redis->get('chain33_game_bet_status_'.$key);
$cache_current_round = Yii::$app->redis->get('chain33_game_bet_status_' . $key);
if (empty($cache_current_round)) {
$cache_current_round = CoinGameBet::find()->where(['platform' => $key])->max('round');
Yii::$app->redis->set('chain33_game_bet_status_'.$key, $cache_current_round, 'EX', 300);
Yii::$app->redis->set('chain33_game_bet_status_' . $key, $cache_current_round, 'EX', 300);
}
$cache_current_round = (false == $cache_current_round ? 0 : $cache_current_round);
if ($cache_current_round >= $current_round) {
echo $key.':'.date('Y-m-d H:i:s') . '数据已为最新' . PHP_EOL;
echo $key . ':' . date('Y-m-d H:i:s') . '数据已为最新' . PHP_EOL;
continue;
}
Yii::$app->redis->set('chain33_game_bet_status_'.$key, $current_round, 'EX', 300);
Yii::$app->redis->set('chain33_game_bet_status_height_'.$key, $current_height, 'EX', 300);
Yii::$app->redis->set('chain33_game_bet_status_' . $key, $current_round, 'EX', 300);
$result = $service->getBetStatus($cache_current_round, $current_round, '', $node);
if (0 !== $result['code']) {
echo $key.':'.date('Y-m-d H:i:s') . '数据错误' . PHP_EOL;
echo $key . ':' . date('Y-m-d H:i:s') . '数据错误' . PHP_EOL;
continue;
}
$queryResultItems = $result['result'] ?? [];
if (empty($queryResultItems)) {
echo $key.':'.date('Y-m-d H:i:s') . '数据错误' . PHP_EOL;
echo $key . ':' . date('Y-m-d H:i:s') . '数据错误' . PHP_EOL;
continue;
}
$platform = $key;
......@@ -70,7 +69,7 @@ class GameBetController extends Controller
];
}
CoinGameBet::loadArray($datas);
echo $platform.':'.date('Y-m-d H:i:s') . '数据更新成功'.PHP_EOL;
echo $platform . ':' . date('Y-m-d H:i:s') . '数据更新成功' . PHP_EOL;
continue;
}
return 0;
......@@ -78,44 +77,66 @@ class GameBetController extends Controller
public function actionBetUpdate()
{
$service = new Chain33Business();
$node_params = [
'scheme' => 'https',
'host' => 'jiedian1.bityuan.com',
'port' => 8801
];
$result = $service->getLastHeader($node_params);
$main_height = isset($result['result']['height']) ? $result['result']['height'] : 0;
$safe_main_height = $main_height - 14;
$nodes = \Yii::$app->params['chain_parallel']['wasm'];
if(empty($nodes)){
echo date('Y-m-d H:i:s') . '无节点'.PHP_EOL;
if (empty($nodes)) {
echo date('Y-m-d H:i:s') . '无节点' . PHP_EOL;
return 0;
}
foreach ($nodes as $key => $node) {
$service = new Chain33Business();
$result = $service->getLastHeader($node);
$service = new Chain33Service($node);
$result = $service->getLastHeader();
$height = $result['result']['height'];
$main_info = $service->getBlock2MainInfo($height - 14, $height);
if (!isset($main_info['result']['items'])) continue;
$items = $main_info['result']['items'];
$items = $this->arraySort($items, 'mainHeight');
$safe_height = 0;
foreach ($items as $item) {
if ($safe_main_height > $item['mainHeight']) {
$safe_height = $item['height'];
break;
}
}
echo $key . ':' . date('Y-m-d H:i:s') . '当前主链高度为:' . $main_height . ';当前平行链高度为:' . $height . ';当前安全高度为:' . $safe_height . PHP_EOL;
$models = CoinGameBet::find()->select('round')->where([
'and',
['valid' => CoinGameBet::VAILD_FALSE],
['<', 'height', $height - 12],
['<', 'height', $safe_height],
['platform' => $key]
])->all();
if(empty($models)){
echo $key.':'.date('Y-m-d H:i:s') . '无需更新的数据'.PHP_EOL;
if (empty($models)) {
echo $key . ':' . date('Y-m-d H:i:s') . '无需更新的数据' . PHP_EOL;
continue;
}
$valid_arr = [];
foreach ($models as $model) {
$valid_arr[] = $model->round;
}
$result = $service->getBetStatus('', '', $valid_arr, $node);
if( 0 !== $result['code']){
echo $key.':'.date('Y-m-d H:i:s') . '数据错误'.PHP_EOL;
$business = new Chain33Business();
$result = $business->getBetStatus('', '', $valid_arr, $node);
if (0 !== $result['code']) {
echo $key . ':' . date('Y-m-d H:i:s') . '数据错误' . PHP_EOL;
continue;
}
$queryResultItems = $result['result'] ?? [];
if(empty($queryResultItems)){
echo $key.':'.date('Y-m-d H:i:s') . '数据错误'.PHP_EOL;
if (empty($queryResultItems)) {
echo $key . ':' . date('Y-m-d H:i:s') . '数据错误' . PHP_EOL;
continue;
}
$platform = $key;
foreach ($queryResultItems['queryResultItems'] as $key => $val){
foreach ($queryResultItems['queryResultItems'] as $key => $val) {
if (false == $val['found']) continue;
$resultArr = json_decode($val['resultJSON'],true);
$resultArr = json_decode($val['resultJSON'], true);
CoinGameBet::updateAll([
'amount' => $resultArr['amount'],
'height' => $resultArr['height'],
......@@ -123,15 +144,25 @@ class GameBetController extends Controller
'rand_num' => $resultArr['rand_num'],
'player_win' => $resultArr['player_win'],
'valid' => CoinGameBet::VAILD_TRUE
],[
], [
'round' => $resultArr['round'],
'platform' => $platform
]);
}
echo $platform.':'.date('Y-m-d H:i:s') . '数据更新成功'.PHP_EOL;
echo $platform . ':' . date('Y-m-d H:i:s') . '数据更新成功' . PHP_EOL;
continue;
}
return 0;
}
protected function arraySort($array, $keys, $sort = SORT_DESC)
{
$keysValue = [];
foreach ($array as $k => $v) {
$keysValue[$k] = $v[$keys];
}
array_multisort($keysValue, $sort, $array);
return $array;
}
}
\ 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