/
GenesisMint.cpp
400 lines (322 loc) · 11.9 KB
/
GenesisMint.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
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
//------------------------------------------------------------------------------
/*
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/app/tx/impl/GenesisMint.h>
#include <ripple/app/tx/impl/Import.h>
#include <ripple/basics/Log.h>
#include <ripple/ledger/View.h>
#include <ripple/protocol/Feature.h>
#include <ripple/protocol/Indexes.h>
#include <ripple/protocol/st.h>
namespace ripple {
TxConsequences
GenesisMint::makeTxConsequences(PreflightContext const& ctx)
{
// RH TODO: review this
return TxConsequences{ctx.tx, TxConsequences::normal};
}
NotTEC
GenesisMint::preflight(PreflightContext const& ctx)
{
if (!ctx.rules.enabled(featureHooks))
return temDISABLED;
if (!ctx.rules.enabled(featureXahauGenesis))
return temDISABLED;
if (auto const ret = preflight1(ctx); !isTesSuccess(ret))
return ret;
auto& tx = ctx.tx;
auto const id = ctx.tx[sfAccount];
static auto const genesisAccountId = calcAccountID(
generateKeyPair(KeyType::secp256k1, generateSeed("masterpassphrase"))
.first);
if (id != genesisAccountId || !tx.isFieldPresent(sfEmitDetails))
{
JLOG(ctx.j.warn()) << "GenesisMint: can only be used by the genesis "
"account in an emitted transaction.";
return temMALFORMED;
}
auto const& dests = tx.getFieldArray(sfGenesisMints);
if (dests.empty())
{
JLOG(ctx.j.warn()) << "GenesisMint: destinations array empty.";
return temMALFORMED;
}
if (dests.size() > 512)
{
JLOG(ctx.j.warn())
<< "GenesisMint: destinations array exceeds 512 entries.";
return temMALFORMED;
}
bool const allowDuplicates = ctx.rules.enabled(fixXahauV1);
std::unordered_set<AccountID> alreadySeen;
for (auto const& dest : dests)
{
if (dest.getFName() != sfGenesisMint)
{
JLOG(ctx.j.warn()) << "GenesisMint: destinations array contained "
"an invalid entry.";
return temMALFORMED;
}
if (!dest.isFieldPresent(sfDestination))
{
JLOG(ctx.j.warn())
<< "GenesisMint: DestinationField missing in array entry.";
return temMALFORMED;
}
bool const hasAmt = dest.isFieldPresent(sfAmount);
bool const hasMarks = dest.isFieldPresent(sfGovernanceMarks);
bool const hasFlags = dest.isFieldPresent(sfGovernanceFlags);
if (!hasAmt && !hasMarks && !hasFlags)
{
JLOG(ctx.j.warn())
<< "GenesisMint: each destination must have at least one of: "
<< "sfAmount, sfGovernanceFlags, sfGovernance marks.";
return temMALFORMED;
}
if (hasAmt)
{
auto const amt = dest.getFieldAmount(sfAmount);
if (!isXRP(amt))
{
JLOG(ctx.j.warn())
<< "GenesisMint: only native amounts can be minted.";
return temMALFORMED;
}
if (amt < beast::zero)
{
JLOG(ctx.j.warn())
<< "GenesisMint: only non-negative amounts can be minted.";
return temMALFORMED;
}
if (amt.xrp().drops() > 10'000'000'000'000ULL)
{
JLOG(ctx.j.warn()) << "GenesisMint: cannot mint more than 10MM "
"in a single txn.";
return temMALFORMED;
}
}
auto const accid = dest.getAccountID(sfDestination);
if (accid == noAccount() || accid == xrpAccount())
{
JLOG(ctx.j.warn()) << "GenesisMint: destinations includes "
"disallowed account zero or one.";
return temMALFORMED;
}
if (allowDuplicates)
continue;
if (alreadySeen.find(accid) != alreadySeen.end())
{
JLOG(ctx.j.warn()) << "GenesisMint: duplicate in destinations.";
return temMALFORMED;
}
alreadySeen.emplace(accid);
}
return preflight2(ctx);
}
TER
GenesisMint::preclaim(PreclaimContext const& ctx)
{
if (!ctx.view.rules().enabled(featureHooks))
return temDISABLED;
if (!ctx.view.rules().enabled(featureXahauGenesis))
return temDISABLED;
// RH UPTO:
// check that printing won't exceed 200% of the total coins on the ledger
// this will act as a hard cap against malfunction
// modify the invariant checkers
return tesSUCCESS;
}
TER
GenesisMint::doApply()
{
auto const& dests = ctx_.tx.getFieldArray(sfGenesisMints);
if (!view().rules().enabled(fixXahauV1))
{
STAmount dropsAdded{0};
for (auto const& dest : dests)
{
auto const amt = dest[~sfAmount];
if (amt && !isXRP(*amt))
{
JLOG(ctx_.journal.warn()) << "GenesisMint: Non-xrp amount.";
return tecINTERNAL;
}
auto const flags = dest[~sfGovernanceFlags];
auto const marks = dest[~sfGovernanceMarks];
auto const id = dest.getAccountID(sfDestination);
auto const k = keylet::account(id);
auto sle = view().peek(k);
bool const created = !sle;
if (created)
{
// Create the account.
std::uint32_t const seqno{
view().info().parentCloseTime.time_since_epoch().count()};
sle = std::make_shared<SLE>(k);
sle->setAccountID(sfAccount, id);
sle->setFieldU32(sfSequence, seqno);
if (amt)
{
sle->setFieldAmount(sfBalance, *amt);
dropsAdded += *amt;
}
else // give them 2 XRP if the account didn't exist, same as
// ttIMPORT
{
XRPAmount const initialXrp =
Import::computeStartingBonus(ctx_.view());
sle->setFieldAmount(sfBalance, initialXrp);
dropsAdded += initialXrp;
}
}
else if (amt)
{
// Credit the account
STAmount startBal = sle->getFieldAmount(sfBalance);
STAmount finalBal = startBal + *amt;
if (finalBal <= startBal)
{
JLOG(ctx_.journal.warn())
<< "GenesisMint: cannot credit " << dest
<< " due to balance overflow";
return tecINTERNAL;
}
sle->setFieldAmount(sfBalance, finalBal);
dropsAdded += *amt;
}
// set flags and marks as applicable
if (flags)
sle->setFieldH256(sfGovernanceFlags, *flags);
if (marks)
sle->setFieldH256(sfGovernanceMarks, *marks);
if (created)
view().insert(sle);
else
view().update(sle);
}
// update ledger header
if (dropsAdded < beast::zero ||
dropsAdded.xrp() + view().info().drops < view().info().drops)
{
JLOG(ctx_.journal.warn()) << "GenesisMint: dropsAdded overflowed\n";
return tecINTERNAL;
}
if (dropsAdded > beast::zero)
ctx_.rawView().rawDestroyXRP(-dropsAdded.xrp());
return tesSUCCESS;
}
// RH NOTE:
// As of fixXahauV1, duplicate accounts are allowed
// so we first do a summation loop then an actioning loop
// if amendment is not active then there's exactly one entry per
std::map<
AccountID, // account to credit
std::tuple<
XRPAmount, // amount to credit
std::optional<uint256>, // gov flags
std::optional<uint256>>> // gov marks
mints;
STAmount dropsAdded{0};
// sum loop
for (auto const& dest : dests)
{
auto const amt = dest[~sfAmount];
if (amt && !isXRP(*amt))
{
JLOG(ctx_.journal.warn()) << "GenesisMint: Non-xrp amount.";
return tecINTERNAL;
}
XRPAmount toCredit = amt ? amt->xrp() : XRPAmount{0};
auto const flags = dest[~sfGovernanceFlags];
auto const marks = dest[~sfGovernanceMarks];
auto const id = dest.getAccountID(sfDestination);
bool const firstOccurance = mints.find(id) == mints.end();
dropsAdded += toCredit;
// if flags / marks appear more than once we just take the first
// appearance
if (firstOccurance)
mints[id] = {toCredit, flags, marks};
else
{
// duplicated entries accumulate in the map
auto& [accTotal, f, m] = mints[id];
// detect overflow
if (accTotal + toCredit < accTotal)
{
JLOG(ctx_.journal.warn()) << "GenesisMint: cannot credit " << id
<< " due to balance overflow";
return tecINTERNAL;
}
accTotal += toCredit;
}
}
// check for ledger header overflow
if (dropsAdded < beast::zero ||
dropsAdded.xrp() + view().info().drops < view().info().drops)
{
JLOG(ctx_.journal.warn()) << "GenesisMint: dropsAdded overflowed\n";
return tecINTERNAL;
}
// action loop
for (auto const& [id, values] : mints)
{
auto const& [amt, flags, marks] = values;
auto const k = keylet::account(id);
auto sle = view().peek(k);
bool const created = !sle;
if (created)
{
// Create the account.
std::uint32_t const seqno{
view().info().parentCloseTime.time_since_epoch().count()};
sle = std::make_shared<SLE>(k);
sle->setAccountID(sfAccount, id);
sle->setFieldU32(sfSequence, seqno);
sle->setFieldAmount(sfBalance, amt);
}
else if (amt > beast::zero)
{
// Credit the account
STAmount startBal = sle->getFieldAmount(sfBalance);
STAmount finalBal = startBal + amt;
if (finalBal <= startBal)
{
JLOG(ctx_.journal.warn()) << "GenesisMint: cannot credit " << id
<< " due to balance overflow";
return tecINTERNAL;
}
sle->setFieldAmount(sfBalance, finalBal);
}
// set flags and marks as applicable
if (flags)
sle->setFieldH256(sfGovernanceFlags, *flags);
if (marks)
sle->setFieldH256(sfGovernanceMarks, *marks);
if (created)
view().insert(sle);
else
view().update(sle);
}
if (dropsAdded > beast::zero)
ctx_.rawView().rawDestroyXRP(-dropsAdded.xrp());
return tesSUCCESS;
}
XRPAmount
GenesisMint::calculateBaseFee(ReadView const& view, STTx const& tx)
{
return XRPAmount{0};
}
} // namespace ripple