2022-03-05 06:55:16 +03:00
# web3-proxy
2022-03-05 06:58:00 +03:00
2022-05-12 08:54:27 +03:00
Web3-proxy is a fast caching and load balancing proxy for web3 (Ethereum or similar) JsonRPC servers.
2022-06-18 23:51:14 +03:00
**Under construction!** This code is under active development. The basics seem to work, but theres lots of tests and features to write still.
2022-05-12 08:54:27 +03:00
Signed transactions (eth_sendRawTransaction) are sent in parallel to the configured private RPCs (eden, ethermine, flashbots, etc.).
All other requests are sent to an RPC server on the latest block (alchemy, moralis, rivet, your own node, or one of many other providers). If multiple servers are in sync, they are prioritized by `active_requests/soft_limit` . Note that this means that the fastest server is most likely to serve requests and slow servers are unlikely to ever get any requests.
2022-05-16 19:15:19 +03:00
Each server has different limits to configure. The `soft_limit` is the number of parallel active requests where a server starts to slow down. The `hard_limit` is where a server starts giving rate limits or other errors.
2022-03-05 07:59:11 +03:00
```
2022-05-20 05:50:22 +03:00
$ cargo run --release -p web3-proxy -- --help
2022-03-05 07:59:11 +03:00
```
2022-03-05 06:58:00 +03:00
```
2022-05-12 08:54:27 +03:00
Compiling web3-proxy v0.1.0 (/home/bryan/src/web3-proxy/web3-proxy)
2022-05-20 05:50:22 +03:00
Finished release [optimized + debuginfo] target(s) in 17.69s
2022-05-07 04:53:16 +03:00
Running `target/release/web3-proxy --help`
2022-05-20 05:50:22 +03:00
Usage: web3-proxy [--port < port > ] [--workers < workers > ] [--config < config > ]
2022-03-05 06:58:00 +03:00
2022-05-12 08:54:27 +03:00
Web3-proxy is a fast caching and load balancing proxy for web3 (Ethereum or similar) JsonRPC servers.
2022-03-05 06:58:00 +03:00
Options:
2022-05-20 05:50:22 +03:00
--port what port the proxy should listen on
--workers number of worker threads
--config path to a toml of rpc servers
2022-03-05 06:58:00 +03:00
--help display usage information
2022-03-05 07:59:11 +03:00
```
2022-03-05 06:58:00 +03:00
2022-05-12 08:54:27 +03:00
Start the server with the defaults (listen on `http://localhost:8544` and use `./config/example.toml` which proxies to a local websocket on 8546 and ankr's public ETH node):
2022-03-05 06:58:00 +03:00
```
2022-05-20 05:48:08 +03:00
cargo run --release -p web3-proxy
2022-03-05 07:59:11 +03:00
```
2022-05-12 08:54:27 +03:00
Check that the proxy is working:
2022-03-05 07:59:11 +03:00
```
2022-05-20 05:50:22 +03:00
curl -X POST -H "Content-Type: application/json" --data '{"jsonrpc":"2.0","method":"web3_clientVersion","id":1}' 127.0.0.1:8544
2022-03-05 07:59:11 +03:00
```
2022-05-12 08:54:27 +03:00
You can copy `config/example.toml` to `config/production-$CHAINNAME.toml` and then run `docker-compose up --build -d` start a proxies for many chains.
2022-04-28 22:30:22 +03:00
## Flame Graphs
2022-05-12 08:54:27 +03:00
Flame graphs make finding slow code painless:
2022-04-28 22:30:22 +03:00
$ cat /proc/sys/kernel/kptr_restrict
1
2022-05-20 05:48:08 +03:00
$ echo 0 | sudo tee /proc/sys/kernel/kptr_restrict
2022-04-28 22:30:22 +03:00
0
$ CARGO_PROFILE_RELEASE_DEBUG=true cargo flamegraph
2022-05-17 07:24:13 +03:00
## GDB
Run the proxy under gdb for advanced debugging:
cargo build --release & & RUST_LOG=web3_proxy=debug rust-gdb --args target/debug/web3-proxy --listen-port 7503 --rpc-config-path ./config/production-eth.toml
2022-04-28 01:53:13 +03:00
## Load Testing
Test the proxy:
2022-05-07 04:53:16 +03:00
wrk -s ./data/wrk/getBlockNumber.lua -t12 -c400 -d30s --latency http://127.0.0.1:8544
wrk -s ./data/wrk/getLatestBlockByNumber.lua -t12 -c400 -d30s --latency http://127.0.0.1:8544
2022-04-28 01:53:13 +03:00
Test geth:
2022-04-28 22:30:22 +03:00
wrk -s ./data/wrk/getBlockNumber.lua -t12 -c400 -d30s --latency http://127.0.0.1:8545
wrk -s ./data/wrk/getLatestBlockByNumber.lua -t12 -c400 -d30s --latency http://127.0.0.1:8545
2022-04-28 01:53:13 +03:00
Test erigon:
2022-04-28 22:30:22 +03:00
wrk -s ./data/wrk/getBlockNumber.lua -t12 -c400 -d30s --latency http://127.0.0.1:8945
wrk -s ./data/wrk/getLatestBlockByNumber.lua -t12 -c400 -d30s --latency http://127.0.0.1:8945
2022-04-28 01:53:13 +03:00
2022-03-05 07:59:11 +03:00
2022-05-12 08:54:27 +03:00
Note: Testing with `getLatestBlockByNumber.lua` is not great because the latest block changes and so one run is likely to be very different than another.