Skip to content

Commit

Permalink
Nil pointer fix (#415)
Browse files Browse the repository at this point in the history
  • Loading branch information
blxdyx authored Jun 21, 2024
1 parent cc88640 commit a533d02
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 24 deletions.
4 changes: 2 additions & 2 deletions p2p/sentry/sentry_multi_client/sentry_multi_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -528,10 +528,10 @@ func (cs *MultiClient) newBlock66(ctx context.Context, inreq *proto_sentry.Inbou
return fmt.Errorf("decode 4 NewBlockMsg: %w", err)
}
if err := request.SanityCheck(); err != nil {
return fmt.Errorf("newBlock66: %w", err)
return fmt.Errorf("newBlock66: %w, PeerID: %s", err, fmt.Sprintf("%x", sentry.ConvertH512ToPeerID(inreq.PeerId))[:8])
}
if err := request.Block.HashCheck(); err != nil {
return fmt.Errorf("newBlock66: %w", err)
return fmt.Errorf("newBlock66: %w, PeerID: %s", err, fmt.Sprintf("%x", sentry.ConvertH512ToPeerID(inreq.PeerId))[:8])
}

if request.Sidecars != nil && len(request.Sidecars) > 0 {
Expand Down
8 changes: 6 additions & 2 deletions turbo/rpchelper/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,9 @@ func _GetBlockNumber(requireCanonical bool, blockNrOrHash rpc.BlockNumberOrHash,
if fs := parliafinality.GetFinalizationService(); fs != nil {
blockHash := fs.GetFinalizeBlockHash()
blockNum := rawdb.ReadHeaderNumber(tx, blockHash)
return *blockNum, blockHash, false, nil
if blockNum != nil {
return *blockNum, blockHash, false, nil
}
}
blockNumber, err = GetFinalizedBlockNumber(tx)
if err != nil {
Expand All @@ -82,7 +84,9 @@ func _GetBlockNumber(requireCanonical bool, blockNrOrHash rpc.BlockNumberOrHash,
if fs := parliafinality.GetFinalizationService(); fs != nil {
blockHash := fs.GetSafeBlockHash()
blockNum := rawdb.ReadHeaderNumber(tx, blockHash)
return *blockNum, blockHash, false, nil
if blockNum != nil {
return *blockNum, blockHash, false, nil
}
}
blockNumber, err = GetSafeBlockNumber(tx)
if err != nil {
Expand Down
31 changes: 15 additions & 16 deletions turbo/stages/bodydownload/body_algos.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func (bd *BodyDownload) UpdateFromDb(db kv.Tx) (headHeight, headTime uint64, hea
bd.maxProgress = headerProgress + 1
// Resetting for requesting a new range of blocks
bd.requestedLow = bodyProgress + 1
bd.requestedMap = make(map[TripleHash]uint64)
bd.requestedMap = make(map[BodyHashes]uint64)
bd.delivered.Clear()
bd.deliveredCount = 0
bd.wastedCount = 0
Expand Down Expand Up @@ -164,15 +164,13 @@ func (bd *BodyDownload) RequestMoreBodies(tx kv.RwTx, blockReader services.FullB
}
}
if request {
var tripleHash TripleHash
copy(tripleHash[:], header.UncleHash.Bytes())
copy(tripleHash[length.Hash:], header.TxHash.Bytes())
var bodyHashes BodyHashes
copy(bodyHashes[:], header.UncleHash.Bytes())
copy(bodyHashes[length.Hash:], header.TxHash.Bytes())
if header.WithdrawalsHash != nil {
copy(tripleHash[2*length.Hash:], header.WithdrawalsHash.Bytes())
} else {
copy(tripleHash[2*length.Hash:], types.EmptyRootHash.Bytes())
copy(bodyHashes[2*length.Hash:], header.WithdrawalsHash.Bytes())
}
bd.requestedMap[tripleHash] = blockNum
bd.requestedMap[bodyHashes] = blockNum
blockNums = append(blockNums, blockNum)
hashes = append(hashes, hash)
} else {
Expand Down Expand Up @@ -307,18 +305,19 @@ Loop:
txs, uncles, withdrawals, lenOfP2PMessage, sidecars := delivery.txs, delivery.uncles, delivery.withdrawals, delivery.lenOfP2PMessage, delivery.sidecars

for i := range txs {
var bodyHashes BodyHashes
uncleHash := types.CalcUncleHash(uncles[i])
copy(bodyHashes[:], uncleHash.Bytes())
txHash := types.DeriveSha(RawTransactions(txs[i]))
withdrawalsHash := types.DeriveSha(withdrawals[i])

var tripleHash TripleHash
copy(tripleHash[:], uncleHash.Bytes())
copy(tripleHash[length.Hash:], txHash.Bytes())
copy(tripleHash[2*length.Hash:], withdrawalsHash.Bytes())
copy(bodyHashes[length.Hash:], txHash.Bytes())
if withdrawals[i] != nil {
withdrawalsHash := types.DeriveSha(withdrawals[i])
copy(bodyHashes[2*length.Hash:], withdrawalsHash.Bytes())
}

// Block numbers are added to the bd.delivered bitmap here, only for blocks for which the body has been received, and their double hashes are present in the bd.requestedMap
// Also, block numbers can be added to bd.delivered for empty blocks, above
blockNum, ok := bd.requestedMap[tripleHash]
blockNum, ok := bd.requestedMap[bodyHashes]
if !ok {
undelivered++
continue
Expand All @@ -329,7 +328,7 @@ Loop:
toClean[blockNum] = struct{}{}
}
}
delete(bd.requestedMap, tripleHash) // Delivered, cleaning up
delete(bd.requestedMap, bodyHashes) // Delivered, cleaning up

bd.addBodyToCache(blockNum, &types.RawBody{Transactions: txs[i], Uncles: uncles[i], Withdrawals: withdrawals[i], Sidecars: sidecars[i]})
bd.delivered.Add(blockNum)
Expand Down
8 changes: 4 additions & 4 deletions turbo/stages/bodydownload/body_data_struct.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ import (
"github.com/ledgerwatch/erigon/core/types"
)

// TripleHash is type to be used for the mapping between TxHash, UncleHash, and WithdrawalsHash to the block header
type TripleHash [3 * length.Hash]byte
// BodyHashes is type to be used for the mapping between TxHash, UncleHash, and WithdrawalsHash to the block header
type BodyHashes [3 * length.Hash]byte

const MaxBodiesInRequest = 1024

Expand All @@ -36,7 +36,7 @@ type BodyTreeItem struct {
// BodyDownload represents the state of body downloading process
type BodyDownload struct {
peerMap map[[64]byte]int
requestedMap map[TripleHash]uint64
requestedMap map[BodyHashes]uint64
DeliveryNotify chan struct{}
deliveryCh chan Delivery
Engine consensus.Engine
Expand Down Expand Up @@ -67,7 +67,7 @@ type BodyRequest struct {
// NewBodyDownload create a new body download state object
func NewBodyDownload(engine consensus.Engine, blockBufferSize, bodyCacheLimit int, br services.FullBlockReader, logger log.Logger) *BodyDownload {
bd := &BodyDownload{
requestedMap: make(map[TripleHash]uint64),
requestedMap: make(map[BodyHashes]uint64),
bodyCacheLimit: bodyCacheLimit,
delivered: roaring64.New(),
deliveriesH: make(map[uint64]*types.Header),
Expand Down

0 comments on commit a533d02

Please sign in to comment.