forked from namecoin/namecoin-legacy
/
611.cpp
2757 lines (2376 loc) · 91.3 KB
/
611.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
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
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright (c) 2010-2011 Vincent Durham
// Distributed under the MIT/X11 software license, see the accompanying
// file license.txt or http://www.opensource.org/licenses/mit-license.php.
//
#include "headers.h"
#include "db.h"
#include "keystore.h"
#include "wallet.h"
#include "init.h"
#include "auxpow.h"
#include "611.h"
#include "bitcoinrpc.h"
#include "json/json_spirit_reader_template.h"
#include "json/json_spirit_writer_template.h"
#include "json/json_spirit_utils.h"
#include <boost/xpressive/xpressive_dynamic.hpp>
using namespace std;
using namespace json_spirit;
static const bool NAME_DEBUG = false;
extern int64 AmountFromValue(const Value& value);
extern Object JSONRPCError(int code, const string& message);
template<typename T> void ConvertTo(Value& value, bool fAllowNull=false);
static const int BUG_WORKAROUND_BLOCK = 0; // Point of hard fork
std::map<vchType, uint256> mapMyNames;
std::map<vchType, set<uint256> > mapNamePending;
std::set<vchType> setNewHashes;
#ifdef GUI
extern std::map<uint160, vchType> mapMyNameHashes;
#endif
extern uint256 SignatureHash(CScript scriptCode, const CTransaction& txTo, unsigned int nIn, int nHashType);
// forward decls
extern bool Solver(const CKeyStore& keystore, const CScript& scriptPubKey, uint256 hash, int nHashType, CScript& scriptSigRet);
extern bool VerifyScript(const CScript& scriptSig, const CScript& scriptPubKey, const CTransaction& txTo, unsigned int nIn, int nHashType);
extern bool IsConflictedTx (DatabaseSet& dbset, const CTransaction& tx, vchType& name);
extern void rescanfornames();
extern Value sendtoaddress(const Array& params, bool fHelp);
const int SIXELEVEN_COIN_GENESIS_EXTRA = 611;
// uint256 hash611GenesisBlock("5046007a912dca485580fbc753ddd247e9bc189d357902813a5952937b3f342a");
// uint256 hash611GenesisBlock("413a8fbe9b5fb31f28527f46818c5be332c258019874f92f6ae30fd1afb28081");
// uint256 hash611GenesisBlock("64b1665d23ccc3dd3ae111c32e32f401578f4e590d946b8612f570487898c5a8");
uint256 hash611GenesisBlock("7530c8ebcdd9d7944ac9de047240f0ac06707b0868cfe57f98831eeb2843d7ab");
class C611Hooks : public CHooks
{
public:
virtual bool IsStandard(const CScript& scriptPubKey);
virtual void AddToWallet(CWalletTx& tx);
virtual bool CheckTransaction(const CTransaction& tx);
virtual bool ConnectInputs(DatabaseSet& dbset,
map<uint256, CTxIndex>& mapTestPool,
const CTransaction& tx,
vector<CTransaction>& vTxPrev,
vector<CTxIndex>& vTxindex,
CBlockIndex* pindexBlock,
CDiskTxPos& txPos,
bool fBlock,
bool fMiner);
virtual bool DisconnectInputs (DatabaseSet& dbset,
const CTransaction& tx,
CBlockIndex* pindexBlock);
virtual bool ConnectBlock (CBlock& block, DatabaseSet& dbset,
CBlockIndex* pindex);
virtual bool DisconnectBlock (CBlock& block, DatabaseSet& dbset,
CBlockIndex* pindex);
virtual bool ExtractAddress(const CScript& script, string& address);
virtual bool GenesisBlock(CBlock& block);
virtual bool Lockin(int nHeight, uint256 hash);
virtual int LockinHeight();
virtual string IrcPrefix();
virtual bool AcceptToMemoryPool(DatabaseSet& dbset, const CTransaction& tx);
virtual void RemoveFromMemoryPool(const CTransaction& tx);
virtual void MessageStart(char* pchMessageStart)
{
// Make the message start different
// pchMessageStart[3] = 0xfe;
// ohhh - sometimes 611 is 46609
pchMessageStart[2] = 0xb6;
pchMessageStart[3] = 0x11;
}
virtual bool IsMine(const CTransaction& tx);
virtual bool IsMine(const CTransaction& tx, const CTxOut& txout, bool ignore_name_new = false);
virtual int GetOurChainID()
{
return 0x0001;
}
virtual int GetAuxPowStartBlock()
{
if (fTestNet)
return 0;
return 19200;
}
virtual int GetFullRetargetStartBlock()
{
if (fTestNet)
return 0;
return 19200;
}
string GetAlertPubkey1()
{
return "046e402e42167adacc1dbaa01fd272fc1ba56aa2a1f53e5b71e57d1ba67a1a1130316ec2a376130af0fb67173de0189865809664aceb2f5b3be874227792a58ddb";
}
string GetAlertPubkey2()
{
return "04ae90ead1eef7380d9efd19e90b0dcc381b0427ed1f62a96a45b67fe864187a4c4c677b644d133058519dad57af15fc07e6fe376b831779536753573066b8f069";
}
};
/**
* Check whether the given height is post-libcoin-hardfork. I. e.,
* strict checks for name_update and tx decoding should be applied.
*/
static bool
postLibcoinFork(unsigned nHeight)
{
if (fTestNet)
return nHeight >= 108000;
return nHeight >= BUG_WORKAROUND_BLOCK;
}
/**
* Check whether the given height is post the "strict checks" hardfork
* done in preparation for switching to the rebased client.
*/
bool
doStrictChecks(unsigned nHeight)
{
if (fTestNet)
return nHeight >= 108000;
return nHeight >= 212500;
}
int64 getAmount(Value value)
{
ConvertTo<double>(value);
double dAmount = value.get_real();
int64 nAmount = roundint64(dAmount * COIN);
if (!MoneyRange(nAmount))
throw JSONRPCError(RPC_TYPE_ERROR, "Invalid amount");
return nAmount;
}
vchType
vchFromValue (const Value& value)
{
const std::string str = value.get_str ();
return vchFromString (str);
}
vchType
vchFromString (const std::string& str)
{
const unsigned char* strbeg;
strbeg = reinterpret_cast<const unsigned char*> (str.c_str ());
return vchType(strbeg, strbeg + str.size ());
}
string stringFromVch(const vector<unsigned char> &vch) {
string res;
vector<unsigned char>::const_iterator vi = vch.begin();
while (vi != vch.end()) {
res += (char)(*vi);
vi++;
}
return res;
}
// Names registered with 611 should be valid at least 365 days.
// Increase expiration to 108000 gradually starting at block 18000.
// Use for validation purposes and pass the chain height.
int GetExpirationDepth(int nHeight) {
if (nHeight < 18000)
return 18000;
if (nHeight < 108000)
return nHeight;
return 108000;
}
// For display purposes, pass the name height.
int GetDisplayExpirationDepth(int nHeight) {
if (nHeight < 18000)
return 18000;
if (nHeight < 108000)
return nHeight;
return 108000;
}
int64 GetNetworkFee(int nHeight)
{
// Speed up network fee decrease 4x starting at 24000
// if (nHeight >= 24000)
// nHeight += (nHeight - 24000) * 3;
// if ((nHeight >> 13) >= 60)
// return 0;
// int64 nStart = 50 * COIN;
// if (fTestNet)
// nStart = 10 * CENT;
// int64 nRes = nStart >> (nHeight >> 13);
// nRes -= (nRes >> 14) * (nHeight % 8192);
// return nRes;
// the standard network fee is 6.11 cent
int64 nStart = 611 * CENT / 100;
// it will decrease by factor two every 2^18 or 262144 blocks
int64 nNetFee = nStart >> (nHeight >> 18);
nNetFee -= (nNetFee >> 19) * (nHeight % 262144);
// but is was fixed for the very early developers
if (nHeight <= 10110)
nNetFee = 611 * CENT / 100;
if (nHeight <= 2880)
nNetFee = 611 * CENT / 1000;
if (fTestNet)
nNetFee = 1 * CENT;
return nNetFee;
}
int GetTxPosHeight(const CNameIndex& txPos)
{
return txPos.nHeight;
}
int GetTxPosHeight(const CDiskTxPos& txPos)
{
// Read block header
CBlock block;
if (!block.ReadFromDisk(txPos.nFile, txPos.nBlockPos, false))
return 0;
// Find the block in the index
map<uint256, CBlockIndex*>::iterator mi = mapBlockIndex.find(block.GetHash());
if (mi == mapBlockIndex.end())
return 0;
CBlockIndex* pindex = (*mi).second;
if (!pindex || !pindex->IsInMainChain())
return 0;
return pindex->nHeight;
}
int GetTxPosHeight2(const CDiskTxPos& txPos, int nHeight)
{
nHeight = GetTxPosHeight(txPos);
return nHeight;
}
int
GetNameHeight (DatabaseSet& dbset, vector<unsigned char> vchName)
{
vector<CNameIndex> vtxPos;
if (dbset.name ().ExistsName (vchName))
{
if (!dbset.name ().ReadName (vchName, vtxPos))
return error("GetNameHeight() : failed to read from name DB");
if (vtxPos.empty ())
return -1;
CNameIndex& txPos = vtxPos.back ();
return GetTxPosHeight (txPos);
}
return -1;
}
CScript RemoveNameScriptPrefix(const CScript& scriptIn)
{
int op;
vector<vector<unsigned char> > vvch;
CScript::const_iterator pc = scriptIn.begin();
if (!DecodeNameScript(scriptIn, op, vvch, pc))
throw runtime_error("RemoveNameScriptPrefix() : could not decode name script");
return CScript(pc, scriptIn.end());
}
bool IsMyName(const CTransaction& tx, const CTxOut& txout)
{
const CScript& scriptPubKey = RemoveNameScriptPrefix(txout.scriptPubKey);
CScript scriptSig;
if (!Solver(*pwalletMain, scriptPubKey, 0, 0, scriptSig))
return false;
return true;
}
bool CreateTransactionWithInputTx(const vector<pair<CScript, int64> >& vecSend, const CWalletTx& wtxIn, int nTxOut, CWalletTx& wtxNew, CReserveKey& reservekey, int64& nFeeRet)
{
int64 nValue = 0;
BOOST_FOREACH(const PAIRTYPE(CScript, int64)& s, vecSend)
{
if (nValue < 0)
return false;
nValue += s.second;
}
if (vecSend.empty() || nValue < 0)
return false;
wtxNew.pwallet = pwalletMain;
CRITICAL_BLOCK(cs_main)
{
// txdb must be opened before the mapWallet lock
CTxDB txdb("r");
CRITICAL_BLOCK(pwalletMain->cs_mapWallet)
{
nFeeRet = nTransactionFee;
loop
{
wtxNew.vin.clear();
wtxNew.vout.clear();
wtxNew.fFromMe = true;
int64 nTotalValue = nValue + nFeeRet;
printf("CreateTransactionWithInputTx: total value = %d\n", nTotalValue);
double dPriority = 0;
// vouts to the payees
BOOST_FOREACH(const PAIRTYPE(CScript, int64)& s, vecSend)
wtxNew.vout.push_back(CTxOut(s.second, s.first));
int64 nWtxinCredit = wtxIn.vout[nTxOut].nValue;
// Choose coins to use
set<pair<const CWalletTx*, unsigned int> > setCoins;
int64 nValueIn = 0;
printf("CreateTransactionWithInputTx: SelectCoins(%s), nTotalValue = %s, nWtxinCredit = %s\n", FormatMoney(nTotalValue - nWtxinCredit).c_str(), FormatMoney(nTotalValue).c_str(), FormatMoney(nWtxinCredit).c_str());
if (nTotalValue - nWtxinCredit > 0)
{
if (!pwalletMain->SelectCoins(nTotalValue - nWtxinCredit, setCoins, nValueIn))
return false;
}
printf("CreateTransactionWithInputTx: selected %d tx outs, nValueIn = %s\n", setCoins.size(), FormatMoney(nValueIn).c_str());
vector<pair<const CWalletTx*, unsigned int> >
vecCoins(setCoins.begin(), setCoins.end());
BOOST_FOREACH(PAIRTYPE(const CWalletTx*, unsigned int)& coin, vecCoins)
{
int64 nCredit = coin.first->vout[coin.second].nValue;
dPriority += (double)nCredit * coin.first->GetDepthInMainChain();
}
// Input tx always at first position
vecCoins.insert(vecCoins.begin(), make_pair(&wtxIn, nTxOut));
nValueIn += nWtxinCredit;
dPriority += (double)nWtxinCredit * wtxIn.GetDepthInMainChain();
// Fill a vout back to self with any change
int64 nChange = nValueIn - nTotalValue;
if (nChange >= CENT)
{
// Note: We use a new key here to keep it from being obvious which side is the change.
// The drawback is that by not reusing a previous key, the change may be lost if a
// backup is restored, if the backup doesn't have the new private key for the change.
// If we reused the old key, it would be possible to add code to look for and
// rediscover unknown transactions that were written with keys of ours to recover
// post-backup change.
// Reserve a new key pair from key pool
vector<unsigned char> vchPubKey = reservekey.GetReservedKey();
assert(pwalletMain->HaveKey(vchPubKey));
// -------------- Fill a vout to ourself, using same address type as the payment
// Now sending always to hash160 (GetBitcoinAddressHash160 will return hash160, even if pubkey is used)
CScript scriptChange;
if (vecSend[0].first.GetBitcoinAddressHash160() != 0)
scriptChange.SetBitcoinAddress(vchPubKey);
else
scriptChange << vchPubKey << OP_CHECKSIG;
// Insert change txn at random position:
vector<CTxOut>::iterator position = wtxNew.vout.begin()+GetRandInt(wtxNew.vout.size());
wtxNew.vout.insert(position, CTxOut(nChange, scriptChange));
}
else
reservekey.ReturnKey();
// Fill vin
BOOST_FOREACH(PAIRTYPE(const CWalletTx*, unsigned int)& coin, vecCoins)
wtxNew.vin.push_back(CTxIn(coin.first->GetHash(), coin.second));
// Sign
int nIn = 0;
BOOST_FOREACH(PAIRTYPE(const CWalletTx*, unsigned int)& coin, vecCoins)
{
if (!SignSignature(*pwalletMain, *coin.first, wtxNew, nIn++))
return false;
}
// Limit size
unsigned int nBytes = ::GetSerializeSize(*(CTransaction*)&wtxNew, SER_NETWORK);
if (nBytes >= MAX_BLOCK_SIZE_GEN/5)
return false;
dPriority /= nBytes;
// Check that enough fee is included
int64 nPayFee = nTransactionFee * (1 + (int64)nBytes / 1000);
bool fAllowFree = CTransaction::AllowFree(dPriority);
int64 nMinFee = wtxNew.GetMinFee(1, fAllowFree);
if (nFeeRet < max(nPayFee, nMinFee))
{
nFeeRet = max(nPayFee, nMinFee);
printf("CreateTransactionWithInputTx: re-iterating (nFreeRet = %s)\n", FormatMoney(nFeeRet).c_str());
continue;
}
// Fill vtxPrev by copying from previous transactions vtxPrev
wtxNew.AddSupportingTransactions(txdb);
wtxNew.fTimeReceivedIsTxTime = true;
break;
}
}
}
printf("CreateTransactionWithInputTx succeeded:\n%s", wtxNew.ToString().c_str());
return true;
}
// nTxOut is the output from wtxIn that we should grab
// requires cs_main lock
string SendMoneyWithInputTx(const CScript& scriptPubKey, int64 nValue, int64 nNetFee, const CWalletTx& wtxIn, CWalletTx& wtxNew, bool fAskFee)
{
int nTxOut = IndexOfNameOutput(wtxIn);
CReserveKey reservekey(pwalletMain);
int64 nFeeRequired;
vector< pair<CScript, int64> > vecSend;
vecSend.push_back(make_pair(scriptPubKey, nValue));
if (nNetFee)
{
CScript scriptFee;
scriptFee << OP_RETURN;
vecSend.push_back(make_pair(scriptFee, nNetFee));
}
if (!CreateTransactionWithInputTx(vecSend, wtxIn, nTxOut, wtxNew, reservekey, nFeeRequired))
{
string strError;
if (nValue + nFeeRequired > pwalletMain->GetBalance())
strError = strprintf(_("Error: This transaction requires a transaction fee of at least %s because of its amount, complexity, or use of recently received funds "), FormatMoney(nFeeRequired).c_str());
else
strError = _("Error: Transaction creation failed ");
printf("SendMoney() : %s", strError.c_str());
return strError;
}
#ifdef GUI
if (fAskFee && !uiInterface.ThreadSafeAskFee(nFeeRequired))
return "ABORTED";
#else
if (fAskFee && !ThreadSafeAskFee(nFeeRequired, "611", NULL))
return "ABORTED";
#endif
if (!pwalletMain->CommitTransaction(wtxNew, reservekey))
return _("Error: The transaction was rejected. This might happen if some of the coins in your wallet were already spent, such as if you used a copy of wallet.dat and coins were spent in the copy but not marked as spent here.");
return "";
}
bool GetValueOfTxPos(const CNameIndex& txPos, vector<unsigned char>& vchValue, uint256& hash, int& nHeight)
{
nHeight = GetTxPosHeight(txPos);
vchValue = txPos.vValue;
CTransaction tx;
if (!tx.ReadFromDisk(txPos.txPos))
return error("GetValueOfTxPos() : could not read tx from disk");
hash = tx.GetHash();
return true;
}
bool GetValueOfTxPos(const CDiskTxPos& txPos, vector<unsigned char>& vchValue, uint256& hash, int& nHeight)
{
nHeight = GetTxPosHeight(txPos);
CTransaction tx;
if (!tx.ReadFromDisk(txPos))
return error("GetValueOfTxPos() : could not read tx from disk");
if (!GetValueOfNameTx(tx, vchValue))
return error("GetValueOfTxPos() : could not decode value from tx");
hash = tx.GetHash();
return true;
}
bool GetValueOfName(CNameDB& dbName, const vector<unsigned char> &vchName, vector<unsigned char>& vchValue, int& nHeight)
{
//vector<CDiskTxPos> vtxPos;
vector<CNameIndex> vtxPos;
if (!dbName.ReadName(vchName, vtxPos) || vtxPos.empty())
return false;
/*CDiskTxPos& txPos = vtxPos.back();
uint256 hash;
return GetValueOfTxPos(txPos, vchValue, hash, nHeight);*/
CNameIndex& txPos = vtxPos.back();
nHeight = txPos.nHeight;
vchValue = txPos.vValue;
return true;
}
bool GetTxOfName(CNameDB& dbName, const vector<unsigned char> &vchName, CTransaction& tx)
{
//vector<CDiskTxPos> vtxPos;
vector<CNameIndex> vtxPos;
if (!dbName.ReadName(vchName, vtxPos) || vtxPos.empty())
return false;
//CDiskTxPos& txPos = vtxPos.back();
CNameIndex& txPos = vtxPos.back();
//int nHeight = GetTxPosHeight(txPos);
int nHeight = txPos.nHeight;
if (nHeight + GetExpirationDepth(pindexBest->nHeight) < pindexBest->nHeight)
{
string name = stringFromVch(vchName);
printf("GetTxOfName(%s) : expired", name.c_str());
return false;
}
if (!tx.ReadFromDisk(txPos.txPos))
return error("GetTxOfName() : could not read tx from disk");
return true;
}
bool GetNameAddress(const CTransaction& tx, std::string& strAddress)
{
int op;
int nOut;
vector<vector<unsigned char> > vvch;
if (!DecodeNameTx(tx, op, nOut, vvch, BUG_WORKAROUND_BLOCK))
return false;
const CTxOut& txout = tx.vout[nOut];
const CScript& scriptPubKey = RemoveNameScriptPrefix(txout.scriptPubKey);
strAddress = scriptPubKey.GetBitcoinAddress();
return true;
}
bool GetNameAddress(const CDiskTxPos& txPos, std::string& strAddress)
{
CTransaction tx;
if (!tx.ReadFromDisk(txPos))
return error("GetNameAddress() : could not read tx from disk");
return GetNameAddress(tx, strAddress);
}
Value sendtoname(const Array& params, bool fHelp)
{
if (fHelp || params.size() < 2 || params.size() > 4)
throw runtime_error(
"sendtoname <611name> <amount> [comment] [comment-to]\n"
"<amount> is a real and is rounded to the nearest 0.01"
+ HelpRequiringPassphrase());
vector<unsigned char> vchName = vchFromValue(params[0]);
CNameDB dbName("r");
if (!dbName.ExistsName(vchName))
throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Name not found");
string strAddress;
CTransaction tx;
GetTxOfName(dbName, vchName, tx);
GetNameAddress(tx, strAddress);
uint160 hash160;
if (!AddressToHash160(strAddress, hash160))
throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "No valid 611 address");
// Amount
int64 nAmount = AmountFromValue(params[1]);
// Wallet comments
CWalletTx wtx;
if (params.size() > 2 && params[2].type() != null_type && !params[2].get_str().empty())
wtx.mapValue["comment"] = params[2].get_str();
if (params.size() > 3 && params[3].type() != null_type && !params[3].get_str().empty())
wtx.mapValue["to"] = params[3].get_str();
CRITICAL_BLOCK(cs_main)
{
EnsureWalletIsUnlocked();
string strError = pwalletMain->SendMoneyToBitcoinAddress(strAddress, nAmount, wtx);
if (strError != "")
throw JSONRPCError(RPC_WALLET_ERROR, strError);
}
return wtx.GetHash().GetHex();
}
Value name_list(const Array& params, bool fHelp)
{
if (fHelp || params.size() > 1)
throw runtime_error(
"name_list [<name>]\n"
"list my own names"
);
vchType vchNameUniq;
if (params.size () == 1)
vchNameUniq = vchFromValue (params[0]);
std::map<vchType, int> vNamesI;
std::map<vchType, Object> vNamesO;
CRITICAL_BLOCK(cs_main)
CRITICAL_BLOCK(pwalletMain->cs_mapWallet)
{
BOOST_FOREACH(PAIRTYPE(const uint256, CWalletTx)& item,
pwalletMain->mapWallet)
{
const CWalletTx& tx = item.second;
vchType vchName, vchValue;
int nOut;
if (!tx.GetNameUpdate (nOut, vchName, vchValue))
continue;
if(!vchNameUniq.empty () && vchNameUniq != vchName)
continue;
const int nHeight = tx.GetHeightInMainChain ();
if (nHeight == -1)
continue;
assert (nHeight >= 0);
// get last active name only
if (vNamesI.find (vchName) != vNamesI.end ()
&& vNamesI[vchName] > nHeight)
continue;
Object oName;
oName.push_back(Pair("name", stringFromVch(vchName)));
oName.push_back(Pair("value", stringFromVch(vchValue)));
if (!hooks->IsMine (tx))
oName.push_back(Pair("transferred", 1));
string strAddress = "";
GetNameAddress(tx, strAddress);
oName.push_back(Pair("address", strAddress));
const int expiresIn = nHeight + GetDisplayExpirationDepth (nHeight) - pindexBest->nHeight;
oName.push_back (Pair("expires_in", expiresIn));
if (expiresIn <= 0)
oName.push_back (Pair("expired", 1));
vNamesI[vchName] = nHeight;
vNamesO[vchName] = oName;
}
}
Array oRes;
BOOST_FOREACH(const PAIRTYPE(vector<unsigned char>, Object)& item, vNamesO)
oRes.push_back(item.second);
return oRes;
}
Value name_debug(const Array& params, bool fHelp)
{
if (fHelp || params.size() < 0)
throw runtime_error(
"name_debug\n"
"Dump pending transactions id in the debug file.\n");
printf("Pending:\n----------------------------\n");
pair<vector<unsigned char>, set<uint256> > pairPending;
CRITICAL_BLOCK(cs_main)
BOOST_FOREACH(pairPending, mapNamePending)
{
string name = stringFromVch(pairPending.first);
printf("%s :\n", name.c_str());
uint256 hash;
BOOST_FOREACH(hash, pairPending.second)
{
printf(" ");
if (!pwalletMain->mapWallet.count(hash))
printf("foreign ");
printf(" %s\n", hash.GetHex().c_str());
}
}
printf("----------------------------\n");
return true;
}
Value name_debug1(const Array& params, bool fHelp)
{
if (fHelp || params.size() < 1)
throw runtime_error(
"name_debug1 <name>\n"
"Dump name blocks number and transactions id in the debug file.\n");
vector<unsigned char> vchName = vchFromValue(params[0]);
printf("Dump name:\n");
CRITICAL_BLOCK(cs_main)
{
//vector<CDiskTxPos> vtxPos;
vector<CNameIndex> vtxPos;
CNameDB dbName("r");
if (!dbName.ReadName(vchName, vtxPos))
{
error("failed to read from name DB");
return false;
}
//CDiskTxPos txPos;
CNameIndex txPos;
BOOST_FOREACH(txPos, vtxPos)
{
CTransaction tx;
if (!tx.ReadFromDisk(txPos.txPos))
{
error("could not read txpos %s", txPos.txPos.ToString().c_str());
continue;
}
printf("@%d %s\n", GetTxPosHeight(txPos), tx.GetHash().GetHex().c_str());
}
}
printf("-------------------------\n");
return true;
}
Value name_show(const Array& params, bool fHelp)
{
if (fHelp || params.size() != 1)
throw runtime_error(
"name_show <name>\n"
"Show values of a name.\n"
);
Object oLastName;
vector<unsigned char> vchName = vchFromValue(params[0]);
string name = stringFromVch(vchName);
CRITICAL_BLOCK(cs_main)
{
//vector<CDiskTxPos> vtxPos;
vector<CNameIndex> vtxPos;
CNameDB dbName("r");
if (!dbName.ReadName(vchName, vtxPos))
throw JSONRPCError(RPC_WALLET_ERROR, "failed to read from name DB");
if (vtxPos.size() < 1)
throw JSONRPCError(RPC_WALLET_ERROR, "no result returned");
CDiskTxPos txPos = vtxPos[vtxPos.size() - 1].txPos;
CTransaction tx;
if (!tx.ReadFromDisk(txPos))
throw JSONRPCError(RPC_WALLET_ERROR, "failed to read from from disk");
Object oName;
vector<unsigned char> vchValue;
int nHeight;
uint256 hash;
if (!txPos.IsNull() && GetValueOfTxPos(txPos, vchValue, hash, nHeight))
{
oName.push_back(Pair("name", name));
string value = stringFromVch(vchValue);
oName.push_back(Pair("value", value));
oName.push_back(Pair("txid", tx.GetHash().GetHex()));
string strAddress = "";
GetNameAddress(txPos, strAddress);
oName.push_back(Pair("address", strAddress));
oName.push_back(Pair("expires_in", nHeight + GetDisplayExpirationDepth(nHeight) - pindexBest->nHeight));
if(nHeight + GetDisplayExpirationDepth(nHeight) - pindexBest->nHeight <= 0)
{
oName.push_back(Pair("expired", 1));
}
oLastName = oName;
}
}
return oLastName;
}
Value name_history(const Array& params, bool fHelp)
{
if (fHelp || params.size() != 1)
throw runtime_error(
"name_history <name>\n"
"List all name values of a name.\n");
Array oRes;
vector<unsigned char> vchName = vchFromValue(params[0]);
string name = stringFromVch(vchName);
CRITICAL_BLOCK(cs_main)
{
//vector<CDiskTxPos> vtxPos;
vector<CNameIndex> vtxPos;
CNameDB dbName("r");
if (!dbName.ReadName(vchName, vtxPos))
throw JSONRPCError(RPC_WALLET_ERROR, "failed to read from name DB");
CNameIndex txPos2;
CDiskTxPos txPos;
BOOST_FOREACH(txPos2, vtxPos)
{
txPos = txPos2.txPos;
CTransaction tx;
if (!tx.ReadFromDisk(txPos))
{
error("could not read txpos %s", txPos.ToString().c_str());
continue;
}
Object oName;
vector<unsigned char> vchValue;
int nHeight;
uint256 hash;
if (!txPos.IsNull() && GetValueOfTxPos(txPos, vchValue, hash, nHeight))
{
oName.push_back(Pair("name", name));
string value = stringFromVch(vchValue);
oName.push_back(Pair("value", value));
oName.push_back(Pair("txid", tx.GetHash().GetHex()));
string strAddress = "";
GetNameAddress(txPos, strAddress);
oName.push_back(Pair("address", strAddress));
oName.push_back(Pair("expires_in", nHeight + GetDisplayExpirationDepth(nHeight) - pindexBest->nHeight));
if(nHeight + GetDisplayExpirationDepth(nHeight) - pindexBest->nHeight <= 0)
{
oName.push_back(Pair("expired", 1));
}
oRes.push_back(oName);
}
}
}
return oRes;
}
Value name_filter(const Array& params, bool fHelp)
{
if (fHelp || params.size() > 5)
throw runtime_error(
"name_filter [[[[[regexp] maxage=36000] from=0] nb=0] stat]\n"
"scan and filter names\n"
"[regexp] : apply [regexp] on names, empty means all names\n"
"[maxage] : look in last [maxage] blocks\n"
"[from] : show results from number [from]\n"
"[nb] : show [nb] results, 0 means all\n"
"[stats] : show some stats instead of results\n"
"name_filter \"\" 5 # list names updated in last 5 blocks\n"
"name_filter \"^id/\" # list all names from the \"id\" namespace\n"
"name_filter \"^id/\" 36000 0 0 stat # display stats (number of names) on active names from the \"id\" namespace\n"
);
string strRegexp;
int nFrom = 0;
int nNb = 0;
int nMaxAge = 36000;
bool fStat = false;
int nCountFrom = 0;
int nCountNb = 0;
if (params.size() > 0)
strRegexp = params[0].get_str();
if (params.size() > 1)
nMaxAge = params[1].get_int();
if (params.size() > 2)
nFrom = params[2].get_int();
if (params.size() > 3)
nNb = params[3].get_int();
if (params.size() > 4)
fStat = (params[4].get_str() == "stat" ? true : false);
CNameDB dbName("r");
Array oRes;
vector<unsigned char> vchName;
vector<pair<vector<unsigned char>, CNameIndex> > nameScan;
if (!dbName.ScanNames(vchName, 100000000, nameScan))
throw JSONRPCError(RPC_WALLET_ERROR, "scan failed");
// compile regex once
using namespace boost::xpressive;
smatch nameparts;
sregex cregex = sregex::compile(strRegexp);
pair<vector<unsigned char>, CNameIndex> pairScan;
BOOST_FOREACH(pairScan, nameScan)
{
string name = stringFromVch(pairScan.first);
// regexp
if(strRegexp != "" && !regex_search(name, nameparts, cregex))
continue;
CNameIndex txName = pairScan.second;
int nHeight = txName.nHeight;
// max age
if(nMaxAge != 0 && pindexBest->nHeight - nHeight >= nMaxAge)
continue;
// from limits
nCountFrom++;
if(nCountFrom < nFrom + 1)
continue;
Object oName;
if (!fStat) {
oName.push_back(Pair("name", name));
int nExpiresIn = nHeight + GetDisplayExpirationDepth(nHeight) - pindexBest->nHeight;
if (nExpiresIn <= 0)
{
oName.push_back(Pair("expired", 1));
}
else
{
string value = stringFromVch(txName.vValue);
oName.push_back(Pair("value", value));
oName.push_back(Pair("expires_in", nExpiresIn));
}
}
oRes.push_back(oName);
nCountNb++;
// nb limits
if(nNb > 0 && nCountNb >= nNb)
break;
}
if (NAME_DEBUG) {
dbName.test();
}
if (fStat)
{
Object oStat;
oStat.push_back(Pair("blocks", (int)nBestHeight));
oStat.push_back(Pair("count", (int)oRes.size()));
//oStat.push_back(Pair("sha256sum", SHA256(oRes), true));
return oStat;
}
return oRes;
}
Value name_scan(const Array& params, bool fHelp)
{
if (fHelp || params.size() > 2)
throw runtime_error(
"name_scan [<start-name>] [<max-returned>]\n"
"scan all names, starting at start-name and returning a maximum number of entries (default 500)\n"
);
vector<unsigned char> vchName;
int nMax = 500;
if (params.size() > 0)
{
vchName = vchFromValue(params[0]);
}
if (params.size() > 1)
{
Value vMax = params[1];
ConvertTo<double>(vMax);
nMax = (int)vMax.get_real();
}
CNameDB dbName("r");
Array oRes;
//vector<pair<vector<unsigned char>, CDiskTxPos> > nameScan;
vector<pair<vector<unsigned char>, CNameIndex> > nameScan;
if (!dbName.ScanNames(vchName, nMax, nameScan))
throw JSONRPCError(RPC_WALLET_ERROR, "scan failed");
//pair<vector<unsigned char>, CDiskTxPos> pairScan;
pair<vector<unsigned char>, CNameIndex> pairScan;
BOOST_FOREACH(pairScan, nameScan)
{
Object oName;
string name = stringFromVch(pairScan.first);
oName.push_back(Pair("name", name));
//vector<unsigned char> vchValue;
CTransaction tx;
CNameIndex txName = pairScan.second;
CDiskTxPos txPos = txName.txPos;
//CDiskTxPos txPos = pairScan.second;
//int nHeight = GetTxPosHeight(txPos);
int nHeight = txName.nHeight;
vector<unsigned char> vchValue = txName.vValue;
if ((nHeight + GetDisplayExpirationDepth(nHeight) - pindexBest->nHeight <= 0)
|| txPos.IsNull()