Fixes to TON

This commit is contained in:
Daniil Gentili 2019-12-14 18:31:28 +01:00
parent 4917fafd3a
commit 1edcecb26b
Signed by: danog
GPG Key ID: 8C1BE3B34B230CA7
14 changed files with 274 additions and 22 deletions

View File

@ -38,6 +38,11 @@ abstract class AbstractAPIFactory extends AsyncConstruct
$this->async = &$async;
}
public function async($async)
{
$this->async = $async;
}
public function __call($name, $arguments)
{
$yielded = Tools::call($this->__call_async($name, $arguments));

View File

@ -86,7 +86,7 @@ trait TD
return $params;
}
if (!isset($params['_'])) {
\array_walk($params, [$this, 'mtproto_to_td']);
\array_walk($params, [$this, 'mtprotoToTd']);
return $params;
}

View File

@ -211,11 +211,7 @@ class TL
if (\strpos($line, ' ?= ') !== false) {
continue;
}
$name = \preg_replace(['/#.*/', '/\\s.*/'], '', $line);
if (\in_array($name, ['bytes', 'int128', 'int256', 'int512', 'int', 'long', 'double', 'string', 'bytes', 'object', 'function'])) {
continue;
}
$line = \preg_replace('/[(]([\w\.]+) ([\w\.]+)[)]/', '$1<$2>', $line);
$line = \preg_replace(['/[(]([\w\.]+) ([\w\.]+)[)]/', '/\s+/'], ['$1<$2>', ' '], $line);
if (\strpos($line, ';') === false) {
$lineBuf .= $line;
continue;
@ -225,7 +221,11 @@ class TL
$lineBuf = '';
}
$clean = \preg_replace(['/:bytes /', '/;/', '/#[a-f0-9]+ /', '/ [a-zA-Z0-9_]+\\:flags\\.[0-9]+\\?true/', '/[<]/', '/[>]/', '/ /', '/^ /', '/ $/', '/\\?bytes /', '/{/', '/}/', '/\s+/'], [':string ', '', ' ', '', ' ', ' ', ' ', '', '', '?string ', '', '', ' '], $line);
$name = \preg_replace(['/#.*/', '/\\s.*/'], '', $line);
if (\in_array($name, ['bytes', 'int128', 'int256', 'int512', 'int', 'long', 'double', 'string', 'bytes', 'object', 'function'])) {
continue;
}
$clean = \preg_replace(['/:bytes /', '/;/', '/#[a-f0-9]+ /', '/ [a-zA-Z0-9_]+\\:flags\\.[0-9]+\\?true/', '/[<]/', '/[>]/', '/ /', '/^ /', '/ $/', '/\\?bytes /', '/{/', '/}/'], [':string ', '', ' ', '', ' ', ' ', ' ', '', '', '?string ', '', ''], $line);
$id = \hash('crc32b', $clean);
if (\preg_match('/^[^\s]+#([a-f0-9]*)/i', $line, $matches)) {
@ -262,6 +262,12 @@ class TL
$TL_dict[$type][$key]['params'][] = ['name' => $explode[0], 'type' => $explode[1]];
}
}
/*
if (!$TL_dict[$type][$key][$type === 'constructors' ? 'predicate' : 'method']) {
var_dump($line);
\var_dump($TL_dict[$type][$key]);
}*/
$key++;
}
} else {
@ -442,21 +448,30 @@ class TL
return \danog\MadelineProto\Tools::packSignedLong($object);
case 'int128':
if (\strlen($object) !== 16) {
$object = \base64_decode($object);
if (\strlen($object) !== 16) {
throw new Exception(\danog\MadelineProto\Lang::$current_lang['long_not_16']);
}
}
return (string) $object;
case 'int256':
if (\strlen($object) !== 32) {
$object = \base64_decode($object);
if (\strlen($object) !== 32) {
throw new Exception(\danog\MadelineProto\Lang::$current_lang['long_not_32']);
}
}
return (string) $object;
case 'int512':
if (\strlen($object) !== 64) {
$object = \base64_decode($object);
if (\strlen($object) !== 64) {
throw new Exception(\danog\MadelineProto\Lang::$current_lang['long_not_64']);
}
}
return (string) $object;
case 'double':
@ -861,6 +876,8 @@ class TL
*/
public function deserialize($stream, $type = ['type' => ''])
{
//var_dump($type);
if (\is_string($stream)) {
$res = \fopen('php://memory', 'rw+b');
\fwrite($res, $stream);
@ -965,6 +982,8 @@ class TL
}
}
}
//var_dump($constructorData);
if ($constructorData['predicate'] === 'gzip_packed') {
if (!isset($type['subtype'])) {
$type['subtype'] = '';

View File

@ -19,13 +19,25 @@
namespace danog\MadelineProto\TON;
use danog\MadelineProto\Magic;
/**
* TON API.
*/
class API extends InternalDoc
{
public function __construct()
/**
* Construct API
*
* @param array $settings Settings
*/
public function __construct(array $settings)
{
Magic::classExists();
$this->API = new Lite($settings);
foreach (\get_class_methods($this->API) as $method) {
$this->methods[$method] = [$this->API, strtolower($method)];
}
}
}

View File

@ -77,4 +77,26 @@ class APIFactory extends AbstractAPIFactory
* @var liteServer
*/
public $liteServer;
/**
* Just proxy async requests to API
*
* @param string $name Method name
* @param array $arguments Arguments
*
* @return mixed
*/
public function __call_async($name, $arguments)
{
$lower_name = \strtolower($name);
if ($this->namespace !== '' || !isset($this->methods[$lower_name])) {
$name = $this->namespace.$name;
$aargs = isset($arguments[1]) && \is_array($arguments[1]) ? $arguments[1] : [];
$aargs['apifactory'] = true;
$args = isset($arguments[0]) && \is_array($arguments[0]) ? $arguments[0] : [];
return $this->API->methodCallAsyncRead($name, $args, $aargs);
}
return $this->methods[$lower_name](...$arguments);
}
}

View File

@ -863,8 +863,26 @@ interface engine
class InternalDoc extends APIFactory
{
public function test(array $extra = [])
/**
* Connect to the lite endpoints specified in the config file.
*
* @param string $config Path to config file
*
* @return \Generator
*/
public function connect(string $config, array $extra = [])
{
return $this->__call(__FUNCTION__, [$extra]);
return $this->__call(__FUNCTION__, [$config, $extra]);
}
/**
* Asynchronously run async callable.
*
* @param callable $func Function
*
* @return \Generator
*/
public function loop(callable $func, array $extra = [])
{
return $this->__call(__FUNCTION__, [$func, $extra]);
}
}

View File

@ -0,0 +1,120 @@
<?php
/**
* TON API module.
*
* This file is part of MadelineProto.
* MadelineProto is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
* MadelineProto is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU Affero General Public License for more details.
* You should have received a copy of the GNU General Public License along with MadelineProto.
* If not, see <http://www.gnu.org/licenses/>.
*
* @author Daniil Gentili <daniil@daniil.it>
* @copyright 2016-2019 Daniil Gentili <daniil@daniil.it>
* @license https://opensource.org/licenses/AGPL-3.0 AGPLv3
*
* @link https://docs.madelineproto.xyz MadelineProto documentation
*/
namespace danog\MadelineProto\TON;
use danog\MadelineProto\Logger;
use danog\MadelineProto\TL\TL;
use function Amp\File\get;
/**
* TON API.
*/
class Lite
{
/**
* Lite client config.
*
* @var array
*/
private $config;
/**
* Misc settings.
*
* @var array
*/
private $settings = [];
/**
* TL serializer instance.
*
* @var \danog\MadelineProto\TL\TL
*/
private $TL;
/**
* Logger instance.
*
* @var Logger
*/
public $logger;
/**
* Construct settings.
*
* @param array $settings
*/
public function __construct(array $settings)
{
$this->settings = $settings;
$this->logger = Logger::getLoggerFromSettings($this->settings);
$this->TL = new TL($this);
$this->TL->init(
[
'lite_api' => __DIR__.'/schemes/lite_api.tl',
'ton_api' => __DIR__.'/schemes/ton_api.tl',
]
);
}
/**
* Connect to the lite endpoints specified in the config file.
*
* @param string $config Path to config file
*
* @return \Generator
*/
public function connect(string $config): \Generator
{
$config = \json_decode(yield get($config), true);
$config['_'] = 'liteclient.config.global';
$config = Tools::convertJsonTL($config);
$config['validator']['init_block'] = $config['validator']['init_block'] ?? $config['validator']['zero_state'];
$this->config = yield $this->TL->deserialize(
yield $this->TL->serializeObject(
['type' => ''],
$config,
'cleanup'
)
);
var_dump($this->config);
}
/**
* Asynchronously run async callable.
*
* @param callable $func Function
*
* @return \Generator
*/
public function loop(callable $func): \Generator
{
return yield $func();
}
/**
* Convert parameters.
*
* @param array $parameters Parameters
*
* @return void
*/
public function botAPItoMTProto(array $parameters)
{
return $parameters;
}
}

View File

@ -1,7 +1,7 @@
<?php
/**
* TON API module.
* TON tools module.
*
* This file is part of MadelineProto.
* MadelineProto is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
@ -19,12 +19,25 @@
namespace danog\MadelineProto\TON;
/**
* TON API.
*/
class ADNL
class Tools
{
public function test()
/**
* Sanify TL obtained from JSON for TL serialization.
*
* @param array $input Data to sanitize
* @return array
*/
public static function convertJsonTL(array $input): array
{
$cb = static function (&$val) use (&$cb) {
if (isset($val['@type'])) {
$val['_'] = $val['@type'];
} elseif (\is_array($val)) {
\array_walk($val, $cb);
}
};
\array_walk($input, $cb);
return $input;
}
}

21
ton/lite-client.php Normal file
View File

@ -0,0 +1,21 @@
<?php
use danog\MadelineProto\Logger;
use danog\MadelineProto\TON\API;
require 'vendor/autoload.php';
$API = new API(
[
'logger' => [
'logger' => Logger::ECHO_LOGGER
]
]
);
$API->async(true);
$API->loop(
function () use ($API) {
yield $API->connect(__DIR__.'/ton-lite-client-test1.config.json');
}
);

View File

@ -0,0 +1,22 @@
{
"liteservers": [
{
"ip": 1137658550,
"port": 4924,
"id": {
"@type": "pub.ed25519",
"key": "peJTw/arlRfssgTuf9BMypJzqOi7SXEqSPSWiEw2U1M="
}
}
],
"validator": {
"@type": "validator.config.global",
"zero_state": {
"workchain": -1,
"shard": -9223372036854775808,
"seqno": 0,
"root_hash": "F6OpKZKqvqeFp6CQmFomXNMfMj2EnaUSOXN+Mh+wVWk=",
"file_hash": "XplPz01CXAps5qeSWUtxcyBfdAo5zVb1N979KLSKD24="
}
}
}

View File

@ -100,9 +100,9 @@ $doc->mkAnnotations();
$ton = [
'tl_schema' => [
'lite_api' => "$d/src/danog/MadelineProto/TON/lite_api.tl",
'ton_api' => "$d/src/danog/MadelineProto/TON/ton_api.tl",
//'tonlib_api' => "$d/src/danog/MadelineProto/TON/tonlib_api.tl",
'lite_api' => "$d/src/danog/MadelineProto/TON/schemes/lite_api.tl",
'ton_api' => "$d/src/danog/MadelineProto/TON/schemes/ton_api.tl",
//'tonlib_api' => "$d/src/danog/MadelineProto/TON/schemes/tonlib_api.tl",
]
];