forked from DiviProject/Divi
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathActiveChainManager.cpp
More file actions
142 lines (132 loc) · 4.45 KB
/
Copy pathActiveChainManager.cpp
File metadata and controls
142 lines (132 loc) · 4.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
#include <ActiveChainManager.h>
#include <primitives/block.h>
#include <ValidationState.h>
#include <chain.h>
#include <coins.h>
#include <BlockUndo.h>
#include <Logging.h>
#include <addressindex.h>
#include <txdb.h>
#include <spentindex.h>
#include <BlockDiskAccessor.h>
#include <utiltime.h>
#include <I_BlockDataReader.h>
#include <IndexDatabaseUpdates.h>
#include <IndexDatabaseUpdateCollector.h>
#include <UtxoCheckingAndUpdating.h>
ActiveChainManager::ActiveChainManager(
const bool& addressIndexingIsEnabled,
CBlockTreeDB* blocktree,
const I_BlockDataReader& blockDataReader
): addressIndexingIsEnabled_(addressIndexingIsEnabled)
, blocktree_(blocktree)
, blockDataReader_(blockDataReader)
{
}
bool ActiveChainManager::ApplyDisconnectionUpdateIndexToDBs(
IndexDatabaseUpdates& indexDBUpdates,
CValidationState& state) const
{
if (addressIndexingIsEnabled_) {
if (!blocktree_->EraseAddressIndex(indexDBUpdates.addressIndex)) {
return state.Abort("Failed to delete address index");
}
if (!blocktree_->UpdateAddressUnspentIndex(indexDBUpdates.addressUnspentIndex)) {
return state.Abort("Failed to write address unspent index");
}
}
return true;
}
static bool CheckTxReversalStatus(const TxReversalStatus status, bool& fClean)
{
if(status == TxReversalStatus::ABORT_NO_OTHER_ERRORS)
{
return false;
}
else if (status == TxReversalStatus::ABORT_WITH_OTHER_ERRORS)
{
fClean = false;
return false;
}
else if (status == TxReversalStatus::CONTINUE_WITH_ERRORS)
{
fClean = false;
}
return true;
}
/** Undo the effects of this block (with given index) on the UTXO set represented by coins.
* In case pfClean is provided, operation will try to be tolerant about errors, and *pfClean
* will be true if no problems were found. Otherwise, the return value will be false in case
* of problems. Note that in any case, coins may be modified. */
bool ActiveChainManager::DisconnectBlock(
CBlock& block,
CValidationState& state,
CBlockIndex* pindex,
CCoinsViewCache& view,
bool* pfClean) const
{
if (pindex->GetBlockHash() != view.GetBestBlock())
LogPrintf("%s : pindex=%s view=%s\n", __func__, pindex->GetBlockHash(), view.GetBestBlock());
assert(pindex->GetBlockHash() == view.GetBestBlock());
if (pfClean)
*pfClean = false;
CBlockUndo blockUndo;
if(!blockDataReader_.ReadBlockUndo(pindex,blockUndo))
{
return false;
}
else if(blockUndo.vtxundo.size() + 1 != block.vtx.size())
{
return error("DisconnectBlock() : block and undo data inconsistent");
}
const BlockUtxoHasher utxoHasher;
bool fClean = true;
IndexDatabaseUpdates indexDBUpdates;
// undo transactions in reverse order
for (int transactionIndex = block.vtx.size() - 1; transactionIndex >= 0; transactionIndex--) {
const CTransaction& tx = block.vtx[transactionIndex];
const TransactionLocationReference txLocationReference(utxoHasher, tx, pindex->nHeight, transactionIndex);
const auto* undo = (transactionIndex > 0 ? &blockUndo.vtxundo[transactionIndex - 1] : nullptr);
const TxReversalStatus status = UpdateCoinsReversingTransaction(tx, txLocationReference, view, undo);
if(!CheckTxReversalStatus(status,fClean))
{
return false;
}
if(!pfClean)
{
IndexDatabaseUpdateCollector::ReverseTransaction(tx,txLocationReference,view,indexDBUpdates);
}
}
// undo transactions in reverse order
view.SetBestBlock(pindex->pprev->GetBlockHash());
if(!pfClean)
{
if(!ApplyDisconnectionUpdateIndexToDBs(indexDBUpdates,state))
{
return false;
}
return fClean;
}
else
{
*pfClean = fClean;
return true;
}
}
void ActiveChainManager::DisconnectBlock(
std::pair<CBlock,bool>& disconnectedBlockAndStatus,
CValidationState& state,
CBlockIndex* pindex,
CCoinsViewCache& coins) const
{
CBlock& block = disconnectedBlockAndStatus.first;
bool& status = disconnectedBlockAndStatus.second;
if (!blockDataReader_.ReadBlock(pindex,block))
{
status = state.Abort("Failed to read block");
return;
}
int64_t nStart = GetTimeMicros();
status = DisconnectBlock(block,state,pindex,coins);
LogPrint("bench", "- Disconnect block: %.2fms\n", (GetTimeMicros() - nStart) * 0.001);
}