Avoid deep-copying txs for ReadTransaction

pull/1546/head
Eugene Kim 5 years ago
parent 258037fee7
commit ef87783790
  1. 6
      core/rawdb/accessors_indexes.go
  2. 4
      core/types/block.go
  3. 9
      core/types/bodyv0.go
  4. 9
      core/types/bodyv1.go

@ -71,8 +71,8 @@ func ReadTransaction(db DatabaseReader, hash common.Hash) (*types.Transaction, c
return nil, common.Hash{}, 0, 0
}
body := ReadBody(db, blockHash, blockNumber)
txs := body.Transactions()
if body == nil || len(txs) <= int(txIndex) {
tx := body.TransactionAt(int(txIndex))
if tx == nil {
utils.Logger().Error().
Uint64("number", blockNumber).
Str("hash", blockHash.Hex()).
@ -80,7 +80,7 @@ func ReadTransaction(db DatabaseReader, hash common.Hash) (*types.Transaction, c
Msg("Transaction referenced missing")
return nil, common.Hash{}, 0, 0
}
return txs[txIndex], blockHash, blockNumber, txIndex
return tx, blockHash, blockNumber, txIndex
}
// ReadReceipt retrieves a specific transaction receipt from the database, along with

@ -81,6 +81,10 @@ type BodyInterface interface {
// Transactions returns a deep copy the list of transactions in this block.
Transactions() []*Transaction
// TransactionAt returns the transaction at the given index in this block.
// It returns nil if index is out of bounds.
TransactionAt(index int) *Transaction
// SetTransactions sets the list of transactions with a deep copy of the
// given list.
SetTransactions(newTransactions []*Transaction)

@ -30,6 +30,15 @@ func (b *BodyV0) Transactions() (txs []*Transaction) {
return txs
}
// TransactionAt returns the transaction at the given index in this block.
// It returns nil if index is out of bounds.
func (b *BodyV0) TransactionAt(index int) *Transaction {
if index < 0 || index >= len(b.f.Transactions) {
return nil
}
return b.f.Transactions[index].Copy()
}
// SetTransactions sets the list of transactions with a deep copy of the given
// list.
func (b *BodyV0) SetTransactions(newTransactions []*Transaction) {

@ -30,6 +30,15 @@ func (b *BodyV1) Transactions() (txs []*Transaction) {
return txs
}
// TransactionAt returns the transaction at the given index in this block.
// It returns nil if index is out of bounds.
func (b *BodyV1) TransactionAt(index int) *Transaction {
if index < 0 || index >= len(b.f.Transactions) {
return nil
}
return b.f.Transactions[index].Copy()
}
// SetTransactions sets the list of transactions with a deep copy of the given
// list.
func (b *BodyV1) SetTransactions(newTransactions []*Transaction) {

Loading…
Cancel
Save