3

Is there a way to read a deployed contract's bytecode from leveldb? From what I understand, within each account in the state tree, there exists the nonce, the balance, the codehash, and the storage root. Does the codehash point to the location in leveldb where the contract bytecode is stored?

And is it correct to say that contract bytecode stored in leveldb is only the body portion of the contract and not the init/constructor bytecode?

1

You can query contract bytecode in this public BigQuery dataset https://medium.com/@medvedev1088/more-blockchain-analysis-on-bigquery-92a863137f01. Additionally you can query blocks, transactions, receipts, logs, erc20_transfers:

### contracts.csv

Column                       |    Type     |
-----------------------------|-------------|
contract_address             | address     |
contract_bytecode            | hex_string  |

### blocks.csv

Column                  | Type               |
------------------------|--------------------|
block_number            | bigint             |
block_hash              | hex_string         |
block_parent_hash       | hex_string         |
block_nonce             | hex_string         |
block_sha3_uncles       | hex_string         |
block_logs_bloom        | hex_string         |
block_transactions_root | hex_string         |
block_state_root        | hex_string         |
block_miner             | address            |
block_difficulty        | numeric            |
block_total_difficulty  | numeric            |
block_size              | bigint             |
block_extra_data        | hex_string         |
block_gas_limit         | bigint             |
block_gas_used          | bigint             |
block_timestamp         | bigint             |
block_transaction_count | bigint             |

### transactions.csv

Column              |    Type     |
--------------------|-------------|
tx_hash             | hex_string  |
tx_nonce            | bigint      |
tx_block_hash       | hex_string  |
tx_block_number     | bigint      |
tx_index            | bigint      |
tx_from             | address     |
tx_to               | address     |
tx_value            | numeric     |
tx_gas              | bigint      |
tx_gas_price        | bigint      |
tx_input            | hex_string  |

### erc20_transfers.csv

Column              |    Type     |
--------------------|-------------|
erc20_token         | address     |
erc20_from          | address     |
erc20_to            | address     |
erc20_value         | numeric     |
erc20_tx_hash       | hex_string  |
erc20_log_index     | bigint      |
erc20_block_number  | bigint      |

### receipts.csv

Column                       |    Type     |
-----------------------------|-------------|
receipt_transaction_hash     | hex_string  |
receipt_transaction_index    | bigint      |
receipt_block_hash           | hex_string  |
receipt_block_number         | bigint      |
receipt_cumulative_gas_used  | bigint      |
receipt_gas_used             | bigint      |
receipt_contract_address     | address     |
receipt_root                 | hex_string  |
receipt_status               | bigint      |

### logs.csv

Column                       |    Type     |
-----------------------------|-------------|
log_index                    | bigint      |
log_transaction_hash         | hex_string  |
log_transaction_index        | bigint      |
log_block_hash   

The tool I used for exporting the data https://github.com/medvedev1088/ethereum-etl

0

Is there a way to read a deployed contract's bytecode from leveldb?

Yes there is surely a way because (e.g. geth) JSON RPC expose the function eth_getcode, that returns the code associated to an account. Under the hood it reads the status database (which in the case of geth client is a leveldb database).

From what I understand, within each account in the state tree, there exists the nonce, the balance, the codehash, and the storage root. Does the codehash point to the location in leveldb where the contract bytecode is stored?

According to the Yellow Paper, which is the formal specification of Ethereum (Section 4.1), yes:

enter image description here enter image description here

And is it correct to say that contract bytecode stored in leveldb is only the body portion of the contract and not the init/constructor bytecode?

Yes, it is so. If you take a look to the Ethereum Yellow Paper section 7. It says that:

enter image description here enter image description here

Therefore i is executed and if all the execution is successful the body of the code o is produced. The hash of the body KEC(o) is stored in the account state. And o is stored in the database for later retrieval in case of contract invocation (i.e. the message call).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.