Skip to content
This repository was archived by the owner on Oct 9, 2022. It is now read-only.

Commit c460d31

Browse files
authored
Supports ropsten ethereum header source for test usages (#66)
* Merge rustify into ropsten * feat(eth): supports default ropsten rpc * chore(tests): remove optional deps * chore(crate): renamme package name to darwinia-shadow * feat(lib): add readme to lib.rs * fix(go): rename internal pkg to shadow * fix(lint): optimize code style * chore(docker): update dockerfile and docker-compose.yml * fix(mmr): fix the genesis mmr size * fix(linker): declare links in Cargo.toml * fix(example): the linker in proof example * perf(linker): supports dynamic link in examples * fix(example): exports ffi for test usages * perf(ffi): move tests into module * perf(build): add go.mod and go.sum into manifest * fix(pkg): remove the missing internal pkg * fix(cgo): enable cgo mod and add libc6-compat to alpine * perf(linker): use dynamic linker to invoke diesel libraries in musl * feat(docker): complete the dockerfile of alpine * chore(bump): bump versions to v.0.2.0 * fix(ci): update LD_LIBRARY_PATH for ubuntu * fix(linker): use rustc-link-search to find libdarwinia_shadow in ubuntu * fix(ci): use OUT_DIR to customize the output of libdarwinia_shadow_ext * fix(dylib): adapt the loading process of dynamic link for common linux * fix(docker): remove the --out-dir flag for building with stable version rustc * fix(dylib): adapt LD_LIBRARY_PATH for unix * fix(dylib): force move and overwrite old libdarwinia_shadow_ext * chore(dylib): use sudo inside build.rs to try to fix perm in ubuntu * chore(dylib): use sudo inside build.rs to fix perm in ubuntu...again * fix(ci): ldconfig in ci after installing * feat(eth): optimzie proof process and add dockerignore * chore(README): remove outdated lines * fix(docs.rs): removed all sudo commands, supports both dylink and static link for unix * chore(chains): remove outdated modules * fix(ci): force link-search out_dir while building static library * fix(typo): fix typo in badge link
1 parent 13dd48a commit c460d31

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

47 files changed

+400
-362
lines changed

.dockerignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
target
2+
examples
3+
tests
Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
name: Rust MMR
1+
name: shadow
22

33
on:
44
push:
@@ -23,6 +23,7 @@ jobs:
2323
else
2424
sudo apt-get update -y
2525
sudo apt-get install -y libsqlite3-dev libdbus-1-dev
26+
export LD_LIBRARY_PATH=/lib:/usr/lib:/usr/local/lib
2627
fi
2728
# Cache MMR Library
2829
- name: Cache MMR Library
@@ -33,6 +34,8 @@ jobs:
3334
path: ./target
3435
key: ${{ runner.os }}-build-${{ env.cache-name }}-${{ hashFiles('./Cargo.lock') }}
3536
- name: Build
36-
run: cargo build --verbose
37+
run: cargo build --release -vv
38+
- name: Check Binary
39+
run: ./target/release/shadow --version
3740
- name: Run tests
38-
run: cargo test --verbose
41+
run: cargo test --release -vv

Cargo.toml

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,25 @@
11
[package]
2-
name = "shadow"
3-
version = "0.1.0"
2+
name = "darwinia-shadow"
3+
version = "0.2.0"
44
authors = ["clearloop <[email protected]>"]
55
edition = "2018"
6+
description = "The shadow service for relayers and verify workers to retrieve header data and generate proof."
7+
repository = "https://github.com/darwinia-network/shadow"
8+
license = "GPL-3.0-or-later"
9+
documentation = "https://docs.rs/darwinia-shadow"
10+
homepage = "https://github.com/darwinia-network/shadow"
11+
include = [
12+
"go.mod",
13+
"go.sum",
14+
"build.rs",
15+
"pkg/**/*",
16+
"src/**/*",
17+
"Cargo.toml",
18+
"./README.md",
19+
]
20+
keywords = ["darwinia", "substrate", "ethereum", "service"]
21+
readme = './README.md'
22+
links = "darwinia_shadow"
623

724
[[bin]]
825
name = "shadow"
@@ -38,12 +55,3 @@ features = ["derive"]
3855
[dependencies.diesel]
3956
version = "1.4.4"
4057
features = ["r2d2", "sqlite"]
41-
42-
[dependencies.eth]
43-
git = "https://github.com/darwinia-network/darwinia-common.git"
44-
package = "ethereum-primitives"
45-
optional = true
46-
47-
[features]
48-
darwinia = [ "eth" ]
49-
default = []

Dockerfile

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,31 @@
1-
# Build MMR in a stock rust builder container
2-
FROM rust:alpine as mmr
1+
# Build Shadow in a stock rust builder container
2+
FROM rust:alpine as shadow
33
ARG DEBIAN_FRONTEND=noninteractive
44
ENV TZ=America/Los_Angeles
55
COPY . shadow
6-
RUN apk add --no-cache sqlite-dev bash musl-dev \
7-
&& cd shadow \
8-
&& cargo build --release
96

10-
# Build Shadow in a stock Go builder container
11-
FROM golang:1.14-alpine as shadow
12-
ARG DEBIAN_FRONTEND=noninteractive
13-
ENV TZ=America/Los_Angeles
14-
COPY --from=mmr /shadow/target/release/libmmr.a /usr/local/lib/
15-
COPY . shadow
16-
RUN apk add --no-cache sqlite-dev sqlite-libs musl-dev gcc bash \
17-
&& mkdir /outputs \
7+
# Required dynamic libraries
8+
#
9+
# libdarwinia_shadow.so => /usr/local/lib/libdarwinia_shadow.so (0x7fd26af02000)
10+
# libssl.so.1.1 => /lib/libssl.so.1.1 (0x7fd26ae81000)
11+
# libcrypto.so.1.1 => /lib/libcrypto.so.1.1 (0x7fd26ac02000)
12+
# libsqlite3.so.0 => /usr/lib/libsqlite3.so.0 (0x7fd26ab1a000)
13+
# libc.musl-x86_64.so.1 => /lib/ld64.so.1 (0x7fd26bebb000)
14+
RUN apk add --no-cache gcc go openssl-dev sqlite-dev\
1815
&& cd shadow \
19-
&& go build -o /usr/local/bin/shadow -v bin/main.go
16+
&& cargo build --release -vv \
17+
&& mkdir /target \
18+
&& cp target/release/shadow /target/ \
19+
&& cp /usr/lib/libsqlite3.so.0 /target/libsqlite3.so.0 \
20+
&& cp /usr/local/lib/libdarwinia_shadow.so /target/libdarwinia_shadow.so
2021

21-
# Pull Geth and Shadow into a third stage deploy alpine container
22+
# Pull Shadow into a second stage deploy alpine container
2223
FROM alpine:latest
23-
COPY --from=shadow /usr/local/bin/shadow /usr/local/bin/shadow
24+
COPY --from=shadow /target /target
25+
RUN mv /target/shadow /usr/local/bin/shadow \
26+
&& mv /target/libsqlite3.so.0 /usr/lib/libsqlite3.so.0 \
27+
&& mv /target/libdarwinia_shadow.so /usr/local/lib/libdarwinia_shadow.so \
28+
&& cp /lib/libc.musl-x86_64.so.1 /lib/ld64.so.1 \
29+
&& rm -rf /target
2430
EXPOSE 3000
2531
ENTRYPOINT ["shadow"]

README.md

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,17 @@
11
# Shadow
22

3-
[![Golang CI][workflow-badge]][github]
3+
[![Shadow][workflow-badge]][github]
4+
[![crate](https://img.shields.io/crates/v/darwinia-shadow.svg)](https://crates.io/crates/darwinia_shadow)
5+
[![doc](https://img.shields.io/badge/current-docs-brightgreen.svg)](https://docs.rs/darwinia_shadow/)
6+
[![LICENSE](https://img.shields.io/crates/l/darwinia-shadow.svg)](https://choosealicense.com/licenses/gpl-3.0/)
47

58
The shadow service for relayers and verify workers to retrieve header data and generate proof. Shadow will index the data it needs from blockchain nodes, such as Ethereum and Darwinia.
69

10+
711
## Usage
812

913
```sh
10-
shadow 0.1.0
14+
shadow 0.2.0
1115

1216
USAGE:
1317
shadow <SUBCOMMAND>
@@ -23,36 +27,30 @@ SUBCOMMANDS:
2327
trim Trim mmr from target leaf
2428
```
2529

26-
## Contribute and Build
2730

28-
Downloads shadow service
31+
## Download
2932

30-
```
31-
git clone https://github.com/darwinia-network/shadow.git
33+
```sh
34+
$ cargo install darwinia-shadow
3235
```
3336

34-
Starts shadow service:
3537

36-
```
37-
# Starts shadow serives at port 3000
38-
$ cargo run -p 3000
38+
### Note
39+
40+
+ Please make sure you have `golang` installed in your machine
41+
+ Please make sure you have `sqlite3` installed in your machine
3942

40-
# If you have fast eth node:
41-
$ ETHEREUM_RPC=<your-api> cargo run -p 3000
42-
```
4343

4444
## Trouble Shooting
4545

4646
Everytime you run `proof` in error, please delete `~/.ethashproof` and `~/.ethash`
4747
and retry.
4848

49+
4950
## LICENSE
5051

5152
GPL-3.0
5253

5354

54-
[infura]: https://infura.io
5555
[github]: https://github.com/darwinia-network/shadow
56-
[spec]: https://github.com/darwinia-network/darwinia/wiki/Darwinia-offchain-worker-shadow-service-spec
57-
[workflow-badge]: https://github.com/darwinia-network/shadow/workflows/Golang%20CI/badge.svg
58-
[api]: https://darwinia-network.github.io/shadow
56+
[workflow-badge]: https://github.com/darwinia-network/shadow/workflows/shadow/badge.svg

build.rs

Lines changed: 50 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,59 @@
1-
use std::process::Command;
1+
use std::{env, process::Command};
22

33
fn main() {
4-
let os = Command::new("uname").output().unwrap();
5-
let ext = match String::from_utf8_lossy(os.stdout.as_slice())
6-
.into_owned()
7-
.trim_end()
8-
.as_ref()
9-
{
10-
"Darwin" => "dylib",
11-
_ => "so",
12-
};
4+
// Pre-check
5+
println!("cargo:rerun-if-changed=build.rs");
6+
println!("cargo:rerun-if-changed=path/to/Cargo.lock");
137

14-
let profile = match std::env::var("PROFILE").unwrap().as_str() {
15-
"release" => "release",
16-
_ => "debug",
17-
};
8+
// Declare build args
9+
let mut dynamic = true;
10+
let out_dir = env::var("OUT_DIR").unwrap();
11+
let staticlib = format!("{}/libdarwinia_shadow.a", out_dir);
12+
let dylib = format!(
13+
"/usr/local/lib/libdarwinia_shadow.{}",
14+
match String::from_utf8_lossy(Command::new("uname").output().unwrap().stdout.as_slice())
15+
.into_owned()
16+
.trim_end()
17+
.as_ref()
18+
{
19+
"Darwin" => "dylib",
20+
_ => "so",
21+
}
22+
);
1823

19-
let lib = format!("target/{}/libeth.{}", profile, ext);
20-
Command::new("go")
21-
.args(&[
24+
let args = |dynamic: bool| {
25+
vec![
2226
"build",
2327
"-o",
24-
&lib,
25-
"-buildmode=c-shared",
26-
"pkg/internal/ffi/mod.go",
27-
])
28+
if dynamic { &dylib } else { &staticlib },
29+
if dynamic {
30+
"-buildmode=c-shared"
31+
} else {
32+
"-buildmode=c-archive"
33+
},
34+
"-v",
35+
"pkg/shadow/ffi/mod.go",
36+
]
37+
};
38+
39+
// Build the link library
40+
if !Command::new("go")
41+
.args(&args(dynamic))
2842
.status()
29-
.unwrap();
43+
.unwrap()
44+
.success()
45+
{
46+
dynamic = false;
47+
Command::new("go").args(&args(dynamic)).status().unwrap();
48+
println!("built static library at {}", out_dir);
49+
}
3050

31-
println!(r"cargo:rustc-link-search=target/debug");
51+
// Post-check
52+
if dynamic {
53+
println!("cargo:rustc-link-search=/usr/local/lib");
54+
println!("cargo:rustc-link-lib=dylib=darwinia_shadow");
55+
} else {
56+
println!("cargo:rustc-link-search={}", out_dir);
57+
println!("cargo:rustc-link-lib=static=darwinia_shadow");
58+
}
3259
}

docker-compose.yml

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,6 @@ services:
1616
- data:/root
1717
depends_on:
1818
- geth
19-
mmr:
20-
image: quay.io/darwinia-network/shadow:next
21-
command: run -vm --no-fetch --no-api
22-
volumes:
23-
- data:/root
24-
depends_on:
25-
- fetcher
2619

2720
volumes:
2821
data:

examples/doctor.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
//! Mock the uncle block
22
use cmmr::{leaf_index_to_mmr_size, MMR};
3-
use shadow::{
4-
hash::{MergeHash, H256},
5-
pool,
6-
store::Store,
3+
use darwinia_shadow::{
4+
db::pool,
5+
mmr::{MergeHash, Store, H256},
76
};
87

98
fn main() {

examples/header.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use shadow::chain::eth::{EthHeader, EthHeaderRPCResp};
1+
use darwinia_shadow::chain::eth::{EthHeader, EthHeaderRPCResp};
22

33
fn main() {
44
let client = reqwest::Client::new();

examples/proof.rs

Lines changed: 0 additions & 10 deletions
This file was deleted.

0 commit comments

Comments
 (0)