dash-docs/en/doxygen/html/merkle_8cpp_source.html

126 lines
42 KiB
HTML

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=9"/>
<meta name="generator" content="Doxygen 1.8.14"/>
<meta name="viewport" content="width=device-width, initial-scale=1"/>
<title>Dash Core: src/consensus/merkle.cpp Source File</title>
<link href="tabs.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="dynsections.js"></script>
<link href="navtree.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="resize.js"></script>
<script type="text/javascript" src="navtreedata.js"></script>
<script type="text/javascript" src="navtree.js"></script>
<script type="text/javascript">
/* @license magnet:?xt=urn:btih:cf05388f2679ee054f2beb29a391d25f4e673ac3&amp;dn=gpl-2.0.txt GPL-v2 */
$(document).ready(initResizable);
/* @license-end */</script>
<link href="search/search.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="search/searchdata.js"></script>
<script type="text/javascript" src="search/search.js"></script>
<link href="doxygen.css" rel="stylesheet" type="text/css" />
</head>
<body>
<div id="top"><!-- do not remove this div, it is closed by doxygen! -->
<div id="titlearea">
<table cellspacing="0" cellpadding="0">
<tbody>
<tr style="height: 56px;">
<td id="projectlogo"><img alt="Logo" src="bitcoin_logo_doxygen.png"/></td>
<td id="projectalign" style="padding-left: 0.5em;">
<div id="projectname">Dash Core
&#160;<span id="projectnumber">0.12.2.1</span>
</div>
<div id="projectbrief">P2P Digital Currency</div>
</td>
</tr>
</tbody>
</table>
</div>
<!-- end header part -->
<!-- Generated by Doxygen 1.8.14 -->
<script type="text/javascript">
/* @license magnet:?xt=urn:btih:cf05388f2679ee054f2beb29a391d25f4e673ac3&amp;dn=gpl-2.0.txt GPL-v2 */
var searchBox = new SearchBox("searchBox", "search",false,'Search');
/* @license-end */
</script>
<script type="text/javascript" src="menudata.js"></script>
<script type="text/javascript" src="menu.js"></script>
<script type="text/javascript">
/* @license magnet:?xt=urn:btih:cf05388f2679ee054f2beb29a391d25f4e673ac3&amp;dn=gpl-2.0.txt GPL-v2 */
$(function() {
initMenu('',true,false,'search.php','Search');
$(document).ready(function() { init_search(); });
});
/* @license-end */</script>
<div id="main-nav"></div>
</div><!-- top -->
<div id="side-nav" class="ui-resizable side-nav-resizable">
<div id="nav-tree">
<div id="nav-tree-contents">
<div id="nav-sync" class="sync"></div>
</div>
</div>
<div id="splitbar" style="-moz-user-select:none;"
class="ui-resizable-handle">
</div>
</div>
<script type="text/javascript">
/* @license magnet:?xt=urn:btih:cf05388f2679ee054f2beb29a391d25f4e673ac3&amp;dn=gpl-2.0.txt GPL-v2 */
$(document).ready(function(){initNavTree('merkle_8cpp_source.html','');});
/* @license-end */
</script>
<div id="doc-content">
<!-- window showing the filter options -->
<div id="MSearchSelectWindow"
onmouseover="return searchBox.OnSearchSelectShow()"
onmouseout="return searchBox.OnSearchSelectHide()"
onkeydown="return searchBox.OnSearchSelectKey(event)">
</div>
<!-- iframe showing the search results (closed by default) -->
<div id="MSearchResultsWindow">
<iframe src="javascript:void(0)" frameborder="0"
name="MSearchResults" id="MSearchResults">
</iframe>
</div>
<div class="header">
<div class="headertitle">
<div class="title">merkle.cpp</div> </div>
</div><!--header-->
<div class="contents">
<a href="merkle_8cpp.html">Go to the documentation of this file.</a><div class="fragment"><div class="line"><a name="l00001"></a><span class="lineno"> 1</span>&#160;<span class="preprocessor">#include &quot;<a class="code" href="merkle_8h.html">merkle.h</a>&quot;</span></div><div class="line"><a name="l00002"></a><span class="lineno"> 2</span>&#160;<span class="preprocessor">#include &quot;hash.h&quot;</span></div><div class="line"><a name="l00003"></a><span class="lineno"> 3</span>&#160;<span class="preprocessor">#include &quot;<a class="code" href="utilstrencodings_8h.html">utilstrencodings.h</a>&quot;</span></div><div class="line"><a name="l00004"></a><span class="lineno"> 4</span>&#160;</div><div class="line"><a name="l00005"></a><span class="lineno"> 5</span>&#160;<span class="comment">/* WARNING! If you&#39;re reading this because you&#39;re learning about crypto</span></div><div class="line"><a name="l00006"></a><span class="lineno"> 6</span>&#160;<span class="comment"> and/or designing a new system that will use merkle trees, keep in mind</span></div><div class="line"><a name="l00007"></a><span class="lineno"> 7</span>&#160;<span class="comment"> that the following merkle tree algorithm has a serious flaw related to</span></div><div class="line"><a name="l00008"></a><span class="lineno"> 8</span>&#160;<span class="comment"> duplicate txids, resulting in a vulnerability (CVE-2012-2459).</span></div><div class="line"><a name="l00009"></a><span class="lineno"> 9</span>&#160;<span class="comment"></span></div><div class="line"><a name="l00010"></a><span class="lineno"> 10</span>&#160;<span class="comment"> The reason is that if the number of hashes in the list at a given time</span></div><div class="line"><a name="l00011"></a><span class="lineno"> 11</span>&#160;<span class="comment"> is odd, the last one is duplicated before computing the next level (which</span></div><div class="line"><a name="l00012"></a><span class="lineno"> 12</span>&#160;<span class="comment"> is unusual in Merkle trees). This results in certain sequences of</span></div><div class="line"><a name="l00013"></a><span class="lineno"> 13</span>&#160;<span class="comment"> transactions leading to the same merkle root. For example, these two</span></div><div class="line"><a name="l00014"></a><span class="lineno"> 14</span>&#160;<span class="comment"> trees:</span></div><div class="line"><a name="l00015"></a><span class="lineno"> 15</span>&#160;<span class="comment"></span></div><div class="line"><a name="l00016"></a><span class="lineno"> 16</span>&#160;<span class="comment"> A A</span></div><div class="line"><a name="l00017"></a><span class="lineno"> 17</span>&#160;<span class="comment"> / \ / \</span></div><div class="line"><a name="l00018"></a><span class="lineno"> 18</span>&#160;<span class="comment"> B C B C</span></div><div class="line"><a name="l00019"></a><span class="lineno"> 19</span>&#160;<span class="comment"> / \ | / \ / \</span></div><div class="line"><a name="l00020"></a><span class="lineno"> 20</span>&#160;<span class="comment"> D E F D E F F</span></div><div class="line"><a name="l00021"></a><span class="lineno"> 21</span>&#160;<span class="comment"> / \ / \ / \ / \ / \ / \ / \</span></div><div class="line"><a name="l00022"></a><span class="lineno"> 22</span>&#160;<span class="comment"> 1 2 3 4 5 6 1 2 3 4 5 6 5 6</span></div><div class="line"><a name="l00023"></a><span class="lineno"> 23</span>&#160;<span class="comment"></span></div><div class="line"><a name="l00024"></a><span class="lineno"> 24</span>&#160;<span class="comment"> for transaction lists [1,2,3,4,5,6] and [1,2,3,4,5,6,5,6] (where 5 and</span></div><div class="line"><a name="l00025"></a><span class="lineno"> 25</span>&#160;<span class="comment"> 6 are repeated) result in the same root hash A (because the hash of both</span></div><div class="line"><a name="l00026"></a><span class="lineno"> 26</span>&#160;<span class="comment"> of (F) and (F,F) is C).</span></div><div class="line"><a name="l00027"></a><span class="lineno"> 27</span>&#160;<span class="comment"></span></div><div class="line"><a name="l00028"></a><span class="lineno"> 28</span>&#160;<span class="comment"> The vulnerability results from being able to send a block with such a</span></div><div class="line"><a name="l00029"></a><span class="lineno"> 29</span>&#160;<span class="comment"> transaction list, with the same merkle root, and the same block hash as</span></div><div class="line"><a name="l00030"></a><span class="lineno"> 30</span>&#160;<span class="comment"> the original without duplication, resulting in failed validation. If the</span></div><div class="line"><a name="l00031"></a><span class="lineno"> 31</span>&#160;<span class="comment"> receiving node proceeds to mark that block as permanently invalid</span></div><div class="line"><a name="l00032"></a><span class="lineno"> 32</span>&#160;<span class="comment"> however, it will fail to accept further unmodified (and thus potentially</span></div><div class="line"><a name="l00033"></a><span class="lineno"> 33</span>&#160;<span class="comment"> valid) versions of the same block. We defend against this by detecting</span></div><div class="line"><a name="l00034"></a><span class="lineno"> 34</span>&#160;<span class="comment"> the case where we would hash two identical hashes at the end of the list</span></div><div class="line"><a name="l00035"></a><span class="lineno"> 35</span>&#160;<span class="comment"> together, and treating that identically to the block having an invalid</span></div><div class="line"><a name="l00036"></a><span class="lineno"> 36</span>&#160;<span class="comment"> merkle root. Assuming no double-SHA256 collisions, this will detect all</span></div><div class="line"><a name="l00037"></a><span class="lineno"> 37</span>&#160;<span class="comment"> known ways of changing the transactions without affecting the merkle</span></div><div class="line"><a name="l00038"></a><span class="lineno"> 38</span>&#160;<span class="comment"> root.</span></div><div class="line"><a name="l00039"></a><span class="lineno"> 39</span>&#160;<span class="comment">*/</span></div><div class="line"><a name="l00040"></a><span class="lineno"> 40</span>&#160;</div><div class="line"><a name="l00041"></a><span class="lineno"> 41</span>&#160;<span class="comment">/* This implements a constant-space merkle root/path calculator, limited to 2^32 leaves. */</span></div><div class="line"><a name="l00042"></a><span class="lineno"><a class="line" href="merkle_8cpp.html#a494314cc8dcede2854b5de8927a5065a"> 42</a></span>&#160;<span class="keyword">static</span> <span class="keywordtype">void</span> <a class="code" href="merkle_8cpp.html#a494314cc8dcede2854b5de8927a5065a">MerkleComputation</a>(<span class="keyword">const</span> std::vector&lt;uint256&gt;&amp; leaves, <a class="code" href="classuint256.html">uint256</a>* proot, <span class="keywordtype">bool</span>* pmutated, uint32_t branchpos, std::vector&lt;uint256&gt;* pbranch) {</div><div class="line"><a name="l00043"></a><span class="lineno"> 43</span>&#160; <span class="keywordflow">if</span> (pbranch) pbranch-&gt;clear();</div><div class="line"><a name="l00044"></a><span class="lineno"> 44</span>&#160; <span class="keywordflow">if</span> (leaves.size() == 0) {</div><div class="line"><a name="l00045"></a><span class="lineno"> 45</span>&#160; <span class="keywordflow">if</span> (pmutated) *pmutated = <span class="keyword">false</span>;</div><div class="line"><a name="l00046"></a><span class="lineno"> 46</span>&#160; <span class="keywordflow">if</span> (proot) *proot = <a class="code" href="classuint256.html">uint256</a>();</div><div class="line"><a name="l00047"></a><span class="lineno"> 47</span>&#160; <span class="keywordflow">return</span>;</div><div class="line"><a name="l00048"></a><span class="lineno"> 48</span>&#160; }</div><div class="line"><a name="l00049"></a><span class="lineno"> 49</span>&#160; <span class="keywordtype">bool</span> mutated = <span class="keyword">false</span>;</div><div class="line"><a name="l00050"></a><span class="lineno"> 50</span>&#160; <span class="comment">// count is the number of leaves processed so far.</span></div><div class="line"><a name="l00051"></a><span class="lineno"> 51</span>&#160; uint32_t <a class="code" href="tests_8c.html#ad43c3812e6d13e0518d9f8b8f463ffcf">count</a> = 0;</div><div class="line"><a name="l00052"></a><span class="lineno"> 52</span>&#160; <span class="comment">// inner is an array of eagerly computed subtree hashes, indexed by tree</span></div><div class="line"><a name="l00053"></a><span class="lineno"> 53</span>&#160; <span class="comment">// level (0 being the leaves).</span></div><div class="line"><a name="l00054"></a><span class="lineno"> 54</span>&#160; <span class="comment">// For example, when count is 25 (11001 in binary), inner[4] is the hash of</span></div><div class="line"><a name="l00055"></a><span class="lineno"> 55</span>&#160; <span class="comment">// the first 16 leaves, inner[3] of the next 8 leaves, and inner[0] equal to</span></div><div class="line"><a name="l00056"></a><span class="lineno"> 56</span>&#160; <span class="comment">// the last leaf. The other inner entries are undefined.</span></div><div class="line"><a name="l00057"></a><span class="lineno"> 57</span>&#160; <a class="code" href="classuint256.html">uint256</a> inner[32];</div><div class="line"><a name="l00058"></a><span class="lineno"> 58</span>&#160; <span class="comment">// Which position in inner is a hash that depends on the matching leaf.</span></div><div class="line"><a name="l00059"></a><span class="lineno"> 59</span>&#160; <span class="keywordtype">int</span> matchlevel = -1;</div><div class="line"><a name="l00060"></a><span class="lineno"> 60</span>&#160; <span class="comment">// First process all leaves into &#39;inner&#39; values.</span></div><div class="line"><a name="l00061"></a><span class="lineno"> 61</span>&#160; <span class="keywordflow">while</span> (<a class="code" href="tests_8c.html#ad43c3812e6d13e0518d9f8b8f463ffcf">count</a> &lt; leaves.size()) {</div><div class="line"><a name="l00062"></a><span class="lineno"> 62</span>&#160; <a class="code" href="classuint256.html">uint256</a> h = leaves[<a class="code" href="tests_8c.html#ad43c3812e6d13e0518d9f8b8f463ffcf">count</a>];</div><div class="line"><a name="l00063"></a><span class="lineno"> 63</span>&#160; <span class="keywordtype">bool</span> matchh = <a class="code" href="tests_8c.html#ad43c3812e6d13e0518d9f8b8f463ffcf">count</a> == branchpos;</div><div class="line"><a name="l00064"></a><span class="lineno"> 64</span>&#160; <a class="code" href="tests_8c.html#ad43c3812e6d13e0518d9f8b8f463ffcf">count</a>++;</div><div class="line"><a name="l00065"></a><span class="lineno"> 65</span>&#160; <span class="keywordtype">int</span> <a class="code" href="namespacewalletbackup.html#aa58c108487435bf8825e26521de44678">level</a>;</div><div class="line"><a name="l00066"></a><span class="lineno"> 66</span>&#160; <span class="comment">// For each of the lower bits in count that are 0, do 1 step. Each</span></div><div class="line"><a name="l00067"></a><span class="lineno"> 67</span>&#160; <span class="comment">// corresponds to an inner value that existed before processing the</span></div><div class="line"><a name="l00068"></a><span class="lineno"> 68</span>&#160; <span class="comment">// current leaf, and each needs a hash to combine it.</span></div><div class="line"><a name="l00069"></a><span class="lineno"> 69</span>&#160; <span class="keywordflow">for</span> (<a class="code" href="namespacewalletbackup.html#aa58c108487435bf8825e26521de44678">level</a> = 0; !(<a class="code" href="tests_8c.html#ad43c3812e6d13e0518d9f8b8f463ffcf">count</a> &amp; (((uint32_t)1) &lt;&lt; <a class="code" href="namespacewalletbackup.html#aa58c108487435bf8825e26521de44678">level</a>)); <a class="code" href="namespacewalletbackup.html#aa58c108487435bf8825e26521de44678">level</a>++) {</div><div class="line"><a name="l00070"></a><span class="lineno"> 70</span>&#160; <span class="keywordflow">if</span> (pbranch) {</div><div class="line"><a name="l00071"></a><span class="lineno"> 71</span>&#160; <span class="keywordflow">if</span> (matchh) {</div><div class="line"><a name="l00072"></a><span class="lineno"> 72</span>&#160; pbranch-&gt;push_back(inner[<a class="code" href="namespacewalletbackup.html#aa58c108487435bf8825e26521de44678">level</a>]);</div><div class="line"><a name="l00073"></a><span class="lineno"> 73</span>&#160; } <span class="keywordflow">else</span> <span class="keywordflow">if</span> (matchlevel == <a class="code" href="namespacewalletbackup.html#aa58c108487435bf8825e26521de44678">level</a>) {</div><div class="line"><a name="l00074"></a><span class="lineno"> 74</span>&#160; pbranch-&gt;push_back(h);</div><div class="line"><a name="l00075"></a><span class="lineno"> 75</span>&#160; matchh = <span class="keyword">true</span>;</div><div class="line"><a name="l00076"></a><span class="lineno"> 76</span>&#160; }</div><div class="line"><a name="l00077"></a><span class="lineno"> 77</span>&#160; }</div><div class="line"><a name="l00078"></a><span class="lineno"> 78</span>&#160; mutated |= (inner[<a class="code" href="namespacewalletbackup.html#aa58c108487435bf8825e26521de44678">level</a>] == h);</div><div class="line"><a name="l00079"></a><span class="lineno"> 79</span>&#160; <a class="code" href="classCHash256.html">CHash256</a>().<a class="code" href="classCHash256.html#a9cc25033c6435cb28e2e8e377c949a7a">Write</a>(inner[<a class="code" href="namespacewalletbackup.html#aa58c108487435bf8825e26521de44678">level</a>].begin(), 32).<a class="code" href="classCHash256.html#a9cc25033c6435cb28e2e8e377c949a7a">Write</a>(h.<a class="code" href="classbase__blob.html#aeee68e00ceeacf49086e98b661e017ff">begin</a>(), 32).Finalize(h.<a class="code" href="classbase__blob.html#aeee68e00ceeacf49086e98b661e017ff">begin</a>());</div><div class="line"><a name="l00080"></a><span class="lineno"> 80</span>&#160; }</div><div class="line"><a name="l00081"></a><span class="lineno"> 81</span>&#160; <span class="comment">// Store the resulting hash at inner position level.</span></div><div class="line"><a name="l00082"></a><span class="lineno"> 82</span>&#160; inner[<a class="code" href="namespacewalletbackup.html#aa58c108487435bf8825e26521de44678">level</a>] = h;</div><div class="line"><a name="l00083"></a><span class="lineno"> 83</span>&#160; <span class="keywordflow">if</span> (matchh) {</div><div class="line"><a name="l00084"></a><span class="lineno"> 84</span>&#160; matchlevel = <a class="code" href="namespacewalletbackup.html#aa58c108487435bf8825e26521de44678">level</a>;</div><div class="line"><a name="l00085"></a><span class="lineno"> 85</span>&#160; }</div><div class="line"><a name="l00086"></a><span class="lineno"> 86</span>&#160; }</div><div class="line"><a name="l00087"></a><span class="lineno"> 87</span>&#160; <span class="comment">// Do a final &#39;sweep&#39; over the rightmost branch of the tree to process</span></div><div class="line"><a name="l00088"></a><span class="lineno"> 88</span>&#160; <span class="comment">// odd levels, and reduce everything to a single top value.</span></div><div class="line"><a name="l00089"></a><span class="lineno"> 89</span>&#160; <span class="comment">// Level is the level (counted from the bottom) up to which we&#39;ve sweeped.</span></div><div class="line"><a name="l00090"></a><span class="lineno"> 90</span>&#160; <span class="keywordtype">int</span> <a class="code" href="namespacewalletbackup.html#aa58c108487435bf8825e26521de44678">level</a> = 0;</div><div class="line"><a name="l00091"></a><span class="lineno"> 91</span>&#160; <span class="comment">// As long as bit number level in count is zero, skip it. It means there</span></div><div class="line"><a name="l00092"></a><span class="lineno"> 92</span>&#160; <span class="comment">// is nothing left at this level.</span></div><div class="line"><a name="l00093"></a><span class="lineno"> 93</span>&#160; <span class="keywordflow">while</span> (!(<a class="code" href="tests_8c.html#ad43c3812e6d13e0518d9f8b8f463ffcf">count</a> &amp; (((uint32_t)1) &lt;&lt; <a class="code" href="namespacewalletbackup.html#aa58c108487435bf8825e26521de44678">level</a>))) {</div><div class="line"><a name="l00094"></a><span class="lineno"> 94</span>&#160; <a class="code" href="namespacewalletbackup.html#aa58c108487435bf8825e26521de44678">level</a>++;</div><div class="line"><a name="l00095"></a><span class="lineno"> 95</span>&#160; }</div><div class="line"><a name="l00096"></a><span class="lineno"> 96</span>&#160; <a class="code" href="classuint256.html">uint256</a> h = inner[<a class="code" href="namespacewalletbackup.html#aa58c108487435bf8825e26521de44678">level</a>];</div><div class="line"><a name="l00097"></a><span class="lineno"> 97</span>&#160; <span class="keywordtype">bool</span> matchh = matchlevel == <a class="code" href="namespacewalletbackup.html#aa58c108487435bf8825e26521de44678">level</a>;</div><div class="line"><a name="l00098"></a><span class="lineno"> 98</span>&#160; <span class="keywordflow">while</span> (<a class="code" href="tests_8c.html#ad43c3812e6d13e0518d9f8b8f463ffcf">count</a> != (((uint32_t)1) &lt;&lt; <a class="code" href="namespacewalletbackup.html#aa58c108487435bf8825e26521de44678">level</a>)) {</div><div class="line"><a name="l00099"></a><span class="lineno"> 99</span>&#160; <span class="comment">// If we reach this point, h is an inner value that is not the top.</span></div><div class="line"><a name="l00100"></a><span class="lineno"> 100</span>&#160; <span class="comment">// We combine it with itself (Bitcoin&#39;s special rule for odd levels in</span></div><div class="line"><a name="l00101"></a><span class="lineno"> 101</span>&#160; <span class="comment">// the tree) to produce a higher level one.</span></div><div class="line"><a name="l00102"></a><span class="lineno"> 102</span>&#160; <span class="keywordflow">if</span> (pbranch &amp;&amp; matchh) {</div><div class="line"><a name="l00103"></a><span class="lineno"> 103</span>&#160; pbranch-&gt;push_back(h);</div><div class="line"><a name="l00104"></a><span class="lineno"> 104</span>&#160; }</div><div class="line"><a name="l00105"></a><span class="lineno"> 105</span>&#160; <a class="code" href="classCHash256.html">CHash256</a>().<a class="code" href="classCHash256.html#a9cc25033c6435cb28e2e8e377c949a7a">Write</a>(h.<a class="code" href="classbase__blob.html#aeee68e00ceeacf49086e98b661e017ff">begin</a>(), 32).Write(h.<a class="code" href="classbase__blob.html#aeee68e00ceeacf49086e98b661e017ff">begin</a>(), 32).Finalize(h.<a class="code" href="classbase__blob.html#aeee68e00ceeacf49086e98b661e017ff">begin</a>());</div><div class="line"><a name="l00106"></a><span class="lineno"> 106</span>&#160; <span class="comment">// Increment count to the value it would have if two entries at this</span></div><div class="line"><a name="l00107"></a><span class="lineno"> 107</span>&#160; <span class="comment">// level had existed.</span></div><div class="line"><a name="l00108"></a><span class="lineno"> 108</span>&#160; <a class="code" href="tests_8c.html#ad43c3812e6d13e0518d9f8b8f463ffcf">count</a> += (((uint32_t)1) &lt;&lt; <a class="code" href="namespacewalletbackup.html#aa58c108487435bf8825e26521de44678">level</a>);</div><div class="line"><a name="l00109"></a><span class="lineno"> 109</span>&#160; <a class="code" href="namespacewalletbackup.html#aa58c108487435bf8825e26521de44678">level</a>++;</div><div class="line"><a name="l00110"></a><span class="lineno"> 110</span>&#160; <span class="comment">// And propagate the result upwards accordingly.</span></div><div class="line"><a name="l00111"></a><span class="lineno"> 111</span>&#160; <span class="keywordflow">while</span> (!(<a class="code" href="tests_8c.html#ad43c3812e6d13e0518d9f8b8f463ffcf">count</a> &amp; (((uint32_t)1) &lt;&lt; <a class="code" href="namespacewalletbackup.html#aa58c108487435bf8825e26521de44678">level</a>))) {</div><div class="line"><a name="l00112"></a><span class="lineno"> 112</span>&#160; <span class="keywordflow">if</span> (pbranch) {</div><div class="line"><a name="l00113"></a><span class="lineno"> 113</span>&#160; <span class="keywordflow">if</span> (matchh) {</div><div class="line"><a name="l00114"></a><span class="lineno"> 114</span>&#160; pbranch-&gt;push_back(inner[<a class="code" href="namespacewalletbackup.html#aa58c108487435bf8825e26521de44678">level</a>]);</div><div class="line"><a name="l00115"></a><span class="lineno"> 115</span>&#160; } <span class="keywordflow">else</span> <span class="keywordflow">if</span> (matchlevel == <a class="code" href="namespacewalletbackup.html#aa58c108487435bf8825e26521de44678">level</a>) {</div><div class="line"><a name="l00116"></a><span class="lineno"> 116</span>&#160; pbranch-&gt;push_back(h);</div><div class="line"><a name="l00117"></a><span class="lineno"> 117</span>&#160; matchh = <span class="keyword">true</span>;</div><div class="line"><a name="l00118"></a><span class="lineno"> 118</span>&#160; }</div><div class="line"><a name="l00119"></a><span class="lineno"> 119</span>&#160; }</div><div class="line"><a name="l00120"></a><span class="lineno"> 120</span>&#160; <a class="code" href="classCHash256.html">CHash256</a>().<a class="code" href="classCHash256.html#a9cc25033c6435cb28e2e8e377c949a7a">Write</a>(inner[<a class="code" href="namespacewalletbackup.html#aa58c108487435bf8825e26521de44678">level</a>].begin(), 32).<a class="code" href="classCHash256.html#a9cc25033c6435cb28e2e8e377c949a7a">Write</a>(h.<a class="code" href="classbase__blob.html#aeee68e00ceeacf49086e98b661e017ff">begin</a>(), 32).Finalize(h.<a class="code" href="classbase__blob.html#aeee68e00ceeacf49086e98b661e017ff">begin</a>());</div><div class="line"><a name="l00121"></a><span class="lineno"> 121</span>&#160; <a class="code" href="namespacewalletbackup.html#aa58c108487435bf8825e26521de44678">level</a>++;</div><div class="line"><a name="l00122"></a><span class="lineno"> 122</span>&#160; }</div><div class="line"><a name="l00123"></a><span class="lineno"> 123</span>&#160; }</div><div class="line"><a name="l00124"></a><span class="lineno"> 124</span>&#160; <span class="comment">// Return result.</span></div><div class="line"><a name="l00125"></a><span class="lineno"> 125</span>&#160; <span class="keywordflow">if</span> (pmutated) *pmutated = mutated;</div><div class="line"><a name="l00126"></a><span class="lineno"> 126</span>&#160; <span class="keywordflow">if</span> (proot) *proot = h;</div><div class="line"><a name="l00127"></a><span class="lineno"> 127</span>&#160;}</div><div class="line"><a name="l00128"></a><span class="lineno"> 128</span>&#160;</div><div class="line"><a name="l00129"></a><span class="lineno"><a class="line" href="merkle_8h.html#aa588f3d369fcbcb2288700dd0c7125ba"> 129</a></span>&#160;<a class="code" href="classuint256.html">uint256</a> <a class="code" href="merkle_8cpp.html#a72cae6e3250784fe5ba63714c20c73c1">ComputeMerkleRoot</a>(<span class="keyword">const</span> std::vector&lt;uint256&gt;&amp; leaves, <span class="keywordtype">bool</span>* mutated) {</div><div class="line"><a name="l00130"></a><span class="lineno"> 130</span>&#160; <a class="code" href="classuint256.html">uint256</a> hash;</div><div class="line"><a name="l00131"></a><span class="lineno"> 131</span>&#160; <a class="code" href="merkle_8cpp.html#a494314cc8dcede2854b5de8927a5065a">MerkleComputation</a>(leaves, &amp;hash, mutated, -1, NULL);</div><div class="line"><a name="l00132"></a><span class="lineno"> 132</span>&#160; <span class="keywordflow">return</span> hash;</div><div class="line"><a name="l00133"></a><span class="lineno"> 133</span>&#160;}</div><div class="line"><a name="l00134"></a><span class="lineno"> 134</span>&#160;</div><div class="line"><a name="l00135"></a><span class="lineno"><a class="line" href="merkle_8h.html#a5b83637c44e07fa76aa1163d192e17d9"> 135</a></span>&#160;std::vector&lt;uint256&gt; <a class="code" href="merkle_8cpp.html#a5b83637c44e07fa76aa1163d192e17d9">ComputeMerkleBranch</a>(<span class="keyword">const</span> std::vector&lt;uint256&gt;&amp; leaves, uint32_t position) {</div><div class="line"><a name="l00136"></a><span class="lineno"> 136</span>&#160; std::vector&lt;uint256&gt; ret;</div><div class="line"><a name="l00137"></a><span class="lineno"> 137</span>&#160; <a class="code" href="merkle_8cpp.html#a494314cc8dcede2854b5de8927a5065a">MerkleComputation</a>(leaves, NULL, NULL, position, &amp;ret);</div><div class="line"><a name="l00138"></a><span class="lineno"> 138</span>&#160; <span class="keywordflow">return</span> ret;</div><div class="line"><a name="l00139"></a><span class="lineno"> 139</span>&#160;}</div><div class="line"><a name="l00140"></a><span class="lineno"> 140</span>&#160;</div><div class="line"><a name="l00141"></a><span class="lineno"><a class="line" href="merkle_8h.html#ae1d841c761af779931b3e9c26751cf14"> 141</a></span>&#160;<a class="code" href="classuint256.html">uint256</a> <a class="code" href="merkle_8cpp.html#a033bc9a6981b1c6acd2fcdfea4e28190">ComputeMerkleRootFromBranch</a>(<span class="keyword">const</span> <a class="code" href="classuint256.html">uint256</a>&amp; leaf, <span class="keyword">const</span> std::vector&lt;uint256&gt;&amp; vMerkleBranch, uint32_t nIndex) {</div><div class="line"><a name="l00142"></a><span class="lineno"> 142</span>&#160; <a class="code" href="classuint256.html">uint256</a> hash = leaf;</div><div class="line"><a name="l00143"></a><span class="lineno"> 143</span>&#160; <span class="keywordflow">for</span> (std::vector&lt;uint256&gt;::const_iterator it = vMerkleBranch.begin(); it != vMerkleBranch.end(); ++it) {</div><div class="line"><a name="l00144"></a><span class="lineno"> 144</span>&#160; <span class="keywordflow">if</span> (nIndex &amp; 1) {</div><div class="line"><a name="l00145"></a><span class="lineno"> 145</span>&#160; hash = <a class="code" href="hash_8h.html#ab3f6c437460137530d86e09c2c102e99">Hash</a>(<a class="code" href="utilstrencodings_8h.html#a5f5e63e2c340bc52049e38c09b37e6e1">BEGIN</a>(*it), <a class="code" href="utilstrencodings_8h.html#ac32457d3f041434e57a2924847796fda">END</a>(*it), <a class="code" href="utilstrencodings_8h.html#a5f5e63e2c340bc52049e38c09b37e6e1">BEGIN</a>(hash), <a class="code" href="utilstrencodings_8h.html#ac32457d3f041434e57a2924847796fda">END</a>(hash));</div><div class="line"><a name="l00146"></a><span class="lineno"> 146</span>&#160; } <span class="keywordflow">else</span> {</div><div class="line"><a name="l00147"></a><span class="lineno"> 147</span>&#160; hash = <a class="code" href="hash_8h.html#ab3f6c437460137530d86e09c2c102e99">Hash</a>(<a class="code" href="utilstrencodings_8h.html#a5f5e63e2c340bc52049e38c09b37e6e1">BEGIN</a>(hash), <a class="code" href="utilstrencodings_8h.html#ac32457d3f041434e57a2924847796fda">END</a>(hash), <a class="code" href="utilstrencodings_8h.html#a5f5e63e2c340bc52049e38c09b37e6e1">BEGIN</a>(*it), <a class="code" href="utilstrencodings_8h.html#ac32457d3f041434e57a2924847796fda">END</a>(*it));</div><div class="line"><a name="l00148"></a><span class="lineno"> 148</span>&#160; }</div><div class="line"><a name="l00149"></a><span class="lineno"> 149</span>&#160; nIndex &gt;&gt;= 1;</div><div class="line"><a name="l00150"></a><span class="lineno"> 150</span>&#160; }</div><div class="line"><a name="l00151"></a><span class="lineno"> 151</span>&#160; <span class="keywordflow">return</span> hash;</div><div class="line"><a name="l00152"></a><span class="lineno"> 152</span>&#160;}</div><div class="line"><a name="l00153"></a><span class="lineno"> 153</span>&#160;</div><div class="line"><a name="l00154"></a><span class="lineno"><a class="line" href="merkle_8h.html#ae073fdf3c0e914b71b322dcb432358ea"> 154</a></span>&#160;<a class="code" href="classuint256.html">uint256</a> <a class="code" href="merkle_8cpp.html#aa6eda15268d7ec708195aed9b5d9eab3">BlockMerkleRoot</a>(<span class="keyword">const</span> <a class="code" href="classCBlock.html">CBlock</a>&amp; block, <span class="keywordtype">bool</span>* mutated)</div><div class="line"><a name="l00155"></a><span class="lineno"> 155</span>&#160;{</div><div class="line"><a name="l00156"></a><span class="lineno"> 156</span>&#160; std::vector&lt;uint256&gt; leaves;</div><div class="line"><a name="l00157"></a><span class="lineno"> 157</span>&#160; leaves.resize(block.<a class="code" href="classCBlock.html#a9b6508d662722775f3029b980b382b66">vtx</a>.size());</div><div class="line"><a name="l00158"></a><span class="lineno"> 158</span>&#160; <span class="keywordflow">for</span> (<span class="keywordtype">size_t</span> s = 0; s &lt; block.<a class="code" href="classCBlock.html#a9b6508d662722775f3029b980b382b66">vtx</a>.size(); s++) {</div><div class="line"><a name="l00159"></a><span class="lineno"> 159</span>&#160; leaves[s] = block.<a class="code" href="classCBlock.html#a9b6508d662722775f3029b980b382b66">vtx</a>[s].GetHash();</div><div class="line"><a name="l00160"></a><span class="lineno"> 160</span>&#160; }</div><div class="line"><a name="l00161"></a><span class="lineno"> 161</span>&#160; <span class="keywordflow">return</span> <a class="code" href="merkle_8cpp.html#a72cae6e3250784fe5ba63714c20c73c1">ComputeMerkleRoot</a>(leaves, mutated);</div><div class="line"><a name="l00162"></a><span class="lineno"> 162</span>&#160;}</div><div class="line"><a name="l00163"></a><span class="lineno"> 163</span>&#160;</div><div class="line"><a name="l00164"></a><span class="lineno"><a class="line" href="merkle_8h.html#a5bae1db6029068e4c3f0e9f14ff19942"> 164</a></span>&#160;std::vector&lt;uint256&gt; <a class="code" href="merkle_8cpp.html#a5bae1db6029068e4c3f0e9f14ff19942">BlockMerkleBranch</a>(<span class="keyword">const</span> <a class="code" href="classCBlock.html">CBlock</a>&amp; block, uint32_t position)</div><div class="line"><a name="l00165"></a><span class="lineno"> 165</span>&#160;{</div><div class="line"><a name="l00166"></a><span class="lineno"> 166</span>&#160; std::vector&lt;uint256&gt; leaves;</div><div class="line"><a name="l00167"></a><span class="lineno"> 167</span>&#160; leaves.resize(block.<a class="code" href="classCBlock.html#a9b6508d662722775f3029b980b382b66">vtx</a>.size());</div><div class="line"><a name="l00168"></a><span class="lineno"> 168</span>&#160; <span class="keywordflow">for</span> (<span class="keywordtype">size_t</span> s = 0; s &lt; block.<a class="code" href="classCBlock.html#a9b6508d662722775f3029b980b382b66">vtx</a>.size(); s++) {</div><div class="line"><a name="l00169"></a><span class="lineno"> 169</span>&#160; leaves[s] = block.<a class="code" href="classCBlock.html#a9b6508d662722775f3029b980b382b66">vtx</a>[s].GetHash();</div><div class="line"><a name="l00170"></a><span class="lineno"> 170</span>&#160; }</div><div class="line"><a name="l00171"></a><span class="lineno"> 171</span>&#160; <span class="keywordflow">return</span> <a class="code" href="merkle_8cpp.html#a5b83637c44e07fa76aa1163d192e17d9">ComputeMerkleBranch</a>(leaves, position);</div><div class="line"><a name="l00172"></a><span class="lineno"> 172</span>&#160;}</div><div class="ttc" id="merkle_8cpp_html_a5bae1db6029068e4c3f0e9f14ff19942"><div class="ttname"><a href="merkle_8cpp.html#a5bae1db6029068e4c3f0e9f14ff19942">BlockMerkleBranch</a></div><div class="ttdeci">std::vector&lt; uint256 &gt; BlockMerkleBranch(const CBlock &amp;block, uint32_t position)</div><div class="ttdef"><b>Definition:</b> <a href="merkle_8cpp_source.html#l00164">merkle.cpp:164</a></div></div>
<div class="ttc" id="merkle_8cpp_html_a5b83637c44e07fa76aa1163d192e17d9"><div class="ttname"><a href="merkle_8cpp.html#a5b83637c44e07fa76aa1163d192e17d9">ComputeMerkleBranch</a></div><div class="ttdeci">std::vector&lt; uint256 &gt; ComputeMerkleBranch(const std::vector&lt; uint256 &gt; &amp;leaves, uint32_t position)</div><div class="ttdef"><b>Definition:</b> <a href="merkle_8cpp_source.html#l00135">merkle.cpp:135</a></div></div>
<div class="ttc" id="merkle_8cpp_html_a033bc9a6981b1c6acd2fcdfea4e28190"><div class="ttname"><a href="merkle_8cpp.html#a033bc9a6981b1c6acd2fcdfea4e28190">ComputeMerkleRootFromBranch</a></div><div class="ttdeci">uint256 ComputeMerkleRootFromBranch(const uint256 &amp;leaf, const std::vector&lt; uint256 &gt; &amp;vMerkleBranch, uint32_t nIndex)</div><div class="ttdef"><b>Definition:</b> <a href="merkle_8cpp_source.html#l00141">merkle.cpp:141</a></div></div>
<div class="ttc" id="merkle_8cpp_html_a72cae6e3250784fe5ba63714c20c73c1"><div class="ttname"><a href="merkle_8cpp.html#a72cae6e3250784fe5ba63714c20c73c1">ComputeMerkleRoot</a></div><div class="ttdeci">uint256 ComputeMerkleRoot(const std::vector&lt; uint256 &gt; &amp;leaves, bool *mutated)</div><div class="ttdef"><b>Definition:</b> <a href="merkle_8cpp_source.html#l00129">merkle.cpp:129</a></div></div>
<div class="ttc" id="classbase__blob_html_aeee68e00ceeacf49086e98b661e017ff"><div class="ttname"><a href="classbase__blob.html#aeee68e00ceeacf49086e98b661e017ff">base_blob::begin</a></div><div class="ttdeci">unsigned char * begin()</div><div class="ttdef"><b>Definition:</b> <a href="uint256_8h_source.html#l00055">uint256.h:55</a></div></div>
<div class="ttc" id="merkle_8h_html"><div class="ttname"><a href="merkle_8h.html">merkle.h</a></div></div>
<div class="ttc" id="classCBlock_html_a9b6508d662722775f3029b980b382b66"><div class="ttname"><a href="classCBlock.html#a9b6508d662722775f3029b980b382b66">CBlock::vtx</a></div><div class="ttdeci">std::vector&lt; CTransaction &gt; vtx</div><div class="ttdef"><b>Definition:</b> <a href="block_8h_source.html#l00077">block.h:77</a></div></div>
<div class="ttc" id="utilstrencodings_8h_html_a5f5e63e2c340bc52049e38c09b37e6e1"><div class="ttname"><a href="utilstrencodings_8h.html#a5f5e63e2c340bc52049e38c09b37e6e1">BEGIN</a></div><div class="ttdeci">#define BEGIN(a)</div><div class="ttdef"><b>Definition:</b> <a href="utilstrencodings_8h_source.html#l00016">utilstrencodings.h:16</a></div></div>
<div class="ttc" id="utilstrencodings_8h_html"><div class="ttname"><a href="utilstrencodings_8h.html">utilstrencodings.h</a></div></div>
<div class="ttc" id="utilstrencodings_8h_html_ac32457d3f041434e57a2924847796fda"><div class="ttname"><a href="utilstrencodings_8h.html#ac32457d3f041434e57a2924847796fda">END</a></div><div class="ttdeci">#define END(a)</div><div class="ttdef"><b>Definition:</b> <a href="utilstrencodings_8h_source.html#l00017">utilstrencodings.h:17</a></div></div>
<div class="ttc" id="merkle_8cpp_html_aa6eda15268d7ec708195aed9b5d9eab3"><div class="ttname"><a href="merkle_8cpp.html#aa6eda15268d7ec708195aed9b5d9eab3">BlockMerkleRoot</a></div><div class="ttdeci">uint256 BlockMerkleRoot(const CBlock &amp;block, bool *mutated)</div><div class="ttdef"><b>Definition:</b> <a href="merkle_8cpp_source.html#l00154">merkle.cpp:154</a></div></div>
<div class="ttc" id="hash_8h_html_ab3f6c437460137530d86e09c2c102e99"><div class="ttname"><a href="hash_8h.html#ab3f6c437460137530d86e09c2c102e99">Hash</a></div><div class="ttdeci">uint256 Hash(const T1 pbegin, const T1 pend)</div><div class="ttdef"><b>Definition:</b> <a href="hash_8h_source.html#l00123">hash.h:123</a></div></div>
<div class="ttc" id="merkle_8cpp_html_a494314cc8dcede2854b5de8927a5065a"><div class="ttname"><a href="merkle_8cpp.html#a494314cc8dcede2854b5de8927a5065a">MerkleComputation</a></div><div class="ttdeci">static void MerkleComputation(const std::vector&lt; uint256 &gt; &amp;leaves, uint256 *proot, bool *pmutated, uint32_t branchpos, std::vector&lt; uint256 &gt; *pbranch)</div><div class="ttdef"><b>Definition:</b> <a href="merkle_8cpp_source.html#l00042">merkle.cpp:42</a></div></div>
<div class="ttc" id="classuint256_html"><div class="ttname"><a href="classuint256.html">uint256</a></div><div class="ttdef"><b>Definition:</b> <a href="uint256_8h_source.html#l00114">uint256.h:114</a></div></div>
<div class="ttc" id="classCHash256_html"><div class="ttname"><a href="classCHash256.html">CHash256</a></div><div class="ttdef"><b>Definition:</b> <a href="hash_8h_source.html#l00074">hash.h:74</a></div></div>
<div class="ttc" id="tests_8c_html_ad43c3812e6d13e0518d9f8b8f463ffcf"><div class="ttname"><a href="tests_8c.html#ad43c3812e6d13e0518d9f8b8f463ffcf">count</a></div><div class="ttdeci">static int count</div><div class="ttdef"><b>Definition:</b> <a href="tests_8c_source.html#l00041">tests.c:41</a></div></div>
<div class="ttc" id="namespacewalletbackup_html_aa58c108487435bf8825e26521de44678"><div class="ttname"><a href="namespacewalletbackup.html#aa58c108487435bf8825e26521de44678">walletbackup.level</a></div><div class="ttdeci">level</div><div class="ttdef"><b>Definition:</b> <a href="walletbackup_8py_source.html#l00040">walletbackup.py:40</a></div></div>
<div class="ttc" id="classCBlock_html"><div class="ttname"><a href="classCBlock.html">CBlock</a></div><div class="ttdef"><b>Definition:</b> <a href="block_8h_source.html#l00073">block.h:73</a></div></div>
<div class="ttc" id="classCHash256_html_a9cc25033c6435cb28e2e8e377c949a7a"><div class="ttname"><a href="classCHash256.html#a9cc25033c6435cb28e2e8e377c949a7a">CHash256::Write</a></div><div class="ttdeci">CHash256 &amp; Write(const unsigned char *data, size_t len)</div><div class="ttdef"><b>Definition:</b> <a href="hash_8h_source.html#l00086">hash.h:86</a></div></div>
</div><!-- fragment --></div><!-- contents -->
</div><!-- doc-content -->
<!-- start footer part -->
<div id="nav-path" class="navpath"><!-- id is needed for treeview function! -->
<ul>
<li class="navelem"><a class="el" href="dir_68267d1309a1af8e8297ef4c3efbcdba.html">src</a></li><li class="navelem"><a class="el" href="dir_dee301ab18d354d094fb7852349ef839.html">consensus</a></li><li class="navelem"><a class="el" href="merkle_8cpp.html">merkle.cpp</a></li>
<li class="footer">Generated on Thu Dec 14 2017 13:15:02 for Dash Core by
<a href="http://www.doxygen.org/index.html">
<img class="footer" src="doxygen.png" alt="doxygen"/></a> 1.8.14 </li>
</ul>
</div>
</body>
</html>