Skip to content

Commit 6b0ddd1

Browse files
matthalpkaralabe
authored andcommitted
core, eth, les, light: store transaction receipts without txHash and gasCost
1 parent 1528b79 commit 6b0ddd1

11 files changed

Lines changed: 552 additions & 123 deletions

File tree

‎core/blockchain.go‎

Lines changed: 15 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -35,15 +35,14 @@ import (
3535
"github.com/ethereum/go-ethereum/core/state"
3636
"github.com/ethereum/go-ethereum/core/types"
3737
"github.com/ethereum/go-ethereum/core/vm"
38-
"github.com/ethereum/go-ethereum/crypto"
3938
"github.com/ethereum/go-ethereum/ethdb"
4039
"github.com/ethereum/go-ethereum/event"
4140
"github.com/ethereum/go-ethereum/log"
4241
"github.com/ethereum/go-ethereum/metrics"
4342
"github.com/ethereum/go-ethereum/params"
4443
"github.com/ethereum/go-ethereum/rlp"
4544
"github.com/ethereum/go-ethereum/trie"
46-
lru "github.com/hashicorp/golang-lru"
45+
"github.com/hashicorp/golang-lru"
4746
)
4847

4948
var (
@@ -79,12 +78,19 @@ const (
7978

8079
// BlockChainVersion ensures that an incompatible database forces a resync from scratch.
8180
//
82-
// During the process of upgrading the database version from 3 to 4,
83-
// the following incompatible database changes were added.
84-
// * the `BlockNumber`, `TxHash`, `TxIndex`, `BlockHash` and `Index` fields of log are deleted
85-
// * the `Bloom` field of receipt is deleted
86-
// * the `BlockIndex` and `TxIndex` fields of txlookup are deleted
87-
BlockChainVersion uint64 = 4
81+
// Changelog:
82+
//
83+
// - Version 4
84+
// The following incompatible database changes were added:
85+
// * the `BlockNumber`, `TxHash`, `TxIndex`, `BlockHash` and `Index` fields of log are deleted
86+
// * the `Bloom` field of receipt is deleted
87+
// * the `BlockIndex` and `TxIndex` fields of txlookup are deleted
88+
// - Version 5
89+
// The following incompatible database changes were added:
90+
// * the `TxHash`, `GasCost`, and `ContractAddress` fields are no longer stored for a receipt
91+
// * the `TxHash`, `GasCost`, and `ContractAddress` fields are computed by looking up the
92+
// receipts' corresponding block
93+
BlockChainVersion uint64 = 5
8894
)
8995

9096
// CacheConfig contains the configuration values for the trie caching/pruning
@@ -784,49 +790,6 @@ func (bc *BlockChain) Rollback(chain []common.Hash) {
784790
}
785791
}
786792

