From 5a6467fa3f62c48668c917123915ac41b0a8cfea Mon Sep 17 00:00:00 2001 From: Bryan Stitt Date: Sun, 24 Apr 2022 07:26:00 +0000 Subject: [PATCH] large improvements --- .dockerignore | 1 + Cargo.lock | 238 ++++++++++++++++++++++++++++-------------------- Cargo.toml | 1 + src/main.rs | 247 +++++++++++++++++++++++++++++++++++++++----------- 4 files changed, 339 insertions(+), 148 deletions(-) create mode 100644 .dockerignore diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 00000000..a6f89c2d --- /dev/null +++ b/.dockerignore @@ -0,0 +1 @@ +/target/ \ No newline at end of file diff --git a/Cargo.lock b/Cargo.lock index 9a44049d..dcda5846 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4,9 +4,9 @@ version = 3 [[package]] name = "anyhow" -version = "1.0.55" +version = "1.0.57" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "159bb86af3a200e19a068f4224eae4c8bb2d0fa054c7e5d1cacd5cef95e684cd" +checksum = "08f9b8508dccb7687a1d6c4ce66b2b0ecef467c94667de27d8d7fe1f8d2a9cdc" [[package]] name = "argh" @@ -131,9 +131,9 @@ checksum = "5827cebf4670468b8772dd191856768aedcb1b0278a04f989f7766351917b9dc" [[package]] name = "cpufeatures" -version = "0.2.1" +version = "0.2.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "95059428f66df56b63431fdb4e1947ed2190586af5c5a8a8b71122bdf5a7f469" +checksum = "59a6001667ab124aebae2a495118e11d30984c3a653e99d86d58971708cf5e4b" dependencies = [ "libc", ] @@ -148,6 +148,17 @@ dependencies = [ "typenum", ] +[[package]] +name = "dashmap" +version = "5.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4c8858831f7781322e539ea39e72449c46b059638250c14344fec8d0aa6e539c" +dependencies = [ + "cfg-if", + "num_cpus", + "parking_lot", +] + [[package]] name = "digest" version = "0.9.0" @@ -169,26 +180,13 @@ dependencies = [ [[package]] name = "encoding_rs" -version = "0.8.30" +version = "0.8.31" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7896dc8abb250ffdda33912550faa54c88ec8b998dec0b2c55ab224921ce11df" +checksum = "9852635589dc9f9ea1b6fe9f05b50ef208c85c834a562f0c6abb1c475736ec2b" dependencies = [ "cfg-if", ] -[[package]] -name = "eth-proxy" -version = "0.1.0" -dependencies = [ - "anyhow", - "argh", - "futures", - "reqwest", - "serde_json", - "tokio", - "warp", -] - [[package]] name = "fastrand" version = "1.7.0" @@ -331,20 +329,20 @@ dependencies = [ [[package]] name = "getrandom" -version = "0.2.5" +version = "0.2.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d39cd93900197114fa1fcb7ae84ca742095eed9442088988ae74fa744e930e77" +checksum = "9be70c98951c83b8d2f8f60d7065fa6d5146873094452a1008da8c2f1e4205ad" dependencies = [ "cfg-if", "libc", - "wasi", + "wasi 0.10.2+wasi-snapshot-preview1", ] [[package]] name = "h2" -version = "0.3.11" +version = "0.3.13" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d9f1f717ddc7b2ba36df7e871fd88db79326551d3d6f1fc406fbfd28b582ff8e" +checksum = "37a82c6d637fc9515a4694bbf1cb2457b79d81ce52b3108bdeea58b07dd34a57" dependencies = [ "bytes", "fnv", @@ -355,7 +353,7 @@ dependencies = [ "indexmap", "slab", "tokio", - "tokio-util", + "tokio-util 0.7.1", "tracing", ] @@ -432,9 +430,9 @@ dependencies = [ [[package]] name = "httparse" -version = "1.6.0" +version = "1.7.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9100414882e15fb7feccb4897e5f0ff0ff1ca7d1a86a23208ada4d7a18e6c6c4" +checksum = "6330e8a36bd8c859f3fa6d9382911fbb7147ec39807f63b923933a247240b9ba" [[package]] name = "httpdate" @@ -444,9 +442,9 @@ checksum = "c4a1e36c821dbe04574f602848a19f742f4fb3c98d40449f11bcad18d6b17421" [[package]] name = "hyper" -version = "0.14.17" +version = "0.14.18" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "043f0e083e9901b6cc658a77d1eb86f4fc650bbb977a4337dd63192826aa85dd" +checksum = "b26ae0a80afebe130861d90abf98e3814a4f28a4c6ffeb5ab8ebb2be311e0ef2" dependencies = [ "bytes", "futures-channel", @@ -492,9 +490,9 @@ dependencies = [ [[package]] name = "indexmap" -version = "1.8.0" +version = "1.8.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "282a6247722caba404c065016bbfa522806e51714c34f5dfc3e4a3a46fcb4223" +checksum = "0f647032dfaa1f8b6dc29bd3edb7bbef4861b8b8007ebb118d6db284fd59f6ee" dependencies = [ "autocfg", "hashbrown", @@ -511,9 +509,9 @@ dependencies = [ [[package]] name = "ipnet" -version = "2.3.1" +version = "2.5.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "68f2d64f2edebec4ce84ad108148e67e1064789bee435edc5b60ad398714a3a9" +checksum = "879d54834c8c76457ef4293a689b2a8c59b076067ad77b15efafbb05f92a592b" [[package]] name = "itoa" @@ -523,9 +521,9 @@ checksum = "1aab8fc367588b89dcee83ab0fd66b72b50b72fa1904d7095045ace2b0c81c35" [[package]] name = "js-sys" -version = "0.3.56" +version = "0.3.57" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a38fc24e30fd564ce974c02bf1d337caddff65be6cc4735a1f7eab22a7440f04" +checksum = "671a26f820db17c2a2750743f1dd03bafd15b98c9f30c7c2628c024c05d73397" dependencies = [ "wasm-bindgen", ] @@ -538,24 +536,25 @@ checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646" [[package]] name = "libc" -version = "0.2.119" +version = "0.2.124" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1bf2e165bb3457c8e098ea76f3e3bc9db55f87aa90d52d0e6be741470916aaa4" +checksum = "21a41fed9d98f27ab1c6d161da622a4fa35e8a54a8adc24bbf3ddd0ef70b0e50" [[package]] name = "lock_api" -version = "0.4.6" +version = "0.4.7" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "88943dd7ef4a2e5a4bfa2753aaab3013e34ce2533d1996fb18ef591e315e2b3b" +checksum = "327fa5b6a6940e4699ec49a9beae1ea4845c6bab9314e4f84ac68742139d8c53" dependencies = [ + "autocfg", "scopeguard", ] [[package]] name = "log" -version = "0.4.14" +version = "0.4.16" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "51b9bbe6c47d51fc3e1a9b945965946b4c44142ab8792c50835a980d362c2710" +checksum = "6389c490849ff5bc16be905ae24bc913a9c8892e19b2341dbc175e14c341c2b8" dependencies = [ "cfg-if", ] @@ -590,14 +589,15 @@ dependencies = [ [[package]] name = "mio" -version = "0.8.0" +version = "0.8.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ba272f85fa0b41fc91872be579b3bbe0f56b792aa361a380eb669469f68dafb2" +checksum = "52da4364ffb0e4fe33a9841a98a3f3014fb964045ce4f7a45a398243c8d6b0c9" dependencies = [ "libc", "log", "miow", "ntapi", + "wasi 0.11.0+wasi-snapshot-preview1", "winapi", ] @@ -630,9 +630,9 @@ dependencies = [ [[package]] name = "native-tls" -version = "0.2.8" +version = "0.2.10" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "48ba9f7719b5a0f42f338907614285fb5fd70e53858141f69898a1fb7203b24d" +checksum = "fd7e2f3618557f980e0b17e8856252eee3c97fa12c54dff0ca290fb6266ca4a9" dependencies = [ "lazy_static", "libc", @@ -722,9 +722,9 @@ dependencies = [ [[package]] name = "parking_lot_core" -version = "0.9.1" +version = "0.9.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "28141e0cc4143da2443301914478dc976a61ffdb3f043058310c70df2fed8954" +checksum = "995f667a6c822200b0433ac218e05582f0e2efa1b922a3fd2fbaadc5f87bab37" dependencies = [ "cfg-if", "libc", @@ -773,9 +773,9 @@ checksum = "8b870d8c151b6f2fb93e84a13146138f05d02ed11c7e7c54f8826aaaf7c9f184" [[package]] name = "pkg-config" -version = "0.3.24" +version = "0.3.25" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "58893f751c9b0412871a09abd62ecd2a00298c6c83befa223ef98c52aef40cbe" +checksum = "1df8c4ec4b0627e53bdf214615ad287367e482558cf84b109250b37464dc03ae" [[package]] name = "ppv-lite86" @@ -785,9 +785,9 @@ checksum = "eb9f9e6e233e5c4a35559a617bf40a4ec447db2e84c20b55a6f83167b7e57872" [[package]] name = "proc-macro2" -version = "1.0.36" +version = "1.0.37" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c7342d5883fbccae1cc37a2353b09c87c9b0f3afd73f5fb9bba687a1f733b029" +checksum = "ec757218438d5fda206afc041538b2f6d889286160d649a86a24d37e1235afd1" dependencies = [ "unicode-xid", ] @@ -800,9 +800,9 @@ checksum = "a1d01941d82fa2ab50be1e79e6714289dd7cde78eba4c074bc5a4374f650dfe0" [[package]] name = "quote" -version = "1.0.15" +version = "1.0.18" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "864d3e96a899863136fc6e99f3d7cae289dafe43bf2c5ac19b70df7210c0a145" +checksum = "a1feb54ed693b93a84e14094943b84b7c4eae204c512b7ccb95ab0c66d278ad1" dependencies = [ "proc-macro2", ] @@ -839,9 +839,9 @@ dependencies = [ [[package]] name = "redox_syscall" -version = "0.2.11" +version = "0.2.13" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8380fe0152551244f0747b1bf41737e0f8a74f97a14ccefd1148187271634f3c" +checksum = "62f25bc4c7e55e0b0b7a1d43fb893f4fa1361d0abe38b9ce4f323c2adfe6ef42" dependencies = [ "bitflags", ] @@ -857,9 +857,9 @@ dependencies = [ [[package]] name = "reqwest" -version = "0.11.9" +version = "0.11.10" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "87f242f1488a539a79bac6dbe7c8609ae43b7914b7736210f239a37cccb32525" +checksum = "46a1f7aa4f35e5e8b4160449f51afc758f0ce6454315a9fa7d0d113e958c41eb" dependencies = [ "base64", "bytes", @@ -1012,9 +1012,9 @@ dependencies = [ [[package]] name = "slab" -version = "0.4.5" +version = "0.4.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9def91fd1e018fe007022791f865d0ccc9b3a0d5001e01aabb8b40e46000afb5" +checksum = "eb703cfe953bccee95685111adeedb76fabe4e97549a58d16f03ea7b9367bb32" [[package]] name = "smallvec" @@ -1034,9 +1034,9 @@ dependencies = [ [[package]] name = "syn" -version = "1.0.86" +version = "1.0.91" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8a65b3f4ffa0092e9887669db0eae07941f023991ab58ea44da8fe8e2d511c6b" +checksum = "b683b2b825c8eef438b77c36a06dc262294da3d5a5813fac20da149241dcd44d" dependencies = [ "proc-macro2", "quote", @@ -1171,6 +1171,20 @@ dependencies = [ "tokio", ] +[[package]] +name = "tokio-util" +version = "0.7.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0edfdeb067411dba2044da6d1cb2df793dd35add7888d73c16e3381ded401764" +dependencies = [ + "bytes", + "futures-core", + "futures-sink", + "pin-project-lite", + "tokio", + "tracing", +] + [[package]] name = "tower-service" version = "0.3.1" @@ -1179,21 +1193,33 @@ checksum = "360dfd1d6d30e05fda32ace2c8c70e9c0a9da713275777f5a4dbb8a1893930c6" [[package]] name = "tracing" -version = "0.1.31" +version = "0.1.34" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f6c650a8ef0cd2dd93736f033d21cbd1224c5a967aa0c258d00fcf7dafef9b9f" +checksum = "5d0ecdcb44a79f0fe9844f0c4f33a342cbcbb5117de8001e6ba0dc2351327d09" dependencies = [ "cfg-if", "log", "pin-project-lite", + "tracing-attributes", "tracing-core", ] [[package]] -name = "tracing-core" -version = "0.1.22" +name = "tracing-attributes" +version = "0.1.20" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "03cfcb51380632a72d3111cb8d3447a8d908e577d31beeac006f836383d29a23" +checksum = "2e65ce065b4b5c53e73bb28912318cb8c9e9ad3921f1d669eb0e68b4c8143a2b" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "tracing-core" +version = "0.1.26" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f54c8ca710e81886d498c2fd3331b56c93aa248d49de2222ad2742247c60072f" dependencies = [ "lazy_static", ] @@ -1339,7 +1365,7 @@ dependencies = [ "tokio", "tokio-stream", "tokio-tungstenite", - "tokio-util", + "tokio-util 0.6.9", "tower-service", "tracing", ] @@ -1351,10 +1377,16 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "fd6fbd9a79829dd1ad0cc20627bf1ed606756a7f77edff7b66b7064f9cb327c6" [[package]] -name = "wasm-bindgen" -version = "0.2.79" +name = "wasi" +version = "0.11.0+wasi-snapshot-preview1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "25f1af7423d8588a3d840681122e72e6a24ddbcb3f0ec385cac0d12d24256c06" +checksum = "9c8d87e72b64a3b4db28d11ce29237c246188f4f51057d65a7eab63b7987e423" + +[[package]] +name = "wasm-bindgen" +version = "0.2.80" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "27370197c907c55e3f1a9fbe26f44e937fe6451368324e009cba39e139dc08ad" dependencies = [ "cfg-if", "wasm-bindgen-macro", @@ -1362,9 +1394,9 @@ dependencies = [ [[package]] name = "wasm-bindgen-backend" -version = "0.2.79" +version = "0.2.80" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8b21c0df030f5a177f3cba22e9bc4322695ec43e7257d865302900290bcdedca" +checksum = "53e04185bfa3a779273da532f5025e33398409573f348985af9a1cbf3774d3f4" dependencies = [ "bumpalo", "lazy_static", @@ -1377,9 +1409,9 @@ dependencies = [ [[package]] name = "wasm-bindgen-futures" -version = "0.4.29" +version = "0.4.30" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2eb6ec270a31b1d3c7e266b999739109abce8b6c87e4b31fcfcd788b65267395" +checksum = "6f741de44b75e14c35df886aff5f1eb73aa114fa5d4d00dcd37b5e01259bf3b2" dependencies = [ "cfg-if", "js-sys", @@ -1389,9 +1421,9 @@ dependencies = [ [[package]] name = "wasm-bindgen-macro" -version = "0.2.79" +version = "0.2.80" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2f4203d69e40a52ee523b2529a773d5ffc1dc0071801c87b3d270b471b80ed01" +checksum = "17cae7ff784d7e83a2fe7611cfe766ecf034111b49deb850a3dc7699c08251f5" dependencies = [ "quote", "wasm-bindgen-macro-support", @@ -1399,9 +1431,9 @@ dependencies = [ [[package]] name = "wasm-bindgen-macro-support" -version = "0.2.79" +version = "0.2.80" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "bfa8a30d46208db204854cadbb5d4baf5fcf8071ba5bf48190c3e59937962ebc" +checksum = "99ec0dc7a4756fffc231aab1b9f2f578d23cd391390ab27f952ae0c9b3ece20b" dependencies = [ "proc-macro2", "quote", @@ -1412,20 +1444,34 @@ dependencies = [ [[package]] name = "wasm-bindgen-shared" -version = "0.2.79" +version = "0.2.80" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3d958d035c4438e28c70e4321a2911302f10135ce78a9c7834c0cab4123d06a2" +checksum = "d554b7f530dee5964d9a9468d95c1f8b8acae4f282807e7d27d4b03099a46744" [[package]] name = "web-sys" -version = "0.3.56" +version = "0.3.57" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c060b319f29dd25724f09a2ba1418f142f539b2be99fbf4d2d5a8f7330afb8eb" +checksum = "7b17e741662c70c8bd24ac5c5b18de314a2c26c32bf8346ee1e6f53de919c283" dependencies = [ "js-sys", "wasm-bindgen", ] +[[package]] +name = "web3-proxy" +version = "0.1.0" +dependencies = [ + "anyhow", + "argh", + "dashmap", + "futures", + "reqwest", + "serde_json", + "tokio", + "warp", +] + [[package]] name = "winapi" version = "0.3.9" @@ -1450,9 +1496,9 @@ checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" [[package]] name = "windows-sys" -version = "0.32.0" +version = "0.34.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3df6e476185f92a12c072be4a189a0210dcdcf512a1891d6dff9edb874deadc6" +checksum = "5acdd78cb4ba54c0045ac14f62d8f94a03d10047904ae2a40afa1e99d8f70825" dependencies = [ "windows_aarch64_msvc", "windows_i686_gnu", @@ -1463,39 +1509,39 @@ dependencies = [ [[package]] name = "windows_aarch64_msvc" -version = "0.32.0" +version = "0.34.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d8e92753b1c443191654ec532f14c199742964a061be25d77d7a96f09db20bf5" +checksum = "17cffbe740121affb56fad0fc0e421804adf0ae00891205213b5cecd30db881d" [[package]] name = "windows_i686_gnu" -version = "0.32.0" +version = "0.34.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6a711c68811799e017b6038e0922cb27a5e2f43a2ddb609fe0b6f3eeda9de615" +checksum = "2564fde759adb79129d9b4f54be42b32c89970c18ebf93124ca8870a498688ed" [[package]] name = "windows_i686_msvc" -version = "0.32.0" +version = "0.34.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "146c11bb1a02615db74680b32a68e2d61f553cc24c4eb5b4ca10311740e44172" +checksum = "9cd9d32ba70453522332c14d38814bceeb747d80b3958676007acadd7e166956" [[package]] name = "windows_x86_64_gnu" -version = "0.32.0" +version = "0.34.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c912b12f7454c6620635bbff3450962753834be2a594819bd5e945af18ec64bc" +checksum = "cfce6deae227ee8d356d19effc141a509cc503dfd1f850622ec4b0f84428e1f4" [[package]] name = "windows_x86_64_msvc" -version = "0.32.0" +version = "0.34.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "504a2476202769977a040c6364301a3f65d0cc9e3fb08600b2bda150a0488316" +checksum = "d19538ccc21819d01deaf88d6a17eae6596a12e9aafdbb97916fb49896d89de9" [[package]] name = "winreg" -version = "0.7.0" +version = "0.10.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0120db82e8a1e0b9fb3345a539c478767c0048d842860994d96113d5b667bd69" +checksum = "80d0f4e272c85def139476380b12f9ac60926689dd2e01d4923222f40580869d" dependencies = [ "winapi", ] diff --git a/Cargo.toml b/Cargo.toml index c873c159..ee19ec3d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -8,6 +8,7 @@ edition = "2021" [dependencies] argh = "0.1" anyhow = "1.0" +dashmap = "5.2" futures = { version = "0.3.0", features = ["thread-pool"]} tokio = { version = "1", features = ["full"] } reqwest = { version = "0.11", features = ["json"] } diff --git a/src/main.rs b/src/main.rs index adee197e..6964e3ac 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,74 +1,217 @@ +use dashmap::DashMap; +use futures::stream::FuturesUnordered; +use futures::StreamExt; use std::sync::Arc; +use tokio::sync::RwLock; +use tokio::time::{sleep, Duration}; use warp::Filter; +/// Load balance to the least-connection rpc +struct BalancedRpcs { + rpcs: RwLock>, + connections: DashMap, + // TODO: what type? store with connections? + // ratelimits: DashMap, +} -const ETH_LOCALHOST_RPC: &str = "http://localhost:8545"; -const ETH_EDEN_RPC: &str = "https://api.edennetwork.io/v1/beta"; +// TODO: also pass rate limits to this? +impl Into for Vec<&str> { + fn into(self) -> BalancedRpcs { + let mut rpcs: Vec = vec![]; + let connections = DashMap::new(); + // let ratelimits = DashMap::new(); + // TODO: i'm sure there is a better way to do this with more iterator things like collect, but this works + for s in self.into_iter() { + rpcs.push(s.to_string()); + connections.insert(s.to_string(), 0); + // ratelimits.insert(s.to_string(), 0); + } -#[derive(argh::FromArgs)] -/// Proxy Web3 Requests -struct Web3ProxyConfig { - /// the primary Ethereum RPC server - #[argh(option, default = "ETH_LOCALHOST_RPC.to_string()")] - eth_primary_rpc: String, + BalancedRpcs { + rpcs: RwLock::new(rpcs), + connections, + // ratelimits, + } + } +} - /// the private Ethereum RPC server - #[argh(option, default = "ETH_EDEN_RPC.to_string()")] - eth_private_rpc: String, +impl BalancedRpcs { + async fn get_upstream_server(&self) -> Option { + let mut balanced_rpcs = self.rpcs.write().await; - /// the port to listen on - #[argh(option, default = "8845")] - listen_port: u16, + balanced_rpcs.sort_unstable_by(|a, b| { + self.connections + .get(a) + .unwrap() + .cmp(&self.connections.get(b).unwrap()) + }); + + // TODO: don't just grab the first. check rate limits + if let Some(selected_rpc) = balanced_rpcs.first() { + let mut connections = self.connections.get_mut(selected_rpc).unwrap(); + *connections += 1; + + return Some(selected_rpc.clone()); + } + + None + } +} + +/// Send to all the Rpcs +struct LoudRpcs { + rpcs: Vec, + // TODO: what type? store with connections? + // ratelimits: DashMap, +} + +impl Into for Vec<&str> { + fn into(self) -> LoudRpcs { + let mut rpcs: Vec = vec![]; + // let ratelimits = DashMap::new(); + + // TODO: i'm sure there is a better way to do this with more iterator things like collect, but this works + for s in self.into_iter() { + rpcs.push(s.to_string()); + // ratelimits.insert(s.to_string(), 0); + } + + LoudRpcs { + rpcs, + // ratelimits, + } + } +} + +impl LoudRpcs { + async fn get_upstream_servers(&self) -> Vec { + self.rpcs.clone() + } + + fn as_bool(&self) -> bool { + self.rpcs.len() > 0 + } +} + +struct Web3ProxyState { + client: reqwest::Client, + balanced_rpc_tiers: Vec, + private_rpcs: LoudRpcs, +} + +impl Web3ProxyState { + fn new(balanced_rpc_tiers: Vec>, private_rpcs: Vec<&str>) -> Web3ProxyState { + // TODO: warn if no private relays + Web3ProxyState { + client: reqwest::Client::new(), + balanced_rpc_tiers: balanced_rpc_tiers.into_iter().map(Into::into).collect(), + private_rpcs: private_rpcs.into(), + } + } + + /// send the request to the approriate RPCs + async fn proxy_web3_rpc( + self: Arc, + json_body: serde_json::Value, + ) -> anyhow::Result { + let eth_send_raw_transaction = + serde_json::Value::String("eth_sendRawTransaction".to_string()); + + if self.private_rpcs.as_bool() && json_body.get("method") == Some(ð_send_raw_transaction) + { + // there are private rpcs configured and the request is eth_sendSignedTransaction. send to all private rpcs + let upstream_servers = self.private_rpcs.get_upstream_servers().await; + + if let Ok(result) = self.try_send_requests(upstream_servers, &json_body).await { + return Ok(result); + } + } else { + // this is not a private transaction (or no private relays are configured) + for balanced_rpcs in self.balanced_rpc_tiers.iter() { + if let Some(upstream_server) = balanced_rpcs.get_upstream_server().await { + // TODO: capture any errors. at least log them + if let Ok(result) = self + .try_send_requests(vec![upstream_server], &json_body) + .await + { + return Ok(result); + } + } + } + } + + return Err(anyhow::anyhow!("all servers failed")); + } + + async fn try_send_requests( + &self, + upstream_servers: Vec, + json_body: &serde_json::Value, + ) -> anyhow::Result { + // send the query to all the servers + let mut future_responses = FuturesUnordered::new(); + for upstream_server in upstream_servers.into_iter() { + let f = self.client.post(upstream_server).json(&json_body).send(); + + future_responses.push(f); + } + + // start loading text responses + let mut future_text = FuturesUnordered::new(); + while let Some(request) = future_responses.next().await { + if let Ok(request) = request { + let f = request.text(); + + future_text.push(f); + } + } + + // return the first response + while let Some(text) = future_text.next().await { + if let Ok(text) = text { + // TODO: if "no block with that header", skip this response (maybe retry) + return Ok(text); + } + // TODO: capture errors + } + + Err(anyhow::anyhow!("no successful responses")) + } } #[tokio::main] async fn main() { - let config: Web3ProxyConfig = argh::from_env(); + // TODO: load the config from yaml instead of hard coding + // TODO: support multiple chains in one process. then we could just point "chain.stytt.com" at this and caddy wouldn't need anything else + // TODO: i kind of want to make use of caddy's load balancing and health checking and such though + let listen_port = 8445; + // TODO: be smart about about using archive nodes? + let state = Web3ProxyState::new( + vec![ + // local nodes + vec!["https://10.11.12.16:8545"], + // paid nodes + // TODO: add them + // free nodes + vec!["https://main-rpc.linkpool.io", "https://rpc.ankr.com/eth"], + ], + vec!["https://api.edennetwork.io/v1/beta"], + ); - let config = Arc::new(config); + let state: Arc = Arc::new(state); - let listen_port = config.listen_port; - - let hello = warp::path::end().map(|| format!("Hello, world!")); - - let proxy_eth_filter = warp::path!("eth") + let proxy_rpc_filter = warp::any() .and(warp::post()) .and(warp::body::json()) - .then(move |json_body| proxy_eth_rpc(config.clone(), json_body)) + .then(move |json_body| state.clone().proxy_web3_rpc(json_body)) .map(handle_anyhow_errors); - // TODO: relay ftm, bsc, polygon, avax, ... + println!("Listening on 0.0.0.0:{}", listen_port); - let routes = warp::any().and(hello.or(proxy_eth_filter)); - - println!("Listening on port {}", listen_port); - - warp::serve(routes).run(([127, 0, 0, 1], listen_port)).await; -} - -/// send the request to the approriate Ethereum RPC -async fn proxy_eth_rpc( - config: Arc, - json_body: serde_json::Value, -) -> anyhow::Result { - // TODO: this should be a list of servers. query all. prefer result from first server even if it is a bit late - // TODO: automatically rank servers based on request latency and block height - let eth_send_signed_transaction = - serde_json::Value::String("eth_sendSignedTransaction".to_string()); - - let upstream_server = if json_body.get("method") == Some(ð_send_signed_transaction) { - &config.eth_private_rpc - } else { - // TODO: if querying a block older than 256 blocks old, send to an archive node. otherwise a fast sync node is fine - &config.eth_primary_rpc - }; - - // TODO: reuse this Client/RequestBuilder - let client = reqwest::Client::new(); - let res = client.post(upstream_server).json(&json_body).send().await?; - - Ok(res.text().await?) + warp::serve(proxy_rpc_filter) + .run(([0, 0, 0, 0], listen_port)) + .await; } /// convert result into an http response. use this at the end of your warp filter