-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
change release config style, fix issues with runnable executable, add…
… rpc
- Loading branch information
1 parent
395de20
commit 29cc012
Showing
12 changed files
with
233 additions
and
59 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)" | ||
|
||
|