Skip to content
Permalink
Browse files

add reference implementation of BTCPool dependent Web API

  • Loading branch information...
SwimmingTiger committed Feb 21, 2019
1 parent d3b83fc commit fc0a60491f98a7e1d5aee820d8f096e4d76b3da1
@@ -480,6 +480,9 @@ $ curl http://xxx.xxx.xxx/GetUserList?last_id=8

There is a demo for the API: https://github.com/btccom/btcpool/issues/16#issuecomment-278245381

There are more reference implementations here, including a user list with automatic registration enabled
and a user list with `POOL__USER_DEFINED_COINBASE=ON`: [install/api-sample](../install/api-sample)

### use `supervisor` to manager services.

```
@@ -0,0 +1,51 @@
Reference implementation of BTCPool dependent Web API
==================



`users.list_id_api_url` in [sserver.cfg](../src/sserver/sserver.cfg)
------------------

### Static User List

[userlist.php](userlist.php)


### User List with Automatic Registration Enabled

[userlist-autoreg.php](userlist-autoreg.php)

#### `UserAutoRegAPI.URL` in [Init User Coin](https://github.com/btccom/btcpool-go-modules/tree/master/initUserCoin)

It is used with the `userlist-autoreg.php` above.

[autoreg.php](autoreg.php)


### User List with `POOL__USER_DEFINED_COINBASE=ON`

[user-list-coinbase-withtime](user-list-coinbase-withtime)



`UserCoinMapURL` in [Switcher API Server](https://github.com/btccom/btcpool-go-modules/tree/master/switcherAPIServer)
------------------

[usercoin.php](usercoin.php)



How to quickly create a web server based on the script?
------------------

You can use `PHP-CLI` to quickly create a web server. Suppose you are using Ubuntu or Debian:
```
mkdir -p /var/www/html
cp *.php /var/www/html
apt install php-cli
php -S localhost:8000 -t /var/www/html
```

Then you can set `list_id_api_url ="http://localhost:8000/userlist.php"`.

Note: the performance of PHP-CLI's web server is very poor, don't use it in a production environment.
@@ -0,0 +1,42 @@
<?php
# A demo for `UserAutoRegAPI.URL` in `btcpool-go-modules/initUserCoin/config.json`.
# Registered users will be saved in `userlist.json`.
error_reporting(E_ALL & ~E_NOTICE & ~E_WARNING);
header('Content-Type: application/json');
$data = json_decode(file_get_contents('php://input'), true);
$name = strtolower(trim($data['sub_name'] ?? ''));
if (empty($name)) {
echo json_encode(
[
'status' => 'failed',
'message' => 'sub_name cannot be empty',
'data' => null
]
);
return;
}
$file = __DIR__.'/userlist.json';
$users = json_decode(file_get_contents($file), true) ?? [];
if (isset($users[$name])) {
$puid = $users[$name];
}
else {
$puid = max(array_values($users)) + 1;
$users[$name] = $puid;
file_put_contents($file, json_encode($users));
}
echo json_encode(
[
'status' => 'success',
'message' => 'success',
'data' => [
'puid' => $puid
]
]
);
@@ -0,0 +1,47 @@
<?php
# A demo for `users.list_id_api_url` in `sserver.cfg` with `POOL__USER_DEFINED_COINBASE=ON`.
# User-defined coinbase will be updated randomly.
header('Content-Type: application/json');
$last_id = (int) $_GET['last_id'];
$last_time = (int) $_GET['last_time'];
$users = [
'user1' => ['puid'=>1, 'coinbase'=>str_shuffle('hiuser1')],
'user2' => ['puid'=>2, 'coinbase'=>str_shuffle('hiuser2')],
'user3' => ['puid'=>3, 'coinbase'=>str_shuffle('hiuser3')],
'user4' => ['puid'=>4, 'coinbase'=>str_shuffle('hiuser4')],
'user5' => ['puid'=>5, 'coinbase'=>str_shuffle('hiuser5')],
];
$time = time();
$times = [
'user1' => rand($time-100, $time+20),
'user2' => rand($time-100, $time+20),
'user3' => rand($time-100, $time+20),
'user4' => rand($time-100, $time+20),
'user5' => rand($time-100, $time+20),
];
$echoUsers = [];
foreach ($users as $uname=>$value) {
$t = $times[$uname];
if ($value['puid'] > $last_id || $t > $last_time) {
$echoUsers[$uname] = $value;
}
}
echo json_encode(
[
'err_no' => 0,
'err_msg' => null,
'data' => [
'users' => $echoUsers,
'time' => $time,
]
]
);
@@ -0,0 +1,28 @@
<?php
# A demo for `UserCoinMapURL` in `btcpool-go-modules/switcherAPIServer/config.json`.
# The coin of users mining will be updated randomly.
header('Content-Type: application/json');
$last_id = (int) $_GET['last_id'];
$coins = ["btc", "bcc"];
$users = [
'hu60' => $coins[rand(0,1)],
'YihaoTest' => $coins[rand(0,1)],
'YihaoTest3' => $coins[rand(0,1)],
'testpool' => $coins[rand(0,1)],
];
if ($last_id >= count($users)) {
$users = [];
}
echo json_encode(
[
'err_no' => 0,
'err_msg' => null,
'data' => (object) $users,
]
);
@@ -0,0 +1,25 @@
<?php
# A demo for `users.list_id_api_url` in `sserver.cfg` with automatic registration enabled.
# It needs to be used with `autoreg.php`.
# Users in `userlist.json` will be read. This file is generated by `autoreg.php`.
header('Content-Type: application/json');
$last_id = (int) $_GET['last_id'];
$users = json_decode(file_get_contents(__DIR__.'/userlist.json'), true);
$requestedUsers = [];
foreach ($users as $name=>$id) {
if ($id > $last_id) {
$requestedUsers [$name] = $id;
}
}
echo json_encode(
[
'err_no' => 0,
'err_msg' => null,
'data' => (object) $requestedUsers,
]
);
@@ -0,0 +1,39 @@
<?php
# A demo for `users.list_id_api_url` in `sserver.cfg` with static user list.
header('Content-Type: application/json');
$last_id = (int) $_GET['last_id'];
$users = [
'aaa' => 1,
'bbb' => 2,
'user3' => 3,
'mmm' => 4,
'user5' => 5,
'ddd' => 6,
'hu60' => 7,
'taaa' => 31,
'tbbb' => 32,
'tuser3' => 33,
'tmmm' => 34,
'tuser5' => 35,
'tddd' => 36,
'thu60' => 37,
'daad_ltc' => 41,
];
$requestedUsers = [];
foreach ($users as $name=>$id) {
if ($id > $last_id) {
$requestedUsers [$name] = $id;
}
}
echo json_encode(
[
'err_no' => 0,
'err_msg' => null,
'data' => (object) $requestedUsers,
]
);

0 comments on commit fc0a604

Please sign in to comment.
You can’t perform that action at this time.