Skip to content

Commit

Permalink
Merge branch 'release/2.1.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
nguyenvinhlinh committed Mar 6, 2022
2 parents a3c7836 + 27a5136 commit acdefef
Show file tree
Hide file tree
Showing 13 changed files with 146 additions and 34 deletions.
29 changes: 18 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,14 @@ ALPH_NODE_PORT=12973 \
ALPH_NODE_API_KEY=cf5062725e62d2096228cd6f7ab0f2a0 \
ALPH_NODE_WALLET_NAME=main \
ALPH_NODE_WALLET_PASSWORD="" \
WORKER_HASHRATE_INTERVAL=1800000 \
ALPH_WORKER_HASHRATE_INTERVAL=1800000 \
KASPA_WORKER=false \
KASPA_WALLET_PATH=/opt/kaspa/bin/kaspawallet \
KASPA_CTL_PATH=/opt/kaspa/bin/kaspactl \
KASPA_WORKER_HASHRATE_INTERVAL=1800000 \
WORKER_IP_INTERVAL=60000 \
TELEGRAM_BOT_TOKEN=cf5062725e62d2096228cd6f7ab0f2a0 \
TELEGRAM_CHAT_ID=cf5062725e62d2096228cd6f7ab0f2a0 \
KASPA_WORKER=false \
KASPA_WALLET_PATH=/opt/kaspa/bin/kaspawallet \
_build/prod/rel/alephium_miner_bot/bin/alephium_miner_bot start
```

Expand All @@ -57,14 +59,18 @@ Alephium Configration:
- `ALPH_NODE_API_KEY`: self-explained
- `ALPH_NODE_WALLET_NAME`: self-explained
- `ALPH_NODE_WALLET_PASSWORD`: self-explained
- `WORKER_HASHRATE_INTERVAL`: interval in microsecond that fetching network hashrate.
- `WORKER_IP_INTERVAL`: interval in microsecond that fetching IP
- `ALPH_WORKER_HASHRATE_INTERVAL`: interval in microsecond that fetching network hashrate.
- `TELEGRAM_BOT_TOKEN`: self-explained
- `TELEGRAM_CHAT_ID`: self-explained

Kaspa Configuration:
- `KASPA_WORKER`: enable/disable kaspa worker, value: true/false
- `KASPA_WALLET_PATH`: path to kaspa wallet executable file
- `KASPA_WALLET_PATH`: path to `kaspawallet` executable file
- `KASPA_CTL_PATH`: path to `kaspactl` executable file
- `KASPA_WORKER_HASHRATE_INTERVAL`: interval in microsecond that fetching network hashrate.

IP Worker Configuration:
- `WORKER_IP_INTERVAL`: interval in microsecond that fetching IP

Finally, make the file `release-run.sh` executable and run it
```sh
Expand All @@ -79,11 +85,12 @@ You should see the following output on terminal.
[Alephium][Worker.WorkerReward] started.
[Alephium][Worker.WorkerHashrate] started.
[Kaspa][Worker.WorkerReward] started.
2022-02-02 13:07 [Kaspa] Total Balance: 635,292.823
2022-02-02 13:07 IP: 42.112.xxx.xxx
2022-02-02 13:07 [Alephium] Global Hashrate: 27.1 TH/s
2022-02-02 13:07 [Alephium] Total Balance Hint: 0 ALPH
2022-02-02 13:07 [Kaspa] Won a block after 0.1 minute(s). Total Balance Hint: 638,292.823
[Kaspa][Worker.WorkerHashrate] started.
2022-03-06 23:48 [Kaspa] Network Hashrate: 1.65 TH/s
2022-03-06 23:48 [Kaspa] Total Balance: 52,592.586
2022-03-06 23:48 IP: 42.115.xxx.xxx
2022-03-06 23:48 [Alephium] Network Hashrate: 42.03 TH/s
2022-03-06 23:48 [Alephium] Total Balance Hint: 27.138334777166927095 ALPH
```

And your telegram should show:
Expand Down
15 changes: 10 additions & 5 deletions config/dev.exs.sample
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,17 @@ config :alephium_miner_bot, :alph_node_port, "12973"
config :alephium_miner_bot, :alph_node_api_key, ""
config :alephium_miner_bot, :alph_node_wallet_name, ""
config :alephium_miner_bot, :alph_node_wallet_password, ""

config :alephium_miner_bot, :worker_hashrate_interval, 5_000
config :alephium_miner_bot, :worker_ip_interval, 5_000
config :alephium_miner_bot, :telegram_bot_token, ""
config :alephium_miner_bot, :telegram_chat_id, ""
config :alephium_miner_bot, :alph_worker_hashrate_interval, 5_000

# Kaspa Configuration
config :alephium_miner_bot, :kaspa_worker, false
config :alephium_miner_bot, :kaspa_wallet_path, "/opt/kaspa/bin/kaspawallet"
config :alephium_miner_bot, :kaspa_ctl_path, "/opt/kaspa/bin/kaspactl"
config :alephium_miner_bot, :kaspa_worker_hashrate_interval, 60_000 * 30

# IP Worker Configuration
config :alephium_miner_bot, :worker_ip_interval, 5_000

# Telegram Configuration
config :alephium_miner_bot, :telegram_bot_token, ""
config :alephium_miner_bot, :telegram_chat_id, ""
15 changes: 10 additions & 5 deletions config/runtime.exs
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,17 @@ config :alephium_miner_bot, :alph_node_port, System.fetch_env!("ALPH_NODE_PORT")
config :alephium_miner_bot, :alph_node_api_key, System.fetch_env!("ALPH_NODE_API_KEY")
config :alephium_miner_bot, :alph_node_wallet_name, System.fetch_env!("ALPH_NODE_WALLET_NAME")
config :alephium_miner_bot, :alph_node_wallet_password, System.fetch_env!("ALPH_NODE_WALLET_PASSWORD")

config :alephium_miner_bot, :worker_hashrate_interval, System.fetch_env!("WORKER_HASHRATE_INTERVAL") |> String.to_integer()
config :alephium_miner_bot, :worker_ip_interval, System.fetch_env!("WORKER_IP_INTERVAL") |> String.to_integer()
config :alephium_miner_bot, :telegram_bot_token, System.fetch_env!("TELEGRAM_BOT_TOKEN")
config :alephium_miner_bot, :telegram_chat_id, System.fetch_env!("TELEGRAM_CHAT_ID")
config :alephium_miner_bot, :alph_worker_hashrate_interval, System.fetch_env!("ALPH_WORKER_HASHRATE_INTERVAL") |> String.to_integer()

# Kaspa Configuration
config :alephium_miner_bot, :kaspa_worker, if(System.fetch_env!("KASPA_WORKER") == "true", do: true, else: false)
config :alephium_miner_bot, :kaspa_wallet_path, System.fetch_env!("KASPA_WALLET_PATH")
config :alephium_miner_bot, :kaspa_ctl_path, System.fetch_env!("KASPA_CTL_PATH")
config :alephium_miner_bot, :kaspa_worker_hashrate_interval, System.fetch_env!("KASPA_WORKER_HASHRATE_INTERVAL") |> String.to_integer()

# IP Worker Configuration
config :alephium_miner_bot, :worker_ip_interval, System.fetch_env!("WORKER_IP_INTERVAL") |> String.to_integer()

# Telegram Configuration
config :alephium_miner_bot, :telegram_bot_token, System.fetch_env!("TELEGRAM_BOT_TOKEN")
config :alephium_miner_bot, :telegram_chat_id, System.fetch_env!("TELEGRAM_CHAT_ID")
Binary file modified diagrams/elixir-process.dia
Binary file not shown.
Binary file modified images/elixir-process.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
6 changes: 3 additions & 3 deletions lib/alephium/worker/worker_hashrate.ex
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ defmodule AlephiumMinerBot.Alephium.Worker.WorkerHashrate do

def start_link() do
IO.puts "[Alephium][Worker.WorkerHashrate] started."
GenServer.start_link(__MODULE__, :worker_hashrate)
GenServer.start_link(__MODULE__, :alephium_worker_hashrate)
end


Expand All @@ -34,7 +34,7 @@ defmodule AlephiumMinerBot.Alephium.Worker.WorkerHashrate do
end

defp schedule_check_hashrate do
interval = Application.get_env(:alephium_miner_bot, :worker_hashrate_interval)
interval = Application.get_env(:alephium_miner_bot, :alph_worker_hashrate_interval)
Process.send_after(self(), :check_hashrate, interval)
end

Expand All @@ -52,7 +52,7 @@ defmodule AlephiumMinerBot.Alephium.Worker.WorkerHashrate do
|> Kernel./(1_000_000)
|> Float.round(2)

"#{new_timestamp_string} [Alephium] Global Hashrate: #{hashrate_string} TH/s"
"#{new_timestamp_string} [Alephium] Network Hashrate: #{hashrate_string} TH/s"
end

def check_hashrate do
Expand Down
2 changes: 1 addition & 1 deletion lib/alephium/worker/worker_reward.ex
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ defmodule AlephiumMinerBot.Alephium.Worker.WorkerReward do

def start_link() do
IO.puts "[Alephium][Worker.WorkerReward] started."
GenServer.start_link(__MODULE__, :worker_reward)
GenServer.start_link(__MODULE__, :alph_worker_reward)
end


Expand Down
6 changes: 3 additions & 3 deletions lib/alephium_miner_bot.ex
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ defmodule AlephiumMinerBot do
use Application

alias AlephiumMinerBot.Alephium.AlephiumSupervisor
alias AlephiumMinerBot.Kaspa.Worker.WorkerReward, as: KaspaWorkerReward
alias AlephiumMinerBot.Kaspa.KaspaSupervisor
alias AlephiumMinerBot.ETC.Worker.WorkerIP

def start(_type, _args) do
Expand Down Expand Up @@ -38,8 +38,8 @@ defmodule AlephiumMinerBot do

def children_kaspa_worker(children) do
if Application.get_env(:alephium_miner_bot, :kaspa_worker) do
children ++ [%{id: KaspaWorkerReward,
start: {KaspaWorkerReward, :start_link, []},
children ++ [%{id: KaspaSupervisor,
start: {KaspaSupervisor, :start_link, []},
type: :supervisor}]
else
children
Expand Down
26 changes: 26 additions & 0 deletions lib/kaspa/kaspa_supervisor.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
defmodule AlephiumMinerBot.Kaspa.KaspaSupervisor do
use Supervisor

alias AlephiumMinerBot.Kaspa.Worker.{WorkerReward, WorkerHashrate}

def start_link() do
Supervisor.start_link(__MODULE__, nil, name: __MODULE__)
end

@impl true
def init(_) do
children = [
%{
id: WorkerReward,
start: {WorkerReward, :start_link, []},
type: :worker
},
%{
id: WorkerHashrate,
start: {WorkerHashrate, :start_link, []},
type: :worker
}
]
Supervisor.init(children, strategy: :one_for_one)
end
end
68 changes: 68 additions & 0 deletions lib/kaspa/worker/worker_hashrate.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
defmodule AlephiumMinerBot.Kaspa.Worker.WorkerHashrate do
use GenServer
alias AlephiumMinerBot.Telegram

def start_link() do
IO.puts "[Kaspa][Worker.WorkerHashrate] started."
GenServer.start_link(__MODULE__, :kaspa_worker_hashrate)
end

@impl true
def init(_) do
Process.send_after(self(), :check_hashrate, 2_000)
{:ok, nil}
end

@impl true
def handle_info(:check_hashrate, _state) do
case check_hashrate() do
{:ok, hashrate} ->
message = generate_message(hashrate)
IO.puts(message)
Telegram.send_message(message)
schedule_check_hashrate()
{:noreply, nil}
:error ->
schedule_check_hashrate()
{:noreply, nil}
end
end

defp schedule_check_hashrate do
interval = Application.get_env(:alephium_miner_bot, :kaspa_worker_hashrate_interval)
Process.send_after(self(), :check_hashrate, interval)
end

def generate_message(hashrate) do
new_timestamp = NaiveDateTime.utc_now()
new_timestamp_string = new_timestamp
|> NaiveDateTime.add(7*60*60, :second)
|> NaiveDateTime.truncate(:second)
|> NaiveDateTime.to_string()
|> String.slice(0..-4)

"#{new_timestamp_string} [Kaspa] Network Hashrate: #{hashrate} TH/s"
end

def check_hashrate do
kaspactl_path = Application.get_env(:alephium_miner_bot, :kaspa_ctl_path)
case System.cmd(kaspactl_path, ["GetBlockDagInfo"]) do
{message, 0} ->
hashrate = extract_hashrate_from_message(message)
{:ok, hashrate}
{"", 1} ->
IO.puts "[Kaspa][Worker.WorkerHashrate][check_hashrate/0] Ensure that you can run `kaspactl GetBlockDagInfo` manually."
:error
end
end

def extract_hashrate_from_message(message) do
Jason.decode!(message)
|> Map.get("getBlockDagInfoResponse")
|> Map.get("difficulty")
|> Decimal.from_float()
|> Decimal.mult(2)
|> Decimal.div(1_000_000_000_000)
|> Number.Currency.number_to_currency([format: "%n", precision: 2])
end
end
3 changes: 1 addition & 2 deletions lib/kaspa/worker/worker_reward.ex
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,9 @@ defmodule AlephiumMinerBot.Kaspa.Worker.WorkerReward do

def start_link() do
IO.puts "[Kaspa][Worker.WorkerReward] started."
GenServer.start_link(__MODULE__, :worker_reward)
GenServer.start_link(__MODULE__, :kaspa_worker_reward)
end


@impl true
def init(_) do
state = %{
Expand Down
2 changes: 1 addition & 1 deletion mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ defmodule AlephiumMinerBot.MixProject do
def project do
[
app: :alephium_miner_bot,
version: "2.0.0",
version: "2.1.0",
elixir: "~> 1.12",
start_permanent: Mix.env() == :prod,
deps: deps()
Expand Down
8 changes: 5 additions & 3 deletions release_run.sh.sample
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@ ALPH_NODE_PORT=12973 \
ALPH_NODE_API_KEY=1234asdf \
ALPH_NODE_WALLET_NAME=main \
ALPH_NODE_WALLET_PASSWORD="" \
WORKER_HASHRATE_INTERVAL=1800000 \
ALPH_WORKER_HASHRATE_INTERVAL=1800000\
KASPA_WORKER=true \
KASPA_WALLET_PATH=/opt/kaspa/bin/kaspawallet \
KASPA_CTL_PATH=/opt/kaspa/bin/kaspactl \
KASPA_WORKER_HASHRATE_INTERVAL=1800000 \
WORKER_IP_INTERVAL=60000 \
TELEGRAM_BOT_TOKEN=1234asdf \
TELEGRAM_CHAT_ID=1234asdf \
KASPA_WORKER=true \
KASPA_WALLET_PATH=/opt/kaspa/bin/kaspawallet \
_build/prod/rel/alephium_miner_bot/bin/alephium_miner_bot start

0 comments on commit acdefef

Please sign in to comment.