aboutsummaryrefslogtreecommitdiff
path: root/tests/functional_tests
diff options
context:
space:
mode:
authormoneromooo-monero <moneromooo-monero@users.noreply.github.com>2018-02-11 15:15:56 +0000
committermoneromooo-monero <moneromooo-monero@users.noreply.github.com>2019-10-25 09:34:38 +0000
commit2899379791b7542e4eb920b5d9d58cf232806937 (patch)
tree7e97a95103837852d5c4e86ee544815d9a7a6671 /tests/functional_tests
parentadd a quick early out to get_blocks.bin when up to date (diff)
downloadmonero-2899379791b7542e4eb920b5d9d58cf232806937.tar.xz
daemon, wallet: new pay for RPC use system
Daemons intended for public use can be set up to require payment in the form of hashes in exchange for RPC service. This enables public daemons to receive payment for their work over a large number of calls. This system behaves similarly to a pool, so payment takes the form of valid blocks every so often, yielding a large one off payment, rather than constant micropayments. This system can also be used by third parties as a "paywall" layer, where users of a service can pay for use by mining Monero to the service provider's address. An example of this for web site access is Primo, a Monero mining based website "paywall": https://github.com/selene-kovri/primo This has some advantages: - incentive to run a node providing RPC services, thereby promoting the availability of third party nodes for those who can't run their own - incentive to run your own node instead of using a third party's, thereby promoting decentralization - decentralized: payment is done between a client and server, with no third party needed - private: since the system is "pay as you go", you don't need to identify yourself to claim a long lived balance - no payment occurs on the blockchain, so there is no extra transactional load - one may mine with a beefy server, and use those credits from a phone, by reusing the client ID (at the cost of some privacy) - no barrier to entry: anyone may run a RPC node, and your expected revenue depends on how much work you do - Sybil resistant: if you run 1000 idle RPC nodes, you don't magically get more revenue - no large credit balance maintained on servers, so they have no incentive to exit scam - you can use any/many node(s), since there's little cost in switching servers - market based prices: competition between servers to lower costs - incentive for a distributed third party node system: if some public nodes are overused/slow, traffic can move to others - increases network security - helps counteract mining pools' share of the network hash rate - zero incentive for a payer to "double spend" since a reorg does not give any money back to the miner And some disadvantages: - low power clients will have difficulty mining (but one can optionally mine in advance and/or with a faster machine) - payment is "random", so a server might go a long time without a block before getting one - a public node's overall expected payment may be small Public nodes are expected to compete to find a suitable level for cost of service. The daemon can be set up this way to require payment for RPC services: monerod --rpc-payment-address 4xxxxxx \ --rpc-payment-credits 250 --rpc-payment-difficulty 1000 These values are an example only. The --rpc-payment-difficulty switch selects how hard each "share" should be, similar to a mining pool. The higher the difficulty, the fewer shares a client will find. The --rpc-payment-credits switch selects how many credits are awarded for each share a client finds. Considering both options, clients will be awarded credits/difficulty credits for every hash they calculate. For example, in the command line above, 0.25 credits per hash. A client mining at 100 H/s will therefore get an average of 25 credits per second. For reference, in the current implementation, a credit is enough to sync 20 blocks, so a 100 H/s client that's just starting to use Monero and uses this daemon will be able to sync 500 blocks per second. The wallet can be set to automatically mine if connected to a daemon which requires payment for RPC usage. It will try to keep a balance of 50000 credits, stopping mining when it's at this level, and starting again as credits are spent. With the example above, a new client will mine this much credits in about half an hour, and this target is enough to sync 500000 blocks (currently about a third of the monero blockchain). There are three new settings in the wallet: - credits-target: this is the amount of credits a wallet will try to reach before stopping mining. The default of 0 means 50000 credits. - auto-mine-for-rpc-payment-threshold: this controls the minimum credit rate which the wallet considers worth mining for. If the daemon credits less than this ratio, the wallet will consider mining to be not worth it. In the example above, the rate is 0.25 - persistent-rpc-client-id: if set, this allows the wallet to reuse a client id across runs. This means a public node can tell a wallet that's connecting is the same as one that connected previously, but allows a wallet to keep their credit balance from one run to the other. Since the wallet only mines to keep a small credit balance, this is not normally worth doing. However, someone may want to mine on a fast server, and use that credit balance on a low power device such as a phone. If left unset, a new client ID is generated at each wallet start, for privacy reasons. To mine and use a credit balance on two different devices, you can use the --rpc-client-secret-key switch. A wallet's client secret key can be found using the new rpc_payments command in the wallet. Note: anyone knowing your RPC client secret key is able to use your credit balance. The wallet has a few new commands too: - start_mining_for_rpc: start mining to acquire more credits, regardless of the auto mining settings - stop_mining_for_rpc: stop mining to acquire more credits - rpc_payments: display information about current credits with the currently selected daemon The node has an extra command: - rpc_payments: display information about clients and their balances The node will forget about any balance for clients which have been inactive for 6 months. Balances carry over on node restart.
Diffstat (limited to 'tests/functional_tests')
-rw-r--r--tests/functional_tests/CMakeLists.txt14
-rwxr-xr-xtests/functional_tests/blockchain.py2
-rwxr-xr-xtests/functional_tests/daemon_info.py3
-rwxr-xr-xtests/functional_tests/functional_tests_rpc.py19
-rw-r--r--tests/functional_tests/make_test_signature.cc60
-rwxr-xr-xtests/functional_tests/mining.py2
-rwxr-xr-xtests/functional_tests/rpc_payment.py412
7 files changed, 506 insertions, 6 deletions
diff --git a/tests/functional_tests/CMakeLists.txt b/tests/functional_tests/CMakeLists.txt
index bc55da9e3..702ccc646 100644
--- a/tests/functional_tests/CMakeLists.txt
+++ b/tests/functional_tests/CMakeLists.txt
@@ -50,6 +50,20 @@ target_link_libraries(functional_tests
${CMAKE_THREAD_LIBS_INIT}
${EXTRA_LIBRARIES})
+set(make_test_signature_sources
+ make_test_signature.cc)
+
+add_executable(make_test_signature
+ ${make_test_signature_sources})
+
+target_link_libraries(make_test_signature
+ PRIVATE
+ rpc_base
+ cncrypto
+ epee
+ ${CMAKE_THREAD_LIBS_INIT}
+ ${EXTRA_LIBRARIES})
+
execute_process(COMMAND ${PYTHON_EXECUTABLE} "-c" "import requests; print('OK')" OUTPUT_VARIABLE REQUESTS_OUTPUT OUTPUT_STRIP_TRAILING_WHITESPACE)
if (REQUESTS_OUTPUT STREQUAL "OK")
add_test(
diff --git a/tests/functional_tests/blockchain.py b/tests/functional_tests/blockchain.py
index 324af624a..78e0d8952 100755
--- a/tests/functional_tests/blockchain.py
+++ b/tests/functional_tests/blockchain.py
@@ -101,7 +101,7 @@ class BlockchainTest():
for n in range(blocks):
res_getblock.append(daemon.getblock(height = height + n))
block_header = res_getblock[n].block_header
- assert abs(block_header.timestamp - time.time()) < 10 # within 10 seconds
+ assert abs(block_header.timestamp - time.time()) < 60 # within 60 seconds
assert block_header.height == height + n
assert block_header.orphan_status == False
assert block_header.depth == blocks - n - 1
diff --git a/tests/functional_tests/daemon_info.py b/tests/functional_tests/daemon_info.py
index 4fa768b03..14fc14062 100755
--- a/tests/functional_tests/daemon_info.py
+++ b/tests/functional_tests/daemon_info.py
@@ -37,6 +37,7 @@ Test the following RPCs:
"""
from __future__ import print_function
+import os
from framework.daemon import Daemon
class DaemonGetInfoTest():
@@ -63,7 +64,7 @@ class DaemonGetInfoTest():
# difficulty should be set to 1 for this test
assert 'difficulty' in res.keys()
- assert res.difficulty == 1;
+ assert res.difficulty == int(os.environ['DIFFICULTY'])
# nettype should not be TESTNET
assert 'testnet' in res.keys()
diff --git a/tests/functional_tests/functional_tests_rpc.py b/tests/functional_tests/functional_tests_rpc.py
index 9043565c7..5f2a3d077 100755
--- a/tests/functional_tests/functional_tests_rpc.py
+++ b/tests/functional_tests/functional_tests_rpc.py
@@ -10,7 +10,7 @@ import string
import os
USAGE = 'usage: functional_tests_rpc.py <python> <srcdir> <builddir> [<tests-to-run> | all]'
-DEFAULT_TESTS = ['address_book', 'bans', 'blockchain', 'cold_signing', 'daemon_info', 'get_output_distribution', 'integrated_address', 'mining', 'multisig', 'proofs', 'sign_message', 'transfer', 'txpool', 'uri', 'validate_address', 'wallet']
+DEFAULT_TESTS = ['address_book', 'bans', 'blockchain', 'cold_signing', 'daemon_info', 'get_output_distribution', 'integrated_address', 'mining', 'multisig', 'proofs', 'rpc_payment', 'sign_message', 'transfer', 'txpool', 'uri', 'validate_address', 'wallet']
try:
python = sys.argv[1]
srcdir = sys.argv[2]
@@ -34,12 +34,19 @@ try:
except:
tests = DEFAULT_TESTS
-N_MONERODS = 1
+N_MONERODS = 2
N_WALLETS = 4
WALLET_DIRECTORY = builddir + "/functional-tests-directory"
+DIFFICULTY = 10
-monerod_base = [builddir + "/bin/monerod", "--regtest", "--fixed-difficulty", "1", "--offline", "--no-igd", "--p2p-bind-port", "monerod_p2p_port", "--rpc-bind-port", "monerod_rpc_port", "--zmq-rpc-bind-port", "monerod_zmq_port", "--non-interactive", "--disable-dns-checkpoints", "--check-updates", "disabled", "--rpc-ssl", "disabled", "--log-level", "1"]
+monerod_base = [builddir + "/bin/monerod", "--regtest", "--fixed-difficulty", str(DIFFICULTY), "--offline", "--no-igd", "--p2p-bind-port", "monerod_p2p_port", "--rpc-bind-port", "monerod_rpc_port", "--zmq-rpc-bind-port", "monerod_zmq_port", "--non-interactive", "--disable-dns-checkpoints", "--check-updates", "disabled", "--rpc-ssl", "disabled", "--log-level", "1"]
+monerod_extra = [
+ [],
+ ["--rpc-payment-address", "44SKxxLQw929wRF6BA9paQ1EWFshNnKhXM3qz6Mo3JGDE2YG3xyzVutMStEicxbQGRfrYvAAYxH6Fe8rnD56EaNwUiqhcwR", "--rpc-payment-difficulty", str(DIFFICULTY), "--rpc-payment-credits", "5000", "--data-dir", builddir + "/functional-tests-directory/monerod1"],
+]
wallet_base = [builddir + "/bin/monero-wallet-rpc", "--wallet-dir", WALLET_DIRECTORY, "--rpc-bind-port", "wallet_port", "--disable-rpc-login", "--rpc-ssl", "disabled", "--daemon-ssl", "disabled", "--daemon-port", "18180", "--log-level", "1"]
+wallet_extra = [
+]
command_lines = []
processes = []
@@ -48,11 +55,15 @@ ports = []
for i in range(N_MONERODS):
command_lines.append([str(18180+i) if x == "monerod_rpc_port" else str(18280+i) if x == "monerod_p2p_port" else str(18380+i) if x == "monerod_zmq_port" else x for x in monerod_base])
+ if i < len(monerod_extra):
+ command_lines[-1] += monerod_extra[i]
outputs.append(open(builddir + '/tests/functional_tests/monerod' + str(i) + '.log', 'a+'))
ports.append(18180+i)
for i in range(N_WALLETS):
command_lines.append([str(18090+i) if x == "wallet_port" else x for x in wallet_base])
+ if i < len(wallet_extra):
+ command_lines[-1] += wallet_extra[i]
outputs.append(open(builddir + '/tests/functional_tests/wallet' + str(i) + '.log', 'a+'))
ports.append(18090+i)
@@ -65,6 +76,8 @@ try:
os.environ['PYTHONPATH'] = PYTHONPATH
os.environ['WALLET_DIRECTORY'] = WALLET_DIRECTORY
os.environ['PYTHONIOENCODING'] = 'utf-8'
+ os.environ['DIFFICULTY'] = str(DIFFICULTY)
+ os.environ['MAKE_TEST_SIGNATURE'] = builddir + '/tests/functional_tests/make_test_signature'
for i in range(len(command_lines)):
#print('Running: ' + str(command_lines[i]))
processes.append(subprocess.Popen(command_lines[i], stdout = outputs[i]))
diff --git a/tests/functional_tests/make_test_signature.cc b/tests/functional_tests/make_test_signature.cc
new file mode 100644
index 000000000..8c0333233
--- /dev/null
+++ b/tests/functional_tests/make_test_signature.cc
@@ -0,0 +1,60 @@
+// Copyright (c) 2019, The Monero Project
+//
+// All rights reserved.
+//
+// Redistribution and use in source and binary forms, with or without modification, are
+// permitted provided that the following conditions are met:
+//
+// 1. Redistributions of source code must retain the above copyright notice, this list of
+// conditions and the following disclaimer.
+//
+// 2. Redistributions in binary form must reproduce the above copyright notice, this list
+// of conditions and the following disclaimer in the documentation and/or other
+// materials provided with the distribution.
+//
+// 3. Neither the name of the copyright holder nor the names of its contributors may be
+// used to endorse or promote products derived from this software without specific
+// prior written permission.
+//
+// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
+// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
+// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
+// THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
+// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
+// STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
+// THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+
+#include <stdio.h>
+#include "string_tools.h"
+#include "rpc/rpc_payment_signature.h"
+
+int main(int argc, const char **argv)
+{
+ if (argc > 2)
+ {
+ fprintf(stderr, "usage: %s <secret_key>\n", argv[0]);
+ return 1;
+ }
+
+ crypto::secret_key skey;
+
+ if (argc == 1)
+ {
+ crypto::public_key pkey;
+ crypto::random32_unbiased((unsigned char*)skey.data);
+ crypto::secret_key_to_public_key(skey, pkey);
+ printf("%s %s\n", epee::string_tools::pod_to_hex(skey).c_str(), epee::string_tools::pod_to_hex(pkey).c_str());
+ return 0;
+ }
+
+ if (!epee::string_tools::hex_to_pod(argv[1], skey))
+ {
+ fprintf(stderr, "invalid secret key\n");
+ return 1;
+ }
+ std::string signature = cryptonote::make_rpc_payment_signature(skey);
+ printf("%s\n", signature.c_str());
+ return 0;
+}
diff --git a/tests/functional_tests/mining.py b/tests/functional_tests/mining.py
index a08a45ad4..ad646417e 100755
--- a/tests/functional_tests/mining.py
+++ b/tests/functional_tests/mining.py
@@ -92,7 +92,7 @@ class MiningTest():
assert res_status.block_reward >= 600000000000
# wait till we mined a few of them
- timeout = 5
+ timeout = 60 # randomx is slow to init
timeout_height = prev_height
while True:
time.sleep(1)
diff --git a/tests/functional_tests/rpc_payment.py b/tests/functional_tests/rpc_payment.py
new file mode 100755
index 000000000..fa72f547a
--- /dev/null
+++ b/tests/functional_tests/rpc_payment.py
@@ -0,0 +1,412 @@
+#!/usr/bin/env python3
+
+# Copyright (c) 2019 The Monero Project
+#
+# All rights reserved.
+#
+# Redistribution and use in source and binary forms, with or without modification, are
+# permitted provided that the following conditions are met:
+#
+# 1. Redistributions of source code must retain the above copyright notice, this list of
+# conditions and the following disclaimer.
+#
+# 2. Redistributions in binary form must reproduce the above copyright notice, this list
+# of conditions and the following disclaimer in the documentation and/or other
+# materials provided with the distribution.
+#
+# 3. Neither the name of the copyright holder nor the names of its contributors may be
+# used to endorse or promote products derived from this software without specific
+# prior written permission.
+#
+# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
+# EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
+# MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
+# THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
+# PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
+# STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
+# THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+
+from __future__ import print_function
+import subprocess
+import os
+
+"""Test daemon RPC payment calls
+"""
+
+from framework.daemon import Daemon
+from framework.wallet import Wallet
+
+class RPCPaymentTest():
+ def run_test(self):
+ self.make_test_signature = os.environ['MAKE_TEST_SIGNATURE']
+ assert len(self.make_test_signature) > 0
+ self.secret_key, self.public_key = self.get_keys()
+ self.reset()
+ self.test_access_tracking()
+ self.test_access_mining()
+ self.test_access_payment()
+ self.test_access_account()
+ self.test_free_access()
+
+ def get_keys(self):
+ output = subprocess.check_output([self.make_test_signature]).rstrip()
+ fields = output.split()
+ assert len(fields) == 2
+ return fields
+
+ def get_signature(self):
+ return subprocess.check_output([self.make_test_signature, self.secret_key]).rstrip()
+
+ def reset(self):
+ print('Resetting blockchain')
+ daemon = Daemon(idx=1)
+ res = daemon.get_height()
+ daemon.pop_blocks(res.height - 1)
+ daemon.flush_txpool()
+
+ def test_access_tracking(self):
+ print('Testing access tracking')
+ daemon = Daemon(idx=1)
+
+ res = daemon.rpc_access_tracking(True)
+
+ res = daemon.rpc_access_tracking()
+ data = sorted(res.data, key = lambda k: k['rpc'])
+ assert len(data) == 1
+ entry = data[0]
+ assert entry.rpc == 'rpc_access_tracking'
+ assert entry.count == 1
+ assert entry.time >= 0
+ assert entry.credits == 0
+
+ daemon.get_connections()
+ res = daemon.rpc_access_tracking()
+ data = sorted(res.data, key = lambda k: k['rpc'])
+ assert len(data) == 2
+ entry = data[0]
+ assert entry.rpc == 'get_connections'
+ assert entry.count == 1
+ assert entry.time >= 0
+ assert entry.credits == 0
+
+ daemon.get_connections()
+ res = daemon.rpc_access_tracking()
+ data = sorted(res.data, key = lambda k: k['rpc'])
+ assert len(data) == 2
+ entry = data[0]
+ assert entry.rpc == 'get_connections'
+ assert entry.count == 2
+ assert entry.time >= 0
+ assert entry.credits == 0
+
+ daemon.get_alternate_chains()
+ res = daemon.rpc_access_tracking()
+ data = sorted(res.data, key = lambda k: k['rpc'])
+ assert len(data) == 3
+ entry = data[0]
+ assert entry.rpc == 'get_alternate_chains'
+ assert entry.count == 1
+ assert entry.time >= 0
+ assert entry.credits == 0
+ entry = res.data[1]
+ assert entry.rpc == 'get_connections'
+ assert entry.count == 2
+ assert entry.time >= 0
+ assert entry.credits == 0
+
+ res = daemon.rpc_access_tracking(True)
+ res = daemon.rpc_access_tracking()
+ data = sorted(res.data, key = lambda k: k['rpc'])
+ assert len(data) == 1
+ entry = data[0]
+ assert entry.rpc == 'rpc_access_tracking'
+ assert entry.count == 1
+
+ def test_access_mining(self):
+ print('Testing access mining')
+ daemon = Daemon(idx=1)
+ wallet = Wallet(idx=3)
+
+ res = daemon.rpc_access_info(client = self.get_signature())
+ assert len(res.hashing_blob) > 39
+ assert res.height == 1
+ assert res.top_hash == '418015bb9ae982a1975da7d79277c2705727a56894ba0fb246adaabb1f4632e3'
+ assert res.credits_per_hash_found == 5000
+ assert res.diff == 10
+ assert res.credits == 0
+ cookie = res.cookie
+
+ # Try random nonces till we find one that's valid and one that's invalid
+ nonce = 0
+ found_valid = 0
+ found_invalid = 0
+ last_credits = 0
+ while found_valid == 0 or found_invalid == 0:
+ nonce += 1
+ try:
+ res = daemon.rpc_access_submit_nonce(nonce = nonce, cookie = cookie, client = self.get_signature())
+ found_valid += 1
+ assert res.credits == last_credits + 5000
+ except Exception as e:
+ found_invalid += 1
+ res = daemon.rpc_access_info(client = self.get_signature())
+ assert res.credits < last_credits or res.credits == 0
+ assert nonce < 1000 # can't find both valid and invalid -> the RPC probably fails
+ last_credits = res.credits
+
+ # we should now have 1 valid nonce, and a number of bad ones
+ res = daemon.rpc_access_info(client = self.get_signature())
+ assert len(res.hashing_blob) > 39
+ assert res.height > 1
+ assert res.top_hash != '418015bb9ae982a1975da7d79277c2705727a56894ba0fb246adaabb1f4632e3' # here, any share matches network diff
+ assert res.credits_per_hash_found == 5000
+ assert res.diff == 10
+ cookie = res.cookie
+
+ res = daemon.rpc_access_data()
+ assert len(res.entries) > 0
+ e = [x for x in res.entries if x['client'] == self.public_key]
+ assert len(e) == 1
+ e = e[0]
+ assert e.nonces_stale == 0
+ assert e.nonces_bad == found_invalid
+ assert e.nonces_good == found_valid
+ assert e.nonces_dupe == 0
+
+ # Try random nonces till we find one that's valid so we get a load of credits
+ while last_credits == 0:
+ nonce += 1
+ try:
+ res = daemon.rpc_access_submit_nonce(nonce = nonce, cookie = cookie, client = self.get_signature())
+ found_valid += 1
+ last_credits = res.credits
+ break
+ except:
+ found_invalid += 1
+ assert nonce < 1000 # can't find a valid none -> the RPC probably fails
+
+ # we should now have at least 5000
+ res = daemon.rpc_access_info(client = self.get_signature())
+ assert res.credits == last_credits
+ assert res.credits >= 5000 # last one was a valid nonce
+
+ res = daemon.rpc_access_data()
+ assert len(res.entries) > 0
+ e = [x for x in res.entries if x['client'] == self.public_key]
+ assert len(e) == 1
+ e = e[0]
+ assert e.nonces_stale == 0
+ assert e.nonces_bad == found_invalid
+ assert e.nonces_good == found_valid
+ assert e.nonces_dupe == 0
+ assert e.balance == 5000
+ assert e.credits_total >= 5000
+
+ # find a valid one, then check dupes aren't allowed
+ res = daemon.rpc_access_info(client = self.get_signature())
+ cookie = res.cookie
+ old_cookie = cookie # we keep that so can submit a stale later
+ while True:
+ nonce += 1
+ try:
+ res = daemon.rpc_access_submit_nonce(nonce = nonce, cookie = cookie, client = self.get_signature())
+ found_valid += 1
+ break
+ except:
+ found_invalid += 1
+ assert nonce < 1000 # can't find both valid and invalid -> the RPC probably fails
+
+ res = daemon.rpc_access_data()
+ assert len(res.entries) > 0
+ e = [x for x in res.entries if x['client'] == self.public_key]
+ assert len(e) == 1
+ e = e[0]
+ assert e.nonces_stale == 0
+ assert e.nonces_bad == found_invalid
+ assert e.nonces_good == found_valid
+ assert e.nonces_dupe == 0
+
+ ok = False
+ try:
+ res = daemon.rpc_access_submit_nonce(nonce = nonce, cookie = cookie, client = self.get_signature())
+ except:
+ ok = True
+ assert ok
+
+ res = daemon.rpc_access_data()
+ assert len(res.entries) > 0
+ e = [x for x in res.entries if x['client'] == self.public_key]
+ assert len(e) == 1
+ e = e[0]
+ assert e.nonces_stale == 0
+ assert e.nonces_bad == found_invalid
+ assert e.nonces_good == found_valid
+ assert e.nonces_dupe == 1
+
+ # find stales without updating cookie, one within 5 seconds (accepted), one later (rejected)
+ res = daemon.rpc_access_info(client = self.get_signature())
+ found_close_stale = 0
+ found_late_stale = 0
+ while found_close_stale == 0 or found_late_stale == 0:
+ nonce += 1
+ try:
+ res = daemon.rpc_access_submit_nonce(nonce = nonce, cookie = cookie, client = self.get_signature())
+ found_close_stale += 1
+ found_valid += 1
+ except Exception as e:
+ if e[0]['error']['code'] == -18: # stale
+ found_late_stale += 1
+ else:
+ found_invalid += 1
+ assert nonce < 1000 # can't find both valid and invalid -> the RPC probably fails
+
+ res = daemon.rpc_access_data()
+ assert len(res.entries) > 0
+ e = [x for x in res.entries if x['client'] == self.public_key]
+ assert len(e) == 1
+ e = e[0]
+ assert e.nonces_stale == found_late_stale # close stales are accepted, don't count here
+ assert e.nonces_bad == found_invalid
+ assert e.nonces_good == found_valid
+ assert e.nonces_dupe == 1
+
+ # find very stale with old cookie (rejected)
+ res = daemon.rpc_access_info(client = self.get_signature())
+ nonce += 1
+ ok = False
+ try:
+ res = daemon.rpc_access_submit_nonce(nonce = nonce, cookie = old_cookie, client = self.get_signature())
+ except:
+ found_late_stale += 1
+ ok = True
+ assert ok
+
+ res = daemon.rpc_access_data()
+ assert len(res.entries) > 0
+ e = [x for x in res.entries if x['client'] == self.public_key]
+ assert len(e) == 1
+ e = e[0]
+ assert e.nonces_stale == found_late_stale
+ assert e.nonces_bad == found_invalid
+ assert e.nonces_good == found_valid
+ assert e.nonces_dupe == 1
+
+ def test_access_payment(self):
+ print('Testing access payment')
+ daemon = Daemon(idx=1)
+ wallet = Wallet(idx=3)
+
+ # Try random nonces till we find one that's valid so we get a load of credits
+ res = daemon.rpc_access_info(client = self.get_signature())
+ credits = res.credits
+ cookie = res.cookie
+ nonce = 0
+ while credits <= 100:
+ nonce += 1
+ try:
+ res = daemon.rpc_access_submit_nonce(nonce = nonce, cookie = cookie, client = self.get_signature())
+ break
+ except:
+ pass
+ assert nonce < 1000 # can't find both valid and invalid -> the RPC probably fails
+
+ res = daemon.rpc_access_info(client = self.get_signature())
+ credits = res.credits
+ assert credits > 0
+
+ res = daemon.get_info(client = self.get_signature())
+ assert res.credits == credits - 1
+ credits = res.credits
+
+ res = daemon.generateblocks('42ey1afDFnn4886T7196doS9GPMzexD9gXpsZJDwVjeRVdFCSoHnv7KPbBeGpzJBzHRCAs9UxqeoyFQMYbqSWYTfJJQAWDm', 100)
+ block_hashes = res.blocks
+
+ # ask for 1 block -> 1 credit
+ res = daemon.getblockheadersrange(0, 0, client = self.get_signature())
+ assert res.credits == credits - 1
+ credits = res.credits
+
+ # ask for 100 blocks -> >1 credit
+ res = daemon.getblockheadersrange(1, 100, client = self.get_signature())
+ assert res.credits < credits - 1
+ credits = res.credits
+
+ # external users
+ res = daemon.rpc_access_pay(payment = 1, paying_for = 'foo', client = self.get_signature())
+ assert res.credits == credits - 1
+ res = daemon.rpc_access_pay(payment = 4, paying_for = 'bar', client = self.get_signature())
+ assert res.credits == credits - 5
+ res = daemon.rpc_access_pay(payment = credits, paying_for = 'baz', client = self.get_signature())
+ assert "PAYMENT REQUIRED" in res.status
+ res = daemon.rpc_access_pay(payment = 2, paying_for = 'quux', client = self.get_signature())
+ assert res.credits == credits - 7
+ res = daemon.rpc_access_pay(payment = 3, paying_for = 'bar', client = self.get_signature())
+ assert res.credits == credits - 10
+
+ # that should be rejected because its cost is massive
+ ok = False
+ try: res = daemon.get_output_histogram(amounts = [], client = self.get_signature())
+ except Exception as e: print('e: ' + str(e)); ok = "PAYMENT REQUIRED" in e.status
+ assert ok or "PAYMENT REQUIRED" in res.status
+
+ def test_access_account(self):
+ print('Testing access account')
+ daemon = Daemon(idx=1)
+ wallet = Wallet(idx=3)
+
+ res = daemon.rpc_access_info(client = self.get_signature())
+ credits = res.credits
+ res = daemon.rpc_access_account(self.get_signature(), 0)
+ assert res.credits == credits
+ res = daemon.rpc_access_account(self.get_signature(), 50)
+ assert res.credits == credits + 50
+ res = daemon.rpc_access_account(self.get_signature(), -10)
+ assert res.credits == credits + 40
+ res = daemon.rpc_access_account(self.get_signature(), -(credits + 50))
+ assert res.credits == 0
+ res = daemon.rpc_access_account(self.get_signature(), 2**63 - 5)
+ assert res.credits == 2**63 - 5
+ res = daemon.rpc_access_account(self.get_signature(), 2**63 - 1)
+ assert res.credits == 2**64 - 6
+ res = daemon.rpc_access_account(self.get_signature(), 2)
+ assert res.credits == 2**64 - 4
+ res = daemon.rpc_access_account(self.get_signature(), 8)
+ assert res.credits == 2**64 - 1
+ res = daemon.rpc_access_account(self.get_signature(), -1)
+ assert res.credits == 2**64 - 2
+ res = daemon.rpc_access_account(self.get_signature(), -(2**63 - 1))
+ assert res.credits == 2**64 - 2 -(2**63 - 1)
+ res = daemon.rpc_access_account(self.get_signature(), -(2**63 - 1))
+ assert res.credits == 0
+
+ def test_free_access(self):
+ print('Testing free access')
+ daemon = Daemon(idx=0)
+ wallet = Wallet(idx=0)
+
+ res = daemon.rpc_access_info(client = self.get_signature())
+ assert res.credits_per_hash_found == 0
+ assert res.diff == 0
+ assert res.credits == 0
+
+ res = daemon.get_info(client = self.get_signature())
+ assert res.credits == 0
+
+ # any nonce will do here
+ res = daemon.rpc_access_submit_nonce(nonce = 0, cookie = 0, client = self.get_signature())
+ assert res.credits == 0
+
+
+class Guard:
+ def __enter__(self):
+ for i in range(4):
+ Wallet(idx = i).auto_refresh(False)
+ def __exit__(self, exc_type, exc_value, traceback):
+ for i in range(4):
+ Wallet(idx = i).auto_refresh(True)
+
+if __name__ == '__main__':
+ with Guard() as guard:
+ RPCPaymentTest().run_test()