Skip to content

Commit

Permalink
server: safety commit
Browse files Browse the repository at this point in the history
  • Loading branch information
MariusVanDerWijden committed May 7, 2024
1 parent fbcd1e9 commit d169095
Showing 1 changed file with 17 additions and 11 deletions.
28 changes: 17 additions & 11 deletions server/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -545,13 +545,17 @@ func (m *BoostService) processCapellaPayload(w http.ResponseWriter, req *http.Re
// Prepare the request context, which will be cancelled after the first successful response from a relay
requestCtx, requestCtxCancel := context.WithCancel(context.Background())
defer requestCtxCancel()

resultCh := make(chan *builderApi.VersionedSubmitBlindedBlockResponse, len(m.relays))
var received atomic.Bool
go func() {
// Make sure we receive a response within the timeout
time.Sleep(m.httpClientGetPayload.Timeout * time.Duration(m.requestMaxRetries+1))
resultCh <- nil
}()

var wg sync.WaitGroup
for _, relay := range m.relays {
wg.Add(1)
go func(relay types.RelayEntry) {
defer wg.Done()
url := relay.GetURI(params.PathGetPayload)
log := log.WithField("url", url)
log.Debug("calling getPayload")
Expand Down Expand Up @@ -596,14 +600,16 @@ func (m *BoostService) processCapellaPayload(w http.ResponseWriter, req *http.Re

// Received successful response. Try to cancel other requests and return immediately
requestCtxCancel()
resultCh <- responsePayload
log.Info("received payload from relay")
if received.CompareAndSwap(false, true) {
resultCh <- responsePayload
log.Info("received payload from relay")
} else {
log.Trace("Discarding response, already received a correct response")
}
}(relay)
}

// Wait for all requests to complete...
wg.Wait()
close(resultCh)
// Wait for the first request to complete
result := <-resultCh

// If no payload has been received from relay, log loudly about withholding!
Expand Down Expand Up @@ -731,11 +737,11 @@ func (m *BoostService) processDenebPayload(w http.ResponseWriter, req *http.Requ
}

requestCtxCancel()
if received.CompareAndSwap(true, true) {
log.Trace("Discarding response, already received a correct response")
} else {
if received.CompareAndSwap(false, true) {
resultCh <- responsePayload
log.Info("received payload from relay")
} else {
log.Trace("Discarding response, already received a correct response")
}
}(relay)
}
Expand Down

0 comments on commit d169095

Please sign in to comment.