From 9b276dda614a05b39983c1cfd3b43edf5cc3d212 Mon Sep 17 00:00:00 2001 From: Martin Holst Swende Date: Fri, 16 Feb 2024 13:35:49 +0100 Subject: [PATCH] miner: use common.hash instead of *types.header --- ethclient/ethclient_test.go | 22 +++++++++++++--------- miner/miner.go | 4 ++-- miner/pending.go | 21 ++++++++++----------- 3 files changed, 25 insertions(+), 22 deletions(-) diff --git a/ethclient/ethclient_test.go b/ethclient/ethclient_test.go index 6014f0102c..2f3229cedc 100644 --- a/ethclient/ethclient_test.go +++ b/ethclient/ethclient_test.go @@ -604,16 +604,20 @@ func testAtFunctions(t *testing.T, client *rpc.Client) { // send a transaction for some interesting pending status // and wait for the transaction to be included in the pending block sendTransaction(ec) - time.Sleep(2000 * time.Millisecond) - // Check pending transaction count - pending, err := ec.PendingTransactionCount(context.Background()) - if err != nil { - t.Fatalf("unexpected error: %v", err) - } - if pending != 1 { - t.Fatalf("unexpected pending, wanted 1 got: %v", pending) + // wait for the transaction to be included in the pending block + for { + // Check pending transaction count + pending, err := ec.PendingTransactionCount(context.Background()) + if err != nil { + t.Fatalf("unexpected error: %v", err) + } + if pending == 1 { + break + } + time.Sleep(100 * time.Millisecond) } + // Query balance balance, err := ec.BalanceAt(context.Background(), testAddr, nil) if err != nil { @@ -738,7 +742,7 @@ func sendTransaction(ec *Client) error { if err != nil { return err } - nonce, err := ec.PendingNonceAt(context.Background(), testAddr) + nonce, err := ec.NonceAt(context.Background(), testAddr, nil) if err != nil { return err } diff --git a/miner/miner.go b/miner/miner.go index 2925eeee0c..1ad8f7ec6a 100644 --- a/miner/miner.go +++ b/miner/miner.go @@ -143,7 +143,7 @@ func (miner *Miner) getPending() *newPayloadResult { miner.confMu.RUnlock() header := miner.chain.CurrentHeader() - if cached := miner.pending.resolve(header, coinbase); cached != nil { + if cached := miner.pending.resolve(header.Hash(), coinbase); cached != nil { return cached } var ( @@ -166,6 +166,6 @@ func (miner *Miner) getPending() *newPayloadResult { if ret.err != nil { return nil } - miner.pending.update(header, ret) + miner.pending.update(header.Hash(), ret) return ret } diff --git a/miner/pending.go b/miner/pending.go index 923e392aeb..2b09e07e0c 100644 --- a/miner/pending.go +++ b/miner/pending.go @@ -21,7 +21,6 @@ import ( "time" "github.com/ethereum/go-ethereum/common" - "github.com/ethereum/go-ethereum/core/types" ) // pendingTTL indicates the period of time a generated pending block should @@ -31,24 +30,24 @@ const pendingTTL = 2 * time.Second // pending wraps a pending block with additional metadata. type pending struct { - created time.Time - parent *types.Header - result *newPayloadResult - lock sync.Mutex + created time.Time + parentHash common.Hash + result *newPayloadResult + lock sync.Mutex } // resolve retrieves the cached pending result if it's available. Nothing will be -// returned if the parent/coinbase is not matched or the result is already too old. +// returned if the parentHash/coinbase is not matched or the result is already too old. // // Note, don't modify the returned payload result. -func (p *pending) resolve(parent *types.Header, coinbase common.Address) *newPayloadResult { +func (p *pending) resolve(parentHash common.Hash, coinbase common.Address) *newPayloadResult { p.lock.Lock() defer p.lock.Unlock() - if p.result == nil || p.parent == nil { + if p.result == nil { return nil } - if parent.Hash() != p.parent.Hash() { + if parentHash != p.parentHash { return nil } if p.result.block.Coinbase() != coinbase { @@ -61,11 +60,11 @@ func (p *pending) resolve(parent *types.Header, coinbase common.Address) *newPay } // update refreshes the cached pending block with newly created one. -func (p *pending) update(parent *types.Header, result *newPayloadResult) { +func (p *pending) update(parent common.Hash, result *newPayloadResult) { p.lock.Lock() defer p.lock.Unlock() - p.parent = parent + p.parentHash = parent p.result = result p.created = time.Now() }