les: generate random nums directly, not via strange conversions

This commit is contained in:
Péter Szilágyi 2021-05-21 12:36:04 +03:00
parent 81662fe827
commit 835fe06f1d
No known key found for this signature in database
GPG Key ID: E9AE538CEDF8293D
5 changed files with 10 additions and 16 deletions

View File

@ -19,6 +19,7 @@ package les
import ( import (
"context" "context"
"math/big" "math/big"
"math/rand"
"sync" "sync"
"sync/atomic" "sync/atomic"
"time" "time"
@ -388,7 +389,7 @@ func (pc *peerConnection) RequestHeadersByHash(origin common.Hash, amount int, s
return dp.(*serverPeer) == pc.peer return dp.(*serverPeer) == pc.peer
}, },
request: func(dp distPeer) func() { request: func(dp distPeer) func() {
reqID := genReqID() reqID := rand.Uint64()
peer := dp.(*serverPeer) peer := dp.(*serverPeer)
cost := peer.getRequestCost(GetBlockHeadersMsg, amount) cost := peer.getRequestCost(GetBlockHeadersMsg, amount)
peer.fcServer.QueuedRequest(reqID, cost) peer.fcServer.QueuedRequest(reqID, cost)
@ -412,7 +413,7 @@ func (pc *peerConnection) RequestHeadersByNumber(origin uint64, amount int, skip
return dp.(*serverPeer) == pc.peer return dp.(*serverPeer) == pc.peer
}, },
request: func(dp distPeer) func() { request: func(dp distPeer) func() {
reqID := genReqID() reqID := rand.Uint64()
peer := dp.(*serverPeer) peer := dp.(*serverPeer)
cost := peer.getRequestCost(GetBlockHeadersMsg, amount) cost := peer.getRequestCost(GetBlockHeadersMsg, amount)
peer.fcServer.QueuedRequest(reqID, cost) peer.fcServer.QueuedRequest(reqID, cost)
@ -429,7 +430,7 @@ func (pc *peerConnection) RequestHeadersByNumber(origin uint64, amount int, skip
// RetrieveSingleHeaderByNumber requests a single header by the specified block // RetrieveSingleHeaderByNumber requests a single header by the specified block
// number. This function will wait the response until it's timeout or delivered. // number. This function will wait the response until it's timeout or delivered.
func (pc *peerConnection) RetrieveSingleHeaderByNumber(context context.Context, number uint64) (*types.Header, error) { func (pc *peerConnection) RetrieveSingleHeaderByNumber(context context.Context, number uint64) (*types.Header, error) {
reqID := genReqID() reqID := rand.Uint64()
rq := &distReq{ rq := &distReq{
getCost: func(dp distPeer) uint64 { getCost: func(dp distPeer) uint64 {
peer := dp.(*serverPeer) peer := dp.(*serverPeer)

View File

@ -507,7 +507,7 @@ func (f *lightFetcher) requestHeaderByHash(peerid enode.ID) func(common.Hash) er
getCost: func(dp distPeer) uint64 { return dp.(*serverPeer).getRequestCost(GetBlockHeadersMsg, 1) }, getCost: func(dp distPeer) uint64 { return dp.(*serverPeer).getRequestCost(GetBlockHeadersMsg, 1) },
canSend: func(dp distPeer) bool { return dp.(*serverPeer).ID() == peerid }, canSend: func(dp distPeer) bool { return dp.(*serverPeer).ID() == peerid },
request: func(dp distPeer) func() { request: func(dp distPeer) func() {
peer, id := dp.(*serverPeer), genReqID() peer, id := dp.(*serverPeer), rand.Uint64()
cost := peer.getRequestCost(GetBlockHeadersMsg, 1) cost := peer.getRequestCost(GetBlockHeadersMsg, 1)
peer.fcServer.QueuedRequest(id, cost) peer.fcServer.QueuedRequest(id, cost)

View File

@ -18,6 +18,7 @@ package les
import ( import (
"context" "context"
"math/rand"
"sort" "sort"
"time" "time"
@ -156,7 +157,7 @@ func (odr *LesOdr) RetrieveTxStatus(ctx context.Context, req *light.TxStatusRequ
var ( var (
// Deep copy the request, so that the partial result won't be mixed. // Deep copy the request, so that the partial result won't be mixed.
req = &TxStatusRequest{Hashes: req.Hashes} req = &TxStatusRequest{Hashes: req.Hashes}
id = genReqID() id = rand.Uint64()
distreq = &distReq{ distreq = &distReq{
getCost: func(dp distPeer) uint64 { return req.GetCost(dp.(*serverPeer)) }, getCost: func(dp distPeer) uint64 { return req.GetCost(dp.(*serverPeer)) },
canSend: func(dp distPeer) bool { return canSend[dp.(*serverPeer).id] }, canSend: func(dp distPeer) bool { return canSend[dp.(*serverPeer).id] },
@ -200,7 +201,7 @@ func (odr *LesOdr) RetrieveTxStatus(ctx context.Context, req *light.TxStatusRequ
func (odr *LesOdr) Retrieve(ctx context.Context, req light.OdrRequest) (err error) { func (odr *LesOdr) Retrieve(ctx context.Context, req light.OdrRequest) (err error) {
lreq := LesRequest(req) lreq := LesRequest(req)
reqID := genReqID() reqID := rand.Uint64()
rq := &distReq{ rq := &distReq{
getCost: func(dp distPeer) uint64 { getCost: func(dp distPeer) uint64 {
return lreq.GetCost(dp.(*serverPeer)) return lreq.GetCost(dp.(*serverPeer))

View File

@ -18,8 +18,6 @@ package les
import ( import (
"context" "context"
"crypto/rand"
"encoding/binary"
"fmt" "fmt"
"sync" "sync"
"time" "time"
@ -430,10 +428,3 @@ func (r *sentReq) stop(err error) {
func (r *sentReq) getError() error { func (r *sentReq) getError() error {
return r.err return r.err
} }
// genReqID generates a new random request ID
func genReqID() uint64 {
var rnd [8]byte
rand.Read(rnd[:])
return binary.BigEndian.Uint64(rnd[:])
}

View File

@ -18,6 +18,7 @@ package les
import ( import (
"context" "context"
"math/rand"
"sync" "sync"
"github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/common"
@ -117,7 +118,7 @@ func (ltrx *lesTxRelay) send(txs types.Transactions, count int) {
ll := list ll := list
enc, _ := rlp.EncodeToBytes(ll) enc, _ := rlp.EncodeToBytes(ll)
reqID := genReqID() reqID := rand.Uint64()
rq := &distReq{ rq := &distReq{
getCost: func(dp distPeer) uint64 { getCost: func(dp distPeer) uint64 {
peer := dp.(*serverPeer) peer := dp.(*serverPeer)