-
Notifications
You must be signed in to change notification settings - Fork 30
/
SHAMapDelta.cpp
267 lines (235 loc) · 10.3 KB
/
SHAMapDelta.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
//------------------------------------------------------------------------------
/*
This file is part of rippled: https://github.com/ripple/rippled
Copyright (c) 2012, 2013 Ripple Labs Inc.
Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
//==============================================================================
#include <ripple/basics/contract.h>
#include <ripple/shamap/SHAMap.h>
namespace ripple {
// This code is used to compare another node's transaction tree
// to our own. It returns a map containing all items that are different
// between two SHA maps. It is optimized not to descend down tree
// branches with the same branch hash. A limit can be passed so
// that we will abort early if a node sends a map to us that
// makes no sense at all. (And our sync algorithm will avoid
// synchronizing matching branches too.)
bool SHAMap::walkBranch (SHAMapAbstractNode* node,
std::shared_ptr<SHAMapItem const> const& otherMapItem,
bool isFirstMap,Delta& differences, int& maxCount) const
{
// Walk a branch of a SHAMap that's matched by an empty branch or single item in the other map
std::stack <SHAMapAbstractNode*, std::vector<SHAMapAbstractNode*>> nodeStack;
nodeStack.push (node);
bool emptyBranch = !otherMapItem;
while (!nodeStack.empty ())
{
node = nodeStack.top ();
nodeStack.pop ();
if (node->isInner ())
{
// This is an inner node, add all non-empty branches
auto inner = static_cast<SHAMapInnerNode*>(node);
for (int i = 0; i < 16; ++i)
if (!inner->isEmptyBranch (i))
nodeStack.push ({descendThrow (inner, i)});
}
else
{
// This is a leaf node, process its item
auto item = static_cast<SHAMapTreeNode*>(node)->peekItem();
if (emptyBranch || (item->key() != otherMapItem->key()))
{
// unmatched
if (isFirstMap)
differences.insert (std::make_pair (item->key(),
DeltaRef (item, std::shared_ptr<SHAMapItem const> ())));
else
differences.insert (std::make_pair (item->key(),
DeltaRef (std::shared_ptr<SHAMapItem const> (), item)));
if (--maxCount <= 0)
return false;
}
else if (item->peekData () != otherMapItem->peekData ())
{
// non-matching items with same tag
if (isFirstMap)
differences.insert (std::make_pair (item->key(),
DeltaRef (item, otherMapItem)));
else
differences.insert (std::make_pair (item->key(),
DeltaRef (otherMapItem, item)));
if (--maxCount <= 0)
return false;
emptyBranch = true;
}
else
{
// exact match
emptyBranch = true;
}
}
}
if (!emptyBranch)
{
// otherMapItem was unmatched, must add
if (isFirstMap) // this is first map, so other item is from second
differences.insert(std::make_pair(otherMapItem->key(),
DeltaRef(std::shared_ptr<SHAMapItem const>(),
otherMapItem)));
else
differences.insert(std::make_pair(otherMapItem->key(),
DeltaRef(otherMapItem, std::shared_ptr<SHAMapItem const>())));
if (--maxCount <= 0)
return false;
}
return true;
}
bool
SHAMap::compare (SHAMap const& otherMap,
Delta& differences, int maxCount) const
{
// compare two hash trees, add up to maxCount differences to the difference table
// return value: true=complete table of differences given, false=too many differences
// throws on corrupt tables or missing nodes
// CAUTION: otherMap is not locked and must be immutable
assert (isValid () && otherMap.isValid ());
if (getHash () == otherMap.getHash ())
return true;
using StackEntry = std::pair <SHAMapAbstractNode*, SHAMapAbstractNode*>;
std::stack <StackEntry, std::vector<StackEntry>> nodeStack; // track nodes we've pushed
nodeStack.push ({root_.get(), otherMap.root_.get()});
while (!nodeStack.empty ())
{
SHAMapAbstractNode* ourNode = nodeStack.top().first;
SHAMapAbstractNode* otherNode = nodeStack.top().second;
nodeStack.pop ();
if (!ourNode || !otherNode)
{
assert (false);
Throw<SHAMapMissingNode> (type_, uint256 ());
}
if (ourNode->isLeaf () && otherNode->isLeaf ())
{
// two leaves
auto ours = static_cast<SHAMapTreeNode*>(ourNode);
auto other = static_cast<SHAMapTreeNode*>(otherNode);
if (ours->peekItem()->key() == other->peekItem()->key())
{
if (ours->peekItem()->peekData () != other->peekItem()->peekData ())
{
differences.insert (std::make_pair (ours->peekItem()->key(),
DeltaRef (ours->peekItem (),
other->peekItem ())));
if (--maxCount <= 0)
return false;
}
}
else
{
differences.insert (std::make_pair(ours->peekItem()->key(),
DeltaRef(ours->peekItem(),
std::shared_ptr<SHAMapItem const>())));
if (--maxCount <= 0)
return false;
differences.insert(std::make_pair(other->peekItem()->key(),
DeltaRef(std::shared_ptr<SHAMapItem const>(), other->peekItem ())));
if (--maxCount <= 0)
return false;
}
}
else if (ourNode->isInner () && otherNode->isLeaf ())
{
auto ours = static_cast<SHAMapInnerNode*>(ourNode);
auto other = static_cast<SHAMapTreeNode*>(otherNode);
if (!walkBranch (ours, other->peekItem (),
true, differences, maxCount))
return false;
}
else if (ourNode->isLeaf () && otherNode->isInner ())
{
auto ours = static_cast<SHAMapTreeNode*>(ourNode);
auto other = static_cast<SHAMapInnerNode*>(otherNode);
if (!otherMap.walkBranch (other, ours->peekItem (),
false, differences, maxCount))
return false;
}
else if (ourNode->isInner () && otherNode->isInner ())
{
auto ours = static_cast<SHAMapInnerNode*>(ourNode);
auto other = static_cast<SHAMapInnerNode*>(otherNode);
for (int i = 0; i < 16; ++i)
if (ours->getChildHash (i) != other->getChildHash (i))
{
if (other->isEmptyBranch (i))
{
// We have a branch, the other tree does not
SHAMapAbstractNode* iNode = descendThrow (ours, i);
if (!walkBranch (iNode,
std::shared_ptr<SHAMapItem const> (), true,
differences, maxCount))
return false;
}
else if (ours->isEmptyBranch (i))
{
// The other tree has a branch, we do not
SHAMapAbstractNode* iNode =
otherMap.descendThrow(other, i);
if (!otherMap.walkBranch (iNode,
std::shared_ptr<SHAMapItem const>(),
false, differences, maxCount))
return false;
}
else // The two trees have different non-empty branches
nodeStack.push ({descendThrow (ours, i),
otherMap.descendThrow (other, i)});
}
}
else
assert (false);
}
return true;
}
void SHAMap::walkMap (std::vector<SHAMapMissingNode>& missingNodes, int maxMissing) const
{
if (!root_->isInner ()) // root_ is only node, and we have it
return;
using StackEntry = std::shared_ptr<SHAMapInnerNode>;
std::stack <StackEntry, std::vector <StackEntry>> nodeStack;
nodeStack.push (std::static_pointer_cast<SHAMapInnerNode>(root_));
while (!nodeStack.empty ())
{
std::shared_ptr<SHAMapInnerNode> node = std::move (nodeStack.top());
nodeStack.pop ();
for (int i = 0; i < 16; ++i)
{
if (!node->isEmptyBranch (i))
{
std::shared_ptr<SHAMapAbstractNode> nextNode = descendNoStore (node, i);
if (nextNode)
{
if (nextNode->isInner ())
nodeStack.push(
std::static_pointer_cast<SHAMapInnerNode>(nextNode));
}
else
{
missingNodes.emplace_back (type_, node->getChildHash (i));
if (--maxMissing <= 0)
return;
}
}
}
}
}
} // ripple