Skip to content

Commit

Permalink
Merge pull request #124 from nnian/govfee
Browse files Browse the repository at this point in the history
Add fee for QWC foundation
  • Loading branch information
exploshot authored Dec 26, 2019
2 parents a803543 + 156db1a commit bec2212
Show file tree
Hide file tree
Showing 4 changed files with 150 additions and 6 deletions.
115 changes: 111 additions & 4 deletions lib/CryptoNoteCore/Currency.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright (c) 2012-2017, The CryptoNote developers, The Bytecoin developers
// Copyright (c) 2018-2019, The Qwertycoin developers
// Copyright (c) 2016-2018 zawy12
// Copyright (c) 2016-2018, The Karbowanec developers
// Copyright (c) 2018-2019, The Qwertycoin developers
//
// This file is part of Qwertycoin.
//
Expand Down Expand Up @@ -60,6 +60,9 @@ bool Currency::init()
m_upgradeHeightV4 = 70;
m_upgradeHeightV5 = 80;
m_upgradeHeightV6 = 100;
m_governancePercent = 10;
m_governanceHeightStart = 1;
m_governanceHeightEnd = 100;
m_blocksFileName = "testnet_" + m_blocksFileName;
m_blocksCacheFileName = "testnet_" + m_blocksCacheFileName;
m_blockIndexesFileName = "testnet_" + m_blockIndexesFileName;
Expand Down Expand Up @@ -156,8 +159,10 @@ bool Currency::getBlockReward(
medianSize = std::max(medianSize, blockGrantedFullRewardZone);
if (currentBlockSize > medianSize * UINT64_C(2)) {
logger(TRACE)
<< "Block cumulative size is too big: " << currentBlockSize
<< ", expected less than " << medianSize * UINT64_C(2);
<< "Block cumulative size is too big: "
<< currentBlockSize
<< ", expected less than "
<< medianSize * UINT64_C(2);
return false;
}

Expand Down Expand Up @@ -198,6 +203,52 @@ size_t Currency::maxBlockCumulativeSize(uint64_t height) const
return maxSize;
}

bool Currency::isGovernanceEnabled(uint32_t height) const
{
if (height >= m_governanceHeightStart && height <= m_governanceHeightEnd) {
return true;
}
else {
return false;
}
}

uint64_t Currency::getGovernanceReward(uint64_t base_reward) const
{
// minimum is 1% to avoid a zero amount and maximum is 50%
uint16_t percent = (m_governancePercent < 1) ? 1 : (m_governancePercent > 50) ? 50 : m_governancePercent;
return (uint64_t)(base_reward * (percent * 0.01));
}

bool Currency::getGovernanceAddressAndKey(AccountKeys& governanceKeys) const
{
std::string address = GOVERNANCE_WALLET_ADDRESS;
std::string viewSecretkey = GOVERNANCE_VIEW_SECRET_KEY;

AccountPublicAddress governanceAddress = boost::value_initialized<AccountPublicAddress>();
if (!parseAccountAddressString(address, governanceAddress)) {
logger(Logging::ERROR)
<< "Failed to parse governance wallet address ("
<< address
<< "), "
<< "Check /lib/Global/CryptoNoteConfig.h";
return false;
}

Crypto::SecretKey governanceViewSecretKey;
if (!Common::podFromHex(viewSecretkey, governanceViewSecretKey)) {
logger(Logging::ERROR)
<< "Failed to parse governance view secret key"
<< "Check /lib/Global/CryptoNoteConfig.h";
return false;
}

governanceKeys.address = governanceAddress;
governanceKeys.viewSecretKey = governanceViewSecretKey;

return true;
}

