Skip to content

Instantly share code, notes, and snippets.

@hayesgm
Last active October 19, 2018 13:24
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save hayesgm/5c0e45f73adaff8d165e04ed2867d69e to your computer and use it in GitHub Desktop.
Save hayesgm/5c0e45f73adaff8d165e04ed2867d69e to your computer and use it in GitHub Desktop.
Simple Block Loader
# ETHEREUM_SCHEME=https ETHEREUM_HOST="ropsten.infura.io" ETHEREUM_PORT=443 iex -S mix
Application.put_env(EVM, :debugger, true)
EVM.Debugger.Breakpoint.init()
EVM.Debugger.Breakpoint.set_breakpoint(%EVM.Debugger.Breakpoint{conditions: [address: <<188, 31, 252, 22, 32, 218, 20, 104, 98, 74, 89, 108, 184, 65, 211, 94, 107, 47, 31, 182>>]})
load_hex = fn("0x" <> hex_string) ->
padded_hex_string = if rem(byte_size(hex_string), 2) == 1, do: "0" <> hex_string, else: hex_string
{:ok, hex} = Base.decode16(padded_hex_string, case: :lower)
hex
end
get_block = fn(block_map) ->
b = block_map["result"]
%Blockchain.Block{
block_hash: b["hash"] |> load_hex.(),
header: %Block.Header{
parent_hash: b["parentHash"] |> load_hex.(),
ommers_hash: b["sha3Uncles"] |> load_hex.(),
beneficiary: b["miner"] |> load_hex.(),
state_root: b["stateRoot"] |> load_hex.(),
transactions_root: b["transactionsRoot"] |> load_hex.(),
receipts_root: b["receiptsRoot"] |> load_hex.(),
logs_bloom: b["logsBloom"] |> load_hex.(),
difficulty: b["difficulty"] |> load_hex.() |> :binary.decode_unsigned(),
number: b["number"] |> load_hex.() |> :binary.decode_unsigned(),
gas_limit: b["gasLimit"] |> load_hex.() |> :binary.decode_unsigned(),
gas_used: b["gasUsed"] |> load_hex.() |> :binary.decode_unsigned(),
timestamp: b["timestamp"] |> load_hex.() |> :binary.decode_unsigned(),
extra_data: b["extraData"] |> load_hex.(),
mix_hash: b["mixHash"] |> load_hex.(),
nonce: b["nonce"] |> load_hex.(),
},
transactions:
for trx <- b["transactions"] do
%Blockchain.Transaction{
nonce: trx["nonce"] |> load_hex.() |> :binary.decode_unsigned,
gas_price: trx["gasPrice"] |> load_hex.() |> :binary.decode_unsigned,
gas_limit: trx["gas"] |> load_hex.() |> :binary.decode_unsigned,
to: (if trx["to"], do: trx["to"] |> load_hex.(), else: <<>>),
value: trx["value"] |> load_hex.() |> :binary.decode_unsigned,
v: trx["v"] |> load_hex.() |> :binary.decode_unsigned,
r: trx["r"] |> load_hex.() |> :binary.decode_unsigned,
s: trx["s"] |> load_hex.() |> :binary.decode_unsigned,
init: (if trx["to"] |> is_nil, do: trx["input"] |> load_hex.(), else: <<>>),
data: (if trx["to"], do: trx["input"] |> load_hex.(), else: <<>>),
}
end,
ommers: [],
}
# TODO: Add ommers
end
db = MerklePatriciaTree.Test.random_ets_db()
tree = Blockchain.Blocktree.new_tree()
chain = Blockchain.Test.ropsten_chain()
{:ok, gen_block_map} = Ethereumex.HttpClient.eth_get_block_by_number(["0x0", true])
block_1 = get_block.(gen_block_map)
{:ok, tree_1} = Blockchain.Blocktree.verify_and_add_block(tree, chain, block_1, db)
{block_map} = Enum.reduce(1..12, {tree_1, block_1}, fn n, {tree, parent} ->
IO.puts("------ GETTING BLOCK #{n} -----")
{:ok, block_map} = Ethereumex.HttpClient.eth_get_block_by_number(["0x#{Integer.to_string(n, 16)}", true])
next_block = get_block.(block_map) |> IO.inspect
{:ok, next_tree} = Blockchain.Blocktree.verify_and_add_block(tree, chain, next_block, db) |> IO.inspect
IO.puts("------ BLOCK #{n} VERIFIED ------")
{next_tree, next_block}
end)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment