-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
1525 lines (1448 loc) · 51.9 KB
/
main.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
#include <boost/python.hpp>
namespace python = boost::python;
#include <bitcoin/bitcoin.hpp>
namespace ph = std::placeholders;
#include "src/primitive.h"
template<class T>
class std_item
{
public:
typedef typename T::value_type V;
static T create_array(boost::python::list l)
{
T result;
for (size_t i = 0; i < result.size() && i < len(l); ++i)
result[i] = python::extract<typename T::value_type>(l[i]);
return result;
}
static T create_vector(boost::python::list passed)
{
T result;
for (size_t i = 0; i < len(passed); ++i)
result.push_back(
python::extract<typename T::value_type>(passed[i]));
return result;
}
static V get(const T& x, int i)
{
if (!is_valid_index(i, x.size()))
index_error();
return x[i];
}
static void set(T& x, int i, const V& v)
{
if (!is_valid_index(i, x.size()))
index_error();
x[i] = v;
}
static void del(T& x, int i)
{
if (!is_valid_index(i, x.size()))
index_error();
x.erase(x.begin() + i);
}
static void add(T& x, const V& v)
{
x.push_back(v);
}
private:
static void index_error()
{
PyErr_SetString(PyExc_IndexError, "Index out of range");
throw boost::python::error_already_set();
}
static bool is_valid_index(int& idx, size_t size)
{
if (idx < 0)
idx += size;
if (idx >= 0 && idx < static_cast<int>(size))
return true;
return false;
}
};
class error_code_wrapper
{
public:
static bool nonzero(const std::error_code& ec)
{
return (bool)ec;
}
static bool eq(const std::error_code& eca, bc::error::error_code_t err)
{
return eca == err;
}
static bool ne(const std::error_code& eca, bc::error::error_code_t err)
{
return eca != err;
}
};
class atomic_counter_wrapper
{
public:
atomic_counter_wrapper(size_t initial_value)
{
counter_ = std::make_shared<bc::atomic_counter>(initial_value);
}
void increment()
{
++(*counter_);
}
size_t count() const
{
return *counter_;
}
private:
bc::atomic_counter_ptr counter_;
};
struct async_service_wrapper
{
async_service_wrapper()
{
s = std::make_shared<bc::async_service>();
}
async_service_wrapper(size_t n)
{
s = std::make_shared<bc::async_service>(n);
}
void spawn()
{
s->spawn();
}
void shutdown()
{
s->shutdown();
}
bc::async_service_ptr s;
};
class acceptor_wrapper
{
public:
acceptor_wrapper(bc::acceptor_ptr accept)
: accept_(accept)
{
}
void accept(python::object handle_accept)
{
accept_->accept(
std::bind(&acceptor_wrapper::call_handle_accept,
ph::_1, ph::_2, handle_accept));
}
private:
static void call_handle_accept(const std::error_code& ec,
bc::channel_ptr node, python::object handle_accept)
{
handle_accept(ec, node);
}
bc::acceptor_ptr accept_;
};
class channel_wrapper
{
public:
channel_wrapper(bc::channel_ptr node)
: node_(node)
{
}
void stop()
{
node_->stop();
}
bool stopped() const
{
return node_->stopped();
}
template <typename Message>
void send(const Message& packet, python::object handle_send)
{
node_->send(packet, pyfunction<const std::error_code&>(handle_send));
}
void send_raw(const bc::message::header& packet,
const bc::data_chunk& payload, python::object handle_send)
{
node_->send_raw(packet, payload,
pyfunction<const std::error_code&>(handle_send));
}
void subscribe_version(python::object handle_receive)
{
node_->subscribe_version(pyfunction<const std::error_code&,
const bc::message::version&>(handle_receive));
}
void subscribe_verack(python::object handle_receive)
{
node_->subscribe_verack(pyfunction<const std::error_code&,
const bc::message::verack&>(handle_receive));
}
void subscribe_address(python::object handle_receive)
{
node_->subscribe_address(pyfunction<const std::error_code&,
const bc::message::address&>(handle_receive));
}
void subscribe_get_address(python::object handle_receive)
{
node_->subscribe_get_address(pyfunction<const std::error_code&,
const bc::message::get_address&>(handle_receive));
}
void subscribe_inventory(python::object handle_receive)
{
node_->subscribe_inventory(pyfunction<const std::error_code&,
const bc::message::inventory&>(handle_receive));
}
void subscribe_get_data(python::object handle_receive)
{
node_->subscribe_get_data(pyfunction<const std::error_code&,
const bc::message::get_data&>(handle_receive));
}
void subscribe_get_blocks(python::object handle_receive)
{
node_->subscribe_get_blocks(pyfunction<const std::error_code&,
const bc::message::get_blocks&>(handle_receive));
}
void subscribe_transaction(python::object handle_receive)
{
node_->subscribe_transaction(pyfunction<const std::error_code&,
const bc::message::transaction&>(handle_receive));
}
void subscribe_block(python::object handle_receive)
{
node_->subscribe_block(pyfunction<const std::error_code&,
const bc::message::block&>(handle_receive));
}
void subscribe_raw(python::object handle_receive)
{
node_->subscribe_raw(pyfunction<
const std::error_code&, const bc::message::header&,
const bc::data_chunk&>(handle_receive));
}
bc::channel_ptr channel() const
{
return node_;
}
private:
bc::channel_ptr node_;
};
class network_wrapper
{
public:
network_wrapper(async_service_wrapper service)
{
net_ = std::make_shared<bc::network>(*service.s);
}
void listen(uint16_t port, python::object handle_listen)
{
net_->listen(port,
std::bind(&network_wrapper::post_listen,
ph::_1, ph::_2, handle_listen));
}
void connect(const std::string& hostname, uint16_t port,
python::object handle_connect)
{
net_->connect(hostname, port,
std::bind(&network_wrapper::post_connect,
ph::_1, ph::_2, handle_connect));
}
bc::network_ptr net() const
{
return net_;
}
private:
static void post_listen(const std::error_code& ec,
bc::acceptor_ptr accept, python::object handle_listen)
{
pyfunction<const std::error_code&,
acceptor_wrapper> f(handle_listen);
f(ec, acceptor_wrapper(accept));
}
static void post_connect(const std::error_code& ec,
bc::channel_ptr node, python::object handle_connect)
{
pyfunction<const std::error_code&,
channel_wrapper> f(handle_connect);
f(ec, channel_wrapper(node));
}
bc::network_ptr net_;
};
class handshake_wrapper
{
public:
handshake_wrapper(async_service_wrapper service)
{
hs_ = std::make_shared<bc::handshake>(*service.s);
}
void start(python::object handle_start)
{
hs_->start(
pyfunction<const std::error_code&>(handle_start));
}
void connect(network_wrapper net_wrap, const std::string& hostname,
uint16_t port, python::object handle_connect)
{
hs_->connect(net_wrap.net(), hostname, port,
std::bind(&handshake_wrapper::post_connect,
ph::_1, ph::_2, handle_connect));
}
void ready(channel_wrapper node, python::object handle_handshake)
{
hs_->ready(node.channel(),
pyfunction<const std::error_code&>(handle_handshake));
}
void discover_external_ip(python::object handle_discover)
{
hs_->discover_external_ip(
pyfunction<const std::error_code&,
const bc::message::ip_address&>(handle_discover));
}
void fetch_network_address(python::object handle_fetch)
{
hs_->fetch_network_address(
pyfunction<const std::error_code&,
const bc::message::network_address&>(handle_fetch));
}
void set_port(uint16_t port, python::object handle_set)
{
hs_->set_port(port,
pyfunction<const std::error_code&>(handle_set));
}
void set_user_agent(const std::string& user_agent,
python::object handle_set)
{
hs_->set_user_agent(user_agent,
pyfunction<const std::error_code&>(handle_set));
}
bc::handshake_ptr hs()
{
return hs_;
}
private:
static void post_connect(const std::error_code& ec,
bc::channel_ptr node, python::object handle_connect)
{
pyfunction<const std::error_code&,
channel_wrapper> f(handle_connect);
f(ec, channel_wrapper(node));
}
bc::handshake_ptr hs_;
};
class hosts_wrapper
{
public:
hosts_wrapper(async_service_wrapper service)
{
hosts_ = std::make_shared<bc::hosts>(*service.s);
}
void load(const std::string& filename, python::object handle_load)
{
hosts_->load(filename, pyfunction<const std::error_code&>(handle_load));
}
void save(const std::string& filename, python::object handle_save)
{
hosts_->save(filename, pyfunction<const std::error_code&>(handle_save));
}
void store(const bc::message::network_address& address,
python::object handle_store)
{
hosts_->store(address,
pyfunction<const std::error_code&>(handle_store));
}
void remove(const bc::message::network_address& address,
python::object handle_remove)
{
hosts_->store(address,
pyfunction<const std::error_code&>(handle_remove));
}
void fetch_address(python::object handle_fetch)
{
hosts_->fetch_address(
pyfunction<const std::error_code&,
const bc::message::network_address&>(handle_fetch));
}
void fetch_count(python::object handle_fetch)
{
hosts_->fetch_count(
pyfunction<const std::error_code&, size_t>(handle_fetch));
}
bc::hosts_ptr hsts()
{
return hosts_;
}
private:
bc::hosts_ptr hosts_;
};
class protocol_wrapper
{
public:
protocol_wrapper(async_service_wrapper service,
hosts_wrapper h, handshake_wrapper hs, network_wrapper n)
{
protocol_ = std::make_shared<bc::protocol>(*service.s,
h.hsts(), hs.hs(), n.net());
}
void start(python::object handle_complete)
{
protocol_->start(
pyfunction<const std::error_code&>(handle_complete));
}
void stop(python::object handle_complete)
{
protocol_->stop(
pyfunction<const std::error_code&>(handle_complete));
}
void bootstrap(python::object handle_complete)
{
protocol_->bootstrap(
pyfunction<const std::error_code&>(handle_complete));
}
void run()
{
protocol_->run();
}
void fetch_connection_count(python::object handle_fetch)
{
protocol_->fetch_connection_count(
pyfunction<const std::error_code&, size_t>(handle_fetch));
}
void subscribe_channel(python::object handle_channel)
{
protocol_->subscribe_channel(
std::bind(&protocol_wrapper::post_new_channel,
ph::_1, handle_channel));
}
template <typename Message>
void broadcast(const Message& packet)
{
protocol_->broadcast(packet);
}
bc::protocol_ptr prot()
{
return protocol_;
}
private:
static void post_new_channel(bc::channel_ptr node,
python::object handle_channel)
{
pyfunction<channel_wrapper> f(handle_channel);
f(channel_wrapper(node));
}
bc::protocol_ptr protocol_;
};
template <typename ListType>
std::string raw_list(const ListType& listobj)
{
return std::string(listobj.begin(), listobj.end());
}
template <typename HashType>
void set_raw_hash(HashType& hashobj, const std::string& raw_repr)
{
for (size_t i = 0; i < hashobj.size() && i < raw_repr.size(); ++i)
hashobj[i] = raw_repr[i];
}
void set_raw_data_chunk(bc::data_chunk& d, const std::string& raw_repr)
{
d.resize(raw_repr.size());
std::copy(raw_repr.begin(), raw_repr.end(), d.begin());
}
template <typename ListType, typename ClassType>
ClassType& extend_vector(ClassType& pyclass)
{
pyclass
.def("__len__", &ListType::size)
.def("clear", &ListType::clear)
.def("append", &std_item<ListType>::add,
python::with_custodian_and_ward<1, 2>())
.def("__getitem__", &std_item<ListType>::get)
.def("__setitem__", &std_item<ListType>::set,
python::with_custodian_and_ward<1, 2>())
.def("__delitem__", &std_item<ListType>::del)
;
return pyclass;
}
int point_cmp(const bc::message::output_point& a,
const bc::message::output_point& b)
{
if (a.index < b.index)
return -1;
else if (a.index > b.index)
return 1;
// a.index == b.index
if (a.hash < b.hash)
return -1;
else if (a.hash > b.hash)
return 1;
return 0;
}
template <typename HashType>
static int hash_cmp(const HashType& h, python::object other)
{
python::extract<const HashType&> ohe(other);
if (ohe.check())
{
const HashType& oh = ohe();
if (h < oh)
return -1;
else if (h > oh)
return 1;
return 0;
}
else
{
std::string other_repr = python::extract<std::string>(other);
std::string h_repr = bc::pretty_hex(h);
if (h_repr < other_repr)
return -1;
else if (h_repr > other_repr)
return 1;
BITCOIN_ASSERT(h_repr == other_repr);
return 0;
}
//return bc::pretty_hex(h) == other_repr;
}
static bool hash_digest_nonzero(const bc::hash_digest& h)
{
return h != bc::null_hash;
}
static bool short_hash_nonzero(const bc::short_hash& h)
{
return h != bc::null_short_hash;
}
template <typename HashType>
static int hash__hash__(const HashType& h)
{
return bc::cast_chunk<int>(bc::data_chunk(h.end() - sizeof(int), h.end()));
}
template <typename HashType, typename ClassType>
ClassType& extend_hash(ClassType& pyclass)
{
pyclass
.def("__repr__", bc::pretty_hex<HashType>)
.def("__str__", bc::pretty_hex<HashType>)
.def("__len__", &HashType::size)
.def("__getitem__", &std_item<HashType>::get)
.def("__setitem__", &std_item<HashType>::set)
.def("__cmp__", hash_cmp<HashType>)
.def("__hash__", hash__hash__<HashType>)
.add_property("raw", raw_list<HashType>, set_raw_hash<HashType>)
;
return pyclass;
}
class network_address_wrapper
{
public:
static python::list get_ip(const bc::message::network_address& net)
{
python::list result;
for (const uint8_t& v: net.ip)
result.append(v);
return result;
}
static void set_ip(bc::message::network_address& net, python::list passed)
{
for (size_t i = 0; i < net.ip.size() && i < (size_t)len(passed); ++i)
net.ip[i] = python::extract<uint8_t>(passed[i]);
}
};
enum class sighash_wrapper
{
all = bc::sighash::all,
none = bc::sighash::none,
single = bc::sighash::single,
anyone_can_pay = bc::sighash::anyone_can_pay
};
class script_wrapper
{
public:
// boost python doesnt like the constref return type
static bc::operation_stack operations(const bc::script& s)
{
return s.operations();
}
static bool run(bc::script& a, bc::script scrin,
bc::message::transaction tx, uint32_t inidx)
{
return a.run(scrin, tx, inidx);
}
};
class exporter_wrapper
{
public:
exporter_wrapper(bc::exporter_ptr ex)
: ex_(ex)
{
}
bc::data_chunk save_header(const bc::message::header& pkt) const
{
return ex_->save(pkt);
}
bc::data_chunk save_version(const bc::message::version& pkt) const
{
return ex_->save(pkt);
}
bc::data_chunk save_verack(const bc::message::verack& pkt) const
{
return ex_->save(pkt);
}
bc::data_chunk save_address(const bc::message::address& pkt) const
{
return ex_->save(pkt);
}
bc::data_chunk save_get_address(const bc::message::get_address& pkt) const
{
return ex_->save(pkt);
}
bc::data_chunk save_inventory(const bc::message::inventory& pkt) const
{
return ex_->save(pkt);
}
bc::data_chunk save_get_data(const bc::message::get_data& pkt) const
{
return ex_->save(pkt);
}
bc::data_chunk save_get_blocks(const bc::message::get_blocks& pkt) const
{
return ex_->save(pkt);
}
bc::data_chunk save_transaction(const bc::message::transaction& pkt) const
{
return ex_->save(pkt);
}
bc::data_chunk save_block(const bc::message::block& pkt) const
{
return ex_->save(pkt);
}
bc::data_chunk save_ping(const bc::message::ping& pkt) const
{
return ex_->save(pkt);
}
bc::message::header load_header(const bc::data_chunk& stream) const
{
return ex_->load_header(stream);
}
bc::message::version load_version(const bc::data_chunk& stream) const
{
return ex_->load_version(stream);
}
bc::message::verack load_verack(const bc::data_chunk& stream) const
{
return ex_->load_verack(stream);
}
bc::message::address load_address(const bc::data_chunk& stream) const
{
return ex_->load_address(stream);
}
bc::message::get_address load_get_address(const bc::data_chunk& stream) const
{
return ex_->load_get_address(stream);
}
bc::message::inventory load_inventory(const bc::data_chunk& stream) const
{
return ex_->load_inventory(stream);
}
bc::message::get_data load_get_data(const bc::data_chunk& stream) const
{
return ex_->load_get_data(stream);
}
bc::message::get_blocks load_get_blocks(const bc::data_chunk& stream) const
{
return ex_->load_get_blocks(stream);
}
bc::message::transaction load_transaction(
const bc::data_chunk& stream) const
{
return ex_->load_transaction(stream);
}
bc::message::block load_block(const bc::data_chunk& stream) const
{
return ex_->load_block(stream);
}
bc::message::ping load_ping(const bc::data_chunk& stream) const
{
return ex_->load_ping(stream);
}
bool verify_header(const bc::message::header& header_msg) const
{
return ex_->verify_header(header_msg);
}
private:
bc::exporter_ptr ex_;
};
exporter_wrapper create_satoshi_exporter()
{
return exporter_wrapper(std::make_shared<bc::satoshi_exporter>());
}
class blockchain_wrapper
{
public:
blockchain_wrapper(bc::blockchain_ptr chain)
: chain_(chain)
{
}
void store(const bc::message::block& blk, python::object handle_store)
{
chain_->store(blk,
pyfunction<const std::error_code&, bc::block_info>(handle_store));
}
void fetch_block_header_by_depth(size_t depth, python::object handle_fetch)
{
chain_->fetch_block_header(depth,
pyfunction<const std::error_code&,
const bc::message::block&>(handle_fetch));
}
void fetch_block_header_by_hash(const bc::hash_digest& block_hash,
python::object handle_fetch)
{
chain_->fetch_block_header(block_hash,
pyfunction<const std::error_code&,
const bc::message::block&>(handle_fetch));
}
void fetch_block_transaction_hashes_by_depth(size_t depth,
python::object handle_fetch)
{
chain_->fetch_block_transaction_hashes(depth,
pyfunction<const std::error_code&,
const bc::message::inventory_list&>(handle_fetch));
}
void fetch_block_transaction_hashes_by_hash(
const bc::hash_digest& block_hash, python::object handle_fetch)
{
chain_->fetch_block_transaction_hashes(block_hash,
pyfunction<const std::error_code&,
const bc::message::inventory_list&>(handle_fetch));
}
void fetch_block_depth(const bc::hash_digest& block_hash,
python::object handle_fetch)
{
chain_->fetch_block_depth(block_hash,
pyfunction<const std::error_code&, size_t>(handle_fetch));
}
void fetch_last_depth(python::object handle_fetch)
{
chain_->fetch_last_depth(
pyfunction<const std::error_code&, size_t>(handle_fetch));
}
void fetch_block_locator(python::object handle_fetch)
{
chain_->fetch_block_locator(
pyfunction<const std::error_code&,
const bc::message::block_locator&>(handle_fetch));
}
void fetch_transaction(const bc::hash_digest& transaction_hash,
python::object handle_fetch)
{
chain_->fetch_transaction(transaction_hash,
pyfunction<const std::error_code&,
const bc::message::transaction&>(handle_fetch));
}
void fetch_transaction_index(
const bc::hash_digest& transaction_hash,
python::object handle_fetch)
{
chain_->fetch_transaction_index(transaction_hash,
pyfunction<const std::error_code&,
size_t, size_t>(handle_fetch));
}
void fetch_spend(const bc::message::output_point& outpoint,
python::object handle_fetch)
{
chain_->fetch_spend(outpoint,
pyfunction<const std::error_code&,
const bc::message::input_point&>(handle_fetch));
}
void fetch_outputs(const bc::payment_address& address,
python::object handle_fetch)
{
chain_->fetch_outputs(address,
pyfunction<const std::error_code&,
const bc::message::output_point_list&>(handle_fetch));
}
void subscribe_reorganize(python::object handle_reorganize)
{
chain_->subscribe_reorganize(
std::bind(&blockchain_wrapper::call_handle_reorganize,
ph::_1, ph::_2, ph::_3, ph::_4, handle_reorganize));
}
bc::blockchain_ptr chain()
{
return chain_;
}
private:
static void call_handle_reorganize(const std::error_code& ec,
size_t fork_point,
const bc::blockchain::block_list& arrivals,
const bc::blockchain::block_list& replaced,
python::object handle_reorganize)
{
python::list py_arrivals, py_replaced;
for (auto blk: arrivals)
py_arrivals.append(*blk);
for (auto blk: replaced)
py_replaced.append(*blk);
pyfunction<const std::error_code&, size_t, python::list, python::list>
f(handle_reorganize);
f(ec, fork_point, py_arrivals, py_replaced);
}
bc::blockchain_ptr chain_;
};
void fetch_block_by_depth(blockchain_wrapper chain, size_t depth,
python::object handle_fetch)
{
bc::fetch_block(chain.chain(), depth,
pyfunction<const std::error_code&,
const bc::message::block&>(handle_fetch));
}
void fetch_block_by_hash(blockchain_wrapper chain,
const bc::hash_digest& block_hash, python::object handle_fetch)
{
bc::fetch_block(chain.chain(), block_hash,
pyfunction<const std::error_code&,
const bc::message::block&>(handle_fetch));
}
void fetch_block_locator(blockchain_wrapper chain,
python::object handle_fetch)
{
bc::fetch_block_locator(chain.chain(),
pyfunction<const std::error_code&,
const bc::message::block_locator&>(handle_fetch));
}
void handle_bdb_blockchain_start(const std::error_code& ec,
bc::blockchain_ptr chain, python::object handle_start)
{
pyfunction<const std::error_code&, blockchain_wrapper> f(handle_start);
f(ec, blockchain_wrapper(chain));
}
blockchain_wrapper create_bdb_blockchain(async_service_wrapper service,
const std::string& prefix, python::object handle_start)
{
return blockchain_wrapper(
bc::bdb_blockchain::create(*service.s, prefix,
std::bind(handle_bdb_blockchain_start,
ph::_1, ph::_2, handle_start)));
}
bool setup_bdb_blockchain(const std::string& prefix)
{
return bc::bdb_blockchain::setup(prefix);
}
std::string pretty_input_point(const bc::message::input_point& inpoint)
{
return bc::pretty_hex(inpoint.hash) + ":" +
boost::lexical_cast<std::string>(inpoint.index);
}
class poller_wrapper
{
public:
poller_wrapper(async_service_wrapper srv, blockchain_wrapper chain)
{
poll_ = std::make_shared<bc::poller>(*srv.s, chain.chain());
}
void query(channel_wrapper node)
{
poll_->query(node.channel());
}
bc::poller_ptr p()
{
return poll_;
}
private:
bc::poller_ptr poll_;
};
void disable_logging()
{
bc::log_debug().alias(bc::log_level::debug, bc::log_level::null);
bc::log_debug().alias(bc::log_level::info, bc::log_level::null);
}
class transaction_pool_wrapper
{
public:
transaction_pool_wrapper(async_service_wrapper service,
blockchain_wrapper b)
{
pool_ = bc::transaction_pool::create(*service.s, b.chain());
}
bc::transaction_pool_ptr p()
{
return pool_;
}
void store(const bc::message::transaction& tx,
python::object handle_confirm, python::object handle_store)
{
pool_->store(tx,
pyfunction<const std::error_code&>(handle_confirm),
pyfunction<const std::error_code&,
const bc::index_list&>(handle_store));
}
void fetch(const bc::hash_digest& tx_hash,
python::object handle_fetch)
{
pool_->fetch(tx_hash,
pyfunction<const std::error_code&,
const bc::message::transaction&>(handle_fetch));
}
void exists(const bc::hash_digest& tx_hash,
python::object handle_exists)
{
pool_->exists(tx_hash,
pyfunction<bool>(handle_exists));
}
private:
bc::transaction_pool_ptr pool_;
};
class session_wrapper
{
public:
session_wrapper(
async_service_wrapper service,
hosts_wrapper hsts,
handshake_wrapper h,
network_wrapper n,
protocol_wrapper pp,
blockchain_wrapper chain,
poller_wrapper pl,
transaction_pool_wrapper tp)
{
bc::session_params p;
p.hosts_ = hsts.hsts();
p.handshake_ = h.hs();
p.network_ = n.net();
p.protocol_ = pp.prot();
p.poller_ = pl.p();
p.blockchain_ = chain.chain();
p.transaction_pool_ = tp.p();
session_ = std::make_shared<bc::session>(*service.s, p);
}
void start(python::object handle_complete)
{
session_->start(
pyfunction<const std::error_code&>(handle_complete));
}
void stop(python::object handle_complete)
{
session_->stop(
pyfunction<const std::error_code&>(handle_complete));
}
private:
bc::session_ptr session_;
};
bc::short_hash payment_address_hash(const bc::payment_address& pa)
{
return pa.hash();
}
BOOST_PYTHON_MODULE(_bitcoin)
{