1 // Copyright (c) 2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2020 The Bitcoin Core developers
3 // Distributed under the MIT software license, see the accompanying
4 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
5 
6 #include <node/coinstats.h>
7 
8 #include <coins.h>
9 #include <hash.h>
10 #include <serialize.h>
11 #include <uint256.h>
12 #include <util/system.h>
13 #include <validation.h>
14 
15 #include <map>
16 
GetBogoSize(const CScript & scriptPubKey)17 static uint64_t GetBogoSize(const CScript& scriptPubKey)
18 {
19     return 32 /* txid */ +
20            4 /* vout index */ +
21            4 /* height + coinbase */ +
22            8 /* amount */ +
23            2 /* scriptPubKey len */ +
24            scriptPubKey.size() /* scriptPubKey */;
25 }
26 
27 namespace
28 {
29 
30 /**
31  * Applies the output amount to the stats, either as "locked in name"
32  * or actual currency output.
33  */
34 void
ApplyOutputToStats(CCoinsStats & stats,const Coin & coin)35 ApplyOutputToStats (CCoinsStats& stats, const Coin& coin)
36 {
37   const CNameScript nameOp(coin.out.scriptPubKey);
38   if (nameOp.isNameOp ())
39     stats.nNameAmount += coin.out.nValue;
40   else
41     stats.nCoinAmount += coin.out.nValue;
42 }
43 
44 } // anonymous namespace
45 
ApplyStats(CCoinsStats & stats,CHashWriter & ss,const uint256 & hash,const std::map<uint32_t,Coin> & outputs)46 static void ApplyStats(CCoinsStats& stats, CHashWriter& ss, const uint256& hash, const std::map<uint32_t, Coin>& outputs)
47 {
48     assert(!outputs.empty());
49     ss << hash;
50     ss << VARINT(outputs.begin()->second.nHeight * 2 + outputs.begin()->second.fCoinBase ? 1u : 0u);
51     stats.nTransactions++;
52     for (const auto& output : outputs) {
53         ss << VARINT(output.first + 1);
54         ss << output.second.out.scriptPubKey;
55         ss << VARINT_MODE(output.second.out.nValue, VarIntMode::NONNEGATIVE_SIGNED);
56         stats.nTransactionOutputs++;
57 
58         ApplyOutputToStats (stats, output.second);
59         stats.nBogoSize += GetBogoSize(output.second.out.scriptPubKey);
60     }
61     ss << VARINT(0u);
62 }
63 
ApplyStats(CCoinsStats & stats,std::nullptr_t,const uint256 & hash,const std::map<uint32_t,Coin> & outputs)64 static void ApplyStats(CCoinsStats& stats, std::nullptr_t, const uint256& hash, const std::map<uint32_t, Coin>& outputs)
65 {
66     assert(!outputs.empty());
67     stats.nTransactions++;
68     for (const auto& output : outputs) {
69         stats.nTransactionOutputs++;
70         ApplyOutputToStats (stats, output.second);
71         stats.nBogoSize += GetBogoSize(output.second.out.scriptPubKey);
72     }
73 }
74 
75 //! Calculate statistics about the unspent transaction output set
76 template <typename T>
GetUTXOStats(CCoinsView * view,CCoinsStats & stats,T hash_obj,const std::function<void ()> & interruption_point)77 static bool GetUTXOStats(CCoinsView* view, CCoinsStats& stats, T hash_obj, const std::function<void()>& interruption_point)
78 {
79     stats = CCoinsStats();
80     std::unique_ptr<CCoinsViewCursor> pcursor(view->Cursor());
81     assert(pcursor);
82 
83     stats.hashBlock = pcursor->GetBestBlock();
84     {
85         LOCK(cs_main);
86         stats.nHeight = LookupBlockIndex(stats.hashBlock)->nHeight;
87     }
88 
89     PrepareHash(hash_obj, stats);
90 
91     uint256 prevkey;
92     std::map<uint32_t, Coin> outputs;
93     while (pcursor->Valid()) {
94         interruption_point();
95         COutPoint key;
96         Coin coin;
97         if (pcursor->GetKey(key) && pcursor->GetValue(coin)) {
98             if (!outputs.empty() && key.hash != prevkey) {
99                 ApplyStats(stats, hash_obj, prevkey, outputs);
100                 outputs.clear();
101             }
102             prevkey = key.hash;
103             outputs[key.n] = std::move(coin);
104             stats.coins_count++;
105         } else {
106             return error("%s: unable to read value", __func__);
107         }
108         pcursor->Next();
109     }
110     if (!outputs.empty()) {
111         ApplyStats(stats, hash_obj, prevkey, outputs);
112     }
113 
114     FinalizeHash(hash_obj, stats);
115 
116     stats.nDiskSize = view->EstimateSize();
117     return true;
118 }
119 
GetUTXOStats(CCoinsView * view,CCoinsStats & stats,CoinStatsHashType hash_type,const std::function<void ()> & interruption_point)120 bool GetUTXOStats(CCoinsView* view, CCoinsStats& stats, CoinStatsHashType hash_type, const std::function<void()>& interruption_point)
121 {
122     switch (hash_type) {
123     case(CoinStatsHashType::HASH_SERIALIZED): {
124         CHashWriter ss(SER_GETHASH, PROTOCOL_VERSION);
125         return GetUTXOStats(view, stats, ss, interruption_point);
126     }
127     case(CoinStatsHashType::NONE): {
128         return GetUTXOStats(view, stats, nullptr, interruption_point);
129     }
130     } // no default case, so the compiler can warn about missing cases
131     assert(false);
132 }
133 
134 // The legacy hash serializes the hashBlock
PrepareHash(CHashWriter & ss,CCoinsStats & stats)135 static void PrepareHash(CHashWriter& ss, CCoinsStats& stats)
136 {
137     ss << stats.hashBlock;
138 }
PrepareHash(std::nullptr_t,CCoinsStats & stats)139 static void PrepareHash(std::nullptr_t, CCoinsStats& stats) {}
140 
FinalizeHash(CHashWriter & ss,CCoinsStats & stats)141 static void FinalizeHash(CHashWriter& ss, CCoinsStats& stats)
142 {
143     stats.hashSerialized = ss.GetHash();
144 }
FinalizeHash(std::nullptr_t,CCoinsStats & stats)145 static void FinalizeHash(std::nullptr_t, CCoinsStats& stats) {}
146