LinkedList源码分析,理解比特币区块block数据结构


# 一,LinkedList 源码分析

1,UML类图关系

2,LinkedList结构体

2,实现的主要方法


   private void grow(int minCapacity) {
        // overflow-conscious code
        int oldCapacity = elementData.length;
        int newCapacity = oldCapacity + (oldCapacity >> 1);    (生产新的数组大小是原来的1.5倍)
        if (newCapacity - minCapacity < 0)   (两者取最大值)
            newCapacity = minCapacity;
        if (newCapacity - MAX_ARRAY_SIZE > 0)  (判断是否超过默认数组最大值)
            newCapacity = hugeCapacity(minCapacity);
        // minCapacity is usually close to size, so this is a win:
        elementData = Arrays.copyOf(elementData, newCapacity);
    }


## 区块结构分析

区块是组成区块链的基本单位,可以通过bitcoin-cli命令查看一个区块的基本信息

区块block的数据结构如下图(存储方式为小端):

  

block数据结构


### 创世块

通过bitcoin-cli命令查询一个区块的信息

```tsx
jc@jc-desktop:~/Documents/project/btc$ bitcoin-cli -datadir="/home/jc/Documents/project/btc/bitdata" getblockhash 0
000000000019d6689c085ae165831e934ff763ae46a2a6c172b3f1b60a8ce26f
jc@jc-desktop:~/Documents/project/btc$ bitcoin-cli -datadir="/home/jc/Documents/project/btc/bitdata" getblock 000000000019d6689c085ae165831e934ff763ae46a2a6c172b3f1b60a8ce26f
{
  "hash": "000000000019d6689c085ae165831e934ff763ae46a2a6c172b3f1b60a8ce26f",
  "confirmations": 190381,
  "strippedsize": 285,
  "size": 285,
  "weight": 1140,
  "height": 0,
  "version": 1,
  "versionHex": "00000001",
  "merkleroot": "4a5e1e4baab89f3a32518a88c31bc87f618f76673e2cc77ab2127b7afdeda33b",
  "tx": [
    "4a5e1e4baab89f3a32518a88c31bc87f618f76673e2cc77ab2127b7afdeda33b"
  ],
  "time": 1231006505,
  "mediantime": 1231006505,
  "nonce": 2083236893,
  "bits": "1d00ffff",
  "difficulty": 1,
  "chainwork": "0000000000000000000000000000000000000000000000000000000100010001",
  "nTx": 1,
  "nextblockhash": "00000000839a8e6886ab5951d76f411475428afc90947ee320161bbf18eb6048"
}
```

## 源码分析

我们来看看源代码,block的结构是存放在src/primitives/block.h

```objectivec
// Copyright (c) 2009-2010 Satoshi Nakamoto
// Copyright (c) 2009-2017 The Bitcoin Core developers
// Distributed under the MIT software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.

#ifndef BITCOIN_PRIMITIVES_BLOCK_H
#define BITCOIN_PRIMITIVES_BLOCK_H

#include <primitives/transaction.h>
#include <serialize.h>
#include <uint256.h>

/** Nodes collect new transactions into a block, hash them into a hash tree,
 * and scan through nonce values to make the block's hash satisfy proof-of-work
 * requirements.  When they solve the proof-of-work, they broadcast the block
 * to everyone and the block is added to the block chain.  The first transaction
 * in the block is a special one that creates a new coin owned by the creator
 * of the block.
 * 网络中的节点不断收集新的交易打包到区块中,所有的交易会通过两两哈希的方式形成一个Merkle树
 * 打包的过程就是要完成工作量证明的要求,当节点解出了当前的随机数时,
 * 它就把当前的区块广播到其他所有节点,并且加到区块链上。
 * 区块中的第一笔交易称之为CoinBase交易,是产生的新币,奖励给区块的产生者  
 */
class CBlockHeader
{
public:
    // header
    int32_t nVersion;                //版本
    uint256 hashPrevBlock;     //上一个版本的hash 
    uint256 hashMerkleRoot;   //包含交易信息的Merkle树根
    uint32_t nTime;                  //时间,打包时间的?
    uint32_t nBits;                    //工作量证明(POW)的难度
    uint32_t nNonce;                //要找的符合POW的随机数

    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                      //难度为0说明区块还未创建,区块头为空
    {
        return (nBits == 0);
    }

    uint256 GetHash() const;          //获取哈希

    int64_t GetBlockTime() const     //获取区块时间
    {
        return (int64_t)nTime;
    }
};


class CBlock : public CBlockHeader         //继承自CBlockHeader,拥有其所有成员变量
{
public:
    // network and disk
    std::vector<CTransactionRef> vtx;        //所有交易的容器,指针?

    // memory only
    mutable bool fChecked;                      //交易是否验证

    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;
};

/** 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.
 * The further back it is, the further before the fork it may be.
 * 描述区块链中在其他节点的一个位置,
 * 如果其他节点没有相同的分支,它可以找到一个最近的中继(最近的相同块)。
 * 更进一步地讲,它可能是分叉前的一个位置
 */
struct CBlockLocator                //还不是很清楚这个结构体是用来干嘛的
{
    std::vector<uint256> vHave;

    CBlockLocator() {}

    explicit CBlockLocator(const std::vector<uint256>& vHaveIn) : vHave(vHaveIn) {}

    ADD_SERIALIZE_METHODS;

    template <typename Stream, typename Operation>
    inline void SerializationOp(Stream& s, Operation ser_action) {
        int nVersion = s.GetVersion();
        if (!(s.GetType() & SER_GETHASH))
            READWRITE(nVersion);
        READWRITE(vHave);
    }

    void SetNull()
    {
        vHave.clear();
    }

    bool IsNull() const
    {
        return vHave.empty();
    }
};

#endif // BITCOIN_PRIMITIVES_BLOCK_H
```

-   **BlockHash** 区块哈希值,是通过SHA256算法对区块头信息进行哈希得到的,这个值必须满足POW的DifficultyTarget,该区块才被认为有效。
-   **BlockHeight** 区块高度,是用来标示区块在区块链中的位置。创世区块高度为0,每一个加在后面的区块,区块高度递增1。

  
参考:https://www.jianshu.com/p/da7356e2202b?utm_campaign=maleskine&utm_content=note&utm_medium=seo_notes&utm_source=recommendation
 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值