比特幣bitcoin源碼解析之重要流程詳解

1. 重要處理流程詳解

1.1交易

1.1.1 交易連接輸入ConnectInputs

  • Ctransaction:: ConnectInputs對應的處理流程
    對交易的輸入進行判斷,並對交易輸入在對應交易輸入索引中進行佔用(標記爲花費),並將對應的交易保存起來。Ctransaction:: ConnectInputs對應的處理流程源碼如下:
// 交易輸入鏈接,將對應的交易輸入佔用對應的交易輸入的花費標記
bool CTransaction::ConnectInputs(CTxDB& txdb, map<uint256, CTxIndex>& mapTestPool, CDiskTxPos posThisTx, int nHeight, int64& nFees, bool fBlock, bool fMiner, int64 nMinFee)
{
    // 佔用前一個交易對應的花費指針
    // Take over previous transactions' spent pointers
    if (!IsCoinBase())
    {
        int64 nValueIn = 0;
        for (int i = 0; i < vin.size(); i++)
        {
            COutPoint prevout = vin[i].prevout;

            // Read txindex
            CTxIndex txindex;
            bool fFound = true;
            if (fMiner && mapTestPool.count(prevout.hash))
            {
                // Get txindex from current proposed changes
                txindex = mapTestPool[prevout.hash];
            }
            else
            {
                // Read txindex from txdb
                fFound = txdb.ReadTxIndex(prevout.hash, txindex);
            }
            if (!fFound && (fBlock || fMiner))
                return fMiner ? false : error("ConnectInputs() : %s prev tx %s index entry not found", GetHash().ToString().substr(0,6).c_str(),  prevout.hash.ToString().substr(0,6).c_str());

            // Read txPrev
            CTransaction txPrev;
            if (!fFound || txindex.pos == CDiskTxPos(1,1,1))
            {
                // Get prev tx from single transactions in memory
                CRITICAL_BLOCK(cs_mapTransactions)
                {
                    if (!mapTransactions.count(prevout.hash))
                        return error("ConnectInputs() : %s mapTransactions prev not found %s", GetHash().ToString().substr(0,6).c_str(),  prevout.hash.ToString().substr(0,6).c_str());
                    txPrev = mapTransactions[prevout.hash];
                }
                if (!fFound)
                    txindex.vSpent.resize(txPrev.vout.size());
            }
            else
            {
                // Get prev tx from disk
                if (!txPrev.ReadFromDisk(txindex.pos))
                    return error("ConnectInputs() : %s ReadFromDisk prev tx %s failed", GetHash().ToString().substr(0,6).c_str(),  prevout.hash.ToString().substr(0,6).c_str());
            }

            if (prevout.n >= txPrev.vout.size() || prevout.n >= txindex.vSpent.size())
                return error("ConnectInputs() : %s prevout.n out of range %d %d %d", GetHash().ToString().substr(0,6).c_str(), prevout.n, txPrev.vout.size(), txindex.vSpent.size());

            // 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);

            // Verify signature
            if (!VerifySignature(txPrev, *this, i))
                return error("ConnectInputs() : %s VerifySignature failed", GetHash().ToString().substr(0,6).c_str());

            // Check for conflicts
            if (!txindex.vSpent[prevout.n].IsNull())
                return fMiner ? false : error("ConnectInputs() : %s prev tx already used at %s", GetHash().ToString().substr(0,6).c_str(), txindex.vSpent[prevout.n].ToString().c_str());

            // 標記前一個交易對應的交易索引對應的花費標記
            // Mark outpoints as spent
            txindex.vSpent[prevout.n] = posThisTx;

            // Write back
            if (fBlock)
                txdb.UpdateTxIndex(prevout.hash, txindex);
            else if (fMiner)
                mapTestPool[prevout.hash] = txindex;

            nValueIn += txPrev.vout[prevout.n].nValue;
        }

        // Tally transaction fees
        int64 nTxFee = nValueIn - GetValueOut();
        if (nTxFee < 0)
            return error("ConnectInputs() : %s nTxFee < 0", GetHash().ToString().substr(0,6).c_str());
        if (nTxFee < nMinFee)
            return false;
        nFees += nTxFee;
    }

    if (fBlock)
    {
        // Add transaction to disk index
        if (!txdb.AddTxIndex(*this, posThisTx, nHeight))
            return error("ConnectInputs() : AddTxPos failed");
    }
    else if (fMiner)
    {
        // 如果是礦工,將對應的交易放入對應的交易測試池中
        // Add transaction to test pool
        mapTestPool[GetHash()] = CTxIndex(CDiskTxPos(1,1,1), vout.size());
    }

    return true;
}

