lbry-sdk/scripts/simulate_sync_console.py

157 lines
5.9 KiB
Python
Raw Normal View History

import asyncio
from random import randrange
2020-06-27 03:52:01 +02:00
from typing import List
2020-07-01 05:13:38 +02:00
from binascii import unhexlify
from google.protobuf.message import DecodeError
from lbry.schema.claim import Claim
from lbry.blockchain import Ledger
from lbry.service import FullNode
2020-06-27 03:52:01 +02:00
from lbry.console import Advanced, Basic
from lbry.conf import Config
2020-06-27 03:52:01 +02:00
from lbry.db.utils import chunk
2020-07-06 05:03:45 +02:00
from lbry.db.query_context import Event
2020-07-01 05:13:38 +02:00
def cause_protobuf_stderr():
try:
Claim.from_bytes(unhexlify(
'005a3c63779597cba4c0e6ee45c3074fc389bd564ccc5d4a90eb4baacb0b028f2f4930'
'0db003d6a27f0cac8be8b45fdda597303208b81845534e4543494c07123e0a420a'
))
except DecodeError:
pass
class Simulator:
2020-06-27 03:52:01 +02:00
def __init__(self, console):
self.console = console
self.sync = console.service.sync
self.progress = self.sync._on_progress_controller
self.starting_height = 0
2020-06-27 03:52:01 +02:00
self.ending_height = 0
self.starting_file = 0
2020-06-27 03:52:01 +02:00
self.processes = console.service.db.processes
2020-06-27 03:52:01 +02:00
self.txs = 0
self.claims = 0
self.supports = 0
@property
2020-06-27 03:52:01 +02:00
def blocks(self, ):
if self.starting_height == 0:
return self.ending_height-self.starting_height
return (self.ending_height-self.starting_height)+1
2020-06-27 03:52:01 +02:00
async def advance(self, initial_sync: bool, ending_height: int, files: List[int], txs: int):
self.ending_height = ending_height
self.txs = txs
self.claims = int(txs/4)
self.supports = int(txs/2)
await self.progress.add({
"event": "blockchain.sync.start",
"data": {
"starting_height": self.starting_height,
2020-06-27 03:52:01 +02:00
"ending_height": ending_height,
"files": len(files),
"blocks": self.blocks,
2020-06-27 03:52:01 +02:00
"txs": self.txs,
"claims": self.claims,
"supports": self.supports,
}
})
2020-06-23 19:11:16 +02:00
blocks_synced = txs_synced = 0
2020-06-27 03:52:01 +02:00
for file_group in chunk(files, self.processes):
tasks = []
2020-06-27 03:52:01 +02:00
for file in file_group:
if file == files[-1]:
2020-07-01 05:13:38 +02:00
cause_protobuf_stderr()
2020-06-27 03:52:01 +02:00
tasks.append(self.sync_block_file(file, self.blocks-blocks_synced, self.txs-txs_synced))
2020-07-01 05:13:38 +02:00
cause_protobuf_stderr()
2020-06-23 19:11:16 +02:00
else:
2020-06-27 03:52:01 +02:00
blocks = int(self.blocks / len(files))
2020-06-23 19:11:16 +02:00
blocks_synced += blocks
2020-06-27 03:52:01 +02:00
txs = int(self.txs / len(files))
2020-06-23 19:11:16 +02:00
txs_synced += txs
2020-06-27 03:52:01 +02:00
tasks.append(self.sync_block_file(file, blocks, txs))
await asyncio.wait(tasks)
2020-07-06 05:03:45 +02:00
for step in Event._events:
if step.name in ("blockchain.sync.block.read", "blockchain.sync.block.save"):
2020-06-27 03:52:01 +02:00
continue
2020-07-06 05:03:45 +02:00
await getattr(self, step.name.replace('.', '_'))()
#await self.progress.add({
# "event": "blockchain.sync.complete",
# "data": {"step": len(self.steps), "total": len(self.steps), "unit": "tasks"}
#})
2020-06-27 03:52:01 +02:00
self.ending_height = ending_height+1
self.starting_height = self.ending_height
async def sync_block_file(self, block_file, blocks, txs):
for i in range(0, blocks, 1000):
await self.progress.add({
"event": "blockchain.sync.block.read",
"data": {"step": i, "total": blocks, "unit": "blocks", "block_file": block_file}
})
await asyncio.sleep(randrange(1, 10)/10)
await self.progress.add({
"event": "blockchain.sync.block.read",
"data": {"step": blocks, "total": blocks, "unit": "blocks", "block_file": block_file}
})
2020-06-23 19:11:16 +02:00
await asyncio.sleep(0.5)
2020-06-27 03:52:01 +02:00
for i in range(0, txs, 2000):
await self.progress.add({
"event": "blockchain.sync.block.save",
"data": {"step": i, "total": txs, "unit": "txs", "block_file": block_file}
})
await asyncio.sleep(randrange(1, 10) / 10)
await self.progress.add({
"event": "blockchain.sync.block.save",
"data": {"step": txs, "total": txs, "unit": "txs", "block_file": block_file}
})
2020-06-27 03:52:01 +02:00
async def generate_steps(self, event, steps, unit, delay=1.0, step=1):
await self.progress.add({"event": event, "data": {"step": 0, "total": steps, "unit": unit}})
remaining = steps
for i in range(1, steps+1, step):
await asyncio.sleep(delay)
await self.progress.add({"event": event, "data": {"step": i, "total": steps, "unit": unit}})
remaining -= i
if remaining:
await asyncio.sleep(delay)
await self.progress.add({"event": event, "data": {"step": steps, "total": steps, "unit": unit}})
2020-07-06 05:03:45 +02:00
async def blockchain_sync_block_filters(self):
await self.generate_steps("blockchain.sync.block.filters", 5, "blocks")
2020-06-27 03:52:01 +02:00
2020-07-06 05:03:45 +02:00
async def blockchain_sync_spends(self):
await self.generate_steps("blockchain.sync.spends", 5, "steps")
2020-06-27 03:52:01 +02:00
2020-07-06 05:03:45 +02:00
async def blockchain_sync_claims(self):
2020-06-27 03:52:01 +02:00
for i in range(0, self.claims, 1_000):
await self.progress.add({
2020-07-06 05:03:45 +02:00
"event": "blockchain.sync.claims",
2020-06-27 03:52:01 +02:00
"data": {"step": i, "total": self.claims, "unit": "claims"}
})
2020-06-23 19:11:16 +02:00
await asyncio.sleep(0.1)
2020-06-27 03:52:01 +02:00
await self.progress.add({
2020-07-06 05:03:45 +02:00
"event": "blockchain.sync.claims",
2020-06-27 03:52:01 +02:00
"data": {"step": self.claims, "total": self.claims, "unit": "claims"}
})
2020-07-06 05:03:45 +02:00
async def blockchain_sync_supports(self):
await self.generate_steps("blockchain.sync.supports", 5, "supports")
async def main():
2020-06-27 03:52:01 +02:00
console = Advanced(FullNode(Ledger(Config(processes=3, spv_address_filters=False))))
sim = Simulator(console)
console.starting()
2020-06-27 03:52:01 +02:00
await sim.advance(True, 100_000, [1, 2, 3, 4, 5], 100_000)
await sim.advance(False, 100_001, [5], 100)
console.stopping()
if __name__ == "__main__":
asyncio.run(main())