Part of the Bitcoin Takeover Satoshi research archive. Measurements come from the November 2008 pre-release (posted by Ray Dillinger) and Bitcoin v0.1.0. The two builds share 23 of the 24 habits we checked, so these are stable personal conventions, not one-off choices.
Satoshi wrote like a Windows C++ programmer of the late 1990s and 2000s, with a personal naming system layered on top.
Layout
- Four spaces for indentation, never tabs.
- Allman braces, with every
{on its own line andelsenever sharing a line with}. - A space after
if/for/while, but no space between a function name and(in a call. - Short one-line bodies such as
{ return x; }are common. - The return type is on the same line as the function name, not on its own line in GNU style.
Naming
- He uses his own form of Hungarian notation, with about half of all declared names carrying a type prefix:
nHeight,fDebug,strAddress,vchPubKey,mapWallet,pindex. - Booleans always start with
f, neverb(139 to 0 in v0.1). - Class names start with
C(CBlock,CWallet,CNode), and functions are PascalCase. - snake_case, Microsoft-style
dw/lp/szprefixes andm_member prefixes are almost absent.
Types and pointers
- The
*and&sit on the type:CNode* pnode,const uint256& hash. - He uses a named 64-bit
int64typedef throughout.
Idioms
- He writes
//line comments, not/* */. - He uses heavy
printfdebug output. - He wrote his own block macros:
foreach,loop,CRITICAL_BLOCK. - There is no
goto, there are very few#defines,== NULLcomparisons are rare, and hex literals are lowercase.
Samples from Satoshi's code
The excerpts below are copied verbatim from the November 2008 pre-release posted by Ray Dillinger, and from Bitcoin v0.1.0. Bitcoin v0.1.0 is released under the MIT/X11 license, copyright 2009 Satoshi Nakamoto. The November 2008 files carry a 2008 copyright and the same MIT text in their headers. Line numbers refer to the copies in this research archive. Ellipses mark where a few lines were skipped; nothing else was edited.
The style was already settled in November 2008
From the November 2008 pre-release, main.h, lines 33–38 and 46–52:
static const unsigned int MAX_SIZE = 0x02000000;
static const int64 COIN = 1000000;
static const int64 CENT = 10000;
static const int64 TRANSACTIONFEE = 1 * CENT; /// change this to a user options setting, optional fee can be zero
///static const unsigned int MINPROOFOFWORK = 40; /// need to decide the right difficulty to start with
static const unsigned int MINPROOFOFWORK = 20; /// ridiculously easy for testing
...
extern map<uint256, CBlockIndex*> mapBlockIndex;
extern const uint256 hashGenesisBlock;
extern CBlockIndex* pindexGenesisBlock;
extern int nBestHeight;
extern CBlockIndex* pindexBest;
extern unsigned int nTransactionsUpdated;
extern int fGenerateBitcoins;
This is from the four-file preview, two months before v0.1.0. The int64 typedef, the C-prefixed classes, the n/f/map/pindex names, pointers written as CBlockIndex* pindexBest, lowercase hex, and the /// aside comments are already in place, including the note about difficulty. The two builds sit 0.30 apart on the composite measure, against 1.0 to 1.75 for every suspect we scored. The differences are content, not style.
A C-prefixed class, Allman braces and foreach
From Bitcoin v0.1.0, main.h, lines 353–363 and 398–406:
//
// The basic transaction that is broadcasted on the network and contained in
// blocks. A transaction can contain multiple inputs and outputs.
//
class CTransaction
{
public:
int nVersion;
vector<CTxIn> vin;
vector<CTxOut> vout;
int nLockTime;
...
bool IsFinal() const
{
if (nLockTime == 0 || nLockTime < nBestHeight)
return true;
foreach(const CTxIn& txin, vin)
if (!txin.IsFinal())
return false;
return true;
}
The opening brace is on its own line (Allman style) and the indent is four spaces, never tabs. Members take an n prefix, methods are PascalCase, and the loop is his foreach macro, with the & bound to the type: const CTxIn& txin. Independently written pre-Bitcoin code from the suspects does not combine this class-naming habit with that macro. The closest independent match, Wei Dai's Crypto++, shares the Allman braces and PascalCase, but not the C prefix, the Hungarian names, or foreach.
Hungarian names on the block index
From Bitcoin v0.1.0, main.h, lines 1009–1024 and 1064–1067:
class CBlockIndex
{
public:
const uint256* phashBlock;
CBlockIndex* pprev;
CBlockIndex* pnext;
unsigned int nFile;
unsigned int nBlockPos;
int nHeight;
// block header
int nVersion;
uint256 hashMerkleRoot;
unsigned int nTime;
unsigned int nBits;
unsigned int nNonce;
...
bool IsInMainChain() const
{
return (pnext || this == pindexBest);
}
This is the densest example of his personal Hungarian dialect: nHeight, nFile, phashBlock, pprev, pnext, and pindexBest. The * binds to the type (CBlockIndex* pprev), which he did 97% of the time. No independent pre-2009 corpus we scored uses this prefix mix at anything like his rate, which is about half of all declared names. Where the same names show up later, in Gavin Andresen's and Peter Todd's Bitcoin work, they are inside Bitcoin's own tree, after those conventions had been absorbed.
The int64 typedef, and the foreach, loop and CRITICAL_BLOCK macros
From Bitcoin v0.1.0, util.h, lines 6–12, 21–22 and 125–131:
#if defined(_MSC_VER) || defined(__BORLANDC__)
typedef __int64 int64;
typedef unsigned __int64 uint64;
#else
typedef long long int64;
typedef unsigned long long uint64;
#endif
...
#define foreach BOOST_FOREACH
#define loop for (;;)
...
// WARNING: This will catch continue and break!
// break is caught with an assertion, but there's no way to detect continue.
// I'd rather be careful than suffer the other more error prone syntax.
// The compiler will optimise away all this loop junk.
#define CRITICAL_BLOCK(cs) \
for (bool fcriticalblockonce=true; fcriticalblockonce; assert(("break caught by CRITICAL_BLOCK!", !fcriticalblockonce)), fcriticalblockonce=false) \
for (CCriticalBlock criticalblock(cs); fcriticalblockonce && (cs.pszFile=__FILE__, cs.nLine=__LINE__, true); fcriticalblockonce=false, cs.pszFile=NULL, cs.nLine=0)
He wraps 64-bit integers in a homemade int64/uint64 typedef, so the same name works with Microsoft's __int64 and with long long. foreach, loop and CRITICAL_BLOCK are his own block macros. The comment above CRITICAL_BLOCK is typical: // only, slightly informal, and even "optimise" in British spelling. None of the independent pre-Bitcoin corpora define this trio of macros. They appear later only in code that grew out of Bitcoin, such as Jed McCaleb's early rippled sources.
The macros in use, with a typical debug print
From Bitcoin v0.1.0, main.cpp, lines 87–105:
//////////////////////////////////////////////////////////////////////////////
//
// mapWallet
//
bool AddToWallet(const CWalletTx& wtxIn)
{
uint256 hash = wtxIn.GetHash();
CRITICAL_BLOCK(cs_mapWallet)
{
// Inserts only if not already there, returns tx inserted or tx found
pair<map<uint256, CWalletTx>::iterator, bool> ret = mapWallet.insert(make_pair(hash, wtxIn));
CWalletTx& wtx = (*ret.first).second;
bool fInsertedNew = ret.second;
if (fInsertedNew)
wtx.nTimeReceived = GetAdjustedTime();
//// debug print
printf("AddToWallet %s %s\n", wtxIn.GetHash().ToString().substr(0,6).c_str(), fInsertedNew ? "new" : "update");
A short function can carry many of the habits at once: const CWalletTx&, CRITICAL_BLOCK(cs_mapWallet), mapWallet, the boolean fInsertedNew (never bInsertedNew), a //// debug print marker, and a printf. Heavy printf logging is not unique, but the //// debug notes and the f boolean prefix together are. Microsoft's own Hungarian uses b for booleans; Paul Le Roux's E4M code follows that Microsoft dialect, not this one.
One-line inline bodies, with the * on the type
From Bitcoin v0.1.0, main.h, lines 132–142:
class CInPoint
{
public:
CTransaction* ptx;
unsigned int n;
CInPoint() { SetNull(); }
CInPoint(CTransaction* ptxIn, unsigned int nIn) { ptx = ptxIn; n = nIn; }
void SetNull() { ptx = NULL; n = -1; }
bool IsNull() const { return (ptx == NULL && n == -1); }
};
Short methods are often written as a single { return x; } line. In v0.1.0 there are 23.8 of these per thousand lines. The pointer is CTransaction* ptx, not CTransaction *ptx. Wei Dai's Crypto++ is close on bracing and also uses compact one-line bodies, but binds the * to the name, not the type, about 94% of the time. The C prefix on both CInPoint and CTransaction is again Satoshi's, not Crypto++'s.
A densely prefixed signature, and return error(...)
From Bitcoin v0.1.0, main.cpp, lines 772–773 and 822–826:
bool CTransaction::ConnectInputs(CTxDB& txdb, map<uint256, CTxIndex>& mapTestPool, CDiskTxPos posThisTx, int nHeight, int64& nFees, bool fBlock, bool fMiner, int64 nMinFee)
{
...
// If prev is coinbase, check that it's matured
if (txPrev.IsCoinBase())
for (CBlockIndex* pindex = pindexBest; pindex && nBestHeight - pindex->nHeight < COINBASE_MATURITY-1; pindex = pindex->pprev)
if (pindex->nBlockPos == txindex.pos.nBlockPos && pindex->nFile == txindex.pos.nFile)
return error("ConnectInputs() : tried to spend coinbase at depth %d", nBestHeight - pindex->nHeight);
The function signature packs mapTestPool, nHeight, int64& nFees, fBlock and fMiner into one line, then the body walks a CBlockIndex* pindex and fails with return error("..."). That error() helper prints a message and returns false; it is one of his regular idioms, alongside the heavy printf logging. The same cluster of prefixes does not appear in the independent pre-Bitcoin corpora.
vch and str prefixes, and const T&
From Bitcoin v0.1.0, db.h, lines 378–387 and 417–420:
bool ReadKey(const vector<unsigned char>& vchPubKey, CPrivKey& vchPrivKey)
{
vchPrivKey.clear();
return Read(make_pair(string("key"), vchPubKey), vchPrivKey);
}
bool WriteKey(const vector<unsigned char>& vchPubKey, const CPrivKey& vchPrivKey)
{
return Write(make_pair(string("key"), vchPubKey), vchPrivKey, false);
}
...
inline bool SetAddressBookName(const string& strAddress, const string& strName)
{
return CWalletDB().WriteName(strAddress, strName);
}
Byte buffers are vchPubKey and vchPrivKey; strings are strAddress and strName. References are written const vector<unsigned char>& and const string&, with the & on the type. Together with n, f, map and p, these prefixes are the real fingerprint. Sharing Allman braces or PascalCase only points to the same late-1990s Visual C++ culture, not to the same person.
Why it matters in hindsight
The naming system is the real fingerprint. No suspect's independent pre-Bitcoin code uses it. The closest independent code is Wei Dai's Crypto++, which matches 15 of the 23 habits that apply to it (one of the 24 is not applicable to its code) because of the shared bracing and PascalCase, and is still far away on the overall measure (1.02, against 0.30 between Satoshi's own two builds). The code that looks most like Satoshi's (Gavin Andresen's, Jed McCaleb's and Peter Todd's) was written inside Bitcoin's codebase after people had absorbed his conventions. Coding style can be learned, so resemblance after 2009 is not evidence of authorship.
Full method, data and source mirrors are in the research archive.