1.1.2 交易斷開連接輸入DisconnectInputs

  • Ctransaction:: DisconnectInputs對應的處理流程
    釋放交易對應的輸入佔用的標記,即是釋放交易輸入對應的交易索引中的標記,並將交易從庫或者mapTestPool中進行移除。Ctransaction:: DisconnectInputs對應的處理流程源碼如下:
// 斷開連接輸入,就是釋放交易對應的輸入的佔用:即是釋放交易輸入對應的交易索引的標記佔用
bool CTransaction::DisconnectInputs(CTxDB& txdb)
{
    // 放棄或者讓出前一個交易對應的花費標記指針
    // Relinquish previous transactions' spent pointers
    if (!IsCoinBase()) // 幣基
    {
        foreach(const CTxIn& txin, vin)
        {
            COutPoint prevout = txin.prevout;

            // Get prev txindex from disk
            CTxIndex txindex;
            // 從數據庫中讀取對應的交易的索引
            if (!txdb.ReadTxIndex(prevout.hash, txindex))
                return error("DisconnectInputs() : ReadTxIndex failed");

            if (prevout.n >= txindex.vSpent.size())
                return error("DisconnectInputs() : prevout.n out of range");

            // Mark outpoint as not spent
            txindex.vSpent[prevout.n].SetNull();

            // Write back
            txdb.UpdateTxIndex(prevout.hash, txindex);
        }
    }

    // 將當前交易從交易索引表中移除
    // Remove transaction from index
    if (!txdb.EraseTxIndex(*this))
        return error("DisconnectInputs() : EraseTxPos failed");

    return true;
}

1.1.3 交易接受處理

  • CTransaction::AcceptTransaction對應的處理流程
    判斷交易能不能被接受,如果能接受將對應的交易放入全局變量中mapTransactions,mapNextTx中CTransaction::AcceptTransaction對應的處理流程源碼如下:
// 判斷這邊交易能不能被接受,如果能接受將對應的交易放入全局變量中mapTransactions,mapNextTx中
bool CTransaction::AcceptTransaction(CTxDB& txdb, bool fCheckInputs, bool* pfMissingInputs)
{
    if (pfMissingInputs)
        *pfMissingInputs = false;

    // 幣基交易僅僅在塊中有效,幣基交易不能做爲一個單獨的交易
    // Coinbase is only valid in a block, not as a loose transaction
    if (IsCoinBase())
        return error("AcceptTransaction() : coinbase as individual tx");

    if (!CheckTransaction())
        return error("AcceptTransaction() : CheckTransaction failed");

    // 判斷當前交易是否我們已經接收到過了
    // Do we already have it?
    uint256 hash = GetHash();
    CRITICAL_BLOCK(cs_mapTransactions)
        if (mapTransactions.count(hash)) // 判斷內存對象map中是否已經存在
            return false;
    if (fCheckInputs)
        if (txdb.ContainsTx(hash)) // 判斷交易db中是否已經存在
            return false;

    // 判斷當前交易對象是否和內存中的交易對象列表衝突
    // Check for conflicts with in-memory transactions
    CTransaction* ptxOld = NULL;
    for (int i = 0; i < vin.size(); i++)
    {
        COutPoint outpoint = vin[i].prevout;
        // 根據當前交易對應的輸入交易,獲得對應輸入交易對應的輸出交易
        if (mapNextTx.count(outpoint))
        {
            // Allow replacing with a newer version of the same transaction
            // i ==0 爲coinbase,也就是coinbase可以替換
            if (i != 0)
                return false;
            // 相對於當前交易更老的交易
            ptxOld = mapNextTx[outpoint].ptx;
            if (!IsNewerThan(*ptxOld)) // 判斷是否比原來交易更新,通過nSequences判斷
                return false;
            for (int i = 0; i < vin.size(); i++)
            {
                COutPoint outpoint = vin[i].prevout;
                // 當前交易的輸入在內存對象mapNextTx對應的輸出如果都存在,且都指向原來老的交易,則接收此交易
                if (!mapNextTx.count(outpoint) || mapNextTx[outpoint].ptx != ptxOld)
                    return false;
            }
            break;
        }
    }

    // 對前交易進行校驗和設置前交易對應的輸出爲花費標記
    // Check against previous transactions
    map<uint256, CTxIndex> mapUnused;
    int64 nFees = 0;
    if (fCheckInputs && !ConnectInputs(txdb, mapUnused, CDiskTxPos(1,1,1), 0, nFees, false, false))
    {
        if (pfMissingInputs)
            *pfMissingInputs = true;
        return error("AcceptTransaction() : ConnectInputs failed %s", hash.ToString().substr(0,6).c_str());
    }

    // 將當前交易存儲在內存,如果老的交易存在,則從內存中將對應的交易移除
    // Store transaction in memory
    CRITICAL_BLOCK(cs_mapTransactions)
    {
        if (ptxOld)
        {
            printf("mapTransaction.erase(%s) replacing with new version\n", ptxOld->GetHash().ToString().c_str());
            mapTransactions.erase(ptxOld->GetHash());
        }
        // 將當前交易存儲到內存對象中
        AddToMemoryPool();
    }

    // 如果老的交易存在,則從錢包中將老的交易移除
    ///// are we sure this is ok when loading transactions or restoring block txes
    // If updated, erase old tx from wallet
    if (ptxOld)
        // 將交易從錢包映射對象mapWallet中移除,同時將交易從CWalletDB中移除
        EraseFromWallet(ptxOld->GetHash());

    printf("AcceptTransaction(): accepted %s\n", hash.ToString().substr(0,6).c_str());
    return true;
}

