Skip to content

Commit

Permalink
Use crypto/rand instead of math/rand for randomReqId
Browse files Browse the repository at this point in the history
  • Loading branch information
bitwiseguy committed Apr 9, 2024
1 parent 201d280 commit 5d67d73
Showing 1 changed file with 10 additions and 3 deletions.
13 changes: 10 additions & 3 deletions op-node/p2p/sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@ package p2p
import (
"bytes"
"context"
"crypto/rand"
"encoding/binary"
"errors"
"fmt"
"io"
"math"
"math/big"
"math/rand"
"sync"
"sync/atomic"
"time"
Expand Down Expand Up @@ -395,8 +396,14 @@ func (s *SyncClient) onRangeRequest(ctx context.Context, req rangeRequest) {
}
}

// Create shared reqId so associated peerRequests can all be cancelled by setting a single flag
randomReqId := rand.Uint64()
// Create shared randomReqId so associated peerRequests can all be cancelled by setting a single flag
bigMax := new(big.Int).SetUint64(math.MaxUint64)
randomBigInt, err := rand.Int(rand.Reader, bigMax)
if err != nil {
log.Error("failed to generate randomReqId for range request")
return
}
randomReqId := randomBigInt.Uint64()
s.activeRangeRequests[randomReqId] = true

// Now try to fetch lower numbers than current end, to traverse back towards the updated start.
Expand Down

0 comments on commit 5d67d73

Please sign in to comment.