commit
06f99d2f37
@ -0,0 +1,26 @@ |
|||||||
|
defmodule Explorer.Chain.Events.DBSender do |
||||||
|
@moduledoc """ |
||||||
|
Sends events to Postgres. |
||||||
|
""" |
||||||
|
alias Explorer.Repo |
||||||
|
|
||||||
|
def send_data(event_type) do |
||||||
|
payload = encode_payload({:chain_event, event_type}) |
||||||
|
send_notify(payload) |
||||||
|
end |
||||||
|
|
||||||
|
def send_data(event_type, broadcast_type, event_data) do |
||||||
|
payload = encode_payload({:chain_event, event_type, broadcast_type, event_data}) |
||||||
|
send_notify(payload) |
||||||
|
end |
||||||
|
|
||||||
|
defp encode_payload(payload) do |
||||||
|
payload |
||||||
|
|> :erlang.term_to_binary([:compressed]) |
||||||
|
|> Base.encode64() |
||||||
|
end |
||||||
|
|
||||||
|
defp send_notify(payload) do |
||||||
|
Repo.query!("select pg_notify('chain_event', $1::text);", [payload]) |
||||||
|
end |
||||||
|
end |
@ -0,0 +1,55 @@ |
|||||||
|
defmodule Explorer.Chain.Events.Listener do |
||||||
|
@moduledoc """ |
||||||
|
Listens and publishes events from PG |
||||||
|
""" |
||||||
|
|
||||||
|
use GenServer |
||||||
|
|
||||||
|
alias Postgrex.Notifications |
||||||
|
|
||||||
|
def start_link(_) do |
||||||
|
GenServer.start_link(__MODULE__, "chain_event", name: __MODULE__) |
||||||
|
end |
||||||
|
|
||||||
|
def init(channel) do |
||||||
|
{:ok, pid} = |
||||||
|
:explorer |
||||||
|
|> Application.get_env(Explorer.Repo) |
||||||
|
|> Notifications.start_link() |
||||||
|
|
||||||
|
ref = Notifications.listen!(pid, channel) |
||||||
|
|
||||||
|
{:ok, {pid, ref, channel}} |
||||||
|
end |
||||||
|
|
||||||
|
def handle_info({:notification, _pid, _ref, _topic, payload}, state) do |
||||||
|
payload |
||||||
|
|> decode_payload!() |
||||||
|
|> broadcast() |
||||||
|
|
||||||
|
{:noreply, state} |
||||||
|
end |
||||||
|
|
||||||
|
# sobelow_skip ["Misc.BinToTerm"] |
||||||
|
defp decode_payload!(payload) do |
||||||
|
payload |
||||||
|
|> Base.decode64!() |
||||||
|
|> :erlang.binary_to_term() |
||||||
|
end |
||||||
|
|
||||||
|
defp broadcast({:chain_event, event_type} = event) do |
||||||
|
Registry.dispatch(Registry.ChainEvents, event_type, fn entries -> |
||||||
|
for {pid, _registered_val} <- entries do |
||||||
|
send(pid, event) |
||||||
|
end |
||||||
|
end) |
||||||
|
end |
||||||
|
|
||||||
|
defp broadcast({:chain_event, event_type, broadcast_type, _data} = event) do |
||||||
|
Registry.dispatch(Registry.ChainEvents, {event_type, broadcast_type}, fn entries -> |
||||||
|
for {pid, _registered_val} <- entries do |
||||||
|
send(pid, event) |
||||||
|
end |
||||||
|
end) |
||||||
|
end |
||||||
|
end |
@ -0,0 +1,21 @@ |
|||||||
|
defmodule Explorer.Chain.Events.SimpleSender do |
||||||
|
@moduledoc """ |
||||||
|
Publishes events through Registry without intermediate levels. |
||||||
|
""" |
||||||
|
|
||||||
|
def send_data(event_type, broadcast_type, event_data) do |
||||||
|
Registry.dispatch(Registry.ChainEvents, {event_type, broadcast_type}, fn entries -> |
||||||
|
for {pid, _registered_val} <- entries do |
||||||
|
send(pid, {:chain_event, event_type, broadcast_type, event_data}) |
||||||
|
end |
||||||
|
end) |
||||||
|
end |
||||||
|
|
||||||
|
def send_data(event_type) do |
||||||
|
Registry.dispatch(Registry.ChainEvents, event_type, fn entries -> |
||||||
|
for {pid, _registered_val} <- entries do |
||||||
|
send(pid, {:chain_event, event_type}) |
||||||
|
end |
||||||
|
end) |
||||||
|
end |
||||||
|
end |
@ -1,77 +1,5 @@ |
|||||||
# BlockScout Docker integration |
# BlockScout Docker Integration |
||||||
|
|
||||||
For now this integration is not production ready. It made only for local usage only ! |
This integration is not production ready, and should be used for local BlockScout deployment only. |
||||||
|
|
||||||
## How to use ? |
|
||||||
First of all, blockscout requires `PostgreSQL` server for working. |
|
||||||
It will be provided by starting script (new docker image will be created named `postgres`) |
|
||||||
|
|
||||||
**Starting command** |
|
||||||
`make start` - will set everything up and start blockscout in container. |
|
||||||
To connect it to your local environment you will have to configure it using [env variables](#env-variables) |
|
||||||
|
|
||||||
Example connecting to local `ganache` instance running on port `2000` on Mac/Windows: |
|
||||||
```bash |
|
||||||
COIN=DAI \ |
|
||||||
ETHEREUM_JSONRPC_VARIANT=ganache \ |
|
||||||
ETHEREUM_JSONRPC_HTTP_URL=http://host.docker.internal:2000 \ |
|
||||||
ETHEREUM_JSONRPC_WS_URL=ws://host.docker.internal:2000 \ |
|
||||||
make start |
|
||||||
``` |
|
||||||
|
|
||||||
Blockscout will be available on `localhost:4000` |
|
||||||
|
|
||||||
**Note** |
|
||||||
On mac/Windows Docker provides with a special URL `host.docker.internal` that will be available into container and routed to your local machine. |
|
||||||
On Linux docker is starting using `--network=host` and all services should be available on `localhost` |
|
||||||
|
|
||||||
### Migrations |
|
||||||
|
|
||||||
By default, `Makefile` will do migrations for you on `PostgreSQL` creation. |
|
||||||
But you could run migrations manually using `make migrate` command. |
|
||||||
|
|
||||||
**WARNING** Migrations will clean up your local database ! |
|
||||||
|
|
||||||
## Env variables |
|
||||||
|
|
||||||
BlockScout supports 3 different JSON RPC Variants. |
|
||||||
Variant could be configured using `ETHEREUM_JSONRPC_VARIANT` environment variable. |
|
||||||
|
|
||||||
Example: |
|
||||||
```bash |
|
||||||
ETHEREUM_JSONRPC_VARIANT=ganache make start |
|
||||||
``` |
|
||||||
|
|
||||||
Available options are: |
|
||||||
|
|
||||||
* `parity` - Parity JSON RPC (**Default one**) |
|
||||||
* `geth` - Geth JSON RPC |
|
||||||
* `ganache` - Ganache JSON RPC |
|
||||||
|
|
||||||
|
|
||||||
| Variable | Description | Default value | |
|
||||||
| -------- | ----------- | ------------- | |
|
||||||
| `ETHEREUM_JSONRPC_VARIANT` | Variant of your JSON RPC service: `parity`, `geth` or `ganache` | `parity` | |
|
||||||
| `ETHEREUM_JSONRPC_HTTP_URL` | HTTP JSON RPC URL Only for `geth` or `ganache` variant | Different per JSONRPC variant | |
|
||||||
| `ETHEREUM_JSONRPC_WS_URL` | WS JSON RPC url | Different per JSONRPC variant | |
|
||||||
| `ETHEREUM_JSONRPC_TRACE_URL` | Trace URL **Only for `parity` variant** | `http://localhost:8545` | |
|
||||||
| `COIN` | Default Coin | `POA` | |
|
||||||
| `LOGO` | Coin logo | Empty | |
|
||||||
| `NETWORK` | Network | Empty | |
|
||||||
| `SUBNETWORK` | Subnetwork | Empty | |
|
||||||
| `NETWORK_ICON` | Network icon | Empty | |
|
||||||
| `NETWORK_PATH` | Network path | `/` | |
|
||||||
|
|
||||||
|
|
||||||
`ETHEREUM_JSONRPC_HTTP_URL` default values: |
|
||||||
|
|
||||||
* For `parity` - `http://localhost:8545` |
|
||||||
* For `geth` - `https://mainnet.infura.io/8lTvJTKmHPCHazkneJsY` |
|
||||||
* For `ganache` - `http://localhost:7545` |
|
||||||
|
|
||||||
`ETHEREUM_JSONRPC_WS_URL` default values: |
|
||||||
|
|
||||||
* For `parity` - `ws://localhost:8546` |
|
||||||
* For `geth` - `wss://mainnet.infura.io/8lTvJTKmHPCHazkneJsY/ws` |
|
||||||
* For `ganache` - `ws://localhost:7545` |
|
||||||
|
|
||||||
|
For usage instructions and ENV variables, see the [docker integration documentation](https://docs.blockscout.com/for-developers/information-and-settings/docker-integration-local-use-only). |
Loading…
Reference in new issue