eth/downloader: improve receipt test logic

I didn't like that the test needed a special receipt list type for
hashing. It should use the protocol types as much as possible.
This commit is contained in:
Felix Lange 2026-02-28 00:16:16 +01:00
parent 3cf8e99374
commit 059f1f00f2

View file

@ -17,7 +17,6 @@
package downloader package downloader
import ( import (
"bytes"
"fmt" "fmt"
"math/big" "math/big"
"sync" "sync"
@ -41,16 +40,6 @@ import (
"github.com/ethereum/go-ethereum/trie" "github.com/ethereum/go-ethereum/trie"
) )
type receiptHashList struct {
items []eth.Receipt
bloomBuf [6]byte
}
func (l *receiptHashList) Len() int { return len(l.items) }
func (l *receiptHashList) EncodeIndex(i int, buf *bytes.Buffer) {
l.items[i].EncodeForHash(&l.bloomBuf, buf)
}
// downloadTester is a test simulator for mocking out local block chain. // downloadTester is a test simulator for mocking out local block chain.
type downloadTester struct { type downloadTester struct {
chain *core.BlockChain chain *core.BlockChain
@ -273,24 +262,23 @@ func (dlp *downloadTesterPeer) RequestBodies(hashes []common.Hash, sink chan *et
// batches of block receipts from the particularly requested peer. // batches of block receipts from the particularly requested peer.
func (dlp *downloadTesterPeer) RequestReceipts(hashes []common.Hash, sink chan *eth.Response) (*eth.Request, error) { func (dlp *downloadTesterPeer) RequestReceipts(hashes []common.Hash, sink chan *eth.Response) (*eth.Request, error) {
blobs := eth.ServiceGetReceiptsQuery(dlp.chain, hashes) blobs := eth.ServiceGetReceiptsQuery(dlp.chain, hashes)
receipts := make([]types.Receipts, blobs.Len())
receipts := make([]types.Receipts, len(blobs)) // compute hashes
hashes = make([]common.Hash, blobs.Len())
hashes = make([]common.Hash, len(blobs))
hasher := trie.NewStackTrie(nil) hasher := trie.NewStackTrie(nil)
for i, blob := range blobs { receiptLists, err := blobs.Items()
rlp.DecodeBytes(blob, &receipts[i]) if err != nil {
panic(err)
}
for i, rl := range receiptLists {
hashes[i] = types.DeriveSha(rl.Derivable(), hasher)
}
var items []eth.Receipt // deliver the response right away
rlp.DecodeBytes(blob, &items)
hashes[i] = types.DeriveSha(&receiptHashList{items: items}, hasher)
}
req := &eth.Request{
Peer: dlp.id,
}
resp := eth.ReceiptsRLPResponse(types.EncodeBlockReceiptLists(receipts)) resp := eth.ReceiptsRLPResponse(types.EncodeBlockReceiptLists(receipts))
res := &eth.Response{ res := &eth.Response{
Req: req, Req: &eth.Request{Peer: dlp.id},
Res: &resp, Res: &resp,
Meta: hashes, Meta: hashes,
Time: 1, Time: 1,
@ -299,7 +287,7 @@ func (dlp *downloadTesterPeer) RequestReceipts(hashes []common.Hash, sink chan *
go func() { go func() {
sink <- res sink <- res
}() }()
return req, nil return res.Req, nil
} }
// ID retrieves the peer's unique identifier. // ID retrieves the peer's unique identifier.