1.2 工作量難度獲得

對應的方法是:

// 根據前一個block對應的工作量獲取下一個block獲取需要的工作量
unsigned  int GetNextWorkRequired(const  CBlockIndex* pindexLast)

看源碼更清晰,主要是保證對應的區塊10分鐘產生一個,14天更新一下對應的工作量難度(即是產生2016區塊就要更新一下工作量難度),源碼如下:

// 根據前一個block對應的工作量獲取下一個block獲取需要的工作量
unsigned int GetNextWorkRequired(const CBlockIndex* pindexLast)
{
    const unsigned int nTargetTimespan = 14 * 24 * 60 * 60; // two weeks
    const unsigned int nTargetSpacing = 10 * 60; // 10分鐘產生一個block
    // 每隔2016個塊對應的工作量難度就需要重新計算一次
    const unsigned int nInterval = nTargetTimespan / nTargetSpacing; // 中間隔了多少個block 2016個塊

    // 說明當前塊是一個創世區塊,因爲當前塊對應的前一個區塊爲空
    // Genesis block
    if (pindexLast == NULL)
        return bnProofOfWorkLimit.GetCompact();

    // 如果不等於0不進行工作量難度改變
    // Only change once per interval
    if ((pindexLast->nHeight+1) % nInterval != 0)
        return pindexLast->nBits;

    // 往前推2016個區塊
    // Go back by what we want to be 14 days worth of blocks
    const CBlockIndex* pindexFirst = pindexLast;
    for (int i = 0; pindexFirst && i < nInterval-1; I++)
        pindexFirst = pindexFirst->pprev;
    assert(pindexFirst);

    // 當前區塊的前一個區塊創建時間 減去 從當前區塊向前推2016個區塊得到區塊創建時間
    // Limit adjustment step
    unsigned int nActualTimespan = pindexLast->nTime - pindexFirst->nTime;
    printf("  nActualTimespan = %d  before bounds\n", nActualTimespan);
    // 控制目標難度調整的跨度不能太大
    if (nActualTimespan < nTargetTimespan/4)
        nActualTimespan = nTargetTimespan/4;
    if (nActualTimespan > nTargetTimespan*4)
        nActualTimespan = nTargetTimespan*4;

    // 重新目標計算難度:當前區塊對應的前一個區塊對應的目標難度 * 實際2016區塊對應的創建時間間隔 / 目標時間跨度14天
    // Retarget
    CBigNum bnNew;
    bnNew.SetCompact(pindexLast->nBits);
    bnNew *= nActualTimespan;
    bnNew /= nTargetTimespan;

    // 如果計算的工作量難度(值越大對應的工作難度越小)小於當前對應的工作量難度
    if (bnNew > bnProofOfWorkLimit)
        bnNew = bnProofOfWorkLimit;

    /// debug print
    printf("\n\n\nGetNextWorkRequired RETARGET *****\n");
    printf("nTargetTimespan = %d    nActualTimespan = %d\n", nTargetTimespan, nActualTimespan);
    printf("Before: %08x  %s\n", pindexLast->nBits, CBigNum().SetCompact(pindexLast->nBits).getuint256().ToString().c_str());
    printf("After:  %08x  %s\n", bnNew.GetCompact(), bnNew.getuint256().ToString().c_str());

    return bnNew.GetCompact();
}

