Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

5% block reward code to AVN foundation wallet #87

Merged
merged 5 commits into from
Sep 20, 2022
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
2 changes: 2 additions & 0 deletions src/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -473,6 +473,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 @@ -513,6 +514,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
2 changes: 1 addition & 1 deletion src/bench/checkblock.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ static void DeserializeAndCheckBlockTest(benchmark::State& state)
assert(stream.Rewind(sizeof(block_bench::block566553)));

CValidationState validationState;
assert(CheckBlock(block, validationState, chainParams->GetConsensus()));
assert(CheckBlock(block, validationState, chainParams->GetConsensus(), 0));
}
}

Expand Down
3 changes: 2 additions & 1 deletion src/blockencodings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,8 @@ ReadStatus PartiallyDownloadedBlock::FillBlock(CBlock& block, const std::vector<
return READ_STATUS_INVALID;

CValidationState state;
if (!CheckBlock(block, state, Params().GetConsensus())) {

if (!CheckBlock(block, state, Params().GetConsensus(), 0)) {
// TODO: We really want to just check merkle tree manually here,
// but that is expensive, and CheckBlock caches a block's
// "checked-status" (in the CBlock?). CBlock should be able to
Expand Down
29 changes: 22 additions & 7 deletions src/chainparams.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,12 @@ class CMainParams : public CChainParams {

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

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

fDefaultConsistencyChecks = false;
fRequireStandard = true;
fMineBlocksOnDemand = false;
Expand Down Expand Up @@ -359,6 +365,12 @@ class CTestNetParams : public CChainParams {

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

// Founder reward
vector<FounderRewardStructure> rewardStructures = {
{INT_MAX, 5} // 5% founder/dev fee forever
};
consensus.nFounderPayment = FounderPayment(rewardStructures, 150, "n1BurnXXXXXXXXXXXXXXXXXXXXXXU1qejP"); // Block 150 (burn coins)

fDefaultConsistencyChecks = false;
fRequireStandard = false;
fMineBlocksOnDemand = false;
Expand All @@ -371,14 +383,11 @@ class CTestNetParams : public CChainParams {
};

chainTxData = ChainTxData{
// Update as we know more about the contents of the Avian chain
// Stats as of 0000cd2943664b4bda8be6e80351f9ff022475ae6a341a868babc4efd846000d block 42000
1654349227, // * UNIX timestamp of last known number of transactions
66085, // * total number of transactions between genesis and that timestamp
// (the tx=... number in the SetBestChain debug.log lines)
0.001 // * estimated number of transactions per second after that timestamp
0,
0,
0
};

/** AVN Start **/
// Burn Amounts
nIssueAssetBurnAmount = 500 * COIN;
Expand Down Expand Up @@ -547,6 +556,12 @@ class CRegTestParams : public CChainParams {
assert(consensus.hashGenesisBlock == uint256S("0x653634d03d27ed84e8aba5dd47903906ad7be4876a1d3677be0db2891dcf787f"));
assert(genesis.hashMerkleRoot == uint256S("63d9b6b6b549a2d96eb5ac4eb2ab80761e6d7bffa9ae1a647191e08d6416184d"));

// Founder reward
vector<FounderRewardStructure> rewardStructures = {
{INT_MAX, 5} // 5% founder/dev fee forever
};
consensus.nFounderPayment = FounderPayment(rewardStructures, 1, "n1BurnXXXXXXXXXXXXXXXXXXXXXXU1qejP"); // Block 1 (burn coins)

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

Expand Down
6 changes: 6 additions & 0 deletions src/consensus/params.h
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
// Copyright (c) 2009-2010 Satoshi Nakamoto
// Copyright (c) 2009-2016 The Bitcoin Core developers
// Copyright (c) 2017 The Raven Core developers
// Copyright (c) 2022 The Avian Core developers
// Distributed under the MIT software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.

#ifndef AVIAN_CONSENSUS_PARAMS_H
#define AVIAN_CONSENSUS_PARAMS_H

#include "uint256.h"
#include "founder_payment.h"

#include <cstdint>
#include <map>
#include <string>
Expand Down Expand Up @@ -90,6 +93,9 @@ struct ConsensusParams {
// AVN Flight Plans
uint32_t nFlightPlansActivationTime;

// AVN Founder Payment
FounderPayment nFounderPayment;

// Avian Name System (ANS)
uint32_t nAvianNameSystemTime;

Expand Down
14 changes: 12 additions & 2 deletions src/consensus/tx_verify.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// Copyright (c) 2017-2017 The Bitcoin Core developers
// Copyright (c) 2017-2020 The Raven Core developers
// Copyright (c) 2022 The Avian Core developers
// Distributed under the MIT software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.

Expand Down Expand Up @@ -166,7 +167,7 @@ int64_t GetTransactionSigOpCost(const CTransaction& tx, const CCoinsViewCache& i
return nSigOps;
}

bool CheckTransaction(const CTransaction& tx, CValidationState &state, bool fCheckDuplicateInputs, bool fMempoolCheck, bool fBlockCheck)
bool CheckTransaction(const CTransaction& tx, CValidationState &state, int nHeight, CAmount blockReward, bool fCheckDuplicateInputs, bool fMempoolCheck, bool fBlockCheck)
{
// Basic checks that don't depend on any context
if (tx.vin.empty())
Expand Down Expand Up @@ -383,10 +384,19 @@ bool CheckTransaction(const CTransaction& tx, CValidationState &state, bool fChe
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");
REJECT_INVALID, "bad-txns-coinbase-contains-asset-txes");
}
}
}

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
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 @@ class CNullAssetTxData;
/** Transaction validation functions */

/** Context-independent validity checks */
bool CheckTransaction(const CTransaction& tx, CValidationState& state, bool fCheckDuplicateInputs=true, bool fMempoolCheck = false, bool fBlockCheck = false);
bool CheckTransaction(const CTransaction& tx, CValidationState& state, int nHeight, CAmount blockReward, bool fCheckDuplicateInputs=true, bool fMempoolCheck = false, bool fBlockCheck = false);

namespace Consensus {
/**
Expand Down
59 changes: 59 additions & 0 deletions src/founder_payment.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
#include "founder_payment.h"

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

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
CAvianAddress 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(CAvianAddress(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;
}
41 changes: 41 additions & 0 deletions src/founder_payment.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#ifndef AVN_FOUNDER_PAYMENT_H_
#define AVN_FOUNDER_PAYMENT_H_

#include "amount.h"
#include "primitives/transaction.h"
#include "script/standard.h"
#include <limits.h>
#include <string>

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 /* AVN_FOUNDER_PAYMENT_H_ */
8 changes: 8 additions & 0 deletions src/miner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,14 @@ 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;

// NOTE: unlike in bitcoin, we need to pass PREVIOUS block height here
CAmount blockReward = nFees + GetBlockSubsidy(pindexPrev->nHeight, Params().GetConsensus());

// Fill founder payment
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
4 changes: 4 additions & 0 deletions src/primitives/block.h
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,9 @@ class CBlock : public CBlockHeader
// network and disk
std::vector<CTransactionRef> vtx;

// founder payment
mutable CTxOut txoutFounder;

// memory only
mutable bool fChecked;

Expand Down Expand Up @@ -161,6 +164,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);
CAvianAddress 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
8 changes: 4 additions & 4 deletions src/test/assets/asset_tx_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -528,7 +528,7 @@ BOOST_FIXTURE_TEST_SUITE(asset_tx_tests, BasicTestingSetup)
bool fCheckBlock = false;

// Check that the CheckTransaction will fail when trying to add it to the mempool
bool fCheck = !CheckTransaction(tx, state, true, fCheckMempool, fCheckBlock);
bool fCheck = !CheckTransaction(tx, state, 0, 0, true, fCheckMempool, fCheckBlock);

BOOST_CHECK(fCheck);
BOOST_CHECK(state.GetRejectReason() == "bad-mempool-txns-asset-reissued-amount-isn't-zero");
Expand All @@ -539,7 +539,7 @@ BOOST_FIXTURE_TEST_SUITE(asset_tx_tests, BasicTestingSetup)
// Turn on the BIP that enforces the block check
SetEnforcedValues(true);

fCheck = !CheckTransaction(tx, state, true, fCheckMempool, fCheckBlock);
fCheck = !CheckTransaction(tx, state, 0, 0, true, fCheckMempool, fCheckBlock);
BOOST_CHECK(fCheck);
BOOST_CHECK(state.GetRejectReason() == "bad-txns-asset-reissued-amount-isn't-zero");
}
Expand Down Expand Up @@ -591,14 +591,14 @@ BOOST_FIXTURE_TEST_SUITE(asset_tx_tests, BasicTestingSetup)
// Setting the coinbase check to true
// This check should now fail on the CheckTransaction call
SetEnforcedCoinbase(true);
bool fCheck = CheckTransaction(tx, state, true);
bool fCheck = CheckTransaction(tx, state, 0, 0, true);
BOOST_CHECK(!fCheck);
BOOST_CHECK(state.GetRejectReason() == "bad-txns-coinbase-contains-asset-txes");

// Setting the coinbase check to false
// This check should now pass the CheckTransaction call
SetEnforcedCoinbase(false);
fCheck = CheckTransaction(tx, state, true);
fCheck = CheckTransaction(tx, state, 0, 0, true);
BOOST_CHECK(fCheck);

// Remove wallet used for testing
Expand Down
2 changes: 1 addition & 1 deletion src/test/sighash_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ BOOST_FIXTURE_TEST_SUITE(sighash_tests, BasicTestingSetup)
stream >> tx;

CValidationState state;
BOOST_CHECK_MESSAGE(CheckTransaction(*tx, state), strTest);
BOOST_CHECK_MESSAGE(CheckTransaction(*tx, state, 0, 0), strTest);
BOOST_CHECK(state.IsValid());

std::vector<unsigned char> raw = ParseHex(raw_script);
Expand Down
8 changes: 4 additions & 4 deletions src/test/transaction_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ BOOST_FIXTURE_TEST_SUITE(transaction_tests, BasicTestingSetup)
CTransaction tx(deserialize, stream);

CValidationState state;
BOOST_CHECK_MESSAGE(CheckTransaction(tx, state), strTest);
BOOST_CHECK_MESSAGE(CheckTransaction(tx, state, 0, 0), strTest);
BOOST_CHECK(state.IsValid());

PrecomputedTransactionData txdata(tx);
Expand Down Expand Up @@ -252,7 +252,7 @@ BOOST_FIXTURE_TEST_SUITE(transaction_tests, BasicTestingSetup)
CTransaction tx(deserialize, stream);

CValidationState state;
fValid = CheckTransaction(tx, state) && state.IsValid();
fValid = CheckTransaction(tx, state, 0, 0) && state.IsValid();

PrecomputedTransactionData txdata(tx);
for (unsigned int i = 0; i < tx.vin.size() && fValid; i++)
Expand Down Expand Up @@ -290,11 +290,11 @@ BOOST_FIXTURE_TEST_SUITE(transaction_tests, BasicTestingSetup)
CMutableTransaction tx;
stream >> tx;
CValidationState state;
BOOST_CHECK_MESSAGE(CheckTransaction(tx, state) && state.IsValid(), "Simple deserialized transaction should be valid.");
BOOST_CHECK_MESSAGE(CheckTransaction(tx, state, 0, 0) && state.IsValid(), "Simple deserialized transaction should be valid.");

// Check that duplicate txins fail
tx.vin.push_back(tx.vin[0]);
BOOST_CHECK_MESSAGE(!CheckTransaction(tx, state) || !state.IsValid(), "Transaction with duplicate txins should be invalid.");
BOOST_CHECK_MESSAGE(!CheckTransaction(tx, state, 0, 0) || !state.IsValid(), "Transaction with duplicate txins should be invalid.");
}

//
Expand Down
Loading