web3-proxy/README.md

134 lines
5.3 KiB
Markdown
Raw Normal View History

2022-08-06 08:46:33 +03:00
# web3_proxy
2022-03-05 06:58:00 +03:00
2022-08-06 08:46:33 +03:00
Web3_proxy is a fast caching and load balancing proxy for web3 (Ethereum or similar) JsonRPC servers.
2022-05-12 08:54:27 +03:00
2022-10-22 09:02:32 +03:00
**Under construction!** This code is under active development. If you want to run this proxy youself, send me a message on [Twitter](https://twitter.com/StittsHappening) and I can explain things that aren't documented yet. Most RPC methods are supported, but filters are coming soon. And of course, more tests are always needed.
2022-06-18 23:51:14 +03:00
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-07-26 01:36:02 +03:00
$ cargo run --release -- --help
2022-03-05 07:59:11 +03:00
```
2022-03-05 06:58:00 +03:00
```
2022-08-06 08:46:33 +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-08-06 08:46:33 +03:00
Running `target/release/web3_proxy --help`
Usage: web3_proxy [--port <port>] [--workers <workers>] [--config <config>]
2022-03-05 06:58:00 +03:00
2022-08-06 08:46:33 +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-07-19 09:41:04 +03:00
Start the server with the defaults (listen on `http://localhost:8544` and use `./config/example.toml` which proxies to a bunch of public nodes:
2022-05-12 08:54:27 +03:00
2022-03-05 06:58:00 +03:00
```
2022-07-26 01:36:02 +03:00
cargo run --release -- --config ./config/example.toml
2022-03-05 07:59:11 +03:00
```
2022-07-19 09:41:04 +03:00
## Common commands
2022-09-22 02:50:55 +03:00
Create a user:
```
cargo run --bin web3_proxy_cli -- --db-url "$YOUR_DB_URL" create_user --address "$USER_ADDRESS_0x"
```
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-07-19 09:41:04 +03:00
Check that the websocket is working:
```
$ websocat ws://127.0.0.1:8544
{"id": 1, "method": "eth_subscribe", "params": ["newHeads"]}
{"id": 2, "method": "eth_subscribe", "params": ["newPendingTransactions"]}
{"id": 3, "method": "eth_subscribe", "params": ["newPendingFullTransactions"]}
{"id": 4, "method": "eth_subscribe", "params": ["newPendingRawTransactions"]}
```
You can copy `config/example.toml` to `config/production-$CHAINNAME.toml` and then run `docker-compose up --build -d` start proxies for many chains.
2022-05-12 08:54:27 +03:00
2022-11-01 21:54:39 +03:00
Run migrations (useful during development. in production, the migrations run on application start)
```
cd migration
cargo run up
```
2022-08-04 02:17:02 +03:00
## Database entities
2022-08-17 00:10:03 +03:00
This command only needs to be run during development. Production should use the already generated entities.
When developing new database migrations, **after you migrate**, run this command to generate updated entity files. It's best to keep the migration and entity changes in the same commit.
2022-08-04 02:17:02 +03:00
2022-10-22 05:57:45 +03:00
```
cargo install sea-orm-cli
```
2022-08-04 02:17:02 +03:00
```
2022-09-24 03:14:35 +03:00
sea-orm-cli generate entity -u mysql://root:dev_web3_proxy@127.0.0.1:13306/dev_web3_proxy -o entities/src --with-serde both
2022-08-04 02:17:02 +03:00
```
2022-08-17 00:10:03 +03:00
After running the above, you will need to manually fix some columns: `Vec<u8>` -> `sea_orm::prelude::Uuid` and `i8` -> `bool`. Related: <https://github.com/SeaQL/sea-query/issues/375> <https://github.com/SeaQL/sea-orm/issues/924>
2022-04-28 22:30:22 +03:00
## Flame Graphs
2022-08-17 00:10:03 +03:00
Flame graphs make a developer's join of finding slow code painless:
2022-05-12 08:54:27 +03:00
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
$ cat /proc/sys/kernel/perf_event_paranoid
4
$ echo -1 | sudo tee /proc/sys/kernel/perf_event_paranoid
2022-11-12 09:11:58 +03:00
-1
$ CARGO_PROFILE_RELEASE_DEBUG=true cargo flamegraph --bin web3_proxy
2022-04-28 22:30:22 +03:00
2022-05-17 07:24:13 +03:00
## GDB
2022-08-17 00:10:03 +03:00
Developers can run the proxy under gdb for advanced debugging:
2022-05-17 07:24:13 +03:00
2022-08-06 08:46:33 +03:00
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-05-17 07:24:13 +03:00
2022-07-26 01:36:02 +03:00
TODO: also enable debug symbols in the release build by modifying the root Cargo.toml
2022-05-17 07:24:13 +03:00
## Load Testing
Test the proxy:
2022-09-07 06:54:16 +03:00
wrk -s ./wrk/getBlockNumber.lua -t12 -c400 -d30s --latency http://127.0.0.1:8544/u/$API_KEY
wrk -s ./wrk/getLatestBlockByNumber.lua -t12 -c400 -d30s --latency http://127.0.0.1:8544/u/$API_KEY
2022-07-19 09:41:04 +03:00
Test geth (assuming it is on 8545):
2022-09-07 06:54:16 +03:00
wrk -s ./wrk/getBlockNumber.lua -t12 -c400 -d30s --latency http://127.0.0.1:8545
wrk -s ./wrk/getLatestBlockByNumber.lua -t12 -c400 -d30s --latency http://127.0.0.1:8545
2022-07-19 09:41:04 +03:00
Test erigon (assuming it is on 8945):
2022-09-07 06:54:16 +03:00
wrk -s ./wrk/getBlockNumber.lua -t12 -c400 -d30s --latency http://127.0.0.1:8945
wrk -s ./wrk/getLatestBlockByNumber.lua -t12 -c400 -d30s --latency http://127.0.0.1:8945
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.
2022-07-19 09:41:04 +03:00
2022-08-12 22:07:14 +03:00
Run [ethspam](https://github.com/INFURA/versus) and [versus](https://github.com/shazow/ethspam) for a more realistic load test:
2022-07-19 09:41:04 +03:00
2022-09-07 06:54:16 +03:00
ethspam --rpc http://127.0.0.1:8544/u/$API_KEY | versus --concurrency=100 --stop-after=10000 http://127.0.0.1:8544/u/$API_KEY