1.3 區塊對應的創建時間

在新建區塊的時候,要設置對應區塊的時間,由於是P2P的,沒有中心化節點能夠獲得對應的時間,所以需要從對應的區塊鏈中區塊的時間中取中位數,然後和當前時間去最大值,對應的代碼就是:

pblock->nTime = max((pindexPrev ? pindexPrev->GetMedianTimePast()+1 : 0), GetAdjustedTime());

1.4 block接收處理

1.4.1 區塊連接處理

對應的方法是:

// 區塊鏈接:每一個交易鏈接,增加到區塊索引鏈中
bool CBlock::ConnectBlock(CTxDB& txdb, CBlockIndex* pindex)
{
    //// issue here: it doesn't know the version
    unsigned int nTxPos = pindex->nBlockPos + ::GetSerializeSize(CBlock(), SER_DISK) - 1 + GetSizeOfCompactSize(vtx.size());

    map<uint256, CTxIndex> mapUnused;
    int64 nFees = 0;
    foreach(CTransaction& tx, vtx)
    {
        CDiskTxPos posThisTx(pindex->nFile, pindex->nBlockPos, nTxPos);
        nTxPos += ::GetSerializeSize(tx, SER_DISK);
        // 對每一個交易進行輸入鏈接判斷
        if (!tx.ConnectInputs(txdb, mapUnused, posThisTx, pindex->nHeight, nFees, true, false))
            return false;
    }
    // 幣基交易中對應的輸出不能大於整個對應的獎勵+交易手續費
    if (vtx[0].GetValueOut() > GetBlockValue(nFees))
        return false;

    // Update block index on disk without changing it in memory.
    // The memory index structure will be changed after the db commits.
    if (pindex->pprev)
    {
        // 將當前區塊索引 掛在 前一個區塊索引之後
        CDiskBlockIndex blockindexPrev(pindex->pprev);
        blockindexPrev.hashNext = pindex->GetBlockHash();
        txdb.WriteBlockIndex(blockindexPrev);
    }

    // 監視在block中哪些
    // Watch for transactions paying to me
    foreach(CTransaction& tx, vtx)
        AddToWalletIfMine(tx, this);

    return true;
}

image.png

1.4.2區塊分叉處理

方法如下:

