Skip to content

Conversation

CodeShark
Copy link
Contributor

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.

@gavinandresen
Copy link
Contributor

Why namespace db_cpp ? Seems to me the database copy of pchMessageStart could be a static member of the DbEnv.

@CodeShark
Copy link
Contributor Author

I'd rather avoid adding more dependencies in CAddrDB for something that isn't really used elsewhere.

@sipa
Copy link
Member

sipa commented Jan 7, 2013

@gavinandresen That doesn't make sense. This is for peers.dat, which doesn't use BDB at all, and I suppose CDbEnv will be gone as soon as we kick out BDB-based wallets.

@CodeShark
Copy link
Contributor Author

I'm still not clear on exactly where in the code it's best to:

  1. set the magic bytes for CAddrDB
  2. Set the call handlers for net.cpp

I tried running and got this error: http://pastebin.com/tJU9Gsi2
But tried restarting and it initialized correctly, and now it seems to be running fine.

@BitcoinPullTester
Copy link

Automatic sanity-testing: PASSED, see http://jenkins.bluematt.me/pull-tester/03014e2ff26d4ab6195f8b0bfc29c712dec509de for binaries and test log.

@gavinandresen
Copy link
Contributor

RE: namespace db_cpp:

Fine, make it static in CAddrDB, I forgot that peers.dat is not a BDB file (and pass it into CAddrDB constructors? or is CAddrDB a singleton class? )

@CodeShark
Copy link
Contributor Author

@gavinandresen Noted and done.

@CodeShark
Copy link
Contributor Author

cp: writing out/bitcoind.exe': No space left on device cp: writingout/test_bitcoin.exe': No space left on device

WTF?!?!?!

Shall we all chip in and get BlueMatt some more hard disk space?

@CodeShark
Copy link
Contributor Author

I am going to keep pushing commits as an extra backup despite BitcoinPullTester being out of disk space.

@Diapolo
Copy link

Diapolo commented Jan 8, 2013

I think you are doing good work, but I'm sure you will get faster ACKs or merges, if you try to keep pulls smaller. Perhaps @sipa or other core devs can comment.

@gavinandresen
Copy link
Contributor

Our bottleneck is code review and testing.

If you want your pulls to get merged, then you need to help fix that bottleneck-- either recruit reviewers/testers to review/test your own code, or volunteer to help review/test other people's pulls.

Or only refactor code that has good unit tests, so we can be more confident that nothing breaks on any of the three platforms we support.

I am generally against refactoring just to improve the code, unless the refactoring comes with some significant benefit or unit tests.

@gavinandresen
Copy link
Contributor

Oh, also: you might want to help test or update https://github.com/libcoin/libcoin which is a fully refactored version of the core code, that, last I checked, nobody used because nobody trusts it (because so many changes were made without thorough testing).

@CodeShark
Copy link
Contributor Author

The changes I'm making will be well-documented and possible to rigorously review. And if more unit tests are needed, I'd be glad to help write some up.

I'm making incremental changes, hopefully each change easy to understand and track. I haven't changed any actual logic in the code nor implementation details - just moved code around - and in large chunks, not small pieces.

As most of the significant changes thus far are things like moving methods from one class to another or turning them into regular functions, it should be possible to do a fairly comprehensive assessment by doing pattern matches on the codebase and making sure each usage was appropriately modified. i.e. tx.CheckTransaction() becomes CheckTransaction(tx); or tx.GetOutputFor(txin, inputs) becomes inputs.GetOutputFor(txin); etc...

@CodeShark
Copy link
Contributor Author

Also, I'm willing to do comprehensive testing on earlier commits with the hope of getting them merged without having to merge everything at once.

//
ProcessMessagesHandler fnProcessMessages = NULL;
SendMessagesHandler fnSendMessages = NULL;
StartShutdownHandler fnStartShutdown = NULL;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can these be static?

@BitcoinPullTester
Copy link

Automatic sanity-testing: PASSED, see http://jenkins.bluematt.me/pull-tester/4af9a6c7e0d1246852195b47c200a974dff47ab1 for binaries and test log.

@sipa
Copy link
Member

sipa commented Jan 8, 2013

Just for the record: I've been discussing these changes extensively with @CodeShark the past few days, and I think they are very valuable. They should make the code easier to understand and reuse.

