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 585c922
Showing 1 changed file with 14 additions and 7 deletions.
21 changes: 14 additions & 7 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,15 +799,15 @@ 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})
Expand Down

0 comments on commit 585c922

Please sign in to comment.