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

rpc: Add "getburnreport" RPC function #2049

Merged
merged 2 commits into from
Mar 18, 2021
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
63 changes: 63 additions & 0 deletions src/rpc/blockchain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2108,3 +2108,66 @@ UniValue rpc_reorganize(const UniValue& params, bool fHelp)
results.pushKV("RollbackChain",fResult);
return results;
}

UniValue getburnreport(const UniValue& params, bool fHelp)
{
if (fHelp || params.size() != 0)
throw runtime_error(
"getburnreport\n"
"Scan for and aggregate network-wide amounts for provably-destroyed outputs.\n");

CBlock block;
CAmount total_amount = 0;
CAmount voluntary_amount = 0;
std::map<GRC::ContractType, CAmount> contract_amounts;

LOCK(cs_main);

// For now, we only consider transaction outputs with scripts that a node
// will immediately refuse to evaluate:
//
// - The script begins with the OP_RETURN op code
// - The script exceeds the maximum size
//
// We can try additional heuristics in the future, but many of these will
// be very difficult or expensive to recognize.
//
for (const CBlockIndex* pindex = pindexGenesisBlock; pindex; pindex = pindex->pnext) {
if (!block.ReadFromDisk(pindex)) {
continue;
}

for (size_t i = pindex->IsProofOfStake() ? 2 : 1; i < block.vtx.size(); ++i) {
const CTransaction& tx = block.vtx[i];

for (const auto& output : tx.vout) {
if (output.scriptPubKey.IsUnspendable()) {
total_amount += output.nValue;

if (!tx.GetContracts().empty()) {
contract_amounts[tx.vContracts[0].m_type.Value()] += output.nValue;
} else {
voluntary_amount += output.nValue;
}
}
}
}
}

UniValue json(UniValue::VOBJ);

json.pushKV("total", ValueFromAmount(total_amount));
json.pushKV("voluntary", ValueFromAmount(voluntary_amount));

UniValue contracts(UniValue::VOBJ);

for (const auto& amount_pair : contract_amounts) {
contracts.pushKV(
GRC::Contract::Type(amount_pair.first).ToString(),
ValueFromAmount(amount_pair.second));
}

json.pushKV("contracts", contracts);

return json;
}
1 change: 1 addition & 0 deletions src/rpc/server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -417,6 +417,7 @@ static const CRPCCommand vRPCCommands[] =
{ "getblockbynumber", &getblockbynumber, cat_network },
{ "getblockcount", &getblockcount, cat_network },
{ "getblockhash", &getblockhash, cat_network },
{ "getburnreport", &getburnreport, cat_network },
{ "getcheckpoint", &getcheckpoint, cat_network },
{ "getconnectioncount", &getconnectioncount, cat_network },
{ "getdifficulty", &getdifficulty, cat_network },
Expand Down
1 change: 1 addition & 0 deletions src/rpc/server.h
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,7 @@ extern UniValue getblockbynumber(const UniValue& params, bool fHelp);
extern UniValue getblockchaininfo(const UniValue& params, bool fHelp);
extern UniValue getblockcount(const UniValue& params, bool fHelp);
extern UniValue getblockhash(const UniValue& params, bool fHelp);
extern UniValue getburnreport(const UniValue& params, bool fHelp);
extern UniValue getcheckpoint(const UniValue& params, bool fHelp);
extern UniValue getconnectioncount(const UniValue& params, bool fHelp);
extern UniValue getdifficulty(const UniValue& params, bool fHelp);
Expand Down
2 changes: 1 addition & 1 deletion src/script.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -332,7 +332,7 @@ bool EvalScript(vector<vector<unsigned char> >& stack, const CScript& script, co
valtype vchPushValue;
vector<bool> vfExec;
vector<valtype> altstack;
if (script.size() > 10000)
if (script.size() > MAX_SCRIPT_SIZE)
return false;
int nOpCount = 0;

Expand Down
13 changes: 13 additions & 0 deletions src/script.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ class CTransaction;
static const unsigned int MAX_SCRIPT_ELEMENT_SIZE = 520; // bytes
static const unsigned int MAX_OP_RETURN_RELAY = 80; // bytes

// Maximum script length in bytes
static const int MAX_SCRIPT_SIZE = 10000;

/** Signature hash types/flags */
enum
{
Expand Down Expand Up @@ -581,6 +584,16 @@ class CScript : public CScriptBase
{
return CScriptID(Hash160(*this));
}

/**
* Returns whether the script is guaranteed to fail at execution,
* regardless of the initial stack. This allows outputs to be pruned
* instantly when entering the UTXO set.
*/
bool IsUnspendable() const
{
return (size() > 0 && *begin() == OP_RETURN) || (size() > MAX_SCRIPT_SIZE);
}
};

/**
Expand Down
6 changes: 3 additions & 3 deletions src/wallet/wallet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -847,7 +847,7 @@ void CWalletTx::GetAmounts(list<COutputEntry>& listReceived, list<COutputEntry>&
{
if (!ExtractDestination(txout.scriptPubKey, address))
{
if (txout.scriptPubKey[0] != OP_RETURN)
if (!txout.scriptPubKey.IsUnspendable())
{
LogPrintf("CWalletTx::GetAmounts: Unknown transaction type found, txid %s",
this->GetHash().ToString().c_str());
Expand Down Expand Up @@ -891,7 +891,7 @@ void CWalletTx::GetAmounts(list<COutputEntry>& listReceived, list<COutputEntry>&
// If this is my output AND the transaction is not from me, then record the output as received.
if (fIsMine != ISMINE_NO && !fIsFromMe)
{
if (!ExtractDestination(txout.scriptPubKey, address) && txout.scriptPubKey[0] != OP_RETURN)
if (!ExtractDestination(txout.scriptPubKey, address) && !txout.scriptPubKey.IsUnspendable())
{
LogPrintf("CWalletTx::GetAmounts: Unknown transaction type found, txid %s",
this->GetHash().ToString().c_str());
Expand All @@ -908,7 +908,7 @@ void CWalletTx::GetAmounts(list<COutputEntry>& listReceived, list<COutputEntry>&
{
if (!ExtractDestination(txout.scriptPubKey, address))
{
if (txout.scriptPubKey[0] != OP_RETURN)
if (!txout.scriptPubKey.IsUnspendable())
{
LogPrintf("CWalletTx::GetAmounts: Unknown transaction type found, txid %s",
this->GetHash().ToString().c_str());
Expand Down