Getting 0.8 out now certainly has priority over refactorings, but as these are almost entirely just code-movement changes, merging them shouldn't be too hard.

for (unsigned int i = 0; i < vin.size(); i++)
nResult += GetOutputFor(vin[i], inputs).nValue;
for (unsigned int i = 0; i < tx.vin.size(); i++)
nResult += this->GetOutputFor(tx.vin[i]).nValue;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this "this->" necessary?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nah, it's mostly a stylistic thing, I guess. I can get rid of it if you don't like it.

@BitcoinPullTester
Copy link

Automatic sanity-testing: PASSED, see http://jenkins.bluematt.me/pull-tester/aea31289a59166195d2d7270eacb0905b14d476a for binaries and test log.

@BitcoinPullTester
Copy link

Automatic sanity-testing: PASSED, see http://jenkins.bluematt.me/pull-tester/366fb8aa64a581c22064dae33a3d08769375b9f3 for binaries and test log.

@CodeShark
Copy link
Contributor Author

The core commits will be reorganized into four stages:

    1. move core class methods that should remain class methods implemented in main.cpp out of main.cpp and into core.cpp. (move)
    1. move core class method implementations in main.h that should not be part of core classes out of the class declarations in main.h. (move)
    1. turn methods that should not be part of the core classes into regular functions in main.h and main.cpp, get rid of the method prototypes in the classes, and add function declarations in main.h where necessary. (pattern replacement, add function declarations to main.h)
    1. move the core class declarations to core.h (move)

@BitcoinPullTester
Copy link

Automatic sanity-testing: FAILED MERGE, see http://jenkins.bluematt.me/pull-tester/366fb8aa64a581c22064dae33a3d08769375b9f3 for test log.

This pull does not merge cleanly onto current master

@luke-jr
Copy link
Member

luke-jr commented Jan 30, 2013

Rebase needed

@Diapolo
Copy link

Diapolo commented Jan 30, 2013

I'm sure @CodeShark intents to rework this pull into smaller more logical pieces after 0.8 is final.


class CAccountingEntry;
class CWalletTx;
class CReserveKey;
class COutput;
class CWalletDB;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this needed?

@jgarzik
Copy link
Contributor

jgarzik commented Jun 4, 2013

OK, very much like where this code is going. ACK 90% of it, modulo inline comments made earlier.

One specific criticism with this rebase: the rebase problems were all fixed in a final, appended commit. That does not work, because it breaks bisection properties. Each commit needs to produce a tree that is buildable and passes tests. Otherwise, "git bisect" will not work, and attempting to find a problematic commit in past history becomes more difficult.