787-
// SetReceiptsData computes all the non-consensus fields of the receipts
788-
func SetReceiptsData(config *params.ChainConfig, block *types.Block, receipts types.Receipts) error {
789-
signer := types.MakeSigner(config, block.Number())
790-
791-
transactions, logIndex := block.Transactions(), uint(0)
792-
if len(transactions) != len(receipts) {
793-
return errors.New("transaction and receipt count mismatch")
794-
}
795-
796-
for j := 0; j < len(receipts); j++ {
797-
// The transaction hash can be retrieved from the transaction itself
798-
receipts[j].TxHash = transactions[j].Hash()
799-
800-
// block location fields
801-
receipts[j].BlockHash = block.Hash()
802-
receipts[j].BlockNumber = block.Number()
803-
receipts[j].TransactionIndex = uint(j)
804-
805-
// The contract address can be derived from the transaction itself
806-
if transactions[j].To() == nil {
807-
// Deriving the signer is expensive, only do if it's actually needed
808-
from, _ := types.Sender(signer, transactions[j])
809-
receipts[j].ContractAddress = crypto.CreateAddress(from, transactions[j].Nonce())
810-
}
811-
// The used gas can be calculated based on previous receipts
812-
if j == 0 {
813-
receipts[j].GasUsed = receipts[j].CumulativeGasUsed
814-
} else {
815-
receipts[j].GasUsed = receipts[j].CumulativeGasUsed - receipts[j-1].CumulativeGasUsed
816-
}
817-
// The derived log fields can simply be set from the block and transaction
818-
for k := 0; k < len(receipts[j].Logs); k++ {
819-
receipts[j].Logs[k].BlockNumber = block.NumberU64()
820-
receipts[j].Logs[k].BlockHash = block.Hash()
821-
receipts[j].Logs[k].TxHash = receipts[j].TxHash
822-
receipts[j].Logs[k].TxIndex = uint(j)
823-
receipts[j].Logs[k].Index = logIndex
824-
logIndex++
825-
}
826-
}
827-
return nil
828-
}
829-
830793
// InsertReceiptChain attempts to complete an already existing header chain with
831794
// transaction and receipt data.
832795
func (bc *BlockChain) InsertReceiptChain(blockChain types.Blocks, receiptChain []types.Receipts) (int, error) {
@@ -865,7 +828,7 @@ func (bc *BlockChain) InsertReceiptChain(blockChain types.Blocks, receiptChain [
865828
continue
866829
}
867830
// Compute all the non-consensus fields of the receipts
868-
if err := SetReceiptsData(bc.chainConfig, block, receipts); err != nil {
831+
if err := rawdb.SetReceiptsData(bc.chainConfig, block.Hash(), block.Number(), block.Body(), receipts); err != nil {
869832
return i, fmt.Errorf("failed to set receipts data: %v", err)
870833
}
871834
// Write all the data out into the database

‎core/chain_makers.go‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,15 @@ func (b *BlockGen) AddTxWithChain(bc *BlockChain, tx *types.Transaction) {
104104
b.receipts = append(b.receipts, receipt)
105105
}
106106

107+
// AddUncheckedTx forcefully adds a transaction to the block without any
108+
// validation.
109+
//
110+
// AddUncheckedTx will cause consensus failures when used during real
111+
// chain processing. This is best used in conjunction with raw block insertion.
112+
func (b *BlockGen) AddUncheckedTx(tx *types.Transaction) {
113+
b.txs = append(b.txs, tx)
114+
}
115+
107116
// Number returns the block number of the block being generated.
108117
func (b *BlockGen) Number() *big.Int {
109118
return new(big.Int).Set(b.header.Number)

‎core/rawdb/accessors_chain.go‎

Lines changed: 94 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,15 @@ package rawdb
1919
import (
2020
"bytes"
2121
"encoding/binary"
22+
"errors"
2223
"math/big"
2324

2425
"github.com/ethereum/go-ethereum/common"
2526
"github.com/ethereum/go-ethereum/core/types"
27+
"github.com/ethereum/go-ethereum/crypto"
2628
"github.com/ethereum/go-ethereum/ethdb"
2729
"github.com/ethereum/go-ethereum/log"
30+
"github.com/ethereum/go-ethereum/params"
2831
"github.com/ethereum/go-ethereum/rlp"
2932
)
3033

@@ -299,39 +302,112 @@ func ReadReceiptsRLP(db ethdb.Reader, hash common.Hash, number uint64) rlp.RawVa
299302
return data
300303
}
301304

302-
// ReadReceipts retrieves all the transaction receipts belonging to a block.
303-
func ReadReceipts(db ethdb.Reader, hash common.Hash, number uint64) types.Receipts {
305+
// ReadRawReceipts retrieves all the transaction receipts belonging to a block.
306+
// The receipt metadata fields are not guaranteed to be populated, so they
307+
// should not be used. Use ReadReceipts instead if the metadata is needed.
308+
func ReadRawReceipts(db ethdb.Reader, hash common.Hash, number uint64) types.Receipts {
304309
// Retrieve the flattened receipt slice
305310
data := ReadReceiptsRLP(db, hash, number)
306311
if len(data) == 0 {
307312
return nil
308313
}
314+
309315
// Convert the receipts from their storage form to their internal representation
310316
storageReceipts := []*types.ReceiptForStorage{}
311317
if err := rlp.DecodeBytes(data, &storageReceipts); err != nil {
312318
log.Error("Invalid receipt array RLP", "hash", hash, "err", err)
313319
return nil
314320
}
315-
receipts := make(types.Receipts, len(storageReceipts))
316-
logIndex := uint(0)
317-
for i, receipt := range storageReceipts {
318-
// Assemble deriving fields for log.
319-
for _, log := range receipt.Logs {
320-
log.TxHash = receipt.TxHash
321-
log.BlockHash = hash
322-
log.BlockNumber = number
323-
log.TxIndex = uint(i)
324-
log.Index = logIndex
325-
logIndex += 1
326-
}
327-
receipts[i] = (*types.Receipt)(receipt)
328-
receipts[i].BlockHash = hash
329-
receipts[i].BlockNumber = big.NewInt(0).SetUint64(number)
330-
receipts[i].TransactionIndex = uint(i)
321+
322+
var receipts types.Receipts
323+
for _, storageReceipt := range storageReceipts {
324+
receipt := (*types.Receipt)(storageReceipt)
325+
receipts = append(receipts, receipt)
331326
}
327+
332328
return receipts
333329
}
334330

331+
// ReadReceipts retrieves all the transaction receipts belonging to a block, including
332+
// its correspoinding metadata fields. If it is unable to populate these metadata
333+
// fields then nil is returned.
334+
//
335+
// The current implementation populates these metadata fields by reading the receipts'
336+
// corresponding block body, so if the block body is not found it will return nil even
337+
// if the receipt itself is stored.
338+
func ReadReceipts(db ethdb.Reader, hash common.Hash, number uint64) types.Receipts {
339+
receipts := ReadRawReceipts(db, hash, number)
340+
if receipts == nil {
341+
return receipts
342+
}
343+
344+
// Retrieve the block body to populate missing fields for receipts and logs
345+
body := ReadBody(db, hash, number)
346+
if body == nil {
347+
log.Error("Missing body but have receipt", "hash", hash, "number", number)
348+
return nil
349+
}
350+
351+
genesisHash := ReadCanonicalHash(db, 0)
352+
if genesisHash == (common.Hash{}) {
353+
log.Error("Missing genesis hash")
354+
return nil
355+
}
356+
357+
config := ReadChainConfig(db, genesisHash)
358+
if config == nil {
359+
log.Error("Missing chain config ", "hash", hash)
360+
return nil
361+
}
362+
363+
SetReceiptsData(config, hash, big.NewInt(int64(number)), body, receipts)
364+
365+
return receipts
366+
}
367+
368+
// SetReceiptsData computes all the non-consensus fields of the receipts
369+
func SetReceiptsData(config *params.ChainConfig, blockHash common.Hash, blockNumber *big.Int, body *types.Body, receipts types.Receipts) error {
370+
signer := types.MakeSigner(config, blockNumber)
371+
372+
transactions, logIndex := body.Transactions, uint(0)
373+
if len(transactions) != len(receipts) {
374+
return errors.New("transaction and receipt count mismatch")
375+
}
376+
377+
for j := 0; j < len(receipts); j++ {
378+
// The transaction hash can be retrieved from the transaction itself
379+
receipts[j].TxHash = transactions[j].Hash()
380+
381+
// block location fields
382+
receipts[j].BlockHash = blockHash
383+
receipts[j].BlockNumber = blockNumber
384+
receipts[j].TransactionIndex = uint(j)
385+
386+
// The contract address can be derived from the transaction itself
387+
if transactions[j].To() == nil {
388+
// Deriving the signer is expensive, only do if it's actually needed
389+
from, _ := types.Sender(signer, transactions[j])
390+
receipts[j].ContractAddress = crypto.CreateAddress(from, transactions[j].Nonce())
391+
}
392+
// The used gas can be calculated based on previous receipts
393+
if j == 0 {
394+
receipts[j].GasUsed = receipts[j].CumulativeGasUsed
395+
} else {
396+
receipts[j].GasUsed = receipts[j].CumulativeGasUsed - receipts[j-1].CumulativeGasUsed
397+
}
398+
// The derived log fields can simply be set from the block and transaction
399+
for k := 0; k < len(receipts[j].Logs); k++ {
400+
receipts[j].Logs[k].BlockNumber = blockNumber.Uint64()
401+
receipts[j].Logs[k].BlockHash = blockHash
402+
receipts[j].Logs[k].TxHash = receipts[j].TxHash
403+
receipts[j].Logs[k].TxIndex = uint(j)
404+
receipts[j].Logs[k].Index = logIndex
405+
logIndex++
406+
}
407+
}
408+
return nil
409+
}
410+
335411
// WriteReceipts stores all the transaction receipts belonging to a block.
336412
func WriteReceipts(db ethdb.Writer, hash common.Hash, number uint64, receipts types.Receipts) {
337413
// Convert the receipts into their storage form and serialize them

0 commit comments

Comments
 (0)