Skip to content

Commit 9ce40d1

Browse files
authored
internal/ethapi, miner: fix GetBlockReceipts for pending (#32461)
1 parent 7d4852b commit 9ce40d1

File tree

7 files changed

+116
-106
lines changed

7 files changed

+116
-106
lines changed

internal/ethapi/api.go

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -597,27 +597,37 @@ func (api *BlockChainAPI) GetStorageAt(ctx context.Context, address common.Addre
597597

598598
// GetBlockReceipts returns the block receipts for the given block hash or number or tag.
599599
func (api *BlockChainAPI) GetBlockReceipts(ctx context.Context, blockNrOrHash rpc.BlockNumberOrHash) ([]map[string]interface{}, error) {
600-
block, err := api.b.BlockByNumberOrHash(ctx, blockNrOrHash)
601-
if block == nil || err != nil {
602-
return nil, err
603-
}
604-
receipts, err := api.b.GetReceipts(ctx, block.Hash())
605-
if err != nil {
606-
return nil, err
600+
var (
601+
err error
602+
block *types.Block
603+
receipts types.Receipts
604+
)
605+
if blockNr, ok := blockNrOrHash.Number(); ok && blockNr == rpc.PendingBlockNumber {
606+
block, receipts, _ = api.b.Pending()
607+
if block == nil {
608+
return nil, errors.New("pending receipts is not available")
609+
}
610+
} else {
611+
block, err = api.b.BlockByNumberOrHash(ctx, blockNrOrHash)
612+
if block == nil || err != nil {
613+
return nil, err
614+
}
615+
receipts, err = api.b.GetReceipts(ctx, block.Hash())
616+
if err != nil {
617+
return nil, err
618+
}
607619
}
608620
txs := block.Transactions()
609621
if len(txs) != len(receipts) {
610622
return nil, fmt.Errorf("receipts length mismatch: %d vs %d", len(txs), len(receipts))
611623
}
612-
613624
// Derive the sender.
614625
signer := types.MakeSigner(api.b.ChainConfig(), block.Number(), block.Time())
615626

616627
result := make([]map[string]interface{}, len(receipts))
617628
for i, receipt := range receipts {
618629
result[i] = marshalReceipt(receipt, block.Hash(), block.NumberU64(), signer, txs[i], i)
619630
}
620-
621631
return result, nil
622632
}
623633

internal/ethapi/api_test.go

Lines changed: 35 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -434,11 +434,13 @@ func newTestAccountManager(t *testing.T) (*accounts.Manager, accounts.Account) {
434434
}
435435

436436
type testBackend struct {
437-
db ethdb.Database
438-
chain *core.BlockChain
439-
pending *types.Block
440-
accman *accounts.Manager
441-
acc accounts.Account
437+
db ethdb.Database
438+
chain *core.BlockChain
439+
accman *accounts.Manager
440+
acc accounts.Account
441+
442+
pending *types.Block
443+
pendingReceipts types.Receipts
442444
}
443445

444446
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
449451
gspec.Alloc[acc.Address] = types.Account{Balance: big.NewInt(params.Ether)}
450452

451453
// Generate blocks for testing
452-
db, blocks, _ := core.GenerateChainWithGenesis(gspec, engine, n, generator)
454+
db, blocks, receipts := core.GenerateChainWithGenesis(gspec, engine, n+1, generator)
453455

454456
chain, err := core.NewBlockChain(db, gspec, engine, options)
455457
if err != nil {
456458
t.Fatalf("failed to create tester chain: %v", err)
457459
}
458-
if n, err := chain.InsertChain(blocks); err != nil {
460+
if n, err := chain.InsertChain(blocks[:n]); err != nil {
459461
t.Fatalf("block %d: failed to insert into chain: %v", n, err)
460462
}
461-
462-
backend := &testBackend{db: db, chain: chain, accman: accman, acc: acc}
463+
backend := &testBackend{
464+
db: db,
465+
chain: chain,
466+
accman: accman,
467+
acc: acc,
468+
pending: blocks[n],
469+
pendingReceipts: receipts[n],
470+
}
463471
return backend
464472
}
465473

466-
func (b *testBackend) setPendingBlock(block *types.Block) {
467-
b.pending = block
468-
}
469-
470474
func (b testBackend) SyncProgress(ctx context.Context) ethereum.SyncProgress {
471475
return ethereum.SyncProgress{}
472476
}
@@ -558,7 +562,13 @@ func (b testBackend) StateAndHeaderByNumberOrHash(ctx context.Context, blockNrOr
558562
}
559563
panic("only implemented for number")
560564
}
561-
func (b testBackend) Pending() (*types.Block, types.Receipts, *state.StateDB) { panic("implement me") }
565+
func (b testBackend) Pending() (*types.Block, types.Receipts, *state.StateDB) {
566+
block := b.pending
567+
if block == nil {
568+
return nil, nil, nil
569+
}
570+
return block, b.pendingReceipts, nil
571+
}
562572
func (b testBackend) GetReceipts(ctx context.Context, hash common.Hash) (types.Receipts, error) {
563573
header, err := b.HeaderByHash(ctx, hash)
564574
if header == nil || err != nil {
@@ -3141,21 +3151,6 @@ func TestRPCGetBlockOrHeader(t *testing.T) {
31413151
}
31423152
genBlocks = 10
31433153
signer = types.HomesteadSigner{}
3144-
tx = types.NewTx(&types.LegacyTx{
3145-
Nonce: 11,
3146-
GasPrice: big.NewInt(11111),
3147-
Gas: 1111,
3148-
To: &acc2Addr,
3149-
Value: big.NewInt(111),
3150-
Data: []byte{0x11, 0x11, 0x11},
3151-
})
3152-
withdrawal = &types.Withdrawal{
3153-
Index: 0,
3154-
Validator: 1,
3155-
Address: common.Address{0x12, 0x34},
3156-
Amount: 10,
3157-
}
3158-
pending = types.NewBlock(&types.Header{Number: big.NewInt(11), Time: 42}, &types.Body{Transactions: types.Transactions{tx}, Withdrawals: types.Withdrawals{withdrawal}}, nil, blocktest.NewHasher())
31593154
)
31603155
backend := newTestBackend(t, genBlocks, genesis, ethash.NewFaker(), func(i int, b *core.BlockGen) {
31613156
// Transfer from account[0] to account[1]
@@ -3164,7 +3159,6 @@ func TestRPCGetBlockOrHeader(t *testing.T) {
31643159
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)
31653160
b.AddTx(tx)
31663161
})
3167-
backend.setPendingBlock(pending)
31683162
api := NewBlockChainAPI(backend)
31693163
blockHashes := make([]common.Hash, genBlocks+1)
31703164
ctx := context.Background()
@@ -3175,7 +3169,7 @@ func TestRPCGetBlockOrHeader(t *testing.T) {
31753169
}
31763170
blockHashes[i] = header.Hash()
31773171
}
3178-
pendingHash := pending.Hash()
3172+
pendingHash := backend.pending.Hash()
31793173

31803174
var testSuite = []struct {
31813175
blockNumber rpc.BlockNumber
@@ -3406,7 +3400,7 @@ func setupReceiptBackend(t *testing.T, genBlocks int) (*testBackend, []common.Ha
34063400
},
34073401
}
34083402
signer = types.LatestSignerForChainID(params.TestChainConfig.ChainID)
3409-
txHashes = make([]common.Hash, genBlocks)
3403+
txHashes = make([]common.Hash, 0, genBlocks)
34103404
)
34113405

34123406
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
34163410
)
34173411
b.SetPoS()
34183412
switch i {
3419-
case 0:
3420-
// transfer 1000wei
3421-
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)
34223413
case 1:
34233414
// create contract
34243415
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
34553446
BlobHashes: []common.Hash{{1}},
34563447
Value: new(uint256.Int),
34573448
}), signer, acc1Key)
3449+
default:
3450+
// transfer 1000wei
3451+
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)
34583452
}
34593453
if err != nil {
34603454
t.Errorf("failed to sign tx: %v", err)
34613455
}
34623456
if tx != nil {
34633457
b.AddTx(tx)
3464-
txHashes[i] = tx.Hash()
3458+
txHashes = append(txHashes, tx.Hash())
34653459
}
34663460
})
34673461
return backend, txHashes
@@ -3577,6 +3571,11 @@ func TestRPCGetBlockReceipts(t *testing.T) {
35773571
test: rpc.BlockNumberOrHashWithNumber(rpc.LatestBlockNumber),
35783572
file: "tag-latest",
35793573
},
3574+
// 3. pending tag
3575+
{
3576+
test: rpc.BlockNumberOrHashWithNumber(rpc.PendingBlockNumber),
3577+
file: "tag-pending",
3578+
},
35803579
// 4. block with legacy transfer tx(hash)
35813580
{
35823581
test: rpc.BlockNumberOrHashWithHash(blockHashes[1], false),
Lines changed: 22 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,49 +1,40 @@
11
{
2-
"difficulty": "0x0",
2+
"baseFeePerGas": "0xde56ab3",
3+
"difficulty": "0x20000",
34
"extraData": "0x",
4-
"gasLimit": "0x0",
5-
"gasUsed": "0x0",
5+
"gasLimit": "0x47e7c4",
6+
"gasUsed": "0x5208",
67
"hash": null,
78
"logsBloom": "0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
89
"miner": null,
910
"mixHash": "0x0000000000000000000000000000000000000000000000000000000000000000",
1011
"nonce": null,
1112
"number": "0xb",
12-
"parentHash": "0x0000000000000000000000000000000000000000000000000000000000000000",
13-
"receiptsRoot": "0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421",
13+
"parentHash": "0xa063415a5020f1569fae73ecb0d37bc5649ebe86d59e764a389eb37814bd42cb",
14+
"receiptsRoot": "0x056b23fbba480696b65fe5a59b8f2148a1299103c4f57df839233af2cf4ca2d2",
1415
"sha3Uncles": "0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347",
15-
"size": "0x256",
16-
"stateRoot": "0x0000000000000000000000000000000000000000000000000000000000000000",
17-
"timestamp": "0x2a",
16+
"size": "0x26a",
17+
"stateRoot": "0xce0e05397e548614a5b93254662174329466f8f4b1b391eb36fec9a7a591e58e",
18+
"timestamp": "0x6e",
1819
"transactions": [
1920
{
20-
"blockHash": "0x6cebd9f966ea686f44b981685e3f0eacea28591a7a86d7fbbe521a86e9f81165",
21+
"blockHash": "0xfda6c7cb7a3a712e0c424909a7724cab0448e89e286617fa8d5fd27f63f28bd2",
2122
"blockNumber": "0xb",
22-
"from": "0x0000000000000000000000000000000000000000",
23-
"gas": "0x457",
24-
"gasPrice": "0x2b67",
25-
"hash": "0x4afee081df5dff7a025964032871f7d4ba4d21baf5f6376a2f4a9f79fc506298",
26-
"input": "0x111111",
27-
"nonce": "0xb",
23+
"from": "0x703c4b2bd70c169f5717101caee543299fc946c7",
24+
"gas": "0x5208",
25+
"gasPrice": "0xde56ab3",
26+
"hash": "0xd773fbb47ec87b1a958ac16430943ddf2797ecae2b33fe7b16ddb334e30325ed",
27+
"input": "0x",
28+
"nonce": "0xa",
2829
"to": "0x0d3ab14bbad3d99f4203bd7a11acb94882050e7e",
2930
"transactionIndex": "0x0",
30-
"value": "0x6f",
31+
"value": "0x3e8",
3132
"type": "0x0",
32-
"chainId": "0x7fffffffffffffee",
33-
"v": "0x0",
34-
"r": "0x0",
35-
"s": "0x0"
33+
"v": "0x1c",
34+
"r": "0xfa029dacd66238d20cd649fe3b323bb458d2cfa4af7db0ff4f6b3e1039bc320a",
35+
"s": "0x52fb4d45c1d623f2f05508bae063a4728761d762ae45b8b0908ffea546f3d95e"
3636
}
3737
],
38-
"transactionsRoot": "0x98d9f6dd0aa479c0fb448f2627e9f1964aca699fccab8f6e95861547a4699e37",
39-
"uncles": [],
40-
"withdrawals": [
41-
{
42-
"index": "0x0",
43-
"validatorIndex": "0x1",
44-
"address": "0x1234000000000000000000000000000000000000",
45-
"amount": "0xa"
46-
}
47-
],
48-
"withdrawalsRoot": "0x73d756269cdfc22e7e17a3548e36f42f750ca06d7e3cd98d1b6d0eb5add9dc84"
38+
"transactionsRoot": "0x59abb8ec0655f66e66450d1502618bc64022ae2d2950fa471eec6e8da2846264",
39+
"uncles": []
4940
}
Lines changed: 12 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,24 @@
11
{
2-
"difficulty": "0x0",
2+
"baseFeePerGas": "0xde56ab3",
3+
"difficulty": "0x20000",
34
"extraData": "0x",
4-
"gasLimit": "0x0",
5-
"gasUsed": "0x0",
5+
"gasLimit": "0x47e7c4",
6+
"gasUsed": "0x5208",
67
"hash": null,
78
"logsBloom": "0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
89
"miner": null,
910
"mixHash": "0x0000000000000000000000000000000000000000000000000000000000000000",
1011
"nonce": null,
1112
"number": "0xb",
12-
"parentHash": "0x0000000000000000000000000000000000000000000000000000000000000000",
13-
"receiptsRoot": "0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421",
13+
"parentHash": "0xa063415a5020f1569fae73ecb0d37bc5649ebe86d59e764a389eb37814bd42cb",
14+
"receiptsRoot": "0x056b23fbba480696b65fe5a59b8f2148a1299103c4f57df839233af2cf4ca2d2",
1415
"sha3Uncles": "0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347",
15-
"size": "0x256",
16-
"stateRoot": "0x0000000000000000000000000000000000000000000000000000000000000000",
17-
"timestamp": "0x2a",
16+
"size": "0x26a",
17+
"stateRoot": "0xce0e05397e548614a5b93254662174329466f8f4b1b391eb36fec9a7a591e58e",
18+
"timestamp": "0x6e",
1819
"transactions": [
19-
"0x4afee081df5dff7a025964032871f7d4ba4d21baf5f6376a2f4a9f79fc506298"
20+
"0xd773fbb47ec87b1a958ac16430943ddf2797ecae2b33fe7b16ddb334e30325ed"
2021
],
21-
"transactionsRoot": "0x98d9f6dd0aa479c0fb448f2627e9f1964aca699fccab8f6e95861547a4699e37",
22-
"uncles": [],
23-
"withdrawals": [
24-
{
25-
"index": "0x0",
26-
"validatorIndex": "0x1",
27-
"address": "0x1234000000000000000000000000000000000000",
28-
"amount": "0xa"
29-
}
30-
],
31-
"withdrawalsRoot": "0x73d756269cdfc22e7e17a3548e36f42f750ca06d7e3cd98d1b6d0eb5add9dc84"
22+
"transactionsRoot": "0x59abb8ec0655f66e66450d1502618bc64022ae2d2950fa471eec6e8da2846264",
23+
"uncles": []
3224
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
[
2+
{
3+
"blockHash": "0xc74cf882395ec92eec3673d93a57f9a3bf1a5e696fae3e52f252059af62756c8",
4+
"blockNumber": "0x7",
5+
"contractAddress": null,
6+
"cumulativeGasUsed": "0x5208",
7+
"effectiveGasPrice": "0x17b07ddf",
8+
"from": "0x703c4b2bd70c169f5717101caee543299fc946c7",
9+
"gasUsed": "0x5208",
10+
"logs": [],
11+
"logsBloom": "0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
12+
"status": "0x1",
13+
"to": "0x0d3ab14bbad3d99f4203bd7a11acb94882050e7e",
14+
"transactionHash": "0xa7eeffe8111539a8f9725eb4d49e341efa1287d33190300adab220929daa5fac",
15+
"transactionIndex": "0x0",
16+
"type": "0x0"
17+
}
18+
]
Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
11
{
2-
"difficulty": "0x0",
2+
"baseFeePerGas": "0xde56ab3",
3+
"difficulty": "0x20000",
34
"extraData": "0x",
4-
"gasLimit": "0x0",
5-
"gasUsed": "0x0",
5+
"gasLimit": "0x47e7c4",
6+
"gasUsed": "0x5208",
67
"hash": null,
78
"logsBloom": "0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
89
"miner": null,
910
"mixHash": "0x0000000000000000000000000000000000000000000000000000000000000000",
1011
"nonce": null,
1112
"number": "0xb",
12-
"parentHash": "0x0000000000000000000000000000000000000000000000000000000000000000",
13-
"receiptsRoot": "0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421",
13+
"parentHash": "0xa063415a5020f1569fae73ecb0d37bc5649ebe86d59e764a389eb37814bd42cb",
14+
"receiptsRoot": "0x056b23fbba480696b65fe5a59b8f2148a1299103c4f57df839233af2cf4ca2d2",
1415
"sha3Uncles": "0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347",
15-
"stateRoot": "0x0000000000000000000000000000000000000000000000000000000000000000",
16-
"timestamp": "0x2a",
17-
"transactionsRoot": "0x98d9f6dd0aa479c0fb448f2627e9f1964aca699fccab8f6e95861547a4699e37",
18-
"withdrawalsRoot": "0x73d756269cdfc22e7e17a3548e36f42f750ca06d7e3cd98d1b6d0eb5add9dc84"
16+
"stateRoot": "0xce0e05397e548614a5b93254662174329466f8f4b1b391eb36fec9a7a591e58e",
17+
"timestamp": "0x6e",
18+
"transactionsRoot": "0x59abb8ec0655f66e66450d1502618bc64022ae2d2950fa471eec6e8da2846264"
1919
}

miner/miner.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,10 +144,10 @@ func (miner *Miner) getPending() *newPayloadResult {
144144
header := miner.chain.CurrentHeader()
145145
miner.pendingMu.Lock()
146146
defer miner.pendingMu.Unlock()
147+
147148
if cached := miner.pending.resolve(header.Hash()); cached != nil {
148149
return cached
149150
}
150-
151151
var (
152152
timestamp = uint64(time.Now().Unix())
153153
withdrawal types.Withdrawals

0 commit comments

Comments
 (0)