Skip to content

Commit

Permalink
change release config style, fix issues with runnable executable, add…
Browse files Browse the repository at this point in the history
… rpc
  • Loading branch information
alexdovzhanyn committed Dec 8, 2018
1 parent 395de20 commit 29cc012
Show file tree
Hide file tree
Showing 12 changed files with 233 additions and 59 deletions.
30 changes: 30 additions & 0 deletions config/defaults.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# -------------- Configuration for Elixium Core --------------
[elixium_core]
# Maximum amount of inbound/outbound connections to have at any
# given moment
max_handlers = 10

# URL used to bootstrap node connections (fetch list of peers)
# on initial connection to the network
bootstrap_url = "https://registry.testnet.elixium.app/"

# Path to store elixium related data (blocks, utxos, peers, etc)
data_path = "~/.elixium"

# The port on which to accept inbound connections (must be
# port-forwarded)
port = 31013

# ------------ End Configuration for Elixium Core ------------

# ------------- Configuration for Elixium Miner --------------
[elixium_node]
# Enable/disable RPC
rpc = false

# Which port to run RPC on (if enabled)
rpc_port = 32123

[logger.info]
level = "info"
path = "./log/info.log"
9 changes: 1 addition & 8 deletions lib/commands/usage.ex
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ defmodule Command.Usage do
def run do
IO.puts "
USAGE
elixium_miner <command> --option value
elixium_miner <command>
COMMANDS
foreground Runs miner with console output in the foreground
Expand All @@ -12,13 +12,6 @@ COMMANDS
remote_console Opens a remote console in the context of a running miner
dropchain Delete all block and utxo data
genkey Generate a new Elixium address keypair
OPTIONS
--address Specifies which Elixium address to credit with rewards and block fees
--port What port to use when connecting to the network (defaults to 31013)
--rpc Enable RPC JSON commands
--rpcPort Use specific port for RPC (defaults to 32123)
--maxHandlers Specify the maximum amount of inbound & outbound connections
"
end

Expand Down
8 changes: 8 additions & 0 deletions lib/release/logger_level_transformer.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
defmodule LoggerLevelTransformer do
use Toml.Transform

def transform(:level, level) when is_atom(level), do: level
def transform(:level, level), do: String.to_atom(level)
# Ignore all other values
def transform(_key, v), do: v
end
24 changes: 24 additions & 0 deletions lib/release/set_overlay_file_permissions.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
defmodule SetOverlayFilePermissions do
use Mix.Releases.Plugin

def after_assembly(%Release{} = release, _opts) do
info "This is executed just after assembling, and just prior to packaging the release"

{_, path} =
release
|> Map.get(:resolved_overlays)
|> Enum.find(fn {name, _path} -> name == "run" end)

File.chmod(path, 0o755)

release
end

def before_assembly(%Release{} = release, _opts), do: release

def before_package(%Release{} = release, _opts), do: release

def after_package(%Release{} = release, _opts), do: release

def after_cleanup(_args, _opts), do: :ok
end
92 changes: 92 additions & 0 deletions lib/rpc/router.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
defmodule ElixiumNode.RPC.Router do
alias Elixium.Store.Ledger

def get("/") do
"ROOT ROUTE"
end

def get("/block_at_height/" <> index) do
try do
block =
index
|> String.to_integer()
|> Ledger.block_at_height()
|> nonbinary_block()

case block do
:none -> "Block #{block} not found."
block -> Poison.encode!(block)
end

rescue
ArgumentError -> "Please provide an integer block index."
end
end

def get("/block_by_hash/" <> hash) do
hash
|> Ledger.retrieve_block()
|> nonbinary_block()
|> Poison.encode!()
end

def get("/latest_block") do
Ledger.last_block()
|> nonbinary_block()
|> Poison.encode!()
end

def get("/last_n_blocks/" <> count) do
try do
count
|> String.to_integer()
|> Ledger.last_n_blocks()
|> Enum.map(&nonbinary_block/1)
|> Poison.encode!()
rescue
ArgumentError -> "Please provide an integer"
end
end

def get("/connected_nodes") do
Elixium.Node.Supervisor.connected_handlers()
|> Enum.map(&Process.info/1)
|> Enum.map(& Keyword.get(&1, :dictionary))
|> Enum.map(& Keyword.get(&1, :connected))
|> Poison.encode!()
end

def get(_), do: "404"

@doc """
Converts binary data within a block to its non-binary equivalent
"""
def nonbinary_block(block) do
b = %{
block |
nonce: :binary.decode_unsigned(block.nonce),
version: :binary.decode_unsigned(block.version),
index: :binary.decode_unsigned(block.index)
}

transactions = Enum.map(block.transactions, fn tx ->
tx =
if Map.has_key?(tx, :sigs) do
sigs = Enum.map(tx.sigs, fn {addr, sig} -> [addr, Base.encode64(sig)] end)

Map.put(tx, :sigs, sigs)
else
tx
end

Map.put(tx, :size, tx |> :erlang.term_to_binary |> byte_size)
end)

