Skip to content

Commit

Permalink
fix BuildBlocksValidator: use bytes buffer pool correctly (#10965)
Browse files Browse the repository at this point in the history
* use bytes buffer poll correctly

* only update res if data is larger

* fix comment about block version

* simplify a bit by shortcut

* op-node: preserve largest bytes buf capacity

---------

Co-authored-by: protolambda <proto@protolambda.com>
  • Loading branch information
zhiqiangxu and protolambda authored Jun 28, 2024
1 parent 9032b60 commit b219b84
Showing 1 changed file with 14 additions and 8 deletions.
22 changes: 14 additions & 8 deletions op-node/p2p/gossip.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,11 @@ func BuildMsgIdFn(cfg *rollup.Config) pubsub.MsgIdFunction {
if err == nil && dLen <= maxGossipSize {
res := msgBufPool.Get().(*[]byte)
defer msgBufPool.Put(res)
if data, err = snappy.Decode((*res)[:0], pmsg.Data); err == nil {
*res = data // if we ended up growing the slice capacity, fine, keep the larger one.
if data, err = snappy.Decode((*res)[:cap(*res)], pmsg.Data); err == nil {
if cap(data) > cap(*res) {
// if we ended up growing the slice capacity, fine, keep the larger one.
*res = data[:cap(data)]
}
valid = true
}
}
Expand Down Expand Up @@ -274,12 +277,15 @@ func BuildBlocksValidator(log log.Logger, cfg *rollup.Config, runCfg GossipRunti

res := msgBufPool.Get().(*[]byte)
defer msgBufPool.Put(res)
data, err := snappy.Decode((*res)[:0], message.Data)
data, err := snappy.Decode((*res)[:cap(*res)], message.Data)
if err != nil {
log.Warn("invalid snappy compression", "err", err, "peer", id)
return pubsub.ValidationReject
}
*res = data // if we ended up growing the slice capacity, fine, keep the larger one.
// if we ended up growing the slice capacity, fine, keep the larger one.
if cap(data) > cap(*res) {
*res = data[:cap(data)]
}

// message starts with compact-encoding secp256k1 encoded signature
signatureBytes, payloadBytes := data[:65], data[65:]
Expand Down Expand Up @@ -336,13 +342,13 @@ func BuildBlocksValidator(log log.Logger, cfg *rollup.Config, runCfg GossipRunti
return pubsub.ValidationReject
}

// [REJECT] if a V2 Block does not have withdrawals
// [REJECT] if a >= V2 Block does not have withdrawals
if blockVersion.HasWithdrawals() && payload.Withdrawals == nil {
log.Warn("payload is on v2/v3 topic, but does not have withdrawals", "bad_hash", payload.BlockHash.String())
return pubsub.ValidationReject
}

// [REJECT] if a V2 Block has non-empty withdrawals
// [REJECT] if a >= V2 Block has non-empty withdrawals
if blockVersion.HasWithdrawals() && len(*payload.Withdrawals) != 0 {
log.Warn("payload is on v2/v3 topic, but has non-empty withdrawals", "bad_hash", payload.BlockHash.String(), "withdrawal_count", len(*payload.Withdrawals))
return pubsub.ValidationReject
Expand All @@ -362,13 +368,13 @@ func BuildBlocksValidator(log log.Logger, cfg *rollup.Config, runCfg GossipRunti

if blockVersion.HasBlobProperties() {
// [REJECT] if the block is on a topic >= V3 and has a blob gas used value that is not zero
if payload.BlobGasUsed == nil || (payload.BlobGasUsed != nil && *payload.BlobGasUsed != 0) {
if payload.BlobGasUsed == nil || *payload.BlobGasUsed != 0 {
log.Warn("payload is on v3 topic, but has non-zero blob gas used", "bad_hash", payload.BlockHash.String(), "blob_gas_used", payload.BlobGasUsed)
return pubsub.ValidationReject
}

// [REJECT] if the block is on a topic >= V3 and has an excess blob gas value that is not zero
if payload.ExcessBlobGas == nil || (payload.ExcessBlobGas != nil && *payload.ExcessBlobGas != 0) {
if payload.ExcessBlobGas == nil || *payload.ExcessBlobGas != 0 {
log.Warn("payload is on v3 topic, but has non-zero excess blob gas", "bad_hash", payload.BlockHash.String(), "excess_blob_gas", payload.ExcessBlobGas)
return pubsub.ValidationReject
}
Expand Down

0 comments on commit b219b84

Please sign in to comment.