Skip to content

Commit

Permalink
Use constants for ResultCode
Browse files Browse the repository at this point in the history
  • Loading branch information
bitwiseguy committed Apr 16, 2024
1 parent 1feada6 commit c2e27fd
Showing 1 changed file with 15 additions and 8 deletions.
23 changes: 15 additions & 8 deletions op-node/p2p/sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,13 @@ const (
clientErrRateCost = peerServerBlocksBurst
)

const (
ResultCodeSuccess byte = 0
ResultCodeNotFoundErr byte = 1
ResultCodeInvalidErr byte = 2
ResultCodeUnknownErr byte = 3
)

func PayloadByNumberProtocolID(l2ChainID *big.Int) protocol.ID {
return protocol.ID(fmt.Sprintf("/opstack/req/payload_by_number/%d/0", l2ChainID))
}
Expand Down Expand Up @@ -574,17 +581,17 @@ func (s *SyncClient) peerLoop(ctx context.Context, id peer.ID) {
// and this is the only loop over this peer, so we can request now.
start := time.Now()

resultCode := byte(0)
resultCode := ResultCodeSuccess
err := s.doRequest(ctx, id, pr.num)
if err != nil {
s.inFlight.delete(pr.num)
log.Warn("failed p2p sync request", "num", pr.num, "err", err)
resultCode = 1
resultCode = ResultCodeNotFoundErr
sendResponseError := true

if re, ok := err.(requestResultErr); ok {
resultCode = re.ResultCode()
if re.ResultCode() == 1 { // indicates block not found error
if resultCode == ResultCodeNotFoundErr {
log.Warn("cancelling p2p sync range request", "rangeReqId", pr.rangeReqId)
s.activeRangeRequestsMu.Lock()
delete(s.activeRangeRequests, pr.rangeReqId)
Expand Down Expand Up @@ -792,22 +799,22 @@ func (srv *ReqRespServer) HandleSyncRequest(ctx context.Context, log log.Logger,
req, err := srv.handleSyncRequest(ctx, stream)
cancel()

resultCode := byte(0)
resultCode := ResultCodeSuccess
if err != nil {
log.Warn("failed to serve p2p sync request", "req", req, "err", err)
if errors.Is(err, ethereum.NotFound) {
resultCode = 1
resultCode = ResultCodeNotFoundErr
} else if errors.Is(err, invalidRequestErr) {
resultCode = 2
resultCode = ResultCodeInvalidErr
} else {
resultCode = 3
resultCode = ResultCodeUnknownErr
}
// try to write error code, so the other peer can understand the reason for failure.
_, _ = stream.Write([]byte{resultCode})
} else {
log.Debug("successfully served sync response", "req", req)
}
srv.metrics.ServerPayloadByNumberEvent(req, 0, time.Since(start))
srv.metrics.ServerPayloadByNumberEvent(req, resultCode, time.Since(start))
}

var invalidRequestErr = errors.New("invalid request")
Expand Down

0 comments on commit c2e27fd

Please sign in to comment.