github.com/hardtosaygoodbye/go-ethereum@v1.10.16-0.20220122011429-97003b9e6c15/eth/catalyst/queue.go (about)

     1  // Copyright 2022 The go-ethereum Authors
     2  // This file is part of the go-ethereum library.
     3  //
     4  // The go-ethereum library is free software: you can redistribute it and/or modify
     5  // it under the terms of the GNU Lesser General Public License as published by
     6  // the Free Software Foundation, either version 3 of the License, or
     7  // (at your option) any later version.
     8  //
     9  // The go-ethereum library is distributed in the hope that it will be useful,
    10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    12  // GNU Lesser General Public License for more details.
    13  //
    14  // You should have received a copy of the GNU Lesser General Public License
    15  // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
    16  
    17  package catalyst
    18  
    19  import "sync"
    20  
    21  // maxTrackedPayloads is the maximum number of prepared payloads the execution
    22  // engine tracks before evicting old ones. Ideally we should only ever track the
    23  // latest one; but have a slight wiggle room for non-ideal conditions.
    24  const maxTrackedPayloads = 10
    25  
    26  // payloadQueueItem represents an id->payload tuple to store until it's retrieved
    27  // or evicted.
    28  type payloadQueueItem struct {
    29  	id      PayloadID
    30  	payload *ExecutableDataV1
    31  }
    32  
    33  // payloadQueue tracks the latest handful of constructed payloads to be retrieved
    34  // by the beacon chain if block production is requested.
    35  type payloadQueue struct {
    36  	payloads []*payloadQueueItem
    37  	lock     sync.RWMutex
    38  }
    39  
    40  // newPayloadQueue creates a pre-initialized queue with a fixed number of slots
    41  // all containing empty items.
    42  func newPayloadQueue() *payloadQueue {
    43  	return &payloadQueue{
    44  		payloads: make([]*payloadQueueItem, maxTrackedPayloads),
    45  	}
    46  }
    47  
    48  // put inserts a new payload into the queue at the given id.
    49  func (q *payloadQueue) put(id PayloadID, data *ExecutableDataV1) {
    50  	q.lock.Lock()
    51  	defer q.lock.Unlock()
    52  
    53  	copy(q.payloads[1:], q.payloads)
    54  	q.payloads[0] = &payloadQueueItem{
    55  		id:      id,
    56  		payload: data,
    57  	}
    58  }
    59  
    60  // get retrieves a previously stored payload item or nil if it does not exist.
    61  func (q *payloadQueue) get(id PayloadID) *ExecutableDataV1 {
    62  	q.lock.RLock()
    63  	defer q.lock.RUnlock()
    64  
    65  	for _, item := range q.payloads {
    66  		if item == nil {
    67  			return nil // no more items
    68  		}
    69  		if item.id == id {
    70  			return item.payload
    71  		}
    72  	}
    73  	return nil
    74  }