/
SetSignerList.cpp
378 lines (325 loc) · 12 KB
/
SetSignerList.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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
//------------------------------------------------------------------------------
/*
This file is part of rippled: https://github.com/ripple/rippled
Copyright (c) 2014 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/app/tx/impl/SetSignerList.h>
#include <ripple/app/ledger/Ledger.h>
#include <ripple/basics/Log.h>
#include <ripple/ledger/ApplyView.h>
#include <ripple/protocol/Feature.h>
#include <ripple/protocol/Indexes.h>
#include <ripple/protocol/STArray.h>
#include <ripple/protocol/STObject.h>
#include <ripple/protocol/STTx.h>
#include <algorithm>
#include <cstdint>
namespace ripple {
// We're prepared for there to be multiple signer lists in the future,
// but we don't need them yet. So for the time being we're manually
// setting the sfSignerListID to zero in all cases.
static std::uint32_t const defaultSignerListID_ = 0;
std::tuple<
NotTEC,
std::uint32_t,
std::vector<SignerEntries::SignerEntry>,
SetSignerList::Operation>
SetSignerList::determineOperation(
STTx const& tx,
ApplyFlags flags,
beast::Journal j)
{
// Check the quorum. A non-zero quorum means we're creating or replacing
// the list. A zero quorum means we're destroying the list.
auto const quorum = tx[sfSignerQuorum];
std::vector<SignerEntries::SignerEntry> sign;
Operation op = unknown;
bool const hasSignerEntries(tx.isFieldPresent(sfSignerEntries));
if (quorum && hasSignerEntries)
{
auto signers = SignerEntries::deserialize(tx, j, "transaction");
if (!signers)
return std::make_tuple(signers.error(), quorum, sign, op);
std::sort(signers->begin(), signers->end());
// Save deserialized list for later.
sign = std::move(*signers);
op = set;
}
else if ((quorum == 0) && !hasSignerEntries)
{
op = destroy;
}
return std::make_tuple(tesSUCCESS, quorum, sign, op);
}
NotTEC
SetSignerList::preflight(PreflightContext const& ctx)
{
if (auto const ret = preflight1(ctx); !isTesSuccess(ret))
return ret;
auto const result = determineOperation(ctx.tx, ctx.flags, ctx.j);
if (!isTesSuccess(std::get<0>(result)))
return std::get<0>(result);
if (std::get<3>(result) == unknown)
{
// Neither a set nor a destroy. Malformed.
JLOG(ctx.j.trace())
<< "Malformed transaction: Invalid signer set list format.";
return temMALFORMED;
}
if (std::get<3>(result) == set)
{
// Validate our settings.
auto const account = ctx.tx.getAccountID(sfAccount);
NotTEC const ter = validateQuorumAndSignerEntries(
std::get<1>(result),
std::get<2>(result),
account,
ctx.j,
ctx.rules);
if (!isTesSuccess(ter))
{
return ter;
}
}
return preflight2(ctx);
}
TER
SetSignerList::doApply()
{
// Perform the operation preCompute() decided on.
switch (do_)
{
case set:
return replaceSignerList();
case destroy:
return destroySignerList();
default:
break;
}
assert(false); // Should not be possible to get here.
return temMALFORMED;
}
void
SetSignerList::preCompute()
{
// Get the quorum and operation info.
auto result = determineOperation(ctx_.tx, view().flags(), j_);
assert(isTesSuccess(std::get<0>(result)));
assert(std::get<3>(result) != unknown);
quorum_ = std::get<1>(result);
signers_ = std::get<2>(result);
do_ = std::get<3>(result);
return Transactor::preCompute();
}
// The return type is signed so it is compatible with the 3rd argument
// of adjustOwnerCount() (which must be signed).
//
// NOTE: This way of computing the OwnerCount associated with a SignerList
// is valid until the featureMultiSignReserve amendment passes. Once it
// passes then just 1 OwnerCount is associated with a SignerList.
int
SetSignerList::signerCountBasedOwnerCountDelta(
std::size_t entryCount,
Rules const& rules)
{
// We always compute the full change in OwnerCount, taking into account:
// o The fact that we're adding/removing a SignerList and
// o Accounting for the number of entries in the list.
// We can get away with that because lists are not adjusted incrementally;
// we add or remove an entire list.
//
// The rule is:
// o Simply having a SignerList costs 2 OwnerCount units.
// o And each signer in the list costs 1 more OwnerCount unit.
// So, at a minimum, adding a SignerList with 1 entry costs 3 OwnerCount
// units. A SignerList with 8 entries would cost 10 OwnerCount units.
//
// The static_cast should always be safe since entryCount should always
// be in the range from 1 to 8 (or 32 if ExpandedSignerList is enabled).
// We've got a lot of room to grow.
assert(entryCount >= STTx::minMultiSigners);
assert(entryCount <= STTx::maxMultiSigners(&rules));
return 2 + static_cast<int>(entryCount);
}
TER
SetSignerList::removeSignersFromLedger(
Application& app,
ApplyView& view,
Keylet const& accountKeylet,
Keylet const& ownerDirKeylet,
Keylet const& signerListKeylet,
beast::Journal j)
{
// We have to examine the current SignerList so we know how much to
// reduce the OwnerCount.
SLE::pointer signers = view.peek(signerListKeylet);
// If the signer list doesn't exist we've already succeeded in deleting it.
if (!signers)
return tesSUCCESS;
// There are two different ways that the OwnerCount could be managed.
// If the lsfOneOwnerCount bit is set then remove just one owner count.
// Otherwise use the pre-MultiSignReserve amendment calculation.
int removeFromOwnerCount = -1;
if ((signers->getFlags() & lsfOneOwnerCount) == 0)
{
STArray const& actualList = signers->getFieldArray(sfSignerEntries);
removeFromOwnerCount =
signerCountBasedOwnerCountDelta(actualList.size(), view.rules()) *
-1;
}
// Remove the node from the account directory.
auto const hint = (*signers)[sfOwnerNode];
if (!view.dirRemove(ownerDirKeylet, hint, signerListKeylet.key, false))
{
JLOG(j.fatal()) << "Unable to delete SignerList from owner.";
return tefBAD_LEDGER;
}
adjustOwnerCount(
view,
view.peek(accountKeylet),
removeFromOwnerCount,
app.journal("View"));
view.erase(signers);
return tesSUCCESS;
}
TER
SetSignerList::removeFromLedger(
Application& app,
ApplyView& view,
AccountID const& account,
beast::Journal j)
{
auto const accountKeylet = keylet::account(account);
auto const ownerDirKeylet = keylet::ownerDir(account);
auto const signerListKeylet = keylet::signers(account);
return removeSignersFromLedger(
app, view, accountKeylet, ownerDirKeylet, signerListKeylet, j);
}
NotTEC
SetSignerList::validateQuorumAndSignerEntries(
std::uint32_t quorum,
std::vector<SignerEntries::SignerEntry> const& signers,
AccountID const& account,
beast::Journal j,
Rules const& rules)
{
// Reject if there are too many or too few entries in the list.
{
std::size_t const signerCount = signers.size();
if ((signerCount < STTx::minMultiSigners) ||
(signerCount > STTx::maxMultiSigners(&rules)))
{
JLOG(j.trace()) << "Too many or too few signers in signer list.";
return temMALFORMED;
}
}
// Make sure there are no duplicate signers.
assert(std::is_sorted(signers.begin(), signers.end()));
if (std::adjacent_find(signers.begin(), signers.end()) != signers.end())
{
JLOG(j.trace()) << "Duplicate signers in signer list";
return temBAD_SIGNER;
}
// Is the ExpandedSignerList amendment active?
bool const expandedSignerList = rules.enabled(featureExpandedSignerList);
// Make sure no signers reference this account. Also make sure the
// quorum can be reached.
std::uint64_t allSignersWeight(0);
for (auto const& signer : signers)
{
std::uint32_t const weight = signer.weight;
if (weight <= 0)
{
JLOG(j.trace()) << "Every signer must have a positive weight.";
return temBAD_WEIGHT;
}
allSignersWeight += signer.weight;
if (signer.account == account)
{
JLOG(j.trace()) << "A signer may not self reference account.";
return temBAD_SIGNER;
}
if (signer.tag && !expandedSignerList)
{
JLOG(j.trace()) << "Malformed transaction: sfWalletLocator "
"specified in SignerEntry "
<< "but featureExpandedSignerList is not enabled.";
return temMALFORMED;
}
// Don't verify that the signer accounts exist. Non-existent accounts
// may be phantom accounts (which are permitted).
}
if ((quorum <= 0) || (allSignersWeight < quorum))
{
JLOG(j.trace()) << "Quorum is unreachable";
return temBAD_QUORUM;
}
return tesSUCCESS;
}
TER
SetSignerList::replaceSignerList()
{
return replaceSignersFromLedger(
ctx_.app, ctx_.view(), j_, account_, quorum_, signers_, mPriorBalance);
}
TER
SetSignerList::destroySignerList()
{
auto const accountKeylet = keylet::account(account_);
// Destroying the signer list is only allowed if either the master key
// is enabled or there is a regular key.
SLE::pointer ledgerEntry = view().peek(accountKeylet);
if (!ledgerEntry)
return tefINTERNAL;
if ((ledgerEntry->isFlag(lsfDisableMaster)) &&
(!ledgerEntry->isFieldPresent(sfRegularKey)))
return tecNO_ALTERNATIVE_KEY;
auto const ownerDirKeylet = keylet::ownerDir(account_);
auto const signerListKeylet = keylet::signers(account_);
return removeSignersFromLedger(
ctx_.app, view(), accountKeylet, ownerDirKeylet, signerListKeylet, j_);
}
void
SetSignerList::writeSignersToSLE(
ApplyView& view,
SLE::pointer const& ledgerEntry,
std::uint32_t flags,
std::uint32_t quorum,
std::vector<SignerEntries::SignerEntry> const& signers)
{
// Assign the quorum, default SignerListID, and flags.
ledgerEntry->setFieldU32(sfSignerQuorum, quorum);
ledgerEntry->setFieldU32(sfSignerListID, defaultSignerListID_);
if (flags) // Only set flags if they are non-default (default is zero).
ledgerEntry->setFieldU32(sfFlags, flags);
bool const expandedSignerList =
view.rules().enabled(featureExpandedSignerList);
// Create the SignerListArray one SignerEntry at a time.
STArray toLedger(signers.size());
for (auto const& entry : signers)
{
toLedger.emplace_back(sfSignerEntry);
STObject& obj = toLedger.back();
obj.reserve(2);
obj.setAccountID(sfAccount, entry.account);
obj.setFieldU16(sfSignerWeight, entry.weight);
// This is a defensive check to make absolutely sure we will never write
// a tag into the ledger while featureExpandedSignerList is not enabled
if (expandedSignerList && entry.tag)
obj.setFieldH256(sfWalletLocator, *(entry.tag));
}
// Assign the SignerEntries.
ledgerEntry->setFieldArray(sfSignerEntries, toLedger);
}
} // namespace ripple