Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion apps/aecore/lib/aecore.ex
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ defmodule Aecore do
supervisor(Aecore.Keys.Worker.Supervisor, []),
supervisor(Aecore.Chain.Worker.Supervisor, []),
supervisor(Aecore.Miner.Worker.Supervisor, []),
supervisor(Aecore.Txs.Pool.Worker.Supervisor, [])
supervisor(Aecore.Txs.Pool.Worker.Supervisor, []),
supervisor(Aecore.Peers.Worker, [])
]

Supervisor.start_link(children, strategy: :one_for_one)
Expand Down
84 changes: 84 additions & 0 deletions apps/aecore/lib/aecore/peers/worker.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
defmodule Aecore.Peers.Worker do
@moduledoc """
Peer manager module
"""

use GenServer

alias Aehttpclient.Client
alias Aecore.Structures.Block
alias Aecore.Utils.Blockchain.BlockValidation

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

def init(initial_peers) do
{:ok, initial_peers}
end

@spec add_peer(term) :: :ok | {:error, term()} | :error
def add_peer(uri) do
GenServer.call(__MODULE__, {:add_peer, uri})
end

@spec remove_peer(term) :: :ok | :error
def remove_peer(uri) do
GenServer.call(__MODULE__, {:remove_peer, uri})
end

@spec check_peers() :: :ok
def check_peers() do
GenServer.call(__MODULE__, :check_peers)
end

@spec all_peers() :: list()
def all_peers() do
GenServer.call(__MODULE__, :all_peers)
end

@spec genesis_block_header_hash() :: term()
def genesis_block_header_hash() do
Block.genesis_header()
|> BlockValidation.block_header_hash()
|> Base.encode16()
end

def handle_call({:add_peer,uri}, _from, peers) do
if(!Enum.member?(peers,uri)) do
case(Client.get_info(uri)) do
{:ok, info} ->
if(Map.get(info,"genesis_block_hash") == genesis_block_header_hash()) do
{:reply, :ok, [uri | peers]}
else
{:reply, {:error, "Genesis header hash not valid"}, peers}
end
:error ->
{:reply, :error, peers}
end
else
{:reply, {:error, "Peer already in list"}, peers}
end
end

def handle_call({:remove_peer, uri}, _from, peers) do
if(Enum.member?(peers,uri)) do
{:reply, :ok, List.delete(peers, uri)}
else
{:reply, {:error, "Peer not found"}, peers}
end
end

def handle_call(:check_peers, _from, peers) do
updated_peers = Enum.filter(peers, fn(peer) ->
{status, info} = Client.get_info(peer)
:ok == status &&
Map.get(info,"genesis_block_hash") == genesis_block_header_hash()
end)
{:reply, :ok, updated_peers}
end

def handle_call(:all_peers, _from, peers) do
{:reply, peers, peers}
end
end
15 changes: 15 additions & 0 deletions apps/aecore/lib/aecore/peers/worker/supervisor.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
defmodule Aecore.Peers.Supervisor do
use Supervisor

def start_link() do
Supervisor.start_link(__MODULE__, :ok)
end

def init(:ok) do
children = [
worker(Aecore.Peers.Worker, [])
]

supervise(children, strategy: :one_for_one)
end
end
1 change: 0 additions & 1 deletion apps/aecore/lib/aecore/structures/block.ex
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ defmodule Aecore.Structures.Block do
@moduledoc """
Structure of the block
"""

alias Aecore.Structures.Block
alias Aecore.Structures.Header

Expand Down
21 changes: 21 additions & 0 deletions apps/aecore/test/aecore_peers_test.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
defmodule AecorePeersTest do

use ExUnit.Case

alias Aecore.Peers.Worker, as: Peers

setup do
Peers.start_link()
[]
end

test "add peer, get all peers, check peers and remove the peer" do
assert :ok = Peers.add_peer("localhost:4000")
assert {:error, "Peer already in list"} = Peers.add_peer("localhost:4000")
assert Enum.count(Peers.all_peers) == 1
assert :ok = Peers.check_peers
assert :ok = Peers.remove_peer("localhost:4000")
assert {:error, "Peer not found"} = Peers.remove_peer("localhost:4001")
end

end
20 changes: 20 additions & 0 deletions apps/aehttpclient/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# The directory Mix will write compiled artifacts to.
/_build/

# If you run "mix test --cover", coverage assets end up here.
/cover/

# The directory Mix downloads your dependencies sources to.
/deps/

# Where 3rd-party dependencies like ExDoc output generated docs.
/doc/

# Ignore .fetch files in case you like to edit your project deps locally.
/.fetch

# If the VM crashes, it generates a dump, let's ignore it too.
erl_crash.dump

# Also ignore archive artifacts (built via "mix archive.build").
*.ez
21 changes: 21 additions & 0 deletions apps/aehttpclient/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Aehttpclient

A basic client for making requests to a node.

## Installation

