1
+ # MIT License
2
+ #
3
+ # Copyright (c) 2018 Evgeny Medvedev, [email protected]
4
+ #
5
+ # Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ # of this software and associated documentation files (the "Software"), to deal
7
+ # in the Software without restriction, including without limitation the rights
8
+ # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ # copies of the Software, and to permit persons to whom the Software is
10
+ # furnished to do so, subject to the following conditions:
11
+ #
12
+ # The above copyright notice and this permission notice shall be included in all
13
+ # copies or substantial portions of the Software.
14
+ #
15
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ # SOFTWARE.
22
+
23
+
24
+ import logging
25
+
26
+ from bitcoinetl .enumeration .chain import Chain
27
+ from bitcoinetl .jobs .enrich_transactions import EnrichTransactionsJob
28
+ from bitcoinetl .jobs .export_blocks_job import ExportBlocksJob
29
+ from bitcoinetl .service .btc_service import BtcService
30
+ from blockchainetl .jobs .exporters .console_item_exporter import ConsoleItemExporter
31
+ from blockchainetl .jobs .exporters .in_memory_item_exporter import InMemoryItemExporter
32
+
33
+
34
+ class BtcStreamerAdapter :
35
+ def __init__ (
36
+ self ,
37
+ bitcoin_rpc ,
38
+ item_exporter = ConsoleItemExporter (),
39
+ chain = Chain .BITCOIN ,
40
+ batch_size = 2 ,
41
+ max_workers = 5 ):
42
+ self .bitcoin_rpc = bitcoin_rpc
43
+ self .chain = chain
44
+ self .btc_service = BtcService (bitcoin_rpc , chain )
45
+ self .item_exporter = item_exporter
46
+ self .batch_size = batch_size
47
+ self .max_workers = max_workers
48
+
49
+ def open (self ):
50
+ self .item_exporter .open ()
51
+
52
+ def get_current_block_number (self ):
53
+ return int (self .btc_service .get_latest_block ().number )
54
+
55
+ def export_all (self , start_block , end_block ):
56
+ # Export blocks and transactions
57
+ blocks_and_transactions_item_exporter = InMemoryItemExporter (item_types = ['block' , 'transaction' ])
58
+
59
+ blocks_and_transactions_job = ExportBlocksJob (
60
+ start_block = start_block ,
61
+ end_block = end_block ,
62
+ batch_size = self .batch_size ,
63
+ bitcoin_rpc = self .bitcoin_rpc ,
64
+ max_workers = self .max_workers ,
65
+ item_exporter = blocks_and_transactions_item_exporter ,
66
+ chain = self .chain ,
67
+ export_blocks = True ,
68
+ export_transactions = True
69
+ )
70
+ blocks_and_transactions_job .run ()
71
+
72
+ blocks = blocks_and_transactions_item_exporter .get_items ('block' )
73
+ transactions = blocks_and_transactions_item_exporter .get_items ('transaction' )
74
+
75
+ # Enrich transactions
76
+ enriched_transactions_item_exporter = InMemoryItemExporter (item_types = ['transaction' ])
77
+
78
+ enrich_transactions_job = EnrichTransactionsJob (
79
+ transactions_iterable = transactions ,
80
+ batch_size = self .batch_size ,
81
+ bitcoin_rpc = self .bitcoin_rpc ,
82
+ max_workers = self .max_workers ,
83
+ item_exporter = enriched_transactions_item_exporter ,
84
+ chain = self .chain
85
+ )
86
+ enrich_transactions_job .run ()
87
+ enriched_transactions = enriched_transactions_item_exporter .get_items ('transaction' )
88
+ if len (enriched_transactions ) != len (transactions ):
89
+ raise ValueError ('The number of transactions is wrong ' + str (transactions ))
90
+
91
+ logging .info ('Exporting with ' + type (self .item_exporter ).__name__ )
92
+ self .item_exporter .export_items (blocks + enriched_transactions )
93
+
94
+ def close (self ):
95
+ self .item_exporter .close ()
0 commit comments