Skip to content

Commit

Permalink
Track TX and non-TX burns. Zero foundation balances.
Browse files Browse the repository at this point in the history
  • Loading branch information
Bushstar committed May 6, 2021
1 parent dd89fc9 commit 4fa84b2
Show file tree
Hide file tree
Showing 17 changed files with 894 additions and 14 deletions.
5 changes: 5 additions & 0 deletions contrib/testgen/gen_burn_addr.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ def print_usage():
print('python3 ' + sys.argv[0] + ' AddressStartString')
print('Mainnet address start with string from 8F ~ 8d')
print('Testnet address start with string from 73 ~ 7R')
print('Regtest address start with string from mf ~ n4')
print('The address start string cannot contain these characters: 0OIl')
print('For example:')
print(' python3 gen_burn_addr.py 8addressForBurn')
Expand All @@ -131,6 +132,9 @@ def check_start_range(fst2):
if fst2 >= '8F' and fst2 <= '8d':
return True

if fst2 >= 'mf' and fst2 <= 'n4':
return True

return False

if __name__ == '__main__':
Expand Down Expand Up @@ -170,6 +174,7 @@ def check_start_range(fst2):
print('Address start is not correct!')
print('Mainnet address start with string from 8F ~ 8d')
print('Testnet address start with string from 73 ~ 7R')
print('Regtest address start with string from mf ~ n4')
sys.exit(0)

