# Skills Needed to Make Bitcoin <span style="color: red;">(this document is a work in progress)</span> When examining the original BitCoin source code from 2009-2011, it is clear that it was extremely over-engineered for a simple cryptocurrency. This document sheds light on the enterprise architectural knowledge and coding skills required to build BitCoin. Enterprise data pipeline architecture, not simply cryptocurrency. ## Architecture - Publish and subscribe model with multiple channels ### Atoms ### Stack Machine (bitcoin script) ### IRC - Internet Relay Chat ### Big Numbers #### UInt256 ### base58 #### Why base-58 instead of standard base-64 encoding? - Don't want 0OIl characters that look the same in some fonts and could be used to create visually identical looking account numbers. - A string with non-alphanumeric characters is not as easily accepted as an account number. - E-mail usually won't line-break if there's no punctuation to break at. - Double-clicking selects the whole number as one word if it's all alphanumeric. ``` Ambiguous characters: 0OIl ``` ## Tools and Disciplines ### BerkleyDB - Why was BerkleyDB chosen? Created in 1994. Key-value database. Transactional. Highly recoverable. - LevelDB - Why not choose SQLite? No write-ahead logging in 2007-2010. - [Berkeley DB Reference Guide: Berkeley DB and the memory pool](https://web.mit.edu/ghudson/dev/nognome/third/evolution-data-server/libdb/docs/ref/mp/intro.html) - BerkleyDB memory pool ### Database Engineering - Two-phase commits - Write-ahead logging - Highly recoverable is a key need - Transaction checkpointing ### Bitcoin Databases #### Marketplace - Primary code files: market.cpp and market.h - Database file: market.dat - Classes: CUser, CReview, CProduct #### Addresses - Primary code files: market.cpp and market.h - Database file: addr.dat #### Wallets - Primary code files: market.cpp and market.h - Database file: wallets.dat #### Transactions - Primary code files: market.cpp and market.h - Database file: blkindex.dat #### Reviews - The purpose is for users to write reviews of transactions - Primary code files: market.cpp and market.h - Database file: rewiews.dat ## Coding ### C++ (original source code) - Use of generics (templates) - Excellent use of constants - Threading - multi-threaded (CRITICAL_BLOCK) - Exception handling - Understands threading very well - Trivia: The term blockchain never appeared in the prerelease source code. It was referred to as a 'timechain'. In the main.h file, line 1009, of the 09 Jan 2009 code, the term 'timechain' was replaced with 'block chain'. ### Classes in the original source code NOTE: The original source code was written in Microsoft Visual C++ 6.0 #### Main (main.cpp and main.h) - **CInPoint:** Inbound transaction pointer - **COutPoint:** Outbound transaction pointer - **CDiskTxPos:** - **CCoinBase:** - **CTransaction:** The basic transaction that is broadcasted on the network and contained in blocks.  A transaction can contain multiple inputs and outputs. - **CTxIn (CTransaction):** An input of a transaction.  It contains the location of the previous transaction's output that it claims and a signature that matches the output's public key. - **CTxOut** (CTransacton): An output of a transaction.  It contains the public key that the next input must be able to sign with to claim it. - **CWalletTx** (CTransaction): - **CBlock:** Blocks are appended to blk0001.dat files on disk. Their location on disk is indexed by CBlockIndex objects in memory. - **CBlockIndex:** The block chain (formerly 'timechain') is a tree shaped structure starting with the genesis block at the root, with each block potentially having multiple candidates to be the next block. - **CDiskBlockIndex:** Used to marshal pointers into hashes for db storage. - **CBlockLocator:** Describes a place in the block chain to another node such that if the other node doesn't have the same branch, it can find a recent common trunk. - **CKeyItem:** - **CMerkleTx:** A transaction with a Merkle branch linking it to the timechain. #### Nodes (net.cpp and net.h) - **CNode**: - **CMessageHeader**: - **CAddress**: - **CInv**: - **CRequestTracker**: #### Bitcoin Script (script.cpp and script.h) - **CScript:** Bitcoin script (stack machine) - **CTransaction:** #### Marketplace (market.cpp and market.h) - **CUser**: - **CReview**: - **CProduct**: #### Serialization (serialize.h) - **CDataStream:** Double ended buffer combining vector and stream-like interfaces. - **CAutoFile:** Automatic closing wrapper for FILE* - **CFlatData:** Wrapper for serializing arrays and POD - **CFixedFieldString:** A string stored as a fixed length field #### Unsigned Integers (uint256.h) - **uint160:** A 160-bit unsigned integer. - **uint256:** A 256-bit unsigned integer. #### Utility (util.cpp and util.h) - **CCriticalSection:** A wrapper to automatically initialize critical section - **CCriticalBlock:** Automatically leave critical section when leaving block, needed for exception safety - CTryCriticalBlock: #### Big Numbers (bignum.h) - **CBigNum**: - **bignum_error**: - **CAutoBN_CTX**: #### About .h and .cpp files Header files (.h) contain declarations of classes, functions, and variables, while .cpp files contain the actual implementations of those declarations. This separation helps organize code and allows multiple .cpp files to use the same declarations without redefining them. ##### Hardcoded IP Addresses > In net.cpp - [72.233.89.199 - IP Address Lookup (IPv4 & IPv6)](https://ip-lookup.net/?ip=72.233.89.199) ##### See Also > [Berkeley DB is key to understanding Bitcoin and Satoshi Nakamoto - YouTube](https://youtu.be/9T9WR4rmC3E?si=IeaD9uzGeB3xUje0) > [Merkle tree - Wikipedia](https://en.wikipedia.org/wiki/Merkle_tree) ##### Relevant Search Tags > #bitcoin #bitcoin-tech