Skip to content

Commit

Permalink
Added Block model
Browse files Browse the repository at this point in the history
  • Loading branch information
masonforest committed Nov 2, 2018
1 parent e98e165 commit 48cd1f1
Show file tree
Hide file tree
Showing 18 changed files with 195 additions and 126 deletions.
4 changes: 2 additions & 2 deletions config/test.exs
Expand Up @@ -17,10 +17,10 @@ config :ethereumex, :web3_url, "wss://rinkeby.infura.io/ws"
config :ethereumex, :client_type, :websocket

config :blacksmith, Blacksmith.Repo,
adapter: Ecto.Adapters.Postgres,
pool: Ecto.Adapters.SQL.Sandbox,
username: "masonf",
password: "",
database: "test_phoenix_dev",
hostname: "localhost",
pool_size: 10,
loggers: []
log: false
4 changes: 3 additions & 1 deletion lib/blacksmith/repo.ex
@@ -1,3 +1,5 @@
defmodule Blacksmith.Repo do
use Ecto.Repo, otp_app: :blacksmith
use Ecto.Repo,
otp_app: :blacksmith,
adapter: Ecto.Adapters.Postgres
end
7 changes: 5 additions & 2 deletions lib/crypto.ex
Expand Up @@ -15,14 +15,17 @@ defmodule Crypto do
:libsodium_crypto_sign_ed25519.detached(message, secret_key)
end

def valid_signature?(signature, message, public_key) do
def valid_signature_ed25519?(signature, message, public_key) do
case :libsodium_crypto_sign_ed25519.verify_detached(signature, message, public_key) do
0 -> true
-1 -> false
end
end

def public_key_from_private_key(private_key) do
def valid_signature_ethereum?(signature, message, address) do
end

def public_key_from_private_key_ed25519(private_key) do
:libsodium_crypto_sign_ed25519.sk_to_pk(private_key)
end
end
52 changes: 52 additions & 0 deletions lib/http/signature_auth.ex
@@ -0,0 +1,52 @@
defmodule HTTP.SignatureAuth do
defmodule UnauthorizedError do
@moduledoc """
Error raised when a signature is invalid
"""

defexception message: "Invaild Signature", plug_status: 401
end

def verify_block_signature(conn) do
public_key = conn.params.sender
signature = get_signature(conn)
body = Enum.fetch!(conn.assigns.raw_body, 0)

if Crypto.valid_signature_ed25519?(
signature,
body,
public_key
) do
conn
else
throw(UnauthorizedError)
end
end

def verify_ed25519_signature(conn) do
public_key = conn.params.sender
signature = get_signature(conn)
body = Enum.fetch!(conn.assigns.raw_body, 0)

if Crypto.valid_signature_ed25519?(
signature,
body,
public_key
) do
conn
else
throw(UnauthorizedError)
end
end

defp get_signature(conn) do
[authorization] = Plug.Conn.get_req_header(conn, "authorization")

[
"Signature",
signature_hex
] = String.split(authorization, " ")

Base.decode16!(signature_hex, case: :lower)
end
end
52 changes: 33 additions & 19 deletions lib/models/block.ex
Expand Up @@ -5,15 +5,18 @@ defmodule Models.Block do
alias Blacksmith.Repo

schema "blocks" do
# belongs_to :parent, Block
belongs_to(:parent, __MODULE__)
field(:number, :integer)
# field(:total_burned, :integer)
# field(:winner, :binary)
# field(:state_changes_hash, :binary)
field(:total_burned, :integer)
field(:winner, :binary)
field(:changeset_hash, :binary)
field(:block_hash, :binary)
timestamps()
end

def max_burned(query \\ __MODULE__), do: from(q in query, order_by: q.total_burned)
def best_block(query \\ __MODULE__), do:
from(q in query, order_by: q.total_burned)
|> Ecto.Query.first

def latest(query \\ __MODULE__, count), do: from(q in query, order_by: q.number, limit: ^count)

Expand All @@ -22,27 +25,38 @@ defmodule Models.Block do
|> cast(params, [:number])
|> validate_required([
:number,
# :state_changes_hash,
# :winner
:changeset_hash,
:block_hash,
:winner
])
end

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

