Skip to content

Commit

Permalink
Merge pull request #187 from TheLindaProjectInc/govPaymentCheck
Browse files Browse the repository at this point in the history
Ensure the winner cannot also be the staker
  • Loading branch information
nibbles83 authored Jun 6, 2022
2 parents fd20a63 + fab8a97 commit 075d23f
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 2 deletions.
1 change: 1 addition & 0 deletions Changelog.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
This document describes breaking changes between Metrix releases for both RPC APIs and for blockchain consensus.
## Consensus Parameters/Rules
* 4.0.7.0 - Upgrade to protocol verion to 70018, soft fork when MIP1 active will disconnect node versions prior to this.
* 4.0.8.2 - Upgrade to protocol verion to 70021.
## EVM Behavior

## RPC APIs
Expand Down
2 changes: 1 addition & 1 deletion configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ AC_PREREQ([2.60])
define(_CLIENT_VERSION_MAJOR, 4)
define(_CLIENT_VERSION_MINOR, 0)
define(_CLIENT_VERSION_REVISION, 8)
define(_CLIENT_VERSION_BUILD, 1)
define(_CLIENT_VERSION_BUILD, 2)
define(_CLIENT_VERSION_RC, 0)
define(_CLIENT_VERSION_IS_RELEASE, true)
define(_COPYRIGHT_YEAR, 2022)
Expand Down
15 changes: 15 additions & 0 deletions doc/release-notes/release-notes-4.0.8.2.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# Metrix 4.0.8.2

## Bugfix update

Please report bugs using the issue tracker at github: https://github.com/thelindaproject/metrix/issues

## How to Upgrade
Shut down Metrix, wait until it has completely shut down (which might take a few minutes
for older versions), then just copy over the appropriate metrixd file.

# Core

- Bugfix - Prevent staker from also receiving the gov reward, in very rare cases this causes consensus issues and unexpected forks.
- Increment version 4.0.8.2
- New protocol version 70021
38 changes: 38 additions & 0 deletions src/miner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#include <consensus/merkle.h>
#include <consensus/tx_verify.h>
#include <consensus/validation.h>
#include <key_io.h>
#include <policy/feerate.h>
#include <policy/policy.h>
#include <pow.h>
Expand Down Expand Up @@ -598,6 +599,39 @@ void BlockAssembler::AddToBlock(CTxMemPool::txiter iter)
}
}

bool BlockAssembler::IsRewardToSelf(dev::Address addrWinner, CMutableTransaction* coinstakeTx)
{
// Get the senderaddress from the output tx script
CTxDestination addressBit;
PKHash senderAddress;
txnouttype txType;
if(!ExtractDestination(coinstakeTx->vout[1].scriptPubKey, addressBit, &txType)) {
return error("%s: Could not extract sender pubkey from output", __func__);
}

std::string currentAddress = EncodeDestination(addressBit);

// Get convert the govWinner hex to a PK address
std::string hexAddress = HexStr(addrWinner.asBytes());
if (hexAddress.size() != 40)
return error("%s: Invalid pubkeyhash hex size (should be 40 hex characters)", __func__);
PKHash raw;
raw.SetReverseHex(hexAddress);
CTxDestination govWinner(raw);

std::string govWinnerStr = EncodeDestination(govWinner);

// If these match then the staker is choosing itsself as winning gov.
// In rare cases this causes bad consensus, and should just be avoided.
if (currentAddress == govWinnerStr) {
LogPrintf("IsRewardToSelf: Gov Winner : %s, Staker Address : %s, gov reward abandoned. The winner cannot be the staker.\n", govWinnerStr, currentAddress);
return true;
}

return false;
}


void BlockAssembler::AddCoinstakeContracts(CMutableTransaction* coinstakeTx)
{
uint64_t nGasLimit = DEFAULT_GAS_LIMIT_OP_SEND;
Expand All @@ -610,6 +644,10 @@ void BlockAssembler::AddCoinstakeContracts(CMutableTransaction* coinstakeTx)
CAmount nGasPrice = qtumDGP.getMinGasPrice(nHeight);
uint64_t nCollateral = qtumDGP.getGovernanceCollateral(nHeight);
uint64_t nGovernorSubsidy = GetGovernorSubsidy(nHeight, nCollateral);

if(hasGovernorToReward) {
IsRewardToSelf(addrWinner, coinstakeTx) ? hasGovernorToReward = false : 0;
}
//LogPrintf("Gov Winner : %s | hasGovernorToReward : %t | Subsidy : %d\n",
// HexStr(addrWinner.asBytes()),
// hasGovernorToReward,
Expand Down
2 changes: 2 additions & 0 deletions src/miner.h
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,8 @@ class BlockAssembler
/** Add coinstake contract transactions to the coinstake transaction
* of the block. */
void AddCoinstakeContracts(CMutableTransaction* coinstakeTx);

bool IsRewardToSelf(dev::Address addrWinner, CMutableTransaction* coinstakeTx);
};

#ifdef ENABLE_WALLET
Expand Down
2 changes: 1 addition & 1 deletion src/version.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
* network protocol versioning
*/

static const int PROTOCOL_VERSION = 70020;
static const int PROTOCOL_VERSION = 70021;

//! initial proto version, to be increased after version/verack negotiation
static const int INIT_PROTO_VERSION = 209;
Expand Down

0 comments on commit 075d23f

Please sign in to comment.