|
| 1 | +package flashblocks |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "encoding/json" |
| 6 | + "fmt" |
| 7 | + "net/http" |
| 8 | + "sync" |
| 9 | + "time" |
| 10 | + |
| 11 | + "github.com/base/base-bench/runner/clients/types" |
| 12 | + "github.com/ethereum/go-ethereum/log" |
| 13 | + "github.com/gorilla/websocket" |
| 14 | +) |
| 15 | + |
| 16 | +// ReplayServer replays flashblock payloads to connected clients via websocket. |
| 17 | +type ReplayServer struct { |
| 18 | + log log.Logger |
| 19 | + port uint64 |
| 20 | + flashblocks []types.FlashblocksPayloadV1 |
| 21 | + blockTime time.Duration |
| 22 | + |
| 23 | + server *http.Server |
| 24 | + upgrader websocket.Upgrader |
| 25 | + |
| 26 | + mu sync.RWMutex |
| 27 | + connections []*websocket.Conn |
| 28 | + started bool |
| 29 | + stopChan chan struct{} |
| 30 | + stopOnce sync.Once |
| 31 | +} |
| 32 | + |
| 33 | +func NewReplayServer(log log.Logger, port uint64, flashblocks []types.FlashblocksPayloadV1, blockTime time.Duration) *ReplayServer { |
| 34 | + return &ReplayServer{ |
| 35 | + log: log, |
| 36 | + port: port, |
| 37 | + flashblocks: flashblocks, |
| 38 | + blockTime: blockTime, |
| 39 | + upgrader: websocket.Upgrader{ |
| 40 | + CheckOrigin: func(r *http.Request) bool { return true }, |
| 41 | + }, |
| 42 | + connections: make([]*websocket.Conn, 0), |
| 43 | + stopChan: make(chan struct{}), |
| 44 | + } |
| 45 | +} |
| 46 | + |
| 47 | +func (s *ReplayServer) URL() string { |
| 48 | + return fmt.Sprintf("ws://127.0.0.1:%d", s.port) |
| 49 | +} |
| 50 | + |
| 51 | +func (s *ReplayServer) Start(ctx context.Context) error { |
| 52 | + s.mu.Lock() |
| 53 | + if s.started { |
| 54 | + s.mu.Unlock() |
| 55 | + return fmt.Errorf("server already started") |
| 56 | + } |
| 57 | + s.started = true |
| 58 | + s.mu.Unlock() |
| 59 | + |
| 60 | + mux := http.NewServeMux() |
| 61 | + mux.HandleFunc("/", s.handleWebSocket) |
| 62 | + |
| 63 | + s.server = &http.Server{ |
| 64 | + Addr: fmt.Sprintf(":%d", s.port), |
| 65 | + Handler: mux, |
| 66 | + } |
| 67 | + |
| 68 | + go func() { |
| 69 | + s.log.Info("Starting flashblock replay server", "port", s.port) |
| 70 | + if err := s.server.ListenAndServe(); err != nil && err != http.ErrServerClosed { |
| 71 | + s.log.Error("Flashblock replay server error", "err", err) |
| 72 | + } |
| 73 | + }() |
| 74 | + |
| 75 | + time.Sleep(100 * time.Millisecond) |
| 76 | + return nil |
| 77 | +} |
| 78 | + |
| 79 | +func (s *ReplayServer) handleWebSocket(w http.ResponseWriter, r *http.Request) { |
| 80 | + conn, err := s.upgrader.Upgrade(w, r, nil) |
| 81 | + if err != nil { |
| 82 | + s.log.Error("Failed to upgrade websocket connection", "err", err) |
| 83 | + return |
| 84 | + } |
| 85 | + |
| 86 | + s.mu.Lock() |
| 87 | + s.connections = append(s.connections, conn) |
| 88 | + s.mu.Unlock() |
| 89 | + |
| 90 | + s.log.Info("New client connected to flashblock replay server") |
| 91 | + |
| 92 | + for { |
| 93 | + select { |
| 94 | + case <-s.stopChan: |
| 95 | + return |
| 96 | + default: |
| 97 | + if _, _, err := conn.ReadMessage(); err != nil { |
| 98 | + s.removeConnection(conn) |
| 99 | + return |
| 100 | + } |
| 101 | + } |
| 102 | + } |
| 103 | +} |
| 104 | + |
| 105 | +func (s *ReplayServer) removeConnection(conn *websocket.Conn) { |
| 106 | + s.mu.Lock() |
| 107 | + defer s.mu.Unlock() |
| 108 | + |
| 109 | + for i, c := range s.connections { |
| 110 | + if c == conn { |
| 111 | + s.connections = append(s.connections[:i], s.connections[i+1:]...) |
| 112 | + _ = conn.Close() |
| 113 | + s.log.Debug("Client disconnected from flashblock replay server") |
| 114 | + return |
| 115 | + } |
| 116 | + } |
| 117 | +} |
| 118 | + |
| 119 | +// ReplayFlashblocks replays flashblocks to connected clients at evenly spaced intervals. |
| 120 | +func (s *ReplayServer) ReplayFlashblocks(ctx context.Context) error { |
| 121 | + if len(s.flashblocks) == 0 { |
| 122 | + s.log.Info("No flashblocks to replay") |
| 123 | + return nil |
| 124 | + } |
| 125 | + |
| 126 | + blockGroups := s.groupFlashblocksByBlock() |
| 127 | + |
| 128 | + s.log.Info("Starting flashblock replay", |
| 129 | + "total_flashblocks", len(s.flashblocks), |
| 130 | + "num_blocks", len(blockGroups), |
| 131 | + ) |
| 132 | + |
| 133 | + for blockNum, flashblocks := range blockGroups { |
| 134 | + select { |
| 135 | + case <-ctx.Done(): |
| 136 | + return ctx.Err() |
| 137 | + default: |
| 138 | + } |
| 139 | + |
| 140 | + if len(flashblocks) == 0 { |
| 141 | + continue |
| 142 | + } |
| 143 | + |
| 144 | + interval := s.blockTime / time.Duration(len(flashblocks)+1) |
| 145 | + |
| 146 | + s.log.Debug("Replaying flashblocks for block", |
| 147 | + "block_number", blockNum, |
| 148 | + "num_flashblocks", len(flashblocks), |
| 149 | + "interval", interval, |
| 150 | + ) |
| 151 | + |
| 152 | + for i, flashblock := range flashblocks { |
| 153 | + select { |
| 154 | + case <-ctx.Done(): |
| 155 | + return ctx.Err() |
| 156 | + default: |
| 157 | + } |
| 158 | + |
| 159 | + time.Sleep(interval) |
| 160 | + |
| 161 | + if err := s.broadcastFlashblock(flashblock); err != nil { |
| 162 | + s.log.Warn("Error broadcasting flashblock", "err", err, "index", i) |
| 163 | + } |
| 164 | + } |
| 165 | + |
| 166 | + remainingTime := s.blockTime - interval*time.Duration(len(flashblocks)) |
| 167 | + if remainingTime > 0 { |
| 168 | + time.Sleep(remainingTime) |
| 169 | + } |
| 170 | + } |
| 171 | + |
| 172 | + s.log.Info("Flashblock replay complete") |
| 173 | + return nil |
| 174 | +} |
| 175 | + |
| 176 | +// groupFlashblocksByBlock groups flashblocks by block number, sorted by index. |
| 177 | +func (s *ReplayServer) groupFlashblocksByBlock() map[uint64][]types.FlashblocksPayloadV1 { |
| 178 | + groups := make(map[uint64][]types.FlashblocksPayloadV1) |
| 179 | + |
| 180 | + // Build PayloadID -> blockNum mapping from flashblocks with Base |
| 181 | + payloadIDToBlockNum := make(map[types.PayloadID]uint64) |
| 182 | + for _, fb := range s.flashblocks { |
| 183 | + if fb.Base != nil { |
| 184 | + payloadIDToBlockNum[fb.PayloadID] = uint64(fb.Base.BlockNumber) |
| 185 | + } |
| 186 | + } |
| 187 | + |
| 188 | + for _, fb := range s.flashblocks { |
| 189 | + var blockNum uint64 |
| 190 | + if fb.Base != nil { |
| 191 | + blockNum = uint64(fb.Base.BlockNumber) |
| 192 | + } else if bn, ok := payloadIDToBlockNum[fb.PayloadID]; ok { |
| 193 | + blockNum = bn |
| 194 | + } |
| 195 | + groups[blockNum] = append(groups[blockNum], fb) |
| 196 | + } |
| 197 | + |
| 198 | + for blockNum := range groups { |
| 199 | + sortByIndex(groups[blockNum]) |
| 200 | + } |
| 201 | + |
| 202 | + return groups |
| 203 | +} |
| 204 | + |
| 205 | +func sortByIndex(flashblocks []types.FlashblocksPayloadV1) { |
| 206 | + for i := 1; i < len(flashblocks); i++ { |
| 207 | + j := i |
| 208 | + for j > 0 && flashblocks[j-1].Index > flashblocks[j].Index { |
| 209 | + flashblocks[j-1], flashblocks[j] = flashblocks[j], flashblocks[j-1] |
| 210 | + j-- |
| 211 | + } |
| 212 | + } |
| 213 | +} |
| 214 | + |
| 215 | +func (s *ReplayServer) broadcastFlashblock(flashblock types.FlashblocksPayloadV1) error { |
| 216 | + data, err := json.Marshal(flashblock) |
| 217 | + if err != nil { |
| 218 | + return fmt.Errorf("failed to marshal flashblock: %w", err) |
| 219 | + } |
| 220 | + |
| 221 | + s.mu.RLock() |
| 222 | + connections := make([]*websocket.Conn, len(s.connections)) |
| 223 | + copy(connections, s.connections) |
| 224 | + s.mu.RUnlock() |
| 225 | + |
| 226 | + var lastErr error |
| 227 | + for _, conn := range connections { |
| 228 | + if err := conn.WriteMessage(websocket.TextMessage, data); err != nil { |
| 229 | + s.log.Warn("Failed to send flashblock to client", "err", err) |
| 230 | + lastErr = err |
| 231 | + } |
| 232 | + } |
| 233 | + |
| 234 | + s.log.Debug("Broadcasted flashblock", |
| 235 | + "payload_id", fmt.Sprintf("%x", flashblock.PayloadID), |
| 236 | + "index", flashblock.Index, |
| 237 | + "num_clients", len(connections), |
| 238 | + ) |
| 239 | + |
| 240 | + return lastErr |
| 241 | +} |
| 242 | + |
| 243 | +// Stop stops the server. Safe to call multiple times. |
| 244 | +func (s *ReplayServer) Stop() error { |
| 245 | + var stopErr error |
| 246 | + |
| 247 | + s.stopOnce.Do(func() { |
| 248 | + s.mu.Lock() |
| 249 | + if !s.started { |
| 250 | + s.mu.Unlock() |
| 251 | + return |
| 252 | + } |
| 253 | + s.started = false |
| 254 | + s.mu.Unlock() |
| 255 | + |
| 256 | + s.log.Info("Stopping flashblock replay server") |
| 257 | + |
| 258 | + close(s.stopChan) |
| 259 | + |
| 260 | + s.mu.Lock() |
| 261 | + for _, conn := range s.connections { |
| 262 | + _ = conn.WriteMessage(websocket.CloseMessage, websocket.FormatCloseMessage(websocket.CloseNormalClosure, "")) |
| 263 | + _ = conn.Close() |
| 264 | + } |
| 265 | + s.connections = nil |
| 266 | + s.mu.Unlock() |
| 267 | + |
| 268 | + ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) |
| 269 | + defer cancel() |
| 270 | + |
| 271 | + if s.server != nil { |
| 272 | + if err := s.server.Shutdown(ctx); err != nil { |
| 273 | + s.log.Warn("Error shutting down flashblock replay server", "err", err) |
| 274 | + stopErr = err |
| 275 | + return |
| 276 | + } |
| 277 | + } |
| 278 | + |
| 279 | + s.log.Info("Flashblock replay server stopped") |
| 280 | + }) |
| 281 | + |
| 282 | + return stopErr |
| 283 | +} |
| 284 | + |
| 285 | +func (s *ReplayServer) WaitForConnection(ctx context.Context, timeout time.Duration) error { |
| 286 | + deadline := time.Now().Add(timeout) |
| 287 | + |
| 288 | + for time.Now().Before(deadline) { |
| 289 | + select { |
| 290 | + case <-ctx.Done(): |
| 291 | + return ctx.Err() |
| 292 | + default: |
| 293 | + } |
| 294 | + |
| 295 | + s.mu.RLock() |
| 296 | + numConnections := len(s.connections) |
| 297 | + s.mu.RUnlock() |
| 298 | + |
| 299 | + if numConnections > 0 { |
| 300 | + return nil |
| 301 | + } |
| 302 | + |
| 303 | + time.Sleep(100 * time.Millisecond) |
| 304 | + } |
| 305 | + |
| 306 | + return fmt.Errorf("timeout waiting for client connection") |
| 307 | +} |
0 commit comments