Bit-Currency source analysis (7)-Data structure-block

Source: Internet
Author: User
0x00 Summary

Block is the basic structure of block chain, is also the carrier of trading information, miners through the form of mining to generate new blocks and get rewards, the new block process is also a transaction packaging process, only to join the block in the transaction will be recognized by all other nodes of the system, is effective. 0x01 Cblockheader

/** * Nodes in the network are constantly collecting new transactions, then a Merkle tree is packaged into a block, the process of packing is to complete the requirements of the work proof, when the node to solve the current random number, * It will broadcast the current block to all other nodes, and add to the block chain.  * The first transaction in the block is called the Coinbase transaction, which is the generated SGD, sent to the creator of the Block/class Cblockheader {public://header int32_t nversion; Version uint256 Hashprevblock; The hash uint256 Hashmerkleroot of the previous block;  Merkle Root uint32_t ntime; Time Stamp uint32_t nbits; POW difficulty uint32_t nnonce;
    The random number to find Cblockheader () {setnull ();

    } add_serialize_methods; Template <typename Stream, typename operation> inline void Serializationop (stream& s, Operation ser_action)
        {ReadWrite (this->nversion);
        ReadWrite (Hashprevblock);
        ReadWrite (Hashmerkleroot);
        ReadWrite (Ntime);
        ReadWrite (nbits);
    ReadWrite (nnonce);
        } void SetNull () {nversion = 0;
        Hashprevblock.setnull ();
        Hashmerkleroot.setnull ();
        ntime = 0;
        nbits = 0;
    nnonce = 0; BOOL IsNull () CONST {return (nbits = 0);

    } uint256 Gethash () const;
    int64_t getblocktime () const {return (int64_t) ntime; }
};
0x02 Cblock
Class Cblock:public Cblockheader {public://Network and disk std::vector<ctransactionref> vtx;//All transactions  Memory only mutable bool fchecked;
    Whether the transaction has been validated and constitutes the Merkle tree Cblock () {setnull ();
        } cblock (const Cblockheader &header) {setnull ();
    * ((cblockheader*) this) = header;

    } add_serialize_methods; Template <typename Stream, typename operation> inline void Serializationop (stream& s, Operation ser_action)
        {ReadWrite (* (cblockheader*) this);
    ReadWrite (VTX);
        } void SetNull () {cblockheader::setnull ();
        Vtx.clear ();
    fchecked = false;
        } cblockheader Getblockheader () const {Cblockheader block;
        Block.nversion = nversion;
        Block.hashprevblock = Hashprevblock;
        Block.hashmerkleroot = Hashmerkleroot;
        Block.ntime = Ntime;
        Block.nbits = nbits;   Block.nnonce      = Nnonce;
    return block;
} std::string ToString () const;
 };

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.