|
| 1 | +# gRPC Trading API |
| 2 | + |
| 3 | +Low-latency binary API for high-frequency trading and MEV operations on Mandarin (Geth fork). |
| 4 | + |
| 5 | +## Overview |
| 6 | + |
| 7 | +This package provides a Protocol Buffer-based gRPC API that replaces slow JSON-RPC for performance-critical operations. Key features: |
| 8 | + |
| 9 | +- **Binary protocol**: 10x performance improvement over JSON-RPC |
| 10 | +- **Bundle operations**: Submit and simulate transaction bundles with profit calculation |
| 11 | +- **Batch operations**: Read multiple storage slots in a single call |
| 12 | +- **Low latency**: Direct integration with miner and state database |
| 13 | +- **Type safety**: Strongly-typed interfaces via Protocol Buffers |
| 14 | + |
| 15 | +## Architecture |
| 16 | + |
| 17 | +- `trader.proto`: Protocol Buffer definitions |
| 18 | +- `trader.pb.go` / `trader_grpc.pb.go`: Generated Go code |
| 19 | +- `server.go`: gRPC server implementation |
| 20 | +- `service.go`: Node lifecycle integration |
| 21 | +- `example_client.go`: Client library and usage examples |
| 22 | + |
| 23 | +## Configuration |
| 24 | + |
| 25 | +Enable gRPC in your node configuration: |
| 26 | + |
| 27 | +```toml |
| 28 | +[Eth] |
| 29 | +EnableGRPC = true |
| 30 | +GRPCHost = "localhost" |
| 31 | +GRPCPort = 9090 |
| 32 | +``` |
| 33 | + |
| 34 | +Or via command-line flags: |
| 35 | + |
| 36 | +```bash |
| 37 | +geth --grpc --grpc.addr localhost --grpc.port 9090 |
| 38 | +``` |
| 39 | + |
| 40 | +## Usage Example |
| 41 | + |
| 42 | +```go |
| 43 | +import grpcapi "github.com/ethereum/go-ethereum/api/grpc" |
| 44 | + |
| 45 | +// Connect to gRPC server |
| 46 | +client, err := grpcapi.NewClient("localhost", 9090) |
| 47 | +if err != nil { |
| 48 | + log.Fatal(err) |
| 49 | +} |
| 50 | +defer client.Close() |
| 51 | + |
| 52 | +// Batch read Uniswap pool state |
| 53 | +poolAddr := common.HexToAddress("0x...") |
| 54 | +slots := []common.Hash{ |
| 55 | + common.HexToHash("0x0"), // slot0 |
| 56 | + common.HexToHash("0x1"), // feeGrowthGlobal0X128 |
| 57 | +} |
| 58 | +values, err := client.GetStorageBatch(ctx, poolAddr, slots, nil) |
| 59 | + |
| 60 | +// Simulate bundle |
| 61 | +simResult, err := client.SimulateBundle(ctx, txs, &grpcapi.BundleOptions{ |
| 62 | + TargetBlock: &blockNum, |
| 63 | +}) |
| 64 | + |
| 65 | +profit := new(big.Int).SetBytes(simResult.Profit) |
| 66 | +fmt.Printf("Bundle profit: %s wei\n", profit) |
| 67 | + |
| 68 | +// Submit if profitable |
| 69 | +if profit.Sign() > 0 { |
| 70 | + bundleHash, err := client.SubmitBundle(ctx, txs, opts) |
| 71 | + fmt.Printf("Bundle submitted: %s\n", bundleHash.Hex()) |
| 72 | +} |
| 73 | +``` |
| 74 | + |
| 75 | +## API Methods |
| 76 | + |
| 77 | +### SimulateBundle |
| 78 | +Simulate transaction bundle execution without submitting to mempool. |
| 79 | + |
| 80 | +**Request:** |
| 81 | +- `transactions`: RLP-encoded transactions |
| 82 | +- `min_timestamp`, `max_timestamp`: Optional timing constraints |
| 83 | +- `target_block`: Specific block number target |
| 84 | +- `reverting_txs`: Indices of transactions allowed to revert |
| 85 | + |
| 86 | +**Response:** |
| 87 | +- `success`: Whether all transactions succeeded |
| 88 | +- `gas_used`: Total gas consumed |
| 89 | +- `profit`: Coinbase profit in wei |
| 90 | +- `coinbase_balance`: Final coinbase balance |
| 91 | +- `tx_results`: Per-transaction results with gas, errors, return values |
| 92 | + |
| 93 | +### SubmitBundle |
| 94 | +Submit bundle for inclusion in future blocks. |
| 95 | + |
| 96 | +**Request:** Same as SimulateBundle |
| 97 | + |
| 98 | +**Response:** |
| 99 | +- `bundle_hash`: Unique bundle identifier |
| 100 | + |
| 101 | +### GetStorageBatch |
| 102 | +Read multiple storage slots in a single call. **10-100x faster** than multiple `eth_getStorageAt` calls. |
| 103 | + |
| 104 | +**Request:** |
| 105 | +- `contract`: Contract address |
| 106 | +- `slots`: Array of storage slot hashes |
| 107 | +- `block_number`: Optional block height |
| 108 | + |
| 109 | +**Response:** |
| 110 | +- `values`: Array of storage values (same order as request) |
| 111 | + |
| 112 | +### GetPendingTransactions |
| 113 | +Retrieve pending transactions from mempool. |
| 114 | + |
| 115 | +**Request:** |
| 116 | +- `min_gas_price`: Optional filter for minimum gas price |
| 117 | + |
| 118 | +**Response:** |
| 119 | +- `transactions`: Array of RLP-encoded transactions |
| 120 | + |
| 121 | +### CallContract |
| 122 | +Execute contract call (equivalent to `eth_call`). |
| 123 | + |
| 124 | +**Request:** |
| 125 | +- `from`, `to`, `data`: Standard call parameters |
| 126 | +- `gas`, `gas_price`, `value`: Optional parameters |
| 127 | +- `block_number`: Optional block height |
| 128 | + |
| 129 | +**Response:** |
| 130 | +- `return_data`: Call result |
| 131 | +- `gas_used`: Gas consumed |
| 132 | +- `success`: Whether call succeeded |
| 133 | +- `error`: Error message if failed |
| 134 | + |
| 135 | +## Performance Characteristics |
| 136 | + |
| 137 | +Based on Phase 2 benchmarks: |
| 138 | + |
| 139 | +- **Transaction feed latency**: ~2.5μs average, 21μs max |
| 140 | +- **Storage batch reads**: 10-100x faster than JSON-RPC (single call vs N round-trips) |
| 141 | +- **Bundle simulation**: Direct EVM access, no JSON marshaling overhead |
| 142 | +- **gRPC overhead**: ~100-500μs vs 1-5ms for JSON-RPC |
| 143 | + |
| 144 | +For a 10-transaction bundle simulation: |
| 145 | +- JSON-RPC: ~50ms (encoding + network + decoding) |
| 146 | +- gRPC: ~5ms (binary encoding, single RTT) |
| 147 | + |
| 148 | +## Development |
| 149 | + |
| 150 | +### Regenerating Protocol Buffers |
| 151 | + |
| 152 | +If you modify `trader.proto`: |
| 153 | + |
| 154 | +```bash |
| 155 | +protoc --go_out=. --go_opt=paths=source_relative \ |
| 156 | + --go-grpc_out=. --go-grpc_opt=paths=source_relative \ |
| 157 | + api/grpc/trader.proto |
| 158 | +``` |
| 159 | + |
| 160 | +### Testing |
| 161 | + |
| 162 | +Integration tests require a running node: |
| 163 | + |
| 164 | +```bash |
| 165 | +# Terminal 1: Start node with gRPC enabled |
| 166 | +geth --dev --grpc --grpc.port 9090 |
| 167 | + |
| 168 | +# Terminal 2: Run tests |
| 169 | +go test -v ./api/grpc/... |
| 170 | +``` |
| 171 | + |
| 172 | +### Benchmarking |
| 173 | + |
| 174 | +Compare gRPC vs JSON-RPC performance: |
| 175 | + |
| 176 | +```bash |
| 177 | +go test -bench=. -benchtime=10s ./api/grpc/... |
| 178 | +``` |
| 179 | + |
| 180 | +## Security Considerations |
| 181 | + |
| 182 | +- **Network exposure**: gRPC server should only listen on localhost or trusted networks |
| 183 | +- **Authentication**: Add mTLS or API keys for production deployments |
| 184 | +- **Rate limiting**: Not currently implemented, add as needed |
| 185 | +- **Bundle privacy**: Bundles are visible to node operator |
| 186 | + |
| 187 | +## Future Enhancements |
| 188 | + |
| 189 | +1. **Streaming APIs**: Real-time transaction feed via server-side streaming |
| 190 | +2. **Hot state cache**: Dedicated cache for frequently-accessed DeFi contracts |
| 191 | +3. **Parallel simulation**: Multiple bundle simulations in concurrent goroutines |
| 192 | +4. **State deltas**: Export compact state diffs instead of full state |
| 193 | +5. **Shared memory**: Zero-copy data sharing for co-located bots |
| 194 | +6. **Authentication**: mTLS, JWT, or API key support |
| 195 | +7. **Metrics**: Prometheus integration for latency and throughput monitoring |
| 196 | + |
| 197 | +## License |
| 198 | + |
| 199 | +LGPL-3.0-or-later (same as go-ethereum) |
| 200 | + |
0 commit comments