anotherString = startString + "X" * (34 - len(startString))
Expand Down
21 changes: 21 additions & 0 deletions src/chainparams.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,10 @@ class CMainParams : public CChainParams {
consensus.foundationMembers.insert(GetScriptForDestination(DecodeDestination("8bL7jZe2Nk5EhqFA6yuf8HPre3M6eewkqj", *this)));
consensus.foundationMembers.insert(GetScriptForDestination(DecodeDestination("8UhqhhiwtUuEqCD7HsekUsgYRuz115eLiQ", *this)));

consensus.accountDestruction.clear();
consensus.accountDestruction.insert(GetScriptForDestination(DecodeDestination("dJEbxbfufyPF14SC93yxiquECEfq4YSd9L", *this)));
consensus.accountDestruction.insert(GetScriptForDestination(DecodeDestination("8UAhRuUFCyFUHEPD7qvtj8Zy2HxF5HH5nb", *this)));

// owner base58, operator base58
vMasternodes.push_back({"8PuErAcazqccCVzRcc8vJ3wFaZGm4vFbLe", "8J846CKFF83Jcj5m4EReJmxiaJ6Jy1Y6Ea"});
vMasternodes.push_back({"8RPZm7SVUNhGN1RgGY3R92rvRkZBwETrCX", "8bzHwhaF2MaVs4owRvpWtZQVug3mKuJji2"});
Expand All @@ -256,6 +260,9 @@ class CMainParams : public CChainParams {
assert(sum_initdist == 588000000 * COIN);
}

consensus.burnAddress = GetScriptForDestination(DecodeDestination("8defichainBurnAddressXXXXXXXdRQkSm", *this));
consensus.retiredBurnAddress = GetScriptForDestination(DecodeDestination("8defichainDSTBurnAddressXXXXaCAuTq", *this));

genesis = CreateGenesisBlock(1587883831, 0x1d00ffff, 1, initdist, CreateGenesisMasternodes()); // old=1231006505
consensus.hashGenesisBlock = genesis.GetHash();

Expand Down Expand Up @@ -440,6 +447,9 @@ class CTestNetParams : public CChainParams {
initdist.push_back(CTxOut(100000000 * COIN, GetScriptForDestination(DecodeDestination("tmYVkwmcv73Hth7hhHz15mx5K8mzC1hSef", *this))));
initdist.push_back(CTxOut(100000000 * COIN, GetScriptForDestination(DecodeDestination("tahuMwb9eX83eJhf2vXL6NPzABy3Ca8DHi", *this))));

consensus.burnAddress = GetScriptForDestination(DecodeDestination("7DefichainBurnAddressXXXXXXXdMUE5n", *this));
consensus.retiredBurnAddress = GetScriptForDestination(DecodeDestination("7DefichainDSTBurnAddressXXXXXzS4Hi", *this));

genesis = CreateGenesisBlock(1586099762, 0x1d00ffff, 1, initdist, CreateGenesisMasternodes()); // old=1296688602
consensus.hashGenesisBlock = genesis.GetHash();

Expand Down Expand Up @@ -606,6 +616,9 @@ class CDevNetParams : public CChainParams {
initdist.push_back(CTxOut(100000000 * COIN, GetScriptForDestination(DecodeDestination("75Wramp2iARchHedXcn1qRkQtMpSt9Mi3V", *this))));
initdist.push_back(CTxOut(100000000 * COIN, GetScriptForDestination(DecodeDestination("7LfqHbyh9dBQDjWB6MxcWvH2PBC5iY4wPa", *this))));

consensus.burnAddress = GetScriptForDestination(DecodeDestination("7DefichainBurnAddressXXXXXXXdMUE5n", *this));
consensus.retiredBurnAddress = GetScriptForDestination(DecodeDestination("7DefichainDSTBurnAddressXXXXXzS4Hi", *this));

genesis = CreateGenesisBlock(1585132338, 0x1d00ffff, 1, initdist, CreateGenesisMasternodes()); // old=1296688602
consensus.hashGenesisBlock = genesis.GetHash();

Expand Down Expand Up @@ -763,6 +776,10 @@ class CRegTestParams : public CChainParams {
consensus.foundationMembers.emplace(GetScriptForDestination(DecodeDestination("bcrt1qyrfrpadwgw7p5eh3e9h3jmu4kwlz4prx73cqny", *this)));
consensus.foundationMembers.emplace(GetScriptForDestination(DecodeDestination("bcrt1qyeuu9rvq8a67j86pzvh5897afdmdjpyankp4mu", *this)));

consensus.accountDestruction.clear();
consensus.accountDestruction.insert(GetScriptForDestination(DecodeDestination("2MxJf6Ak8MGrLoGdekrU6AusW29szZUFphH", *this)));
consensus.accountDestruction.insert(GetScriptForDestination(DecodeDestination("mxiaFfAnCoXEUy4RW8NgsQM7yU5YRCiFSh", *this)));

// owner base58, operator base58
vMasternodes.push_back({"mwsZw8nF7pKxWH8eoKL9tPxTpaFkz7QeLU", "mswsMVsyGMj1FzDMbbxw2QW3KvQAv2FKiy"});
vMasternodes.push_back({"msER9bmJjyEemRpQoS8YYVL21VyZZrSgQ7", "mps7BdmwEF2vQ9DREDyNPibqsuSRZ8LuwQ"});
Expand All @@ -773,6 +790,10 @@ class CRegTestParams : public CChainParams {
vMasternodes.push_back({"bcrt1qyrfrpadwgw7p5eh3e9h3jmu4kwlz4prx73cqny", "bcrt1qmfvw3dp3u6fdvqkdc0y3lr0e596le9cf22vtsv"});
vMasternodes.push_back({"bcrt1qyeuu9rvq8a67j86pzvh5897afdmdjpyankp4mu", "bcrt1qurwyhta75n2g75u2u5nds9p6w9v62y8wr40d2r"});

// For testing send after Eunos: 93ViFmLeJVgKSPxWGQHmSdT5RbeGDtGW4bsiwQM2qnQyucChMqQ
consensus.burnAddress = GetScriptForDestination(DecodeDestination("mfburnZSAM7Gs1hpDeNaMotJXSGA7edosG", *this));
consensus.retiredBurnAddress = GetScriptForDestination(DecodeDestination("mfdefichainDSTBurnAddressXXXZcE1vs", *this));

genesis = CreateGenesisBlock(1579045065, 0x207fffff, 1, {
CTxOut(consensus.baseBlockSubsidy,
GetScriptForDestination(DecodeDestination("mud4VMfbBqXNpbt8ur33KHKx8pk3npSq8c", *this)) // 6th masternode owner. for initdist tests
Expand Down
5 changes: 5 additions & 0 deletions src/consensus/params.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ struct Params {
CScript foundationShareScript;
uint32_t foundationShare;
std::set<CScript> foundationMembers;
std::set<CScript> accountDestruction;
/* Block hash that is excepted from BIP16 enforcement */
uint256 BIP16Exception;
/** Block height and hash at which BIP34 becomes active */
Expand Down Expand Up @@ -89,6 +90,10 @@ struct Params {
int EunosHeight;
/** Foundation share after AMK, normalized to COIN = 100% */
CAmount foundationShareDFIP1;
/** Trackable burn address */
CScript burnAddress;
/** Previous burn address to transfer tokens from */
CScript retiredBurnAddress;

/** Struct to hold percentages for coinbase distribution.
* Percentages are calculated out of 10000 */
Expand Down
3 changes: 3 additions & 0 deletions src/init.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1609,6 +1609,9 @@ bool AppInitMain(InitInterfaces& interfaces)
paccountHistoryDB = MakeUnique<CAccountHistoryStorage>(GetDataDir() / "history", nCustomCacheSize, false, fReset || fReindexChainState);
}

pburnHistoryDB.reset();
pburnHistoryDB = MakeUnique<CBurnHistoryStorage>(GetDataDir() / "burn", nCustomCacheSize, false, fReset || fReindexChainState);

// If necessary, upgrade from older database format.
// This is a no-op if we cleared the coinsviewdb with -reindex or -reindex-chainstate
if (!::ChainstateActive().CoinsDB().Upgrade()) {
Expand Down
44 changes: 40 additions & 4 deletions src/masternodes/accountshistory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,13 @@ CAccountHistoryStorage::CAccountHistoryStorage(const fs::path& dbName, std::size
{
}

CAccountsHistoryWriter::CAccountsHistoryWriter(CCustomCSView & storage, uint32_t height, uint32_t txn, const uint256& txid, uint8_t type, CAccountsHistoryView* historyView)
: CStorageView(new CFlushableStorageKV(static_cast<CStorageKV&>(storage.GetStorage()))), height(height), txn(txn), txid(txid), type(type), historyView(historyView)
CBurnHistoryStorage::CBurnHistoryStorage(const fs::path& dbName, std::size_t cacheSize, bool fMemory, bool fWipe)
: CStorageView(new CStorageLevelDB(dbName, cacheSize, fMemory, fWipe))
{
}

CAccountsHistoryWriter::CAccountsHistoryWriter(CCustomCSView & storage, uint32_t height, uint32_t txn, const uint256& txid, uint8_t type, CAccountsHistoryView* historyView, CAccountsHistoryView* burnView)
: CStorageView(new CFlushableStorageKV(static_cast<CStorageKV&>(storage.GetStorage()))), height(height), txn(txn), txid(txid), type(type), historyView(historyView), burnView(burnView)
{
}

Expand All @@ -43,15 +48,29 @@ Res CAccountsHistoryWriter::AddBalance(CScript const & owner, CTokenAmount amoun
if (historyView && res.ok && amount.nValue != 0) {
diffs[owner][amount.nTokenId] += amount.nValue;
}
if (burnView && res.ok && amount.nValue != 0 && owner == Params().GetConsensus().burnAddress) {
burnDiffs[owner][amount.nTokenId] += amount.nValue;
}
return res;
}

Res CAccountsHistoryWriter::AddFeeBurn(CScript const & owner, CAmount amount)
{
if (burnView && amount != 0) {
burnDiffs[owner][DCT_ID{0}] += amount;
}
return Res::Ok();
}

Res CAccountsHistoryWriter::SubBalance(CScript const & owner, CTokenAmount amount)
{
auto res = CCustomCSView::SubBalance(owner, amount);
if (historyView && res.ok && amount.nValue != 0) {
diffs[owner][amount.nTokenId] -= amount.nValue;
}
if (burnView && res.ok && amount.nValue != 0 && owner == Params().GetConsensus().burnAddress) {
burnDiffs[owner][amount.nTokenId] -= amount.nValue;
}
return res;
}

Expand All @@ -62,11 +81,16 @@ bool CAccountsHistoryWriter::Flush()
historyView->WriteAccountHistory({diff.first, height, txn}, {txid, type, diff.second});
}
}
if (burnView) {
for (const auto& diff : burnDiffs) {
burnView->WriteAccountHistory({diff.first, height, txn}, {txid, type, diff.second});
}
}
return CCustomCSView::Flush();
}

CAccountsHistoryEraser::CAccountsHistoryEraser(CCustomCSView & storage, uint32_t height, uint32_t txn, CAccountsHistoryView* historyView)
: CStorageView(new CFlushableStorageKV(static_cast<CStorageKV&>(storage.GetStorage()))), height(height), txn(txn), historyView(historyView)
CAccountsHistoryEraser::CAccountsHistoryEraser(CCustomCSView & storage, uint32_t height, uint32_t txn, CAccountsHistoryView* historyView, CAccountsHistoryView* burnView)
: CStorageView(new CFlushableStorageKV(static_cast<CStorageKV&>(storage.GetStorage()))), height(height), txn(txn), historyView(historyView), burnView(burnView)
{
}

Expand All @@ -75,6 +99,9 @@ Res CAccountsHistoryEraser::AddBalance(CScript const & owner, CTokenAmount)
if (historyView) {
accounts.insert(owner);
}
if (burnView && owner == Params().GetConsensus().burnAddress) {
burnAccounts.insert(owner);
}
return Res::Ok();
}

Expand All @@ -83,6 +110,9 @@ Res CAccountsHistoryEraser::SubBalance(CScript const & owner, CTokenAmount)
if (historyView) {
accounts.insert(owner);
}
if (burnView && owner == Params().GetConsensus().burnAddress) {
burnAccounts.insert(owner);
}
return Res::Ok();
}

Expand All @@ -93,7 +123,13 @@ bool CAccountsHistoryEraser::Flush()
historyView->EraseAccountHistory({account, height, txn});
}
}
if (burnView) {
for (const auto& account : burnAccounts) {
burnView->EraseAccountHistory({account, height, txn});
}
}
return CCustomCSView::Flush();
}

std::unique_ptr<CAccountHistoryStorage> paccountHistoryDB;
std::unique_ptr<CBurnHistoryStorage> pburnHistoryDB;
16 changes: 14 additions & 2 deletions src/masternodes/accountshistory.h
Original file line number Diff line number Diff line change
Expand Up @@ -69,19 +69,28 @@ class CAccountHistoryStorage : public CAccountsHistoryView
CAccountHistoryStorage(const fs::path& dbName, std::size_t cacheSize, bool fMemory = false, bool fWipe = false);
};

class CBurnHistoryStorage : public CAccountsHistoryView
{
public:
CBurnHistoryStorage(const fs::path& dbName, std::size_t cacheSize, bool fMemory = false, bool fWipe = false);
};

class CAccountsHistoryWriter : public CCustomCSView
{
const uint32_t height;
const uint32_t txn;
const uint256 txid;
const uint8_t type;
std::map<CScript, TAmounts> diffs;
std::map<CScript, TAmounts> burnDiffs;
CAccountsHistoryView* historyView;
CAccountsHistoryView* burnView;

public:
CAccountsHistoryWriter(CCustomCSView & storage, uint32_t height, uint32_t txn, const uint256& txid, uint8_t type, CAccountsHistoryView* historyView);
CAccountsHistoryWriter(CCustomCSView & storage, uint32_t height, uint32_t txn, const uint256& txid, uint8_t type, CAccountsHistoryView* historyView, CAccountsHistoryView* burnView);
Res AddBalance(CScript const & owner, CTokenAmount amount) override;
Res SubBalance(CScript const & owner, CTokenAmount amount) override;
Res AddFeeBurn(CScript const & owner, CAmount amount);
bool Flush();
};

Expand All @@ -90,16 +99,19 @@ class CAccountsHistoryEraser : public CCustomCSView
const uint32_t height;
const uint32_t txn;
std::set<CScript> accounts;
std::set<CScript> burnAccounts;
CAccountsHistoryView* historyView;
CAccountsHistoryView* burnView;

public:
CAccountsHistoryEraser(CCustomCSView & storage, uint32_t height, uint32_t txn, CAccountsHistoryView* historyView);
CAccountsHistoryEraser(CCustomCSView & storage, uint32_t height, uint32_t txn, CAccountsHistoryView* historyView, CAccountsHistoryView* burnView);
Res AddBalance(CScript const & owner, CTokenAmount amount) override;
Res SubBalance(CScript const & owner, CTokenAmount amount) override;
bool Flush();
};

extern std::unique_ptr<CAccountHistoryStorage> paccountHistoryDB;
extern std::unique_ptr<CBurnHistoryStorage> pburnHistoryDB;

static constexpr bool DEFAULT_ACINDEX = true;

Expand Down
13 changes: 9 additions & 4 deletions src/masternodes/mn_checks.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1181,7 +1181,7 @@ bool ShouldReturnNonFatalError(const CTransaction& tx, uint32_t height) {
return it != skippedTx.end() && it->second == tx.GetHash();
}

Res RevertCustomTx(CCustomCSView& mnview, const CCoinsViewCache& coins, const CTransaction& tx, const Consensus::Params& consensus, uint32_t height, uint32_t txn, CAccountsHistoryView* historyView) {
Res RevertCustomTx(CCustomCSView& mnview, const CCoinsViewCache& coins, const CTransaction& tx, const Consensus::Params& consensus, uint32_t height, uint32_t txn, CAccountsHistoryView* historyView, CAccountsHistoryView *burnView) {
if (tx.IsCoinBase() && height > 0) { // genesis contains custom coinbase txs
return Res::Ok();
}
Expand All @@ -1201,7 +1201,7 @@ Res RevertCustomTx(CCustomCSView& mnview, const CCoinsViewCache& coins, const CT
break;
}
auto txMessage = customTypeToMessage(txType);
CAccountsHistoryEraser view(mnview, height, txn, historyView);
CAccountsHistoryEraser view(mnview, height, txn, historyView, burnView);
if ((res = CustomMetadataParse(height, consensus, metadata, txMessage))) {
res = CustomTxRevert(view, coins, tx, height, consensus, txMessage);
}
Expand All @@ -1212,7 +1212,7 @@ Res RevertCustomTx(CCustomCSView& mnview, const CCoinsViewCache& coins, const CT
return (view.Flush(), res);
}

Res ApplyCustomTx(CCustomCSView& mnview, const CCoinsViewCache& coins, const CTransaction& tx, const Consensus::Params& consensus, uint32_t height, uint64_t time, uint32_t txn, CAccountsHistoryView* historyView) {
Res ApplyCustomTx(CCustomCSView& mnview, const CCoinsViewCache& coins, const CTransaction& tx, const Consensus::Params& consensus, uint32_t height, uint64_t time, uint32_t txn, CAccountsHistoryView* historyView, CAccountsHistoryView* burnView) {
auto res = Res::Ok();
if (tx.IsCoinBase() && height > 0) { // genesis contains custom coinbase txs
return res;
Expand All @@ -1223,9 +1223,14 @@ Res ApplyCustomTx(CCustomCSView& mnview, const CCoinsViewCache& coins, const CTr
return res;
}
auto txMessage = customTypeToMessage(txType);
CAccountsHistoryWriter view(mnview, height, txn, tx.GetHash(), uint8_t(txType), historyView);
CAccountsHistoryWriter view(mnview, height, txn, tx.GetHash(), uint8_t(txType), historyView, burnView);
if ((res = CustomMetadataParse(height, consensus, metadata, txMessage))) {
res = CustomTxVisit(view, coins, tx, height, consensus, txMessage, time);

// Track burn fee
if (txType == CustomTxType::CreateToken || txType == CustomTxType::CreateMasternode) {
view.AddFeeBurn(tx.vout[0].scriptPubKey, tx.vout[0].nValue);
}
}
// list of transactions which aren't allowed to fail:
if (!res) {
Expand Down
4 changes: 2 additions & 2 deletions src/masternodes/mn_checks.h
Original file line number Diff line number Diff line change
Expand Up @@ -245,8 +245,8 @@ CCustomTxMessage customTypeToMessage(CustomTxType txType);
bool IsMempooledCustomTxCreate(const CTxMemPool& pool, const uint256& txid);
Res RpcInfo(const CTransaction& tx, uint32_t height, CustomTxType& type, UniValue& results);
Res CustomMetadataParse(uint32_t height, const Consensus::Params& consensus, const std::vector<unsigned char>& metadata, CCustomTxMessage& txMessage);
Res ApplyCustomTx(CCustomCSView& mnview, const CCoinsViewCache& coins, const CTransaction& tx, const Consensus::Params& consensus, uint32_t height, uint64_t time = 0, uint32_t txn = 0, CAccountsHistoryView* historyView = nullptr);
Res RevertCustomTx(CCustomCSView& mnview, const CCoinsViewCache& coins, const CTransaction& tx, const Consensus::Params& consensus, uint32_t height, uint32_t txn = 0, CAccountsHistoryView* historyView = nullptr);
Res ApplyCustomTx(CCustomCSView& mnview, const CCoinsViewCache& coins, const CTransaction& tx, const Consensus::Params& consensus, uint32_t height, uint64_t time = 0, uint32_t txn = 0, CAccountsHistoryView* historyView = nullptr, CAccountsHistoryView *burnView = nullptr);
Res RevertCustomTx(CCustomCSView& mnview, const CCoinsViewCache& coins, const CTransaction& tx, const Consensus::Params& consensus, uint32_t height, uint32_t txn = 0, CAccountsHistoryView* historyView = nullptr, CAccountsHistoryView *burnView = nullptr);
Res CustomTxVisit(CCustomCSView& mnview, const CCoinsViewCache& coins, const CTransaction& tx, uint32_t height, const Consensus::Params& consensus, const CCustomTxMessage& txMessage, uint64_t time = 0);
ResVal<uint256> ApplyAnchorRewardTx(CCustomCSView& mnview, const CTransaction& tx, int height, const uint256& prevStakeModifier, const std::vector<unsigned char>& metadata, const Consensus::Params& consensusParams);
ResVal<uint256> ApplyAnchorRewardTxPlus(CCustomCSView& mnview, const CTransaction& tx, int height, const std::vector<unsigned char>& metadata, const Consensus::Params& consensusParams);
Expand Down
Loading

0 comments on commit 4fa84b2

Please sign in to comment.