aboutsummaryrefslogtreecommitdiff
path: root/doc/developer-notes.md
diff options
context:
space:
mode:
Diffstat (limited to 'doc/developer-notes.md')
-rw-r--r--doc/developer-notes.md187
1 files changed, 149 insertions, 38 deletions
diff --git a/doc/developer-notes.md b/doc/developer-notes.md
index 97659cf76a..583c50a763 100644
--- a/doc/developer-notes.md
+++ b/doc/developer-notes.md
@@ -14,7 +14,7 @@ Developer Notes
- [Compiling for debugging](#compiling-for-debugging)
- [Compiling for gprof profiling](#compiling-for-gprof-profiling)
- [`debug.log`](#debuglog)
- - [Testnet and Regtest modes](#testnet-and-regtest-modes)
+ - [Signet, testnet, and regtest modes](#signet-testnet-and-regtest-modes)
- [DEBUG_LOCKORDER](#debug_lockorder)
- [Valgrind suppressions file](#valgrind-suppressions-file)
- [Compiling for test coverage](#compiling-for-test-coverage)
@@ -75,6 +75,11 @@ tool to clean up patches automatically before submission.
on the same line as the `if`, without braces. In every other case,
braces are required, and the `then` and `else` clauses must appear
correctly indented on a new line.
+ - There's no hard limit on line width, but prefer to keep lines to <100
+ characters if doing so does not decrease readability. Break up long
+ function declarations over multiple lines using the Clang Format
+ [AlignAfterOpenBracket](https://clang.llvm.org/docs/ClangFormatStyleOptions.html)
+ style option.
- **Symbol naming conventions**. These are preferred in new code, but are not
required when doing so would need changes to significant pieces of existing
@@ -135,7 +140,7 @@ Refer to [/test/functional/README.md#style-guidelines](/test/functional/README.m
Coding Style (Doxygen-compatible comments)
------------------------------------------
-Bitcoin Core uses [Doxygen](http://www.doxygen.nl/) to generate its official documentation.
+Bitcoin Core uses [Doxygen](https://www.doxygen.nl/) to generate its official documentation.
Use Doxygen-compatible comment blocks for functions, methods, and fields.
@@ -156,7 +161,7 @@ For example, to describe a function use:
bool function(int arg1, const char *arg2, std::string& arg3)
```
-A complete list of `@xxx` commands can be found at http://www.doxygen.nl/manual/commands.html.
+A complete list of `@xxx` commands can be found at https://www.doxygen.nl/manual/commands.html.
As Doxygen recognizes the comments by the delimiters (`/**` and `*/` in this case), you don't
*need* to provide any commands for a comment to be valid; just a description text is fine.
@@ -203,7 +208,7 @@ Also not picked up by Doxygen:
*/
```
-A full list of comment syntaxes picked up by Doxygen can be found at http://www.doxygen.nl/manual/docblocks.html,
+A full list of comment syntaxes picked up by Doxygen can be found at https://www.doxygen.nl/manual/docblocks.html,
but the above styles are favored.
Recommendations:
@@ -216,7 +221,7 @@ Recommendations:
- Backticks aren't required when referring to functions Doxygen already knows
about; it will build hyperlinks for these automatically. See
- http://www.doxygen.nl/manual/autolink.html for complete info.
+ https://www.doxygen.nl/manual/autolink.html for complete info.
- Avoid linking to external documentation; links can break.
@@ -259,14 +264,15 @@ on all categories (and give you a very large `debug.log` file).
The Qt code routes `qDebug()` output to `debug.log` under category "qt": run with `-debug=qt`
to see it.
-### Testnet and Regtest modes
+### Signet, testnet, and regtest modes
-Run with the `-testnet` option to run with "play bitcoins" on the test network, if you
-are testing multi-machine code that needs to operate across the internet.
+If you are testing multi-machine code that needs to operate across the internet,
+you can run with either the `-signet` or the `-testnet` config option to test
+with "play bitcoins" on a test network.
-If you are testing something that can run on one machine, run with the `-regtest` option.
-In regression test mode, blocks can be created on-demand; see [test/functional/](/test/functional) for tests
-that run in `-regtest` mode.
+If you are testing something that can run on one machine, run with the
+`-regtest` option. In regression test mode, blocks can be created on demand;
+see [test/functional/](/test/functional) for tests that run in `-regtest` mode.
### DEBUG_LOCKORDER
@@ -276,6 +282,33 @@ configure option adds `-DDEBUG_LOCKORDER` to the compiler flags. This inserts
run-time checks to keep track of which locks are held and adds warnings to the
`debug.log` file if inconsistencies are detected.
+### Assertions and Checks
+
+The util file `src/util/check.h` offers helpers to protect against coding and
+internal logic bugs. They must never be used to validate user, network or any
+other input.
+
+* `assert` or `Assert` should be used to document assumptions when any
+ violation would mean that it is not safe to continue program execution. The
+ code is always compiled with assertions enabled.
+ - For example, a nullptr dereference or any other logic bug in validation
+ code means the program code is faulty and must terminate immediately.
+* `CHECK_NONFATAL` should be used for recoverable internal logic bugs. On
+ failure, it will throw an exception, which can be caught to recover from the
+ error.
+ - For example, a nullptr dereference or any other logic bug in RPC code
+ means that the RPC code is faulty and can not be executed. However, the
+ logic bug can be shown to the user and the program can continue to run.
+* `Assume` should be used to document assumptions when program execution can
+ safely continue even if the assumption is violated. In debug builds it
+ behaves like `Assert`/`assert` to notify developers and testers about
+ nonfatal errors. In production it doesn't warn or log anything, though the
+ expression is always evaluated.
+ - For example it can be assumed that a variable is only initialized once,
+ but a failed assumption does not result in a fatal bug. A failed
+ assumption may or may not result in a slightly degraded user experience,
+ but it is safe to continue program execution.
+
### Valgrind suppressions file
Valgrind is a programming tool for memory debugging, memory leak detection, and
@@ -423,27 +456,52 @@ and its `cs_KeyStore` lock for example).
Threads
-------
-- ThreadScriptCheck : Verifies block scripts.
+- [Main thread (`bitcoind`)](https://doxygen.bitcoincore.org/bitcoind_8cpp.html#a0ddf1224851353fc92bfbff6f499fa97)
+ : Started from `main()` in `bitcoind.cpp`. Responsible for starting up and
+ shutting down the application.
+
+- [ThreadImport (`b-loadblk`)](https://doxygen.bitcoincore.org/init_8cpp.html#ae9e290a0e829ec0198518de2eda579d1)
+ : Loads blocks from `blk*.dat` files or `-loadblock=<file>` on startup.
+
+- [ThreadScriptCheck (`b-scriptch.x`)](https://doxygen.bitcoincore.org/validation_8cpp.html#a925a33e7952a157922b0bbb8dab29a20)
+ : Parallel script validation threads for transactions in blocks.
-- ThreadImport : Loads blocks from `blk*.dat` files or `-loadblock=<file>`.
+- [ThreadHTTP (`b-http`)](https://doxygen.bitcoincore.org/httpserver_8cpp.html#abb9f6ea8819672bd9a62d3695070709c)
+ : Libevent thread to listen for RPC and REST connections.
-- ThreadDNSAddressSeed : Loads addresses of peers from the DNS.
+- [HTTP worker threads(`b-httpworker.x`)](https://doxygen.bitcoincore.org/httpserver_8cpp.html#aa6a7bc27265043bc0193220c5ae3a55f)
+ : Threads to service RPC and REST requests.
-- ThreadMapPort : Universal plug-and-play startup/shutdown.
+- [Indexer threads (`b-txindex`, etc)](https://doxygen.bitcoincore.org/class_base_index.html#a96a7407421fbf877509248bbe64f8d87)
+ : One thread per indexer.
-- ThreadSocketHandler : Sends/Receives data from peers on port 8333.
+- [SchedulerThread (`b-scheduler`)](https://doxygen.bitcoincore.org/class_c_scheduler.html#a14d2800815da93577858ea078aed1fba)
+ : Does asynchronous background tasks like dumping wallet contents, dumping
+ addrman and running asynchronous validationinterface callbacks.
-- ThreadOpenAddedConnections : Opens network connections to added nodes.
+- [TorControlThread (`b-torcontrol`)](https://doxygen.bitcoincore.org/torcontrol_8cpp.html#a4faed3692d57a0d7bdbecf3b37f72de0)
+ : Libevent thread for tor connections.
-- ThreadOpenConnections : Initiates new connections to peers.
+- Net threads:
-- ThreadMessageHandler : Higher-level message handling (sending and receiving).
+ - [ThreadMessageHandler (`b-msghand`)](https://doxygen.bitcoincore.org/class_c_connman.html#aacdbb7148575a31bb33bc345e2bf22a9)
+ : Application level message handling (sending and receiving). Almost
+ all net_processing and validation logic runs on this thread.
-- DumpAddresses : Dumps IP addresses of nodes to `peers.dat`.
+ - [ThreadDNSAddressSeed (`b-dnsseed`)](https://doxygen.bitcoincore.org/class_c_connman.html#aa7c6970ed98a4a7bafbc071d24897d13)
+ : Loads addresses of peers from the DNS.
-- ThreadRPCServer : Remote procedure call handler, listens on port 8332 for connections and services them.
+ - [ThreadMapPort (`b-upnp`)](https://doxygen.bitcoincore.org/net_8cpp.html#a63f82a71c4169290c2db1651a9bbe249)
+ : Universal plug-and-play startup/shutdown.
-- Shutdown : Does an orderly shutdown of everything.
+ - [ThreadSocketHandler (`b-net`)](https://doxygen.bitcoincore.org/class_c_connman.html#a765597cbfe99c083d8fa3d61bb464e34)
+ : Sends/Receives data from peers on port 8333.
+
+ - [ThreadOpenAddedConnections (`b-addcon`)](https://doxygen.bitcoincore.org/class_c_connman.html#a0b787caf95e52a346a2b31a580d60a62)
+ : Opens network connections to added nodes.
+
+ - [ThreadOpenConnections (`b-opencon`)](https://doxygen.bitcoincore.org/class_c_connman.html#a55e9feafc3bab78e5c9d408c207faa45)
+ : Initiates new connections to peers.
Ignoring IDE/editor files
--------------------------
@@ -491,7 +549,7 @@ General Bitcoin Core
- *Rationale*: RPC allows for better automatic testing. The test suite for
the GUI is very limited.
-- Make sure pull requests pass Travis CI before merging.
+- Make sure pull requests pass CI before merging.
- *Rationale*: Makes sure that they pass thorough testing, and that the tester will keep passing
on the master branch. Otherwise, all new pull requests will start failing the tests, resulting in
@@ -537,11 +595,6 @@ Common misconceptions are clarified in those sections:
- *Rationale*: This avoids memory and resource leaks, and ensures exception safety.
-- Use `MakeUnique()` to construct objects owned by `unique_ptr`s.
-
- - *Rationale*: `MakeUnique` is concise and ensures exception safety in complex expressions.
- `MakeUnique` is a temporary project local implementation of `std::make_unique` (C++14).
-
C++ data structures
--------------------
@@ -595,6 +648,19 @@ class A
- *Rationale*: Easier to understand what is happening, thus easier to spot mistakes, even for those
that are not language lawyers.
+- Use `Span` as function argument when it can operate on any range-like container.
+
+ - *Rationale*: Compared to `Foo(const vector<int>&)` this avoids the need for a (potentially expensive)
+ conversion to vector if the caller happens to have the input stored in another type of container.
+ However, be aware of the pitfalls documented in [span.h](../src/span.h).
+
+```cpp
+void Foo(Span<const int> data);
+
+std::vector<int> vec{1,2,3};
+Foo(vec);
+```
+
- Prefer `enum class` (scoped enumerations) over `enum` (traditional enumerations) where possible.
- *Rationale*: Scoped enumerations avoid two potential pitfalls/problems with traditional C++ enumerations: implicit conversions to `int`, and name clashes due to enumerators being exported to the surrounding scope.
@@ -708,6 +774,58 @@ the upper cycle, etc.
Threads and synchronization
----------------------------
+- Prefer `Mutex` type to `RecursiveMutex` one
+
+- Consistently use [Clang Thread Safety Analysis](https://clang.llvm.org/docs/ThreadSafetyAnalysis.html) annotations to
+ get compile-time warnings about potential race conditions in code. Combine annotations in function declarations with
+ run-time asserts in function definitions:
+
+ - In functions that are declared separately from where they are defined, the
+ thread safety annotations should be added exclusively to the function
+ declaration. Annotations on the definition could lead to false positives
+ (lack of compile failure) at call sites between the two.
+
+```C++
+// txmempool.h
+class CTxMemPool
+{
+public:
+ ...
+ mutable RecursiveMutex cs;
+ ...
+ void UpdateTransactionsFromBlock(...) EXCLUSIVE_LOCKS_REQUIRED(::cs_main, cs);
+ ...
+}
+
+// txmempool.cpp
+void CTxMemPool::UpdateTransactionsFromBlock(...)
+{
+ AssertLockHeld(::cs_main);
+ AssertLockHeld(cs);
+ ...
+}
+```
+
+```C++
+// validation.h
+class ChainstateManager
+{
+public:
+ ...
+ bool ProcessNewBlock(...) LOCKS_EXCLUDED(::cs_main);
+ ...
+}
+
+// validation.cpp
+bool ChainstateManager::ProcessNewBlock(...)
+{
+ AssertLockNotHeld(::cs_main);
+ ...
+ LOCK(::cs_main);
+ ...
+}
+```
+
- Build and run tests with `-DDEBUG_LOCKORDER` to verify that no potential
deadlocks are introduced. As of 0.12, this is defined by default when
configuring with `--enable-debug`.
@@ -849,7 +967,7 @@ Others are external projects without a tight relationship with our project. Chan
be sent upstream, but bugfixes may also be prudent to PR against Bitcoin Core so that they can be integrated
quickly. Cosmetic changes should be purely taken upstream.
-There is a tool in `test/lint/git-subtree-check.sh` to check a subtree directory for consistency with
+There is a tool in `test/lint/git-subtree-check.sh` ([instructions](../test/lint#git-subtree-checksh)) to check a subtree directory for consistency with
its upstream repository.
Current subtrees include:
@@ -924,7 +1042,7 @@ Scripted diffs
--------------
For reformatting and refactoring commits where the changes can be easily automated using a bash script, we use
-scripted-diff commits. The bash script is included in the commit message and our Travis CI job checks that
+scripted-diff commits. The bash script is included in the commit message and our CI job checks that
the result of the script is identical to the commit. This aids reviewers since they can verify that the script
does exactly what it is supposed to do. It is also helpful for rebasing (since the same script can just be re-run
on the new master commit).
@@ -965,7 +1083,7 @@ Some good examples of scripted-diff:
- [scripted-diff: Rename InitInterfaces to NodeContext](https://github.com/bitcoin/bitcoin/commit/301bd41a2e6765b185bd55f4c541f9e27aeea29d)
uses an elegant script to replace occurrences of multiple terms in all source files.
-- [scripted-diff: Remove g_connman, g_banman globals](https://github.com/bitcoin/bitcoin/commit/301bd41a2e6765b185bd55f4c541f9e27aeea29d)
+- [scripted-diff: Remove g_connman, g_banman globals](https://github.com/bitcoin/bitcoin/commit/8922d7f6b751a3e6b3b9f6fb7961c442877fb65a)
replaces specific terms in a list of specific source files.
- [scripted-diff: Replace fprintf with tfm::format](https://github.com/bitcoin/bitcoin/commit/fac03ec43a15ad547161e37e53ea82482cc508f9)
@@ -1042,13 +1160,6 @@ A few guidelines for introducing and reviewing new RPC interfaces:
- *Rationale*: If not, the call can not be used with name-based arguments.
-- Set okSafeMode in the RPC command table to a sensible value: safe mode is when the
- blockchain is regarded to be in a confused state, and the client deems it unsafe to
- do anything irreversible such as send. Anything that just queries should be permitted.
-
- - *Rationale*: Troubleshooting a node in safe mode is difficult if half the
- RPCs don't work.
-
- Add every non-string RPC argument `(method, idx, name)` to the table `vRPCConvertParams` in `rpc/client.cpp`.
- *Rationale*: `bitcoin-cli` and the GUI debug console use this table to determine how to