def to_dict(self, with_state=False, full_transactions=False): """ serializes the block with_state: include state for all accounts full_transactions: include serialized tx (hashes otherwise) """ self.commit_state() b = {} for name, typ, default in block_structure: b[name] = utils.printers[typ](getattr(self, name)) txlist = [] for i in range(self.transaction_count): tx_rlp = self.transactions.get(rlp.encode(utils.encode_int(i))) tx, msr, gas = rlp.decode(tx_rlp) if full_transactions: txjson = transactions.Transaction.create(tx).to_dict() else: txjson = utils.sha3(rlp.descend(tx_rlp, 0)).encode('hex') # tx hash txlist.append({ "tx": txjson, "medstate": msr.encode('hex'), "gas": str(utils.decode_int(gas)) }) b["transactions"] = txlist if with_state: state_dump = {} for address, v in self.state.to_dict().iteritems(): state_dump[address.encode('hex')] = self.account_to_dict( address) b['state'] = state_dump return b
def to_dict(self, with_state=False, full_transactions=False, with_storage_roots=False): """ serializes the block with_state: include state for all accounts full_transactions: include serialized tx (hashes otherwise) """ b = {} for name, typ, default in block_structure: b[name] = utils.printers[typ](getattr(self, name)) txlist = [] for i in range(self.transaction_count): tx_rlp = self.transactions.get(rlp.encode(utils.encode_int(i))) tx, msr, gas = rlp.decode(tx_rlp) if full_transactions: txjson = transactions.Transaction.create(tx).to_dict() else: txjson = utils.sha3(rlp.descend(tx_rlp, 0)).encode('hex') # tx hash txlist.append({ "tx": txjson, "medstate": msr.encode('hex'), "gas": str(utils.decode_int(gas)) }) b["transactions"] = txlist if with_state: state_dump = {} for address, v in self.state.to_dict().iteritems(): state_dump[address.encode('hex')] = \ self.account_to_dict(address, with_storage_roots) b['state'] = state_dump return b
def _add_transactions(self, blk): "'tx_hash' -> 'rlp([blockhash,tx_number])" for i in range(blk.transaction_count): i_enc = utils.encode_int(i) # work on rlp data to avoid unnecessary de/serialization td = blk.transactions.get(rlp.encode(i_enc)) tx = rlp.descend(td, 0) key = utils.sha3(tx) value = rlp.encode([blk.hash, i_enc]) self.db.put(key, value)
def to_dict(self, with_state=False, full_transactions=False, with_storage_roots=False, with_uncles=False): """ serializes the block with_state: include state for all accounts full_transactions: include serialized tx (hashes otherwise) with_uncles: include uncle hashes """ b = {} for name, typ, default in block_structure: b[name] = utils.printers[typ](getattr(self, name)) txlist = [] for i in range(self.transaction_count): tx_rlp = self.transactions.get(rlp.encode(utils.encode_int(i))) tx = rlp.decode(tx_rlp) receipt_rlp = self.receipts.get(rlp.encode(utils.encode_int(i))) msr, gas, mybloom, mylogs = rlp.decode(receipt_rlp) if full_transactions: txjson = transactions.Transaction.create(tx).to_dict() else: # tx hash txjson = utils.sha3(rlp.descend(tx_rlp, 0)).encode('hex') txlist.append({ "tx": txjson, "medstate": msr.encode('hex'), "gas": str(utils.decode_int(gas)), "logs": mylogs, "bloom": mybloom.encode('hex') }) b["transactions"] = txlist if with_state: state_dump = {} for address, v in self.state.to_dict().iteritems(): state_dump[address.encode('hex')] = \ self.account_to_dict(address, with_storage_roots) b['state'] = state_dump if with_uncles: b['uncles'] = [ utils.sha3(rlp.encode(u)).encode('hex') for u in self.uncles ] return b
def to_dict(self): b = {} for name, typ, default in block_structure: b[name] = getattr(self, name) for key in ["nonce", "state_root", "uncles_hash", "prevhash"]: b[key] = b[key].encode("hex") b["state"] = {} for address, v in self.state.to_dict().iteritems(): b["state"][address.encode('hex')] = self.account_to_dict(address) txlist = [] for i in range(self.transaction_count): td = self.transactions.get(utils.encode_int(i)) tx, msr, gas = map(lambda i: rlp.descend(td, i), range(3)) txjson = transactions.Transaction.deserialize(tx).to_dict() txlist.append({ "tx": txjson, "medstate": msr, "gas": utils.decode_int(gas) }) b["transactions"] = txlist return b
def to_dict(self): self.commit_state() b = {} for name, typ, default in block_structure: b[name] = utils.printers[typ](getattr(self, name)) b["state"] = {} for address, v in self.state.to_dict().iteritems(): b["state"][address.encode('hex')] = self.account_to_dict(address) txlist = [] for i in range(self.transaction_count): td = self.transactions.get(rlp.encode(utils.encode_int(i))) tx = rlp.descend(td, 0) msr = rlp.descend_to_val(td, 1) gas = rlp.descend_to_val(td, 2) txjson = transactions.Transaction.deserialize(tx).to_dict() txlist.append({ "tx": txjson, "medstate": msr.encode('hex'), "gas": str(utils.decode_int(gas)) }) b["transactions"] = txlist return b
def get_bloom(self, blockhash): h = rlp.decode(rlp.descend(self.db.get(blockhash), 0, 6)) return utils.big_endian_to_int(h)
for endBlock in endBlocks: txStarted = 0 uncStarted = 0 pointer_csv_file_blockData, blockWriter = openCSVWriter( "blockData_%s.csv" % (endBlock, )) pointer_csv_file_transactionData, transactionWriter = openCSVWriter( "transactionData_%s.csv" % (endBlock, )) pointer_csv_file_uncleData, uncleWriter = openCSVWriter( "uncleData_%s.csv" % (endBlock, )) while (blknum < endBlock): f.seek(pos) prefix = f.read(10) _typ, _len, _pos = rlp.codec.consume_length_prefix(prefix, 0) blkdata = prefix + f.read(_pos + _len - 10) header = rlp.decode(rlp.descend(blkdata, 0), BlockHeader) headerDictionary = header.to_dict() del headerDictionary['bloom'] if blknum % 100000 == 0: list_columnNames = headerDictionary.keys() list_columnNames.append('pos') list_columnNames.append('txcount') list_columnNames.append('unclecount') blockWriter.writerow(list_columnNames) print 'reached here' list_blockvalues = [v for v in headerDictionary.values()] list_blockvalues.append(pos) txCount = len(rlp.decode(rlp.descend(blkdata, 1))) uncleCount = len(rlp.decode(rlp.descend(blkdata, 2)))
def packet_cmd(cls, packet): try: v = idec(rlp.descend(packet[8:200], 0)) except rlp.DecodingError: v = -1 return Packeter.cmd_map.get(v, 'invalid')
from ethereum.block import BlockHeader import rlp try: data = json.load(open('progress.json')) blknum = data['blknum'] pos = data['pos'] except: blknum, pos = 0, 0 f = open('geth-2283415.dump') outdata = [] while 1: f.seek(pos) prefix = f.read(10) _typ, _len, _pos = rlp.codec.consume_length_prefix(prefix, 0) blkdata = prefix + f.read(_pos + _len - 10) header = rlp.decode(rlp.descend(blkdata, 0), BlockHeader) txcount = len(rlp.decode(rlp.descend(blkdata, 1))) uncles = [ BlockHeader.deserialize(x) for x in rlp.decode(rlp.descend(blkdata, 2)) ] outdata.append([ header.number, len(uncles), sum([4.375 - 0.625 * (header.number - u.number) for u in uncles]), sum([u.gas_used for u in uncles]), txcount, header.gas_used, _len + _pos, blkdata.count('\x00') ]) print outdata[-1] pos += _pos + _len
import json from ethereum.block import BlockHeader import rlp try: data = json.load(open('progress.json')) blknum = data['blknum'] pos = data['pos'] except: blknum, pos = 0, 0 f = open('geth-2283415.dump') outdata = [] while 1: f.seek(pos) prefix = f.read(10) _typ, _len, _pos = rlp.codec.consume_length_prefix(prefix, 0) blkdata = prefix + f.read(_pos + _len - 10) header = rlp.decode(rlp.descend(blkdata, 0), BlockHeader) txcount = len(rlp.decode(rlp.descend(blkdata, 1))) uncles = [BlockHeader.deserialize(x) for x in rlp.decode(rlp.descend(blkdata, 2))] outdata.append([header.number, len(uncles), sum([4.375 - 0.625 * (header.number - u.number) for u in uncles]), sum([u.gas_used for u in uncles]), txcount, header.gas_used, _len + _pos, blkdata.count('\x00')]) print outdata[-1] pos += _pos + _len
def packet_cmd(cls, packet): try: v = idec(rlp.descend(packet[8:200], 0)) except rlp.DecodingError: v = -1 return v