def forge() do
def forge(winner) do
TransactionProccessor.proccess_transactions(1)
TransactionProccessor.wait_until_done()
block = %__MODULE__{number: 0}
IO.inspect block
{:ok, changeset} = Redis.get("changeset")

parent = best_block() |> Repo.one()
Redis.delete("changeset")

block = %__MODULE__{
parent: parent,
winner: winner,
number: 0,
changeset_hash: Crypto.hash(changeset)
}
block = Map.put(block, :block_hash, hash(block))

Repo.insert(block)
block
end

# defp to_binary(%{
# number: number,
# winner: winner,
# state_changes_hash: state_changes_hash
# }) do
# <<number::size(256)>> <> winner <> state_changes_hash
# end
defp to_binary(%{
number: number,
winner: winner,
changeset_hash: changeset_hash
}) do
<<number::size(256)>> <> winner <> changeset_hash
end
end
61 changes: 0 additions & 61 deletions lib/plug/signature_auth.ex

This file was deleted.

6 changes: 3 additions & 3 deletions lib/redis.ex
Expand Up @@ -11,8 +11,8 @@ defmodule Redis do
{:ok, redis}
end

def get_binary(key) do
GenServer.call(Redis, {:get_binary, key})
def get(key) do
GenServer.call(Redis, {:get, key})
end

def reset() do
Expand Down Expand Up @@ -176,7 +176,7 @@ defmodule Redis do
{:reply, value, redis}
end

