Skip to content

Commit e8558c0

Browse files
authored
Merge pull request #1815 from cyrossignol/optimize-contract-replay
contract: Optimize contract replay after chain reorganization
2 parents 4181504 + a85aa2b commit e8558c0

File tree

7 files changed

+57
-5
lines changed

7 files changed

+57
-5
lines changed

src/main.cpp

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2226,11 +2226,6 @@ bool CBlock::DisconnectBlock(CTxDB& txdb, CBlockIndex* pindex)
22262226
{
22272227
bDiscTxFailed = true;
22282228
}
2229-
2230-
if (pindex->nIsContract == 1)
2231-
{
2232-
NN::RevertContracts(vtx[i], pindex);
2233-
}
22342229
}
22352230

22362231
// Update block index on disk without changing it in memory.

src/neuralnet/beacon.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -319,6 +319,12 @@ bool BeaconRegistry::ContainsActive(const Cpid& cpid) const
319319
return ContainsActive(cpid, GetAdjustedTime());
320320
}
321321

322+
void BeaconRegistry::Reset()
323+
{
324+
m_beacons.clear();
325+
m_pending.clear();
326+
}
327+
322328
void BeaconRegistry::Add(const ContractContext& ctx)
323329
{
324330
BeaconPayload payload = ctx->CopyPayloadAs<BeaconPayload>();

src/neuralnet/beacon.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -465,6 +465,12 @@ class BeaconRegistry : public IContractHandler
465465
//!
466466
bool ContainsActive(const Cpid& cpid) const;
467467

468+
//!
469+
//! \brief Destroy the contract handler state to prepare for historical
470+
//! contract replay.
471+
//!
472+
void Reset() override;
473+
468474
//!
469475
//! \brief Determine whether a beacon contract is valid.
470476
//!

src/neuralnet/contract/contract.cpp

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,15 @@ class LegacyPayload : public IContractPayload
144144
//!
145145
class AppCacheContractHandler : public IContractHandler
146146
{
147+
public:
148+
void Reset() override
149+
{
150+
ClearCache(Section::POLL);
151+
ClearCache(Section::PROTOCOL);
152+
ClearCache(Section::SCRAPER);
153+
ClearCache(Section::VOTE);
154+
}
155+
147156
bool Validate(const Contract& contract, const CTransaction& tx) const override
148157
{
149158
return true; // No contextual validation needed yet
@@ -182,6 +191,12 @@ class AppCacheContractHandler : public IContractHandler
182191
//!
183192
class UnknownContractHandler : public IContractHandler
184193
{
194+
public:
195+
void Reset() override
196+
{
197+
// Nothing to do.
198+
}
199+
185200
bool Validate(const Contract& contract, const CTransaction& tx) const override
186201
{
187202
return true; // No contextual validation needed yet
@@ -225,6 +240,17 @@ class UnknownContractHandler : public IContractHandler
225240
class Dispatcher
226241
{
227242
public:
243+
//!
244+
//! \brief Reset the cached state of each contract handler to prepare for
245+
//! historical contract replay.
246+
//!
247+
void ResetHandlers()
248+
{
249+
GetBeaconRegistry().Reset();
250+
GetWhitelist().Reset();
251+
m_appcache_handler.Reset();
252+
}
253+
228254
//!
229255
//! \brief Validate the provided contract and forward it to the appropriate
230256
//! contract handler.
@@ -343,6 +369,8 @@ void NN::ReplayContracts(const CBlockIndex* pindex)
343369
return;
344370
}
345371

372+
g_dispatcher.ResetHandlers();
373+
346374
CBlock block;
347375

348376
// These are memorized consecutively in order from oldest to newest.

src/neuralnet/contract/handler.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,12 @@ struct IContractHandler
7777
//!
7878
virtual bool Validate(const Contract& contract, const CTransaction& tx) const = 0;
7979

80+
//!
81+
//! \brief Destroy the contract handler state to prepare for historical
82+
//! contract replay.
83+
//!
84+
virtual void Reset() = 0;
85+
8086
//!
8187
//! \brief Handle an contract addition.
8288
//!

src/neuralnet/project.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,11 @@ WhitelistSnapshot Whitelist::Snapshot() const
154154
return WhitelistSnapshot(std::atomic_load(&m_projects));
155155
}
156156

157+
void Whitelist::Reset()
158+
{
159+
std::atomic_store(&m_projects, std::make_shared<ProjectList>());
160+
}
161+
157162
void Whitelist::Add(const ContractContext& ctx)
158163
{
159164
Project project = ctx->CopyPayloadAs<Project>();

src/neuralnet/project.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -280,6 +280,12 @@ class Whitelist : public IContractHandler
280280
//!
281281
WhitelistSnapshot Snapshot() const;
282282

283+
//!
284+
//! \brief Destroy the contract handler state to prepare for historical
285+
//! contract replay.
286+
//!
287+
void Reset() override;
288+
283289
//!
284290
//! \brief Perform contextual validation for the provided contract.
285291
//!

0 commit comments

Comments
 (0)