If [available in Hex](https://hex.pm/docs/publish), the package can be installed
by adding `aehttpclient` to your list of dependencies in `mix.exs`:

```elixir
def deps do
[
{:aehttpclient, "~> 0.1.0"}
]
end
```

Documentation can be generated with [ExDoc](https://github.com/elixir-lang/ex_doc)
and published on [HexDocs](https://hexdocs.pm). Once published, the docs can
be found at [https://hexdocs.pm/aehttpclient](https://hexdocs.pm/aehttpclient).

30 changes: 30 additions & 0 deletions apps/aehttpclient/config/config.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# This file is responsible for configuring your application
# and its dependencies with the aid of the Mix.Config module.
use Mix.Config

# This configuration is loaded before any dependency and is restricted
# to this project. If another project depends on this project, this
# file won't be loaded nor affect the parent project. For this reason,
# if you want to provide default values for your application for
# 3rd-party users, it should be done in your "mix.exs" file.

# You can configure your application as:
#
# config :aehttpclient, key: :value
#
# and access this configuration in your application as:
#
# Application.get_env(:aehttpclient, :key)
#
# You can also configure a 3rd-party app:
#
# config :logger, level: :info
#

# It is also possible to import configuration files, relative to this
# directory. For example, you can emulate configuration per environment
# by uncommenting the line below and defining dev.exs, test.exs and such.
# Configuration from the imported file will override the ones defined
# here (which is why it is important to import them last).
#
# import_config "#{Mix.env}.exs"
18 changes: 18 additions & 0 deletions apps/aehttpclient/lib/client.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
defmodule Aehttpclient.Client do
@moduledoc """
Client used for making requests to a node.
"""

@spec get_info(term()) :: {:ok, map()} | :error
def get_info(uri) do
case(HTTPoison.get(uri <> "/info")) do
{:ok, %{body: body, status_code: 200}} ->
response = Poison.decode!(body)
{:ok, response}
{:ok, %HTTPoison.Response{status_code: 404}} ->
:error
{:error, %HTTPoison.Error{}} ->
:error
end
end
end
27 changes: 27 additions & 0 deletions apps/aehttpclient/mix.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
defmodule Aehttpclient.Mixfile do
use Mix.Project

def project do
[
app: :aehttpclient,
version: "0.1.0",
elixir: "~> 1.5",
start_permanent: Mix.env == :prod,
deps: deps()
]
end

# Run "mix help compile.app" to learn about applications.
def application do
[
extra_applications: [:logger]
]
end

# Run "mix help deps" to learn about dependencies.
defp deps do
[
{:httpoison, "~> 0.13"}
]
end
end
5 changes: 5 additions & 0 deletions apps/aehttpclient/test/aehttpclient_test.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
defmodule AehttpclientTest do

use ExUnit.Case

end
1 change: 1 addition & 0 deletions apps/aehttpclient/test/test_helper.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ExUnit.start()
16 changes: 16 additions & 0 deletions apps/aehttpserver/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# App artifacts
/_build
/db
/deps
/*.ez

# Generated on crash by the VM
erl_crash.dump

# Files matching config/*.secret.exs pattern contain sensitive
# data and you should not commit them into version control.
#
# Alternatively, you may comment the line below and commit the
# secrets files as long as you replace their contents by environment
# variables.
/config/*.secret.exs
18 changes: 18 additions & 0 deletions apps/aehttpserver/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Aehttpserver

To start your Phoenix app:

* Install dependencies with `mix deps.get`
* Start Phoenix endpoint with `mix phoenix.server`

Now you can visit [`localhost:4000`](http://localhost:4000) from your browser.

Ready to run in production? Please [check our deployment guides](http://www.phoenixframework.org/docs/deployment).

## Learn more

* Official website: http://www.phoenixframework.org/
* Guides: http://phoenixframework.org/docs/overview
* Docs: https://hexdocs.pm/phoenix
* Mailing list: http://groups.google.com/group/phoenix-talk
* Source: https://github.com/phoenixframework/phoenix
15 changes: 15 additions & 0 deletions apps/aehttpserver/config/config.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# This file is responsible for configuring your application
# and its dependencies with the aid of the Mix.Config module.
#
# This configuration file is loaded before any dependency and
# is restricted to this project.
use Mix.Config

# Configures Elixir's Logger
config :logger, :console,
format: "$time $metadata[$level] $message\n",
metadata: [:request_id]

# Import environment specific config. This must remain at the bottom
# of this file so it overrides the configuration defined above.
import_config "#{Mix.env}.exs"
34 changes: 34 additions & 0 deletions apps/aehttpserver/config/dev.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
use Mix.Config

# For development, we disable any cache and enable
# debugging and code reloading.
#
# The watchers configuration can be used to run external
# watchers to your application. For example, we use it
# with brunch.io to recompile .js and .css sources.
#
# In order to use HTTPS in development, a self-signed
# certificate can be generated by running the following
# command from your terminal:
#
# openssl req -new -newkey rsa:4096 -days 365 -nodes -x509 -subj "/C=US/ST=Denial/L=Springfield/O=Dis/CN=www.example.com" -keyout priv/server.key -out priv/server.pem
#
# The `http:` config below can be replaced with:
# https: [port: 4000, keyfile: "priv/server.key", certfile: "priv/server.pem"],
#
# If desired, both `http:` and `https:` keys can be
# configured to run both http and https servers on
# different ports.
config :aehttpserver, Aehttpserver.Endpoint,
http: [port: 4000],
debug_errors: true,
code_reloader: true,
check_origin: false,
watchers: []

# Do not include metadata nor timestamps in development logs
config :logger, :console, format: "[$level] $message\n"

# Set a higher stacktrace during development. Avoid configuring such
# in production as building large stacktraces may be expensive.
config :phoenix, :stacktrace_depth, 20
Loading