Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(neo4j): add connection pool for neo4j #32

Merged
merged 8 commits into from Jun 7, 2019
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 9 additions & 0 deletions .dockerignore
@@ -0,0 +1,9 @@
rlay_data
target
website
Dockerfile
LICENSE*
*.md

# This allows the build to be cached
!target/release
318 changes: 218 additions & 100 deletions Cargo.lock

Large diffs are not rendered by default.

10 changes: 6 additions & 4 deletions Cargo.toml
Expand Up @@ -8,7 +8,7 @@ edition = "2018"
[dependencies]
web3 = "0.6.0"
cid = { git = 'https://github.com/hobofan/rust-cid', rev = '87332277d2cdb5b7cb6484c23b5711c28b44c684' }
rlay_ontology = { git = 'https://github.com/rlay-project/rlay-ontology', rev = 'de2bcef024b740d08fe0a6fabdef9d9f19cf24c0', features = ["web3_compat"] }
rlay_ontology = { git = 'https://github.com/rlay-project/rlay-ontology', rev = 'd02a9f3781381f5d19236144a19a2a6f8ed71319', features = ["web3_compat"] }
merkle_light = { git = "https://github.com/hobofan/merkle_light", rev = 'f069616df909cb7a4426f6deb328bde6422a3510' }
ethabi = { git = "https://github.com/hobofan/ethabi", rev = '42df1ca0ac5976040b9091f35875b9548c30ef41' }
rustc-hex = "1.0.0"
Expand All @@ -25,7 +25,6 @@ log = "0.4.6"
env_logger = "0.5.10"
tiny-keccak = "1.4.2"
serde_json = { version = "1.0.22", features = ["preserve_order"] }
# using from git, as jsonrpc-ws-server is not on crates.io
jsonrpc-core = "10.1.0"
jsonrpc-pubsub = "10.1.0"
jsonrpc-http-server = "10.1.0"
Expand All @@ -36,9 +35,11 @@ failure_derive = "=0.1.1"
dialoguer = "0.1.0"

# using fork for https support
rusted_cypher = { git = "https://github.com/xenodata-lab/rusted-cypher", rev = "3218935b067fe41d06e2de55d0af07f6e4cb176b", optional = true }
rusted_cypher = { git = "https://github.com/hobofan/rusted-cypher", rev = '4b68788e33708ea006e087d341685e6c7875ea1d', optional = true }
hlua = "0.4.1"
toml_edit = "0.1.3"
bb8 = { version = "0.3.0", optional = true }
bb8_cypher = { git = "https://github.com/hobofan/bb8-cypher", rev = 'f4cafe2aa6aacc072697bda3d58e03668482d353', optional = true }

[dev-dependencies]
assert_cmd = "0.10.2"
Expand All @@ -47,9 +48,10 @@ assert_cmd = "0.10.2"
# see https://github.com/ipld/rust-cid/pull/9
cid = { git = 'https://github.com/hobofan/rust-cid', rev = '87332277d2cdb5b7cb6484c23b5711c28b44c684' }
ethabi = { git = "https://github.com/hobofan/ethabi", rev = '42df1ca0ac5976040b9091f35875b9548c30ef41' }
rusted_cypher = { git = "https://github.com/hobofan/rusted-cypher", rev = '4b68788e33708ea006e087d341685e6c7875ea1d', optional = true }

[features]
default = ["transport_ws"]
transport_ipc = []
transport_ws = []
backend_neo4j = ["rusted_cypher"]
backend_neo4j = ["rusted_cypher", "bb8", "bb8_cypher"]
20 changes: 20 additions & 0 deletions Dockerfile
@@ -0,0 +1,20 @@
FROM rust:1.33.0 AS builder

WORKDIR /usr/src/app
COPY . .

RUN cargo build \
--release \
--features backend_neo4j

# Ubuntu 18.04 as runner image
FROM ubuntu@sha256:5f4bdc3467537cbbe563e80db2c3ec95d548a9145d64453b06939c4592d67b6d

