I realize this introduction is a bit long, so if you don’t feel like reading skip to the bullet points below under “strategy”.
The codebase as it exists right now has a number of unnecessary dependencies which makes code modularization much more difficult. In particular, the satoshi client was built to handle all bitcoin-related tasks, but its value as a “reference implementation” lies primarily in doing verification and acting as a relay agent as these are the most essential tasks that participating nodes must perform to keeping the network in operation. Things like wallets, historical databases, mining, and notification agents could be written as entirely separate third-party applications without risk to the network’s fundamental integrity.
The basic architecture of a bitcoin node is as follows:
At the core there exist fundamental bitcoin message structures, along with the code necessary for serialization/deserialization. These structures belong in their own source files with minimal dependencies so they can be reused for applications that needn’t perform verification and relay - for instance, filtering and notification agents. Unfortunately, these core structures currently reside for the most part in main.h/main.cpp, one of the central problems this pull request attempts to fix.
On top of these core structures sits a network component that manages sockets, does peer discovery, and handles queueing and dispatching of messages. This component is clearly dependent on the core message structures but does not depend on the specific logic used to verify blocks and transactions nor to identify misbehaving peers nor sign transactions nor maintain a block chain database.
Then we have a scripting engine, signature verification component, and a signing component. Historical database applications do not need signature verification/signing functionality at all. Filtering messages and sending alerts generally does not even require a scripting engine and does fine with basic pattern matching.
The most critical high-level operations needed by a verification/relay node such as the satoshi client are transaction verification; block chain and memory pool management; and detection/management of misbehaving peers. These things are currently primarily implemented in main.h/main.cpp. These are indeed the main operations of the satoshi client - but the core low-level structures should not depend at all on this logic.
Then there’s the UI, but let’s leave that aside for a moment.
Finally there’s init.h/init.cpp, which sets up the particular environment in which the satoshi client runs.
This branch takes the following strategy:
- Remove source file dependencies on main.h and init.h by only including necessary headers wherever possible.
- If source files depend on definitions in main or init, either move the dependent portions into main/init or move the depended-upon portions into separate files.
- If the dependent source files use global variables or functions that clearly belong in either main or init, copy the value over to a class member or a variable with file scope in the dependent source or expose a registration function to set a callback.
- If moving a core class out of main is impossible because its methods depend on variables or functions defined in main, isolate the methods that depend on main and either move them to another class that does belong in main or convert them into regular functions in main.
It is important that all modifications made in this branch are easy to review and to test. This branch does not encourage rewriting things from scratch - only moving them and rearranging them in easily identifiable chunks. Furthermore, the focus of the branch is not so much on coding style and style consistency - but on isolation of functional units and elimination of unnecessary dependencies.