@@ -41,9 +41,9 @@ bool BaseIndex::DB::ReadBestBlock(CBlockLocator& locator) const
41
41
return success;
42
42
}
43
43
44
- bool BaseIndex::DB::WriteBestBlock (const CBlockLocator& locator)
44
+ void BaseIndex::DB::WriteBestBlock (CDBBatch& batch, const CBlockLocator& locator)
45
45
{
46
- return Write (DB_BEST_BLOCK, locator);
46
+ batch. Write (DB_BEST_BLOCK, locator);
47
47
}
48
48
49
49
BaseIndex::~BaseIndex ()
@@ -95,17 +95,22 @@ void BaseIndex::ThreadSync()
95
95
int64_t last_locator_write_time = 0 ;
96
96
while (true ) {
97
97
if (m_interrupt) {
98
- WriteBestBlock (pindex);
98
+ m_best_block_index = pindex;
99
+ // No need to handle errors in Commit. If it fails, the error will be already be
100
+ // logged. The best way to recover is to continue, as index cannot be corrupted by
101
+ // a missed commit to disk for an advanced index state.
102
+ Commit ();
99
103
return ;
100
104
}
101
105
102
106
{
103
107
LOCK (cs_main);
104
108
const CBlockIndex* pindex_next = NextSyncBlock (pindex);
105
109
if (!pindex_next) {
106
- WriteBestBlock (pindex);
107
110
m_best_block_index = pindex;
108
111
m_synced = true ;
112
+ // No need to handle errors in Commit. See rationale above.
113
+ Commit ();
109
114
break ;
110
115
}
111
116
pindex = pindex_next;
@@ -119,8 +124,10 @@ void BaseIndex::ThreadSync()
119
124
}
120
125
121
126
if (last_locator_write_time + SYNC_LOCATOR_WRITE_INTERVAL < current_time) {
122
- WriteBestBlock ( pindex) ;
127
+ m_best_block_index = pindex;
123
128
last_locator_write_time = current_time;
129
+ // No need to handle errors in Commit. See rationale above.
130
+ Commit ();
124
131
}
125
132
126
133
CBlock block;
@@ -144,15 +151,22 @@ void BaseIndex::ThreadSync()
144
151
}
145
152
}
146
153
147
- bool BaseIndex::WriteBestBlock ( const CBlockIndex* block_index )
154
+ bool BaseIndex::Commit ( )
148
155
{
149
- LOCK (cs_main );
150
- if (!GetDB ().WriteBestBlock (chainActive. GetLocator (block_index) )) {
151
- return error (" %s: Failed to write locator to disk " , __func__);
156
+ CDBBatch batch ( GetDB () );
157
+ if (!CommitInternal (batch) || ! GetDB ().WriteBatch (batch )) {
158
+ return error (" %s: Failed to commit latest %s state " , __func__, GetName () );
152
159
}
153
160
return true ;
154
161
}
155
162
163
+ bool BaseIndex::CommitInternal (CDBBatch& batch)
164
+ {
165
+ LOCK (cs_main);
166
+ GetDB ().WriteBestBlock (batch, chainActive.GetLocator (m_best_block_index));
167
+ return true ;
168
+ }
169
+
156
170
void BaseIndex::BlockConnected (const std::shared_ptr<const CBlock>& block, const CBlockIndex* pindex,
157
171
const std::vector<CTransactionRef>& txn_conflicted)
158
172
{
@@ -224,9 +238,10 @@ void BaseIndex::ChainStateFlushed(const CBlockLocator& locator)
224
238
return ;
225
239
}
226
240
227
- if (!GetDB ().WriteBestBlock (locator)) {
228
- error (" %s: Failed to write locator to disk" , __func__);
229
- }
241
+ // No need to handle errors in Commit. If it fails, the error will be already be logged. The
242
+ // best way to recover is to continue, as index cannot be corrupted by a missed commit to disk
243
+ // for an advanced index state.
244
+ Commit ();
230
245
}
231
246
232
247
bool BaseIndex::BlockUntilSyncedToCurrentChain ()
0 commit comments