def handle_call({:get_binary, key}, _from, redis) do
def handle_call({:get, key}, _from, redis) do
value =
Redix.command(redis, [
"GET",
Expand Down
29 changes: 14 additions & 15 deletions lib/router.ex
Expand Up @@ -17,11 +17,11 @@ defmodule Router do
cbor_decoder: Cbor
)

plug(
SignatureAuth,
only_methods: ["POST", "PUT"]
)

# plug(
# SignatureAuth,
# only_methods: ["POST", "PUT"]
# )
#
use Plug.ErrorHandler

plug(:match)
Expand All @@ -37,6 +37,14 @@ defmodule Router do
send_resp(conn, 200, result)
end

post "/blocks" do
winner = EllipitcoinStakingContract.winner()
HTTP.SignatureAuth.verify_block_signature(conn, winner)

Block.apply(conn.params)
send_resp(conn, 200, "")
end

get "/blocks" do
_number =
if conn.query_params["number"] do
Expand All @@ -48,18 +56,9 @@ defmodule Router do
send_resp(conn, 200, "{\"blocks\": []}")
end

put "/contracts" do
TransactionPool.add(conn.assigns.body)

result =
receive do
{:transaction_forged, transaction} -> transaction
end

send_resp(conn, 200, result)
end

post "/transactions" do
HTTP.SignatureAuth.verify_ed25519_signature(conn)
Contract.post(conn.params)

send_resp(conn, 200, "")
Expand Down
10 changes: 3 additions & 7 deletions lib/staking_contract_monitor.ex
Expand Up @@ -13,17 +13,13 @@ defmodule StakingContractMonitor do
{: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)
def winner() do
contract_address = Application.fetch_env!(:blacksmith, :staking_contract_address)

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

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

def handle_info(_block = %{"hash" => _hash}, state) do
{:noreply, state}
end

Expand Down
7 changes: 4 additions & 3 deletions mix.exs
Expand Up @@ -50,15 +50,16 @@ defmodule Blacksmith.Mixfile do
{:cowboy, "~> 2.3"},
{: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},
{:ecto, "~> 3.0"},
{:ex_machina, [github: "thoughtbot/ex_machina", branch: "dependabot/hex/ecto-3.0.0", only: :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"},
{:ecto_sql, "~> 3.0-rc.1"},
{:postgrex, "~> 0.14.0"},
{:exth_crypto, "~> 0.1.4", override: true},
{:mana, [github: "mana-ethereum/mana", app: false]},
{:ethereumex,
Expand Down
10 changes: 6 additions & 4 deletions mix.lock
Expand Up @@ -13,20 +13,21 @@
"cowboy": {:hex, :cowboy, "2.4.0", "f1b72fabe9c8a5fc64ac5ac85fb65474d64733d1df52a26fad5d4ba3d9f70a9f", [:rebar3], [{:cowlib, "~> 2.3.0", [hex: :cowlib, repo: "hexpm", optional: false]}, {:ranch, "~> 1.5.0", [hex: :ranch, repo: "hexpm", optional: false]}], "hexpm"},
"cowlib": {:hex, :cowlib, "2.3.0", "bbd58ef537904e4f7c1dd62e6aa8bc831c8183ce4efa9bd1150164fe15be4caa", [:rebar3], [], "hexpm"},
"credo": {:hex, :credo, "0.10.2", "03ad3a1eff79a16664ed42fc2975b5e5d0ce243d69318060c626c34720a49512", [:mix], [{:bunt, "~> 0.2.0", [hex: :bunt, repo: "hexpm", optional: false]}, {:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: false]}], "hexpm"},
"db_connection": {:hex, :db_connection, "1.1.3", "89b30ca1ef0a3b469b1c779579590688561d586694a3ce8792985d4d7e575a61", [:mix], [{:connection, "~> 1.0.2", [hex: :connection, repo: "hexpm", optional: false]}, {:poolboy, "~> 1.5", [hex: :poolboy, repo: "hexpm", optional: true]}, {:sbroker, "~> 1.0", [hex: :sbroker, repo: "hexpm", optional: true]}], "hexpm"},
"db_connection": {:hex, :db_connection, "2.0.0", "e28c878035eec1b891e629555ddfed6456e43d8482340a81924da8c85eb6b8a1", [:mix], [{:connection, "~> 1.0.2", [hex: :connection, repo: "hexpm", optional: false]}], "hexpm"},
"decimal": {:hex, :decimal, "1.5.0", "b0433a36d0e2430e3d50291b1c65f53c37d56f83665b43d79963684865beab68", [:mix], [], "hexpm"},
"deep_merge": {:hex, :deep_merge, "0.1.1", "c27866a7524a337b6a039eeb8dd4f17d458fd40fbbcb8c54661b71a22fffe846", [:mix], [], "hexpm"},
"dialyxir": {:hex, :dialyxir, "1.0.0-rc.3", "774306f84973fc3f1e2e8743eeaa5f5d29b117f3916e5de74c075c02f1b8ef55", [:mix], [], "hexpm"},
"distillery": {:hex, :distillery, "2.0.10", "e9f1f1d3f4a89996a3e1a555872feed8a3a73e3d10b51886941382d29ca58f99", [:mix], [{:artificery, "~> 0.2", [hex: :artificery, repo: "hexpm", optional: false]}], "hexpm"},
"earmark": {:hex, :earmark, "1.2.6", "b6da42b3831458d3ecc57314dff3051b080b9b2be88c2e5aa41cd642a5b044ed", [:mix], [], "hexpm"},
"ecto": {:hex, :ecto, "2.2.11", "4bb8f11718b72ba97a2696f65d247a379e739a0ecabf6a13ad1face79844791c", [:mix], [{:db_connection, "~> 1.1", [hex: :db_connection, repo: "hexpm", optional: true]}, {:decimal, "~> 1.2", [hex: :decimal, repo: "hexpm", optional: false]}, {:mariaex, "~> 0.8.0", [hex: :mariaex, repo: "hexpm", optional: true]}, {:poison, "~> 2.2 or ~> 3.0", [hex: :poison, repo: "hexpm", optional: true]}, {:poolboy, "~> 1.5", [hex: :poolboy, repo: "hexpm", optional: false]}, {:postgrex, "~> 0.13.0", [hex: :postgrex, repo: "hexpm", optional: true]}, {:sbroker, "~> 1.0", [hex: :sbroker, repo: "hexpm", optional: true]}], "hexpm"},
"ecto": {:hex, :ecto, "3.0.0", "059250d96f17f9c10f524fcb09d058f566691343e90318a161cf62a48f3912a9", [:mix], [{:decimal, "~> 1.5", [hex: :decimal, repo: "hexpm", optional: false]}, {:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: true]}, {:poison, "~> 2.2 or ~> 3.0", [hex: :poison, repo: "hexpm", optional: true]}], "hexpm"},
"ecto_sql": {:hex, :ecto_sql, "3.0.0-rc.1", "f7feaffa5af631b0d5728118a74ec9f4854371baff565d7006120c59f91353ca", [:mix], [{:db_connection, "~> 2.0", [hex: :db_connection, repo: "hexpm", optional: false]}, {:ecto, "~> 3.0.0", [hex: :ecto, repo: "hexpm", optional: false]}, {:mariaex, "~> 0.9.0-rc", [hex: :mariaex, repo: "hexpm", optional: true]}, {:postgrex, "~> 0.14.0", [hex: :postgrex, repo: "hexpm", optional: true]}, {:telemetry, "~> 0.2.0", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm"},
"ed25519": {:hex, :ed25519, "1.2.0", "9ce0b4e62de5b8d2035a5149bb322620b8d37b512c40758dedc84786fd5157fb", [:mix], [], "hexpm"},
"eleveldb": {:hex, :eleveldb, "2.2.20", "1fff63a5055bbf4bf821f797ef76065882b193f5e8095f95fcd9287187773b58", [:rebar3], [], "hexpm"},
"enacl": {:git, "https://github.com/jlouis/enacl.git", "c8403ab198b80863479c2ab5a9ccd0a8d73a57c4", []},
"ethereum": {:git, "https://github.com/exthereum/ethereum.git", "282ca2a23a897c5b9684ddf9abae2bf65691b039", []},
"ethereumex": {:git, "https://github.com/masonforest/ethereumex.git", "4f6c7adafe2ffdfd2e24cd10dc99ef78ff39e8a4", [branch: "websocket-client"]},
"ex_doc": {:hex, :ex_doc, "0.19.1", "519bb9c19526ca51d326c060cb1778d4a9056b190086a8c6c115828eaccea6cf", [:mix], [{:earmark, "~> 1.1", [hex: :earmark, repo: "hexpm", optional: false]}, {:makeup_elixir, "~> 0.7", [hex: :makeup_elixir, repo: "hexpm", optional: false]}], "hexpm"},
"ex_machina": {:hex, :ex_machina, "2.2.0", "fec496331e04fc2db2a1a24fe317c12c0c4a50d2beb8ebb3531ed1f0d84be0ed", [:mix], [{:ecto, "~> 2.1", [hex: :ecto, repo: "hexpm", optional: true]}], "hexpm"},
"ex_machina": {:git, "https://github.com/thoughtbot/ex_machina.git", "1df57b0a6778e10cd9be1b07627044a5a3e6f38f", [branch: "dependabot/hex/ecto-3.0.0"]},
"ex_rlp": {:hex, :ex_rlp, "0.3.1", "190554f7b26f79734fc5a772241eec14a71b2e83576e43f451479feb017013e9", [:mix], [], "hexpm"},
"excbor": {:git, "https://github.com/cabo/excbor.git", "3b69e45cb2d35eee5b91774300dd1b997c0d4c33", []},
"exleveldb": {:hex, :exleveldb, "0.14.0", "8e9353bbce38482d6971d254c6b98ceb50f3f179c94732b5d17db1be426fca18", [:mix], [{:eleveldb, "~> 2.2.20", [hex: :eleveldb, repo: "hexpm", optional: false]}], "hexpm"},
Expand Down Expand Up @@ -60,7 +61,7 @@
"plug": {:hex, :plug, "1.5.0", "224b25b4039bedc1eac149fb52ed456770b9678bbf0349cdd810460e1e09195b", [:mix], [{:cowboy, "~> 1.0.1 or ~> 1.1 or ~> 2.1", [hex: :cowboy, repo: "hexpm", optional: true]}, {:mime, "~> 1.0", [hex: :mime, repo: "hexpm", optional: false]}], "hexpm"},
"poison": {:hex, :poison, "4.0.1", "bcb755a16fac91cad79bfe9fc3585bb07b9331e50cfe3420a24bcc2d735709ae", [:mix], [], "hexpm"},
"poolboy": {:hex, :poolboy, "1.5.1", "6b46163901cfd0a1b43d692657ed9d7e599853b3b21b95ae5ae0a777cf9b6ca8", [:rebar], [], "hexpm"},
"postgrex": {:hex, :postgrex, "0.13.5", "3d931aba29363e1443da167a4b12f06dcd171103c424de15e5f3fc2ba3e6d9c5", [:mix], [{:connection, "~> 1.0", [hex: :connection, repo: "hexpm", optional: false]}, {:db_connection, "~> 1.1", [hex: :db_connection, repo: "hexpm", optional: false]}, {:decimal, "~> 1.0", [hex: :decimal, repo: "hexpm", optional: false]}], "hexpm"},
"postgrex": {:hex, :postgrex, "0.14.0", "f3d6ffea1ca8a156e0633900a5338a3d17b00435227726baed8982718232b694", [:mix], [{:connection, "~> 1.0", [hex: :connection, repo: "hexpm", optional: false]}, {:db_connection, "~> 2.0", [hex: :db_connection, repo: "hexpm", optional: false]}, {:decimal, "~> 1.0", [hex: :decimal, repo: "hexpm", optional: false]}, {:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: true]}], "hexpm"},
"protobuf": {:hex, :protobuf, "0.5.0", "ec9857903f8c49cf01ad5f1340956e19ebe0ef05e225b15b442f33b13031ce08", [:mix], [], "hexpm"},
"ranch": {:hex, :ranch, "1.5.0", "f04166f456790fee2ac1aa05a02745cc75783c2bfb26d39faf6aefc9a3d3a58a", [:rebar3], [], "hexpm"},
"redix": {:hex, :redix, "0.6.0", "b0ee9b66cd15b5fe72deeaba285e90b65dbf069b7be67f610d32d4304226b1b2", [:mix], [{:connection, "~> 1.0", [hex: :connection, repo: "hexpm", optional: false]}], "hexpm"},
Expand All @@ -71,6 +72,7 @@
"rustler": {:git, "https://github.com/cristianberneanu/rustler.git", "344ab4f0949297aad789f18b58086bbdca1133d5", []},
"sha3": {:hex, :sha3, "2.0.0", "fbe5db75a8389fdc810b682446c053b91e591d38d5422808f3a702ed2b270515", [:rebar3], [{:hex2bin, "1.0.0", [hex: :hex2bin, repo: "hexpm", optional: false]}], "hexpm"},
"ssl_verify_fun": {:hex, :ssl_verify_fun, "1.1.4", "f0eafff810d2041e93f915ef59899c923f4568f4585904d010387ed74988e77b", [:make, :mix, :rebar3], [], "hexpm"},
"telemetry": {:hex, :telemetry, "0.2.0", "5b40caa3efe4deb30fb12d7cd8ed4f556f6d6bd15c374c2366772161311ce377", [:mix], [], "hexpm"},
"unicode_util_compat": {:hex, :unicode_util_compat, "0.4.1", "d869e4c68901dd9531385bb0c8c40444ebf624e60b6962d95952775cac5e90cd", [:rebar3], [], "hexpm"},
"websockex": {:hex, :websockex, "0.4.1", "d7b7191ec3d5dd136683b60114405a5a130a175faade773a07cb52dbd98f9442", [:mix], [], "hexpm"},
}
4 changes: 2 additions & 2 deletions native/vm/src/db/redis.rs
Expand Up @@ -10,8 +10,8 @@ impl DB for redis::Connection {
.arg(key)
.arg(value)
.ignore()
.cmd("RPUSH")
.arg("state_changes")
.cmd("APPEND")
.arg("changeset")
.arg(([&key[..], &value[..]]).concat())
.ignore()
.query(self)
Expand Down

0 comments on commit 48cd1f1

Please sign in to comment.