diff --git a/mythril/leveldb/accountindexing.py b/mythril/leveldb/accountindexing.py index a4690a07..75acf387 100644 --- a/mythril/leveldb/accountindexing.py +++ b/mythril/leveldb/accountindexing.py @@ -1,18 +1,14 @@ -import os -import hashlib -import transaction -from threading import Thread import logging from mythril import ether import time from ethereum.messages import Log -from ethereum.block import BlockHeader import rlp from rlp.sedes import big_endian_int, binary from ethereum import utils from ethereum.utils import hash32, address, int256 -BATCH_SIZE = 8*4096 +BATCH_SIZE = 8 * 4096 + class CountableList(object): """A sedes for lists of arbitrary length. @@ -35,6 +31,7 @@ class CountableList(object): except: return [] + class ReceiptForStorage(rlp.Serializable): ''' Receipt format stored in levelDB @@ -50,6 +47,7 @@ class ReceiptForStorage(rlp.Serializable): ('gas_used', big_endian_int) ] + class AccountIndexer(object): ''' Updates address index @@ -117,7 +115,7 @@ class AccountIndexer(object): blockNum = 0 if self.lastProcessedBlock is not None: - blockNum = self.lastProcessedBlock+1 + blockNum = self.lastProcessedBlock + 1 print("Updating hash-to-address index from block " + str(self.lastProcessedBlock)) else: print("Starting hash-to-address index") @@ -136,20 +134,19 @@ class AccountIndexer(object): # store new mappings self.db.writer._start_writing() count += len(results) - for address in results: - self.db.writer._store_account_address(address) + for addr in results: + self.db.writer._store_account_address(addr) self.db.writer._commit_batch() processed += BATCH_SIZE - blockNum = min(blockNum + BATCH_SIZE, self.lastBlock+1) + blockNum = min(blockNum + BATCH_SIZE, self.lastBlock + 1) cost_time = time.time() - ether.start_time print("%d blocks processed (in %d seconds), %d unique addresses found, next block: %d" % (processed, cost_time, count, min(self.lastBlock, blockNum))) - self.lastProcessedBlock = blockNum-1 + self.lastProcessedBlock = blockNum - 1 self.db.writer._set_last_indexed_number(self.lastProcessedBlock) - print("Finished indexing") self.lastBlock = self.lastProcessedBlock \ No newline at end of file diff --git a/mythril/leveldb/client.py b/mythril/leveldb/client.py index 6cd5cd8b..580de1df 100644 --- a/mythril/leveldb/client.py +++ b/mythril/leveldb/client.py @@ -17,11 +17,11 @@ numSuffix = b'n' # headerPrefix + num (uint64 big endian) + numSuffix blockHashPrefix = b'H' # blockHashPrefix + hash -> num (uint64 big endian) blockReceiptsPrefix = b'r' # blockReceiptsPrefix + num (uint64 big endian) + hash -> block receipts # known geth keys -headHeaderKey = b'LastBlock' # head (latest) header hash +headHeaderKey = b'LastBlock' # head (latest) header hash # custom prefixes addressPrefix = b'AM' # addressPrefix + hash -> address # custom keys -addressMappingHeadKey = b'accountMapping' # head (latest) number of indexed block +addressMappingHeadKey = b'accountMapping' # head (latest) number of indexed block headHeaderKey = b'LastBlock' # head (latest) header hash @@ -38,6 +38,7 @@ def _encode_hex(v): ''' return '0x' + utils.encode_hex(v) + class LevelDBReader(object): ''' level db reading interface, can be used with snapshot @@ -128,6 +129,7 @@ class LevelDBReader(object): receipts = rlp.decode(receiptsData, sedes=CountableList(ReceiptForStorage)) return receipts + class LevelDBWriter(object): ''' level db writing interface @@ -162,6 +164,7 @@ class LevelDBWriter(object): addressKey = addressPrefix + utils.sha3(address) self.wb.put(addressKey, address) + class EthLevelDB(object): ''' Go-Ethereum LevelDB client class diff --git a/mythril/leveldb/eth_db.py b/mythril/leveldb/eth_db.py index 62585441..a46d9e93 100644 --- a/mythril/leveldb/eth_db.py +++ b/mythril/leveldb/eth_db.py @@ -1,6 +1,6 @@ import plyvel from ethereum.db import BaseDB -from ethereum import utils + class ETH_DB(BaseDB): '''