diff --git a/accounts/abi/bind/backends/simulated.go b/accounts/abi/bind/backends/simulated.go index 7ac8b58204..14bf7bd752 100644 --- a/accounts/abi/bind/backends/simulated.go +++ b/accounts/abi/bind/backends/simulated.go @@ -144,7 +144,8 @@ func (b *SimulatedBackend) StorageAt(ctx context.Context, contract common.Addres // TransactionReceipt returns the receipt of a transaction. func (b *SimulatedBackend) TransactionReceipt(ctx context.Context, txHash common.Hash) (*types.Receipt, error) { - return core.GetReceipt(b.database, txHash), nil + receipt, _, _, _ := core.GetReceipt(b.database, txHash) + return receipt, nil } // PendingCodeAt returns the code associated with an account in the pending state. diff --git a/core/blockchain.go b/core/blockchain.go index 6772ea2848..f103abdb5a 100644 --- a/core/blockchain.go +++ b/core/blockchain.go @@ -759,16 +759,10 @@ func (bc *BlockChain) InsertReceiptChain(blockChain types.Blocks, receiptChain [ log.Crit("Failed to write log blooms", "err", err) return } - if err := WriteTransactions(bc.chainDb, block); err != nil { - errs[index] = fmt.Errorf("failed to write individual transactions: %v", err) + if err := WriteLookupEntries(bc.chainDb, block); err != nil { + errs[index] = fmt.Errorf("failed to write lookup metadata: %v", err) atomic.AddInt32(&failed, 1) - log.Crit("Failed to write individual transactions", "err", err) - return - } - if err := WriteReceipts(bc.chainDb, receipts); err != nil { - errs[index] = fmt.Errorf("failed to write individual receipts: %v", err) - atomic.AddInt32(&failed, 1) - log.Crit("Failed to write individual receipts", "err", err) + log.Crit("Failed to write lookup metadata", "err", err) return } atomic.AddInt32(&stats.processed, 1) @@ -1002,12 +996,8 @@ func (bc *BlockChain) InsertChain(chain types.Blocks) (int, error) { blockInsertTimer.UpdateSince(bstart) events = append(events, ChainEvent{block, block.Hash(), logs}) - // This puts transactions in a extra db for rpc - if err := WriteTransactions(bc.chainDb, block); err != nil { - return i, err - } - // store the receipts - if err := WriteReceipts(bc.chainDb, receipts); err != nil { + // Write the positional metadata for transaction and receipt lookups + if err := WriteLookupEntries(bc.chainDb, block); err != nil { return i, err } // Write map map bloom filters @@ -1167,16 +1157,12 @@ func (bc *BlockChain) reorg(oldBlock, newBlock *types.Block) error { for _, block := range newChain { // insert the block in the canonical way, re-writing history bc.insert(block) - // write canonical receipts and transactions - if err := WriteTransactions(bc.chainDb, block); err != nil { - return err - } - receipts := GetBlockReceipts(bc.chainDb, block.Hash(), block.NumberU64()) - // write receipts - if err := WriteReceipts(bc.chainDb, receipts); err != nil { + // write lookup entries for hash based transaction/receipt searches + if err := WriteLookupEntries(bc.chainDb, block); err != nil { return err } // Write map map bloom filters + receipts := GetBlockReceipts(bc.chainDb, block.Hash(), block.NumberU64()) if err := WriteMipmapBloom(bc.chainDb, block.NumberU64(), receipts); err != nil { return err } @@ -1188,8 +1174,7 @@ func (bc *BlockChain) reorg(oldBlock, newBlock *types.Block) error { // When transactions get deleted from the database that means the // receipts that were created in the fork must also be deleted for _, tx := range diff { - DeleteReceipt(bc.chainDb, tx.Hash()) - DeleteTransaction(bc.chainDb, tx.Hash()) + DeleteLookupEntry(bc.chainDb, tx.Hash()) } // Must be posted in a goroutine because of the transaction pool trying // to acquire the chain manager lock diff --git a/core/blockchain_test.go b/core/blockchain_test.go index 371522ab77..5fa671e2b9 100644 --- a/core/blockchain_test.go +++ b/core/blockchain_test.go @@ -806,8 +806,8 @@ func TestChainTxReorgs(t *testing.T) { if txn, _, _, _ := GetTransaction(db, tx.Hash()); txn != nil { t.Errorf("drop %d: tx %v found while shouldn't have been", i, txn) } - if GetReceipt(db, tx.Hash()) != nil { - t.Errorf("drop %d: receipt found while shouldn't have been", i) + if rcpt, _, _, _ := GetReceipt(db, tx.Hash()); rcpt != nil { + t.Errorf("drop %d: receipt %v found while shouldn't have been", i, rcpt) } } // added tx @@ -815,7 +815,7 @@ func TestChainTxReorgs(t *testing.T) { if txn, _, _, _ := GetTransaction(db, tx.Hash()); txn == nil { t.Errorf("add %d: expected tx to be found", i) } - if GetReceipt(db, tx.Hash()) == nil { + if rcpt, _, _, _ := GetReceipt(db, tx.Hash()); rcpt == nil { t.Errorf("add %d: expected receipt to be found", i) } } @@ -824,7 +824,7 @@ func TestChainTxReorgs(t *testing.T) { if txn, _, _, _ := GetTransaction(db, tx.Hash()); txn == nil { t.Errorf("share %d: expected tx to be found", i) } - if GetReceipt(db, tx.Hash()) == nil { + if rcpt, _, _, _ := GetReceipt(db, tx.Hash()); rcpt == nil { t.Errorf("share %d: expected receipt to be found", i) } } diff --git a/core/database_util.go b/core/database_util.go index b4a230c9c9..20ffb2a9e8 100644 --- a/core/database_util.go +++ b/core/database_util.go @@ -45,11 +45,9 @@ var ( blockHashPrefix = []byte("H") // blockHashPrefix + hash -> num (uint64 big endian) bodyPrefix = []byte("b") // bodyPrefix + num (uint64 big endian) + hash -> block body blockReceiptsPrefix = []byte("r") // blockReceiptsPrefix + num (uint64 big endian) + hash -> block receipts + lookupPrefix = []byte("l") // lookupPrefix + hash -> transaction/receipt lookup metadata preimagePrefix = "secure-key-" // preimagePrefix + hash -> preimage - txMetaSuffix = []byte{0x01} - receiptsPrefix = []byte("receipts-") - mipmapPre = []byte("mipmap-log-bloom-") MIPMapLevels = []uint64{1000000, 500000, 100000, 50000, 1000} @@ -62,7 +60,8 @@ var ( oldBodySuffix = []byte("-body") oldBlockNumPrefix = []byte("block-num-") oldBlockReceiptsPrefix = []byte("receipts-block-") - oldBlockHashPrefix = []byte("block-hash-") // [deprecated by the header/block split, remove eventually] + oldReceiptsPrefix = []byte("receipts-") + oldTxMetaSuffix = []byte{0x01} ErrChainConfigNotFound = errors.New("ChainConfig not found") // general config not found error @@ -256,10 +255,43 @@ func GetBlockReceipts(db ethdb.Database, hash common.Hash, number uint64) types. return receipts } +// GetLookupEntry retrieves the positional metadata associated with a transaction +// hash to allow retrieving the transaction or receipt by hash. +func GetLookupEntry(db ethdb.Database, hash common.Hash) (common.Hash, uint64, uint64) { + // Define the transaction positional metadata + var entry struct { + BlockHash common.Hash + BlockIndex uint64 + Index uint64 + } + // Retrieve the transaction metadata and resolve the transaction from the body + data, _ := db.Get(append(lookupPrefix, hash.Bytes()...)) + if len(data) != 0 { + // New style transaction, retrieve contents from the block + if err := rlp.DecodeBytes(data, &entry); err != nil { + log.Error("Invalid lookup entry RLP", "hash", hash, "err", err) + return common.Hash{}, 0, 0 + } + return entry.BlockHash, entry.BlockIndex, entry.Index + } + return common.Hash{}, 0, 0 +} + // GetTransaction retrieves a specific transaction from the database, along with // its added positional metadata. func GetTransaction(db ethdb.Database, hash common.Hash) (*types.Transaction, common.Hash, uint64, uint64) { - // Retrieve the transaction itself from the database + // Retrieve the lookup metadata and resolve the transaction from the body + blockHash, blockNumber, txIndex := GetLookupEntry(db, hash) + + if blockHash != (common.Hash{}) { + body := GetBody(db, blockHash, blockNumber) + if body == nil || len(body.Transactions) <= int(txIndex) { + log.Error("Transaction refereced missing", "number", blockNumber, "hash", blockHash, "index", txIndex) + return nil, common.Hash{}, 0, 0 + } + return body.Transactions[txIndex], blockHash, blockNumber, txIndex + } + // Old transaction representation, load the transaction and it's metadata separately data, _ := db.Get(hash.Bytes()) if len(data) == 0 { return nil, common.Hash{}, 0, 0 @@ -269,7 +301,7 @@ func GetTransaction(db ethdb.Database, hash common.Hash) (*types.Transaction, co return nil, common.Hash{}, 0, 0 } // Retrieve the blockchain positional metadata - data, _ = db.Get(append(hash.Bytes(), txMetaSuffix...)) + data, _ = db.Get(append(hash.Bytes(), oldTxMetaSuffix...)) if len(data) == 0 { return nil, common.Hash{}, 0, 0 } @@ -284,18 +316,31 @@ func GetTransaction(db ethdb.Database, hash common.Hash) (*types.Transaction, co return &tx, meta.BlockHash, meta.BlockIndex, meta.Index } -// GetReceipt returns a receipt by hash -func GetReceipt(db ethdb.Database, hash common.Hash) *types.Receipt { - data, _ := db.Get(append(receiptsPrefix, hash[:]...)) +// GetReceipt retrieves a specific transaction receipt from the database, along with +// its added positional metadata. +func GetReceipt(db ethdb.Database, hash common.Hash) (*types.Receipt, common.Hash, uint64, uint64) { + // Retrieve the lookup metadata and resolve the receipt from the receipts + blockHash, blockNumber, receiptIndex := GetLookupEntry(db, hash) + + if blockHash != (common.Hash{}) { + receipts := GetBlockReceipts(db, blockHash, blockNumber) + if len(receipts) <= int(receiptIndex) { + log.Error("Receipt refereced missing", "number", blockNumber, "hash", blockHash, "index", receiptIndex) + return nil, common.Hash{}, 0, 0 + } + return receipts[receiptIndex], blockHash, blockNumber, receiptIndex + } + // Old receipt representation, load the receipt and set an unknown metadata + data, _ := db.Get(append(oldReceiptsPrefix, hash[:]...)) if len(data) == 0 { - return nil + return nil, common.Hash{}, 0, 0 } var receipt types.ReceiptForStorage err := rlp.DecodeBytes(data, &receipt) if err != nil { log.Error("Invalid receipt RLP", "hash", hash, "err", err) } - return (*types.Receipt)(&receipt) + return (*types.Receipt)(&receipt), common.Hash{}, 0, 0 } // WriteCanonicalHash stores the canonical hash for the given block number. @@ -416,24 +461,13 @@ func WriteBlockReceipts(db ethdb.Database, hash common.Hash, number uint64, rece return nil } -// WriteTransactions stores the transactions associated with a specific block -// into the given database. Beside writing the transaction, the function also -// stores a metadata entry along with the transaction, detailing the position -// of this within the blockchain. -func WriteTransactions(db ethdb.Database, block *types.Block) error { +// WriteLookupEntries stores a positional metadata for every transaction from +// a block, enabling hash based transaction and receipt lookups. +func WriteLookupEntries(db ethdb.Database, block *types.Block) error { batch := db.NewBatch() - // Iterate over each transaction and encode it with its metadata + // Iterate over each transaction and encode its metadata for i, tx := range block.Transactions() { - // Encode and queue up the transaction for storage - data, err := rlp.EncodeToBytes(tx) - if err != nil { - return err - } - if err = batch.Put(tx.Hash().Bytes(), data); err != nil { - return err - } - // Encode and queue up the transaction metadata for storage meta := struct { BlockHash common.Hash BlockIndex uint64 @@ -443,49 +477,17 @@ func WriteTransactions(db ethdb.Database, block *types.Block) error { BlockIndex: block.NumberU64(), Index: uint64(i), } - data, err = rlp.EncodeToBytes(meta) + data, err := rlp.EncodeToBytes(meta) if err != nil { return err } - if err := batch.Put(append(tx.Hash().Bytes(), txMetaSuffix...), data); err != nil { + if err := batch.Put(append(lookupPrefix, tx.Hash().Bytes()...), data); err != nil { return err } } // Write the scheduled data into the database if err := batch.Write(); err != nil { - log.Crit("Failed to store transactions", "err", err) - } - return nil -} - -// WriteReceipt stores a single transaction receipt into the database. -func WriteReceipt(db ethdb.Database, receipt *types.Receipt) error { - storageReceipt := (*types.ReceiptForStorage)(receipt) - data, err := rlp.EncodeToBytes(storageReceipt) - if err != nil { - return err - } - return db.Put(append(receiptsPrefix, receipt.TxHash.Bytes()...), data) -} - -// WriteReceipts stores a batch of transaction receipts into the database. -func WriteReceipts(db ethdb.Database, receipts types.Receipts) error { - batch := db.NewBatch() - - // Iterate over all the receipts and queue them for database injection - for _, receipt := range receipts { - storageReceipt := (*types.ReceiptForStorage)(receipt) - data, err := rlp.EncodeToBytes(storageReceipt) - if err != nil { - return err - } - if err := batch.Put(append(receiptsPrefix, receipt.TxHash.Bytes()...), data); err != nil { - return err - } - } - // Write the scheduled data into the database - if err := batch.Write(); err != nil { - log.Crit("Failed to store receipts", "err", err) + log.Crit("Failed to store lookup entries", "err", err) } return nil } @@ -524,15 +526,9 @@ func DeleteBlockReceipts(db ethdb.Database, hash common.Hash, number uint64) { db.Delete(append(append(blockReceiptsPrefix, encodeBlockNumber(number)...), hash.Bytes()...)) } -// DeleteTransaction removes all transaction data associated with a hash. -func DeleteTransaction(db ethdb.Database, hash common.Hash) { - db.Delete(hash.Bytes()) - db.Delete(append(hash.Bytes(), txMetaSuffix...)) -} - -// DeleteReceipt removes all receipt data associated with a transaction hash. -func DeleteReceipt(db ethdb.Database, hash common.Hash) { - db.Delete(append(receiptsPrefix, hash.Bytes()...)) +// DeleteLookupEntry removes all transaction data associated with a hash. +func DeleteLookupEntry(db ethdb.Database, hash common.Hash) { + db.Delete(append(lookupPrefix, hash.Bytes()...)) } // returns a formatted MIP mapped key by adding prefix, canonical number and level diff --git a/core/database_util_test.go b/core/database_util_test.go index 9f16b660a9..905f85ae20 100644 --- a/core/database_util_test.go +++ b/core/database_util_test.go @@ -290,8 +290,8 @@ func TestHeadStorage(t *testing.T) { } } -// Tests that transactions and associated metadata can be stored and retrieved. -func TestTransactionStorage(t *testing.T) { +// Tests that positional lookup metadata can be stored and retrieved. +func TestLookupStorage(t *testing.T) { db, _ := ethdb.NewMemDatabase() tx1 := types.NewTransaction(1, common.BytesToAddress([]byte{0x11}), big.NewInt(111), big.NewInt(1111), big.NewInt(11111), []byte{0x11, 0x11, 0x11}) @@ -308,7 +308,10 @@ func TestTransactionStorage(t *testing.T) { } } // Insert all the transactions into the database, and verify contents - if err := WriteTransactions(db, block); err != nil { + if err := WriteBlock(db, block); err != nil { + t.Fatalf("failed to write block contents: %v", err) + } + if err := WriteLookupEntries(db, block); err != nil { t.Fatalf("failed to write transactions: %v", err) } for i, tx := range txs { @@ -325,72 +328,13 @@ func TestTransactionStorage(t *testing.T) { } // Delete the transactions and check purge for i, tx := range txs { - DeleteTransaction(db, tx.Hash()) + DeleteLookupEntry(db, tx.Hash()) if txn, _, _, _ := GetTransaction(db, tx.Hash()); txn != nil { t.Fatalf("tx #%d [%x]: deleted transaction returned: %v", i, tx.Hash(), txn) } } } -// Tests that receipts can be stored and retrieved. -func TestReceiptStorage(t *testing.T) { - db, _ := ethdb.NewMemDatabase() - - receipt1 := &types.Receipt{ - PostState: []byte{0x01}, - CumulativeGasUsed: big.NewInt(1), - Logs: []*types.Log{ - {Address: common.BytesToAddress([]byte{0x11})}, - {Address: common.BytesToAddress([]byte{0x01, 0x11})}, - }, - TxHash: common.BytesToHash([]byte{0x11, 0x11}), - ContractAddress: common.BytesToAddress([]byte{0x01, 0x11, 0x11}), - GasUsed: big.NewInt(111111), - } - receipt2 := &types.Receipt{ - PostState: []byte{0x02}, - CumulativeGasUsed: big.NewInt(2), - Logs: []*types.Log{ - {Address: common.BytesToAddress([]byte{0x22})}, - {Address: common.BytesToAddress([]byte{0x02, 0x22})}, - }, - TxHash: common.BytesToHash([]byte{0x22, 0x22}), - ContractAddress: common.BytesToAddress([]byte{0x02, 0x22, 0x22}), - GasUsed: big.NewInt(222222), - } - receipts := []*types.Receipt{receipt1, receipt2} - - // Check that no receipt entries are in a pristine database - for i, receipt := range receipts { - if r := GetReceipt(db, receipt.TxHash); r != nil { - t.Fatalf("receipt #%d [%x]: non existent receipt returned: %v", i, receipt.TxHash, r) - } - } - // Insert all the receipts into the database, and verify contents - if err := WriteReceipts(db, receipts); err != nil { - t.Fatalf("failed to write receipts: %v", err) - } - for i, receipt := range receipts { - if r := GetReceipt(db, receipt.TxHash); r == nil { - t.Fatalf("receipt #%d [%x]: receipt not found", i, receipt.TxHash) - } else { - rlpHave, _ := rlp.EncodeToBytes(r) - rlpWant, _ := rlp.EncodeToBytes(receipt) - - if !bytes.Equal(rlpHave, rlpWant) { - t.Fatalf("receipt #%d [%x]: receipt mismatch: have %v, want %v", i, receipt.TxHash, r, receipt) - } - } - } - // Delete the receipts and check purge - for i, receipt := range receipts { - DeleteReceipt(db, receipt.TxHash) - if r := GetReceipt(db, receipt.TxHash); r != nil { - t.Fatalf("receipt #%d [%x]: deleted receipt returned: %v", i, receipt.TxHash, r) - } - } -} - // Tests that receipts associated with a single block can be stored and retrieved. func TestBlockReceiptStorage(t *testing.T) { db, _ := ethdb.NewMemDatabase() @@ -530,10 +474,6 @@ func TestMipmapChain(t *testing.T) { } // store the receipts - err := WriteReceipts(db, receipts) - if err != nil { - t.Fatal(err) - } WriteMipmapBloom(db, uint64(i+1), receipts) }) for i, block := range chain { diff --git a/internal/ethapi/api.go b/internal/ethapi/api.go index c22c56dfb3..183508e851 100644 --- a/internal/ethapi/api.go +++ b/internal/ethapi/api.go @@ -17,7 +17,6 @@ package ethapi import ( - "bytes" "context" "errors" "fmt" @@ -35,7 +34,6 @@ import ( "github.com/ethereum/go-ethereum/core/types" "github.com/ethereum/go-ethereum/core/vm" "github.com/ethereum/go-ethereum/crypto" - "github.com/ethereum/go-ethereum/ethdb" "github.com/ethereum/go-ethereum/log" "github.com/ethereum/go-ethereum/p2p" "github.com/ethereum/go-ethereum/params" @@ -835,7 +833,7 @@ func newRPCPendingTransaction(tx *types.Transaction) *RPCTransaction { } } -// newRPCTransaction returns a transaction that will serialize to the RPC representation. +// newRPCTransactionFromBlockIndex returns a transaction that will serialize to the RPC representation. func newRPCTransactionFromBlockIndex(b *types.Block, txIndex uint) (*RPCTransaction, error) { if txIndex < uint(len(b.Transactions())) { tx := b.Transactions()[txIndex] @@ -872,18 +870,16 @@ func newRPCRawTransactionFromBlockIndex(b *types.Block, txIndex uint) (hexutil.B tx := b.Transactions()[txIndex] return rlp.EncodeToBytes(tx) } - return nil, nil } // newRPCTransaction returns a transaction that will serialize to the RPC representation. -func newRPCTransaction(b *types.Block, txHash common.Hash) (*RPCTransaction, error) { +func newRPCTransaction(b *types.Block, hash common.Hash) (*RPCTransaction, error) { for idx, tx := range b.Transactions() { - if tx.Hash() == txHash { + if tx.Hash() == hash { return newRPCTransactionFromBlockIndex(b, uint(idx)) } } - return nil, nil } @@ -898,24 +894,6 @@ func NewPublicTransactionPoolAPI(b Backend, nonceLock *AddrLocker) *PublicTransa return &PublicTransactionPoolAPI{b, nonceLock} } -func getTransaction(chainDb ethdb.Database, b Backend, txHash common.Hash) (*types.Transaction, bool, error) { - txData, err := chainDb.Get(txHash.Bytes()) - isPending := false - tx := new(types.Transaction) - - if err == nil && len(txData) > 0 { - if err := rlp.DecodeBytes(txData, tx); err != nil { - return nil, isPending, err - } - } else { - // pending transaction? - tx = b.GetPoolTransaction(txHash) - isPending = true - } - - return tx, isPending, nil -} - // GetBlockTransactionCountByNumber returns the number of transactions in the block with the given block number. func (s *PublicTransactionPoolAPI) GetBlockTransactionCountByNumber(ctx context.Context, blockNr rpc.BlockNumber) *hexutil.Uint { if block, _ := s.b.BlockByNumber(ctx, blockNr); block != nil { @@ -976,90 +954,45 @@ func (s *PublicTransactionPoolAPI) GetTransactionCount(ctx context.Context, addr return (*hexutil.Uint64)(&nonce), state.Error() } -// getTransactionBlockData fetches the meta data for the given transaction from the chain database. This is useful to -// retrieve block information for a hash. It returns the block hash, block index and transaction index. -func getTransactionBlockData(chainDb ethdb.Database, txHash common.Hash) (common.Hash, uint64, uint64, error) { - var txBlock struct { - BlockHash common.Hash - BlockIndex uint64 - Index uint64 - } - - blockData, err := chainDb.Get(append(txHash.Bytes(), 0x0001)) - if err != nil { - return common.Hash{}, uint64(0), uint64(0), err - } - - reader := bytes.NewReader(blockData) - if err = rlp.Decode(reader, &txBlock); err != nil { - return common.Hash{}, uint64(0), uint64(0), err - } - - return txBlock.BlockHash, txBlock.BlockIndex, txBlock.Index, nil -} - // GetTransactionByHash returns the transaction for the given hash func (s *PublicTransactionPoolAPI) GetTransactionByHash(ctx context.Context, hash common.Hash) (*RPCTransaction, error) { - var tx *types.Transaction - var isPending bool - var err error - - if tx, isPending, err = getTransaction(s.b.ChainDb(), s.b, hash); err != nil { - log.Debug("Failed to retrieve transaction", "hash", hash, "err", err) - return nil, nil - } else if tx == nil { + // Try to return an already finalized transaction + if blockHash, _, index := core.GetLookupEntry(s.b.ChainDb(), hash); blockHash != (common.Hash{}) { + if block, _ := s.b.GetBlock(ctx, blockHash); block != nil { + return newRPCTransactionFromBlockIndex(block, uint(index)) + } return nil, nil } - if isPending { + // No finalized transaction, try to retrieve it from the pool + if tx := s.b.GetPoolTransaction(hash); tx != nil { return newRPCPendingTransaction(tx), nil } - - blockHash, _, _, err := getTransactionBlockData(s.b.ChainDb(), hash) - if err != nil { - log.Debug("Failed to retrieve transaction block", "hash", hash, "err", err) - return nil, nil - } - - if block, _ := s.b.GetBlock(ctx, blockHash); block != nil { - return newRPCTransaction(block, hash) - } + // Transaction unknown, return as such return nil, nil } // GetRawTransactionByHash returns the bytes of the transaction for the given hash. func (s *PublicTransactionPoolAPI) GetRawTransactionByHash(ctx context.Context, hash common.Hash) (hexutil.Bytes, error) { var tx *types.Transaction - var err error - if tx, _, err = getTransaction(s.b.ChainDb(), s.b, hash); err != nil { - log.Debug("Failed to retrieve transaction", "hash", hash, "err", err) - return nil, nil - } else if tx == nil { - return nil, nil + // Retrieve a finalized transaction, or a pooled otherwise + if tx, _, _, _ = core.GetTransaction(s.b.ChainDb(), hash); tx == nil { + if tx = s.b.GetPoolTransaction(hash); tx == nil { + // Transaction not found anywhere, abort + return nil, nil + } } - + // Serialize to RLP and return return rlp.EncodeToBytes(tx) } // GetTransactionReceipt returns the transaction receipt for the given transaction hash. func (s *PublicTransactionPoolAPI) GetTransactionReceipt(hash common.Hash) (map[string]interface{}, error) { - receipt := core.GetReceipt(s.b.ChainDb(), hash) + receipt, blockHash, blockNumber, index := core.GetReceipt(s.b.ChainDb(), hash) if receipt == nil { - log.Debug("Receipt not found for transaction", "hash", hash) - return nil, nil - } - - tx, _, err := getTransaction(s.b.ChainDb(), s.b, hash) - if err != nil { - log.Debug("Failed to retrieve transaction", "hash", hash, "err", err) - return nil, nil - } - - txBlock, blockIndex, index, err := getTransactionBlockData(s.b.ChainDb(), hash) - if err != nil { - log.Debug("Failed to retrieve transaction block", "hash", hash, "err", err) return nil, nil } + tx, _, _, _ := core.GetTransaction(s.b.ChainDb(), hash) var signer types.Signer = types.FrontierSigner{} if tx.Protected() { @@ -1069,8 +1002,8 @@ func (s *PublicTransactionPoolAPI) GetTransactionReceipt(hash common.Hash) (map[ fields := map[string]interface{}{ "root": hexutil.Bytes(receipt.PostState), - "blockHash": txBlock, - "blockNumber": hexutil.Uint64(blockIndex), + "blockHash": blockHash, + "blockNumber": hexutil.Uint64(blockNumber), "transactionHash": hash, "transactionIndex": hexutil.Uint64(index), "from": from, diff --git a/light/txpool.go b/light/txpool.go index 0430b280f6..a747867464 100644 --- a/light/txpool.go +++ b/light/txpool.go @@ -130,19 +130,6 @@ type txBlockData struct { Index uint64 } -// storeTxBlockData stores the block position of a mined tx in the local db -func (pool *TxPool) storeTxBlockData(txh common.Hash, tbd txBlockData) { - //fmt.Println("storeTxBlockData", txh, tbd) - data, _ := rlp.EncodeToBytes(tbd) - pool.chainDb.Put(append(txh[:], byte(1)), data) -} - -// removeTxBlockData removes the stored block position of a rolled back tx -func (pool *TxPool) removeTxBlockData(txh common.Hash) { - //fmt.Println("removeTxBlockData", txh) - pool.chainDb.Delete(append(txh[:], byte(1))) -} - // txStateChanges stores the recent changes between pending/mined states of // transactions. True means mined, false means rolled back, no entry means no change type txStateChanges map[common.Hash]bool @@ -172,59 +159,56 @@ func (txc txStateChanges) getLists() (mined []common.Hash, rollback []common.Has // checkMinedTxs checks newly added blocks for the currently pending transactions // and marks them as mined if necessary. It also stores block position in the db // and adds them to the received txStateChanges map. -func (pool *TxPool) checkMinedTxs(ctx context.Context, hash common.Hash, idx uint64, txc txStateChanges) error { - //fmt.Println("checkMinedTxs") +func (pool *TxPool) checkMinedTxs(ctx context.Context, hash common.Hash, number uint64, txc txStateChanges) error { + // If no transactions are pending, we don't care about anything if len(pool.pending) == 0 { return nil } - //fmt.Println("len(pool) =", len(pool.pending)) - - block, err := GetBlock(ctx, pool.odr, hash, idx) - var receipts types.Receipts + block, err := GetBlock(ctx, pool.odr, hash, number) if err != nil { - //fmt.Println(err) return err } - //fmt.Println("len(block.Transactions()) =", len(block.Transactions())) - + // Gather all the local transaction mined in this block list := pool.mined[hash] - for i, tx := range block.Transactions() { - txHash := tx.Hash() - //fmt.Println(" txHash:", txHash) - if tx, ok := pool.pending[txHash]; ok { - //fmt.Println("TX FOUND") - if receipts == nil { - receipts, err = GetBlockReceipts(ctx, pool.odr, hash, idx) - if err != nil { - return err - } - if len(receipts) != len(block.Transactions()) { - panic(nil) // should never happen if hashes did match - } - core.SetReceiptsData(pool.config, block, receipts) - } - //fmt.Println("WriteReceipt", receipts[i].TxHash) - core.WriteReceipt(pool.chainDb, receipts[i]) - pool.storeTxBlockData(txHash, txBlockData{hash, idx, uint64(i)}) - delete(pool.pending, txHash) + for _, tx := range block.Transactions() { + if _, ok := pool.pending[tx.Hash()]; ok { list = append(list, tx) - txc.setState(txHash, true) } } + // If some transactions have been mined, write the needed data to disk and update if list != nil { + // Retrieve all the receipts belonging to this block + receipts, err := GetBlockReceipts(ctx, pool.odr, hash, number) + if err != nil { + return err + } + // Write the transactions, receipts and lookup table to disk (all needed for lookups) + if err := core.WriteBlock(pool.chainDb, block); err != nil { + return err + } + if err := core.WriteBlockReceipts(pool.chainDb, hash, number, receipts); err != nil { + return err + } + if err := core.WriteLookupEntries(pool.chainDb, block); err != nil { + return err + } + // Update the transaction pool's state + for _, tx := range list { + delete(pool.pending, tx.Hash()) + txc.setState(tx.Hash(), true) + } pool.mined[hash] = list } return nil } // rollbackTxs marks the transactions contained in recently rolled back blocks -// as rolled back. It also removes block position info from the db and adds them -// to the received txStateChanges map. +// as rolled back. It also removes any positional lookup entries. func (pool *TxPool) rollbackTxs(hash common.Hash, txc txStateChanges) { if list, ok := pool.mined[hash]; ok { for _, tx := range list { txHash := tx.Hash() - pool.removeTxBlockData(txHash) + core.DeleteLookupEntry(pool.chainDb, txHash) pool.pending[txHash] = tx txc.setState(txHash, false) } diff --git a/miner/worker.go b/miner/worker.go index e44514755a..7efde21b97 100644 --- a/miner/worker.go +++ b/miner/worker.go @@ -293,9 +293,7 @@ func (self *worker) wait() { // check if canon block and write transactions if stat == core.CanonStatTy { // This puts transactions in a extra db for rpc - core.WriteTransactions(self.chainDb, block) - // store the receipts - core.WriteReceipts(self.chainDb, work.receipts) + core.WriteLookupEntries(self.chainDb, block) // Write map map bloom filters core.WriteMipmapBloom(self.chainDb, block.NumberU64(), work.receipts) // implicit by posting ChainHeadEvent