mirror of https://github.com/ethereum/go-ethereum
eth/catalyst: evict old payloads, type PayloadID (#24236)
* eth/catalyst: evict old payloads, type PayloadID * eth/catalyst: added tracing info to engine api * eth/catalyst: add test for create payload timestamps * catalyst: better logs * eth/catalyst: computePayloadId return style * catalyst: add queue for payloads * eth/catalyst: nitpicks Co-authored-by: Marius van der Wijden <m.vanderwijden@live.de> Co-authored-by: Péter Szilágyi <peterke@gmail.com>pull/24260/head
parent
03aaea11d1
commit
514ae7cfa3
@ -1,36 +0,0 @@ |
|||||||
// Code generated by github.com/fjl/gencodec. DO NOT EDIT.
|
|
||||||
|
|
||||||
package catalyst |
|
||||||
|
|
||||||
import ( |
|
||||||
"encoding/json" |
|
||||||
|
|
||||||
"github.com/ethereum/go-ethereum/common/hexutil" |
|
||||||
) |
|
||||||
|
|
||||||
var _ = (*payloadResponseMarshaling)(nil) |
|
||||||
|
|
||||||
// MarshalJSON marshals as JSON.
|
|
||||||
func (p PayloadResponse) MarshalJSON() ([]byte, error) { |
|
||||||
type PayloadResponse struct { |
|
||||||
PayloadID hexutil.Uint64 `json:"payloadId"` |
|
||||||
} |
|
||||||
var enc PayloadResponse |
|
||||||
enc.PayloadID = hexutil.Uint64(p.PayloadID) |
|
||||||
return json.Marshal(&enc) |
|
||||||
} |
|
||||||
|
|
||||||
// UnmarshalJSON unmarshals from JSON.
|
|
||||||
func (p *PayloadResponse) UnmarshalJSON(input []byte) error { |
|
||||||
type PayloadResponse struct { |
|
||||||
PayloadID *hexutil.Uint64 `json:"payloadId"` |
|
||||||
} |
|
||||||
var dec PayloadResponse |
|
||||||
if err := json.Unmarshal(input, &dec); err != nil { |
|
||||||
return err |
|
||||||
} |
|
||||||
if dec.PayloadID != nil { |
|
||||||
p.PayloadID = uint64(*dec.PayloadID) |
|
||||||
} |
|
||||||
return nil |
|
||||||
} |
|
@ -0,0 +1,74 @@ |
|||||||
|
// Copyright 2022 The go-ethereum Authors
|
||||||
|
// This file is part of the go-ethereum library.
|
||||||
|
//
|
||||||
|
// The go-ethereum library is free software: you can redistribute it and/or modify
|
||||||
|
// it under the terms of the GNU Lesser General Public License as published by
|
||||||
|
// the Free Software Foundation, either version 3 of the License, or
|
||||||
|
// (at your option) any later version.
|
||||||
|
//
|
||||||
|
// The go-ethereum library is distributed in the hope that it will be useful,
|
||||||
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
// GNU Lesser General Public License for more details.
|
||||||
|
//
|
||||||
|
// You should have received a copy of the GNU Lesser General Public License
|
||||||
|
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
|
package catalyst |
||||||
|
|
||||||
|
import "sync" |
||||||
|
|
||||||
|
// maxTrackedPayloads is the maximum number of prepared payloads the execution
|
||||||
|
// engine tracks before evicting old ones. Ideally we should only ever track the
|
||||||
|
// latest one; but have a slight wiggle room for non-ideal conditions.
|
||||||
|
const maxTrackedPayloads = 10 |
||||||
|
|
||||||
|
// payloadQueueItem represents an id->payload tuple to store until it's retrieved
|
||||||
|
// or evicted.
|
||||||
|
type payloadQueueItem struct { |
||||||
|
id PayloadID |
||||||
|
payload *ExecutableDataV1 |
||||||
|
} |
||||||
|
|
||||||
|
// payloadQueue tracks the latest handful of constructed payloads to be retrieved
|
||||||
|
// by the beacon chain if block production is requested.
|
||||||
|
type payloadQueue struct { |
||||||
|
payloads []*payloadQueueItem |
||||||
|
lock sync.RWMutex |
||||||
|
} |
||||||
|
|
||||||
|
// newPayloadQueue creates a pre-initialized queue with a fixed number of slots
|
||||||
|
// all containing empty items.
|
||||||
|
func newPayloadQueue() *payloadQueue { |
||||||
|
return &payloadQueue{ |
||||||
|
payloads: make([]*payloadQueueItem, maxTrackedPayloads), |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
// put inserts a new payload into the queue at the given id.
|
||||||
|
func (q *payloadQueue) put(id PayloadID, data *ExecutableDataV1) { |
||||||
|
q.lock.Lock() |
||||||
|
defer q.lock.Unlock() |
||||||
|
|
||||||
|
copy(q.payloads[1:], q.payloads) |
||||||
|
q.payloads[0] = &payloadQueueItem{ |
||||||
|
id: id, |
||||||
|
payload: data, |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
// get retrieves a previously stored payload item or nil if it does not exist.
|
||||||
|
func (q *payloadQueue) get(id PayloadID) *ExecutableDataV1 { |
||||||
|
q.lock.RLock() |
||||||
|
defer q.lock.RUnlock() |
||||||
|
|
||||||
|
for _, item := range q.payloads { |
||||||
|
if item == nil { |
||||||
|
return nil // no more items
|
||||||
|
} |
||||||
|
if item.id == id { |
||||||
|
return item.payload |
||||||
|
} |
||||||
|
} |
||||||
|
return nil |
||||||
|
} |
Loading…
Reference in new issue