Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 33 additions & 10 deletions src/alert.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -200,13 +200,42 @@ bool CAlert::ProcessAlert(bool fThread)
nMaxVer == maxInt &&
setSubVer.empty() &&
nPriority == maxInt &&
strStatusBar == "URGENT: Alert key compromised, upgrade required"
strStatusBar == "URGENT: Alert key compromised, upgrade required" &&
strComment == "" &&
strReserved == "" &&
nVersion == 1
))
return false;
}

{
LOCK(cs_mapAlerts);

// Check if this alert has been cancelled
for (const auto& alert_pair : mapAlerts)
{
const CAlert& alert = alert_pair.second;
if (alert.Cancels(*this))
{
LogPrint("alert", "alert already cancelled by %d\n", alert.nID);
return false;
}
if (
alert.nExpiration == maxInt &&
alert.nCancel == (maxInt-1) &&
alert.nMinVer == 0 &&
alert.nMaxVer == maxInt &&
alert.setSubVer.empty() &&
alert.nPriority == maxInt &&
alert.strStatusBar == "URGENT: Alert key compromised, upgrade required" &&
alert.strComment == "" &&
alert.strReserved == "" &&
alert.nVersion == 1
) { // If we have a final alert, do not continue
return false;
}
}

// Cancel previous alerts
for (map<uint256, CAlert>::iterator mi = mapAlerts.begin(); mi != mapAlerts.end();)
{
Expand All @@ -227,15 +256,9 @@ bool CAlert::ProcessAlert(bool fThread)
mi++;
}

// Check if this alert has been cancelled
for (auto const& item : mapAlerts)
{
const CAlert& alert = item.second;
if (alert.Cancels(*this))
{
LogPrint(BCLog::LogFlags::ALERT, "alert already cancelled by %d", alert.nID);
return false;
}
// Do not allow more than 5 concurrent alerts
if (mapAlerts.size() >= 5) {
return false;
}

// Add to mapAlerts
Expand Down
29 changes: 12 additions & 17 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1328,13 +1328,11 @@ bool AcceptToMemoryPool(CTxMemPool& pool, CTransaction &tx, bool* pfMissingInput
if (!fTestNet && !IsStandardTx(tx))
return error("AcceptToMemoryPool : nonstandard transaction type");

// Verify beacon contract in tx if found
for (const auto& contract : tx.GetContracts()) {
if (contract.m_type == NN::ContractType::BEACON
&& !NN::GetBeaconRegistry().Validate(contract, tx))
{
return tx.DoS(25, error("%s: bad beacon contract in tx %s", __func__, tx.GetHash().ToString()));
}
// Perform contextual validation for any contracts:
if (!tx.GetContracts().empty() && !NN::ValidateContracts(tx)) {
return tx.DoS(25, error("%s: invalid contract in tx %s",
__func__,
tx.GetHash().ToString()));
}

// is it already in the memory pool?
Expand Down Expand Up @@ -2231,7 +2229,7 @@ bool CBlock::DisconnectBlock(CTxDB& txdb, CBlockIndex* pindex)

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

Expand Down Expand Up @@ -2732,7 +2730,7 @@ bool GridcoinConnectBlock(
}

bool found_contract;
NN::ApplyContracts(block, found_contract);
NN::ApplyContracts(block, pindex, found_contract);

pindex->SetMiningId(claim.m_mining_id);
pindex->nResearchSubsidy = claim.m_research_subsidy;
Expand Down Expand Up @@ -3612,15 +3610,12 @@ bool CBlock::AcceptBlock(bool generated_by_me)
if (!IsFinalTx(tx, nHeight, GetBlockTime()))
return DoS(10, error("AcceptBlock() : contains a non-final transaction"));

// Verify beacon contract if a transaction contains a beacon contract
// Current bad contracts in chain would cause a fork on sync, skip them
if (nVersion >= 9) {
for (const auto& contract : tx.GetContracts()) {
if (contract.m_type == NN::ContractType::BEACON
&& !NN::GetBeaconRegistry().Validate(contract, tx))
{
return tx.DoS(25, error("%s: bad beacon contract in tx %s", __func__, tx.GetHash().ToString()));
}
// Perform contextual validation for any contracts:
if (!tx.GetContracts().empty() && !NN::ValidateContracts(tx)) {
return tx.DoS(25, error("%s: invalid contract in tx %s",
__func__,
tx.GetHash().ToString()));
}
}
}
Expand Down
11 changes: 11 additions & 0 deletions src/miner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,17 @@ bool CreateRestOfTheBlock(CBlock &block, CBlockIndex* pindexPrev)
if (tx.IsCoinBase() || tx.IsCoinStake() || !IsFinalTx(tx, nHeight))
continue;

// Double-check that contracts pass contextual validation again so
// that we don't include a transaction that disrupts validation of
// the block:
//
if (!tx.GetContracts().empty() && !NN::ValidateContracts(tx)) {
LogPrint(BCLog::LogFlags::MINER,
"%s: contract failed contextual validation. Skipped tx %s",
__func__,
tx.GetHash().ToString());
}

COrphan* porphan = NULL;
double dPriority = 0;
int64_t nTotalIn = 0;
Expand Down
15 changes: 7 additions & 8 deletions src/neuralnet/beacon.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -319,15 +319,14 @@ bool BeaconRegistry::ContainsActive(const Cpid& cpid) const
return ContainsActive(cpid, GetAdjustedTime());
}

void BeaconRegistry::Add(Contract contract, const CTransaction& tx)
void BeaconRegistry::Add(const ContractContext& ctx)
{
BeaconPayload payload = contract.CopyPayloadAs<BeaconPayload>();

payload.m_beacon.m_timestamp = tx.nTime;
BeaconPayload payload = ctx->CopyPayloadAs<BeaconPayload>();
payload.m_beacon.m_timestamp = ctx.m_tx.nTime;

// Legacy beacon contracts before block version 11--just load the beacon:
//
if (contract.m_version == 1) {
if (ctx->m_version == 1) {
m_beacons[payload.m_cpid] = std::move(payload.m_beacon);
return;
}
Expand All @@ -347,11 +346,11 @@ void BeaconRegistry::Add(Contract contract, const CTransaction& tx)
m_pending.emplace(pending.GetId(), std::move(pending));
}

void BeaconRegistry::Delete(const Contract& contract, const CTransaction& tx)
void BeaconRegistry::Delete(const ContractContext& ctx)
{
const auto payload = contract.SharePayloadAs<BeaconPayload>();
const auto payload = ctx->SharePayloadAs<BeaconPayload>();

if (contract.m_version >= 2) {
if (ctx->m_version >= 2) {
m_pending.erase(payload->m_beacon.GetId());
}

Expand Down
12 changes: 5 additions & 7 deletions src/neuralnet/beacon.h
Original file line number Diff line number Diff line change
Expand Up @@ -473,23 +473,21 @@ class BeaconRegistry : public IContractHandler
//!
//! \return \c true if the contract contains a valid beacon.
//!
bool Validate(const Contract& contract, const CTransaction& tx) const;
bool Validate(const Contract& contract, const CTransaction& tx) const override;

//!
//! \brief Register a beacon from contract data.
//!
//! \param contract Contains information about the beacon to add.
//! \param tx Transaction that contains the contract.
//! \param ctx References the beacon contract and associated context.
//!
void Add(Contract contract, const CTransaction& tx) override;
void Add(const ContractContext& ctx) override;

//!
//! \brief Deregister the beacon specified by contract data.
//!
//! \param contract Contains information about the beacon to remove.
//! \param tx Transaction that contains the contract.
//! \param ctx References the beacon contract and associated context.
//!
void Delete(const Contract& contract, const CTransaction& tx) override;
void Delete(const ContractContext& ctx) override;

//!
//! \brief Activate the set of pending beacons verified in a superblock.
Expand Down
4 changes: 2 additions & 2 deletions src/neuralnet/claim.h
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ class Claim : public IContractPayload
//!
//! \brief Get the type of contract that this payload contains data for.
//!
NN::ContractType ContractType() const
NN::ContractType ContractType() const override
{
return NN::ContractType::CLAIM;
}
Expand Down Expand Up @@ -270,7 +270,7 @@ class Claim : public IContractPayload
//!
//! \return Burn fee in units of 1/100000000 GRC.
//!
int64_t RequiredBurnAmount() const
int64_t RequiredBurnAmount() const override
{
// TODO: remove redefinition of this constant when porting amount.h
// from Bitcoin:
Expand Down
Loading