I know that makes rebasing more difficult for changes like this, sorry :(

We cannot have: broken commit, broken commit, broken commit, commit that fixes everything.

@sipa
Copy link
Member

sipa commented Jun 4, 2013

I'm very much in favor of the code changes here (and they look move-only to, apart from the registration functions). It's only a first step, but it's a very needed one IMHO.

I agree with the comment about the last commit fixing everything, but apart from that, I'd like to see this merged soon. Since now seems the ideal time for this, I don't mind holding up other pullreqs for a short time, so this can get reviewed and finalized.

@jgarzik
Copy link
Contributor

jgarzik commented Jun 4, 2013

Agreed, RE holding other pullreqs, to avoid the endless rebase pain on @CodeShark 's part.

@sipa
Copy link
Member

sipa commented Jun 6, 2013

@CodeShark You'll still at least need to add core.h to bitcoin-qt.pro, and while you're on it, I don't see any harm in adding core.cpp to the other makefiles too - that'll make it easier to move stuff there in the future.

CodeShark added 7 commits June 5, 2013 23:14
…ctures from main to core beginning with COutPoint.
…ngs to the mempool instance.

Removed AreInputsStandard from CTransaction, made it a regular function in main.
Moved CTransaction::GetOutputFor to CCoinsViewCache.

Moved GetLegacySigOpCount and GetP2SHSigOpCount out of CTransaction into regular functions in main.

Moved GetValueIn and HaveInputs from CTransaction into CCoinsViewCache.

Moved AllowFree, ClientCheckInputs, CheckInputs, UpdateCoins, and CheckTransaction out of CTransaction and into main.

Moved IsStandard and IsFinal out of CTransaction and put them in main as IsStandardTx and IsFinalTx. Moved GetValueOut out of CTransaction into main. Moved CTxIn, CTxOut, and CTransaction into core.

Added minimum fee parameter to CTxOut::IsDust() temporarily until CTransaction is moved to core.h so that CTxOut needn't know about CTransaction.
@CodeShark
Copy link
Contributor Author

@sipa done
@theuni core.h/core.cpp will have to be considered in what you're doing

@CodeShark CodeShark closed this Jun 6, 2013
@CodeShark CodeShark reopened this Jun 6, 2013
@CodeShark
Copy link
Contributor Author

Sorry, hit the close button by accident.

@CodeShark
Copy link
Contributor Author

Alright - code freeze until merge.

@@ -486,6 +488,7 @@ void CDBEnv::Flush(bool fShutdown)
// CAddrDB
//

unsigned char CAddrDB::pchMessageStart[4] = { 0x00, 0x00, 0x00, 0x00 };
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This duplication is quite ugly. Mike's refactor that moves these chain-dependent constants to a separate class is nicer, but you may want to check that it doesn't introduce new dependencies.

@sipa
Copy link
Member

sipa commented Jun 7, 2013

ACK. Code changes look good to me, and I've tested syncing/mining/receiving/sending on testnet. I have a few inline comments left, but those can be dealt with in follow-up commits, so we don't need to stall the merging process too long.

// Distributed under the MIT/X11 software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.

#include "core.h"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why have this implementation file that only includes a header?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

More things are intended to be moved to core (in particular, CBlock), in a second step. As those will include implementation code too, already create a file for them, so e.g. #2748 picks it up already.

@laanwj
Copy link
Member

laanwj commented Jun 9, 2013

I've tested this commit on testnet, no problems found.

jgarzik pushed a commit that referenced this pull request Jun 10, 2013
@jgarzik jgarzik merged commit f59530c into bitcoin:master Jun 10, 2013
sidhujag pushed a commit to syscoin/syscoin that referenced this pull request Jul 13, 2018
* Make the togglePrivateSend button not react to the spacebar or enter. bitcoin#1766

* redo comment

* change `privateSendAuto` focus policy to remain consistent
HashUnlimited pushed a commit to HashUnlimited/chaincoin that referenced this pull request Jul 27, 2018
* Make the togglePrivateSend button not react to the spacebar or enter. bitcoin#1766

* redo comment

* change `privateSendAuto` focus policy to remain consistent
owlhooter pushed a commit to owlhooter/mazacoin-new that referenced this pull request Oct 11, 2018
* Make the togglePrivateSend button not react to the spacebar or enter. bitcoin#1766

* redo comment

* change `privateSendAuto` focus policy to remain consistent
guruvan added a commit to guruvan/maza that referenced this pull request Nov 8, 2018
* MAZA-POS: (5575 commits)
  Mazafication of code More mazafication More mazafications and compile correction fixes fix for build issues fix for build issues fix string in net.cpp correct pow.cpp correct validation.cpp fixing for build errors fix typo Merge remote-tracking branch 'origin/MAZA-POS' into MAZA-POS
  merge to dash rebase
  Release notes 0.12.3.3
  Remove redundant parameter fCheckDuplicateInputs from CheckTransaction
  Fix crash bug with duplicate inputs within a transaction
  Bump to 0.12.3.3
  Release notes 0.12.3.2 (bitcoin#2174)
  Add tests for special rules for slow blocks on devnet/testnet (bitcoin#2176)
  Allow mining min diff for very slow (2h+) blocks (bitcoin#2175)
  Fix issues with selections on Masternode tab (bitcoin#2170)
  Sync mn list and mnw list from 3 peers max (bitcoin#2169)
  A few devnet related fixes (bitcoin#2168)
  Adjust diff for slow testnet/devnet blocks a bit smoother (bitcoin#2161)
  Make PS Buttons not react to spacebar (bitcoin#2154)
  Bump to 0.12.3.2 (bitcoin#2173)
  Bump to 0.12.3.1 (bitcoin#2158)
  Update release notes (bitcoin#2155)
  Use correct protocol when serializing messages in reply to `getdata` (bitcoin#2157)
  Fix p2pkh tests asserts (bitcoin#2153)
  Fix block value/payee validation in lite mode (bitcoin#2148)
  ...
@bitcoin bitcoin locked as resolved and limited conversation to collaborators Sep 8, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

9 participants