Block data structure of bitcoin source scenario analysis

Source: Internet
Author: User
Tags bool data structures hash
Chunk (block) data structure in disk
Blocks are composed of block size + transaction data, so Cblock is inherited from Cblockheader

Class Cblock:public Cblockheader {public://Network and disk std::vector<ctransactionref> vtx;
Memory only mutable bool fchecked; }
Class Cblockheader {public://header int32_t nversion;     uint256 Hashprevblock;     uint256 Hashmerkleroot;     uint32_t ntime;     uint32_t nBits; uint32_t nnonce; }
Transaction data

Class CTransaction {public://Default transaction version. static const int32_t current_version=2;
Changing the default transaction version requires a and step Process:first//Adapting relay policy by bumping MAX     _standard_version, and then later date//bumping the default current_version at which point both current_version and     Max_standard_version'll be equal. static const int32_t max_standard_version=2;
The local variables is made const to prevent unintended modification//without updating the cached hash value. However, ctransaction is not//actually immutable; Deserialization and assignment are implemented,//and bypass the constness.     This is safe, as they update the entire//structure, including the hash.     Const std::vector<ctxin> VIN;     Const std::vector<ctxout> Vout;     Const int32_t Nversion; Const uint32_t Nlocktime;
Private:/** Memory only. */const UINT256 hash; }

A transaction has multiple inputs txin and multiple outputs Txout constitute class CTxIn {Public:coutpoint prevout;     CScript Scriptsig;     uint32_t nsequence; Cscriptwitness scriptwitness; //! Only serialized through CTransaction}
Trade output txout, i.e. utxo (unspent Transaction output)
Class Ctxout {Public:camount nvalue; CScript Scriptpubkey; }

The above data structures exist in the form of files in the order of ~/.bitcoin/block/xxx.data
Run-time chunk (block) data structureThe above block occupies too much space, the runtime reads from the disk to save in memory is not realistic, so the system runs only load Blockheader, block's transaction content data is dynamic on-demand loading. The Block object at runtime is Cblockindex, which file is Nfile (which ) and Mdatapos (the location of the file) field indicate the storage information for the chunk contents.
Class Cblockindex {public://! pointer to the hash of the block, if any. Memory is owned by this cblockindex const uint256* Phashblock;
//! Pointer to the index of the predecessor of this block cblockindex* Pprev;
//! Pointer to the index of some further predecessor of this block cblockindex* Pskip;
//! Height of the entry in the chain. The Genesis block has height 0 int nheight;
//! Which # File this block was stored in (Blk?????. DAT) int nFile;
//! Byte offset within Blk?????. DAT where this block's data is stored unsigned int ndatapos;
//! Byte offset within Rev?????. DAT where this block's undo data is stored unsigned int nundopos;
//! (Memory only) Total amount of work (expected number of hashes) in the chain, and including this block arith_uint256 nchainwork;
//!     Number of transactions in this block. //! Note:in a potential Headers-first mode, this number cannot be relied upon unsigned int nTx;
//! (Memory only)     Number of transactions in the chain, and including this block. //!     This value is Non-zero only if and only if transactions for the this block and all its parents is available. //! Change to 64-bit type when necessary; Won ' t happen before 2030 unsigned int nchaintx;
//! Verification status of this block. See enum Blockstatus uint32_t nstatus;
//!     Block header int32_t Nversion;     uint256 Hashmerkleroot;     uint32_t ntime;     uint32_t nBits; uint32_t nnonce;
//! (Memory only)     Sequential ID assigned to distinguish order in which blocks is received. int32_t Nsequenceid;
//! (Memory only)     Maximum Ntime in the chain up to and including this block. unsigned int ntimemax; }
Cblockindex contains only chunk header information, and new Pprev information is added to maintain the list structure. At the same time, through the Nfile,ndatapos indirect reference block transaction data, when the need for trading information through the Datapos, nfile information can be read from the corresponding file. Cblockindex corresponding storage structure is cdiskblockindex, one more Hashprev field, this field corresponds to Pprev in Cblockindex, because the object pointer pprev existence of the database is meaningless, Only hash (Hashprev) can be stored
Class Cdiskblockindex:public Cblockindex {public:uint256 hashprev;}
The Cblockindex object is written to the database through the Pblocktree object, Pblocktree Cblockindex is converted to Cdiskblockindex stored in a database such as LEVELDB, and the database file is ~/.bitcoin/ Blocks/index/***.ldb
Std::unique_ptr<cblocktreedb> Pblocktree
BOOL Cblocktreedb::readblockfileinfo (int nFile, Cblockfileinfo &info) {return Read (Std::make_pair (db_block_files , nFile), info); }
BOOL Cblocktreedb::writereindexing (bool freindexing) {if (freindexing) return Write (Db_reindex_flag, ' 1 '); else return Erase (Db_reindex_flag); }
BOOL Cblocktreedb::readreindexing (bool &freindexing) {freindexing = Exists (Db_reindex_flag); return true; }
BOOL Cblocktreedb::readlastblockfile (int &nfile) {return Read (Db_last_block, nFile);}
bitcoin coin related data structuresThe user's funds are composed of Utxo, a utxo equivalent to a coin, a coin object is coin. Coin is also stored in the database, stored in <key, coin>, and key is the ctxout value of the Coinentry (serialize) object.
Class Coin {public://! unspent transaction output ctxout out;
//! Whether containing transaction was a coinbase unsigned int fcoinbase:1;
//! At which height this containing transaction is included in the active block chain uint32_t nheight:31;
//!     Construct a Coin from a ctxout and height/coinbase information. Coin (ctxout&& outin, int nheightin, BOOL Fcoinbasein): Out (Std::move (Outin)), Fcoinbase (Fcoinbasein), NHeight ( Nheightin) {} Coin (const ctxout& outin, int nheightin, BOOL Fcoinbasein): Out (Outin), Fcoinbase (Fcoinbasein), Nhei Ght (nheightin) {}
void Clear () {out.         SetNull ();         Fcoinbase = false;     nheight = 0; }
//! Empty constructor Coin (): Fcoinbase (False), nheight (0) {}
BOOL Iscoinbase () const {return fcoinbase; }
Template<typename stream> void Serialize (Stream &s) const {
ASSERT (!         Isspent ());         uint32_t code = nheight * 2 + fcoinbase;         :: Serialize (S, varint (code));     :: Serialize (S, Ctxoutcompressor (REF (out))); }
Template<typename stream> void Unserialize (Stream &s) {uint32_t code = 0;         :: Unserialize (S, varint (code));         nheight = code >> 1;         Fcoinbase = code & 1;     :: Unserialize (S, Ctxoutcompressor (out)); }
BOOL Isspent () const {return out.     IsNull (); } };
struct Coinentry {coutpoint* outpoint;     Char key; Explicit coinentry (const coutpoint* PTR): Outpoint (Const_cast<coutpoint*> (PTR)), key (Db_coin) {}
Template<typename stream>//key is db_coin+hash+n void Serialize (Stream &s) const {s << key;         S << outpoint->hash;     S << varint (outpoint->n); }w
Template<typename stream> void Unserialize (stream& s) {s >> key;         S >> outpoint->hash;     S >> varint (outpoint->n); } };
}
Coin Access Interface abstract class Ccoinsview

Ccoins

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.