RealPath:
WebPath:
2017/01/08 08:11 (JST) 更新
ブロック >>

ブロックの伝搬

Contents

他ノードからのブロック情報の受信

他ノードからのブロードキャストにより CBlockIndex 情報を受信し、グローバル変数 CChain chainActive; に CBlockIndex 情報を追加格納する。

bool ProcessMessages(CNode* pfrom, CConnman& connman)
↓
bool static ProcessMessage(CNode* pfrom, string strCommand, CDataStream& vRecv, int64_t nTimeReceived, const CChainParams& chainparams, CConnman& connman)
{
    ....
    else if (strCommand == NetMsgType::CMPCTBLOCK && !fImporting && !fReindex) // Ignore blocks received while importing
    {
        ....
        return ProcessMessage(pfrom, NetMsgType::BLOCKTXN, blockTxnMsg, nTimeReceived, chainparams, connman);
        ....
    }
    ....
}
↓
bool static ProcessMessage(CNode* pfrom, string strCommand, CDataStream& vRecv, int64_t nTimeReceived, const CChainParams& chainparams, CConnman& connman)
{
    ....
    else if (strCommand == NetMsgType::BLOCKTXN && !fImporting && !fReindex) // Ignore blocks received while importing
    {
        ....
        ProcessNewBlock(chainparams, pblock, true, &fNewBlock);
        ....
    }
    ....
}
↓
bool static ProcessMessage(CNode* pfrom, string strCommand, CDataStream& vRecv, int64_t nTimeReceived, const CChainParams& chainparams, CConnman& connman)
{
    ....
    else if (strCommand == NetMsgType::BLOCKTXN && !fImporting && !fReindex) // Ignore blocks received while importing
    {
        ....
        ProcessNewBlock(chainparams, pblock, true, &fNewBlock);
        ....
    }
    ....
}
↓
bool ProcessNewBlock(const CChainParams& chainparams, const std::shared_ptr<const CBlock> pblock, bool fForceProcessing, bool *fNewBlock)
{
    ....
    if (!ActivateBestChain(state, chainparams, pblock))
    ....
}
↓
bool ActivateBestChain(CValidationState &state, const CChainParams& chainparams, std::shared_ptr<const CBlock> pblock) {
{
    ....
    // 追加すべき CBlockIndex の取得
    if (pindexMostWork == NULL) {
        pindexMostWork = FindMostWorkChain();
    ....
    // 追加
    if (!ActivateBestChainStep(state, chainparams, pindexMostWork, pblock && pblock->GetHash() == pindexMostWork->GetBlockHash() ? pblock : nullBlockPtr, fInvalidFound, connectTrace))
    ....
}
↓
static bool ActivateBestChainStep(CValidationState& state, const CChainParams& chainparams, CBlockIndex* pindexMostWork, const std::shared_ptr<const CBlock>& pblock, bool& fInvalidFound, ConnectTrace& connectTrace)
{
    ....
    if (!ConnectTip(state, chainparams, pindexConnect, pindexConnect == pindexMostWork ? pblock : std::shared_ptr<const CBlock>(), connectTrace)) {
    ....
}
↓
bool static ConnectTip(CValidationState& state, const CChainParams& chainparams, CBlockIndex* pindexNew, const std::shared_ptr<const CBlock>& pblock, ConnectTrace& connectTrace)
{
    ....
    UpdateTip(pindexNew, chainparams);
    ....
}
↓
void static UpdateTip(CBlockIndex *pindexNew, const CChainParams& chainParams)
{
    ....
    chainActive.SetTip(pindexNew);
    ....
}
↓
void CChain::SetTip(CBlockIndex *pindex)
{
    ....
    // ここで CChain に CBlockIndex が追加される
    vChain.resize(pindex->nHeight + 1);
    while (pindex && vChain[pindex->nHeight] != pindex) {
        vChain[pindex->nHeight] = pindex;
        pindex = pindex->pprev;
    }
}