mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-08-19 02:12:23 +00:00
eth, les: share code to serve block receipts
This commit is contained in:
parent
daa1923141
commit
9b75ce0713
2 changed files with 23 additions and 25 deletions
|
|
@ -505,17 +505,8 @@ func (pm *ProtocolManager) handleMsg(p *peer) error {
|
||||||
} else if err != nil {
|
} else if err != nil {
|
||||||
return errResp(ErrDecode, "msg %v: %v", msg, err)
|
return errResp(ErrDecode, "msg %v: %v", msg, err)
|
||||||
}
|
}
|
||||||
// Retrieve the requested block's receipts, skipping if unknown to us
|
|
||||||
results := pm.blockchain.GetReceiptsByHash(hash)
|
if encoded := ServeBlockReceipts(pm.blockchain, hash); encoded != nil {
|
||||||
if results == nil {
|
|
||||||
if header := pm.blockchain.GetHeaderByHash(hash); header == nil || header.ReceiptHash != types.EmptyRootHash {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
}
|
|
||||||
// If known, encode and queue for response packet
|
|
||||||
if encoded, err := rlp.EncodeToBytes(results); err != nil {
|
|
||||||
log.Error("Failed to encode receipt", "err", err)
|
|
||||||
} else {
|
|
||||||
receipts = append(receipts, encoded)
|
receipts = append(receipts, encoded)
|
||||||
bytes += len(encoded)
|
bytes += len(encoded)
|
||||||
}
|
}
|
||||||
|
|
@ -694,6 +685,25 @@ func ServeBlockHeaders(blockchain *core.BlockChain, peer *p2p.Peer, originHash c
|
||||||
return headers
|
return headers
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ServeBlockReceipts fetches and encodes the block receipts of a single block,
|
||||||
|
// returning nil if unknown
|
||||||
|
func ServeBlockReceipts(blockchain *core.BlockChain, hash common.Hash) rlp.RawValue {
|
||||||
|
// Retrieve the requested block's receipts, skipping if unknown to us
|
||||||
|
results := blockchain.GetReceiptsByHash(hash)
|
||||||
|
if results == nil {
|
||||||
|
if header := blockchain.GetHeaderByHash(hash); header == nil || header.ReceiptHash != types.EmptyRootHash {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// If known, encode and queue for response packet
|
||||||
|
if encoded, err := rlp.EncodeToBytes(results); err != nil {
|
||||||
|
log.Error("Failed to encode receipt", "err", err)
|
||||||
|
return nil
|
||||||
|
} else {
|
||||||
|
return encoded
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// BroadcastBlock will either propagate a block to a subset of it's peers, or
|
// BroadcastBlock will either propagate a block to a subset of it's peers, or
|
||||||
// will only announce it's availability (depending what's requested).
|
// will only announce it's availability (depending what's requested).
|
||||||
func (pm *ProtocolManager) BroadcastBlock(block *types.Block, propagate bool) {
|
func (pm *ProtocolManager) BroadcastBlock(block *types.Block, propagate bool) {
|
||||||
|
|
|
||||||
|
|
@ -587,20 +587,8 @@ func (pm *ProtocolManager) handleMsg(p *peer) error {
|
||||||
if bytes >= softResponseLimit {
|
if bytes >= softResponseLimit {
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
// Retrieve the requested block's receipts, skipping if unknown to us
|
|
||||||
var results types.Receipts
|
if encoded := eth.ServeBlockReceipts(pm.blockchain.(*core.BlockChain), hash); encoded != nil {
|
||||||
if number := rawdb.ReadHeaderNumber(pm.chainDb, hash); number != nil {
|
|
||||||
results = rawdb.ReadReceipts(pm.chainDb, hash, *number)
|
|
||||||
}
|
|
||||||
if results == nil {
|
|
||||||
if header := pm.blockchain.GetHeaderByHash(hash); header == nil || header.ReceiptHash != types.EmptyRootHash {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
}
|
|
||||||
// If known, encode and queue for response packet
|
|
||||||
if encoded, err := rlp.EncodeToBytes(results); err != nil {
|
|
||||||
log.Error("Failed to encode receipt", "err", err)
|
|
||||||
} else {
|
|
||||||
receipts = append(receipts, encoded)
|
receipts = append(receipts, encoded)
|
||||||
bytes += len(encoded)
|
bytes += len(encoded)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue