|
| 1 | +# Restricted Address Filtering - Architecture |
| 2 | + |
| 3 | +## Overview |
| 4 | + |
| 5 | +The `restrictedaddr` package provides compliance-based address filtering for Nitro sequencers. It maintains a list of restricted address hashes (loaded from S3) and blocks transactions involving those addresses. |
| 6 | + |
| 7 | +## Architecture Diagram |
| 8 | + |
| 9 | +```mermaid |
| 10 | +flowchart LR |
| 11 | + subgraph DataSync["Data Synchronization"] |
| 12 | + direction TB |
| 13 | + S3[("S3 Bucket")] |
| 14 | + S3Sync["S3Syncer"] |
| 15 | + S3 -->|"HeadObject<br/>(ETag check)"| S3Sync |
| 16 | + S3 -->|"GetObject<br/>(if changed)"| S3Sync |
| 17 | + end |
| 18 | +
|
| 19 | + subgraph Storage["In-Memory Storage"] |
| 20 | + direction TB |
| 21 | + HashStore["HashStore<br/>atomic.Pointer"] |
| 22 | + LRU["LRU Cache<br/>(10k entries)"] |
| 23 | + HashStore --> LRU |
| 24 | + end |
| 25 | +
|
| 26 | + subgraph TxProcessing["Transaction Processing"] |
| 27 | + direction TB |
| 28 | + UserTx(("User Tx")) |
| 29 | + ExecEngine["ExecutionEngine"] |
| 30 | + TxFilter["txfilter"] |
| 31 | + Sequencer["Sequencer"] |
| 32 | +
|
| 33 | + UserTx --> ExecEngine |
| 34 | + ExecEngine --> TxFilter |
| 35 | + TxFilter -->|"allowed"| Sequencer |
| 36 | + end |
| 37 | +
|
| 38 | + S3Sync -->|"atomic swap"| HashStore |
| 39 | + TxFilter -.->|"IsRestricted?"| HashStore |
| 40 | +
|
| 41 | + style S3 fill:#f5f5f5,stroke:#424242,stroke-width:2px,color:#212121 |
| 42 | + style S3Sync fill:#fff,stroke:#424242,stroke-width:1px,color:#212121 |
| 43 | + style HashStore fill:#e3f2fd,stroke:#1565c0,stroke-width:2px,color:#0d47a1 |
| 44 | + style LRU fill:#e3f2fd,stroke:#1565c0,stroke-width:1px,color:#0d47a1 |
| 45 | + style UserTx fill:#fff,stroke:#424242,stroke-width:1px,color:#212121 |
| 46 | + style ExecEngine fill:#f5f5f5,stroke:#424242,stroke-width:1px,color:#212121 |
| 47 | + style TxFilter fill:#fff3e0,stroke:#e65100,stroke-width:2px,color:#bf360c |
| 48 | + style Sequencer fill:#f5f5f5,stroke:#424242,stroke-width:1px,color:#212121 |
| 49 | +
|
| 50 | + style DataSync fill:#fafafa,stroke:#9e9e9e,stroke-width:1px,color:#212121 |
| 51 | + style Storage fill:#e3f2fd,stroke:#1565c0,stroke-width:1px,color:#0d47a1 |
| 52 | + style TxProcessing fill:#fafafa,stroke:#9e9e9e,stroke-width:1px,color:#212121 |
| 53 | +``` |
| 54 | + |
| 55 | +## Data Flow |
| 56 | + |
| 57 | +| Flow | Description | |
| 58 | +|------|-------------| |
| 59 | +| **Sync** | S3 → S3Syncer → HashStore (atomic swap on ETag change) | |
| 60 | +| **Lookup** | txfilter → HashStore → LRU cache or `sha256(salt \|\| addr)` hash lookup | |
| 61 | +| **Transaction** | User → ExecutionEngine → txfilter → Sequencer | |
| 62 | + |
| 63 | +## Components |
| 64 | + |
| 65 | +| Component | Role | |
| 66 | +|-----------|------| |
| 67 | +| **Service** | Orchestrates lifecycle: initialization, polling, shutdown | |
| 68 | +| **S3Syncer** | Polls S3 via HeadObject, downloads on ETag change (multipart, 10 concurrent parts) | |
| 69 | +| **HashStore** | Lock-free storage with `atomic.Pointer`, per-snapshot LRU cache (10k entries) | |
| 70 | +| **txfilter** | Blocks transactions touching restricted addresses | |
| 71 | + |
| 72 | +## Package Structure |
| 73 | + |
| 74 | +``` |
| 75 | +restrictedaddr/ |
| 76 | +├── config.go # Configuration struct and validation |
| 77 | +├── service.go # Service lifecycle (Initialize, Start, Stop) |
| 78 | +├── s3_sync.go # S3 polling and concurrent download |
| 79 | +├── hash_store.go # Lock-free hash storage with LRU caching |
| 80 | +└── service_test.go # Unit and integration tests |
| 81 | +``` |
| 82 | + |
| 83 | +## Configuration |
| 84 | + |
| 85 | +| Option | Default | Description | |
| 86 | +|--------|---------|-------------| |
| 87 | +| `--restricted-addr.enable` | `false` | Enable the service | |
| 88 | +| `--restricted-addr.s3-bucket` | - | S3 bucket name (required if enabled) | |
| 89 | +| `--restricted-addr.s3-region` | - | AWS region (required if enabled) | |
| 90 | +| `--restricted-addr.s3-object-key` | - | Path to hash list JSON (required if enabled) | |
| 91 | +| `--restricted-addr.s3-access-key` | - | AWS access key (optional, uses default chain) | |
| 92 | +| `--restricted-addr.s3-secret-key` | - | AWS secret key (optional) | |
| 93 | +| `--restricted-addr.poll-interval` | `5m` | Interval between S3 ETag checks | |
| 94 | + |
| 95 | +## S3 Hash List Format |
| 96 | + |
| 97 | +```json |
| 98 | +{ |
| 99 | + "salt": "hex_encoded_salt", |
| 100 | + "address_hashes": [ |
| 101 | + {"hash": "hex_encoded_32byte_sha256"}, |
| 102 | + {"hash": "hex_encoded_32byte_sha256"} |
| 103 | + ] |
| 104 | +} |
| 105 | +``` |
| 106 | + |
| 107 | +**Hash computation:** `SHA256(salt || address_bytes)` - addresses are never stored in plaintext. |
| 108 | + |
| 109 | +## Service Lifecycle |
| 110 | + |
| 111 | +1. **NewService** - Validates config, creates S3 client |
| 112 | +2. **Initialize** - Blocking initial download (node won't start if this fails) |
| 113 | +3. **Start** - Begins background polling goroutine |
| 114 | +4. **StopAndWait** - Graceful shutdown |
| 115 | + |
| 116 | +The service initializes **early** in the node startup sequence (before inbox tracker, transaction streamer, etc.) to ensure filtering is active before any transactions are processed. |
| 117 | + |
| 118 | +## HashStore Design |
| 119 | + |
| 120 | +- **Lock-free reads:** Uses `atomic.Pointer[hashData]` for concurrent access |
| 121 | +- **Double-buffering:** New data prepared while old data still serves requests |
| 122 | +- **Per-snapshot LRU:** Each atomic swap includes a fresh 10k-entry cache |
| 123 | +- **Lookup methods:** |
| 124 | + - `IsRestricted(addr)` - Single address check |
| 125 | + - `IsAnyRestricted(addrs)` - True if any address restricted |
| 126 | + - `IsAllRestricted(addrs)` - True only if all addresses restricted |
| 127 | + |
| 128 | +## Transaction Filtering Points |
| 129 | + |
| 130 | +Transactions are blocked if any of these addresses are restricted: |
| 131 | + |
| 132 | +| Operation | Addresses Checked | |
| 133 | +|-----------|-------------------| |
| 134 | +| Transfer | Sender, Recipient | |
| 135 | +| CALL | Target contract | |
| 136 | +| STATICCALL | Target contract | |
| 137 | +| CREATE/CREATE2 | New contract address | |
| 138 | +| SELFDESTRUCT | Beneficiary address | |
| 139 | + |
| 140 | +## S3 Polling Strategy |
| 141 | + |
| 142 | +1. **HeadObject** call to check ETag (lightweight, no data transfer) |
| 143 | +2. If ETag unchanged, skip download |
| 144 | +3. If ETag changed: |
| 145 | + - Download to temp file (multipart: 32MB parts, 10 concurrent, 5 retries/part) |
| 146 | + - Parse and validate JSON |
| 147 | + - Atomic pointer swap into HashStore |
| 148 | +4. Repeat after `poll-interval` |
0 commit comments