minor optimizations
This commit is contained in:
parent
4c342fae29
commit
e61d083219
1 changed files with 54 additions and 58 deletions
112
src/txdb.cpp
112
src/txdb.cpp
|
@ -100,53 +100,43 @@ bool CCoinsViewDB::BatchWrite(CCoinsMap &mapCoins, const uint256 &hashBlock, boo
|
||||||
int crash_simulate = gArgs.GetArg("-dbcrashratio", 0);
|
int crash_simulate = gArgs.GetArg("-dbcrashratio", 0);
|
||||||
assert(!hashBlock.IsNull());
|
assert(!hashBlock.IsNull());
|
||||||
|
|
||||||
uint256 old_tip = GetBestBlock();
|
|
||||||
if (old_tip.IsNull()) {
|
|
||||||
// We may be in the middle of replaying.
|
|
||||||
std::vector<uint256> old_heads = GetHeadBlocks();
|
|
||||||
if (old_heads.size() == 2) {
|
|
||||||
assert(old_heads[0] == hashBlock);
|
|
||||||
old_tip = old_heads[1];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
db << "BEGIN";
|
db << "BEGIN";
|
||||||
db << "INSERT OR REPLACE INTO marker VALUES('head_block', ?)" << hashBlock;
|
if (!mapCoins.empty()) {
|
||||||
|
db << "INSERT OR REPLACE INTO marker VALUES('head_block', ?)" << hashBlock;
|
||||||
auto dbd = db << "DELETE FROM unspent WHERE txID = ? AND txN = ?";
|
auto dbd = db << "DELETE FROM unspent WHERE txID = ? AND txN = ?";
|
||||||
auto dbi = db << "INSERT OR REPLACE INTO unspent VALUES(?,?,?,?,?,?,?)";
|
auto dbi = db << "INSERT OR REPLACE INTO unspent VALUES(?,?,?,?,?,?,?)";
|
||||||
for (auto it = mapCoins.begin(); it != mapCoins.end(); it = mapCoins.erase(it)) {
|
for (auto it = mapCoins.begin(); it != mapCoins.end(); it = mapCoins.erase(it)) {
|
||||||
if (it->second.flags & CCoinsCacheEntry::DIRTY) {
|
if (it->second.flags & CCoinsCacheEntry::DIRTY) {
|
||||||
if (it->second.coin.IsSpent()) {
|
if (it->second.coin.IsSpent()) {
|
||||||
// at present the "IsSpent" flag is used for both "spent" and "block going backwards"
|
// at present the "IsSpent" flag is used for both "spent" and "block going backwards"
|
||||||
dbd << it->first.hash << it->first.n;
|
dbd << it->first.hash << it->first.n;
|
||||||
dbd++;
|
dbd++;
|
||||||
|
} else {
|
||||||
|
CTxDestination address;
|
||||||
|
std::string destination;
|
||||||
|
if (ExtractDestination(it->second.coin.out.scriptPubKey, address))
|
||||||
|
destination = EncodeDestination(address);
|
||||||
|
uint32_t isCoinBase = it->second.coin.fCoinBase; // bit-field
|
||||||
|
uint32_t coinHeight = it->second.coin.nHeight; // bit-field
|
||||||
|
dbi << it->first.hash << it->first.n << isCoinBase << coinHeight
|
||||||
|
<< it->second.coin.out.nValue << it->second.coin.out.scriptPubKey << destination;
|
||||||
|
dbi++;
|
||||||
|
}
|
||||||
|
changed++;
|
||||||
}
|
}
|
||||||
else {
|
if (crash_simulate && ++count % 200000 == 0) {
|
||||||
CTxDestination address;
|
static FastRandomContext rng;
|
||||||
std::string destination;
|
if (rng.randrange(crash_simulate) == 0) {
|
||||||
if (ExtractDestination(it->second.coin.out.scriptPubKey, address))
|
LogPrintf("Simulating a crash. Goodbye.\n");
|
||||||
destination = EncodeDestination(address);
|
_Exit(0);
|
||||||
uint32_t isCoinBase = it->second.coin.fCoinBase; // bit-field
|
}
|
||||||
uint32_t coinHeight = it->second.coin.nHeight; // bit-field
|
|
||||||
dbi << it->first.hash << it->first.n << isCoinBase << coinHeight
|
|
||||||
<< it->second.coin.out.nValue << it->second.coin.out.scriptPubKey << destination;
|
|
||||||
dbi++;
|
|
||||||
}
|
|
||||||
changed++;
|
|
||||||
}
|
|
||||||
if (crash_simulate && ++count % 200000 == 0) {
|
|
||||||
static FastRandomContext rng;
|
|
||||||
if (rng.randrange(crash_simulate) == 0) {
|
|
||||||
LogPrintf("Simulating a crash. Goodbye.\n");
|
|
||||||
_Exit(0);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
dbd.used(true);
|
||||||
|
dbi.used(true);
|
||||||
|
db << "DELETE FROM marker WHERE name = 'head_block'";
|
||||||
}
|
}
|
||||||
dbd.used(true);
|
|
||||||
dbi.used(true);
|
|
||||||
db << "INSERT OR REPLACE INTO marker VALUES('best_block', ?)" << hashBlock;
|
db << "INSERT OR REPLACE INTO marker VALUES('best_block', ?)" << hashBlock;
|
||||||
db << "DELETE FROM marker WHERE name = 'head_block'";
|
|
||||||
|
|
||||||
auto code = sqlite::commit(db);
|
auto code = sqlite::commit(db);
|
||||||
if (code != SQLITE_OK) {
|
if (code != SQLITE_OK) {
|
||||||
|
@ -309,27 +299,33 @@ void CCoinsViewDBCursor::Next()
|
||||||
bool CBlockTreeDB::BatchWrite(const std::vector<std::pair<int, const CBlockFileInfo*> >& fileInfo,
|
bool CBlockTreeDB::BatchWrite(const std::vector<std::pair<int, const CBlockFileInfo*> >& fileInfo,
|
||||||
int nLastFile, const std::vector<const CBlockIndex*>& blockInfo, bool sync) {
|
int nLastFile, const std::vector<const CBlockIndex*>& blockInfo, bool sync) {
|
||||||
db << "BEGIN";
|
db << "BEGIN";
|
||||||
auto ibf = db << "INSERT OR REPLACE INTO block_file(file, blocks, size, undoSize, heightFirst, heightLast, timeFirst, timeLast) "
|
if (!fileInfo.empty()) {
|
||||||
"VALUES(?,?,?,?,?,?,?,?)";
|
auto ibf = db << "INSERT OR REPLACE INTO block_file(file, blocks, size, undoSize, heightFirst, "
|
||||||
for (auto& kvp: fileInfo) {
|
"heightLast, timeFirst, timeLast) VALUES(?,?,?,?,?,?,?,?)";
|
||||||
ibf << kvp.first << kvp.second->nBlocks << kvp.second->nSize << kvp.second->nUndoSize
|
for (auto &kvp: fileInfo) {
|
||||||
<< kvp.second->nHeightFirst << kvp.second->nHeightLast << kvp.second->nTimeFirst << kvp.second->nTimeLast;
|
ibf << kvp.first << kvp.second->nBlocks << kvp.second->nSize << kvp.second->nUndoSize
|
||||||
ibf++;
|
<< kvp.second->nHeightFirst << kvp.second->nHeightLast << kvp.second->nTimeFirst
|
||||||
|
<< kvp.second->nTimeLast;
|
||||||
|
ibf++;
|
||||||
|
}
|
||||||
|
ibf.used(true);
|
||||||
}
|
}
|
||||||
ibf.used(true);
|
|
||||||
db << "INSERT OR REPLACE INTO flag VALUES('last_block', ?)" << nLastFile; // TODO: is this always max(file column)?
|
db << "INSERT OR REPLACE INTO flag VALUES('last_block', ?)" << nLastFile; // TODO: is this always max(file column)?
|
||||||
|
|
||||||
auto ibi = db << "INSERT OR REPLACE INTO block_info(hash, prevHash, height, file, dataPos, undoPos, "
|
if(!blockInfo.empty()) {
|
||||||
"txCount, status, version, rootTxHash, rootTrieHash, time, bits, nonce) "
|
auto ibi = db << "INSERT OR REPLACE INTO block_info(hash, prevHash, height, file, dataPos, undoPos, "
|
||||||
"VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
|
"txCount, status, version, rootTxHash, rootTrieHash, time, bits, nonce) "
|
||||||
for (auto& bi: blockInfo) {
|
"VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
|
||||||
ibi << bi->GetBlockHash() << (bi->pprev ? bi->pprev->GetBlockHash() : uint256())
|
for (auto &bi: blockInfo) {
|
||||||
<< bi->nHeight << bi->nFile << bi->nDataPos << bi->nUndoPos << bi->nTx
|
ibi << bi->GetBlockHash() << (bi->pprev ? bi->pprev->GetBlockHash() : uint256())
|
||||||
<< bi->nStatus << bi->nVersion << bi->hashMerkleRoot << bi->hashClaimTrie
|
<< bi->nHeight << bi->nFile << bi->nDataPos << bi->nUndoPos << bi->nTx
|
||||||
<< bi->nTime << bi->nBits << bi->nNonce;
|
<< bi->nStatus << bi->nVersion << bi->hashMerkleRoot << bi->hashClaimTrie
|
||||||
ibi++;
|
<< bi->nTime << bi->nBits << bi->nNonce;
|
||||||
|
ibi++;
|
||||||
|
}
|
||||||
|
ibi.used(true);
|
||||||
}
|
}
|
||||||
ibi.used(true);
|
|
||||||
auto code = sqlite::commit(db);
|
auto code = sqlite::commit(db);
|
||||||
if (code != SQLITE_OK) {
|
if (code != SQLITE_OK) {
|
||||||
LogPrintf("%s: Error committing block info to database. SQLite error: %d\n", __func__, code);
|
LogPrintf("%s: Error committing block info to database. SQLite error: %d\n", __func__, code);
|
||||||
|
|
Loading…
Add table
Reference in a new issue