b = Map.put(b, :transactions, transactions)

b = Map.put(b, :size, Elixium.BlockEncoder.encode(block) |> byte_size)

Map.put(b, :reward, Elixium.Block.calculate_block_reward(b.index))
end

end
48 changes: 48 additions & 0 deletions lib/rpc/rpc.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
defmodule ElixiumNode.RPC do
use GenServer
require IEx
require Logger

def start_link(_args) do
port = Application.get_env(:elixium_node, :rpc_port)

{:ok, socket} = :gen_tcp.listen(port, packet: :http, active: true, exit_on_close: false)
GenServer.start_link(__MODULE__, socket, name: __MODULE__)
end

def init(socket) do
Process.send_after(self(), :start_accept, 1000)

{:ok, %{listen: socket}}
end

def start_accept do
GenServer.cast(__MODULE__, :start_accept)
end

def handle_info(:start_accept, state) do
Logger.info("RPC listening")

{:ok, socket} = :gen_tcp.accept(state.listen)

state = Map.put(state, :socket, socket)
{:noreply, state}
end

def handle_info({:http, socket, {:http_request, :GET, {:abs_path, path}, _}}, state) do
data = ElixiumNode.RPC.Router.get(to_string(path))

:gen_tcp.send(socket, "HTTP/1.1 200 OK\nAccess-Control-Allow-Origin: *\nContent-Length: #{byte_size(data)}\nContent-Type: application/json\n\n#{data}\n")

{:noreply, state}
end

def handle_info({:tcp_closed, _}, state) do
{:ok, socket} = :gen_tcp.accept(state.listen)

state = Map.put(state, :socket, socket)
{:noreply, state}
end

def handle_info(_any, state), do: {:noreply, state}
end
15 changes: 15 additions & 0 deletions lib/rpc/supervisor.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
defmodule ElixiumNode.RPC.Supervisor do
use Supervisor

def start_link(_args) do
Supervisor.start_link(__MODULE__, [], name: __MODULE__)
end

def init(_args) do
children = [
ElixiumNode.RPC
]

Supervisor.init(children, strategy: :one_for_one)
end
end
13 changes: 2 additions & 11 deletions lib/supervisor.ex
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,13 @@ defmodule ElixiumNode.Supervisor do
end

def init(_args) do
port =
:port
|> Util.get_arg("-1")
|> String.to_integer()
|> case do
-1 -> nil
p -> p
end

children = [
{Elixium.Node.Supervisor, [:"Elixir.ElixiumNode.PeerRouter", port]},
{Elixium.Node.Supervisor, [:"Elixir.ElixiumNode.PeerRouter"]},
ElixiumNode.PeerRouter.Supervisor
]

children =
if Util.get_arg(:rpc) do
if Application.get_env(:elixium_node, :rpc) do
[ElixiumNode.RPC.Supervisor | children]
else
children
Expand Down
35 changes: 0 additions & 35 deletions lib/util.ex

This file was deleted.

9 changes: 6 additions & 3 deletions mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ defmodule ElixiumNode.MixProject do
def project do
[
app: :elixium_node,
version: "1.0.1",
version: "1.1.0",
elixir: "~> 1.7",
start_permanent: true,
deps: deps()
Expand All @@ -28,8 +28,11 @@ defmodule ElixiumNode.MixProject do
# Run "mix help deps" to learn about dependencies.
defp deps do
[
{:elixium_core, "~> 0.4"},
{:distillery, "~> 2.0"}
{:elixium_core, "~> 0.5"},
{:poison, "~> 3.1"},
{:distillery, "~> 2.0"},
{:toml, "~> 0.5"},
{:logger_file_backend, "~> 0.0.10"}
]
end
end
7 changes: 6 additions & 1 deletion rel/config.exs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ environment :dev do
end

environment :prod do
plugin SetOverlayFilePermissions
set include_erts: true
set include_src: false
set cookie: :"p@|CnzPH9,v[~/lDm,Y:*W>@dq8d:s~b5ofzKc&c1S8]53mj4R!OIQnz@%|Xl&DW"
Expand All @@ -44,8 +45,12 @@ environment :prod do
dropchain: "rel/commands/drop_chain.sh",
usage: "rel/commands/usage.sh"
]
set config_providers: [
{Toml.Provider, [path: "${RELEASE_ROOT_DIR}/config.toml", transforms: [LoggerLevelTransformer]]}
]
set overlays: [
{:copy, "rel/overlays/run.sh", "run.sh"},
{:copy, "rel/overlays/run.sh", "run"},
{:copy, "config/defaults.toml", "config.toml"},
]
end

Expand Down
2 changes: 1 addition & 1 deletion rel/overlays/run.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/bin/bash
#!/usr/bin/env bash

SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd -P)"

Expand Down

0 comments on commit 29cc012

Please sign in to comment.