aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/coins.cpp8
-rw-r--r--src/coins.h8
-rw-r--r--src/qt/clientmodel.cpp3
-rw-r--r--src/rpc/server.cpp17
-rw-r--r--src/test/coins_tests.cpp22
-rw-r--r--src/txmempool.cpp6
-rw-r--r--src/txmempool.h3
-rw-r--r--src/util.cpp8
-rw-r--r--src/util.h5
-rw-r--r--test/README.md154
-rw-r--r--test/functional/README.md174
-rwxr-xr-xtest/functional/example_test.py219
-rwxr-xr-xtest/functional/test_runner.py2
-rwxr-xr-xtest/functional/uptime.py32
14 files changed, 532 insertions, 129 deletions
diff --git a/src/coins.cpp b/src/coins.cpp
index f8df835e9f..3113b7755d 100644
--- a/src/coins.cpp
+++ b/src/coins.cpp
@@ -11,11 +11,15 @@
#include <assert.h>
bool CCoinsView::GetCoin(const COutPoint &outpoint, Coin &coin) const { return false; }
-bool CCoinsView::HaveCoin(const COutPoint &outpoint) const { return false; }
uint256 CCoinsView::GetBestBlock() const { return uint256(); }
bool CCoinsView::BatchWrite(CCoinsMap &mapCoins, const uint256 &hashBlock) { return false; }
CCoinsViewCursor *CCoinsView::Cursor() const { return 0; }
+bool CCoinsView::HaveCoin(const COutPoint &outpoint) const
+{
+ Coin coin;
+ return GetCoin(outpoint, coin);
+}
CCoinsViewBacked::CCoinsViewBacked(CCoinsView *viewIn) : base(viewIn) { }
bool CCoinsViewBacked::GetCoin(const COutPoint &outpoint, Coin &coin) const { return base->GetCoin(outpoint, coin); }
@@ -55,7 +59,7 @@ bool CCoinsViewCache::GetCoin(const COutPoint &outpoint, Coin &coin) const {
CCoinsMap::const_iterator it = FetchCoin(outpoint);
if (it != cacheCoins.end()) {
coin = it->second.coin;
- return true;
+ return !coin.IsSpent();
}
return false;
}
diff --git a/src/coins.h b/src/coins.h
index 4774c9f6a6..077545a55b 100644
--- a/src/coins.h
+++ b/src/coins.h
@@ -145,11 +145,13 @@ private:
class CCoinsView
{
public:
- //! Retrieve the Coin (unspent transaction output) for a given outpoint.
+ /** Retrieve the Coin (unspent transaction output) for a given outpoint.
+ * Returns true only when an unspent coin was found, which is returned in coin.
+ * When false is returned, coin's value is unspecified.
+ */
virtual bool GetCoin(const COutPoint &outpoint, Coin &coin) const;
- //! Just check whether we have data for a given outpoint.
- //! This may (but cannot always) return true for spent outputs.
+ //! Just check whether a given outpoint is unspent.
virtual bool HaveCoin(const COutPoint &outpoint) const;
//! Retrieve the block hash whose state this CCoinsView currently represents
diff --git a/src/qt/clientmodel.cpp b/src/qt/clientmodel.cpp
index 3dfb51ccfa..33f4535ee2 100644
--- a/src/qt/clientmodel.cpp
+++ b/src/qt/clientmodel.cpp
@@ -26,7 +26,6 @@
class CBlockIndex;
-static const int64_t nClientStartupTime = GetTime();
static int64_t nLastHeaderTipUpdateNotification = 0;
static int64_t nLastBlockTipUpdateNotification = 0;
@@ -238,7 +237,7 @@ bool ClientModel::isReleaseVersion() const
QString ClientModel::formatClientStartupTime() const
{
- return QDateTime::fromTime_t(nClientStartupTime).toString();
+ return QDateTime::fromTime_t(GetStartupTime()).toString();
}
QString ClientModel::dataDir() const
diff --git a/src/rpc/server.cpp b/src/rpc/server.cpp
index 1a04ce2b47..c320d20453 100644
--- a/src/rpc/server.cpp
+++ b/src/rpc/server.cpp
@@ -258,6 +258,22 @@ UniValue stop(const JSONRPCRequest& jsonRequest)
return "Bitcoin server stopping";
}
+UniValue uptime(const JSONRPCRequest& jsonRequest)
+{
+ if (jsonRequest.fHelp || jsonRequest.params.size() > 1)
+ throw std::runtime_error(
+ "uptime\n"
+ "\nReturns the total uptime of the server.\n"
+ "\nResult:\n"
+ "ttt (numeric) The number of seconds that the server has been running\n"
+ "\nExamples:\n"
+ + HelpExampleCli("uptime", "")
+ + HelpExampleRpc("uptime", "")
+ );
+
+ return GetTime() - GetStartupTime();
+}
+
/**
* Call Table
*/
@@ -267,6 +283,7 @@ static const CRPCCommand vRPCCommands[] =
/* Overall control/query calls */
{ "control", "help", &help, true, {"command"} },
{ "control", "stop", &stop, true, {} },
+ { "control", "uptime", &uptime, true, {} },
};
CRPCTable::CRPCTable()
diff --git a/src/test/coins_tests.cpp b/src/test/coins_tests.cpp
index 622b157621..e24431528a 100644
--- a/src/test/coins_tests.cpp
+++ b/src/test/coins_tests.cpp
@@ -50,12 +50,6 @@ public:
return true;
}
- bool HaveCoin(const COutPoint& outpoint) const override
- {
- Coin coin;
- return GetCoin(outpoint, coin);
- }
-
uint256 GetBestBlock() const override { return hashBestBlock_; }
bool BatchWrite(CCoinsMap& mapCoins, const uint256& hashBlock) override
@@ -147,8 +141,22 @@ BOOST_AUTO_TEST_CASE(coins_cache_simulation_test)
{
uint256 txid = txids[InsecureRandRange(txids.size())]; // txid we're going to modify in this iteration.
Coin& coin = result[COutPoint(txid, 0)];
+
+ // Determine whether to test HaveCoin before or after Access* (or both). As these functions
+ // can influence each other's behaviour by pulling things into the cache, all combinations
+ // are tested.
+ bool test_havecoin_before = InsecureRandBits(2) == 0;
+ bool test_havecoin_after = InsecureRandBits(2) == 0;
+
+ bool result_havecoin = test_havecoin_before ? stack.back()->HaveCoin(COutPoint(txid, 0)) : false;
const Coin& entry = (InsecureRandRange(500) == 0) ? AccessByTxid(*stack.back(), txid) : stack.back()->AccessCoin(COutPoint(txid, 0));
BOOST_CHECK(coin == entry);
+ BOOST_CHECK(!test_havecoin_before || result_havecoin == !entry.IsSpent());
+
+ if (test_havecoin_after) {
+ bool ret = stack.back()->HaveCoin(COutPoint(txid, 0));
+ BOOST_CHECK(ret == !entry.IsSpent());
+ }
if (InsecureRandRange(5) == 0 || coin.IsSpent()) {
Coin newcoin;
@@ -628,7 +636,7 @@ BOOST_AUTO_TEST_CASE(ccoins_access)
CheckAccessCoin(ABSENT, VALUE2, VALUE2, FRESH , FRESH );
CheckAccessCoin(ABSENT, VALUE2, VALUE2, DIRTY , DIRTY );
CheckAccessCoin(ABSENT, VALUE2, VALUE2, DIRTY|FRESH, DIRTY|FRESH);
- CheckAccessCoin(PRUNED, ABSENT, PRUNED, NO_ENTRY , FRESH );
+ CheckAccessCoin(PRUNED, ABSENT, ABSENT, NO_ENTRY , NO_ENTRY );
CheckAccessCoin(PRUNED, PRUNED, PRUNED, 0 , 0 );
CheckAccessCoin(PRUNED, PRUNED, PRUNED, FRESH , FRESH );
CheckAccessCoin(PRUNED, PRUNED, PRUNED, DIRTY , DIRTY );
diff --git a/src/txmempool.cpp b/src/txmempool.cpp
index 8deb703d2e..dcfc5ffde0 100644
--- a/src/txmempool.cpp
+++ b/src/txmempool.cpp
@@ -903,11 +903,7 @@ bool CCoinsViewMemPool::GetCoin(const COutPoint &outpoint, Coin &coin) const {
return false;
}
}
- return (base->GetCoin(outpoint, coin) && !coin.IsSpent());
-}
-
-bool CCoinsViewMemPool::HaveCoin(const COutPoint &outpoint) const {
- return mempool.exists(outpoint) || base->HaveCoin(outpoint);
+ return base->GetCoin(outpoint, coin);
}
size_t CTxMemPool::DynamicMemoryUsage() const {
diff --git a/src/txmempool.h b/src/txmempool.h
index 7ca3b18a1e..78ac3c209b 100644
--- a/src/txmempool.h
+++ b/src/txmempool.h
@@ -684,8 +684,7 @@ protected:
public:
CCoinsViewMemPool(CCoinsView* baseIn, const CTxMemPool& mempoolIn);
- bool GetCoin(const COutPoint &outpoint, Coin &coin) const;
- bool HaveCoin(const COutPoint &outpoint) const;
+ bool GetCoin(const COutPoint &outpoint, Coin &coin) const override;
};
/**
diff --git a/src/util.cpp b/src/util.cpp
index 27ccd40b7b..b76c173f90 100644
--- a/src/util.cpp
+++ b/src/util.cpp
@@ -84,6 +84,8 @@
#include <openssl/rand.h>
#include <openssl/conf.h>
+// Application startup time (used for uptime calculation)
+const int64_t nStartupTime = GetTime();
const char * const BITCOIN_CONF_FILENAME = "bitcoin.conf";
const char * const BITCOIN_PID_FILENAME = "bitcoind.pid";
@@ -891,3 +893,9 @@ std::string CopyrightHolders(const std::string& strPrefix)
}
return strCopyrightHolders;
}
+
+// Obtain the application startup time (used for uptime calculation)
+int64_t GetStartupTime()
+{
+ return nStartupTime;
+}
diff --git a/src/util.h b/src/util.h
index a4d7aa4db8..824ad51ac4 100644
--- a/src/util.h
+++ b/src/util.h
@@ -5,7 +5,7 @@
/**
* Server/client environment: argument handling, config file parsing,
- * logging, thread wrappers
+ * logging, thread wrappers, startup time
*/
#ifndef BITCOIN_UTIL_H
#define BITCOIN_UTIL_H
@@ -29,6 +29,9 @@
#include <boost/signals2/signal.hpp>
+// Application startup time (used for uptime calculation)
+int64_t GetStartupTime();
+
static const bool DEFAULT_LOGTIMEMICROS = false;
static const bool DEFAULT_LOGIPS = false;
static const bool DEFAULT_LOGTIMESTAMPS = true;
diff --git a/test/README.md b/test/README.md
index 4dd512638d..15f6df790f 100644
--- a/test/README.md
+++ b/test/README.md
@@ -15,84 +15,152 @@ The util tests are run as part of `make check` target. The functional
tests are run by the travis continuous build process whenever a pull
request is opened. Both sets of tests can also be run locally.
-Functional Test dependencies
-============================
+# Running tests locally
+
+Build for your system first. Be sure to enable wallet, utils and daemon when you configure. Tests will not run otherwise.
+
+### Functional tests
+
+#### Dependencies
+
The ZMQ functional test requires a python ZMQ library. To install it:
- on Unix, run `sudo apt-get install python3-zmq`
- on mac OS, run `pip3 install pyzmq`
-Running tests locally
-=====================
+#### Running the tests
-Build for your system first. Be sure to enable wallet, utils and daemon when you configure. Tests will not run otherwise.
+Individual tests can be run by directly calling the test script, eg:
-Functional tests
-----------------
+```
+test/functional/replace-by-fee.py
+```
-You can run any single test by calling
+or can be run through the test_runner harness, eg:
- test/functional/test_runner.py <testname>
+```
+test/functional/test_runner.py replace-by-fee.py
+```
-Or you can run any combination (incl. duplicates) of tests by calling
+You can run any combination (incl. duplicates) of tests by calling:
- test/functional/test_runner.py <testname1> <testname2> <testname3> ...
+```
+test/functional/test_runner.py <testname1> <testname2> <testname3> ...
+```
-Run the regression test suite with
+Run the regression test suite with:
- test/functional/test_runner.py
+```
+test/functional/test_runner.py
+```
Run all possible tests with
- test/functional/test_runner.py --extended
+```
+test/functional/test_runner.py --extended
+```
+
+By default, up to 4 tests will be run in parallel by test_runner. To specify
+how many jobs to run, append `--jobs=n`
-By default, tests will be run in parallel. To specify how many jobs to run,
-append `--jobs=n` (default n=4).
+The individual tests and the test_runner harness have many command-line
+options. Run `test_runner.py -h` to see them all.
-If you want to create a basic coverage report for the RPC test suite, append `--coverage`.
+#### Troubleshooting and debugging test failures
-Possible options, which apply to each individual test run:
+##### Resource contention
+The P2P and RPC ports used by the bitcoind nodes-under-test are chosen to make
+conflicts with other processes unlikely. However, if there is another bitcoind
+process running on the system (perhaps from a previous test which hasn't successfully
+killed all its bitcoind nodes), then there may be a port conflict which will
+cause the test to fail. It is recommended that you run the tests on a system
+where no other bitcoind processes are running.
+
+On linux, the test_framework will warn if there is another
+bitcoind process running when the tests are started.
+
+If there are zombie bitcoind processes after test failure, you can kill them
+by running the following commands. **Note that these commands will kill all
+bitcoind processes running on the system, so should not be used if any non-test
+bitcoind processes are being run.**
+
+```bash
+killall bitcoind
```
- -h, --help show this help message and exit
- --nocleanup Leave bitcoinds and test.* datadir on exit or error
- --noshutdown Don't stop bitcoinds after the test execution
- --srcdir=SRCDIR Source directory containing bitcoind/bitcoin-cli
- (default: ../../src)
- --tmpdir=TMPDIR Root directory for datadirs
- --tracerpc Print out all RPC calls as they are made
- --coveragedir=COVERAGEDIR
- Write tested RPC commands into this directory
-```
-If you set the environment variable `PYTHON_DEBUG=1` you will get some debug
-output (example: `PYTHON_DEBUG=1 test/functional/test_runner.py wallet`).
+or
+
+```bash
+pkill -9 bitcoind
+```
-A 200-block -regtest blockchain and wallets for four nodes
-is created the first time a regression test is run and
-is stored in the cache/ directory. Each node has 25 mature
-blocks (25*50=1250 BTC) in its wallet.
-After the first run, the cache/ blockchain and wallets are
-copied into a temporary directory and used as the initial
-test state.
+##### Data directory cache
-If you get into a bad state, you should be able
-to recover with:
+A pre-mined blockchain with 200 blocks is generated the first time a
+functional test is run and is stored in test/cache. This speeds up
+test startup times since new blockchains don't need to be generated for
+each test. However, the cache may get into a bad state, in which case
+tests will fail. If this happens, remove the cache directory (and make
+sure bitcoind processes are stopped as above):
```bash
rm -rf cache
killall bitcoind
```
-Util tests
-----------
+##### Test logging
+
+The tests contain logging at different levels (debug, info, warning, etc). By
+default:
+
+- when run through the test_runner harness, *all* logs are written to
+ `test_framework.log` and no logs are output to the console.
+- when run directly, *all* logs are written to `test_framework.log` and INFO
+ level and above are output to the console.
+- when run on Travis, no logs are output to the console. However, if a test
+ fails, the `test_framework.log` and bitcoind `debug.log`s will all be dumped
+ to the console to help troubleshooting.
+
+To change the level of logs output to the console, use the `-l` command line
+argument.
+
+`test_framework.log` and bitcoind `debug.log`s can be combined into a single
+aggregate log by running the `combine_logs.py` script. The output can be plain
+text, colorized text or html. For example:
+
+```
+combine_logs.py -c <test data directory> | less -r
+```
+
+will pipe the colorized logs from the test into less.
+
+Use `--tracerpc` to trace out all the RPC calls and responses to the console. For
+some tests (eg any that use `submitblock` to submit a full block over RPC),
+this can result in a lot of screen output.
+
+By default, the test data directory will be deleted after a successful run.
+Use `--nocleanup` to leave the test data directory intact. The test data
+directory is never deleted after a failed test.
+
+##### Attaching a debugger
+
+A python debugger can be attached to tests at any point. Just add the line:
+
+```py
+import pdb; pdb.set_trace()
+```
+
+anywhere in the test. You will then be able to inspect variables, as well as
+call methods that interact with the bitcoind nodes-under-test.
+
+### Util tests
Util tests can be run locally by running `test/util/bitcoin-util-test.py`.
Use the `-v` option for verbose output.
-Writing functional tests
-========================
+# Writing functional tests
You are encouraged to write functional tests for new or existing features.
Further information about the functional test framework and individual
diff --git a/test/functional/README.md b/test/functional/README.md
index e6c4849702..96fe0becce 100644
--- a/test/functional/README.md
+++ b/test/functional/README.md
@@ -1,108 +1,154 @@
-Regression tests
-================
+# Functional tests
-### [test_framework/authproxy.py](test_framework/authproxy.py)
-Taken from the [python-bitcoinrpc repository](https://github.com/jgarzik/python-bitcoinrpc).
+### Writing Functional Tests
-### [test_framework/test_framework.py](test_framework/test_framework.py)
-Base class for new regression tests.
+#### Example test
-### [test_framework/util.py](test_framework/util.py)
-Generally useful functions.
+The [example_test.py](example_test.py) is a heavily commented example of a test case that uses both
+the RPC and P2P interfaces. If you are writing your first test, copy that file
+and modify to fit your needs.
-### [test_framework/mininode.py](test_framework/mininode.py)
-Basic code to support p2p connectivity to a bitcoind.
+#### Coverage
-### [test_framework/comptool.py](test_framework/comptool.py)
-Framework for comparison-tool style, p2p tests.
+Running `test_runner.py` with the `--coverage` argument tracks which RPCs are
+called by the tests and prints a report of uncovered RPCs in the summary. This
+can be used (along with the `--extended` argument) to find out which RPCs we
+don't have test cases for.
-### [test_framework/script.py](test_framework/script.py)
-Utilities for manipulating transaction scripts (originally from python-bitcoinlib)
+#### Style guidelines
-### [test_framework/blockstore.py](test_framework/blockstore.py)
-Implements disk-backed block and tx storage.
+- Where possible, try to adhere to [PEP-8 guidelines]([https://www.python.org/dev/peps/pep-0008/)
+- Use a python linter like flake8 before submitting PRs to catch common style
+ nits (eg trailing whitespace, unused imports, etc)
+- Avoid wildcard imports where possible
+- Use a module-level docstring to describe what the test is testing, and how it
+ is testing it.
+- When subclassing the BitcoinTestFramwork, place overrides for the
+ `__init__()`, and `setup_xxxx()` methods at the top of the subclass, then
+ locally-defined helper methods, then the `run_test()` method.
-### [test_framework/key.py](test_framework/key.py)
-Wrapper around OpenSSL EC_Key (originally from python-bitcoinlib)
+#### General test-writing advice
-### [test_framework/bignum.py](test_framework/bignum.py)
-Helpers for script.py
+- Set `self.num_nodes` to the minimum number of nodes necessary for the test.
+ Having additional unrequired nodes adds to the execution time of the test as
+ well as memory/CPU/disk requirements (which is important when running tests in
+ parallel or on Travis).
+- Avoid stop-starting the nodes multiple times during the test if possible. A
+ stop-start takes several seconds, so doing it several times blows up the
+ runtime of the test.
+- Set the `self.setup_clean_chain` variable in `__init__()` to control whether
+ or not to use the cached data directories. The cached data directories
+ contain a 200-block pre-mined blockchain and wallets for four nodes. Each node
+ has 25 mature blocks (25x50=1250 BTC) in its wallet.
+- When calling RPCs with lots of arguments, consider using named keyword
+ arguments instead of positional arguments to make the intent of the call
+ clear to readers.
-### [test_framework/blocktools.py](test_framework/blocktools.py)
-Helper functions for creating blocks and transactions.
+#### RPC and P2P definitions
-P2P test design notes
----------------------
+Test writers may find it helpful to refer to the definitions for the RPC and
+P2P messages. These can be found in the following source files:
-## Mininode
+- `/src/rpc/*` for RPCs
+- `/src/wallet/rpc*` for wallet RPCs
+- `ProcessMessage()` in `/src/net_processing.cpp` for parsing P2P messages
-* ```mininode.py``` contains all the definitions for objects that pass
-over the network (```CBlock```, ```CTransaction```, etc, along with the network-level
-wrappers for them, ```msg_block```, ```msg_tx```, etc).
+#### Using the P2P interface
-* P2P tests have two threads. One thread handles all network communication
+- `mininode.py` contains all the definitions for objects that pass
+over the network (`CBlock`, `CTransaction`, etc, along with the network-level
+wrappers for them, `msg_block`, `msg_tx`, etc).
+
+- P2P tests have two threads. One thread handles all network communication
with the bitcoind(s) being tested (using python's asyncore package); the other
implements the test logic.
-* ```NodeConn``` is the class used to connect to a bitcoind. If you implement
-a callback class that derives from ```NodeConnCB``` and pass that to the
-```NodeConn``` object, your code will receive the appropriate callbacks when
+- `NodeConn` is the class used to connect to a bitcoind. If you implement
+a callback class that derives from `NodeConnCB` and pass that to the
+`NodeConn` object, your code will receive the appropriate callbacks when
events of interest arrive.
-* You can pass the same handler to multiple ```NodeConn```'s if you like, or pass
-different ones to each -- whatever makes the most sense for your test.
-
-* Call ```NetworkThread.start()``` after all ```NodeConn``` objects are created to
+- Call `NetworkThread.start()` after all `NodeConn` objects are created to
start the networking thread. (Continue with the test logic in your existing
thread.)
-* RPC calls are available in p2p tests.
+- Can be used to write tests where specific P2P protocol behavior is tested.
+Examples tests are `p2p-accept-block.py`, `p2p-compactblocks.py`.
-* Can be used to write free-form tests, where specific p2p-protocol behavior
-is tested. Examples: ```p2p-accept-block.py```, ```p2p-compactblocks.py```.
+#### Comptool
-## Comptool
+- Comptool is a Testing framework for writing tests that compare the block/tx acceptance
+behavior of a bitcoind against 1 or more other bitcoind instances. It should not be used
+to write static tests with known outcomes, since that type of test is easier to write and
+maintain using the standard BitcoinTestFramework.
-* Testing framework for writing tests that compare the block/tx acceptance
-behavior of a bitcoind against 1 or more other bitcoind instances, or against
-known outcomes, or both.
-
-* Set the ```num_nodes``` variable (defined in ```ComparisonTestFramework```) to start up
-1 or more nodes. If using 1 node, then ```--testbinary``` can be used as a command line
+- Set the `num_nodes` variable (defined in `ComparisonTestFramework`) to start up
+1 or more nodes. If using 1 node, then `--testbinary` can be used as a command line
option to change the bitcoind binary used by the test. If using 2 or more nodes,
-then ```--refbinary``` can be optionally used to change the bitcoind that will be used
+then `--refbinary` can be optionally used to change the bitcoind that will be used
on nodes 2 and up.
-* Implement a (generator) function called ```get_tests()``` which yields ```TestInstance```s.
-Each ```TestInstance``` consists of:
- - a list of ```[object, outcome, hash]``` entries
- * ```object``` is a ```CBlock```, ```CTransaction```, or
- ```CBlockHeader```. ```CBlock```'s and ```CTransaction```'s are tested for
- acceptance. ```CBlockHeader```s can be used so that the test runner can deliver
+- Implement a (generator) function called `get_tests()` which yields `TestInstance`s.
+Each `TestInstance` consists of:
+ - a list of `[object, outcome, hash]` entries
+ * `object` is a `CBlock`, `CTransaction`, or
+ `CBlockHeader`. `CBlock`'s and `CTransaction`'s are tested for
+ acceptance. `CBlockHeader`s can be used so that the test runner can deliver
complete headers-chains when requested from the bitcoind, to allow writing
tests where blocks can be delivered out of order but still processed by
headers-first bitcoind's.
- * ```outcome``` is ```True```, ```False```, or ```None```. If ```True```
- or ```False```, the tip is compared with the expected tip -- either the
+ * `outcome` is `True`, `False`, or `None`. If `True`
+ or `False`, the tip is compared with the expected tip -- either the
block passed in, or the hash specified as the optional 3rd entry. If
- ```None``` is specified, then the test will compare all the bitcoind's
+ `None` is specified, then the test will compare all the bitcoind's
being tested to see if they all agree on what the best tip is.
- * ```hash``` is the block hash of the tip to compare against. Optional to
+ * `hash` is the block hash of the tip to compare against. Optional to
specify; if left out then the hash of the block passed in will be used as
the expected tip. This allows for specifying an expected tip while testing
the handling of either invalid blocks or blocks delivered out of order,
which complete a longer chain.
- - ```sync_every_block```: ```True/False```. If ```False```, then all blocks
+ - `sync_every_block`: `True/False`. If `False`, then all blocks
are inv'ed together, and the test runner waits until the node receives the
last one, and tests only the last block for tip acceptance using the
- outcome and specified tip. If ```True```, then each block is tested in
+ outcome and specified tip. If `True`, then each block is tested in
sequence and synced (this is slower when processing many blocks).
- - ```sync_every_transaction```: ```True/False```. Analogous to
- ```sync_every_block```, except if the outcome on the last tx is "None",
+ - `sync_every_transaction`: `True/False`. Analogous to
+ `sync_every_block`, except if the outcome on the last tx is "None",
then the contents of the entire mempool are compared across all bitcoind
- connections. If ```True``` or ```False```, then only the last tx's
+ connections. If `True` or `False`, then only the last tx's
acceptance is tested against the given outcome.
-* For examples of tests written in this framework, see
- ```invalidblockrequest.py``` and ```p2p-fullblocktest.py```.
+- For examples of tests written in this framework, see
+ `invalidblockrequest.py` and `p2p-fullblocktest.py`.
+
+### test-framework modules
+
+#### [test_framework/authproxy.py](test_framework/authproxy.py)
+Taken from the [python-bitcoinrpc repository](https://github.com/jgarzik/python-bitcoinrpc).
+
+#### [test_framework/test_framework.py](test_framework/test_framework.py)
+Base class for functional tests.
+#### [test_framework/util.py](test_framework/util.py)
+Generally useful functions.
+
+#### [test_framework/mininode.py](test_framework/mininode.py)
+Basic code to support P2P connectivity to a bitcoind.
+
+#### [test_framework/comptool.py](test_framework/comptool.py)
+Framework for comparison-tool style, P2P tests.
+
+#### [test_framework/script.py](test_framework/script.py)
+Utilities for manipulating transaction scripts (originally from python-bitcoinlib)
+
+#### [test_framework/blockstore.py](test_framework/blockstore.py)
+Implements disk-backed block and tx storage.
+
+#### [test_framework/key.py](test_framework/key.py)
+Wrapper around OpenSSL EC_Key (originally from python-bitcoinlib)
+
+#### [test_framework/bignum.py](test_framework/bignum.py)
+Helpers for script.py
+
+#### [test_framework/blocktools.py](test_framework/blocktools.py)
+Helper functions for creating blocks and transactions.
diff --git a/test/functional/example_test.py b/test/functional/example_test.py
new file mode 100755
index 0000000000..1ba5f756cd
--- /dev/null
+++ b/test/functional/example_test.py
@@ -0,0 +1,219 @@
+#!/usr/bin/env python3
+# Copyright (c) 2017 The Bitcoin Core developers
+# Distributed under the MIT software license, see the accompanying
+# file COPYING or http://www.opensource.org/licenses/mit-license.php.
+"""An example functional test
+
+The module-level docstring should include a high-level description of
+what the test is doing. It's the first thing people see when they open
+the file and should give the reader information about *what* the test
+is testing and *how* it's being tested
+"""
+# Imports should be in PEP8 ordering (std library first, then third party
+# libraries then local imports).
+from collections import defaultdict
+
+# Avoid wildcard * imports if possible
+from test_framework.blocktools import (create_block, create_coinbase)
+from test_framework.mininode import (
+ CInv,
+ NetworkThread,
+ NodeConn,
+ NodeConnCB,
+ mininode_lock,
+ msg_block,
+ msg_getdata,
+ wait_until,
+)
+from test_framework.test_framework import BitcoinTestFramework
+from test_framework.util import (
+ assert_equal,
+ connect_nodes,
+ p2p_port,
+)
+
+# NodeConnCB is a class containing callbacks to be executed when a P2P
+# message is received from the node-under-test. Subclass NodeConnCB and
+# override the on_*() methods if you need custom behaviour.
+class BaseNode(NodeConnCB):
+ def __init__(self):
+ """Initialize the NodeConnCB
+
+ Used to inialize custom properties for the Node that aren't
+ included by default in the base class. Be aware that the NodeConnCB
+ base class already stores a counter for each P2P message type and the
+ last received message of each type, which should be sufficient for the
+ needs of most tests.
+
+ Call super().__init__() first for standard initialization and then
+ initialize custom properties."""
+ super().__init__()
+ # Stores a dictionary of all blocks received
+ self.block_receive_map = defaultdict(int)
+
+ def on_block(self, conn, message):
+ """Override the standard on_block callback
+
+ Store the hash of a received block in the dictionary."""
+ message.block.calc_sha256()
+ self.block_receive_map[message.block.sha256] += 1
+
+def custom_function():
+ """Do some custom behaviour
+
+ If this function is more generally useful for other tests, consider
+ moving it to a module in test_framework."""
+ # self.log.info("running custom_function") # Oops! Can't run self.log outside the BitcoinTestFramework
+ pass
+
+class ExampleTest(BitcoinTestFramework):
+ # Each functional test is a subclass of the BitcoinTestFramework class.
+
+ # Override the __init__(), add_options(), setup_chain(), setup_network()
+ # and setup_nodes() methods to customize the test setup as required.
+
+ def __init__(self):
+ """Initialize the test
+
+ Call super().__init__() first, and then override any test parameters
+ for your individual test."""
+ super().__init__()
+ self.setup_clean_chain = True
+ self.num_nodes = 3
+ # Use self.extra_args to change command-line arguments for the nodes
+ self.extra_args = [[], ["-logips"], []]
+
+ # self.log.info("I've finished __init__") # Oops! Can't run self.log before run_test()
+
+ # Use add_options() to add specific command-line options for your test.
+ # In practice this is not used very much, since the tests are mostly written
+ # to be run in automated environments without command-line options.
+ # def add_options()
+ # pass
+
+ # Use setup_chain() to customize the node data directories. In practice
+ # this is not used very much since the default behaviour is almost always
+ # fine
+ # def setup_chain():
+ # pass
+
+ def setup_network(self):
+ """Setup the test network topology
+
+ Often you won't need to override this, since the standard network topology
+ (linear: node0 <-> node1 <-> node2 <-> ...) is fine for most tests.
+
+ If you do override this method, remember to start the nodes, assign
+ them to self.nodes, connect them and then sync."""
+
+ self.setup_nodes()
+
+ # In this test, we're not connecting node2 to node0 or node1. Calls to
+ # sync_all() should not include node2, since we're not expecting it to
+ # sync.
+ connect_nodes(self.nodes[0], 1)
+ self.sync_all([self.nodes[0:1]])
+
+ # Use setup_nodes() to customize the node start behaviour (for example if
+ # you don't want to start all nodes at the start of the test).
+ # def setup_nodes():
+ # pass
+
+ def custom_method(self):
+ """Do some custom behaviour for this test
+
+ Define it in a method here because you're going to use it repeatedly.
+ If you think it's useful in general, consider moving it to the base
+ BitcoinTestFramework class so other tests can use it."""
+
+ self.log.info("Running custom_method")
+
+ def run_test(self):
+ """Main test logic"""
+
+ # Create a P2P connection to one of the nodes
+ node0 = BaseNode()
+ connections = []
+ connections.append(NodeConn('127.0.0.1', p2p_port(0), self.nodes[0], node0))
+ node0.add_connection(connections[0])
+
+ # Start up network handling in another thread. This needs to be called
+ # after the P2P connections have been created.
+ NetworkThread().start()
+ # wait_for_verack ensures that the P2P connection is fully up.
+ node0.wait_for_verack()
+
+ # Generating a block on one of the nodes will get us out of IBD
+ blocks = [int(self.nodes[0].generate(nblocks=1)[0], 16)]
+ self.sync_all([self.nodes[0:1]])
+
+ # Notice above how we called an RPC by calling a method with the same
+ # name on the node object. Notice also how we used a keyword argument
+ # to specify a named RPC argument. Neither of those are defined on the
+ # node object. Instead there's some __getattr__() magic going on under
+ # the covers to dispatch unrecognised attribute calls to the RPC
+ # interface.
+
+ # Logs are nice. Do plenty of them. They can be used in place of comments for
+ # breaking the test into sub-sections.
+ self.log.info("Starting test!")
+
+ self.log.info("Calling a custom function")
+ custom_function()
+
+ self.log.info("Calling a custom method")
+ self.custom_method()
+
+ self.log.info("Create some blocks")
+ self.tip = int(self.nodes[0].getbestblockhash(), 16)
+ self.block_time = self.nodes[0].getblock(self.nodes[0].getbestblockhash())['time'] + 1
+
+ height = 1
+
+ for i in range(10):
+ # Use the mininode and blocktools functionality to manually build a block
+ # Calling the generate() rpc is easier, but this allows us to exactly
+ # control the blocks and transactions.
+ block = create_block(self.tip, create_coinbase(height), self.block_time)
+ block.solve()
+ block_message = msg_block(block)
+ # Send message is used to send a P2P message to the node over our NodeConn connection
+ node0.send_message(block_message)
+ self.tip = block.sha256
+ blocks.append(self.tip)
+ self.block_time += 1
+ height += 1
+
+ self.log.info("Wait for node1 to reach current tip (height 11) using RPC")
+ self.nodes[1].waitforblockheight(11)
+
+ self.log.info("Connect node2 and node1")
+ connect_nodes(self.nodes[1], 2)
+
+ self.log.info("Add P2P connection to node2")
+ node2 = BaseNode()
+ connections.append(NodeConn('127.0.0.1', p2p_port(2), self.nodes[2], node2))
+ node2.add_connection(connections[1])
+ node2.wait_for_verack()
+
+ self.log.info("Wait for node2 reach current tip. Test that it has propogated all the blocks to us")
+
+ for block in blocks:
+ getdata_request = msg_getdata()
+ getdata_request.inv.append(CInv(2, block))
+ node2.send_message(getdata_request)
+
+ # wait_until() will loop until a predicate condition is met. Use it to test properties of the
+ # NodeConnCB objects.
+ assert wait_until(lambda: sorted(blocks) == sorted(list(node2.block_receive_map.keys())), timeout=5)
+
+ self.log.info("Check that each block was received only once")
+ # The network thread uses a global lock on data access to the NodeConn objects when sending and receiving
+ # messages. The test thread should acquire the global lock before accessing any NodeConn data to avoid locking
+ # and synchronization issues. Note wait_until() acquires this global lock when testing the predicate.
+ with mininode_lock:
+ for block in node2.block_receive_map.values():
+ assert_equal(block, 1)
+
+if __name__ == '__main__':
+ ExampleTest().main()
diff --git a/test/functional/test_runner.py b/test/functional/test_runner.py
index 0dca318af8..9952835951 100755
--- a/test/functional/test_runner.py
+++ b/test/functional/test_runner.py
@@ -113,6 +113,7 @@ BASE_SCRIPTS= [
'listsinceblock.py',
'p2p-leaktests.py',
'wallet-encryption.py',
+ 'uptime.py',
]
EXTENDED_SCRIPTS = [
@@ -138,6 +139,7 @@ EXTENDED_SCRIPTS = [
'bip65-cltv-p2p.py',
'bipdersig-p2p.py',
'bipdersig.py',
+ 'example_test.py',
'getblocktemplate_proposals.py',
'txn_doublespend.py',
'txn_clone.py --mineblock',
diff --git a/test/functional/uptime.py b/test/functional/uptime.py
new file mode 100755
index 0000000000..b20d6f5cb6
--- /dev/null
+++ b/test/functional/uptime.py
@@ -0,0 +1,32 @@
+#!/usr/bin/env python3
+# Copyright (c) 2017 The Bitcoin Core developers
+# Distributed under the MIT software license, see the accompanying
+# file COPYING or http://www.opensource.org/licenses/mit-license.php.
+"""Test the RPC call related to the uptime command.
+
+Test corresponds to code in rpc/server.cpp.
+"""
+
+import time
+
+from test_framework.test_framework import BitcoinTestFramework
+
+
+class UptimeTest(BitcoinTestFramework):
+ def __init__(self):
+ super().__init__()
+
+ self.num_nodes = 1
+ self.setup_clean_chain = True
+
+ def run_test(self):
+ self._test_uptime()
+
+ def _test_uptime(self):
+ wait_time = 10
+ self.nodes[0].setmocktime(int(time.time() + wait_time))
+ assert(self.nodes[0].uptime() >= wait_time)
+
+
+if __name__ == '__main__':
+ UptimeTest().main()