-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathadd_coinbase_to_txs.py
More file actions
96 lines (78 loc) · 2.71 KB
/
add_coinbase_to_txs.py
File metadata and controls
96 lines (78 loc) · 2.71 KB
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
import asyncio
import json
import pathlib
import typing
import sys
sys.path.append(str(pathlib.Path(__file__).parent.parent))
from sqlalchemy import func, select, update
from app.database import sessionmanager
from app.settings import get_settings
from app.parser import make_request
from app.models import Transaction
settings: typing.Any = get_settings()
async def main():
sessionmanager.init(settings.database.endpoint)
async with sessionmanager.session() as session:
total = (
await session.scalar(
select(func.count(Transaction.id)).filter(
(Transaction.coinbase == None) | (Transaction.block_index == None)
)
)
or 0
)
limit = 100
query = (
select(Transaction.id, Transaction.txid)
.filter((Transaction.coinbase == None) | (Transaction.block_index == None))
.limit(limit)
)
processed = 0
while txs := (await session.execute(query)).all():
updates: list[dict[str, typing.Any]] = []
for dbid, txid in txs:
transaction_result = await make_request(
settings.blockchain.endpoint,
[
{
"id": str(dbid),
"method": "getrawtransaction",
"params": [txid, True],
}
],
)
tx = transaction_result[0]["result"]
block_response = await make_request(
settings.blockchain.endpoint,
[
{
"id": tx["blockhash"],
"method": "getblock",
"params": [tx["blockhash"]],
}
],
)
block = block_response[0]["result"]
block_index = block["tx"].index(txid)
updates.append(
{
"id": dbid,
"block_index": block_index,
"coinbase": block_index == 0,
}
)
await session.execute(
update(Transaction),
updates,
execution_options={"synchronize_session": False},
)
await session.commit()
processed += limit
print(
f"Progress: {processed}/{total} ({(processed/total)*100:.2f})",
end="\r",
flush=True,
)
print("\nDone")
if __name__ == "__main__":
asyncio.run(main())