transaction_util.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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 common.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. // PutReceipts stores the receipts in the current database
  71. func PutReceipts(db common.Database, receipts types.Receipts) error {
  72. batch := new(leveldb.Batch)
  73. _, batchWrite := db.(*ethdb.LDBDatabase)
  74. for _, receipt := range receipts {
  75. storageReceipt := (*types.ReceiptForStorage)(receipt)
  76. bytes, err := rlp.EncodeToBytes(storageReceipt)
  77. if err != nil {
  78. return err
  79. }
  80. if batchWrite {
  81. batch.Put(append(receiptsPre, receipt.TxHash[:]...), bytes)
  82. } else {
  83. err = db.Put(append(receiptsPre, receipt.TxHash[:]...), bytes)
  84. if err != nil {
  85. return err
  86. }
  87. }
  88. }
  89. if db, ok := db.(*ethdb.LDBDatabase); ok {
  90. if err := db.LDB().Write(batch, nil); err != nil {
  91. return err
  92. }
  93. }
  94. return nil
  95. }
  96. // GetReceipt returns a receipt by hash
  97. func GetReceipt(db common.Database, txHash common.Hash) *types.Receipt {
  98. data, _ := db.Get(append(receiptsPre, txHash[:]...))
  99. if len(data) == 0 {
  100. return nil
  101. }
  102. var receipt types.Receipt
  103. err := rlp.DecodeBytes(data, &receipt)
  104. if err != nil {
  105. glog.V(logger.Core).Infoln("GetReceipt err:", err)
  106. }
  107. return &receipt
  108. }
  109. // GetBlockReceipts returns the receipts generated by the transactions
  110. // included in block's given hash.
  111. func GetBlockReceipts(db common.Database, hash common.Hash) types.Receipts {
  112. data, _ := db.Get(append(blockReceiptsPre, hash[:]...))
  113. if len(data) == 0 {
  114. return nil
  115. }
  116. var receipts types.Receipts
  117. err := rlp.DecodeBytes(data, &receipts)
  118. if err != nil {
  119. glog.V(logger.Core).Infoln("GetReceiptse err", err)
  120. }
  121. return receipts
  122. }
  123. // PutBlockReceipts stores the block's transactions associated receipts
  124. // and stores them by block hash in a single slice. This is required for
  125. // forks and chain reorgs
  126. func PutBlockReceipts(db common.Database, block *types.Block, receipts types.Receipts) error {
  127. rs := make([]*types.ReceiptForStorage, len(receipts))
  128. for i, receipt := range receipts {
  129. rs[i] = (*types.ReceiptForStorage)(receipt)
  130. }
  131. bytes, err := rlp.EncodeToBytes(rs)
  132. if err != nil {
  133. return err
  134. }
  135. hash := block.Hash()
  136. err = db.Put(append(blockReceiptsPre, hash[:]...), bytes)
  137. if err != nil {
  138. return err
  139. }
  140. return nil
  141. }