bool Currency::constructMinerTx(
uint8_t blockMajorVersion,
uint32_t height,
Expand Down Expand Up @@ -225,8 +276,12 @@ bool Currency::constructMinerTx(
BaseInput in;
in.blockIndex = height;

uint64_t governanceReward = 0;
uint64_t totalRewardWGR = 0; //totalRewardWithoutGovernanceReward

uint64_t blockReward;
int64_t emissionChange;

if (!getBlockReward(
blockMajorVersion,
medianSize,
Expand All @@ -242,6 +297,20 @@ bool Currency::constructMinerTx(
return false;
}

totalRewardWGR = blockReward;

// If Governance Fee blockReward is decreased by GOVERNANCE_PERCENT_FEE
bool enableGovernance = isGovernanceEnabled(height);

if (enableGovernance) {
governanceReward = getGovernanceReward(blockReward);

if (alreadyGeneratedCoins != 0) {
blockReward -= governanceReward;
totalRewardWGR = blockReward + governanceReward;
}
}

std::vector<uint64_t> outAmounts;
decompose_amount_into_digits(
blockReward,
Expand Down Expand Up @@ -301,7 +370,41 @@ bool Currency::constructMinerTx(
tx.outputs.push_back(out);
}

if (summaryAmounts != blockReward) {
if (enableGovernance) {
AccountKeys governanceKeys;
getGovernanceAddressAndKey(governanceKeys);

Crypto::KeyDerivation derivation = boost::value_initialized<Crypto::KeyDerivation>();
Crypto::PublicKey outEphemeralPubKey = boost::value_initialized<Crypto::PublicKey>();

bool r = Crypto::generate_key_derivation(governanceKeys.address.viewPublicKey, txkey.secretKey, derivation);
if (!(r)) {
logger(ERROR, BRIGHT_RED)
<< "while creating outs: failed to generate_key_derivation("
<< governanceKeys.address.viewPublicKey << ", " << txkey.secretKey << ")";
return false;
}
size_t pos = tx.outputs.size();
r = Crypto::derive_public_key(derivation, pos++, governanceKeys.address.spendPublicKey, outEphemeralPubKey);

if (!(r)) {
logger(ERROR, BRIGHT_RED)
<< "while creating outs: failed to derive_public_key("
<< derivation << ", " << 0 << ", "
<< governanceKeys.address.spendPublicKey << ")";
return false;
}

KeyOutput tk;
tk.key = outEphemeralPubKey;

TransactionOutput out;
summaryAmounts += out.amount = governanceReward;
out.target = tk;
tx.outputs.push_back(out);
}

if (summaryAmounts != totalRewardWGR) {
logger(ERROR, BRIGHT_RED)
<< "Failed to construct miner tx, summaryAmounts = "
<< summaryAmounts
Expand Down Expand Up @@ -971,6 +1074,10 @@ CurrencyBuilder::CurrencyBuilder(Logging::ILogger &log)
minerTxBlobReservedSize(parameters::CRYPTONOTE_COINBASE_BLOB_RESERVED_SIZE);
maxTransactionSizeLimit(parameters::MAX_TRANSACTION_SIZE_LIMIT);

governancePercent(parameters::GOVERNANCE_PERCENT_FEE);
governanceHeightStart(parameters::GOVERNANCE_HEIGHT_START);
governanceHeightEnd(parameters::GOVERNANCE_HEIGHT_END);

minMixin(parameters::MIN_TX_MIXIN_SIZE);
maxMixin(parameters::MAX_TX_MIXIN_SIZE);

Expand Down
28 changes: 28 additions & 0 deletions lib/CryptoNoteCore/Currency.h
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,10 @@ class Currency
size_t minerTxBlobReservedSize() const { return m_minerTxBlobReservedSize; }
uint64_t maxTransactionSizeLimit() const { return m_maxTransactionSizeLimit; }

uint32_t governancePercent() const { return m_governancePercent; }
uint32_t governanceHeightStart() const { return m_governanceHeightStart; }
uint32_t governanceHeightEnd() const { return m_governanceHeightEnd; }

size_t minMixin() const { return m_minMixin; }
size_t maxMixin() const { return m_maxMixin; }

Expand Down Expand Up @@ -245,6 +249,10 @@ class Currency
size_t outputCount,
size_t mixinCount) const;

bool isGovernanceEnabled(uint32_t height) const;
bool getGovernanceAddressAndKey(AccountKeys& m_account_keys) const;
uint64_t getGovernanceReward(uint64_t base_reward) const;

static const std::vector<uint64_t> PRETTY_AMOUNTS;

private:
Expand Down Expand Up @@ -285,6 +293,10 @@ class Currency

uint64_t m_minimumFee;

uint32_t m_governancePercent;
uint32_t m_governanceHeightStart;
uint32_t m_governanceHeightEnd;

size_t m_minMixin;
size_t m_maxMixin;

Expand Down Expand Up @@ -431,6 +443,22 @@ class CurrencyBuilder : boost::noncopyable
return *this;
}

CurrencyBuilder& governancePercent(uint32_t val)
{
m_currency.m_governancePercent = val;
return *this;
}
CurrencyBuilder& governanceHeightStart(uint32_t val)
{
m_currency.m_governanceHeightStart = val;
return *this;
}
CurrencyBuilder& governanceHeightEnd(uint32_t val)
{
m_currency.m_governanceHeightEnd = val;
return *this;
}

CurrencyBuilder &minMixin(size_t val) { m_currency.m_minMixin = val; return *this; }
CurrencyBuilder &maxMixin(size_t val) { m_currency.m_maxMixin = val; return *this; }

Expand Down
9 changes: 9 additions & 0 deletions lib/Global/CryptoNoteConfig.h
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,11 @@ const char P2P_NET_DATA_FILENAME[] = "p2pstate.dat";
const char CRYPTONOTE_BLOCKCHAIN_INDICES_FILENAME[] = "blockchainindices.bin";
const char MINER_CONFIG_FILE_NAME[] = "miner_conf.json";

/* Governance Fee and range // The Qwertycoin Foundation */
const uint16_t GOVERNANCE_PERCENT_FEE = 10; // 10 percent of base block reward
const uint32_t GOVERNANCE_HEIGHT_START = 800000;
const uint32_t GOVERNANCE_HEIGHT_END = 8000000;

} // namespace parameters

const char CRYPTONOTE_NAME[] = "Qwertycoin";
Expand Down Expand Up @@ -223,6 +228,10 @@ const std::string LICENSE_URL = "https://github.c
const bool P2P_MESSAGES = true;
const uint16_t P2P_MESSAGES_CHAR_COUNT = 160;

/* Governance Fee Wallets // The Qwertycoin Foundation */
const std::string GOVERNANCE_WALLET_ADDRESS = "QWC1W9dWEf955q711Qn2fsLbw8fDW1AixUqZQiMCcHhkB59CivmxLPdXXDhkL2yfJkMBKiVWg7hgT3dP5RC5r8Q15kCNADeJ7x";
const std::string GOVERNANCE_VIEW_SECRET_KEY = "d6c03cec7de78fe30895bf19edeb5c24529c3d2e40d00b300fa2336c6fdafe0d";

const char *const SEED_NODES[] = {
"node-00.qwertycoin.org:8196",
"node-01.qwertycoin.org:8196",
Expand Down
4 changes: 2 additions & 2 deletions tests/UnitTests/BlockReward.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@ using namespace CryptoNote;

namespace {

const uint64_t TEST_GRANTED_FULL_REWARD_ZONE = 10000;
const uint64_t TEST_GRANTED_FULL_REWARD_ZONE = 1000000;
const uint64_t TEST_MONEY_SUPPLY = static_cast<uint64_t>(-1);
const uint64_t TEST_EMISSION_SPEED_FACTOR = 18;
const uint64_t TEST_EMISSION_SPEED_FACTOR = 19;

class getBlockReward_and_already_generated_coins : public ::testing::Test
{
Expand Down

0 comments on commit bec2212

Please sign in to comment.