Skip to content

Commit

Permalink
Add function facilitate non-tx burns
Browse files Browse the repository at this point in the history
  • Loading branch information
Bushstar committed May 6, 2021
1 parent 9c6a9ad commit 0726fb1
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 6 deletions.
42 changes: 36 additions & 6 deletions src/validation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2113,10 +2113,21 @@ static int64_t nBlocksTotal = 0;
// Holds position for burn TXs appended to block in burn history
static std::vector<CTransactionRef>::size_type nPhantomBurnTx{0};

static std::map<CScript, CBalances> mapBurnAmounts;

static uint32_t GetNextBurnPosition() {
return nPhantomBurnTx++;
}

// Burn non-transaction amounts, that is burns that are not sent directly to the burn address
// in a account or UTXO transaction. When parsing TXs via ConnectBlock that result in a burn
// from an account in this way call the function below. This will add the burn to the map to
// be added to the burn index as a phantom TX appended to the end of the connecting block.
Res AddNonTxToBurnIndex(const CScript& from, const CBalances& amounts)
{
return mapBurnAmounts[from].AddBalances(amounts.balances);
}

/** Apply the effects of this block (with given index) on the UTXO set represented by coins.
* Validity checks that depend on the UTXO set are also done; ConnectBlock()
* can fail if those validity checks fail (among other reasons). */
Expand Down Expand Up @@ -2611,16 +2622,35 @@ bool CChainState::ConnectBlock(const CBlock& block, CValidationState& state, CBl
return true;
}, BalanceKey{chainparams.GetConsensus().retiredBurnAddress, DCT_ID{}});

cache.SubBalances(chainparams.GetConsensus().retiredBurnAddress, amounts);
cache.AddBalances(chainparams.GetConsensus().burnAddress, amounts);
AddNonTxToBurnIndex(chainparams.GetConsensus().retiredBurnAddress, amounts);
}

// Add transfer as additional TX in block
for (const auto& entry : amounts.balances) {
pburnHistoryDB->WriteAccountHistory({Params().GetConsensus().burnAddress, static_cast<uint32_t>(pindex->nHeight), GetNextBurnPosition()},
{uint256{}, static_cast<uint8_t>(CustomTxType::AccountToAccount), {{entry.first, entry.second}}});
// Add any non-Tx burns to index as phantom Txs
for (const auto& item : mapBurnAmounts)
{
for (const auto& subItem : item.second.balances)
{
// If amount cannot be deducted then burn skipped.
auto result = cache.SubBalance(item.first, {subItem.first, subItem.second});
if (result.ok)
{
cache.AddBalance(chainparams.GetConsensus().burnAddress, {subItem.first, subItem.second});

// Add transfer as additional TX in block
pburnHistoryDB->WriteAccountHistory({Params().GetConsensus().burnAddress, static_cast<uint32_t>(pindex->nHeight), GetNextBurnPosition()},
{uint256{}, static_cast<uint8_t>(CustomTxType::AccountToAccount), {{subItem.first, subItem.second}}});
}
else // Log burn failure
{
CTxDestination dest;
ExtractDestination(item.first, dest);
LogPrintf("Burn failed: %s Address: %s Token: %d Amount: %d\n", result.msg, EncodeDestination(dest), subItem.first.v, subItem.second);
}
}
}

mapBurnAmounts.clear();

// construct undo
auto& flushable = cache.GetStorage();
auto undo = CUndo::Construct(mnview.GetStorage(), flushable.GetRaw());
Expand Down
3 changes: 3 additions & 0 deletions src/validation.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
#include <vector>

class CAnchorConfirmMessage;
struct CBalances;
class CChainState;
class CDoubleSignFact;
class CCustomCSView;
Expand Down Expand Up @@ -821,4 +822,6 @@ inline CAmount CalculateCoinbaseReward(const CAmount blockReward, const uint32_t
return (blockReward * percentage) / 10000;
}

Res AddNonTxToBurnIndex(const CScript& from, const CBalances& amounts);

#endif // DEFI_VALIDATION_H

0 comments on commit 0726fb1

Please sign in to comment.