-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathextract_ledger_transactions.py
318 lines (273 loc) · 10.4 KB
/
extract_ledger_transactions.py
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
import argparse
import asyncio
import logging
import queue
from logging.handlers import QueueHandler, QueueListener
import xrpl
from fluent.asyncsender import FluentSender
from prometheus_client import (
CollectorRegistry,
generate_latest,
)
from xrpl.asyncio.clients import AsyncJsonRpcClient
import ekspiper.util.xrplpy_patches
from ekspiper.builder.flow import (
ProcessCollectorsMapBuilder,
TemplateFlowBuilder,
)
from ekspiper.connect.counter import PartitionedCounterDataSource
from ekspiper.connect.file_data_source import FileDataSource
from ekspiper.connect.queue import QueueSourceSink
from ekspiper.metric.prom import ScriptExecutionMetrics
from ekspiper.processor.etl import (
ETLTemplateProcessor,
GenericValidator,
XRPLGenericTransformer,
)
from ekspiper.processor.fetch_transactions import (
XRPLFetchLedgerDetailsProcessor,
XRPLExtractTransactionsFromLedgerProcessor, XRPLLedgerProcessor,
)
from ekspiper.schema.xrp import XRPLTransactionSchema, XRPLObjectSchema, XRPLLedgerSchema
from ekspiper.util.endpoints import endpoints
from ekspiper.util.xrplpy_patches import get_latest_validated_ledger_sequence
# TODO: remove monkey patch when clio release is done: https://ripplelabs.atlassian.net/browse/CLIO-260
xrpl.models.Ledger = ekspiper.util.xrplpy_patches.Ledger
logger = logging.getLogger(__name__)
log_queue = queue.Queue(-1)
queue_handler = QueueHandler(log_queue)
logger.addHandler(queue_handler)
listener = QueueListener(log_queue)
listener.start()
# logging.basicConfig(level=logging.INFO)
async def start_ledger_sequence(client) -> int:
return await get_latest_validated_ledger_sequence(client) - 1
async def amain_file(
file: str = None,
fluent_tag: str = "test",
fluent_host: str = "0.0.0.0",
fluent_port: int = 25225,
schema: str = "transaction",
):
xrpl_endpoint = endpoints[fluent_tag]
if xrpl_endpoint is None:
raise RuntimeError(
"[ExtractXRPLTransactions] missing xrpl endpoint - did you forget to specify the fluent tag?")
file_data_source = FileDataSource(file=file)
file_data_source.start()
async_rpc_client = AsyncJsonRpcClient(xrpl_endpoint)
ledger_record_source_sink = QueueSourceSink(
name="ledger_record_source",
)
pc_map = ProcessCollectorsMapBuilder().with_processor(
XRPLFetchLedgerDetailsProcessor(
rpc_client=async_rpc_client,
)
).add_data_sink_output_collector(
data_sink=ledger_record_source_sink,
name="ledger_record_source_sink"
).build()
flow_payment_detail = TemplateFlowBuilder().add_process_collectors_map(
pc_map
).build()
flow_ledger_detail_tasks = [asyncio.create_task(flow_payment_detail.aexecute(
message_iterator=file_data_source
))]
txn_record_source_sink = QueueSourceSink(
name="txn_record_source_sink",
)
# Flow: Break down the ledger into transactions
#
pc_map = ProcessCollectorsMapBuilder().with_processor(
XRPLExtractTransactionsFromLedgerProcessor(
is_include_ledger_index=True,
)
).add_data_sink_output_collector(
data_sink=txn_record_source_sink,
name="txn_record_source_sink"
).build()
flow_ledger_to_txns_brk = TemplateFlowBuilder().add_process_collectors_map(pc_map).build()
flow_ledger_to_txns_brk_task = asyncio.create_task(flow_ledger_to_txns_brk.aexecute(
message_iterator=ledger_record_source_sink,
))
# setup fluent client
fluent_sender = FluentSender(
fluent_tag,
host=fluent_host,
port=fluent_port,
)
# Flow: Transaction Record
#
schema_to_use = XRPLTransactionSchema.SCHEMA
if schema == "object":
schema_to_use = XRPLObjectSchema.SCHEMA
logger.warning("Using schema: ", schema)
txn_rec_pc_map_builder = ProcessCollectorsMapBuilder()
pc_map = txn_rec_pc_map_builder.with_processor(
ETLTemplateProcessor(
validator=GenericValidator(schema_to_use),
transformer=XRPLGenericTransformer(schema_to_use),
)
).with_stdout_output_collector(
tag_name="transactions",
is_simplified=False
).add_bigquery_output_collector(
project='ripplex-347905',
dataset='testnet',
table='transactions'
).build()
# ).add_fluent_output_collector(
# tag_name="ledger_txn",
# fluent_sender=fluent_sender
# ).build()
flow_txn_record = TemplateFlowBuilder().add_process_collectors_map(pc_map).build()
flow_txn_record_task = asyncio.create_task(flow_txn_record.aexecute(
message_iterator=txn_record_source_sink,
))
await flow_txn_record_task
listener.stop()
async def amain(
fluent_tag: str = "mainnet",
fluent_host: str = "0.0.0.0",
fluent_port: int = 25225,
):
xrpl_endpoint = endpoints[fluent_tag]
if xrpl_endpoint is None:
raise RuntimeError(
"[ExtractXRPLTransactions] missing xrpl endpoint - did you forget to specify the fluent tag?")
async_rpc_client = AsyncJsonRpcClient(xrpl_endpoint)
start_index = await start_ledger_sequence(async_rpc_client)
# build the ledger queue for processing
ledger_record_source_sink = QueueSourceSink(name="ledger_record_source")
# Flow: Obtain Ledger Details
#
flow_ledger_detail_tasks = []
partition_size = 10
for i in range(partition_size):
# start with the counter
index_decrementor_data_source = PartitionedCounterDataSource(
starting_count=start_index,
shard_index=i,
shard_size=partition_size,
)
index_decrementor_data_source.start()
pc_map = ProcessCollectorsMapBuilder().with_processor(
XRPLFetchLedgerDetailsProcessor(
rpc_client=async_rpc_client,
)
).add_data_sink_output_collector(
data_sink=ledger_record_source_sink,
name="ledger_record_source_sink"
).build()
flow_payment_detail = TemplateFlowBuilder().add_process_collectors_map(pc_map).build()
flow_ledger_detail_tasks.append(asyncio.create_task(flow_payment_detail.aexecute(
message_iterator=index_decrementor_data_source
)))
# build the transaction queue for processing
txn_record_source_sink = QueueSourceSink(name="txn_record_source_sink")
formatted_ledger_source_sink = QueueSourceSink(name="formatted_ledger_source_sink")
# Flow: Break down the ledger into transactions
#
pc_map = ProcessCollectorsMapBuilder().with_processor(
XRPLExtractTransactionsFromLedgerProcessor(
is_include_ledger_index=True,
)
).add_data_sink_output_collector(
data_sink=txn_record_source_sink,
name="txn_record_source_sink"
).build()
ledger_record_pc_map = ProcessCollectorsMapBuilder().with_processor(
XRPLLedgerProcessor()
).add_data_sink_output_collector(
data_sink=formatted_ledger_source_sink,
name="ledger_record_source_sink",
).build()
flow_ledger_to_txns_brk = TemplateFlowBuilder().add_process_collectors_map(pc_map).add_process_collectors_map(
ledger_record_pc_map).build()
flow_ledger_to_txns_brk_task = asyncio.create_task(flow_ledger_to_txns_brk.aexecute(
message_iterator=ledger_record_source_sink,
))
pc_map_transaction = build_fluent_map(schema=XRPLTransactionSchema.SCHEMA, fluent_tag=fluent_tag + ".transactions",
fluent_host=fluent_host, fluent_port=fluent_port)
flow_txn_record = TemplateFlowBuilder().add_process_collectors_map(pc_map_transaction).build()
flow_txn_record_task = asyncio.create_task(flow_txn_record.aexecute(
message_iterator=txn_record_source_sink,
))
pc_map_ledgers = build_fluent_map(schema=XRPLLedgerSchema.SCHEMA, fluent_tag=fluent_tag + ".ledgers",
fluent_host=fluent_host, fluent_port=fluent_port)
flow_ledger_record = TemplateFlowBuilder().add_process_collectors_map(pc_map_ledgers).build()
flow_ledger_record_task = asyncio.create_task(flow_ledger_record.aexecute(
message_iterator=formatted_ledger_source_sink,
))
# TODO: for now
await asyncio.gather(flow_ledger_record_task, flow_txn_record_task)
# await flow_txn_record_task
listener.stop()
def build_fluent_map(schema, fluent_tag, fluent_host="0.0.0.0", fluent_port=25225):
return ProcessCollectorsMapBuilder().with_processor(
ETLTemplateProcessor(
validator=GenericValidator(schema),
transformer=XRPLGenericTransformer(schema),
)
).with_stdout_output_collector(
tag_name=fluent_tag,
is_simplified=True
).add_fluent_output_collector(
fluent_sender=FluentSender(fluent_tag, host=fluent_host, port=fluent_port),
).build()
def parse_arguments() -> argparse.Namespace:
arg_parser = argparse.ArgumentParser()
arg_parser.add_argument(
"-ft",
"--fluent_tag",
help="specify the name to tag the FluentD/Bit entries",
type=str,
default="test", # use prod for forwarding to GCP
)
arg_parser.add_argument(
"-fh",
"--fluent_host",
help="specify the FluentD/Bit host",
type=str,
default="0.0.0.0",
)
arg_parser.add_argument(
"-fp",
"--fluent_port",
help="specify the FluentD/Bit port",
type=int,
default=25225,
)
arg_parser.add_argument(
"-f",
"--file",
help="get ledgers from file",
type=str,
default=None,
)
return arg_parser.parse_args()
if __name__ == "__main__":
args = parse_arguments()
registry = CollectorRegistry()
with ScriptExecutionMetrics(
prom_registry=registry,
job_name="extract_ledger_txns",
):
logger.warning("running other main")
if args.file is None:
asyncio.run(amain(
fluent_tag=args.fluent_tag,
fluent_host=args.fluent_host,
fluent_port=args.fluent_port,
))
else:
asyncio.run(amain_file(
file=args.file,
fluent_tag=args.fluent_tag,
fluent_host=args.fluent_host,
fluent_port=args.fluent_port,
schema=args.schema,
))
print(generate_latest(registry))
# previous method
asyncio.run(amain())