forked from dashpay/dash
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcoinjoin.cpp
More file actions
474 lines (416 loc) · 18.3 KB
/
coinjoin.cpp
File metadata and controls
474 lines (416 loc) · 18.3 KB
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
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
// Copyright (c) 2019-2024 The Dash Core developers
// Distributed under the MIT/X11 software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
#include <node/context.h>
#include <validation.h>
#include <coinjoin/context.h>
#include <coinjoin/server.h>
#include <rpc/blockchain.h>
#include <rpc/server.h>
#include <rpc/server_util.h>
#include <util/check.h>
#include <rpc/util.h>
#include <util/strencodings.h>
#include <wallet/rpc/util.h>
#ifdef ENABLE_WALLET
#include <coinjoin/client.h>
#include <coinjoin/options.h>
#include <interfaces/coinjoin.h>
#endif // ENABLE_WALLET
#include <univalue.h>
#ifdef ENABLE_WALLET
namespace {
void ValidateCoinJoinArguments()
{
/* If CoinJoin is enabled, everything is working as expected, we can bail */
if (CCoinJoinClientOptions::IsEnabled())
return;
/* CoinJoin is on by default, unless a command line argument says otherwise */
if (!gArgs.GetBoolArg("-enablecoinjoin", true)) {
throw JSONRPCError(RPC_INTERNAL_ERROR, "Mixing is disabled via -enablecoinjoin=0 command line option, remove it to enable mixing again");
}
/* Most likely something bad happened and we disabled it while running the wallet */
throw JSONRPCError(RPC_INTERNAL_ERROR, "Mixing is disabled due to an internal error");
}
} // anonymous namespace
static RPCHelpMan coinjoin()
{
return RPCHelpMan{"coinjoin",
"\nAvailable commands:\n"
" start - Start mixing\n"
" stop - Stop mixing\n"
" reset - Reset mixing",
{
{"command", RPCArg::Type::STR, RPCArg::Optional::NO, "The command to execute"},
},
RPCResults{},
RPCExamples{""},
[&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
{
throw JSONRPCError(RPC_INVALID_PARAMETER, "Must be a valid command");
},
};
}
static RPCHelpMan coinjoin_reset()
{
return RPCHelpMan{"coinjoin reset",
"\nReset CoinJoin mixing\n",
{},
RPCResult{
RPCResult::Type::STR, "", "Status of request"
},
RPCExamples{
HelpExampleCli("coinjoin reset", "")
+ HelpExampleRpc("coinjoin reset", "")
},
[&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
{
const std::shared_ptr<const CWallet> wallet = GetWalletForJSONRPCRequest(request);
if (!wallet) return NullUniValue;
const NodeContext& node = EnsureAnyNodeContext(request.context);
if (node.mn_activeman) {
throw JSONRPCError(RPC_INTERNAL_ERROR, "Client-side mixing is not supported on masternodes");
}
ValidateCoinJoinArguments();
auto cj_clientman = CHECK_NONFATAL(node.coinjoin_loader)->walletman().Get(wallet->GetName());
CHECK_NONFATAL(cj_clientman)->ResetPool();
return "Mixing was reset";
},
};
}
static RPCHelpMan coinjoin_start()
{
return RPCHelpMan{"coinjoin start",
"\nStart CoinJoin mixing\n"
"Wallet must be unlocked for mixing\n",
{},
RPCResult{
RPCResult::Type::STR, "", "Status of request"
},
RPCExamples{
HelpExampleCli("coinjoin start", "")
+ HelpExampleRpc("coinjoin start", "")
},
[&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
{
const std::shared_ptr<const CWallet> wallet = GetWalletForJSONRPCRequest(request);
if (!wallet) return NullUniValue;
const NodeContext& node = EnsureAnyNodeContext(request.context);
if (node.mn_activeman) {
throw JSONRPCError(RPC_INTERNAL_ERROR, "Client-side mixing is not supported on masternodes");
}
ValidateCoinJoinArguments();
{
LOCK(wallet->cs_wallet);
if (wallet->IsLocked(true))
throw JSONRPCError(RPC_WALLET_UNLOCK_NEEDED, "Error: Please unlock wallet for mixing with walletpassphrase first.");
}
auto cj_clientman = CHECK_NONFATAL(CHECK_NONFATAL(node.coinjoin_loader)->walletman().Get(wallet->GetName()));
if (!cj_clientman->StartMixing()) {
throw JSONRPCError(RPC_INTERNAL_ERROR, "Mixing has been started already.");
}
ChainstateManager& chainman = EnsureChainman(node);
CTxMemPool& mempool = EnsureMemPool(node);
CConnman& connman = EnsureConnman(node);
bool result = cj_clientman->DoAutomaticDenominating(chainman, connman, mempool);
return "Mixing " + (result ? "started successfully" : ("start failed: " + cj_clientman->GetStatuses().original + ", will retry"));
},
};
}
static RPCHelpMan coinjoin_stop()
{
return RPCHelpMan{"coinjoin stop",
"\nStop CoinJoin mixing\n",
{},
RPCResult{
RPCResult::Type::STR, "", "Status of request"
},
RPCExamples{
HelpExampleCli("coinjoin stop", "")
+ HelpExampleRpc("coinjoin stop", "")
},
[&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
{
const std::shared_ptr<const CWallet> wallet = GetWalletForJSONRPCRequest(request);
if (!wallet) return NullUniValue;
const NodeContext& node = EnsureAnyNodeContext(request.context);
if (node.mn_activeman) {
throw JSONRPCError(RPC_INTERNAL_ERROR, "Client-side mixing is not supported on masternodes");
}
ValidateCoinJoinArguments();
CHECK_NONFATAL(node.coinjoin_loader);
auto cj_clientman = node.coinjoin_loader->walletman().Get(wallet->GetName());
CHECK_NONFATAL(cj_clientman);
if (!cj_clientman->IsMixing()) {
throw JSONRPCError(RPC_INTERNAL_ERROR, "No mix session to stop");
}
cj_clientman->StopMixing();
return "Mixing was stopped";
},
};
}
static RPCHelpMan coinjoinsalt()
{
return RPCHelpMan{"coinjoinsalt",
"\nAvailable commands:\n"
" generate - Generate new CoinJoin salt\n"
" get - Fetch existing CoinJoin salt\n"
" set - Set new CoinJoin salt\n",
{
{"command", RPCArg::Type::STR, RPCArg::Optional::NO, "The command to execute"},
},
RPCResults{},
RPCExamples{""},
[&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
{
throw JSONRPCError(RPC_INVALID_PARAMETER, "Must be a valid command");
},
};
}
static RPCHelpMan coinjoinsalt_generate()
{
return RPCHelpMan{"coinjoinsalt generate",
"\nGenerate new CoinJoin salt and store it in the wallet database\n"
"Cannot generate new salt if CoinJoin mixing is in process or wallet has private keys disabled.\n",
{
{"overwrite", RPCArg::Type::BOOL, RPCArg::Default{false}, "Generate new salt even if there is an existing salt and/or there is CoinJoin balance"},
},
RPCResult{
RPCResult::Type::BOOL, "", "Status of CoinJoin salt generation and commitment"
},
RPCExamples{
HelpExampleCli("coinjoinsalt generate", "")
+ HelpExampleRpc("coinjoinsalt generate", "")
},
[&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
{
std::shared_ptr<CWallet> const wallet = GetWalletForJSONRPCRequest(request);
if (!wallet) return NullUniValue;
const auto str_wallet = wallet->GetName();
if (wallet->IsWalletFlagSet(WALLET_FLAG_DISABLE_PRIVATE_KEYS)) {
throw JSONRPCError(RPC_INVALID_REQUEST,
strprintf("Wallet \"%s\" has private keys disabled, cannot perform CoinJoin!", str_wallet));
}
bool enable_overwrite{false}; // Default value
if (!request.params[0].isNull()) {
enable_overwrite = ParseBoolV(request.params[0], "overwrite");
}
if (!enable_overwrite && !wallet->GetCoinJoinSalt().IsNull()) {
throw JSONRPCError(RPC_INVALID_REQUEST,
strprintf("Wallet \"%s\" already has set CoinJoin salt!", str_wallet));
}
const NodeContext& node = EnsureAnyNodeContext(request.context);
if (node.coinjoin_loader != nullptr) {
auto cj_clientman = node.coinjoin_loader->walletman().Get(wallet->GetName());
if (cj_clientman != nullptr && cj_clientman->IsMixing()) {
throw JSONRPCError(RPC_WALLET_ERROR,
strprintf("Wallet \"%s\" is currently mixing, cannot change salt!", str_wallet));
}
}
const auto wallet_balance{wallet->GetBalance()};
const bool has_balance{(wallet_balance.m_anonymized
+ wallet_balance.m_denominated_trusted
+ wallet_balance.m_denominated_untrusted_pending) > 0};
if (!enable_overwrite && has_balance) {
throw JSONRPCError(RPC_WALLET_ERROR,
strprintf("Wallet \"%s\" has CoinJoin balance, cannot continue!", str_wallet));
}
if (!wallet->SetCoinJoinSalt(GetRandHash())) {
throw JSONRPCError(RPC_INVALID_REQUEST,
strprintf("Unable to set new CoinJoin salt for wallet \"%s\"!", str_wallet));
}
wallet->ClearCoinJoinRoundsCache();
return true;
},
};
}
static RPCHelpMan coinjoinsalt_get()
{
return RPCHelpMan{"coinjoinsalt get",
"\nFetch existing CoinJoin salt\n"
"Cannot fetch salt if wallet has private keys disabled.\n",
{},
RPCResult{
RPCResult::Type::STR_HEX, "", "CoinJoin salt"
},
RPCExamples{
HelpExampleCli("coinjoinsalt get", "")
+ HelpExampleRpc("coinjoinsalt get", "")
},
[&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
{
std::shared_ptr<CWallet> const wallet = GetWalletForJSONRPCRequest(request);
if (!wallet) return NullUniValue;
const auto str_wallet = wallet->GetName();
if (wallet->IsWalletFlagSet(WALLET_FLAG_DISABLE_PRIVATE_KEYS)) {
throw JSONRPCError(RPC_INVALID_REQUEST,
strprintf("Wallet \"%s\" has private keys disabled, cannot perform CoinJoin!", str_wallet));
}
const auto salt{wallet->GetCoinJoinSalt()};
if (salt.IsNull()) {
throw JSONRPCError(RPC_WALLET_ERROR,
strprintf("Wallet \"%s\" has no CoinJoin salt!", str_wallet));
}
return salt.GetHex();
},
};
}
static RPCHelpMan coinjoinsalt_set()
{
return RPCHelpMan{"coinjoinsalt set",
"\nSet new CoinJoin salt\n"
"Cannot set salt if CoinJoin mixing is in process or wallet has private keys disabled.\n"
"Will overwrite existing salt. The presence of a CoinJoin balance will cause the wallet to rescan.\n",
{
{"salt", RPCArg::Type::STR, RPCArg::Optional::NO, "Desired CoinJoin salt value for the wallet"},
{"overwrite", RPCArg::Type::BOOL, RPCArg::Default{false}, "Overwrite salt even if CoinJoin balance present"},
},
RPCResult{
RPCResult::Type::BOOL, "", "Status of CoinJoin salt change request"
},
RPCExamples{
HelpExampleCli("coinjoinsalt set", "f4184fc596403b9d638783cf57adfe4c75c605f6356fbc91338530e9831e9e16")
+ HelpExampleRpc("coinjoinsalt set", "f4184fc596403b9d638783cf57adfe4c75c605f6356fbc91338530e9831e9e16")
},
[&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
{
std::shared_ptr<CWallet> const wallet = GetWalletForJSONRPCRequest(request);
if (!wallet) return NullUniValue;
const auto salt{ParseHashV(request.params[0], "salt")};
if (salt == uint256::ZERO) {
throw JSONRPCError(RPC_INVALID_PARAMETER, "Invalid CoinJoin salt value");
}
bool enable_overwrite{false}; // Default value
if (!request.params[1].isNull()) {
enable_overwrite = ParseBoolV(request.params[1], "overwrite");
}
const auto str_wallet = wallet->GetName();
if (wallet->IsWalletFlagSet(WALLET_FLAG_DISABLE_PRIVATE_KEYS)) {
throw JSONRPCError(RPC_INVALID_REQUEST,
strprintf("Wallet \"%s\" has private keys disabled, cannot perform CoinJoin!", str_wallet));
}
const NodeContext& node = EnsureAnyNodeContext(request.context);
if (node.coinjoin_loader != nullptr) {
auto cj_clientman = node.coinjoin_loader->walletman().Get(wallet->GetName());
if (cj_clientman != nullptr && cj_clientman->IsMixing()) {
throw JSONRPCError(RPC_WALLET_ERROR,
strprintf("Wallet \"%s\" is currently mixing, cannot change salt!", str_wallet));
}
}
const auto wallet_balance{wallet->GetBalance()};
const bool has_balance{(wallet_balance.m_anonymized
+ wallet_balance.m_denominated_trusted
+ wallet_balance.m_denominated_untrusted_pending) > 0};
if (has_balance && !enable_overwrite) {
throw JSONRPCError(RPC_WALLET_ERROR,
strprintf("Wallet \"%s\" has CoinJoin balance, cannot continue!", str_wallet));
}
if (!wallet->SetCoinJoinSalt(salt)) {
throw JSONRPCError(RPC_WALLET_ERROR,
strprintf("Unable to set new CoinJoin salt for wallet \"%s\"!", str_wallet));
}
wallet->ClearCoinJoinRoundsCache();
return true;
},
};
}
#endif // ENABLE_WALLET
static RPCHelpMan getcoinjoininfo()
{
return RPCHelpMan{"getcoinjoininfo",
"Returns an object containing an information about CoinJoin settings and state.\n",
{},
{
RPCResult{"for regular nodes",
RPCResult::Type::OBJ, "", "",
{
{RPCResult::Type::BOOL, "enabled", "Whether mixing functionality is enabled"},
{RPCResult::Type::BOOL, "multisession", "Whether CoinJoin Multisession option is enabled"},
{RPCResult::Type::NUM, "max_sessions", "How many parallel mixing sessions can there be at once"},
{RPCResult::Type::NUM, "max_rounds", "How many rounds to mix"},
{RPCResult::Type::NUM, "max_amount", "Target CoinJoin balance in " + CURRENCY_UNIT + ""},
{RPCResult::Type::NUM, "denoms_goal", "How many inputs of each denominated amount to target"},
{RPCResult::Type::NUM, "denoms_hardcap", "Maximum limit of how many inputs of each denominated amount to create"},
{RPCResult::Type::NUM, "queue_size", "How many queues there are currently on the network"},
{RPCResult::Type::BOOL, "running", "Whether mixing is currently running"},
{RPCResult::Type::ARR, "sessions", "",
{
{RPCResult::Type::OBJ, "", "",
{
{RPCResult::Type::STR_HEX, "protxhash", "The ProTxHash of the masternode"},
{RPCResult::Type::STR_HEX, "outpoint", "The outpoint of the masternode"},
{RPCResult::Type::STR, "service", "The IP address and port of the masternode"},
{RPCResult::Type::NUM, "denomination", "The denomination of the mixing session in " + CURRENCY_UNIT + ""},
{RPCResult::Type::STR_HEX, "state", "Current state of the mixing session"},
{RPCResult::Type::NUM, "entries_count", "The number of entries in the mixing session"},
}},
}},
{RPCResult::Type::NUM, "keys_left", /* optional */ true, "How many new keys are left since last automatic backup (if applicable)"},
{RPCResult::Type::STR, "warnings", "Warnings if any"},
}},
RPCResult{"for masternodes",
RPCResult::Type::OBJ, "", "",
{
{RPCResult::Type::NUM, "queue_size", "How many queues there are currently on the network"},
{RPCResult::Type::NUM, "denomination", "The denomination of the mixing session in " + CURRENCY_UNIT + ""},
{RPCResult::Type::STR_HEX, "state", "Current state of the mixing session"},
{RPCResult::Type::NUM, "entries_count", "The number of entries in the mixing session"},
}},
},
RPCExamples{
HelpExampleCli("getcoinjoininfo", "")
+ HelpExampleRpc("getcoinjoininfo", "")
},
[&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
{
UniValue obj(UniValue::VOBJ);
const NodeContext& node = EnsureAnyNodeContext(request.context);
if (node.mn_activeman) {
node.cj_ctx->server->GetJsonInfo(obj);
return obj;
}
#ifdef ENABLE_WALLET
CCoinJoinClientOptions::GetJsonInfo(obj);
obj.pushKV("queue_size", node.cj_ctx->queueman->GetQueueSize());
const std::shared_ptr<const CWallet> wallet = GetWalletForJSONRPCRequest(request);
if (!wallet) {
return obj;
}
auto* manager = CHECK_NONFATAL(node.coinjoin_loader->walletman().Get(wallet->GetName()));
manager->GetJsonInfo(obj);
std::string warning_msg{""};
if (wallet->IsLegacy()) {
obj.pushKV("keys_left", wallet->nKeysLeftSinceAutoBackup);
if (wallet->nKeysLeftSinceAutoBackup < COINJOIN_KEYS_THRESHOLD_WARNING) {
warning_msg = "WARNING: keypool is almost depleted!";
}
}
obj.pushKV("warnings", warning_msg);
#endif // ENABLE_WALLET
return obj;
},
};
}
void RegisterCoinJoinRPCCommands(CRPCTable &t)
{
// clang-format off
static const CRPCCommand commands[] =
{ // category actor (function)
// --------------------- -----------------------
{ "dash", &getcoinjoininfo, },
#ifdef ENABLE_WALLET
{ "dash", &coinjoin, },
{ "dash", &coinjoin_reset, },
{ "dash", &coinjoin_start, },
{ "dash", &coinjoin_stop, },
{ "dash", &coinjoinsalt, },
{ "dash", &coinjoinsalt_generate, },
{ "dash", &coinjoinsalt_get, },
{ "dash", &coinjoinsalt_set, },
#endif // ENABLE_WALLET
};
// clang-format on
for (const auto& command : commands) {
t.appendCommand(command.name, &command);
}
}