From e7f43735c58edc8e853c1427732065fa3587697b Mon Sep 17 00:00:00 2001 From: decentralisedcoder <57637916+decentralisedcoder@users.noreply.github.com> Date: Wed, 13 Jul 2022 19:59:56 +0100 Subject: [PATCH] Commit of block reward code 1st Commit of 5% block reward code to AVN foundation wallet --- src/Makefile.am | 2 ++ src/blockencodings.cpp | 1 + src/chainparams.cpp | 17 +++++++++++ src/consensus/params.h | 4 +++ src/consensus/tx_verify.cpp | 23 ++++++++------- src/founder_payment.cpp | 56 +++++++++++++++++++++++++++++++++++++ src/founder_payment.h | 36 ++++++++++++++++++++++++ src/miner.cpp | 2 ++ src/primitives/block.h | 2 ++ src/rpc/mining.cpp | 13 +++++++++ 10 files changed, 146 insertions(+), 10 deletions(-) create mode 100644 src/founder_payment.cpp create mode 100644 src/founder_payment.h diff --git a/src/Makefile.am b/src/Makefile.am index 552cc7816..77a88cef5 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -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 \ @@ -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 \ diff --git a/src/blockencodings.cpp b/src/blockencodings.cpp index e3c1ee1bc..a5382a5d9 100644 --- a/src/blockencodings.cpp +++ b/src/blockencodings.cpp @@ -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 diff --git a/src/chainparams.cpp b/src/chainparams.cpp index 0d89694e6..760b3bb26 100644 --- a/src/chainparams.cpp +++ b/src/chainparams.cpp @@ -215,6 +215,10 @@ class CMainParams : public CChainParams { // Avian BIP44 cointype in mainnet is '175' nExtCoinType = 175; + vector rewardStructures = { {INT_MAX, 5}// 5% founder/dev fee forever + }; + consensus.nFounderPayment = FounderPayment(rewardStructures, 250); + vFixedSeeds = std::vector(pnSeed6_main, pnSeed6_main + ARRAYLEN(pnSeed6_main)); fDefaultConsistencyChecks = false; @@ -367,6 +371,11 @@ class CTestNetParams : public CChainParams { vFixedSeeds = std::vector(pnSeed6_test, pnSeed6_test + ARRAYLEN(pnSeed6_test)); + vector 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; @@ -557,6 +566,10 @@ class CRegTestParams : public CChainParams { assert(consensus.hashGenesisBlock == uint256S("0x653634d03d27ed84e8aba5dd47903906ad7be4876a1d3677be0db2891dcf787f")); assert(genesis.hashMerkleRoot == uint256S("63d9b6b6b549a2d96eb5ac4eb2ab80761e6d7bffa9ae1a647191e08d6416184d")); + vector 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. @@ -565,6 +578,10 @@ class CRegTestParams : public CChainParams { fMineBlocksOnDemand = true; fMiningRequiresPeers = false; + vector rewardStructures = { {INT_MAX, 5}// 5% founder/dev fee forever + }; + consensus.nFounderPayment = FounderPayment(rewardStructures, 200); + checkpointData = (CCheckpointData) { { } diff --git a/src/consensus/params.h b/src/consensus/params.h index 132153c9a..d3fc01307 100644 --- a/src/consensus/params.h +++ b/src/consensus/params.h @@ -8,6 +8,7 @@ #define AVIAN_CONSENSUS_PARAMS_H #include "uint256.h" +#include "founder_payment.h" #include #include #include @@ -86,6 +87,9 @@ struct ConsensusParams { // AVN Flight Plans uint32_t nFlightPlansActivationTime; + // AVN Founder Payment + FounderPayment nFounderPayment; + }; } // namespace Consensus diff --git a/src/consensus/tx_verify.cpp b/src/consensus/tx_verify.cpp index e0a6d4feb..aa9108e8a 100644 --- a/src/consensus/tx_verify.cpp +++ b/src/consensus/tx_verify.cpp @@ -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 { diff --git a/src/founder_payment.cpp b/src/founder_payment.cpp new file mode 100644 index 000000000..3d573bac4 --- /dev/null +++ b/src/founder_payment.cpp @@ -0,0 +1,56 @@ +#include "founder_payment.h" + +#include "util.h" +#include "chainparams.h" +#include +#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; +} diff --git a/src/founder_payment.h b/src/founder_payment.h new file mode 100644 index 000000000..418054977 --- /dev/null +++ b/src/founder_payment.h @@ -0,0 +1,36 @@ +#ifndef SRC_FOUNDER_PAYMENT_H_ +#define SRC_FOUNDER_PAYMENT_H_ +#include +#include "amount.h" +#include "primitives/transaction.h" +#include "script/standard.h" +#include +using namespace std; + +static const string DEFAULT_FOUNDER_ADDRESS = "RDs4A4sDHp4otDHQQuFSaPDYEg2xx3hbdN"; +struct FounderRewardStructure { + int blockHeight; + int rewardPercentage; +}; + +class FounderPayment { +public: + FounderPayment(vector 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 rewardStructures; +}; + + + +#endif /* SRC_FOUNDER_PAYMENT_H_ */ \ No newline at end of file diff --git a/src/miner.cpp b/src/miner.cpp index 54334ed86..aa29bc6fc 100644 --- a/src/miner.cpp +++ b/src/miner.cpp @@ -197,6 +197,8 @@ std::unique_ptr 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; diff --git a/src/primitives/block.h b/src/primitives/block.h index d3a8b343d..4e55961d9 100644 --- a/src/primitives/block.h +++ b/src/primitives/block.h @@ -134,6 +134,7 @@ class CBlock : public CBlockHeader // network and disk std::vector vtx; + mutable CTxOut txoutFounder; // founder payment // memory only mutable bool fChecked; @@ -161,6 +162,7 @@ class CBlock : public CBlockHeader CBlockHeader::SetNull(); vtx.clear(); fChecked = false; + txoutFounder = CTxOut(); } CBlockHeader GetBlockHeader() const diff --git a/src/rpc/mining.cpp b/src/rpc/mining.cpp index 9782d786f..3f9ef9bfe 100644 --- a/src/rpc/mining.cpp +++ b/src/rpc/mining.cpp @@ -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) {