Skip to content

Commit

Permalink
Revert "updatemasternode (#1343)"
Browse files Browse the repository at this point in the history
This reverts commit e9ebb04.
  • Loading branch information
prasannavl committed Aug 25, 2022
1 parent 227bbe8 commit b6dce06
Show file tree
Hide file tree
Showing 25 changed files with 368 additions and 922 deletions.
30 changes: 15 additions & 15 deletions src/consensus/tx_verify.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -161,27 +161,14 @@ int64_t GetTransactionSigOpCost(const CTransaction& tx, const CCoinsViewCache& i
return nSigOps;
}

bool Consensus::CheckTxInputs(const CTransaction& tx, CValidationState& state, const CCoinsViewCache& inputs, CCustomCSView& mnview, int nSpendHeight, CAmount& txfee, const CChainParams& chainparams)
bool Consensus::CheckTxInputs(const CTransaction& tx, CValidationState& state, const CCoinsViewCache& inputs, const CCustomCSView * mnview, int nSpendHeight, CAmount& txfee, const CChainParams& chainparams)
{
// are the actual inputs available?
if (!inputs.HaveInputs(tx)) {
return state.Invalid(ValidationInvalidReason::TX_MISSING_INPUTS, false, REJECT_INVALID, "bad-txns-inputs-missingorspent",
strprintf("%s: inputs missing/spent", __func__));
}

// check for tokens values
uint256 canSpend;
std::vector<unsigned char> dummy;
const auto txType = GuessCustomTxType(tx, dummy);

if (NotAllowedToFail(txType, nSpendHeight) || (nSpendHeight >= chainparams.GetConsensus().GreatWorldHeight && txType == CustomTxType::UpdateMasternode)) {
CCustomCSView discardCache(mnview);
auto res = ApplyCustomTx(discardCache, inputs, tx, chainparams.GetConsensus(), nSpendHeight, 0, &canSpend);
if (!res.ok && (res.code & CustomTxErrCodes::Fatal)) {
return state.Invalid(ValidationInvalidReason::CONSENSUS, false, REJECT_INVALID, "bad-txns-customtx", res.msg);
}
}

TAmounts nValuesIn;
for (unsigned int i = 0; i < tx.vin.size(); ++i) {
const COutPoint &prevout = tx.vin[i].prevout;
Expand All @@ -200,7 +187,8 @@ bool Consensus::CheckTxInputs(const CTransaction& tx, CValidationState& state, c
return state.Invalid(ValidationInvalidReason::CONSENSUS, false, REJECT_INVALID, "bad-txns-inputvalues-outofrange");
}
/// @todo tokens: later match the range with TotalSupply
if (canSpend != prevout.hash && prevout.n == 1 && !mnview.CanSpend(prevout.hash, nSpendHeight)) {

if (prevout.n == 1 && !mnview->CanSpend(prevout.hash, nSpendHeight)) {
return state.Invalid(ValidationInvalidReason::CONSENSUS, false, REJECT_INVALID, "bad-txns-collateral-locked",
strprintf("tried to spend locked collateral for %s", prevout.hash.ToString())); /// @todo may be somehow place the height of unlocking?
}
Expand All @@ -227,6 +215,18 @@ bool Consensus::CheckTxInputs(const CTransaction& tx, CValidationState& state, c
return state.Invalid(ValidationInvalidReason::CONSENSUS, false, REJECT_INVALID, "bad-txns-tokens-in-old-version-tx");
}

// check for tokens values
std::vector<unsigned char> dummy;
const auto txType = GuessCustomTxType(tx, dummy);

if (NotAllowedToFail(txType, nSpendHeight)) {
CCustomCSView discardCache(const_cast<CCustomCSView&>(*mnview));
auto res = ApplyCustomTx(discardCache, inputs, tx, chainparams.GetConsensus(), nSpendHeight);
if (!res.ok && (res.code & CustomTxErrCodes::Fatal)) {
return state.Invalid(ValidationInvalidReason::CONSENSUS, false, REJECT_INVALID, "bad-txns-customtx", res.msg);
}
}

for (auto const & kv : non_minted_values_out) {
DCT_ID const & tokenId = kv.first;

Expand Down
2 changes: 1 addition & 1 deletion src/consensus/tx_verify.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ namespace Consensus {
* @param[out] txfee Set to the transaction fee if successful.
* Preconditions: tx.IsCoinBase() is false.
*/
bool CheckTxInputs(const CTransaction& tx, CValidationState& state, const CCoinsViewCache& inputs, CCustomCSView& mnview, int nSpendHeight, CAmount& txfee, const CChainParams& chainparams);
bool CheckTxInputs(const CTransaction& tx, CValidationState& state, const CCoinsViewCache& inputs, const CCustomCSView * mnview, int nSpendHeight, CAmount& txfee, const CChainParams& chainparams);
} // namespace Consensus

/** Auxiliary functions for transaction validation (ideally should not be exposed) */
Expand Down
2 changes: 1 addition & 1 deletion src/masternodes/anchors.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -993,7 +993,7 @@ bool CAnchorAwaitingConfirms::Validate(CAnchorConfirmMessage const &confirmMessa
}

auto it = pcustomcsview->GetMasternodeIdByOperator(signer);
if (!it || !pcustomcsview->GetMasternode(*it)->IsActive(height, *pcustomcsview)) {
if (!it || !pcustomcsview->GetMasternode(*it)->IsActive(height)) {
LogPrint(BCLog::ANCHORING, "%s: Warning! Masternode with operator key %s does not exist or not active!\n", __func__, signer.ToString());
return false;
}
Expand Down
162 changes: 79 additions & 83 deletions src/masternodes/masternodes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -87,28 +87,18 @@ CMasternode::CMasternode()
, resignHeight(-1)
, version(-1)
, resignTx()
, collateralTx()
, banTx()
{
}

CMasternode::State CMasternode::GetState(int height, const CMasternodesView& mnview) const
CMasternode::State CMasternode::GetState(int height) const
{
int EunosPayaHeight = Params().GetConsensus().EunosPayaHeight;

if (height < creationHeight) {
return State::UNKNOWN;
}

if (!collateralTx.IsNull()) {
auto idHeight = mnview.GetNewCollateral(collateralTx);
assert(idHeight);
if (static_cast<uint32_t>(height) < idHeight->blockHeight) {
return State::TRANSFERRING;
} else if (static_cast<uint32_t>(height) < idHeight->blockHeight + GetMnActivationDelay(idHeight->blockHeight)) {
return State::PRE_ENABLED;
}
}

if (resignHeight == -1 || height < resignHeight) { // enabled or pre-enabled
// Special case for genesis block
int activationDelay = height < EunosPayaHeight ? GetMnActivationDelay(height) : GetMnActivationDelay(creationHeight);
Expand All @@ -128,9 +118,9 @@ CMasternode::State CMasternode::GetState(int height, const CMasternodesView& mnv
return State::UNKNOWN;
}

bool CMasternode::IsActive(int height, const CMasternodesView& mnview) const
bool CMasternode::IsActive(int height) const
{
State state = GetState(height, mnview);
State state = GetState(height);
if (height >= Params().GetConsensus().EunosPayaHeight) {
return state == ENABLED;
}
Expand All @@ -148,8 +138,6 @@ std::string CMasternode::GetHumanReadableState(State state)
return "PRE_RESIGNED";
case RESIGNED:
return "RESIGNED";
case TRANSFERRING:
return "TRANSFERRING";
default:
return "UNKNOWN";
}
Expand Down Expand Up @@ -177,7 +165,7 @@ bool operator==(CMasternode const & a, CMasternode const & b)
a.resignHeight == b.resignHeight &&
a.version == b.version &&
a.resignTx == b.resignTx &&
a.collateralTx == b.collateralTx
a.banTx == b.banTx
);
}

Expand Down Expand Up @@ -306,9 +294,14 @@ Res CMasternodesView::CreateMasternode(const uint256 & nodeId, const CMasternode
return Res::Ok();
}

Res CMasternodesView::ResignMasternode(CMasternode& node, const uint256 & nodeId, const uint256 & txid, int height)
Res CMasternodesView::ResignMasternode(const uint256 & nodeId, const uint256 & txid, int height)
{
auto state = node.GetState(height, *this);
// auth already checked!
auto node = GetMasternode(nodeId);
if (!node) {
return Res::Err("node %s does not exists", nodeId.ToString());
}
auto state = node->GetState(height);
if (height >= Params().GetConsensus().EunosPayaHeight) {
if (state != CMasternode::ENABLED) {
return Res::Err("node %s state is not 'ENABLED'", nodeId.ToString());
Expand All @@ -317,85 +310,96 @@ Res CMasternodesView::ResignMasternode(CMasternode& node, const uint256 & nodeId
return Res::Err("node %s state is not 'PRE_ENABLED' or 'ENABLED'", nodeId.ToString());
}

const auto timelock = GetTimelock(nodeId, node, height);
const auto timelock = GetTimelock(nodeId, *node, height);
if (timelock) {
return Res::Err("Trying to resign masternode before timelock expiration.");
}

node.resignTx = txid;
node.resignHeight = height;
WriteBy<ID>(nodeId, node);
node->resignTx = txid;
node->resignHeight = height;
WriteBy<ID>(nodeId, *node);

return Res::Ok();
}

void CMasternodesView::SetForcedRewardAddress(uint256 const & nodeId, CMasternode& node, const char rewardAddressType, CKeyID const & rewardAddress, int height)
Res CMasternodesView::SetForcedRewardAddress(uint256 const & nodeId, const char rewardAddressType, CKeyID const & rewardAddress, int height)
{
// If old masternode update for new serialisation
if (node.version < CMasternode::VERSION0) {
node.version = CMasternode::VERSION0;
// Temporarily disabled for 2.2
return Res::Err("reward address change is disabled for Fort Canning");

auto node = GetMasternode(nodeId);
if (!node) {
return Res::Err("masternode %s does not exists", nodeId.ToString());
}
auto state = node->GetState(height);
if ((state != CMasternode::PRE_ENABLED && state != CMasternode::ENABLED)) {
return Res::Err("masternode %s state is not 'PRE_ENABLED' or 'ENABLED'", nodeId.ToString());
}

// If old masternode update foor new serialisatioono
if (node->version < CMasternode::VERSION0) {
node->version = CMasternode::VERSION0;
}

// Set new reward address
node.rewardAddressType = rewardAddressType;
node.rewardAddress = rewardAddress;
WriteBy<ID>(nodeId, node);
}
node->rewardAddressType = rewardAddressType;
node->rewardAddress = rewardAddress;
WriteBy<ID>(nodeId, *node);

void CMasternodesView::RemForcedRewardAddress(uint256 const & nodeId, CMasternode& node, int height)
{
node.rewardAddressType = 0;
node.rewardAddress.SetNull();
WriteBy<ID>(nodeId, node);
return Res::Ok();
}

void CMasternodesView::UpdateMasternodeOperator(uint256 const & nodeId, CMasternode& node, const char operatorType, const CKeyID& operatorAuthAddress, int height)
Res CMasternodesView::RemForcedRewardAddress(uint256 const & nodeId, int height)
{
// Remove old record
EraseBy<Operator>(node.operatorAuthAddress);
// Temporarily disabled for 2.2
return Res::Err("reward address change is disabled for Fort Canning");

node.operatorType = operatorType;
node.operatorAuthAddress = operatorAuthAddress;
auto node = GetMasternode(nodeId);
if (!node) {
return Res::Err("masternode %s does not exists", nodeId.ToString());
}
auto state = node->GetState(height);
if ((state != CMasternode::PRE_ENABLED && state != CMasternode::ENABLED)) {
return Res::Err("masternode %s state is not 'PRE_ENABLED' or 'ENABLED'", nodeId.ToString());
}

// Overwrite and create new record
WriteBy<ID>(nodeId, node);
WriteBy<Operator>(node.operatorAuthAddress, nodeId);
node->rewardAddressType = 0;
node->rewardAddress.SetNull();
WriteBy<ID>(nodeId, *node);

return Res::Ok();
}

void CMasternodesView::UpdateMasternodeOwner(uint256 const & nodeId, CMasternode& node, const char ownerType, const CKeyID& ownerAuthAddress)
{
// Remove old record
EraseBy<Owner>(node.ownerAuthAddress);
Res CMasternodesView::UpdateMasternode(uint256 const & nodeId, char operatorType, const CKeyID& operatorAuthAddress, int height) {
// Temporarily disabled for 2.2
return Res::Err("updatemasternode is disabled for Fort Canning");

node.ownerType = ownerType;
node.ownerAuthAddress = ownerAuthAddress;
// auth already checked!
auto node = GetMasternode(nodeId);
if (!node) {
return Res::Err("node %s does not exists", nodeId.ToString());
}

// Overwrite and create new record
WriteBy<ID>(nodeId, node);
WriteBy<Owner>(node.ownerAuthAddress, nodeId);
}
const auto state = node->GetState(height);
if (state != CMasternode::ENABLED) {
return Res::Err("node %s state is not 'ENABLED'", nodeId.ToString());
}

void CMasternodesView::UpdateMasternodeCollateral(uint256 const & nodeId, CMasternode& node, const uint256& newCollateralTx, const int height)
{
// Remove old record.
EraseBy<NewCollateral>(node.collateralTx);
if (operatorType == node->operatorType && operatorAuthAddress == node->operatorAuthAddress) {
return Res::Err("The new operator is same as existing operator");
}

// Store new collateral. Used by HasCollateralAuth.
node.collateralTx = newCollateralTx;
WriteBy<ID>(nodeId, node);
// Remove old record
EraseBy<Operator>(node->operatorAuthAddress);

// Prioritise fast lookup in CanSpend() and GetState()
WriteBy<NewCollateral>(newCollateralTx, MNNewOwnerHeightValue{static_cast<uint32_t>(height + GetMnResignDelay(height)), nodeId});
}
node->operatorType = operatorType;
node->operatorAuthAddress = operatorAuthAddress;

std::optional<MNNewOwnerHeightValue> CMasternodesView::GetNewCollateral(const uint256& txid) const
{
return ReadBy<NewCollateral, MNNewOwnerHeightValue>(txid);
}
// Overwrite and create new record
WriteBy<ID>(nodeId, *node);
WriteBy<Operator>(node->operatorAuthAddress, nodeId);

void CMasternodesView::ForEachNewCollateral(std::function<bool(const uint256&, CLazySerialize<MNNewOwnerHeightValue>)> callback)
{
ForEach<NewCollateral, uint256, MNNewOwnerHeightValue>(callback);
return Res::Ok();
}

void CMasternodesView::SetMasternodeLastBlockTime(const CKeyID & minter, const uint32_t &blockHeight, const int64_t& time)
Expand Down Expand Up @@ -766,7 +770,7 @@ CTeamView::CTeam CCustomCSView::CalcNextTeam(int height, const uint256 & stakeMo

std::map<arith_uint256, CKeyID, std::less<arith_uint256>> priorityMN;
ForEachMasternode([&] (uint256 const & id, CMasternode node) {
if(!node.IsActive(height, *this))
if(!node.IsActive(height))
return true;

CDataStream ss{SER_GETHASH, PROTOCOL_VERSION};
Expand Down Expand Up @@ -806,7 +810,7 @@ void CCustomCSView::CalcAnchoringTeams(const uint256 & stakeModifier, const CBlo
std::map<arith_uint256, CKeyID, std::less<arith_uint256>> authMN;
std::map<arith_uint256, CKeyID, std::less<arith_uint256>> confirmMN;
ForEachMasternode([&] (uint256 const & id, CMasternode node) {
if(!node.IsActive(pindexNew->nHeight, *this))
if(!node.IsActive(pindexNew->nHeight))
return true;

// Not in our list of MNs from last week, skip.
Expand Down Expand Up @@ -883,17 +887,9 @@ bool CCustomCSView::CanSpend(const uint256 & txId, int height) const
auto node = GetMasternode(txId);
// check if it was mn collateral and mn was resigned or banned
if (node) {
auto state = node->GetState(height, *this);
return state == CMasternode::RESIGNED;
}

if (auto mn = GetNewCollateral(txId)) {
auto node = GetMasternode(mn->masternodeID);
assert(node);
auto state = node->GetState(height, *this);
auto state = node->GetState(height);
return state == CMasternode::RESIGNED;
}

// check if it was token collateral and token already destroyed
/// @todo token check for total supply/limit when implemented
auto pair = GetTokenByCreationTx(txId);
Expand Down Expand Up @@ -1173,7 +1169,7 @@ std::map<CKeyID, CKey> AmISignerNow(int height, CAnchorData::CTeam const & team)
continue;
}

if (node->IsActive(height, *pcustomcsview) && team.find(mnId.first) != team.end()) {
if (node->IsActive(height) && team.find(mnId.first) != team.end()) {
CKey masternodeKey;
std::vector<std::shared_ptr<CWallet>> wallets = GetWallets();
for (auto const & wallet : wallets) {
Expand Down
Loading

0 comments on commit b6dce06

Please sign in to comment.