ENV DEBIAN_FRONTEND=noninteractive
RUN apt-get update && apt-get -y install ca-certificates libssl-dev && rm -rf /var/lib/apt/lists/*

COPY --from=builder /usr/src/app/target/release/rlay-client /rlay-client
COPY docker/rlay.config.toml .

ENTRYPOINT ["/rlay-client"]
CMD ["client"]
8 changes: 8 additions & 0 deletions README.md
Expand Up @@ -23,6 +23,14 @@ If you encounter any problems while trying to run rlay-client you can try to use
cargo run -- doctor
```

### Via Docker

We provide a Docker image which can be used to run `rlay-client`. I currently has the assumption that it is used with a single Neo4J backend at `127.0.0.1:7474`, which requires it to be run with `--net=host`. You can use it with another configuration by building your own Docker image based on it and overwriting the `/rlay.config.toml` file.

```
docker run rlayproject/rlay-client
```


## Contributing & Contact

Expand Down
1 change: 1 addition & 0 deletions ci/script.sh
Expand Up @@ -6,6 +6,7 @@ set -ex
main() {
# cross build --target $TARGET
# cross build --target $TARGET --release
cargo build
cargo build --features backend_neo4j

if [ ! -z $DISABLE_TESTS ]; then
Expand Down
22 changes: 22 additions & 0 deletions docker/rlay.config.toml
@@ -0,0 +1,22 @@
[rpc]
disabled = false
network_address = "http://127.0.0.1:8546"
ws_network_address = "ws://127.0.0.1:8547"

[backends]

# [backends.default_eth]
# type = "ethereum"
# `network_address` is the address of the Ethereum RPC endpoint we are connecting to
# network_address = "ws://127.0.0.1:8545"
# `payout_root_submission_disabled` determines wether the calculated payout roots are submitted to the blockchain via transactions
# payout_root_submission_disabled = true

# [backends.default_eth.contract_addresses]
# OntologyStorage = "0x174eab2713537c7e4fb9ea1f4202d909656f2642"
# RlayToken = "0x28208f2ad2abb7cb82136f1d6234fb0aee8571d2"
# PropositionLedger = "0x8caf5e029445227ec24cc090026a07b1b521e208"

[backends.myneo4j]
type = "neo4j"
uri = "http://neo4j:toasty123@127.0.0.1:7474/db/data/"
22 changes: 11 additions & 11 deletions rlay.config.toml
Expand Up @@ -5,18 +5,18 @@ ws_network_address = "ws://127.0.0.1:8547"

[backends]

[backends.default_eth]
type = "ethereum"
# [backends.default_eth]
# type = "ethereum"
# `network_address` is the address of the Ethereum RPC endpoint we are connecting to
network_address = "ws://127.0.0.1:8545"
# network_address = "ws://127.0.0.1:8545"
# `payout_root_submission_disabled` determines wether the calculated payout roots are submitted to the blockchain via transactions
payout_root_submission_disabled = true
# payout_root_submission_disabled = true

[backends.default_eth.contract_addresses]
OntologyStorage = "0xb6ae1328cd90badfd6a8b35210921c95cbff84fe"
RlayToken = "0x8caf5e029445227ec24cc090026a07b1b521e208"
PropositionLedger = "0xf008add117ab0f330b5b4f22f710526c47768ab1"
# [backends.default_eth.contract_addresses]
# OntologyStorage = "0x174eab2713537c7e4fb9ea1f4202d909656f2642"
# RlayToken = "0x28208f2ad2abb7cb82136f1d6234fb0aee8571d2"
# PropositionLedger = "0x8caf5e029445227ec24cc090026a07b1b521e208"

# [backends.myneo4j]
# type = "neo4j"
# uri = "http://neo4j:toasty123@localhost:7474/db/data"
[backends.myneo4j]
type = "neo4j"
uri = "http://neo4j:toasty123@127.0.0.1:7474/db/data/"
28 changes: 12 additions & 16 deletions src/backend/ethereum.rs
Expand Up @@ -4,8 +4,9 @@ use rlay_ontology::ontology::Entity;
use rustc_hex::{FromHex, ToHex};
use std::collections::BTreeMap;
use std::sync::{Arc, Mutex};
use web3::futures::future::{self, Future};

use crate::backend::{BackendFromConfig, BackendFromConfigAndSyncState, BackendRpcMethods};
use crate::backend::{BackendFromConfigAndSyncState, BackendRpcMethods};
use crate::config::backend::EthereumBackendConfig;
use crate::sync_ontology::{BlockEntityMap, EntityMap};
use crate::sync_proposition_ledger::PropositionLedger;
Expand All @@ -15,23 +16,13 @@ pub struct EthereumBackend {
pub sync_state: SyncState,
}

impl BackendFromConfig for EthereumBackend {
type C = EthereumBackendConfig;

fn from_config(config: Self::C) -> Result<Self, Error> {
Ok(Self {
config,
sync_state: SyncState::new(),
})
}
}

impl BackendFromConfigAndSyncState for EthereumBackend {
type C = EthereumBackendConfig;
type S = SyncState;
type R = Box<Future<Item = Self, Error = Error> + Send>;

fn from_config_and_syncstate(config: Self::C, sync_state: Self::S) -> Result<Self, Error> {
Ok(Self { config, sync_state })
fn from_config_and_syncstate(config: Self::C, sync_state: Self::S) -> Self::R {
Box::new(future::ok(Self { config, sync_state }))
}
}

Expand Down Expand Up @@ -125,13 +116,18 @@ impl OntologySyncState {
}

impl BackendRpcMethods for EthereumBackend {
fn get_entity(&mut self, cid: &str) -> Result<Option<Entity>, Error> {
fn get_entity(
&mut self,
cid: &str,
) -> Box<Future<Item = Option<Entity>, Error = Error> + Send> {
let entity_map = self.sync_state.entity_map();
let entity_map_lock = entity_map.lock().unwrap();

let cid_no_prefix = str::replace(cid, "0x", "");
let cid_bytes = cid_no_prefix.from_hex().unwrap();

Ok(entity_map_lock.get(&cid_bytes).map(|n| n.clone()))
Box::new(future::ok(
entity_map_lock.get(&cid_bytes).map(|n| n.clone()),
))
}
}