transaction_util.go 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. // Copyright 2015 The go-ethereum Authors
  2. // This file is part of the go-ethereum library.
  3. //
  4. // The go-ethereum library is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU Lesser General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // The go-ethereum library is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU Lesser General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU Lesser General Public License
  15. // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
  16. package core
  17. import (
  18. "fmt"
  19. "github.com/ethereum/go-ethereum/common"
  20. "github.com/ethereum/go-ethereum/core/types"
  21. "github.com/ethereum/go-ethereum/ethdb"
  22. "github.com/ethereum/go-ethereum/logger"
  23. "github.com/ethereum/go-ethereum/logger/glog"
  24. "github.com/ethereum/go-ethereum/rlp"
  25. "github.com/syndtr/goleveldb/leveldb"
  26. )
  27. var (
  28. receiptsPre = []byte("receipts-")
  29. blockReceiptsPre = []byte("receipts-block-")
  30. )
  31. // PutTransactions stores the transactions in the given database
  32. func PutTransactions(db ethdb.Database, block *types.Block, txs types.Transactions) error {
  33. batch := db.NewBatch()
  34. for i, tx := range block.Transactions() {
  35. rlpEnc, err := rlp.EncodeToBytes(tx)
  36. if err != nil {
  37. return fmt.Errorf("failed encoding tx: %v", err)
  38. }
  39. batch.Put(tx.Hash().Bytes(), rlpEnc)
  40. var txExtra struct {
  41. BlockHash common.Hash
  42. BlockIndex uint64
  43. Index uint64
  44. }
  45. txExtra.BlockHash = block.Hash()
  46. txExtra.BlockIndex = block.NumberU64()
  47. txExtra.Index = uint64(i)
  48. rlpMeta, err := rlp.EncodeToBytes(txExtra)
  49. if err != nil {
  50. return fmt.Errorf("failed encoding tx meta data: %v", err)
  51. }
  52. batch.Put(append(tx.Hash().Bytes(), 0x0001), rlpMeta)
  53. }
  54. if err := batch.Write(); err != nil {
  55. return fmt.Errorf("failed writing tx to db: %v", err)
  56. }
  57. return nil
  58. }
  59. func DeleteTransaction(db ethdb.Database, txHash common.Hash) {
  60. db.Delete(txHash[:])
  61. }
  62. func GetTransaction(db ethdb.Database, txhash common.Hash) *types.Transaction {
  63. data, _ := db.Get(txhash[:])
  64. if len(data) != 0 {
  65. var tx types.Transaction
  66. if err := rlp.DecodeBytes(data, &tx); err != nil {
  67. return nil
  68. }
  69. return &tx
  70. }
  71. return nil
  72. }
  73. // PutReceipts stores the receipts in the current database
  74. func PutReceipts(db ethdb.Database, receipts types.Receipts) error {
  75. batch := new(leveldb.Batch)
  76. _, batchWrite := db.(*ethdb.LDBDatabase)
  77. for _, receipt := range receipts {
  78. storageReceipt := (*types.ReceiptForStorage)(receipt)
  79. bytes, err := rlp.EncodeToBytes(storageReceipt)
  80. if err != nil {
  81. return err
  82. }
  83. if batchWrite {
  84. batch.Put(append(receiptsPre, receipt.TxHash[:]...), bytes)
  85. } else {
  86. err = db.Put(append(receiptsPre, receipt.TxHash[:]...), bytes)
  87. if err != nil {
  88. return err
  89. }
  90. }
  91. }
  92. if db, ok := db.(*ethdb.LDBDatabase); ok {
  93. if err := db.LDB().Write(batch, nil); err != nil {
  94. return err
  95. }
  96. }
  97. return nil
  98. }
  99. // Delete a receipts from the database
  100. func DeleteReceipt(db ethdb.Database, txHash common.Hash) {
  101. db.Delete(append(receiptsPre, txHash[:]...))
  102. }
  103. // GetReceipt returns a receipt by hash
  104. func GetReceipt(db ethdb.Database, txHash common.Hash) *types.Receipt {
  105. data, _ := db.Get(append(receiptsPre, txHash[:]...))
  106. if len(data) == 0 {
  107. return nil
  108. }
  109. var receipt types.Receipt
  110. err := rlp.DecodeBytes(data, &receipt)
  111. if err != nil {
  112. glog.V(logger.Core).Infoln("GetReceipt err:", err)
  113. }
  114. return &receipt
  115. }
  116. // GetBlockReceipts returns the receipts generated by the transactions
  117. // included in block's given hash.
  118. func GetBlockReceipts(db ethdb.Database, hash common.Hash) types.Receipts {
  119. data, _ := db.Get(append(blockReceiptsPre, hash[:]...))
  120. if len(data) == 0 {
  121. return nil
  122. }
  123. receipts := new(types.Receipts)
  124. if err := rlp.DecodeBytes(data, receipts); err != nil {
  125. glog.V(logger.Error).Infof("invalid receipt array RLP for hash %x: %v", hash, err)
  126. return nil
  127. }
  128. return *receipts
  129. }
  130. // PutBlockReceipts stores the block's transactions associated receipts
  131. // and stores them by block hash in a single slice. This is required for
  132. // forks and chain reorgs
  133. func PutBlockReceipts(db ethdb.Database, block *types.Block, receipts types.Receipts) error {
  134. rs := make([]*types.ReceiptForStorage, len(receipts))
  135. for i, receipt := range receipts {
  136. rs[i] = (*types.ReceiptForStorage)(receipt)
  137. }
  138. bytes, err := rlp.EncodeToBytes(rs)
  139. if err != nil {
  140. return err
  141. }
  142. hash := block.Hash()
  143. err = db.Put(append(blockReceiptsPre, hash[:]...), bytes)
  144. if err != nil {
  145. return err
  146. }
  147. return nil
  148. }