Permalink
Browse files

Watch the staking contract

  • Loading branch information...
masonforest committed Oct 30, 2018
1 parent 6f56649 commit 479935c1bcb86b5a59e0f0de7d51050ce73c7aab
@@ -24,3 +24,4 @@ erl_crash.dump
# Ignore package tarball (built via "mix hex.build").
blacksmith-*.tar

config/*.secret.ex
@@ -1,3 +1,4 @@
use Mix.Config
config :blacksmith, ecto_repos: [Blacksmith.Repo]
import_config "#{Mix.env()}.exs"
import_config "#{Mix.env}.secret.exs"
@@ -1,6 +1,16 @@
use Mix.Config
private_key = <<238, 163, 60, 8, 21, 24, 67, 133, 67, 178, 163, 66, 57, 255, 25, 13, 1, 184, 12, 138, 211, 125, 187, 121, 14, 43, 17, 237, 131, 168, 205, 133>>
config :blacksmith, base_contracts_path: "./base_contracts"
config :blacksmith, port: 4047
config :blacksmith, private_key: private_key
config :blacksmith, staking_contract_address: "9cEe17E3614aAd1CacF3E003c83350ad6fd761C6" |> Base.decode16!(case: :mixed)

config :ex_wire, private_key, private_key
# private_key:
# <<18, 116, 234, 41, 220, 113, 180, 178, 230, 67, 159, 221, 16, 149, 69, 232, 193, 88, 94, 43,
# 22, 188, 212, 82, 54, 254, 32, 251, 249, 25, 167, 13>>
config :ethereumex, :web3_url, "wss://rinkeby.infura.io/ws"
config :ethereumex, :client_type, :websocket

config :blacksmith, Blacksmith.Repo,
adapter: Ecto.Adapters.Postgres,
No changes.
@@ -1,8 +1,17 @@
use Mix.Config
config :blacksmith, base_contracts_path: "./base_contracts"
config :blacksmith, port: 4047
config :blacksmith, staking_contract_address: "9cEe17E3614aAd1CacF3E003c83350ad6fd761C6" |> Base.decode16!(case: :mixed)
config :blacksmith, auto_forge: true

config :ex_wire,
private_key:
<<18, 116, 234, 41, 220, 113, 180, 178, 230, 67, 159, 221, 16, 149, 69, 232, 193, 88, 94, 43,
22, 188, 212, 82, 54, 254, 32, 251, 249, 25, 167, 13>>
config :ethereumex, :web3_url, "wss://rinkeby.infura.io/ws"
config :ethereumex, :client_type, :websocket


config :blacksmith, Blacksmith.Repo,
adapter: Ecto.Adapters.Postgres,
username: "masonf",
@@ -0,0 +1,2 @@
use Mix.Config
config :blacksmith, private_key: Base.decode16!("1274EA29DC71B4B2E6439FDD109545E8C1585E2B16BCD45236FE20FBF919A70D")
@@ -13,10 +13,11 @@ defmodule Blacksmith.Application do
children = [
supervisor(Blacksmith.Repo, []),
{Redis, name: Redis},
{TransactionPool, name: TransactionPool},
{TransactionProccessor, name: TransactionProccessor},
{StakingContractMonitor, name: StakingContractMonitor},
{Forger, name: Forger},
{VM, name: VM},
{TransactionPool, name: TransactionPool},
Plug.Adapters.Cowboy2.child_spec(
scheme: :http,
plug: Router,

This file was deleted.

Oops, something went wrong.
@@ -43,10 +43,11 @@ defmodule Forger do
case Redix.command(redis, ["BRPOP", "transactions::done", 1]) do
{:ok, ["transactions::done", receipt]} ->
GenServer.cast(self(), {:forge, [receipt]})

{:ok, nil} ->
nil
end


if auto_forge do
GenServer.cast(self(), :auto_forge)
end
@@ -62,6 +63,7 @@ defmodule Forger do
state = Map.put(state, :subscribers, [])
{:noreply, state}
end

def handle_cast({:forge, receipts}, state) do
{:noreply, state}
end
@@ -29,6 +29,11 @@ defmodule Models.Block do

def hash(block), do: Crypto.hash(to_binary(block))

def forge() do
TransactionProccessor.proccess_transactions()
end


defp to_binary(%{
parent_block: parent_block,
number: number,
@@ -33,17 +33,17 @@ defmodule Models.Contract do
address: <<0::256>>,
contract_name: contract_name,
method: method,
params: params,
params: params
}) do
VM.get(%{
code: system_code(contract_name),
env: %{
address: Constants.system_address(),
contract_name: contract_name
},
method: method,
params: params
})
code: system_code(contract_name),
env: %{
address: Constants.system_address(),
contract_name: contract_name
},
method: method,
params: params
})
end

def system_code(contract_name) do
@@ -31,6 +31,7 @@ defmodule Router do
result =
conn
|> parse_get_request()

{:ok, result} = Contract.get(result)

send_resp(conn, 200, result)
@@ -0,0 +1,45 @@
defmodule StakingContractMonitor do
use GenServer
use Utils
alias ABI.TypeDecoder
alias Models.Block

def start_link(name) do
GenServer.start_link(__MODULE__, %{name: name})
end

def init(state) do
Ethereumex.WebSocketClient.eth_subscribe("newHeads")

{:ok, state}
end

def handle_info(_block = %{"hash" => _hash}, state) do
private_key = Application.fetch_env!(:blacksmith, :private_key)
address = private_key_to_address(private_key)
contract_address = Application.fetch_env!(:blacksmith, :staking_contract_address)

winner = web3_call(contract_address, :winner, [], [:address])

if winner == address do
# Block.forge()
end

{:noreply, state}
end

defp web3_call(contract_address, method, args, return_type) do
abi_encoded_data = ABI.encode("#{method}()", args) |> Base.encode16(case: :lower)
{:ok, result_bytes} = Ethereumex.WebSocketClient.eth_call(%{
data: "0x" <> abi_encoded_data,
to: "0x" <> Base.encode16(contract_address)
})


result_bytes
|> String.slice(2..-1)
|> Base.decode16!(case: :lower)
|> TypeDecoder.decode_raw(return_type)
|> List.first
end
end
@@ -1,5 +1,4 @@
defmodule TransactionPool do
@channel "transactions"
use GenServer

def start_link(opts) do
@@ -32,9 +31,6 @@ defmodule TransactionPool do
receive do
{:transaction, :done, <<return_code::size(32), result::binary>>} ->
{return_code, result}

other ->
IO.inspect(other)
end
end

@@ -44,9 +40,9 @@ defmodule TransactionPool do

def handle_call(
{:add, transaction},
{pid, _reference},
{_pid, _reference},
state = %{
redis: redis
redis: _redis
}
) do
Redis.push("transactions::queued", transaction)
@@ -2,6 +2,7 @@ defmodule TransactionProccessor do
use GenServer

@crate "transaction_processor"

def start_link(opts) do
Port.open({:spawn_executable, path_to_executable},
args: ["redis://127.0.0.1/"]
@@ -10,8 +11,37 @@ defmodule TransactionProccessor do
GenServer.start_link(__MODULE__, %{}, opts)
end

def init(_args) do
{:ok, nil}
def init(state) do
{:ok, redis} = Redix.start_link()

{:ok,
Map.merge(state, %{
redis => redis,
})
}
end

def proccess_transactions() do
start_proccessing_transactions()
:timer.apply_after(15000, __MODULE__, :start_proccessing_transactions, [])
end

def start_proccessing_transactions do
GenServer.cast(__MODULE__, {:start_proccessing_transactions})
end

def stop_proccessing_transactions do
GenServer.cast(__MODULE__, {:start_proccessing_transactions})
end

def handle_cast({:start_proccessing_transactions}, state) do
Redis.set_binary("continue_processing_transactions", true)
{:noreply, state}
end

def handle_cast({:stop_proccessing_transactions}, state) do
Redis.set_binary("continue_processing_transactions", false)
{:noreply, state}
end

def path_to_executable() do
@@ -20,4 +20,17 @@ defmodule Utils do
def ok({:ok, x}), do: x
def ok({:error, x}), do: raise(x)
def ok(x), do: raise(x)

def private_key_to_address(private_key) do
private_key_to_public_key(private_key)
|> ExthCrypto.Key.der_to_raw()
|> ExthCrypto.Hash.Keccak.kec()
|> EVM.Helpers.take_n_last_bytes(20)
end

def private_key_to_public_key(private_key) do
private_key
|> ExthCrypto.Signature.get_public_key()
|> elem(1)
end
end
21 mix.exs
@@ -34,31 +34,40 @@ defmodule Blacksmith.Mixfile do
def application do
[
mod: {Blacksmith.Application, []},
extra_applications: [:cowboy, :ranch, :redix, :plug, :sha3]
extra_applications: extra_applications(Mix.env),
]
end

defp extra_applications(:dev), do: extra_applications(:all) ++ [:remix]
defp extra_applications(_all), do: [:cowboy, :ranch, :redix, :plug, :sha3]

defp deps do
[
{:benchee, "~> 0.11", only: [:dev, :test]},
{:cbor, "~> 0.1"},
{:abi, "~> 0.1"},
{:cors_plug, "~> 1.5"},
{:cowboy, "~> 2.3"},
{:dialyxir, "~> 0.5", only: [:dev, :test], runtime: false},
{:distillery, "~> 1.5", runtime: false},
{:ecto, "~> 2.2"},
{:dialyxir, "~> 1.0.0-rc.3", only: [:dev, :test], runtime: false},
{:distillery, "~> 2.0", runtime: false},
{:ecto, "~> 2.1"},
{:ex_machina, "~> 2.2", only: :test},
{:httpoison, "~> 1.1", only: [:dev, :test]},
{:poison, "~> 4.0", override: true},
{:httpoison, "~> 1.3"},
{:libsodium, "~> 0.0.10"},
{:binary, "~> 0.0.5"},
{:ok, "~> 2.0"},
{:plug, "~> 1.5"},
{:postgrex, "~> 0.13.0"},
{:exth_crypto, "~> 0.1.4", override: true},
{:mana, [github: "mana-ethereum/mana", app: false]},
{:ethereumex, [github: "masonforest/ethereumex", branch: "websocket-client", override: true]},
{:redix, "0.6.0"},
{:remix, "~> 0.0.1", only: :dev},
{:redix_pubsub, "~> 0.4.2"},
# Pull down [this branch](https://github.com/cristianberneanu/rustler)
# Until [this PR](https://github.com/hansihe/rustler/pull/166) is merged
{:rustler, [path: "../rustler/rustler_mix"]},
{:rustler, [path: "../rustler/rustler_mix", override: true]},
{:sha3, "2.0.0"}
]
end
Oops, something went wrong.

0 comments on commit 479935c

Please sign in to comment.