From 4ca418d75c2901611371ab3dac128fb4900dbed3 Mon Sep 17 00:00:00 2001 From: clearloop Date: Thu, 14 Oct 2021 14:34:29 +0800 Subject: [PATCH 01/13] feat(api): arch api client --- Cargo.lock | 531 +++++++++++++++++++++++++++++++++++++++++++++- Cargo.toml | 1 + api/Cargo.toml | 10 + api/src/db.rs | 4 + api/src/env.rs | 4 + api/src/lib.rs | 7 + api/src/result.rs | 2 + api/src/types.rs | 94 ++++++++ 8 files changed, 652 insertions(+), 1 deletion(-) create mode 100644 api/Cargo.toml create mode 100644 api/src/db.rs create mode 100644 api/src/env.rs create mode 100644 api/src/lib.rs create mode 100644 api/src/result.rs create mode 100644 api/src/types.rs diff --git a/Cargo.lock b/Cargo.lock index 644f404..5c9bff9 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -62,6 +62,15 @@ version = "1.0.44" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "61604a8f862e1d5c3229fdd78f8b02c68dcf73a4c4b05fd636d12240aaa242c1" +[[package]] +name = "api" +version = "0.1.0" +dependencies = [ + "reqwest", + "serde", + "thiserror", +] + [[package]] name = "approx" version = "0.5.0" @@ -142,6 +151,12 @@ version = "0.12.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3441f0f7b02788e948e47f457ca01f1d7e6d92c693bc132c22b087d3141c03ff" +[[package]] +name = "base64" +version = "0.13.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "904dfeac50f3cdaba28fc6f57fdcddb75f49ed61346676a78c4ffe55877802fd" + [[package]] name = "bitflags" version = "1.3.2" @@ -224,6 +239,12 @@ version = "1.4.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "14c189c53d098945499cdfa7ecc63567cf3886b3332b312a5b4585d8d3a6a610" +[[package]] +name = "bytes" +version = "1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c4872d67bab6358e59559027aa3b9157c53d9358c51423c17554809a8858e0f8" + [[package]] name = "cc" version = "1.0.70" @@ -278,6 +299,22 @@ version = "0.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "6245d59a3e82a7fc217c5828a6692dbc6dfb63a0c8c90495621f7b9d79704a0e" +[[package]] +name = "core-foundation" +version = "0.9.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6888e10551bb93e424d8df1d07f1a8b4fceb0001a3a4b048bfc47554946f47b3" +dependencies = [ + "core-foundation-sys", + "libc", +] + +[[package]] +name = "core-foundation-sys" +version = "0.8.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5827cebf4670468b8772dd191856768aedcb1b0278a04f989f7766351917b9dc" + [[package]] name = "cpufeatures" version = "0.2.1" @@ -432,6 +469,15 @@ version = "1.6.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e78d4f1cc4ae33bbfc157ed5d5a5ef3bc29227303d595861deb238fcec4e9457" +[[package]] +name = "encoding_rs" +version = "0.8.28" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "80df024fbc5ac80f87dfef0d9f5209a252f2a497f7f42944cff24d8253cac065" +dependencies = [ + "cfg-if 1.0.0", +] + [[package]] name = "environmental" version = "1.1.3" @@ -479,6 +525,37 @@ dependencies = [ "static_assertions", ] +[[package]] +name = "fnv" +version = "1.0.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3f9eec918d3f24069decb9af1554cad7c880e2da24a9afd88aca000531ab82c1" + +[[package]] +name = "foreign-types" +version = "0.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f6f339eb8adc052cd2ca78910fda869aefa38d22d5cb648e6485e4d3fc06f3b1" +dependencies = [ + "foreign-types-shared", +] + +[[package]] +name = "foreign-types-shared" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "00b0228411908ca8685dba7fc2cdd70ec9990a6e753e89b6ac91a84c40fbaf4b" + +[[package]] +name = "form_urlencoded" +version = "1.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5fc25a87fa4fd2094bffb06925852034d90a17f0d1e05197d4956d3555752191" +dependencies = [ + "matches", + "percent-encoding", +] + [[package]] name = "frame-benchmarking" version = "4.0.0-dev" @@ -736,6 +813,25 @@ version = "0.25.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f0a01e0497841a3b2db4f8afa483cce65f7e96a3498bd6c541734792aeac8fe7" +[[package]] +name = "h2" +version = "0.3.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6c06815895acec637cd6ed6e9662c935b866d20a106f8361892893a7d9234964" +dependencies = [ + "bytes", + "fnv", + "futures-core", + "futures-sink", + "futures-util", + "http", + "indexmap", + "slab", + "tokio", + "tokio-util", + "tracing", +] + [[package]] name = "hash-db" version = "0.15.2" @@ -806,6 +902,88 @@ dependencies = [ "hmac 0.8.1", ] +[[package]] +name = "http" +version = "0.2.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1323096b05d41827dadeaee54c9981958c0f94e670bc94ed80037d1a7b8b186b" +dependencies = [ + "bytes", + "fnv", + "itoa", +] + +[[package]] +name = "http-body" +version = "0.4.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "399c583b2979440c60be0821a6199eca73bc3c8dcd9d070d75ac726e2c6186e5" +dependencies = [ + "bytes", + "http", + "pin-project-lite", +] + +[[package]] +name = "httparse" +version = "1.5.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "acd94fdbe1d4ff688b67b04eee2e17bd50995534a61539e45adfefb45e5e5503" + +[[package]] +name = "httpdate" +version = "1.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6456b8a6c8f33fee7d958fcd1b60d55b11940a79e63ae87013e6d22e26034440" + +[[package]] +name = "hyper" +version = "0.14.13" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "15d1cfb9e4f68655fa04c01f59edb405b6074a0f7118ea881e5026e4a1cd8593" +dependencies = [ + "bytes", + "futures-channel", + "futures-core", + "futures-util", + "h2", + "http", + "http-body", + "httparse", + "httpdate", + "itoa", + "pin-project-lite", + "socket2", + "tokio", + "tower-service", + "tracing", + "want", +] + +[[package]] +name = "hyper-tls" +version = "0.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d6183ddfa99b85da61a140bea0efc93fdf56ceaa041b37d553518030827f9905" +dependencies = [ + "bytes", + "hyper", + "native-tls", + "tokio", + "tokio-native-tls", +] + +[[package]] +name = "idna" +version = "0.2.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "418a0a6fab821475f634efe3ccc45c013f742efe03d853e8d3355d5cb850ecf8" +dependencies = [ + "matches", + "unicode-bidi", + "unicode-normalization", +] + [[package]] name = "impl-codec" version = "0.5.1" @@ -835,6 +1013,16 @@ dependencies = [ "syn", ] +[[package]] +name = "indexmap" +version = "1.7.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bc633605454125dec4b66843673f01c7df2b89479b32e0ed634e43a91cff62a5" +dependencies = [ + "autocfg", + "hashbrown", +] + [[package]] name = "instant" version = "0.1.11" @@ -853,6 +1041,12 @@ dependencies = [ "num-traits", ] +[[package]] +name = "ipnet" +version = "2.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "68f2d64f2edebec4ce84ad108148e67e1064789bee435edc5b60ad398714a3a9" + [[package]] name = "itoa" version = "0.4.8" @@ -908,7 +1102,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c9d220bc1feda2ac231cb78c3d26f27676b8cf82c96971f7aeef3d0cf2797c73" dependencies = [ "arrayref", - "base64", + "base64 0.12.3", "digest 0.9.0", "hmac-drbg", "libsecp256k1-core", @@ -995,6 +1189,12 @@ dependencies = [ "regex-automata", ] +[[package]] +name = "matches" +version = "0.1.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a3e378b66a060d48947b590737b30a1be76706c8dd7b8ba0f2fe3989c68a853f" + [[package]] name = "matrixmultiply" version = "0.3.1" @@ -1039,6 +1239,12 @@ dependencies = [ "zeroize", ] +[[package]] +name = "mime" +version = "0.3.16" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2a60c7ce501c71e03a9c9c0d35b861413ae925bd979cc7a4e30d060069aaac8d" + [[package]] name = "miniz_oxide" version = "0.4.4" @@ -1049,6 +1255,28 @@ dependencies = [ "autocfg", ] +[[package]] +name = "mio" +version = "0.7.13" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8c2bdb6314ec10835cd3293dd268473a835c02b7b352e788be788b3c6ca6bb16" +dependencies = [ + "libc", + "log", + "miow", + "ntapi", + "winapi", +] + +[[package]] +name = "miow" +version = "0.3.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b9f1c5b025cda876f66ef43a113f91ebc9f4ccef34843000e0adf6ebbab84e21" +dependencies = [ + "winapi", +] + [[package]] name = "nalgebra" version = "0.27.1" @@ -1078,12 +1306,39 @@ dependencies = [ "syn", ] +[[package]] +name = "native-tls" +version = "0.2.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "48ba9f7719b5a0f42f338907614285fb5fd70e53858141f69898a1fb7203b24d" +dependencies = [ + "lazy_static", + "libc", + "log", + "openssl", + "openssl-probe", + "openssl-sys", + "schannel", + "security-framework", + "security-framework-sys", + "tempfile", +] + [[package]] name = "nodrop" version = "0.1.14" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "72ef4a56884ca558e5ddb05a1d1e7e1bfd9a68d9ed024c21704cc98872dae1bb" +[[package]] +name = "ntapi" +version = "0.3.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3f6bb902e437b6d86e03cce10a7e2af662292c5dfef23b65899ea3ac9354ad44" +dependencies = [ + "winapi", +] + [[package]] name = "num-bigint" version = "0.2.6" @@ -1184,6 +1439,39 @@ version = "0.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "624a8340c38c1b80fd549087862da4ba43e08858af025b236e509b6649fc13d5" +[[package]] +name = "openssl" +version = "0.10.36" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8d9facdb76fec0b73c406f125d44d86fdad818d66fef0531eec9233ca425ff4a" +dependencies = [ + "bitflags", + "cfg-if 1.0.0", + "foreign-types", + "libc", + "once_cell", + "openssl-sys", +] + +[[package]] +name = "openssl-probe" +version = "0.1.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "28988d872ab76095a6e6ac88d99b54fd267702734fd7ffe610ca27f533ddb95a" + +[[package]] +name = "openssl-sys" +version = "0.9.67" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "69df2d8dfc6ce3aaf44b40dec6f487d5a886516cf6879c49e98e0710f310a058" +dependencies = [ + "autocfg", + "cc", + "libc", + "pkg-config", + "vcpkg", +] + [[package]] name = "parity-scale-codec" version = "2.3.1" @@ -1315,6 +1603,12 @@ dependencies = [ "crypto-mac 0.11.1", ] +[[package]] +name = "percent-encoding" +version = "2.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d4fd5641d01c8f18a23da7b6fe29298ff4b55afcccdf78973b24cf3175fee32e" + [[package]] name = "pest" version = "2.1.3" @@ -1336,6 +1630,12 @@ version = "0.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8b870d8c151b6f2fb93e84a13146138f05d02ed11c7e7c54f8826aaaf7c9f184" +[[package]] +name = "pkg-config" +version = "0.3.20" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7c9b1041b4387893b91ee6746cddfc28516aff326a3519fb2adf820932c5e6cb" + [[package]] name = "ppv-lite86" version = "0.2.10" @@ -1568,6 +1868,50 @@ version = "0.6.25" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f497285884f3fcff424ffc933e56d7cbca511def0c9831a7f9b5f6153e3cc89b" +[[package]] +name = "remove_dir_all" +version = "0.5.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3acd125665422973a33ac9d3dd2df85edad0f4ae9b00dafb1a05e43a9f5ef8e7" +dependencies = [ + "winapi", +] + +[[package]] +name = "reqwest" +version = "0.11.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "51c732d463dd300362ffb44b7b125f299c23d2990411a4253824630ebc7467fb" +dependencies = [ + "base64 0.13.0", + "bytes", + "encoding_rs", + "futures-core", + "futures-util", + "http", + "http-body", + "hyper", + "hyper-tls", + "ipnet", + "js-sys", + "lazy_static", + "log", + "mime", + "native-tls", + "percent-encoding", + "pin-project-lite", + "serde", + "serde_json", + "serde_urlencoded", + "tokio", + "tokio-native-tls", + "url", + "wasm-bindgen", + "wasm-bindgen-futures", + "web-sys", + "winreg", +] + [[package]] name = "rustc-demangle" version = "0.1.21" @@ -1611,6 +1955,16 @@ version = "1.0.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "71d301d4193d031abdd79ff7e3dd721168a9572ef3fe51a1517aba235bd8f86e" +[[package]] +name = "schannel" +version = "0.1.19" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8f05ba609c234e60bee0d547fe94a4c7e9da733d1c962cf6e59efa4cd9c8bc75" +dependencies = [ + "lazy_static", + "winapi", +] + [[package]] name = "schnorrkel" version = "0.9.1" @@ -1644,6 +1998,29 @@ dependencies = [ "zeroize", ] +[[package]] +name = "security-framework" +version = "2.4.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "525bc1abfda2e1998d152c45cf13e696f76d0a4972310b22fac1658b05df7c87" +dependencies = [ + "bitflags", + "core-foundation", + "core-foundation-sys", + "libc", + "security-framework-sys", +] + +[[package]] +name = "security-framework-sys" +version = "2.4.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a9dd14d83160b528b7bfd66439110573efcfbe281b17fc2ca9f39f550d619c7e" +dependencies = [ + "core-foundation-sys", + "libc", +] + [[package]] name = "semver" version = "0.11.0" @@ -1693,6 +2070,18 @@ dependencies = [ "serde", ] +[[package]] +name = "serde_urlencoded" +version = "0.7.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "edfa57a7f8d9c1d260a549e7224100f6c43d43f9103e06dd8b4095a9b2b43ce9" +dependencies = [ + "form_urlencoded", + "itoa", + "ryu", + "serde", +] + [[package]] name = "sha2" version = "0.8.2" @@ -1766,6 +2155,16 @@ version = "1.7.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1ecab6c735a6bb4139c0caafd0cc3635748bbb3acf4550e8138122099251f309" +[[package]] +name = "socket2" +version = "0.4.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5dc90fe6c7be1a323296982db1836d1ea9e47b6839496dde9a541bc496df3516" +dependencies = [ + "libc", + "winapi", +] + [[package]] name = "sp-api" version = "4.0.0-dev" @@ -2196,6 +2595,20 @@ version = "1.0.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "55937e1799185b12863d447f42597ed69d9928686b8d88a1df17376a097d8369" +[[package]] +name = "tempfile" +version = "3.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dac1c663cfc93810f88aed9b8941d48cabf856a1b111c29a40439018d870eb22" +dependencies = [ + "cfg-if 1.0.0", + "libc", + "rand 0.8.4", + "redox_syscall 0.2.10", + "remove_dir_all", + "winapi", +] + [[package]] name = "thiserror" version = "1.0.29" @@ -2268,6 +2681,45 @@ version = "0.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "cda74da7e1a664f795bb1f8a87ec406fb89a02522cf6e50620d016add6dbbf5c" +[[package]] +name = "tokio" +version = "1.12.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c2c2416fdedca8443ae44b4527de1ea633af61d8f7169ffa6e72c5b53d24efcc" +dependencies = [ + "autocfg", + "bytes", + "libc", + "memchr", + "mio", + "pin-project-lite", + "winapi", +] + +[[package]] +name = "tokio-native-tls" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f7d995660bd2b7f8c1568414c1126076c13fbb725c40112dc0120b78eb9b717b" +dependencies = [ + "native-tls", + "tokio", +] + +[[package]] +name = "tokio-util" +version = "0.6.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "08d3725d3efa29485e87311c5b699de63cde14b00ed4d256b8318aa30ca452cd" +dependencies = [ + "bytes", + "futures-core", + "futures-sink", + "log", + "pin-project-lite", + "tokio", +] + [[package]] name = "toml" version = "0.5.8" @@ -2277,6 +2729,12 @@ dependencies = [ "serde", ] +[[package]] +name = "tower-service" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "360dfd1d6d30e05fda32ace2c8c70e9c0a9da713275777f5a4dbb8a1893930c6" + [[package]] name = "tracing" version = "0.1.28" @@ -2374,6 +2832,12 @@ dependencies = [ "hash-db", ] +[[package]] +name = "try-lock" +version = "0.2.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "59547bce71d9c38b83d9c0e92b6066c4253371f15005def0c30d9657f50c7642" + [[package]] name = "twox-hash" version = "1.6.1" @@ -2409,6 +2873,12 @@ dependencies = [ "static_assertions", ] +[[package]] +name = "unicode-bidi" +version = "0.3.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1a01404663e3db436ed2746d9fefef640d868edae3cceb81c3b8d5732fda678f" + [[package]] name = "unicode-normalization" version = "0.1.19" @@ -2424,12 +2894,40 @@ version = "0.2.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8ccb82d61f80a663efe1f787a51b16b5a51e3314d6ac365b08639f52387b33f3" +[[package]] +name = "url" +version = "2.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a507c383b2d33b5fc35d1861e77e6b383d158b2da5e14fe51b83dfedf6fd578c" +dependencies = [ + "form_urlencoded", + "idna", + "matches", + "percent-encoding", +] + +[[package]] +name = "vcpkg" +version = "0.2.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "accd4ea62f7bb7a82fe23066fb0957d48ef677f6eeb8215f372f52e48bb32426" + [[package]] name = "version_check" version = "0.9.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "5fecdca9a5291cc2b8dcf7dc02453fee791a280f3743cb0905f8822ae463b3fe" +[[package]] +name = "want" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1ce8a968cb1cd110d136ff8b819a556d6fb6d919363c61534f6860c7eb172ba0" +dependencies = [ + "log", + "try-lock", +] + [[package]] name = "wasi" version = "0.9.0+wasi-snapshot-preview1" @@ -2467,6 +2965,18 @@ dependencies = [ "wasm-bindgen-shared", ] +[[package]] +name = "wasm-bindgen-futures" +version = "0.4.28" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8e8d7523cb1f2a4c96c1317ca690031b714a51cc14e05f712446691f413f5d39" +dependencies = [ + "cfg-if 1.0.0", + "js-sys", + "wasm-bindgen", + "web-sys", +] + [[package]] name = "wasm-bindgen-macro" version = "0.2.78" @@ -2520,6 +3030,16 @@ dependencies = [ "parity-wasm", ] +[[package]] +name = "web-sys" +version = "0.3.55" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "38eb105f1c59d9eaa6b5cdc92b859d85b926e82cb2e0945cd0c9259faa6fe9fb" +dependencies = [ + "js-sys", + "wasm-bindgen", +] + [[package]] name = "winapi" version = "0.3.9" @@ -2542,6 +3062,15 @@ version = "0.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" +[[package]] +name = "winreg" +version = "0.7.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0120db82e8a1e0b9fb3345a539c478767c0048d842860994d96113d5b667bd69" +dependencies = [ + "winapi", +] + [[package]] name = "wyz" version = "0.2.0" diff --git a/Cargo.toml b/Cargo.toml index 11a6bbd..666544c 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,4 +1,5 @@ [workspace] members = [ + "api", "filecoindot" ] \ No newline at end of file diff --git a/api/Cargo.toml b/api/Cargo.toml new file mode 100644 index 0000000..593d567 --- /dev/null +++ b/api/Cargo.toml @@ -0,0 +1,10 @@ +[package] +name = "api" +version = "0.1.0" +edition = "2018" + +[dependencies] +reqwest = "^0.11" +rocksdb = "0.17.0" +serde = { version = "^1", features = ["derive"] } +thiserror = "^1" diff --git a/api/src/db.rs b/api/src/db.rs new file mode 100644 index 0000000..19e43a4 --- /dev/null +++ b/api/src/db.rs @@ -0,0 +1,4 @@ +// Copyright 2021 ChainSafe Systems +// SPDX-License-Identifier: LGPL-3.0-only + +// local storage for handling cache diff --git a/api/src/env.rs b/api/src/env.rs new file mode 100644 index 0000000..e19c02a --- /dev/null +++ b/api/src/env.rs @@ -0,0 +1,4 @@ +// Copyright 2021 ChainSafe Systems +// SPDX-License-Identifier: LGPL-3.0-only + +//! Module for handling environments diff --git a/api/src/lib.rs b/api/src/lib.rs new file mode 100644 index 0000000..b21659f --- /dev/null +++ b/api/src/lib.rs @@ -0,0 +1,7 @@ +// Copyright 2021 ChainSafe Systems +// SPDX-License-Identifier: LGPL-3.0-only + +mod db; +mod env; +mod result; +mod types; diff --git a/api/src/result.rs b/api/src/result.rs new file mode 100644 index 0000000..b309007 --- /dev/null +++ b/api/src/result.rs @@ -0,0 +1,2 @@ +// Copyright 2021 ChainSafe Systems +// SPDX-License-Identifier: LGPL-3.0-only diff --git a/api/src/types.rs b/api/src/types.rs new file mode 100644 index 0000000..8e291f7 --- /dev/null +++ b/api/src/types.rs @@ -0,0 +1,94 @@ +// Copyright 2021 ChainSafe Systems +// SPDX-License-Identifier: LGPL-3.0-only + +//! Filecoin api types + +use serde::{Deserialize, Serialize}; + +/// Response of the [`ChainHead`](https://docs.filecoin.io/reference/lotus-api/#chainhead) RPC call +#[derive(Clone, Debug, Serialize, Deserialize)] +pub struct GetChainHead { + pub jsonrpc: String, + pub result: TipSet, + pub id: i64, +} + +#[derive(Clone, Debug, Serialize, Deserialize)] +pub struct TipSet { + #[serde(rename = "Cids")] + pub cids: Vec, + #[serde(rename = "Blocks")] + pub blocks: Vec, + #[serde(rename = "Height")] + pub height: i64, +} + +#[derive(Clone, Debug, Serialize, Deserialize)] +pub struct Block { + #[serde(rename = "Miner")] + pub miner: String, + #[serde(rename = "Ticket")] + pub ticket: ElectionProof, + #[serde(rename = "ElectionProof")] + pub election_proof: ElectionProof, + #[serde(rename = "BeaconEntries")] + pub beacon_entries: Vec, + #[serde(rename = "WinPoStProof")] + pub win_po_st_proof: Vec, + #[serde(rename = "Parents")] + pub parents: Vec, + #[serde(rename = "ParentWeight")] + pub parent_weight: String, + #[serde(rename = "Height")] + pub height: i64, + #[serde(rename = "ParentStateRoot")] + pub parent_state_root: Cid, + #[serde(rename = "ParentMessageReceipts")] + pub parent_message_receipts: Cid, + #[serde(rename = "Messages")] + pub messages: Cid, + #[serde(rename = "BLSAggregate")] + pub bls_aggregate: BlsAggregate, + #[serde(rename = "Timestamp")] + pub timestamp: i64, + #[serde(rename = "BlockSig")] + pub block_sig: BlsAggregate, + #[serde(rename = "ForkSignaling")] + pub fork_signaling: i64, +} + +#[derive(Clone, Debug, Serialize, Deserialize)] +pub struct BeaconEntry { + #[serde(rename = "Round")] + pub round: i64, + #[serde(rename = "Data")] + pub data: String, +} + +#[derive(Clone, Debug, Serialize, Deserialize)] +pub struct BlsAggregate { + #[serde(rename = "Type")] + pub bls_aggregate_type: i64, + #[serde(rename = "Data")] + pub data: String, +} + +#[derive(Clone, Debug, Serialize, Deserialize)] +pub struct ElectionProof { + #[serde(rename = "VRFProof")] + pub vrf_proof: String, +} + +#[derive(Clone, Debug, Serialize, Deserialize)] +pub struct Cid { + #[serde(rename = "/")] + pub empty: String, +} + +#[derive(Clone, Debug, Serialize, Deserialize)] +pub struct WinPoStProof { + #[serde(rename = "RegisteredProof")] + pub registered_proof: i64, + #[serde(rename = "ProofBytes")] + pub proof_bytes: String, +} From 03dfc10a0417d2a592a4c588498518928ef033be Mon Sep 17 00:00:00 2001 From: clearloop Date: Thu, 14 Oct 2021 15:00:12 +0800 Subject: [PATCH 02/13] feat(relayer): rename crate api to relayer --- {api => relayer}/Cargo.toml | 2 +- relayer/src/api.rs | 6 ++++++ relayer/src/client.rs | 4 ++++ {api => relayer}/src/db.rs | 0 {api => relayer}/src/env.rs | 0 {api => relayer}/src/lib.rs | 2 ++ {api => relayer}/src/result.rs | 0 relayer/src/traits.rs | 4 ++++ {api => relayer}/src/types.rs | 0 9 files changed, 17 insertions(+), 1 deletion(-) rename {api => relayer}/Cargo.toml (90%) create mode 100644 relayer/src/api.rs create mode 100644 relayer/src/client.rs rename {api => relayer}/src/db.rs (100%) rename {api => relayer}/src/env.rs (100%) rename {api => relayer}/src/lib.rs (85%) rename {api => relayer}/src/result.rs (100%) create mode 100644 relayer/src/traits.rs rename {api => relayer}/src/types.rs (100%) diff --git a/api/Cargo.toml b/relayer/Cargo.toml similarity index 90% rename from api/Cargo.toml rename to relayer/Cargo.toml index 593d567..11129b2 100644 --- a/api/Cargo.toml +++ b/relayer/Cargo.toml @@ -1,5 +1,5 @@ [package] -name = "api" +name = "relayer" version = "0.1.0" edition = "2018" diff --git a/relayer/src/api.rs b/relayer/src/api.rs new file mode 100644 index 0000000..d4bb99c --- /dev/null +++ b/relayer/src/api.rs @@ -0,0 +1,6 @@ +// Copyright 2021 ChainSafe Systems +// SPDX-License-Identifier: LGPL-3.0-only + +//! Filecoin APIs + +use crate::types::Block; diff --git a/relayer/src/client.rs b/relayer/src/client.rs new file mode 100644 index 0000000..f17c563 --- /dev/null +++ b/relayer/src/client.rs @@ -0,0 +1,4 @@ +// Copyright 2021 ChainSafe Systems +// SPDX-License-Identifier: LGPL-3.0-only + +//! RPC client for requesting data from filecoin RPC diff --git a/api/src/db.rs b/relayer/src/db.rs similarity index 100% rename from api/src/db.rs rename to relayer/src/db.rs diff --git a/api/src/env.rs b/relayer/src/env.rs similarity index 100% rename from api/src/env.rs rename to relayer/src/env.rs diff --git a/api/src/lib.rs b/relayer/src/lib.rs similarity index 85% rename from api/src/lib.rs rename to relayer/src/lib.rs index b21659f..98fa281 100644 --- a/api/src/lib.rs +++ b/relayer/src/lib.rs @@ -1,6 +1,8 @@ // Copyright 2021 ChainSafe Systems // SPDX-License-Identifier: LGPL-3.0-only +mod api; +mod client; mod db; mod env; mod result; diff --git a/api/src/result.rs b/relayer/src/result.rs similarity index 100% rename from api/src/result.rs rename to relayer/src/result.rs diff --git a/relayer/src/traits.rs b/relayer/src/traits.rs new file mode 100644 index 0000000..4877376 --- /dev/null +++ b/relayer/src/traits.rs @@ -0,0 +1,4 @@ +// Copyright 2021 ChainSafe Systems +// SPDX-License-Identifier: LGPL-3.0-only + +//! Custom tratis diff --git a/api/src/types.rs b/relayer/src/types.rs similarity index 100% rename from api/src/types.rs rename to relayer/src/types.rs From d973ddb7eef0c5976dad8be8bf8e12e4746cec18 Mon Sep 17 00:00:00 2001 From: clearloop Date: Thu, 14 Oct 2021 15:27:58 +0800 Subject: [PATCH 03/13] feat(relayer): add env getter --- Cargo.lock | 179 +++++++++++++++++++++++++++++++++++------- Cargo.toml | 2 +- relayer/src/api.rs | 2 +- relayer/src/client.rs | 5 ++ relayer/src/env.rs | 15 ++++ relayer/src/lib.rs | 8 +- relayer/src/result.rs | 10 +++ 7 files changed, 190 insertions(+), 31 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 5c9bff9..fd62be3 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -14,9 +14,9 @@ dependencies = [ [[package]] name = "addr2line" -version = "0.16.0" +version = "0.15.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3e61f2b7f93d2c7d2b08263acaa4a363b3e276806c68af6134c44f523bf1aacd" +checksum = "e7a2e47a1fbe209ee101dd6d61285226744c6c8d3c21c8dc878ba6cb9f467f3a" dependencies = [ "gimli", ] @@ -40,9 +40,9 @@ dependencies = [ [[package]] name = "aho-corasick" -version = "0.7.18" +version = "0.7.15" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1e37cfd5e7657ada45f742d6e99ca5788580b5c529dc78faf11ece6dc702656f" +checksum = "7404febffaa47dac81aa44dba71523c9d069b1bdc50a77db41195149e17f68e5" dependencies = [ "memchr", ] @@ -62,15 +62,6 @@ version = "1.0.44" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "61604a8f862e1d5c3229fdd78f8b02c68dcf73a4c4b05fd636d12240aaa242c1" -[[package]] -name = "api" -version = "0.1.0" -dependencies = [ - "reqwest", - "serde", - "thiserror", -] - [[package]] name = "approx" version = "0.5.0" @@ -126,9 +117,9 @@ checksum = "cdb031dd78e28731d87d56cc8ffef4a8f36ca26c38fe2de700543e627f8a464a" [[package]] name = "backtrace" -version = "0.3.61" +version = "0.3.59" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e7a905d892734eea339e896738c14b9afce22b5318f64b951e70bf3844419b01" +checksum = "4717cfcbfaa661a0fd48f8453951837ae7e8f81e481fbb136e3202d72805a744" dependencies = [ "addr2line", "cc", @@ -157,12 +148,43 @@ version = "0.13.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "904dfeac50f3cdaba28fc6f57fdcddb75f49ed61346676a78c4ffe55877802fd" +[[package]] +name = "bindgen" +version = "0.59.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "453c49e5950bb0eb63bb3df640e31618846c89d5b7faa54040d76e98e0134375" +dependencies = [ + "bitflags", + "cexpr", + "clang-sys", + "lazy_static", + "lazycell", + "peeking_take_while", + "proc-macro2", + "quote", + "regex", + "rustc-hash", + "shlex", +] + [[package]] name = "bitflags" version = "1.3.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a" +[[package]] +name = "bitvec" +version = "0.19.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8942c8d352ae1838c9dda0b0ca2ab657696ef2232a20147cf1b30ae1a9cb4321" +dependencies = [ + "funty", + "radium 0.5.3", + "tap", + "wyz", +] + [[package]] name = "bitvec" version = "0.20.4" @@ -170,7 +192,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7774144344a4faa177370406a7ff5f1da24303817368584c6206c8303eb07848" dependencies = [ "funty", - "radium", + "radium 0.6.2", "tap", "wyz", ] @@ -254,6 +276,15 @@ dependencies = [ "jobserver", ] +[[package]] +name = "cexpr" +version = "0.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "db507a7679252d2276ed0dd8113c6875ec56d3089f9225b2b42c30cc1f8e5c89" +dependencies = [ + "nom", +] + [[package]] name = "cfg-if" version = "0.1.10" @@ -278,6 +309,17 @@ dependencies = [ "winapi", ] +[[package]] +name = "clang-sys" +version = "1.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "10612c0ec0e0a1ff0e97980647cb058a6e7aedb913d01d009c406b8b7d0b26ee" +dependencies = [ + "glob", + "libc", + "libloading", +] + [[package]] name = "cloudabi" version = "0.0.3" @@ -809,9 +851,15 @@ dependencies = [ [[package]] name = "gimli" -version = "0.25.0" +version = "0.24.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0e4075386626662786ddb0ec9081e7c7eeb1ba31951f447ca780ef9f5d568189" + +[[package]] +name = "glob" +version = "0.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f0a01e0497841a3b2db4f8afa483cce65f7e96a3498bd6c541734792aeac8fe7" +checksum = "9b919933a397b79c37e33b77bb2aa3dc8eb6e165ad809e58ff75bc7db2e34574" [[package]] name = "h2" @@ -1083,18 +1131,46 @@ version = "1.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646" +[[package]] +name = "lazycell" +version = "1.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "830d08ce1d1d941e6b30645f1a0eb5643013d835ce3779a5fc208261dbe10f55" + [[package]] name = "libc" version = "0.2.103" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "dd8f7255a17a627354f321ef0055d63b898c6fb27eff628af4d1b66b7331edf6" +[[package]] +name = "libloading" +version = "0.7.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c0cf036d15402bea3c5d4de17b3fce76b3e4a56ebc1f577be0e7a72f7c607cf0" +dependencies = [ + "cfg-if 1.0.0", + "winapi", +] + [[package]] name = "libm" version = "0.2.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c7d73b3f436185384286bd8098d17ec07c9a7d2388a6599f824d8502b529702a" +[[package]] +name = "librocksdb-sys" +version = "6.20.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c309a9d2470844aceb9a4a098cf5286154d20596868b75a6b36357d2bb9ca25d" +dependencies = [ + "bindgen", + "cc", + "glob", + "libc", +] + [[package]] name = "libsecp256k1" version = "0.6.0" @@ -1206,9 +1282,9 @@ dependencies = [ [[package]] name = "memchr" -version = "2.4.1" +version = "2.3.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "308cc39be01b73d0d18f82a0e7b2a3df85245f84af96fdddc5d202d27e47b86a" +checksum = "0ee1c47aaa256ecabcaea351eae4a9b01ef39ed810004e298d2511ed284b1525" [[package]] name = "memory-db" @@ -1330,6 +1406,18 @@ version = "0.1.14" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "72ef4a56884ca558e5ddb05a1d1e7e1bfd9a68d9ed024c21704cc98872dae1bb" +[[package]] +name = "nom" +version = "6.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9c5c51b9083a3c620fa67a2a635d1ce7d95b897e957d6b28ff9a5da960a103a6" +dependencies = [ + "bitvec 0.19.5", + "funty", + "memchr", + "version_check", +] + [[package]] name = "ntapi" version = "0.3.6" @@ -1414,12 +1502,9 @@ dependencies = [ [[package]] name = "object" -version = "0.26.2" +version = "0.24.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "39f37e50073ccad23b6d09bcb5b263f4e76d3bb6038e4a3c08e52162ffa8abc2" -dependencies = [ - "memchr", -] +checksum = "1a5b3dd1c072ee7963717671d1ca129f1048fda25edea6b752bfc71ac8854170" [[package]] name = "once_cell" @@ -1479,7 +1564,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "373b1a4c1338d9cd3d1fa53b3a11bdab5ab6bd80a20f7f7becd76953ae2be909" dependencies = [ "arrayvec 0.7.1", - "bitvec", + "bitvec 0.20.4", "byte-slice-cast", "impl-trait-for-tuples", "parity-scale-codec-derive", @@ -1603,6 +1688,12 @@ dependencies = [ "crypto-mac 0.11.1", ] +[[package]] +name = "peeking_take_while" +version = "0.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "19b17cddbe7ec3f8bc800887bab5e717348c95ea2ca0b1bf0837fb964dc67099" + [[package]] name = "percent-encoding" version = "2.1.0" @@ -1694,6 +1785,12 @@ dependencies = [ "proc-macro2", ] +[[package]] +name = "radium" +version = "0.5.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "941ba9d78d8e2f7ce474c015eea4d9c6d25b6a3327f9832ee29a4de27f91bbb8" + [[package]] name = "radium" version = "0.6.2" @@ -1844,9 +1941,9 @@ dependencies = [ [[package]] name = "regex" -version = "1.5.4" +version = "1.4.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d07a8629359eb56f1e2fb1652bb04212c072a87ba68546a04065d525673ac461" +checksum = "2a26af418b574bd56588335b3a3659a65725d4e636eb1016c2f9e3b38c7cc759" dependencies = [ "aho-corasick", "memchr", @@ -1868,6 +1965,16 @@ version = "0.6.25" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f497285884f3fcff424ffc933e56d7cbca511def0c9831a7f9b5f6153e3cc89b" +[[package]] +name = "relayer" +version = "0.1.0" +dependencies = [ + "reqwest", + "rocksdb", + "serde", + "thiserror", +] + [[package]] name = "remove_dir_all" version = "0.5.3" @@ -1912,6 +2019,16 @@ dependencies = [ "winreg", ] +[[package]] +name = "rocksdb" +version = "0.17.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7a62eca5cacf2c8261128631bed9f045598d40bfbe4b29f5163f0f802f8f44a7" +dependencies = [ + "libc", + "librocksdb-sys", +] + [[package]] name = "rustc-demangle" version = "0.1.21" @@ -2116,6 +2233,12 @@ dependencies = [ "lazy_static", ] +[[package]] +name = "shlex" +version = "1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "43b2853a4d09f215c24cc5489c992ce46052d359b5109343cbafbf26bc62f8a3" + [[package]] name = "signature" version = "1.3.1" diff --git a/Cargo.toml b/Cargo.toml index 666544c..f68a440 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,5 +1,5 @@ [workspace] members = [ - "api", + "relayer", "filecoindot" ] \ No newline at end of file diff --git a/relayer/src/api.rs b/relayer/src/api.rs index d4bb99c..2448da6 100644 --- a/relayer/src/api.rs +++ b/relayer/src/api.rs @@ -3,4 +3,4 @@ //! Filecoin APIs -use crate::types::Block; +// use crate::types::Block; diff --git a/relayer/src/client.rs b/relayer/src/client.rs index f17c563..eac0471 100644 --- a/relayer/src/client.rs +++ b/relayer/src/client.rs @@ -2,3 +2,8 @@ // SPDX-License-Identifier: LGPL-3.0-only //! RPC client for requesting data from filecoin RPC + +use reqwest::Client as ReqwestClinet; + +/// RPC Client of filecoindot relayers +pub struct Client(ReqwestClinet); diff --git a/relayer/src/env.rs b/relayer/src/env.rs index e19c02a..93f0c78 100644 --- a/relayer/src/env.rs +++ b/relayer/src/env.rs @@ -2,3 +2,18 @@ // SPDX-License-Identifier: LGPL-3.0-only //! Module for handling environments + +use crate::{Error, Result}; +use std::env::var; + +pub const FILECOIN_RPC_ENV: &str = "FILECOIN_RPC"; + +/// A set of reserved environemts for relayer client +pub struct Env; + +impl Env { + /// Get environment variable `FILECOIN_RPC` + pub fn rpc() -> Result { + var(FILECOIN_RPC_ENV).map_err(|_| Error::NoRPCEndpoint) + } +} diff --git a/relayer/src/lib.rs b/relayer/src/lib.rs index 98fa281..39436db 100644 --- a/relayer/src/lib.rs +++ b/relayer/src/lib.rs @@ -6,4 +6,10 @@ mod client; mod db; mod env; mod result; -mod types; +pub mod types; + +pub use crate::{ + client::Client, + env::Env, + result::{Error, Result}, +}; diff --git a/relayer/src/result.rs b/relayer/src/result.rs index b309007..6ff9ab9 100644 --- a/relayer/src/result.rs +++ b/relayer/src/result.rs @@ -1,2 +1,12 @@ // Copyright 2021 ChainSafe Systems // SPDX-License-Identifier: LGPL-3.0-only + +use thiserror::Error as ThisError; + +#[derive(ThisError, Debug)] +pub enum Error { + #[error("could not find FILECOINT_RPC in environment variables")] + NoRPCEndpoint, +} + +pub type Result = std::result::Result; From c3708d52422911a85ed28b9bbc290a993d2610f5 Mon Sep 17 00:00:00 2001 From: clearloop Date: Thu, 14 Oct 2021 16:39:30 +0800 Subject: [PATCH 04/13] feat(relayer): add http client with ChainGetBlock api --- Cargo.lock | 1 + relayer/Cargo.toml | 5 +++-- relayer/src/api.rs | 34 +++++++++++++++++++++++++++++++++- relayer/src/client.rs | 32 +++++++++++++++++++++++++++++++- relayer/src/result.rs | 2 ++ relayer/src/types.rs | 2 +- 6 files changed, 71 insertions(+), 5 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index fd62be3..ed8d00c 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1969,6 +1969,7 @@ checksum = "f497285884f3fcff424ffc933e56d7cbca511def0c9831a7f9b5f6153e3cc89b" name = "relayer" version = "0.1.0" dependencies = [ + "async-trait", "reqwest", "rocksdb", "serde", diff --git a/relayer/Cargo.toml b/relayer/Cargo.toml index 11129b2..eb57316 100644 --- a/relayer/Cargo.toml +++ b/relayer/Cargo.toml @@ -4,7 +4,8 @@ version = "0.1.0" edition = "2018" [dependencies] -reqwest = "^0.11" +async-trait = "0.1.51" +reqwest = { version = "^0.11", features = [ "json" ] } rocksdb = "0.17.0" -serde = { version = "^1", features = ["derive"] } +serde = { version = "^1", features = [ "derive" ] } thiserror = "^1" diff --git a/relayer/src/api.rs b/relayer/src/api.rs index 2448da6..f7f26a2 100644 --- a/relayer/src/api.rs +++ b/relayer/src/api.rs @@ -3,4 +3,36 @@ //! Filecoin APIs -// use crate::types::Block; +use crate::{Client, Result}; +use async_trait::async_trait; +use serde::de::DeserializeOwned; + +/// Abstract filecoin api requests +#[async_trait] +pub trait Req { + /// Request method with params + async fn req(&self, client: &Client, params: &[&str]) -> Result; +} + +#[async_trait] +impl Req for &'static str { + /// request this path with response + async fn req(&self, client: &Client, params: &[&str]) -> Result { + Ok(client + .inner + .post(&client.base) + .form(&[ + ("id", "0"), + ("jsonrpc", "2.0"), + ("method", self), + ("params", &format!("{:?}", params)), + ]) + .send() + .await? + .json::() + .await?) + } +} + +/// Method `Filecoin.ChainGetBlock` +pub const CHAIN_GET_BLOCK: &str = "Filecoin.ChainGetBlock"; diff --git a/relayer/src/client.rs b/relayer/src/client.rs index eac0471..bc14228 100644 --- a/relayer/src/client.rs +++ b/relayer/src/client.rs @@ -3,7 +3,37 @@ //! RPC client for requesting data from filecoin RPC +use crate::{ + api::{Req, CHAIN_GET_BLOCK}, + types::Block, + Env, Result, +}; use reqwest::Client as ReqwestClinet; /// RPC Client of filecoindot relayers -pub struct Client(ReqwestClinet); +pub struct Client { + /// base url of rpc endpoint + pub base: String, + /// inner rpc client + pub inner: ReqwestClinet, +} + +impl Client { + /// New client with rpc endpoint + /// + /// If passing None, will try to get rpc endpoint + /// from environment variables. + pub fn new(rpc: Option) -> Result { + Ok(Self { + base: rpc.unwrap_or(Env::rpc()?), + inner: ReqwestClinet::new(), + }) + } + + /// "Filecoin.ChainGetBlock" + /// + /// Get `Block` by block number + pub async fn block(&self, number: usize) -> Result { + CHAIN_GET_BLOCK.req(self, &[&number.to_string()]).await + } +} diff --git a/relayer/src/result.rs b/relayer/src/result.rs index 6ff9ab9..883ca2a 100644 --- a/relayer/src/result.rs +++ b/relayer/src/result.rs @@ -7,6 +7,8 @@ use thiserror::Error as ThisError; pub enum Error { #[error("could not find FILECOINT_RPC in environment variables")] NoRPCEndpoint, + #[error("rpc request failed")] + RequestFailed(#[from] reqwest::Error), } pub type Result = std::result::Result; diff --git a/relayer/src/types.rs b/relayer/src/types.rs index 8e291f7..c71b399 100644 --- a/relayer/src/types.rs +++ b/relayer/src/types.rs @@ -2,7 +2,7 @@ // SPDX-License-Identifier: LGPL-3.0-only //! Filecoin api types - +#![allow(missing_docs)] use serde::{Deserialize, Serialize}; /// Response of the [`ChainHead`](https://docs.filecoin.io/reference/lotus-api/#chainhead) RPC call From 85fedadb11cb6790940df2d0925285e1bc8f8a6d Mon Sep 17 00:00:00 2001 From: clearloop Date: Thu, 14 Oct 2021 17:09:50 +0800 Subject: [PATCH 05/13] feat(relayer): check only support http endpoints --- relayer/src/env.rs | 7 ++++++- relayer/src/result.rs | 2 ++ 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/relayer/src/env.rs b/relayer/src/env.rs index 93f0c78..12e2295 100644 --- a/relayer/src/env.rs +++ b/relayer/src/env.rs @@ -14,6 +14,11 @@ pub struct Env; impl Env { /// Get environment variable `FILECOIN_RPC` pub fn rpc() -> Result { - var(FILECOIN_RPC_ENV).map_err(|_| Error::NoRPCEndpoint) + let r = var(FILECOIN_RPC_ENV).map_err(|_| Error::NoRPCEndpoint)?; + if !r.starts_with("http") { + Err(Error::NotHttpEndpoint) + } else { + r + } } } diff --git a/relayer/src/result.rs b/relayer/src/result.rs index 883ca2a..313bced 100644 --- a/relayer/src/result.rs +++ b/relayer/src/result.rs @@ -7,6 +7,8 @@ use thiserror::Error as ThisError; pub enum Error { #[error("could not find FILECOINT_RPC in environment variables")] NoRPCEndpoint, + #[error("Only supports http endpoints for now")] + NotHttpEndpoint, #[error("rpc request failed")] RequestFailed(#[from] reqwest::Error), } From 0f449b7d94325144db51ad1d270fe9f37d23e6a5 Mon Sep 17 00:00:00 2001 From: clearloop Date: Thu, 14 Oct 2021 18:01:18 +0800 Subject: [PATCH 06/13] feat(relayer): adds inline tests in docs for relayer api --- Cargo.lock | 46 ++++++++++++++++++++++++++++++++++++++++++ relayer/Cargo.toml | 3 +++ relayer/src/client.rs | 7 +++++++ relayer/src/env.rs | 2 +- relayer/src/lib.rs | 2 ++ relayer/src/testing.rs | 2 ++ 6 files changed, 61 insertions(+), 1 deletion(-) create mode 100644 relayer/src/testing.rs diff --git a/Cargo.lock b/Cargo.lock index ed8d00c..247209e 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -98,6 +98,27 @@ version = "0.7.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "be4dc07131ffa69b8072d35f5007352af944213cde02545e2103680baed38fcd" +[[package]] +name = "async-stream" +version = "0.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "171374e7e3b2504e0e5236e3b59260560f9fe94bfe9ac39ba5e4e929c5590625" +dependencies = [ + "async-stream-impl", + "futures-core", +] + +[[package]] +name = "async-stream-impl" +version = "0.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "648ed8c8d2ce5409ccd57453d9d1b214b342a0d69376a6feda1fd6cae3299308" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + [[package]] name = "async-trait" version = "0.1.51" @@ -1974,6 +1995,7 @@ dependencies = [ "rocksdb", "serde", "thiserror", + "tokio-test", ] [[package]] @@ -2830,6 +2852,30 @@ dependencies = [ "tokio", ] +[[package]] +name = "tokio-stream" +version = "0.1.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7b2f3f698253f03119ac0102beaa64f67a67e08074d03a22d18784104543727f" +dependencies = [ + "futures-core", + "pin-project-lite", + "tokio", +] + +[[package]] +name = "tokio-test" +version = "0.4.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "53474327ae5e166530d17f2d956afcb4f8a004de581b3cae10f12006bc8163e3" +dependencies = [ + "async-stream", + "bytes", + "futures-core", + "tokio", + "tokio-stream", +] + [[package]] name = "tokio-util" version = "0.6.8" diff --git a/relayer/Cargo.toml b/relayer/Cargo.toml index eb57316..2bdfaba 100644 --- a/relayer/Cargo.toml +++ b/relayer/Cargo.toml @@ -9,3 +9,6 @@ reqwest = { version = "^0.11", features = [ "json" ] } rocksdb = "0.17.0" serde = { version = "^1", features = [ "derive" ] } thiserror = "^1" + +[dev-dependencies] +tokio-test = "0.4.2" diff --git a/relayer/src/client.rs b/relayer/src/client.rs index bc14228..07261e3 100644 --- a/relayer/src/client.rs +++ b/relayer/src/client.rs @@ -33,6 +33,13 @@ impl Client { /// "Filecoin.ChainGetBlock" /// /// Get `Block` by block number + /// + // /// ``` + // /// use relayer::Client; + // /// + // /// let client = Client::new(None).unwrap(); + // /// println!("{:?}", tokio_test::block_on(client.block(42))); + // /// ``` pub async fn block(&self, number: usize) -> Result { CHAIN_GET_BLOCK.req(self, &[&number.to_string()]).await } diff --git a/relayer/src/env.rs b/relayer/src/env.rs index 12e2295..7ddee1a 100644 --- a/relayer/src/env.rs +++ b/relayer/src/env.rs @@ -18,7 +18,7 @@ impl Env { if !r.starts_with("http") { Err(Error::NotHttpEndpoint) } else { - r + Ok(r) } } } diff --git a/relayer/src/lib.rs b/relayer/src/lib.rs index 39436db..3b55e9b 100644 --- a/relayer/src/lib.rs +++ b/relayer/src/lib.rs @@ -6,6 +6,8 @@ mod client; mod db; mod env; mod result; +#[cfg(test)] +mod testing; pub mod types; pub use crate::{ diff --git a/relayer/src/testing.rs b/relayer/src/testing.rs new file mode 100644 index 0000000..b309007 --- /dev/null +++ b/relayer/src/testing.rs @@ -0,0 +1,2 @@ +// Copyright 2021 ChainSafe Systems +// SPDX-License-Identifier: LGPL-3.0-only From 74aa951c60a9c0d2928492372eea10cc9f9b4b63 Mon Sep 17 00:00:00 2001 From: clearloop Date: Fri, 15 Oct 2021 11:34:56 +0800 Subject: [PATCH 07/13] feat(client): enable tests with endpoints --- relayer/src/client.rs | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/relayer/src/client.rs b/relayer/src/client.rs index 07261e3..f8f12a5 100644 --- a/relayer/src/client.rs +++ b/relayer/src/client.rs @@ -34,12 +34,12 @@ impl Client { /// /// Get `Block` by block number /// - // /// ``` - // /// use relayer::Client; - // /// - // /// let client = Client::new(None).unwrap(); - // /// println!("{:?}", tokio_test::block_on(client.block(42))); - // /// ``` + /// ``` + /// use relayer::Client; + /// + /// let client = Client::new(None).unwrap(); + /// tokio_test::block_on(client.block(42)); + /// ``` pub async fn block(&self, number: usize) -> Result { CHAIN_GET_BLOCK.req(self, &[&number.to_string()]).await } From f2e41215d441891b65d2e22ee3e448f97c456550 Mon Sep 17 00:00:00 2001 From: clearloop Date: Fri, 15 Oct 2021 12:29:42 +0800 Subject: [PATCH 08/13] ci(cargo): test codebase with --all-features --- .github/workflows/tests.yml | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index ba9df01..f653f23 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -29,9 +29,9 @@ jobs: profile: minimal toolchain: nightly override: true - - uses: actions-rs/cargo@v1 - with: - command: test + - env: + FILECOIN_RPC: ${{ secrets.FILECOIN_RPC }} + run: cargo t --all-features fmt: name: Rustfmt @@ -63,4 +63,4 @@ jobs: - uses: actions-rs/cargo@v1 with: command: clippy - args: -- -D warnings \ No newline at end of file + args: -- -D warnings From a0b6c205b4615d0d07546da6bc1f85f828dd740d Mon Sep 17 00:00:00 2001 From: clearloop Date: Mon, 18 Oct 2021 12:54:20 +0800 Subject: [PATCH 09/13] feat(relayer): ChainGetTipSetByHeight with doc tests --- relayer/src/api.rs | 38 ---- relayer/src/api/get_tip_set_by_height.rs | 33 +++ relayer/src/api/mod.rs | 75 +++++++ relayer/src/client.rs | 30 ++- relayer/src/lib.rs | 3 +- relayer/src/testing.rs | 250 +++++++++++++++++++++++ relayer/src/types.rs | 20 +- 7 files changed, 390 insertions(+), 59 deletions(-) delete mode 100644 relayer/src/api.rs create mode 100644 relayer/src/api/get_tip_set_by_height.rs create mode 100644 relayer/src/api/mod.rs diff --git a/relayer/src/api.rs b/relayer/src/api.rs deleted file mode 100644 index f7f26a2..0000000 --- a/relayer/src/api.rs +++ /dev/null @@ -1,38 +0,0 @@ -// Copyright 2021 ChainSafe Systems -// SPDX-License-Identifier: LGPL-3.0-only - -//! Filecoin APIs - -use crate::{Client, Result}; -use async_trait::async_trait; -use serde::de::DeserializeOwned; - -/// Abstract filecoin api requests -#[async_trait] -pub trait Req { - /// Request method with params - async fn req(&self, client: &Client, params: &[&str]) -> Result; -} - -#[async_trait] -impl Req for &'static str { - /// request this path with response - async fn req(&self, client: &Client, params: &[&str]) -> Result { - Ok(client - .inner - .post(&client.base) - .form(&[ - ("id", "0"), - ("jsonrpc", "2.0"), - ("method", self), - ("params", &format!("{:?}", params)), - ]) - .send() - .await? - .json::() - .await?) - } -} - -/// Method `Filecoin.ChainGetBlock` -pub const CHAIN_GET_BLOCK: &str = "Filecoin.ChainGetBlock"; diff --git a/relayer/src/api/get_tip_set_by_height.rs b/relayer/src/api/get_tip_set_by_height.rs new file mode 100644 index 0000000..28f50b1 --- /dev/null +++ b/relayer/src/api/get_tip_set_by_height.rs @@ -0,0 +1,33 @@ +// Copyright 2021 ChainSafe Systems +// SPDX-License-Identifier: LGPL-3.0-only +use crate::{ + api::Api, + types::{Block, Cid}, +}; +use serde::{Deserialize, Serialize}; + +/// Method `Filecoin.ChainGetTipSetByHeight` +pub const CHAIN_GET_TIP_SET_BY_HEIGHT: &str = "Filecoin.ChainGetTipSetByHeight"; + +/// Response of `Filecoin.ChainGetTipSetByHeight` +#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq)] +pub struct ChainGetTipSetByHeightResult { + /// TipSet Cids + #[serde(rename = "Cids")] + pub cids: Vec, + /// TipSet Blocks + #[serde(rename = "Blocks")] + pub blocks: Vec, + /// TipSet Height + #[serde(rename = "Height")] + pub height: usize, +} + +/// `Filecoin.ChainGetTipSetByHeight` +pub struct ChainGetTipSetByHeight; + +impl Api for ChainGetTipSetByHeight { + const METHOD: &'static str = CHAIN_GET_TIP_SET_BY_HEIGHT; + type Params = Vec>; + type Result = ChainGetTipSetByHeightResult; +} diff --git a/relayer/src/api/mod.rs b/relayer/src/api/mod.rs new file mode 100644 index 0000000..ae2e1c0 --- /dev/null +++ b/relayer/src/api/mod.rs @@ -0,0 +1,75 @@ +// Copyright 2021 ChainSafe Systems +// SPDX-License-Identifier: LGPL-3.0-only + +//! Filecoin APIs + +mod get_tip_set_by_height; + +pub use self::get_tip_set_by_height::{ChainGetTipSetByHeight, ChainGetTipSetByHeightResult}; +use crate::{Client, Result}; +use async_trait::async_trait; +use serde::{de::DeserializeOwned, Deserialize, Serialize}; + +/// Wrapper for jsonrpc result +#[derive(Clone, Debug, Serialize, Deserialize)] +pub struct Resp { + /// reponse id + pub id: usize, + /// JsonRPC version + pub jsonrpc: String, + /// JsonRPC result + pub result: T, +} + +/// Request JSON body +#[derive(Clone, Debug, Serialize, Deserialize)] +pub struct Req<'r, T> { + /// reponse id + pub id: usize, + /// JsonRPC method + pub method: &'r str, + /// JsonRPC version + pub jsonrpc: &'r str, + /// JsonRPC result + pub params: T, +} + +/// Abstract filecoin api requests +#[async_trait] +pub trait Api { + const METHOD: &'static str; + type Params: Serialize + Send + Sync; + type Result: DeserializeOwned; + + /// Request method with params + async fn req(&self, client: &Client, params: Self::Params) -> Result { + // let context = client + // .inner + // .post(&client.base) + // .json(&Req { + // id: 0, + // method: Self::METHOD, + // jsonrpc: "2.0", + // params, + // }) + // .send() + // .await?; + // + // panic!("{}", context.text().await?); + // Err(Error::NoRPCEndpoint) + Ok(client + .inner + .post(&client.base) + .json(&Req { + id: 0, + method: Self::METHOD, + jsonrpc: "2.0", + params, + }) + .send() + .await? + .json::>() + .await? + .result) + } +} diff --git a/relayer/src/client.rs b/relayer/src/client.rs index f8f12a5..64aa16a 100644 --- a/relayer/src/client.rs +++ b/relayer/src/client.rs @@ -4,8 +4,7 @@ //! RPC client for requesting data from filecoin RPC use crate::{ - api::{Req, CHAIN_GET_BLOCK}, - types::Block, + api::{Api, ChainGetTipSetByHeight, ChainGetTipSetByHeightResult}, Env, Result, }; use reqwest::Client as ReqwestClinet; @@ -30,17 +29,30 @@ impl Client { }) } - /// "Filecoin.ChainGetBlock" + /// `Filecoin.ChainGetTipSetByHeight` /// - /// Get `Block` by block number + /// Get tipset by block height /// /// ``` - /// use relayer::Client; + /// # use relayer::Result; + /// use relayer::{Client}; /// - /// let client = Client::new(None).unwrap(); - /// tokio_test::block_on(client.block(42)); + /// # fn main() -> Result<()> { + /// assert_eq!( + /// tokio_test::block_on( + /// Client::new(None)? + /// .chain_get_tip_set_by_height(1199840), + /// )?, + /// relayer::testing::get_tip_set_by_height_1199840(), + /// ); + /// + /// # Ok(()) + /// } /// ``` - pub async fn block(&self, number: usize) -> Result { - CHAIN_GET_BLOCK.req(self, &[&number.to_string()]).await + pub async fn chain_get_tip_set_by_height( + &self, + number: usize, + ) -> Result { + Api::req(&ChainGetTipSetByHeight, &self, vec![Some(number), None]).await } } diff --git a/relayer/src/lib.rs b/relayer/src/lib.rs index 3b55e9b..bf22c3a 100644 --- a/relayer/src/lib.rs +++ b/relayer/src/lib.rs @@ -6,8 +6,7 @@ mod client; mod db; mod env; mod result; -#[cfg(test)] -mod testing; +pub mod testing; pub mod types; pub use crate::{ diff --git a/relayer/src/testing.rs b/relayer/src/testing.rs index b309007..7f9a243 100644 --- a/relayer/src/testing.rs +++ b/relayer/src/testing.rs @@ -1,2 +1,252 @@ // Copyright 2021 ChainSafe Systems // SPDX-License-Identifier: LGPL-3.0-only +use crate::{ + api::ChainGetTipSetByHeightResult, + types::{BeaconEntry, Block, BlsAggregate, Cid, ElectionProof, WinPoStProof}, +}; + +pub fn get_tip_set_by_height_1199840() -> ChainGetTipSetByHeightResult { + ChainGetTipSetByHeightResult { + cids: vec![ + Cid { + empty: "bafy2bzacedbaliyx3k64d4rxy5q2og3wf5r5e2ra6bvf52ogldc6oad3jukbe".to_string(), + }, + Cid { + empty: "bafy2bzaceblrey44c6ekyu7iu6dni4inrjgnyp7sgjrrgg3xnd3poxjrt2v2i".to_string(), + }, + Cid { + empty: "bafy2bzacedd2wb4ijvvowm2gq3izffhl2oqlogigfubizozgbgo5l7rk73ick".to_string(), + }, + Cid { + empty: "bafy2bzacecav4sjwonnjryjb5kmrint45yenyhorzn2it5noxdqhnudsquyoo".to_string(), + }, + ], + blocks: vec![ + Block { + miner: "f0733242".to_string(), + ticket: ElectionProof { + vrf_proof: "hgNW658F5ud+QBZ6ED7v5gT7I8aVhdKes8fSRJrJaRNdxdUp4gz+3dk7f13BA+WTDxPLozhZtlk97cnW/NO7szY0zM4DxVwBDOHWLjx7OYzzfYXj56icybIKI+PF1S1t".to_string(), + }, + election_proof: ElectionProof { + vrf_proof: "qanIItIDpw16T+dWXIyPcB94/TJD36zpTwQAAXrhfM3SqYtswfJ8KjTZgWlU7GSQAtxK44AuSZvg1GeTh0EdhL9x0o3RueUFucNPoggirwLyVJfB6jq/EN5uLyHEdDjY".to_string(), + }, + beacon_entries: vec![ + BeaconEntry { + round: 1295684, + data: "pCNFJKhlTrL+7Gp0YtqUjOaWP0YX3J76euYOZZqkwYYynvVC3VJVSCXW0MLm+XO5GdesTAjsUoxdMy6myma5LWeXqggCU6bMxwtS5Kz3+v0+UjvsN1zbJWwG/imJAJp5".to_string(), + }, + ], + win_po_st_proof: vec![ + WinPoStProof { + po_st_proof: 4, + proof_bytes: "qYV9RCTinHmlrEh2T0vEnwfwCJJWD+T4ZmoE10s7FjMlkqwnyfMbbAvh/cKG5s90ijY1AyoPW1C5VeZBzzECFnUuY7oN/gE6zSc6+RuheFi5VXTAHRx5SouAI9qlocQgFvQaxMlDDtOs1CtHh3vHo5v5iXf2SX9ZjHO5pz+ZXb+eV0pbNH/Neo/xK5FPH1EYlNO64rvECMGnQvaR1brWqXTO3Ce2QjXGp8W8esm/h6Q0IM2gyn4G8SkSyGpqOarl".to_string(), + }, + ], + parents: vec![ + Cid { + empty: "bafy2bzaceaue3sd5qijwafs7ycyhpcdzb4kpribce7zx2ervpjsqutdvl7vxq".to_string(), + }, + Cid { + empty: "bafy2bzaceabf3mewrf6xha3als5misrzb6erhkeenphfbl2ylodfx4dzngh5u".to_string(), + }, + Cid { + empty: "bafy2bzacecw372lcniqqo3je4tjobgsbq6bsqhkv4rmmkstr46vlxqn63meem".to_string(), + }, + Cid { + empty: "bafy2bzacebfoleuxh5ebggsi272s5c7xff64kdslkfcoe5km6ere73fc42yns".to_string(), + }, + ], + parent_weight: "27397340404".to_string(), + height: 1199840, + parent_state_root: Cid { + empty: "bafy2bzacedldmgjc3pjvrhvsgg4tx736dmvhw2nuwekx2xolrbos4cgh6clza".to_string(), + }, + parent_message_receipts: Cid { + empty: "bafy2bzaceb75fgw6yg7ekoqhw5ai3nuodtnnuerhcbamnkbhhtzkago6vacjg".to_string(), + }, + messages: Cid { + empty: "bafy2bzacea4v4uwhlffeznk5sn2naw7zsl6tahsstc37ygongb3tppnqjosey".to_string(), + }, + bls_aggregate: BlsAggregate { + bls_aggregate_type: 2, + data: "jV3n9VBzIGlcpVuklDaQ3Z3aKkHXoq6bnEdH9IVDshU2jIjc3xu+NCKyQkFhPdOuGalL/ud9MNkm/D++6zf1sHerLPXJ0TDGi0J5cELIzyLOQfqFVbJhz1rvAaBxdija".to_string(), + }, + timestamp: 1634301600, + block_sig: BlsAggregate { + bls_aggregate_type: 2, + data: "hC8thFZuAB8fsHMHhEA1Y6/LeypZUuoH+r3RMfbyNvFkIeRaGFg7+Ks2Z65RSmUHACdGYEj49NNWUZcMVTBTwX823Grm+zwW5qiB/XKG2ZH9XCv6TfDy2fMbjKcnZoG3".to_string(), + }, + fork_signaling: 0, + }, + Block { + miner: "f01038199".to_string(), + ticket: ElectionProof { + vrf_proof: "hREYIhTXRlr8lSE8Sp5iC7EMRLULop+8+vbAKb99RqEsTZjfgTenBWvYb8IjRc1ZFoF/Gxvr6LTDVRa5skSeWa7LILZ1kf4sWWfa8l0irLveVSgn6QB/PSSQ32D5HQpC".to_string(), + }, + election_proof: ElectionProof { + vrf_proof: "rsxBCMqKoRiSlV//tyvSKOAOnAT3MO6HPXcJe2HVOepsEQ+VVJpSmw6o8MpaVq/tCubf6zvZ2eMB/0JkMPrA30FSco0QDI70IeRr6QE9EELBhBLDzhZGBcGm8me8e2Ad".to_string(), + }, + beacon_entries: vec![ + BeaconEntry { + round: 1295684, + data: "pCNFJKhlTrL+7Gp0YtqUjOaWP0YX3J76euYOZZqkwYYynvVC3VJVSCXW0MLm+XO5GdesTAjsUoxdMy6myma5LWeXqggCU6bMxwtS5Kz3+v0+UjvsN1zbJWwG/imJAJp5".to_string(), + }, + ], + win_po_st_proof: vec![ + WinPoStProof { + po_st_proof: 3, + proof_bytes: "jjDdF126FO5XUbk0SdXCEQdUTbrtOpDXyNpnBdqkB/1a0bkoHmeAKc8r4RXdRL6uiPHrGE5ImEBvR9v6PFkn/BvmCtLLcrH2dB3bu96gLEBklPFLNlCoWdMF80L3ZPEQBo+oRAIjnODJdTQL4wCtDPg8rWFGRXIkRdse1M03Som1mk+a8H0FWCQopqcTwqkBg7f9KRxKK4yPM/cVC1AehGIWVUdkjMktHynciU5S7ClqXoUadbxuQZFkrIfzW1j1".to_string(), + }, + ], + parents: vec![ + Cid { + empty: "bafy2bzaceaue3sd5qijwafs7ycyhpcdzb4kpribce7zx2ervpjsqutdvl7vxq".to_string(), + }, + Cid { + empty: "bafy2bzaceabf3mewrf6xha3als5misrzb6erhkeenphfbl2ylodfx4dzngh5u".to_string(), + }, + Cid { + empty: "bafy2bzacecw372lcniqqo3je4tjobgsbq6bsqhkv4rmmkstr46vlxqn63meem".to_string(), + }, + Cid { + empty: "bafy2bzacebfoleuxh5ebggsi272s5c7xff64kdslkfcoe5km6ere73fc42yns".to_string(), + }, + ], + parent_weight: "27397340404".to_string(), + height: 1199840, + parent_state_root: Cid { + empty: "bafy2bzacedldmgjc3pjvrhvsgg4tx736dmvhw2nuwekx2xolrbos4cgh6clza".to_string(), + }, + parent_message_receipts: Cid { + empty: "bafy2bzaceb75fgw6yg7ekoqhw5ai3nuodtnnuerhcbamnkbhhtzkago6vacjg".to_string(), + }, + messages: Cid { + empty: "bafy2bzacebyoxanjivzgsj3aisd5e5wrdai3oeqodxnkw2gbo2yctt33mbeeo".to_string(), + }, + bls_aggregate: BlsAggregate { + bls_aggregate_type: 2, + data: "phYYbetc5nFCn0I5Y3tNi9PX7Y5bvNtAHGnkdtOrPWSsHNxJqwsTpLW7rfAhGZrSA+WiGB52hG340A52gE05j2xucucMX4H4YDiqXTubSAfrzdzLrL8l4uBOE4zBmVxI".to_string(), + }, + timestamp: 1634301600, + block_sig: BlsAggregate { + bls_aggregate_type: 2, + data: "izTyZTkQRgtBx2TcbrjEjoX849Cxr8c65uBeoqqs5SQuEX6jyNKFR2GXN3hAku3kByOvJ6s+7kiEqHIKRSw6jaMWOA5YfwzBWNKxGtNHdfaSTtQtH44n6+UfTWypvRfY".to_string(), + }, + fork_signaling: 0, + }, + Block { + miner: "f0442370".to_string(), + ticket: ElectionProof { + vrf_proof: "uXYqEqAU8JuL39NLpklBh0gQCLbU4bj4ShPVcwxbZ8BXB0NxRBUQGy/Dhy+B17yYCfGOb9x66NC9d8gJYZfenx61Ktd1g0/gVg4qn3+8JGKe49sy9mQLyLQSUkHWJT0I".to_string(), + }, + election_proof: ElectionProof { + vrf_proof: "i7kHYkfDyQFcMTBftRsrwNnt4a8JBEykpiAr49HHOZpJrk/C2x5rxBI2+FsdH8tzARYLI9qWS+L77lKThahv64jFQSQIrsUlTXX49C+v9COK9w3eTKu22LK43ILxXws7".to_string(), + }, + beacon_entries: vec![ + BeaconEntry { + round: 1295684, + data: "pCNFJKhlTrL+7Gp0YtqUjOaWP0YX3J76euYOZZqkwYYynvVC3VJVSCXW0MLm+XO5GdesTAjsUoxdMy6myma5LWeXqggCU6bMxwtS5Kz3+v0+UjvsN1zbJWwG/imJAJp5".to_string(), + }, + ], + win_po_st_proof: vec![ + WinPoStProof { + po_st_proof: 4, + proof_bytes: "kl2OrN5GNG/zhfT+qqI3dIeSqvqu03mCSm1IUp8Xcg+eSmstj7I61a8Fe/sRL4gmtycK2J08inrYJJmRGXDZjrS03wEvTTnDquDttvDSiW8aJC1EkpqmqDxxZp9TgRdNC9D5+Afq5Je7GDA9YEoL7y4J/H8ZGMJNZhmj402dHw+gVaIwor4lBOvh4IhAPqC+piREOH+efoXusbMIvNjlV0deDfcG09xQQ/2J8Lksa7VYVbMu4cgV1DtyQFieclTp".to_string(), + }, + ], + parents: vec![ + Cid { + empty: "bafy2bzaceaue3sd5qijwafs7ycyhpcdzb4kpribce7zx2ervpjsqutdvl7vxq".to_string(), + }, + Cid { + empty: "bafy2bzaceabf3mewrf6xha3als5misrzb6erhkeenphfbl2ylodfx4dzngh5u".to_string(), + }, + Cid { + empty: "bafy2bzacecw372lcniqqo3je4tjobgsbq6bsqhkv4rmmkstr46vlxqn63meem".to_string(), + }, + Cid { + empty: "bafy2bzacebfoleuxh5ebggsi272s5c7xff64kdslkfcoe5km6ere73fc42yns".to_string(), + }, + ], + parent_weight: "27397340404".to_string(), + height: 1199840, + parent_state_root: Cid { + empty: "bafy2bzacedldmgjc3pjvrhvsgg4tx736dmvhw2nuwekx2xolrbos4cgh6clza".to_string(), + }, + parent_message_receipts: Cid { + empty: "bafy2bzaceb75fgw6yg7ekoqhw5ai3nuodtnnuerhcbamnkbhhtzkago6vacjg".to_string(), + }, + messages: Cid { + empty: "bafy2bzacebizldd4vzjmjqp6gmmtixshxo7xlc4fl77drjmvhjg6zm5z6ojoc".to_string(), + }, + bls_aggregate: BlsAggregate { + bls_aggregate_type: 2, + data: "gnFyHlLZAwN2PTfiyNXQ0ZkA5LSl+nzmblojPyvIM/0zC5glLpHbX3tJaTbCmHlxGdsBS4J8GFcNru7s6paawg+6An18UImd/+KA9do0URrAaSMMH9VcIneaVKHOPclr".to_string(), + }, + timestamp: 1634301600, + block_sig: BlsAggregate { + bls_aggregate_type: 2, + data: "jZhkpnlJlBWQIbMZEnfDnllcw+/K5ndCp2h4kSMU/m47sZvSF1ff7hfpeGEryp/OBWxQiD4rwDMP40/vT6xj3k5YQl4r0+sR1PhuKXG97QA3GaNmmwkSAW7wlK0k4hDV".to_string(), + }, + fork_signaling: 0, + }, + Block { + miner: "f0133501".to_string(), + ticket: ElectionProof { + vrf_proof: "rP1n2/a02PX4ObrZlrA20xf9z9jSN+DFfnziyIhLRsotSSbR2SaNLsWjE50jG2dpDqDWYcNVVvVEyh8r0w53fJCcMb6Zq95gyu8nePcjtxm+ZtN24UbOPnYwGnlslENp".to_string(), + }, + election_proof: ElectionProof { + vrf_proof: "ufVJNHy1K9J7A6zyGvO1SK81FOCIt0z6s9Ip5lil8AcpZd9mDeHuM5fUjptl69j9BJGPsi7w4JjfhQ12mPZiCLiHH+Pt76jPvu0f+6ZfcQ3ec2dYQjK9rvvySXJpHGYb".to_string(), + }, + beacon_entries: vec![ + BeaconEntry { + round: 1295684, + data: "pCNFJKhlTrL+7Gp0YtqUjOaWP0YX3J76euYOZZqkwYYynvVC3VJVSCXW0MLm+XO5GdesTAjsUoxdMy6myma5LWeXqggCU6bMxwtS5Kz3+v0+UjvsN1zbJWwG/imJAJp5".to_string(), + }, + ], + win_po_st_proof: vec![ + WinPoStProof { + po_st_proof: 4, + proof_bytes: "hShOJhfo/A3SfHXtG1j5CuC7q/8rDAYoVyb6r2a5Js3LDfLRfmDlO47kyFSsfhwvrEymOUfn/8J+ct8GKkQu2ZqpHQQUYXf00cIKitKiv8XXZ7u3eMRQYSEDLMXs8NKyC3kb4KvzDL/I7yFmPkkOyuiu6J3yVRWXb3kfSsNVM5EcZcUWhrfCm1OezMTYPChQuMS/rQmPU3BQLcs9uFsx4ue5SO5kf/w0oP5VzZgFs/f+jKEq5+tTMTP4OIAh+BMJ".to_string(), + }, + ], + parents: vec![ + Cid { + empty: "bafy2bzaceaue3sd5qijwafs7ycyhpcdzb4kpribce7zx2ervpjsqutdvl7vxq".to_string(), + }, + Cid { + empty: "bafy2bzaceabf3mewrf6xha3als5misrzb6erhkeenphfbl2ylodfx4dzngh5u".to_string(), + }, + Cid { + empty: "bafy2bzacecw372lcniqqo3je4tjobgsbq6bsqhkv4rmmkstr46vlxqn63meem".to_string(), + }, + Cid { + empty: "bafy2bzacebfoleuxh5ebggsi272s5c7xff64kdslkfcoe5km6ere73fc42yns".to_string(), + }, + ], + parent_weight: "27397340404".to_string(), + height: 1199840, + parent_state_root: Cid { + empty: "bafy2bzacedldmgjc3pjvrhvsgg4tx736dmvhw2nuwekx2xolrbos4cgh6clza".to_string(), + }, + parent_message_receipts: Cid { + empty: "bafy2bzaceb75fgw6yg7ekoqhw5ai3nuodtnnuerhcbamnkbhhtzkago6vacjg".to_string(), + }, + messages: Cid { + empty: "bafy2bzaceducpch7kljxpbsybi5uc3wljabh3zfbk2jvhpk56c2a4gnjbveoc".to_string(), + }, + bls_aggregate: BlsAggregate { + bls_aggregate_type: 2, + data: "sjROXiMW6wPu4T3vRsfeJAK8ybd0pVgJ5IyM/bd9B3C+x9K46WIzT+fMjRRsRxKtEb7tL/myXgcco8+hutNru/h66wdC3ohJMKqjDMbArIp4olrabG8pjbTH8yNBwHm8".to_string(), + }, + timestamp: 1634301600, + block_sig: BlsAggregate { + bls_aggregate_type: 2, + data: "h+tWWo3z2LDfrohvbC+5WQdqcWAcaA67P4JApx7G2Dl7C2e+WlV1q6BRo67GBU6BCmpEufcj5yfc9Tlewrpp2Ql2oKuDiforqDCMzDbX8qMTzql2SR1KLP3DQO7zGmsq".to_string(), + }, + fork_signaling: 0, + }, + ], + height: 1199840, + } +} diff --git a/relayer/src/types.rs b/relayer/src/types.rs index c71b399..c7e196d 100644 --- a/relayer/src/types.rs +++ b/relayer/src/types.rs @@ -6,14 +6,14 @@ use serde::{Deserialize, Serialize}; /// Response of the [`ChainHead`](https://docs.filecoin.io/reference/lotus-api/#chainhead) RPC call -#[derive(Clone, Debug, Serialize, Deserialize)] +#[derive(Clone, Debug, Serialize, Deserialize, Eq, PartialEq)] pub struct GetChainHead { pub jsonrpc: String, pub result: TipSet, pub id: i64, } -#[derive(Clone, Debug, Serialize, Deserialize)] +#[derive(Clone, Debug, Serialize, Deserialize, Eq, PartialEq)] pub struct TipSet { #[serde(rename = "Cids")] pub cids: Vec, @@ -23,7 +23,7 @@ pub struct TipSet { pub height: i64, } -#[derive(Clone, Debug, Serialize, Deserialize)] +#[derive(Clone, Debug, Serialize, Deserialize, Eq, PartialEq)] pub struct Block { #[serde(rename = "Miner")] pub miner: String, @@ -57,7 +57,7 @@ pub struct Block { pub fork_signaling: i64, } -#[derive(Clone, Debug, Serialize, Deserialize)] +#[derive(Clone, Debug, Serialize, Deserialize, Eq, PartialEq)] pub struct BeaconEntry { #[serde(rename = "Round")] pub round: i64, @@ -65,7 +65,7 @@ pub struct BeaconEntry { pub data: String, } -#[derive(Clone, Debug, Serialize, Deserialize)] +#[derive(Clone, Debug, Serialize, Deserialize, Eq, PartialEq)] pub struct BlsAggregate { #[serde(rename = "Type")] pub bls_aggregate_type: i64, @@ -73,22 +73,22 @@ pub struct BlsAggregate { pub data: String, } -#[derive(Clone, Debug, Serialize, Deserialize)] +#[derive(Clone, Debug, Serialize, Deserialize, Eq, PartialEq)] pub struct ElectionProof { #[serde(rename = "VRFProof")] pub vrf_proof: String, } -#[derive(Clone, Debug, Serialize, Deserialize)] +#[derive(Clone, Debug, Serialize, Deserialize, Eq, PartialEq)] pub struct Cid { #[serde(rename = "/")] pub empty: String, } -#[derive(Clone, Debug, Serialize, Deserialize)] +#[derive(Clone, Debug, Serialize, Deserialize, Eq, PartialEq)] pub struct WinPoStProof { - #[serde(rename = "RegisteredProof")] - pub registered_proof: i64, + #[serde(rename = "PoStProof")] + pub po_st_proof: i64, #[serde(rename = "ProofBytes")] pub proof_bytes: String, } From 417d52074b6b4f2b8ffdad43f22ce8e8bf82aca0 Mon Sep 17 00:00:00 2001 From: clearloop Date: Mon, 18 Oct 2021 13:03:24 +0800 Subject: [PATCH 10/13] feat(relayer): clean unnecessary annotations --- relayer/src/api/mod.rs | 14 -------------- 1 file changed, 14 deletions(-) diff --git a/relayer/src/api/mod.rs b/relayer/src/api/mod.rs index ae2e1c0..fbd8f16 100644 --- a/relayer/src/api/mod.rs +++ b/relayer/src/api/mod.rs @@ -43,20 +43,6 @@ pub trait Api { /// Request method with params async fn req(&self, client: &Client, params: Self::Params) -> Result { - // let context = client - // .inner - // .post(&client.base) - // .json(&Req { - // id: 0, - // method: Self::METHOD, - // jsonrpc: "2.0", - // params, - // }) - // .send() - // .await?; - // - // panic!("{}", context.text().await?); - // Err(Error::NoRPCEndpoint) Ok(client .inner .post(&client.base) From 505b4a5329d186ed567e1d9fab040108b7cd46b4 Mon Sep 17 00:00:00 2001 From: clearloop Date: Mon, 18 Oct 2021 14:47:49 +0800 Subject: [PATCH 11/13] feat(relayer): make clippy happy --- relayer/src/{db.rs => cache.rs} | 0 relayer/src/client.rs | 2 +- relayer/src/lib.rs | 2 +- 3 files changed, 2 insertions(+), 2 deletions(-) rename relayer/src/{db.rs => cache.rs} (100%) diff --git a/relayer/src/db.rs b/relayer/src/cache.rs similarity index 100% rename from relayer/src/db.rs rename to relayer/src/cache.rs diff --git a/relayer/src/client.rs b/relayer/src/client.rs index 64aa16a..97d96fc 100644 --- a/relayer/src/client.rs +++ b/relayer/src/client.rs @@ -53,6 +53,6 @@ impl Client { &self, number: usize, ) -> Result { - Api::req(&ChainGetTipSetByHeight, &self, vec![Some(number), None]).await + Api::req(&ChainGetTipSetByHeight, self, vec![Some(number), None]).await } } diff --git a/relayer/src/lib.rs b/relayer/src/lib.rs index bf22c3a..25ddae0 100644 --- a/relayer/src/lib.rs +++ b/relayer/src/lib.rs @@ -2,8 +2,8 @@ // SPDX-License-Identifier: LGPL-3.0-only mod api; +mod cache; mod client; -mod db; mod env; mod result; pub mod testing; From e43abeb2236e414ec998d6bcc25b66dd6116010e Mon Sep 17 00:00:00 2001 From: clearloop Date: Mon, 18 Oct 2021 16:12:57 +0800 Subject: [PATCH 12/13] feat(relayer): adds cache --- .github/workflows/tests.yml | 1 + Cargo.lock | 41 ++++++++++++++++++++++++++++++++ Cargo.toml | 2 +- relayer/Cargo.toml | 4 +++- relayer/src/api/mod.rs | 47 ++++++++++++++++++++++++------------- relayer/src/cache.rs | 36 +++++++++++++++++++++++++++- relayer/src/client.rs | 6 ++++- relayer/src/result.rs | 6 +++++ relayer/src/traits.rs | 4 ---- 9 files changed, 123 insertions(+), 24 deletions(-) delete mode 100644 relayer/src/traits.rs diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index f653f23..e8de775 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -64,3 +64,4 @@ jobs: with: command: clippy args: -- -D warnings + diff --git a/Cargo.lock b/Cargo.lock index 247209e..ab17c32 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -169,6 +169,15 @@ version = "0.13.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "904dfeac50f3cdaba28fc6f57fdcddb75f49ed61346676a78c4ffe55877802fd" +[[package]] +name = "bincode" +version = "1.3.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b1f45e9417d87227c7a56d22e471c6206462cba514c7590c09aff4cf6d1ddcad" +dependencies = [ + "serde", +] + [[package]] name = "bindgen" version = "0.59.1" @@ -470,6 +479,26 @@ dependencies = [ "generic-array 0.14.4", ] +[[package]] +name = "dirs" +version = "4.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ca3aa72a6f96ea37bbc5aa912f6788242832f75369bdfdadcb0e38423f100059" +dependencies = [ + "dirs-sys", +] + +[[package]] +name = "dirs-sys" +version = "0.3.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "03d86534ed367a67548dc68113a0f5db55432fdfbb6e6f9d77704397d95d5780" +dependencies = [ + "libc", + "redox_users", + "winapi", +] + [[package]] name = "downcast-rs" version = "1.2.0" @@ -1940,6 +1969,16 @@ dependencies = [ "bitflags", ] +[[package]] +name = "redox_users" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "528532f3d801c87aec9def2add9ca802fe569e44a544afe633765267840abe64" +dependencies = [ + "getrandom 0.2.3", + "redox_syscall 0.2.10", +] + [[package]] name = "ref-cast" version = "1.0.6" @@ -1991,6 +2030,8 @@ name = "relayer" version = "0.1.0" dependencies = [ "async-trait", + "bincode", + "dirs", "reqwest", "rocksdb", "serde", diff --git a/Cargo.toml b/Cargo.toml index f68a440..dca9092 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -2,4 +2,4 @@ members = [ "relayer", "filecoindot" -] \ No newline at end of file +] diff --git a/relayer/Cargo.toml b/relayer/Cargo.toml index 2bdfaba..73f2af4 100644 --- a/relayer/Cargo.toml +++ b/relayer/Cargo.toml @@ -4,7 +4,9 @@ version = "0.1.0" edition = "2018" [dependencies] -async-trait = "0.1.51" +async-trait = "^0.1" +bincode = "^1" +dirs = "4.0.0" reqwest = { version = "^0.11", features = [ "json" ] } rocksdb = "0.17.0" serde = { version = "^1", features = [ "derive" ] } diff --git a/relayer/src/api/mod.rs b/relayer/src/api/mod.rs index fbd8f16..0006aba 100644 --- a/relayer/src/api/mod.rs +++ b/relayer/src/api/mod.rs @@ -36,26 +36,41 @@ pub struct Req<'r, T> { /// Abstract filecoin api requests #[async_trait] -pub trait Api { +pub trait Api: Sized { const METHOD: &'static str; type Params: Serialize + Send + Sync; - type Result: DeserializeOwned; + type Result: DeserializeOwned + Serialize; + + /// Storage key in bytes + fn storage_key(params: &Self::Params) -> Result> { + let mut key = bincode::serialize(Self::METHOD)?; + key.append(&mut bincode::serialize(params)?); + Ok(key) + } /// Request method with params async fn req(&self, client: &Client, params: Self::Params) -> Result { - Ok(client - .inner - .post(&client.base) - .json(&Req { - id: 0, - method: Self::METHOD, - jsonrpc: "2.0", - params, - }) - .send() - .await? - .json::>() - .await? - .result) + Ok(if let Ok(Some(res)) = client.cache.get::(¶ms) { + bincode::deserialize(&res)? + } else { + let res = client + .inner + .post(&client.base) + .json(&Req { + id: 0, + method: Self::METHOD, + jsonrpc: "2.0", + params: ¶ms, + }) + .send() + .await? + .json::>() + .await? + .result; + client + .cache + .put::(¶ms, &bincode::serialize(&res)?)?; + res + }) } } diff --git a/relayer/src/cache.rs b/relayer/src/cache.rs index 19e43a4..11ef852 100644 --- a/relayer/src/cache.rs +++ b/relayer/src/cache.rs @@ -1,4 +1,38 @@ // Copyright 2021 ChainSafe Systems // SPDX-License-Identifier: LGPL-3.0-only -// local storage for handling cache +//! local storage for handling cache +use crate::{api::Api, Error, Result}; +use rocksdb::DB; + +const FILECOINDOT_CACHE_FILE: &str = "filecoindot_cache"; + +/// api requests cache +pub struct Cache(DB); + +impl Cache { + /// New cache with data dir + /// + /// | OS | dir | + /// | ------- | ------------------------------------- | + /// | Linux | $XDG_DATA_HOME or $HOME/.local/share | + /// | macOS | $HOME/Library/Application Support | + /// | Windows | {FOLDERID_RoamingAppData} | + pub fn new() -> Result { + Ok(Self(DB::open_default( + dirs::data_dir() + .ok_or(Error::DirectoryNotFound)? + .join(FILECOINDOT_CACHE_FILE), + )?)) + } + + /// Get api cache from cache + pub fn get(&self, params: &A::Params) -> Result>> { + Ok(self.0.get(::storage_key(params)?)?) + } + + /// Put api cache to cache + pub fn put(&self, params: &A::Params, value: &[u8]) -> Result<()> { + Ok(self.0.put(::storage_key(params)?, value)?) + } +} diff --git a/relayer/src/client.rs b/relayer/src/client.rs index 97d96fc..cc5c13c 100644 --- a/relayer/src/client.rs +++ b/relayer/src/client.rs @@ -5,6 +5,7 @@ use crate::{ api::{Api, ChainGetTipSetByHeight, ChainGetTipSetByHeightResult}, + cache::Cache, Env, Result, }; use reqwest::Client as ReqwestClinet; @@ -13,6 +14,8 @@ use reqwest::Client as ReqwestClinet; pub struct Client { /// base url of rpc endpoint pub base: String, + /// api cache + pub cache: Cache, /// inner rpc client pub inner: ReqwestClinet, } @@ -25,6 +28,7 @@ impl Client { pub fn new(rpc: Option) -> Result { Ok(Self { base: rpc.unwrap_or(Env::rpc()?), + cache: Cache::new()?, inner: ReqwestClinet::new(), }) } @@ -53,6 +57,6 @@ impl Client { &self, number: usize, ) -> Result { - Api::req(&ChainGetTipSetByHeight, self, vec![Some(number), None]).await + Ok(Api::req(&ChainGetTipSetByHeight, self, vec![Some(number), None]).await?) } } diff --git a/relayer/src/result.rs b/relayer/src/result.rs index 313bced..e72d06c 100644 --- a/relayer/src/result.rs +++ b/relayer/src/result.rs @@ -11,6 +11,12 @@ pub enum Error { NotHttpEndpoint, #[error("rpc request failed")] RequestFailed(#[from] reqwest::Error), + #[error("Serialize storage key failed")] + BuildStorageKeyFailed(#[from] bincode::Error), + #[error("Directory not found")] + DirectoryNotFound, + #[error("Rocksdb error")] + RocksDB(#[from] rocksdb::Error), } pub type Result = std::result::Result; diff --git a/relayer/src/traits.rs b/relayer/src/traits.rs deleted file mode 100644 index 4877376..0000000 --- a/relayer/src/traits.rs +++ /dev/null @@ -1,4 +0,0 @@ -// Copyright 2021 ChainSafe Systems -// SPDX-License-Identifier: LGPL-3.0-only - -//! Custom tratis From c5e8d70c4faf1bd3ccee79daa72e334a8fdf4dfb Mon Sep 17 00:00:00 2001 From: clearloop Date: Mon, 18 Oct 2021 17:06:45 +0800 Subject: [PATCH 13/13] feat(relayer): go with filecoindot_relayer --- Cargo.lock | 28 +++++++++---------- Cargo.toml | 4 +-- {relayer => filecoindot_relayer}/Cargo.toml | 2 +- .../src/api/get_tip_set_by_height.rs | 0 .../src/api/mod.rs | 0 {relayer => filecoindot_relayer}/src/cache.rs | 0 .../src/client.rs | 6 ++-- {relayer => filecoindot_relayer}/src/env.rs | 0 {relayer => filecoindot_relayer}/src/lib.rs | 0 .../src/result.rs | 0 .../src/testing.rs | 0 {relayer => filecoindot_relayer}/src/types.rs | 0 12 files changed, 20 insertions(+), 20 deletions(-) rename {relayer => filecoindot_relayer}/Cargo.toml (91%) rename {relayer => filecoindot_relayer}/src/api/get_tip_set_by_height.rs (100%) rename {relayer => filecoindot_relayer}/src/api/mod.rs (100%) rename {relayer => filecoindot_relayer}/src/cache.rs (100%) rename {relayer => filecoindot_relayer}/src/client.rs (90%) rename {relayer => filecoindot_relayer}/src/env.rs (100%) rename {relayer => filecoindot_relayer}/src/lib.rs (100%) rename {relayer => filecoindot_relayer}/src/result.rs (100%) rename {relayer => filecoindot_relayer}/src/testing.rs (100%) rename {relayer => filecoindot_relayer}/src/types.rs (100%) diff --git a/Cargo.lock b/Cargo.lock index ab17c32..ae029fe 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -605,6 +605,20 @@ dependencies = [ "sp-runtime", ] +[[package]] +name = "filecoindot_relayer" +version = "0.1.0" +dependencies = [ + "async-trait", + "bincode", + "dirs", + "reqwest", + "rocksdb", + "serde", + "thiserror", + "tokio-test", +] + [[package]] name = "fixed-hash" version = "0.7.0" @@ -2025,20 +2039,6 @@ version = "0.6.25" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f497285884f3fcff424ffc933e56d7cbca511def0c9831a7f9b5f6153e3cc89b" -[[package]] -name = "relayer" -version = "0.1.0" -dependencies = [ - "async-trait", - "bincode", - "dirs", - "reqwest", - "rocksdb", - "serde", - "thiserror", - "tokio-test", -] - [[package]] name = "remove_dir_all" version = "0.5.3" diff --git a/Cargo.toml b/Cargo.toml index dca9092..f46a48a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,5 +1,5 @@ [workspace] members = [ - "relayer", - "filecoindot" + "filecoindot", + "filecoindot_relayer" ] diff --git a/relayer/Cargo.toml b/filecoindot_relayer/Cargo.toml similarity index 91% rename from relayer/Cargo.toml rename to filecoindot_relayer/Cargo.toml index 73f2af4..0eb5215 100644 --- a/relayer/Cargo.toml +++ b/filecoindot_relayer/Cargo.toml @@ -1,5 +1,5 @@ [package] -name = "relayer" +name = "filecoindot_relayer" version = "0.1.0" edition = "2018" diff --git a/relayer/src/api/get_tip_set_by_height.rs b/filecoindot_relayer/src/api/get_tip_set_by_height.rs similarity index 100% rename from relayer/src/api/get_tip_set_by_height.rs rename to filecoindot_relayer/src/api/get_tip_set_by_height.rs diff --git a/relayer/src/api/mod.rs b/filecoindot_relayer/src/api/mod.rs similarity index 100% rename from relayer/src/api/mod.rs rename to filecoindot_relayer/src/api/mod.rs diff --git a/relayer/src/cache.rs b/filecoindot_relayer/src/cache.rs similarity index 100% rename from relayer/src/cache.rs rename to filecoindot_relayer/src/cache.rs diff --git a/relayer/src/client.rs b/filecoindot_relayer/src/client.rs similarity index 90% rename from relayer/src/client.rs rename to filecoindot_relayer/src/client.rs index cc5c13c..11e5582 100644 --- a/relayer/src/client.rs +++ b/filecoindot_relayer/src/client.rs @@ -38,8 +38,8 @@ impl Client { /// Get tipset by block height /// /// ``` - /// # use relayer::Result; - /// use relayer::{Client}; + /// # use filecoindot_relayer::Result; + /// use filecoindot_relayer::{Client}; /// /// # fn main() -> Result<()> { /// assert_eq!( @@ -47,7 +47,7 @@ impl Client { /// Client::new(None)? /// .chain_get_tip_set_by_height(1199840), /// )?, - /// relayer::testing::get_tip_set_by_height_1199840(), + /// filecoindot_relayer::testing::get_tip_set_by_height_1199840(), /// ); /// /// # Ok(()) diff --git a/relayer/src/env.rs b/filecoindot_relayer/src/env.rs similarity index 100% rename from relayer/src/env.rs rename to filecoindot_relayer/src/env.rs diff --git a/relayer/src/lib.rs b/filecoindot_relayer/src/lib.rs similarity index 100% rename from relayer/src/lib.rs rename to filecoindot_relayer/src/lib.rs diff --git a/relayer/src/result.rs b/filecoindot_relayer/src/result.rs similarity index 100% rename from relayer/src/result.rs rename to filecoindot_relayer/src/result.rs diff --git a/relayer/src/testing.rs b/filecoindot_relayer/src/testing.rs similarity index 100% rename from relayer/src/testing.rs rename to filecoindot_relayer/src/testing.rs diff --git a/relayer/src/types.rs b/filecoindot_relayer/src/types.rs similarity index 100% rename from relayer/src/types.rs rename to filecoindot_relayer/src/types.rs