// 重新組織區塊的索引:因爲此時已經出現區塊鏈分叉
bool Reorganize(CTxDB& txdb, CBlockIndex* pindexNew)
{
    printf("*** REORGANIZE ***\n");

    // 找到區塊分叉點
    // Find the fork
    CBlockIndex* pfork = pindexBest;
    CBlockIndex* plonger = pindexNew;
    // 找到主鏈和分叉鏈對應的交叉點
    while (pfork != plonger)
    {
        if (!(pfork = pfork->pprev))
            return error("Reorganize() : pfork->pprev is null");
        while (plonger->nHeight > pfork->nHeight)
            if (!(plonger = plonger->pprev))
                return error("Reorganize() : plonger->pprev is null");
    }

    // 列舉出當前節點認爲的最長鏈中(從當前最長鏈到交叉點)失去連接的塊
    // List of what to disconnect
    vector<CBlockIndex*> vDisconnect;
    for (CBlockIndex* pindex = pindexBest; pindex != pfork; pindex = pindex->pprev)
        vDisconnect.push_back(pindex);

    // 獲取需要連接的塊,因爲自己認爲的最長鏈實際上不是最長鏈
    // List of what to connect
    vector<CBlockIndex*> vConnect;
    for (CBlockIndex* pindex = pindexNew; pindex != pfork; pindex = pindex->pprev)
        vConnect.push_back(pindex);
    // 因爲上面放入的時候是倒着放的,所以這裏在將這個逆序,得到正向的
    reverse(vConnect.begin(), vConnect.end());

    // 釋放斷鏈(僅僅釋放對應的block鏈,對應的block索引鏈還沒有釋放)
    // Disconnect shorter branch
    vector<CTransaction> vResurrect;
    foreach(CBlockIndex* pindex, vDisconnect)
    {
        CBlock block;
        if (!block.ReadFromDisk(pindex->nFile, pindex->nBlockPos, true))
            return error("Reorganize() : ReadFromDisk for disconnect failed");
        if (!block.DisconnectBlock(txdb, pindex))
            return error("Reorganize() : DisconnectBlock failed");

        // 將釋放塊中的交易放入vResurrect,等待復活
        // Queue memory transactions to resurrect
        foreach(const CTransaction& tx, block.vtx)
            if (!tx.IsCoinBase())
                vResurrect.push_back(tx);
    }

    // 連接最長的分支
    // Connect longer branch
    vector<CTransaction> vDelete;
    for (int i = 0; i < vConnect.size(); i++)
    {
        CBlockIndex* pindex = vConnect[i];
        CBlock block;
        if (!block.ReadFromDisk(pindex->nFile, pindex->nBlockPos, true))
            return error("Reorganize() : ReadFromDisk for connect failed");
        if (!block.ConnectBlock(txdb, pindex))
        {
            // 如果block連接失敗之後,說明這個block無效,則刪除這塊之後的分支
            // Invalid block, delete the rest of this branch
            txdb.TxnAbort();
            for (int j = i; j < vConnect.size(); j++)
            {
                CBlockIndex* pindex = vConnect[j];
                pindex->EraseBlockFromDisk();
                txdb.EraseBlockIndex(pindex->GetBlockHash());
                mapBlockIndex.erase(pindex->GetBlockHash());
                delete pindex;
            }
            return error("Reorganize() : ConnectBlock failed");
        }
        // 將加入區塊鏈的塊中的交易從對應的內存中刪除
        // Queue memory transactions to delete
        foreach(const CTransaction& tx, block.vtx)
            vDelete.push_back(tx);
    }
    // 寫入最長鏈
    if (!txdb.WriteHashBestChain(pindexNew->GetBlockHash()))
        return error("Reorganize() : WriteHashBestChain failed");

    // Commit now because resurrecting 復活could take some time
    txdb.TxnCommit();

    // 釋放對應的塊索引鏈
    // Disconnect shorter branch
    foreach(CBlockIndex* pindex, vDisconnect)
        if (pindex->pprev)
            pindex->pprev->pnext = NULL; // 表示這些塊沒有在主鏈上

    // 形成一條主鏈的塊索引鏈
    // Connect longer branch
    foreach(CBlockIndex* pindex, vConnect)
        if (pindex->pprev)
            pindex->pprev->pnext = pindex;

    // 從釋放鏈接的分支中獲取對應的交易,將這些交易放入對應的全局變量中得到復活
    // Resurrect memory transactions that were in the disconnected branch
    foreach(CTransaction& tx, vResurrect)
        tx.AcceptTransaction(txdb, false);

    // 從全局變量中刪除那些已經在主鏈中的交易
    // Delete redundant memory transactions that are in the connected branch
    foreach(CTransaction& tx, vDelete)
        tx.RemoveFromMemoryPool();

    return true;
}

Reorganize流程

1.4.3將區塊新增到區塊索引鏈中

