forked from blockchain-etl/ethereum-etl
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
256 additions
and
40 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
# MIT License | ||
# | ||
# Copyright (c) 2020 Evgeny Medvedev, [email protected] | ||
# | ||
# Permission is hereby granted, free of charge, to any person obtaining a copy | ||
# of this software and associated documentation files (the "Software"), to deal | ||
# in the Software without restriction, including without limitation the rights | ||
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
# copies of the Software, and to permit persons to whom the Software is | ||
# furnished to do so, subject to the following conditions: | ||
# | ||
# The above copyright notice and this permission notice shall be included in all | ||
# copies or substantial portions of the Software. | ||
# | ||
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
# SOFTWARE. | ||
|
||
import json | ||
import logging | ||
from collections import defaultdict | ||
|
||
from google.cloud import storage | ||
|
||
|
||
def build_block_bundles(items): | ||
blocks = defaultdict(list) | ||
transactions = defaultdict(list) | ||
logs = defaultdict(list) | ||
token_transfers = defaultdict(list) | ||
traces = defaultdict(list) | ||
for item in items: | ||
item_type = item.get('type') | ||
if item_type == 'block': | ||
blocks[item.get('number')].append(item) | ||
elif item_type == 'transaction': | ||
transactions[item.get('block_number')].append(item) | ||
elif item_type == 'log': | ||
logs[item.get('block_number')].append(item) | ||
elif item_type == 'token_transfer': | ||
token_transfers[item.get('block_number')].append(item) | ||
elif item_type == 'trace': | ||
traces[item.get('block_number')].append(item) | ||
else: | ||
logging.info(f'Skipping item with type {item_type}') | ||
|
||
block_bundles = [] | ||
for block_number in sorted(blocks.keys()): | ||
if len(blocks[block_number]) != 1: | ||
raise ValueError(f'There must be a single block for a given block number, was {len(blocks[block_number])} for block number {block_number}') | ||
block_bundles.append({ | ||
'block': blocks[block_number][0], | ||
'transactions': transactions[block_number], | ||
'logs': logs[block_number], | ||
'token_transfers': token_transfers[block_number], | ||
'traces': traces[block_number], | ||
}) | ||
|
||
return block_bundles | ||
|
||
|
||
class GcsItemExporter: | ||
|
||
def __init__( | ||
self, | ||
bucket, | ||
path='blocks', | ||
build_block_bundles_func=build_block_bundles): | ||
self.bucket = bucket | ||
self.path = normalize_path(path) | ||
self.build_block_bundles_func = build_block_bundles_func | ||
self.storage_client = storage.Client() | ||
|
||
def open(self): | ||
pass | ||
|
||
def export_items(self, items): | ||
block_bundles = self.build_block_bundles_func(items) | ||
|
||
for block_bundle in block_bundles: | ||
block = block_bundle.get('block') | ||
if block is None: | ||
raise ValueError('block_bundle must include the block field') | ||
block_number = block.get('number') | ||
if block_number is None: | ||
raise ValueError('block_bundle must include the block.number field') | ||
|
||
destination_blob_name = f'{self.path}/{block_number}.json' | ||
|
||
bucket = self.storage_client.bucket(self.bucket) | ||
blob = bucket.blob(destination_blob_name) | ||
blob.upload_from_string(json.dumps(block_bundle)) | ||
logging.info(f'Uploaded file gs://{self.bucket}/{destination_blob_name}') | ||
|
||
def close(self): | ||
pass | ||
|
||
|
||
def normalize_path(p): | ||
if p is None: | ||
p = '' | ||
if p.startswith('/'): | ||
p = p[1:] | ||
if p.endswith('/'): | ||
p = p[:len(p) - 1] | ||
|
||
return p |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
# MIT License | ||
# | ||
# Copyright (c) 2018 Evgeny Medvedev, [email protected] | ||
# | ||
# Permission is hereby granted, free of charge, to any person obtaining a copy | ||
# of this software and associated documentation files (the "Software"), to deal | ||
# in the Software without restriction, including without limitation the rights | ||
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
# copies of the Software, and to permit persons to whom the Software is | ||
# furnished to do so, subject to the following conditions: | ||
# | ||
# The above copyright notice and this permission notice shall be included in all | ||
# copies or substantial portions of the Software. | ||
# | ||
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
# SOFTWARE. | ||
|
||
|
||
class MultiItemExporter: | ||
def __init__(self, item_exporters): | ||
self.item_exporters = item_exporters | ||
|
||
def open(self): | ||
for exporter in self.item_exporters: | ||
exporter.open() | ||
|
||
def export_items(self, items): | ||
for exporter in self.item_exporters: | ||
exporter.export_items(items) | ||
|
||
def export_item(self, item): | ||
for exporter in self.item_exporters: | ||
exporter.export_item(item) | ||
|
||
def close(self): | ||
for exporter in self.item_exporters: | ||
exporter.close() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -207,11 +207,13 @@ You can tune `--batch-size`, `--max-workers` for performance. | |
- This command outputs blocks, transactions, logs, token_transfers to the console by default. | ||
- Entity types can be specified with the `-e` option, | ||
e.g. `-e block,transaction,log,token_transfer,trace,contract,token`. | ||
- Use `--output` option to specify the Google Pub/Sub topic or Postgres database where to publish blockchain data, | ||
- Use `--output` option to specify the Google Pub/Sub topic, Postgres database or GCS bucket where to publish blockchain data, | ||
- For Google PubSub: `--output=projects/<your-project>/topics/crypto_ethereum`. | ||
Data will be pushed to `projects/<your-project>/topics/crypto_ethereum.blocks`, `projects/<your-project>/topics/crypto_ethereum.transactions` etc. topics. | ||
- For Postgres: `--output=postgresql+pg8000://<user>:<password>@<host>:<port>/<database_name>`, | ||
e.g. `--output=postgresql+pg8000://postgres:[email protected]:5432/ethereum`. | ||
e.g. `--output=postgresql+pg8000://postgres:[email protected]:5432/ethereum`. | ||
- For GCS: `--output=gs://<bucket_name>`. Make sure to install and initialize `gcloud` cli. | ||
- Those output types can be combined with a comma e.g. `--output=gs://<bucket_name>,projects/<your-project>/topics/crypto_ethereum` | ||
The [schema](https://github.com/blockchain-etl/ethereum-etl-postgres/tree/master/schema) | ||
and [indexes](https://github.com/blockchain-etl/ethereum-etl-postgres/tree/master/indexes) can be found in this | ||
repo [ethereum-etl-postgres](https://github.com/blockchain-etl/ethereum-etl-postgres). | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -41,3 +41,4 @@ def __init__(self): | |
self.error = None | ||
self.status = None | ||
self.trace_id = None | ||
self.trace_index = None |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.