From c072bbaac149d9fd22f98919c3a7f7680b990a97 Mon Sep 17 00:00:00 2001 From: JukLee0ira Date: Fri, 12 Jul 2024 10:26:36 +0800 Subject: [PATCH] eth,les,miner: implement feeHistory API (#23033) --- eth/api_backend.go | 4 ++++ les/api_backend.go | 4 ++++ miner/miner.go | 5 +++++ miner/worker.go | 12 ++++++++++++ 4 files changed, 25 insertions(+) diff --git a/eth/api_backend.go b/eth/api_backend.go index 732d3e5156..cf796fb40a 100644 --- a/eth/api_backend.go +++ b/eth/api_backend.go @@ -179,6 +179,10 @@ func (b *EthApiBackend) BlockByNumberOrHash(ctx context.Context, blockNrOrHash r return nil, errors.New("invalid arguments; neither block nor hash specified") } +func (b *EthApiBackend) PendingBlockAndReceipts() (*types.Block, types.Receipts) { + return b.eth.miner.PendingBlockAndReceipts() +} + func (b *EthApiBackend) StateAndHeaderByNumber(ctx context.Context, blockNr rpc.BlockNumber) (*state.StateDB, *types.Header, error) { // Pending state is only known by the miner if blockNr == rpc.PendingBlockNumber { diff --git a/les/api_backend.go b/les/api_backend.go index 110d5fb705..98ef15d850 100644 --- a/les/api_backend.go +++ b/les/api_backend.go @@ -130,6 +130,10 @@ func (b *LesApiBackend) BlockByNumberOrHash(ctx context.Context, blockNrOrHash r return nil, errors.New("invalid arguments; neither block nor hash specified") } +func (b *LesApiBackend) PendingBlockAndReceipts() (*types.Block, types.Receipts) { + return nil, nil +} + func (b *LesApiBackend) StateAndHeaderByNumber(ctx context.Context, blockNr rpc.BlockNumber) (*state.StateDB, *types.Header, error) { header, err := b.HeaderByNumber(ctx, blockNr) if header == nil || err != nil { diff --git a/miner/miner.go b/miner/miner.go index 2d5a971cd1..835f0f014b 100644 --- a/miner/miner.go +++ b/miner/miner.go @@ -178,6 +178,11 @@ func (self *Miner) PendingBlock() *types.Block { return self.worker.pendingBlock() } +// PendingBlockAndReceipts returns the currently pending block and corresponding receipts. +func (miner *Miner) PendingBlockAndReceipts() (*types.Block, types.Receipts) { + return miner.worker.pendingBlockAndReceipts() +} + func (self *Miner) SetEtherbase(addr common.Address) { self.coinbase = addr self.worker.setEtherbase(addr) diff --git a/miner/worker.go b/miner/worker.go index 4b084b6c52..64b195b30f 100644 --- a/miner/worker.go +++ b/miner/worker.go @@ -128,6 +128,10 @@ type worker struct { coinbase common.Address extra []byte + snapshotMu sync.RWMutex // The lock used to protect the block snapshot and state snapshot + snapshotBlock *types.Block + snapshotReceipts types.Receipts + currentMu sync.Mutex current *Work @@ -219,6 +223,14 @@ func (self *worker) pendingBlock() *types.Block { return self.current.Block } +// pendingBlockAndReceipts returns pending block and corresponding receipts. +func (w *worker) pendingBlockAndReceipts() (*types.Block, types.Receipts) { + // return a snapshot to avoid contention on currentMu mutex + w.snapshotMu.RLock() + defer w.snapshotMu.RUnlock() + return w.snapshotBlock, w.snapshotReceipts +} + func (self *worker) start() { self.mu.Lock() defer self.mu.Unlock()