transaction_util.go 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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. "github.com/ethereum/go-ethereum/common"
  19. "github.com/ethereum/go-ethereum/core/types"
  20. "github.com/ethereum/go-ethereum/ethdb"
  21. "github.com/ethereum/go-ethereum/logger"
  22. "github.com/ethereum/go-ethereum/logger/glog"
  23. "github.com/ethereum/go-ethereum/rlp"
  24. "github.com/syndtr/goleveldb/leveldb"
  25. )
  26. var (
  27. receiptsPre = []byte("receipts-")
  28. blockReceiptsPre = []byte("receipts-block-")
  29. )
  30. // PutTransactions stores the transactions in the given database
  31. func PutTransactions(db ethdb.Database, block *types.Block, txs types.Transactions) {
  32. batch := new(leveldb.Batch)
  33. _, batchWrite := db.(*ethdb.LDBDatabase)
  34. for i, tx := range block.Transactions() {
  35. rlpEnc, err := rlp.EncodeToBytes(tx)
  36. if err != nil {
  37. glog.V(logger.Debug).Infoln("Failed encoding tx", err)
  38. return
  39. }
  40. if batchWrite {
  41. batch.Put(tx.Hash().Bytes(), rlpEnc)
  42. } else {
  43. db.Put(tx.Hash().Bytes(), rlpEnc)
  44. }
  45. var txExtra struct {
  46. BlockHash common.Hash
  47. BlockIndex uint64
  48. Index uint64
  49. }
  50. txExtra.BlockHash = block.Hash()
  51. txExtra.BlockIndex = block.NumberU64()
  52. txExtra.Index = uint64(i)
  53. rlpMeta, err := rlp.EncodeToBytes(txExtra)
  54. if err != nil {
  55. glog.V(logger.Debug).Infoln("Failed encoding tx meta data", err)
  56. return
  57. }
  58. if batchWrite {
  59. batch.Put(append(tx.Hash().Bytes(), 0x0001), rlpMeta)
  60. } else {
  61. db.Put(append(tx.Hash().Bytes(), 0x0001), rlpMeta)
  62. }
  63. }
  64. if db, ok := db.(*ethdb.LDBDatabase); ok {
  65. if err := db.LDB().Write(batch, nil); err != nil {
  66. glog.V(logger.Error).Infoln("db write err:", err)
  67. }
  68. }
  69. }
  70. func DeleteTransaction(db ethdb.Database, txHash common.Hash) {
  71. db.Delete(txHash[:])
  72. }
  73. func GetTransaction(db ethdb.Database, txhash common.Hash) *types.Transaction {
  74. data, _ := db.Get(txhash[:])
  75. if len(data) != 0 {
  76. var tx types.Transaction
  77. if err := rlp.DecodeBytes(data, &tx); err != nil {
  78. return nil
  79. }
  80. return &tx
  81. }
  82. return nil
  83. }
  84. // PutReceipts stores the receipts in the current database
  85. func PutReceipts(db ethdb.Database, receipts types.Receipts) error {
  86. batch := new(leveldb.Batch)
  87. _, batchWrite := db.(*ethdb.LDBDatabase)
  88. for _, receipt := range receipts {
  89. storageReceipt := (*types.ReceiptForStorage)(receipt)
  90. bytes, err := rlp.EncodeToBytes(storageReceipt)
  91. if err != nil {
  92. return err
  93. }
  94. if batchWrite {
  95. batch.Put(append(receiptsPre, receipt.TxHash[:]...), bytes)
  96. } else {
  97. err = db.Put(append(receiptsPre, receipt.TxHash[:]...), bytes)
  98. if err != nil {
  99. return err
  100. }
  101. }
  102. }
  103. if db, ok := db.(*ethdb.LDBDatabase); ok {
  104. if err := db.LDB().Write(batch, nil); err != nil {
  105. return err
  106. }
  107. }
  108. return nil
  109. }
  110. // Delete a receipts from the database
  111. func DeleteReceipt(db ethdb.Database, txHash common.Hash) {
  112. db.Delete(append(receiptsPre, txHash[:]...))
  113. }
  114. // GetReceipt returns a receipt by hash
  115. func GetReceipt(db ethdb.Database, txHash common.Hash) *types.Receipt {
  116. data, _ := db.Get(append(receiptsPre, txHash[:]...))
  117. if len(data) == 0 {
  118. return nil
  119. }
  120. var receipt types.Receipt
  121. err := rlp.DecodeBytes(data, &receipt)
  122. if err != nil {
  123. glog.V(logger.Core).Infoln("GetReceipt err:", err)
  124. }
  125. return &receipt
  126. }
  127. // GetBlockReceipts returns the receipts generated by the transactions
  128. // included in block's given hash.
  129. func GetBlockReceipts(db ethdb.Database, hash common.Hash) types.Receipts {
  130. data, _ := db.Get(append(blockReceiptsPre, hash[:]...))
  131. if len(data) == 0 {
  132. return nil
  133. }
  134. var receipts types.Receipts
  135. err := rlp.DecodeBytes(data, &receipts)
  136. if err != nil {
  137. glog.V(logger.Core).Infoln("GetReceiptse err", err)
  138. }
  139. return receipts
  140. }
  141. // PutBlockReceipts stores the block's transactions associated receipts
  142. // and stores them by block hash in a single slice. This is required for
  143. // forks and chain reorgs
  144. func PutBlockReceipts(db ethdb.Database, block *types.Block, receipts types.Receipts) error {
  145. rs := make([]*types.ReceiptForStorage, len(receipts))
  146. for i, receipt := range receipts {
  147. rs[i] = (*types.ReceiptForStorage)(receipt)
  148. }
  149. bytes, err := rlp.EncodeToBytes(rs)
  150. if err != nil {
  151. return err
  152. }
  153. hash := block.Hash()
  154. err = db.Put(append(blockReceiptsPre, hash[:]...), bytes)
  155. if err != nil {
  156. return err
  157. }
  158. return nil
  159. }