diff --git a/cmd/state/py/chain.py b/cmd/state/py/chain.py new file mode 100644 index 000000000..f9c9930c6 --- /dev/null +++ b/cmd/state/py/chain.py @@ -0,0 +1,20 @@ +import dbutils +import common + +import rlp as rlp +from eth.rlp.headers import BlockHeader, BlockHeaderAPI + + +def lastBlockNumber(env): + b = env.open_db(dbutils.HeadHeaderKey, create=False) + b1 = env.open_db(dbutils.HeaderNumberPrefix, create=False) + with env.begin(write=False) as txn: + blockHashData = txn.get(dbutils.HeadHeaderKey, db=b) + assert len(blockHashData) == common.HashLength, "%d != %d" % (len(blockHashData), common.HashLength) + blockNumberData = txn.get(blockHashData, db=b1) + assert len(blockNumberData) == 8 + return common.bytesToUint64(blockNumberData) + + +def decode_block_header(header_rlp: bytes) -> BlockHeaderAPI: + return rlp.decode(header_rlp, sedes=BlockHeader) diff --git a/cmd/state/py/common.py b/cmd/state/py/common.py new file mode 100644 index 000000000..dae7c6460 --- /dev/null +++ b/cmd/state/py/common.py @@ -0,0 +1,10 @@ +import struct + +HashLength = 32 +AddressLength = 20 +BlockNumberLength = 8 +IncarnationLength = 8 + + +def bytesToUint64(val): return struct.unpack(" StartedWhenBlockNumber: + break + if not dbutils.isHeaderHashKey(k): + continue + + mainHash = bytes(v) + writeTx.put(mainHash, common.uint64ToBytes(0), mainHashes) + + + def gas_limits(readTx, writeTx, file): + blockNum = 0 + with readTx.cursor(b) as curs: + for i, (k, v) in enumerate(curs.iternext()): + timestamp = common.bytesToUint64(k[:common.BlockNumberLength]) + if timestamp > StartedWhenBlockNumber: + break + if not dbutils.isHeaderKey(k): + continue + val = writeTx.get(k[common.BlockNumberLength:], None, mainHashes) + if val is None: + continue + header = chain.decode_block_header(v) + file.writerow([blockNum, header.GasLimit]) + blockNum += 1 + + + with env.begin(write=False) as txn: + with analyticsEnv.begin(write=True) as writeTx: + with open('gas_limits.csv', 'w') as csvfile: + collect_main_hashes(txn, writeTx) + print("Preloaded: %d" % writeTx.stat(mainHashes)["entries"]) + gas_limits(txn, writeTx, csv.writer(csvfile)) + +else: + print("unknown command %s" % cmd)