-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathblock_ref.py
103 lines (79 loc) · 3.19 KB
/
block_ref.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
import asyncio
import os
import random
from dataclasses import dataclass
from pathlib import Path
from time import monotonic
from typing import List, Optional
import aiosqlite
import click
from moon.consensus.blockchain import Blockchain
from moon.consensus.default_constants import DEFAULT_CONSTANTS
from moon.full_node.block_store import BlockStore
from moon.full_node.coin_store import CoinStore
from moon.full_node.hint_store import HintStore
from moon.types.blockchain_format.program import SerializedProgram
from moon.types.blockchain_format.sized_bytes import bytes32
from moon.util.db_version import lookup_db_version
from moon.util.db_wrapper import DBWrapper2
from moon.util.ints import uint32
# the first transaction block. Each byte in transaction_height_delta is the
# number of blocks to skip forward to get to the next transaction block
transaction_block_heights = []
last = 225698
file_path = os.path.realpath(__file__)
for delta in open(Path(file_path).parent / "transaction_height_delta", "rb").read():
new = last + delta
transaction_block_heights.append(new)
last = new
@dataclass(frozen=True)
class BlockInfo:
prev_header_hash: bytes32
transactions_generator: Optional[SerializedProgram]
transactions_generator_ref_list: List[uint32]
def random_refs() -> List[uint32]:
ret = random.sample(transaction_block_heights, DEFAULT_CONSTANTS.MAX_GENERATOR_REF_LIST_SIZE)
random.shuffle(ret)
return [uint32(i) for i in ret]
REPETITIONS = 100
async def main(db_path: Path):
random.seed(0x213FB154)
async with aiosqlite.connect(db_path) as connection:
await connection.execute("pragma journal_mode=wal")
await connection.execute("pragma synchronous=FULL")
await connection.execute("pragma query_only=ON")
db_version: int = await lookup_db_version(connection)
db_wrapper = DBWrapper2(connection, db_version=db_version)
await db_wrapper.add_connection(await aiosqlite.connect(db_path))
block_store = await BlockStore.create(db_wrapper)
hint_store = await HintStore.create(db_wrapper)
coin_store = await CoinStore.create(db_wrapper)
start_time = monotonic()
# make configurable
reserved_cores = 4
blockchain = await Blockchain.create(
coin_store, block_store, DEFAULT_CONSTANTS, hint_store, db_path.parent, reserved_cores
)
peak = blockchain.get_peak()
assert peak is not None
timing = 0.0
for i in range(REPETITIONS):
block = BlockInfo(
peak.header_hash,
SerializedProgram.from_bytes(bytes.fromhex("80")),
random_refs(),
)
start_time = monotonic()
gen = await blockchain.get_block_generator(block)
one_call = monotonic() - start_time
timing += one_call
assert gen is not None
print(f"get_block_generator(): {timing/REPETITIONS:0.3f}s")
blockchain.shut_down()
@click.command()
@click.argument("db-path", type=click.Path())
def entry_point(db_path: Path):
asyncio.run(main(Path(db_path)))
if __name__ == "__main__":
# pylint: disable = no-value-for-parameter
entry_point()