github.1485827954.workers.dev/ethereum/go-ethereum@v1.14.3/miner/pending.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 miner 18 19 import ( 20 "sync" 21 "time" 22 23 "github.com/ethereum/go-ethereum/common" 24 ) 25 26 // pendingTTL indicates the period of time a generated pending block should 27 // exist to serve RPC requests before being discarded if the parent block 28 // has not changed yet. The value is chosen to align with the recommit interval. 29 const pendingTTL = 2 * time.Second 30 31 // pending wraps a pending block with additional metadata. 32 type pending struct { 33 created time.Time 34 parentHash common.Hash 35 result *newPayloadResult 36 lock sync.Mutex 37 } 38 39 // resolve retrieves the cached pending result if it's available. Nothing will be 40 // returned if the parentHash is not matched or the result is already too old. 41 // 42 // Note, don't modify the returned payload result. 43 func (p *pending) resolve(parentHash common.Hash) *newPayloadResult { 44 p.lock.Lock() 45 defer p.lock.Unlock() 46 47 if p.result == nil { 48 return nil 49 } 50 if parentHash != p.parentHash { 51 return nil 52 } 53 if time.Since(p.created) > pendingTTL { 54 return nil 55 } 56 return p.result 57 } 58 59 // update refreshes the cached pending block with newly created one. 60 func (p *pending) update(parent common.Hash, result *newPayloadResult) { 61 p.lock.Lock() 62 defer p.lock.Unlock() 63 64 p.parentHash = parent 65 p.result = result 66 p.created = time.Now() 67 }