github.com/ethereum-optimism/optimism/l2geth@v0.0.0-20230612200230-50b04ade19e3/miner/unconfirmed.go (about) 1 // Copyright 2016 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 miner 18 19 import ( 20 "container/ring" 21 "sync" 22 23 "github.com/ethereum-optimism/optimism/l2geth/common" 24 "github.com/ethereum-optimism/optimism/l2geth/core/types" 25 "github.com/ethereum-optimism/optimism/l2geth/log" 26 ) 27 28 // chainRetriever is used by the unconfirmed block set to verify whether a previously 29 // mined block is part of the canonical chain or not. 30 type chainRetriever interface { 31 // GetHeaderByNumber retrieves the canonical header associated with a block number. 32 GetHeaderByNumber(number uint64) *types.Header 33 34 // GetBlockByNumber retrieves the canonical block associated with a block number. 35 GetBlockByNumber(number uint64) *types.Block 36 } 37 38 // unconfirmedBlock is a small collection of metadata about a locally mined block 39 // that is placed into a unconfirmed set for canonical chain inclusion tracking. 40 type unconfirmedBlock struct { 41 index uint64 42 hash common.Hash 43 } 44 45 // unconfirmedBlocks implements a data structure to maintain locally mined blocks 46 // have not yet reached enough maturity to guarantee chain inclusion. It is 47 // used by the miner to provide logs to the user when a previously mined block 48 // has a high enough guarantee to not be reorged out of the canonical chain. 49 type unconfirmedBlocks struct { 50 chain chainRetriever // Blockchain to verify canonical status through 51 depth uint // Depth after which to discard previous blocks 52 blocks *ring.Ring // Block infos to allow canonical chain cross checks 53 lock sync.RWMutex // Protects the fields from concurrent access 54 } 55 56 // newUnconfirmedBlocks returns new data structure to track currently unconfirmed blocks. 57 func newUnconfirmedBlocks(chain chainRetriever, depth uint) *unconfirmedBlocks { 58 return &unconfirmedBlocks{ 59 chain: chain, 60 depth: depth, 61 } 62 } 63 64 // Insert adds a new block to the set of unconfirmed ones. 65 func (set *unconfirmedBlocks) Insert(index uint64, hash common.Hash) { 66 // If a new block was mined locally, shift out any old enough blocks 67 set.Shift(index) 68 69 // Create the new item as its own ring 70 item := ring.New(1) 71 item.Value = &unconfirmedBlock{ 72 index: index, 73 hash: hash, 74 } 75 // Set as the initial ring or append to the end 76 set.lock.Lock() 77 defer set.lock.Unlock() 78 79 if set.blocks == nil { 80 set.blocks = item 81 } else { 82 set.blocks.Move(-1).Link(item) 83 } 84 } 85 86 // Shift drops all unconfirmed blocks from the set which exceed the unconfirmed sets depth 87 // allowance, checking them against the canonical chain for inclusion or staleness 88 // report. 89 func (set *unconfirmedBlocks) Shift(height uint64) { 90 set.lock.Lock() 91 defer set.lock.Unlock() 92 93 for set.blocks != nil { 94 // Retrieve the next unconfirmed block and abort if too fresh 95 next := set.blocks.Value.(*unconfirmedBlock) 96 if next.index+uint64(set.depth) > height { 97 break 98 } 99 // Block seems to exceed depth allowance, check for canonical status 100 header := set.chain.GetHeaderByNumber(next.index) 101 switch { 102 case header == nil: 103 log.Warn("Failed to retrieve header of mined block", "number", next.index, "hash", next.hash) 104 case header.Hash() == next.hash: 105 // Do nothing 106 default: 107 // Block is not canonical, check whether we have an uncle or a lost block 108 included := false 109 for number := next.index; !included && number < next.index+uint64(set.depth) && number <= height; number++ { 110 if block := set.chain.GetBlockByNumber(number); block != nil { 111 for _, uncle := range block.Uncles() { 112 if uncle.Hash() == next.hash { 113 included = true 114 break 115 } 116 } 117 } 118 } 119 if included { 120 log.Info("⑂ block became an uncle", "number", next.index, "hash", next.hash) 121 } else { 122 log.Info("😱 block lost", "number", next.index, "hash", next.hash) 123 } 124 } 125 // Drop the block out of the ring 126 if set.blocks.Value == set.blocks.Next().Value { 127 set.blocks = nil 128 } else { 129 set.blocks = set.blocks.Move(-1) 130 set.blocks.Unlink(1) 131 set.blocks = set.blocks.Move(1) 132 } 133 } 134 }