Skip to content

Commit

Permalink
Commit of block reward code
Browse files Browse the repository at this point in the history
1st Commit of 5% block reward code to AVN foundation wallet
  • Loading branch information
decentralisedcoder authored and alamshafil committed Jul 13, 2022
1 parent 70b4838 commit e7f4373
Show file tree
Hide file tree
Showing 10 changed files with 146 additions and 10 deletions.
2 changes: 2 additions & 0 deletions src/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -475,6 +475,7 @@ libavian_consensus_a_CPPFLAGS = $(AM_CPPFLAGS) $(AVIAN_INCLUDES)
libavian_consensus_a_CXXFLAGS = $(AM_CXXFLAGS) $(PIE_FLAGS)
libavian_consensus_a_SOURCES = \
amount.h \
founder_payment.h \
arith_uint256.cpp \
arith_uint256.h \
consensus/merkle.cpp \
Expand Down Expand Up @@ -515,6 +516,7 @@ libavian_common_a_CPPFLAGS = $(AM_CPPFLAGS) $(AVIAN_INCLUDES)
libavian_common_a_CXXFLAGS = $(AM_CXXFLAGS) $(PIE_FLAGS)
libavian_common_a_SOURCES = \
base58.cpp \
founder_payment.cpp \
chainparams.cpp \
coins.cpp \
compressor.cpp \
Expand Down
1 change: 1 addition & 0 deletions src/blockencodings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,7 @@ ReadStatus PartiallyDownloadedBlock::FillBlock(CBlock& block, const std::vector<
return READ_STATUS_INVALID;

CValidationState state;
//skip founder check
if (!CheckBlock(block, state, Params().GetConsensus())) {
// TODO: We really want to just check merkle tree manually here,
// but that is expensive, and CheckBlock caches a block's
Expand Down
17 changes: 17 additions & 0 deletions src/chainparams.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,10 @@ class CMainParams : public CChainParams {
// Avian BIP44 cointype in mainnet is '175'
nExtCoinType = 175;

vector<FounderRewardStructure> rewardStructures = { {INT_MAX, 5}// 5% founder/dev fee forever
};
consensus.nFounderPayment = FounderPayment(rewardStructures, 250);

vFixedSeeds = std::vector<SeedSpec6>(pnSeed6_main, pnSeed6_main + ARRAYLEN(pnSeed6_main));

fDefaultConsistencyChecks = false;
Expand Down Expand Up @@ -367,6 +371,11 @@ class CTestNetParams : public CChainParams {

vFixedSeeds = std::vector<SeedSpec6>(pnSeed6_test, pnSeed6_test + ARRAYLEN(pnSeed6_test));

vector<FounderRewardStructure> rewardStructures = { {INT_MAX, 5}// 5% founder/dev fee forever
};
// NEED TO SET TESTNET REWARD ADDRESS
consensus.nFounderPayment = FounderPayment(rewardStructures, 200, "NEEDTOSETASTESTNETREWARDADDRESS");

fDefaultConsistencyChecks = false;
fRequireStandard = false;
fMineBlocksOnDemand = false;
Expand Down Expand Up @@ -557,6 +566,10 @@ class CRegTestParams : public CChainParams {
assert(consensus.hashGenesisBlock == uint256S("0x653634d03d27ed84e8aba5dd47903906ad7be4876a1d3677be0db2891dcf787f"));
assert(genesis.hashMerkleRoot == uint256S("63d9b6b6b549a2d96eb5ac4eb2ab80761e6d7bffa9ae1a647191e08d6416184d"));

vector<FounderRewardStructure> rewardStructures = { {INT_MAX, 5}// 5% founder/dev fee forever
};
consensus.nFounderPayment = FounderPayment(rewardStructures, 200);

vFixedSeeds.clear(); //!< Regtest mode doesn't have any fixed seeds.
vSeeds.clear(); //!< Regtest mode doesn't have any DNS seeds.

Expand All @@ -565,6 +578,10 @@ class CRegTestParams : public CChainParams {
fMineBlocksOnDemand = true;
fMiningRequiresPeers = false;

vector<FounderRewardStructure> rewardStructures = { {INT_MAX, 5}// 5% founder/dev fee forever
};
consensus.nFounderPayment = FounderPayment(rewardStructures, 200);

checkpointData = (CCheckpointData) {
{
}
Expand Down
4 changes: 4 additions & 0 deletions src/consensus/params.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#define AVIAN_CONSENSUS_PARAMS_H

#include "uint256.h"
#include "founder_payment.h"
#include <cstdint>
#include <map>
#include <string>
Expand Down Expand Up @@ -86,6 +87,9 @@ struct ConsensusParams {
// AVN Flight Plans
uint32_t nFlightPlansActivationTime;

// AVN Founder Payment
FounderPayment nFounderPayment;

};
} // namespace Consensus

Expand Down
23 changes: 13 additions & 10 deletions src/consensus/tx_verify.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -376,17 +376,20 @@ bool CheckTransaction(const CTransaction& tx, CValidationState &state, bool fChe

if (tx.IsCoinBase())
{
if (tx.vin[0].scriptSig.size() < 2 || tx.vin[0].scriptSig.size() > 100)
return state.DoS(100, false, REJECT_INVALID, "bad-cb-length");

if (AreCoinbaseCheckAssetsDeployed()) {
for (auto vout : tx.vout) {
if (vout.scriptPubKey.IsAssetScript() || vout.scriptPubKey.IsNullAsset()) {
return state.DoS(0, error("%s: coinbase contains asset transaction", __func__),
REJECT_INVALID, "bad-txns-coinbase-contains-asset-txes");
}
}
size_t minCbSize = 2;
if (tx.nType == TRANSACTION_COINBASE) {
// With the introduction of CbTx, coinbase scripts are not required anymore to hold a valid block height
minCbSize = 1;
}
if (tx.vin[0].scriptSig.size() < minCbSize || tx.vin[0].scriptSig.size() > 100)
return state.DoS(100, false, REJECT_INVALID, "bad-cb-length");
FounderPayment founderPayment = Params().GetConsensus().nFounderPayment;
CAmount founderReward = founderPayment.getFounderPaymentAmount(nHeight, blockReward);
int founderStartHeight = founderPayment.getStartBlock();
//std::cout << "height=" << nHeight << ", reward=" << founderReward << endl;
if(nHeight > founderStartHeight && founderReward && !founderPayment.IsBlockPayeeValid(tx,nHeight,blockReward)) {
return state.DoS(100, false, REJECT_INVALID, "bad-cb-founder-payment-not-found");
}
}
else
{
Expand Down
56 changes: 56 additions & 0 deletions src/founder_payment.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
#include "founder_payment.h"

#include "util.h"
#include "chainparams.h"
#include <boost/foreach.hpp>
#include "base58.h"

CAmount FounderPayment::getFounderPaymentAmount(int blockHeight, CAmount blockReward) {
if (blockHeight <= startBlock){
return 0;
}
for(int i = 0; i < rewardStructures.size(); i++) {
FounderRewardStructure rewardStructure = rewardStructures[i];
if(rewardStructure.blockHeight == INT_MAX || blockHeight <= rewardStructure.blockHeight) {
return blockReward * rewardStructure.rewardPercentage / 100;
}
}
return 0;
}

void FounderPayment::FillFounderPayment(CMutableTransaction& txNew, int nBlockHeight, CAmount blockReward, CTxOut& txoutFounderRet) {
// make sure it's not filled yet
CAmount founderPayment = getFounderPaymentAmount(nBlockHeight, blockReward);
// if(founderPayment == 0) {
// LogPrintf("FounderPayment::FillFounderPayment -- Founder payment has not started\n");
// return;
//
// }
txoutFounderRet = CTxOut();
CScript payee;
// fill payee with the foundFounderRewardStrcutureFounderRewardStrcutureer address
CBitcoinAddress cbAddress(founderAddress);
payee = GetScriptForDestination(cbAddress.Get());
// GET FOUNDER PAYMENT VARIABLES SETUP

// split reward between miner ...
txNew.vout[0].nValue -= founderPayment;
txoutFounderRet = CTxOut(founderPayment, payee);
txNew.vout.push_back(txoutFounderRet);
LogPrintf("FounderPayment::FillFounderPayment -- Founder payment %lld to %s\n", founderPayment, founderAddress.c_str());
}

bool FounderPayment::IsBlockPayeeValid(const CTransaction& txNew, const int height, const CAmount blockReward) {
CScript payee;
// fill payee with the founder address
payee = GetScriptForDestination(CBitcoinAddress(founderAddress).Get());
const CAmount founderReward = getFounderPaymentAmount(height, blockReward);
//std::cout << "founderReward = " << founderReward << endl;
BOOST_FOREACH(const CTxOut& out, txNew.vout) {
if(out.scriptPubKey == payee && out.nValue >= founderReward) {
return true;
}
}

return false;
}
36 changes: 36 additions & 0 deletions src/founder_payment.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#ifndef SRC_FOUNDER_PAYMENT_H_
#define SRC_FOUNDER_PAYMENT_H_
#include <string>
#include "amount.h"
#include "primitives/transaction.h"
#include "script/standard.h"
#include <limits.h>
using namespace std;

static const string DEFAULT_FOUNDER_ADDRESS = "RDs4A4sDHp4otDHQQuFSaPDYEg2xx3hbdN";
struct FounderRewardStructure {
int blockHeight;
int rewardPercentage;
};

class FounderPayment {
public:
FounderPayment(vector<FounderRewardStructure> rewardStructures = {}, int startBlock = 0, const string &address = DEFAULT_FOUNDER_ADDRESS) {
this->founderAddress = address;
this->startBlock = startBlock;
this->rewardStructures = rewardStructures;
}
~FounderPayment(){};
CAmount getFounderPaymentAmount(int blockHeight, CAmount blockReward);
void FillFounderPayment(CMutableTransaction& txNew, int nBlockHeight, CAmount blockReward, CTxOut& txoutFounderRet);
bool IsBlockPayeeValid(const CTransaction& txNew, const int height, const CAmount blockReward);
int getStartBlock() {return this->startBlock;}
private:
string founderAddress;
int startBlock;
vector<FounderRewardStructure> rewardStructures;
};



#endif /* SRC_FOUNDER_PAYMENT_H_ */
2 changes: 2 additions & 0 deletions src/miner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,8 @@ std::unique_ptr<CBlockTemplate> BlockAssembler::CreateNewBlock(const CScript& sc
coinbaseTx.vout[0].scriptPubKey = scriptPubKeyIn;
coinbaseTx.vout[0].nValue = nFees + GetBlockSubsidy(nHeight, chainparams.GetConsensus());
coinbaseTx.vin[0].scriptSig = CScript() << nHeight << OP_0;
FounderPayment founderPayment = chainparams.GetConsensus().nFounderPayment;
founderPayment.FillFounderPayment(coinbaseTx, nHeight, blockReward, pblock->txoutFounder);
pblock->vtx[0] = MakeTransactionRef(std::move(coinbaseTx));
pblocktemplate->vchCoinbaseCommitment = GenerateCoinbaseCommitment(*pblock, pindexPrev, chainparams.GetConsensus());
pblocktemplate->vTxFees[0] = -nFees;
Expand Down
2 changes: 2 additions & 0 deletions src/primitives/block.h
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ class CBlock : public CBlockHeader
// network and disk
std::vector<CTransactionRef> vtx;

mutable CTxOut txoutFounder; // founder payment
// memory only
mutable bool fChecked;

Expand Down Expand Up @@ -161,6 +162,7 @@ class CBlock : public CBlockHeader
CBlockHeader::SetNull();
vtx.clear();
fChecked = false;
txoutFounder = CTxOut();
}

CBlockHeader GetBlockHeader() const
Expand Down
13 changes: 13 additions & 0 deletions src/rpc/mining.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -697,6 +697,19 @@ UniValue getblocktemplate(const JSONRPCRequest& request)
UniValue result(UniValue::VOBJ);
result.push_back(Pair("capabilities", aCaps));

UniValue founderObj(UniValue::VOBJ);
FounderPayment founderPayment = Params().GetConsensus().nFounderPayment;
if(pblock->txoutFounder!= CTxOut()) {
CTxDestination address;
ExtractDestination(pblock->txoutFounder.scriptPubKey, address);
CBitcoinAddress address2(address);
founderObj.push_back(Pair("payee", address2.ToString().c_str()));
founderObj.push_back(Pair("script", HexStr(pblock->txoutFounder.scriptPubKey.begin(), pblock->txoutFounder.scriptPubKey.end())));
founderObj.push_back(Pair("amount", pblock->txoutFounder.nValue));
}
result.push_back(Pair("founder", founderObj));
result.push_back(Pair("founder_payments_started", pindexPrev->nHeight + 1 > founderPayment.getStartBlock()));

UniValue aRules(UniValue::VARR);
UniValue vbavailable(UniValue::VOBJ);
for (int j = 0; j < (int)Consensus::MAX_VERSION_BITS_DEPLOYMENTS; ++j) {
Expand Down

0 comments on commit e7f4373

Please sign in to comment.