internal/ethapi, miner: fix GetBlockReceipts for pending (#32461)
Some checks are pending
/ Linux Build (push) Waiting to run
/ Linux Build (arm) (push) Waiting to run
/ Windows Build (push) Waiting to run
/ Docker Image (push) Waiting to run

This commit is contained in:
rjl493456442 2025-08-20 09:20:21 +08:00 committed by GitHub
parent 7d4852b9eb
commit 9ce40d19a8
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
7 changed files with 116 additions and 106 deletions

View file

@ -597,19 +597,30 @@ func (api *BlockChainAPI) GetStorageAt(ctx context.Context, address common.Addre
// GetBlockReceipts returns the block receipts for the given block hash or number or tag.
func (api *BlockChainAPI) GetBlockReceipts(ctx context.Context, blockNrOrHash rpc.BlockNumberOrHash) ([]map[string]interface{}, error) {
block, err := api.b.BlockByNumberOrHash(ctx, blockNrOrHash)
if block == nil || err != nil {
return nil, err
}
receipts, err := api.b.GetReceipts(ctx, block.Hash())
if err != nil {
return nil, err
var (
err error
block *types.Block
receipts types.Receipts
)
if blockNr, ok := blockNrOrHash.Number(); ok && blockNr == rpc.PendingBlockNumber {
block, receipts, _ = api.b.Pending()
if block == nil {
return nil, errors.New("pending receipts is not available")
}
} else {
block, err = api.b.BlockByNumberOrHash(ctx, blockNrOrHash)
if block == nil || err != nil {
return nil, err
}
receipts, err = api.b.GetReceipts(ctx, block.Hash())
if err != nil {
return nil, err
}
}
txs := block.Transactions()
if len(txs) != len(receipts) {
return nil, fmt.Errorf("receipts length mismatch: %d vs %d", len(txs), len(receipts))
}
// Derive the sender.
signer := types.MakeSigner(api.b.ChainConfig(), block.Number(), block.Time())
@ -617,7 +628,6 @@ func (api *BlockChainAPI) GetBlockReceipts(ctx context.Context, blockNrOrHash rp
for i, receipt := range receipts {
result[i] = marshalReceipt(receipt, block.Hash(), block.NumberU64(), signer, txs[i], i)
}
return result, nil
}

View file

@ -434,11 +434,13 @@ func newTestAccountManager(t *testing.T) (*accounts.Manager, accounts.Account) {
}
type testBackend struct {
db ethdb.Database
chain *core.BlockChain
pending *types.Block
accman *accounts.Manager
acc accounts.Account
db ethdb.Database
chain *core.BlockChain
accman *accounts.Manager
acc accounts.Account
pending *types.Block
pendingReceipts types.Receipts
}
func newTestBackend(t *testing.T, n int, gspec *core.Genesis, engine consensus.Engine, generator func(i int, b *core.BlockGen)) *testBackend {
@ -449,24 +451,26 @@ func newTestBackend(t *testing.T, n int, gspec *core.Genesis, engine consensus.E
gspec.Alloc[acc.Address] = types.Account{Balance: big.NewInt(params.Ether)}
// Generate blocks for testing
db, blocks, _ := core.GenerateChainWithGenesis(gspec, engine, n, generator)
db, blocks, receipts := core.GenerateChainWithGenesis(gspec, engine, n+1, generator)
chain, err := core.NewBlockChain(db, gspec, engine, options)
if err != nil {
t.Fatalf("failed to create tester chain: %v", err)
}
if n, err := chain.InsertChain(blocks); err != nil {
if n, err := chain.InsertChain(blocks[:n]); err != nil {
t.Fatalf("block %d: failed to insert into chain: %v", n, err)
}
backend := &testBackend{db: db, chain: chain, accman: accman, acc: acc}
backend := &testBackend{
db: db,
chain: chain,
accman: accman,
acc: acc,
pending: blocks[n],
pendingReceipts: receipts[n],
}
return backend
}
func (b *testBackend) setPendingBlock(block *types.Block) {
b.pending = block
}
func (b testBackend) SyncProgress(ctx context.Context) ethereum.SyncProgress {
return ethereum.SyncProgress{}
}
@ -558,7 +562,13 @@ func (b testBackend) StateAndHeaderByNumberOrHash(ctx context.Context, blockNrOr
}
panic("only implemented for number")
}
func (b testBackend) Pending() (*types.Block, types.Receipts, *state.StateDB) { panic("implement me") }
func (b testBackend) Pending() (*types.Block, types.Receipts, *state.StateDB) {
block := b.pending
if block == nil {
return nil, nil, nil
}
return block, b.pendingReceipts, nil
}
func (b testBackend) GetReceipts(ctx context.Context, hash common.Hash) (types.Receipts, error) {
header, err := b.HeaderByHash(ctx, hash)
if header == nil || err != nil {
@ -3141,21 +3151,6 @@ func TestRPCGetBlockOrHeader(t *testing.T) {
}
genBlocks = 10
signer = types.HomesteadSigner{}
tx = types.NewTx(&types.LegacyTx{
Nonce: 11,
GasPrice: big.NewInt(11111),
Gas: 1111,
To: &acc2Addr,
Value: big.NewInt(111),
Data: []byte{0x11, 0x11, 0x11},
})
withdrawal = &types.Withdrawal{
Index: 0,
Validator: 1,
Address: common.Address{0x12, 0x34},
Amount: 10,
}
pending = types.NewBlock(&types.Header{Number: big.NewInt(11), Time: 42}, &types.Body{Transactions: types.Transactions{tx}, Withdrawals: types.Withdrawals{withdrawal}}, nil, blocktest.NewHasher())
)
backend := newTestBackend(t, genBlocks, genesis, ethash.NewFaker(), func(i int, b *core.BlockGen) {
// Transfer from account[0] to account[1]
@ -3164,7 +3159,6 @@ func TestRPCGetBlockOrHeader(t *testing.T) {
tx, _ := types.SignTx(types.NewTx(&types.LegacyTx{Nonce: uint64(i), To: &acc2Addr, Value: big.NewInt(1000), Gas: params.TxGas, GasPrice: b.BaseFee(), Data: nil}), signer, acc1Key)
b.AddTx(tx)
})
backend.setPendingBlock(pending)
api := NewBlockChainAPI(backend)
blockHashes := make([]common.Hash, genBlocks+1)
ctx := context.Background()
@ -3175,7 +3169,7 @@ func TestRPCGetBlockOrHeader(t *testing.T) {
}
blockHashes[i] = header.Hash()
}
pendingHash := pending.Hash()
pendingHash := backend.pending.Hash()
var testSuite = []struct {
blockNumber rpc.BlockNumber
@ -3406,7 +3400,7 @@ func setupReceiptBackend(t *testing.T, genBlocks int) (*testBackend, []common.Ha
},
}
signer = types.LatestSignerForChainID(params.TestChainConfig.ChainID)
txHashes = make([]common.Hash, genBlocks)
txHashes = make([]common.Hash, 0, genBlocks)
)
backend := newTestBackend(t, genBlocks, genesis, beacon.New(ethash.NewFaker()), func(i int, b *core.BlockGen) {
@ -3416,9 +3410,6 @@ func setupReceiptBackend(t *testing.T, genBlocks int) (*testBackend, []common.Ha
)
b.SetPoS()
switch i {
case 0:
// transfer 1000wei
tx, err = types.SignTx(types.NewTx(&types.LegacyTx{Nonce: uint64(i), To: &acc2Addr, Value: big.NewInt(1000), Gas: params.TxGas, GasPrice: b.BaseFee(), Data: nil}), types.HomesteadSigner{}, acc1Key)
case 1:
// create contract
tx, err = types.SignTx(types.NewTx(&types.LegacyTx{Nonce: uint64(i), To: nil, Gas: 53100, GasPrice: b.BaseFee(), Data: common.FromHex("0x60806040")}), signer, acc1Key)
@ -3455,13 +3446,16 @@ func setupReceiptBackend(t *testing.T, genBlocks int) (*testBackend, []common.Ha
BlobHashes: []common.Hash{{1}},
Value: new(uint256.Int),
}), signer, acc1Key)
default:
// transfer 1000wei
tx, err = types.SignTx(types.NewTx(&types.LegacyTx{Nonce: uint64(i), To: &acc2Addr, Value: big.NewInt(1000), Gas: params.TxGas, GasPrice: b.BaseFee(), Data: nil}), types.HomesteadSigner{}, acc1Key)
}
if err != nil {
t.Errorf("failed to sign tx: %v", err)
}
if tx != nil {
b.AddTx(tx)
txHashes[i] = tx.Hash()
txHashes = append(txHashes, tx.Hash())
}
})
return backend, txHashes
@ -3577,6 +3571,11 @@ func TestRPCGetBlockReceipts(t *testing.T) {
test: rpc.BlockNumberOrHashWithNumber(rpc.LatestBlockNumber),
file: "tag-latest",
},
// 3. pending tag
{
test: rpc.BlockNumberOrHashWithNumber(rpc.PendingBlockNumber),
file: "tag-pending",
},
// 4. block with legacy transfer tx(hash)
{
test: rpc.BlockNumberOrHashWithHash(blockHashes[1], false),

View file

@ -1,49 +1,40 @@
{
"difficulty": "0x0",
"baseFeePerGas": "0xde56ab3",
"difficulty": "0x20000",
"extraData": "0x",
"gasLimit": "0x0",
"gasUsed": "0x0",
"gasLimit": "0x47e7c4",
"gasUsed": "0x5208",
"hash": null,
"logsBloom": "0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
"miner": null,
"mixHash": "0x0000000000000000000000000000000000000000000000000000000000000000",
"nonce": null,
"number": "0xb",
"parentHash": "0x0000000000000000000000000000000000000000000000000000000000000000",
"receiptsRoot": "0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421",
"parentHash": "0xa063415a5020f1569fae73ecb0d37bc5649ebe86d59e764a389eb37814bd42cb",
"receiptsRoot": "0x056b23fbba480696b65fe5a59b8f2148a1299103c4f57df839233af2cf4ca2d2",
"sha3Uncles": "0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347",
"size": "0x256",
"stateRoot": "0x0000000000000000000000000000000000000000000000000000000000000000",
"timestamp": "0x2a",
"size": "0x26a",
"stateRoot": "0xce0e05397e548614a5b93254662174329466f8f4b1b391eb36fec9a7a591e58e",
"timestamp": "0x6e",
"transactions": [
{
"blockHash": "0x6cebd9f966ea686f44b981685e3f0eacea28591a7a86d7fbbe521a86e9f81165",
"blockHash": "0xfda6c7cb7a3a712e0c424909a7724cab0448e89e286617fa8d5fd27f63f28bd2",
"blockNumber": "0xb",
"from": "0x0000000000000000000000000000000000000000",
"gas": "0x457",
"gasPrice": "0x2b67",
"hash": "0x4afee081df5dff7a025964032871f7d4ba4d21baf5f6376a2f4a9f79fc506298",
"input": "0x111111",
"nonce": "0xb",
"from": "0x703c4b2bd70c169f5717101caee543299fc946c7",
"gas": "0x5208",
"gasPrice": "0xde56ab3",
"hash": "0xd773fbb47ec87b1a958ac16430943ddf2797ecae2b33fe7b16ddb334e30325ed",
"input": "0x",
"nonce": "0xa",
"to": "0x0d3ab14bbad3d99f4203bd7a11acb94882050e7e",
"transactionIndex": "0x0",
"value": "0x6f",
"value": "0x3e8",
"type": "0x0",
"chainId": "0x7fffffffffffffee",
"v": "0x0",
"r": "0x0",
"s": "0x0"
"v": "0x1c",
"r": "0xfa029dacd66238d20cd649fe3b323bb458d2cfa4af7db0ff4f6b3e1039bc320a",
"s": "0x52fb4d45c1d623f2f05508bae063a4728761d762ae45b8b0908ffea546f3d95e"
}
],
"transactionsRoot": "0x98d9f6dd0aa479c0fb448f2627e9f1964aca699fccab8f6e95861547a4699e37",
"uncles": [],
"withdrawals": [
{
"index": "0x0",
"validatorIndex": "0x1",
"address": "0x1234000000000000000000000000000000000000",
"amount": "0xa"
}
],
"withdrawalsRoot": "0x73d756269cdfc22e7e17a3548e36f42f750ca06d7e3cd98d1b6d0eb5add9dc84"
"transactionsRoot": "0x59abb8ec0655f66e66450d1502618bc64022ae2d2950fa471eec6e8da2846264",
"uncles": []
}

View file

@ -1,32 +1,24 @@
{
"difficulty": "0x0",
"baseFeePerGas": "0xde56ab3",
"difficulty": "0x20000",
"extraData": "0x",
"gasLimit": "0x0",
"gasUsed": "0x0",
"gasLimit": "0x47e7c4",
"gasUsed": "0x5208",
"hash": null,
"logsBloom": "0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
"miner": null,
"mixHash": "0x0000000000000000000000000000000000000000000000000000000000000000",
"nonce": null,
"number": "0xb",
"parentHash": "0x0000000000000000000000000000000000000000000000000000000000000000",
"receiptsRoot": "0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421",
"parentHash": "0xa063415a5020f1569fae73ecb0d37bc5649ebe86d59e764a389eb37814bd42cb",
"receiptsRoot": "0x056b23fbba480696b65fe5a59b8f2148a1299103c4f57df839233af2cf4ca2d2",
"sha3Uncles": "0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347",
"size": "0x256",
"stateRoot": "0x0000000000000000000000000000000000000000000000000000000000000000",
"timestamp": "0x2a",
"size": "0x26a",
"stateRoot": "0xce0e05397e548614a5b93254662174329466f8f4b1b391eb36fec9a7a591e58e",
"timestamp": "0x6e",
"transactions": [
"0x4afee081df5dff7a025964032871f7d4ba4d21baf5f6376a2f4a9f79fc506298"
"0xd773fbb47ec87b1a958ac16430943ddf2797ecae2b33fe7b16ddb334e30325ed"
],
"transactionsRoot": "0x98d9f6dd0aa479c0fb448f2627e9f1964aca699fccab8f6e95861547a4699e37",
"uncles": [],
"withdrawals": [
{
"index": "0x0",
"validatorIndex": "0x1",
"address": "0x1234000000000000000000000000000000000000",
"amount": "0xa"
}
],
"withdrawalsRoot": "0x73d756269cdfc22e7e17a3548e36f42f750ca06d7e3cd98d1b6d0eb5add9dc84"
"transactionsRoot": "0x59abb8ec0655f66e66450d1502618bc64022ae2d2950fa471eec6e8da2846264",
"uncles": []
}

View file

@ -0,0 +1,18 @@
[
{
"blockHash": "0xc74cf882395ec92eec3673d93a57f9a3bf1a5e696fae3e52f252059af62756c8",
"blockNumber": "0x7",
"contractAddress": null,
"cumulativeGasUsed": "0x5208",
"effectiveGasPrice": "0x17b07ddf",
"from": "0x703c4b2bd70c169f5717101caee543299fc946c7",
"gasUsed": "0x5208",
"logs": [],
"logsBloom": "0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
"status": "0x1",
"to": "0x0d3ab14bbad3d99f4203bd7a11acb94882050e7e",
"transactionHash": "0xa7eeffe8111539a8f9725eb4d49e341efa1287d33190300adab220929daa5fac",
"transactionIndex": "0x0",
"type": "0x0"
}
]

View file

@ -1,19 +1,19 @@
{
"difficulty": "0x0",
"baseFeePerGas": "0xde56ab3",
"difficulty": "0x20000",
"extraData": "0x",
"gasLimit": "0x0",
"gasUsed": "0x0",
"gasLimit": "0x47e7c4",
"gasUsed": "0x5208",
"hash": null,
"logsBloom": "0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
"miner": null,
"mixHash": "0x0000000000000000000000000000000000000000000000000000000000000000",
"nonce": null,
"number": "0xb",
"parentHash": "0x0000000000000000000000000000000000000000000000000000000000000000",
"receiptsRoot": "0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421",
"parentHash": "0xa063415a5020f1569fae73ecb0d37bc5649ebe86d59e764a389eb37814bd42cb",
"receiptsRoot": "0x056b23fbba480696b65fe5a59b8f2148a1299103c4f57df839233af2cf4ca2d2",
"sha3Uncles": "0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347",
"stateRoot": "0x0000000000000000000000000000000000000000000000000000000000000000",
"timestamp": "0x2a",
"transactionsRoot": "0x98d9f6dd0aa479c0fb448f2627e9f1964aca699fccab8f6e95861547a4699e37",
"withdrawalsRoot": "0x73d756269cdfc22e7e17a3548e36f42f750ca06d7e3cd98d1b6d0eb5add9dc84"
"stateRoot": "0xce0e05397e548614a5b93254662174329466f8f4b1b391eb36fec9a7a591e58e",
"timestamp": "0x6e",
"transactionsRoot": "0x59abb8ec0655f66e66450d1502618bc64022ae2d2950fa471eec6e8da2846264"
}

View file

@ -144,10 +144,10 @@ func (miner *Miner) getPending() *newPayloadResult {
header := miner.chain.CurrentHeader()
miner.pendingMu.Lock()
defer miner.pendingMu.Unlock()
if cached := miner.pending.resolve(header.Hash()); cached != nil {
return cached
}
var (
timestamp = uint64(time.Now().Unix())
withdrawal types.Withdrawals