Skip to content

Commit

Permalink
Merge pull request #1815 from cyrossignol/optimize-contract-replay
Browse files Browse the repository at this point in the history
contract: Optimize contract replay after chain reorganization
  • Loading branch information
jamescowens committed Aug 7, 2020
2 parents 4181504 + a85aa2b commit e8558c0
Show file tree
Hide file tree
Showing 7 changed files with 57 additions and 5 deletions.
5 changes: 0 additions & 5 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2226,11 +2226,6 @@ bool CBlock::DisconnectBlock(CTxDB& txdb, CBlockIndex* pindex)
{
bDiscTxFailed = true;
}

if (pindex->nIsContract == 1)
{
NN::RevertContracts(vtx[i], pindex);
}
}

// Update block index on disk without changing it in memory.
Expand Down
6 changes: 6 additions & 0 deletions src/neuralnet/beacon.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,12 @@ bool BeaconRegistry::ContainsActive(const Cpid& cpid) const
return ContainsActive(cpid, GetAdjustedTime());
}

void BeaconRegistry::Reset()
{
m_beacons.clear();
m_pending.clear();
}

void BeaconRegistry::Add(const ContractContext& ctx)
{
BeaconPayload payload = ctx->CopyPayloadAs<BeaconPayload>();
Expand Down
6 changes: 6 additions & 0 deletions src/neuralnet/beacon.h
Original file line number Diff line number Diff line change
Expand Up @@ -465,6 +465,12 @@ class BeaconRegistry : public IContractHandler
//!
bool ContainsActive(const Cpid& cpid) const;

//!
//! \brief Destroy the contract handler state to prepare for historical
//! contract replay.
//!
void Reset() override;

//!
//! \brief Determine whether a beacon contract is valid.
//!
Expand Down
28 changes: 28 additions & 0 deletions src/neuralnet/contract/contract.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,15 @@ class LegacyPayload : public IContractPayload
//!
class AppCacheContractHandler : public IContractHandler
{
public:
void Reset() override
{
ClearCache(Section::POLL);
ClearCache(Section::PROTOCOL);
ClearCache(Section::SCRAPER);
ClearCache(Section::VOTE);
}

bool Validate(const Contract& contract, const CTransaction& tx) const override
{
return true; // No contextual validation needed yet
Expand Down Expand Up @@ -182,6 +191,12 @@ class AppCacheContractHandler : public IContractHandler
//!
class UnknownContractHandler : public IContractHandler
{
public:
void Reset() override
{
// Nothing to do.
}

bool Validate(const Contract& contract, const CTransaction& tx) const override
{
return true; // No contextual validation needed yet
Expand Down Expand Up @@ -225,6 +240,17 @@ class UnknownContractHandler : public IContractHandler
class Dispatcher
{
public:
//!
//! \brief Reset the cached state of each contract handler to prepare for
//! historical contract replay.
//!
void ResetHandlers()
{
GetBeaconRegistry().Reset();
GetWhitelist().Reset();
m_appcache_handler.Reset();
}

//!
//! \brief Validate the provided contract and forward it to the appropriate
//! contract handler.
Expand Down Expand Up @@ -343,6 +369,8 @@ void NN::ReplayContracts(const CBlockIndex* pindex)
return;
}

g_dispatcher.ResetHandlers();

CBlock block;

// These are memorized consecutively in order from oldest to newest.
Expand Down
6 changes: 6 additions & 0 deletions src/neuralnet/contract/handler.h
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,12 @@ struct IContractHandler
//!
virtual bool Validate(const Contract& contract, const CTransaction& tx) const = 0;

//!
//! \brief Destroy the contract handler state to prepare for historical
//! contract replay.
//!
virtual void Reset() = 0;

//!
//! \brief Handle an contract addition.
//!
Expand Down
5 changes: 5 additions & 0 deletions src/neuralnet/project.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,11 @@ WhitelistSnapshot Whitelist::Snapshot() const
return WhitelistSnapshot(std::atomic_load(&m_projects));
}

void Whitelist::Reset()
{
std::atomic_store(&m_projects, std::make_shared<ProjectList>());
}

void Whitelist::Add(const ContractContext& ctx)
{
Project project = ctx->CopyPayloadAs<Project>();
Expand Down
6 changes: 6 additions & 0 deletions src/neuralnet/project.h
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,12 @@ class Whitelist : public IContractHandler
//!
WhitelistSnapshot Snapshot() const;

//!
//! \brief Destroy the contract handler state to prepare for historical
//! contract replay.
//!
void Reset() override;

//!
//! \brief Perform contextual validation for the provided contract.
//!
Expand Down

0 comments on commit e8558c0

Please sign in to comment.