// 將當前區塊增加到對應的區塊索引鏈中mapBlockIndex
bool CBlock::AddToBlockIndex(unsigned int nFile, unsigned int nBlockPos)
{
    // Check for duplicate
    uint256 hash = GetHash();
    if (mapBlockIndex.count(hash))
        return error("AddToBlockIndex() : %s already exists", hash.ToString().substr(0,14).c_str());

    // Construct new block index object
    CBlockIndex* pindexNew = new CBlockIndex(nFile, nBlockPos, *this);
    if (!pindexNew)
        return error("AddToBlockIndex() : new CBlockIndex failed");
    map<uint256, CBlockIndex*>::iterator mi = mapBlockIndex.insert(make_pair(hash, pindexNew)).first;
    pindexNew->phashBlock = &((*mi).first);
    map<uint256, CBlockIndex*>::iterator miPrev = mapBlockIndex.find(hashPrevBlock);
    if (miPrev != mapBlockIndex.end())
    {
        pindexNew->pprev = (*miPrev).second;
        // 增加前一個區塊索引對應的高度
        pindexNew->nHeight = pindexNew->pprev->nHeight + 1;
    }

    CTxDB txdb;
    txdb.TxnBegin();
    txdb.WriteBlockIndex(CDiskBlockIndex(pindexNew));

    // 更新最長鏈對應的指針
    // New best
    // 新鏈的高度已經超過主鏈了(即是新鏈到創世區塊的長度 大於 本節點認爲的最長鏈到創世區塊的長度
    if (pindexNew->nHeight > nBestHeight)
    {
        // 判斷是否是創世區塊
        if (pindexGenesisBlock == NULL && hash == hashGenesisBlock)
        {
            pindexGenesisBlock = pindexNew;
            txdb.WriteHashBestChain(hash);
        }
        else if (hashPrevBlock == hashBestChain)
        {
            // 如果當前塊對應的前一個塊是最長的鏈
            // Adding to current best branch
            if (!ConnectBlock(txdb, pindexNew) || !txdb.WriteHashBestChain(hash))
            {
                txdb.TxnAbort();
                pindexNew->EraseBlockFromDisk();
                mapBlockIndex.erase(pindexNew->GetBlockHash());
                delete pindexNew;
                return error("AddToBlockIndex() : ConnectBlock failed");
            }
            txdb.TxnCommit();
            // 如果在最長鏈中,才設置對應區塊索引的pnext字段,將當前區塊索引設置在前一個區塊索引的後面
            pindexNew->pprev->pnext = pindexNew;

            // 如果對應的區塊已經放入到主鏈中,則對應的區塊交易應該要從本節點保存的交易內存池中刪除
            // Delete redundant memory transactions
            foreach(CTransaction& tx, vtx)
                tx.RemoveFromMemoryPool();
        }
        else
        {
            // 當前區塊既不是創世區塊,且當前區塊對應的前一個區塊也不在最長主鏈上的情況
            // 再加上新區塊所在鏈的長度大於本節點認爲主鏈的長度,所有將進行分叉處理
            // New best branch
            if (!Reorganize(txdb, pindexNew))
            {
                txdb.TxnAbort();
                return error("AddToBlockIndex() : Reorganize failed");
            }
        }

        // New best link
        hashBestChain = hash;
        pindexBest = pindexNew;
        nBestHeight = pindexBest->nHeight;
        nTransactionsUpdated++;
        printf("AddToBlockIndex: new best=%s  height=%d\n", hashBestChain.ToString().substr(0,14).c_str(), nBestHeight);
    }

    txdb.TxnCommit();
    txdb.Close();

    // 轉播那些到目前爲止還沒有進入block中的錢包交易
    // Relay wallet transactions that haven't gotten in yet
    if (pindexNew == pindexBest)
        RelayWalletTransactions();// 在節點之間進行轉播

    MainFrameRepaint();
    return true;
}

CBlock::AddToBlockIndex

1.4.4區塊接受處理

對應的方法如下:

// 判斷當前區塊能夠被接收
bool CBlock::AcceptBlock()
{
    // Check for duplicate
    uint256 hash = GetHash();
    if (mapBlockIndex.count(hash)) 
        return error("AcceptBlock() : block already in mapBlockIndex");

    // Get prev block index
    map<uint256, CBlockIndex*>::iterator mi = mapBlockIndex.find(hashPrevBlock);
    if (mi == mapBlockIndex.end())
        return error("AcceptBlock() : prev block not found");
    CBlockIndex* pindexPrev = (*mi).second;

    // 當前塊創建的時間要大於前一個塊對應的中位數時間
    // Check timestamp against prev
    if (nTime <= pindexPrev->GetMedianTimePast())
        return error("AcceptBlock() : block's timestamp is too early");

    //工作量證明校驗:每一個節點自己計算對應的工作量難度
    // Check proof of work
    if (nBits != GetNextWorkRequired(pindexPrev))
        return error("AcceptBlock() : incorrect proof of work");

    // Write block to history file
    unsigned int nFile;
    unsigned int nBlockPos;
    // 將塊信息寫入文件中
    if (!WriteToDisk(!fClient, nFile, nBlockPos))
        return error("AcceptBlock() : WriteToDisk failed");
    // 增加塊對應的快索引信息
    if (!AddToBlockIndex(nFile, nBlockPos))
        return error("AcceptBlock() : AddToBlockIndex failed");

    if (hashBestChain == hash)
        RelayInventory(CInv(MSG_BLOCK, hash));

    // // Add atoms to user reviews for coins created
    // vector<unsigned char> vchPubKey;
    // if (ExtractPubKey(vtx[0].vout[0].scriptPubKey, false, vchPubKey))
    // {
    //     unsigned short nAtom = GetRand(USHRT_MAX - 100) + 100;
    //     vector<unsigned short> vAtoms(1, nAtom);
    //     AddAtomsAndPropagate(Hash(vchPubKey.begin(), vchPubKey.end()), vAtoms, true);
    // }

    return true;
}

CBlock::AcceptBlock

2. 源碼地址

我對比特幣bitcoin-0.1.0源碼加了詳細的註釋,對應的下載地址:https://github.com/lwjaiyjk/bitcoin-comment-0.1.0.git

轉載請說明出處

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章