Skip to content

Instantly share code, notes, and snippets.

@cdetrio
Created May 27, 2017 20:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cdetrio/6574161b03df57756dc63bd502960a52 to your computer and use it in GitHub Desktop.
Save cdetrio/6574161b03df57756dc63bd502960a52 to your computer and use it in GitHub Desktop.
pyethereum block decode example
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"# mkdir pyeth\n",
"# cd pyeth\n",
"# pipenv --two\n",
"# pipenv install git+https://github.com/ethereum/pyethereum@develop#egg=ethereum\n",
"# pipenv install jupyter\n",
"# pipenv shell\n",
"# jupyter notebook"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"import ethereum"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
"from ethereum.block import Block\n",
"from ethereum.block import BlockHeader\n",
"from ethereum.utils import parse_as_bin"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"import rlp"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"# raw hex of block https://etherscan.io/block/3598320\n",
"block_data = 'f90212f9020da053e106c52d0239a68f1684f4872845fe57446aaef4cce908abce2fcef8ea7b45a01dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d493479461c808d82a3ac53231750dadc13c777b59310bd9a0317540ef723746d377e3f592c4b5a6e01611be348335615bc625df93fcd4a8cda056e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421a056e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421b9010000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000870111dedbb4a7598336e7f083416d19808458ff51a08fe4b883e5bda9e7a59ee4bb99e9b1bca067b801c0b8cddac93397758d8175dea1fbf273a0216d057850b774426a03b35888ec40975811dd80f8c0c0'"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"block_bin = parse_as_bin(block_data)"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [],
"source": [
"block = rlp.decode(block_bin, Block)"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[('header', ethereum.block.BlockHeader),\n",
" ('transactions', <rlp.sedes.lists.CountableList at 0x108fe8a10>),\n",
" ('uncles', <rlp.sedes.lists.CountableList at 0x108fe8a50>)]"
]
},
"execution_count": 8,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"block.fields"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"<BlockHeader(#3598320 575d5e6a)>"
]
},
"execution_count": 9,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"block.header"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[('prevhash', <rlp.sedes.binary.Binary at 0x108e22110>),\n",
" ('uncles_hash', <rlp.sedes.binary.Binary at 0x108e22110>),\n",
" ('coinbase', <rlp.sedes.binary.Binary at 0x108e130d0>),\n",
" ('state_root', <rlp.sedes.binary.Binary at 0x108e22150>),\n",
" ('tx_list_root', <rlp.sedes.binary.Binary at 0x108e22150>),\n",
" ('receipts_root', <rlp.sedes.binary.Binary at 0x108e22150>),\n",
" ('bloom', <rlp.sedes.big_endian_int.BigEndianInt at 0x108e220d0>),\n",
" ('difficulty', <rlp.sedes.big_endian_int.BigEndianInt at 0x108dfc410>),\n",
" ('number', <rlp.sedes.big_endian_int.BigEndianInt at 0x108dfc410>),\n",
" ('gas_limit', <rlp.sedes.big_endian_int.BigEndianInt at 0x108dfc410>),\n",
" ('gas_used', <rlp.sedes.big_endian_int.BigEndianInt at 0x108dfc410>),\n",
" ('timestamp', <rlp.sedes.big_endian_int.BigEndianInt at 0x108dfc410>),\n",
" ('extra_data', <rlp.sedes.binary.Binary at 0x108dfc2d0>),\n",
" ('mixhash', <rlp.sedes.binary.Binary at 0x108dfc2d0>),\n",
" ('nonce', <rlp.sedes.binary.Binary at 0x108dfc2d0>)]"
]
},
"execution_count": 10,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"block.header.fields"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'53e106c52d0239a68f1684f4872845fe57446aaef4cce908abce2fcef8ea7b45'"
]
},
"execution_count": 11,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"block.header.prevhash.encode('hex')"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 2",
"language": "python",
"name": "python2"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 2
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython2",
"version": "2.7.13"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment