github.com/ethereum/go-ethereum@v1.16.1/core/txpool/blobpool/lookup.go (about)

     1  // Copyright 2024 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 blobpool
    18  
    19  import (
    20  	"github.com/ethereum/go-ethereum/common"
    21  )
    22  
    23  type txMetadata struct {
    24  	id   uint64 // the billy id of transction
    25  	size uint64 // the RLP encoded size of transaction (blobs are included)
    26  }
    27  
    28  // lookup maps blob versioned hashes to transaction hashes that include them,
    29  // transaction hashes to billy entries that include them, transaction hashes
    30  // to the transaction size
    31  type lookup struct {
    32  	blobIndex map[common.Hash]map[common.Hash]struct{}
    33  	txIndex   map[common.Hash]*txMetadata
    34  }
    35  
    36  // newLookup creates a new index for tracking blob to tx; and tx to billy mappings.
    37  func newLookup() *lookup {
    38  	return &lookup{
    39  		blobIndex: make(map[common.Hash]map[common.Hash]struct{}),
    40  		txIndex:   make(map[common.Hash]*txMetadata),
    41  	}
    42  }
    43  
    44  // exists returns whether a transaction is already tracked or not.
    45  func (l *lookup) exists(txhash common.Hash) bool {
    46  	_, exists := l.txIndex[txhash]
    47  	return exists
    48  }
    49  
    50  // storeidOfTx returns the datastore storage item id of a transaction.
    51  func (l *lookup) storeidOfTx(txhash common.Hash) (uint64, bool) {
    52  	meta, ok := l.txIndex[txhash]
    53  	if !ok {
    54  		return 0, false
    55  	}
    56  	return meta.id, true
    57  }
    58  
    59  // storeidOfBlob returns the datastore storage item id of a blob.
    60  func (l *lookup) storeidOfBlob(vhash common.Hash) (uint64, bool) {
    61  	// If the blob is unknown, return a miss
    62  	txs, ok := l.blobIndex[vhash]
    63  	if !ok {
    64  		return 0, false
    65  	}
    66  	// If the blob is known, return any tx for it
    67  	for tx := range txs {
    68  		return l.storeidOfTx(tx)
    69  	}
    70  	return 0, false // Weird, don't choke
    71  }
    72  
    73  // sizeOfTx returns the RLP-encoded size of transaction
    74  func (l *lookup) sizeOfTx(txhash common.Hash) (uint64, bool) {
    75  	meta, ok := l.txIndex[txhash]
    76  	if !ok {
    77  		return 0, false
    78  	}
    79  	return meta.size, true
    80  }
    81  
    82  // track inserts a new set of mappings from blob versioned hashes to transaction
    83  // hashes; and from transaction hashes to datastore storage item ids.
    84  func (l *lookup) track(tx *blobTxMeta) {
    85  	// Map all the blobs to the transaction hash
    86  	for _, vhash := range tx.vhashes {
    87  		if _, ok := l.blobIndex[vhash]; !ok {
    88  			l.blobIndex[vhash] = make(map[common.Hash]struct{})
    89  		}
    90  		l.blobIndex[vhash][tx.hash] = struct{}{} // may be double mapped if a tx contains the same blob twice
    91  	}
    92  	// Map the transaction hash to the datastore id and RLP-encoded transaction size
    93  	l.txIndex[tx.hash] = &txMetadata{
    94  		id:   tx.id,
    95  		size: tx.size,
    96  	}
    97  }
    98  
    99  // untrack removes a set of mappings from blob versioned hashes to transaction
   100  // hashes from the blob index.
   101  func (l *lookup) untrack(tx *blobTxMeta) {
   102  	// Unmap the transaction hash from the datastore id
   103  	delete(l.txIndex, tx.hash)
   104  
   105  	// Unmap all the blobs from the transaction hash
   106  	for _, vhash := range tx.vhashes {
   107  		delete(l.blobIndex[vhash], tx.hash) // may be double deleted if a tx contains the same blob twice
   108  		if len(l.blobIndex[vhash]) == 0 {
   109  			delete(l.blobIndex, vhash)
   110  		}
   111  	}
   112  }