transaction_util.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. // Copyright 2015 The go-ethereum Authors
  2. // This file is part of go-ethereum.
  3. //
  4. // go-ethereum 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. // go-ethereum 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 go-ethereum. 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/logger"
  21. "github.com/ethereum/go-ethereum/logger/glog"
  22. "github.com/ethereum/go-ethereum/rlp"
  23. )
  24. var receiptsPre = []byte("receipts-")
  25. // PutTransactions stores the transactions in the given database
  26. func PutTransactions(db common.Database, block *types.Block, txs types.Transactions) {
  27. for i, tx := range block.Transactions() {
  28. rlpEnc, err := rlp.EncodeToBytes(tx)
  29. if err != nil {
  30. glog.V(logger.Debug).Infoln("Failed encoding tx", err)
  31. return
  32. }
  33. db.Put(tx.Hash().Bytes(), rlpEnc)
  34. var txExtra struct {
  35. BlockHash common.Hash
  36. BlockIndex uint64
  37. Index uint64
  38. }
  39. txExtra.BlockHash = block.Hash()
  40. txExtra.BlockIndex = block.NumberU64()
  41. txExtra.Index = uint64(i)
  42. rlpMeta, err := rlp.EncodeToBytes(txExtra)
  43. if err != nil {
  44. glog.V(logger.Debug).Infoln("Failed encoding tx meta data", err)
  45. return
  46. }
  47. db.Put(append(tx.Hash().Bytes(), 0x0001), rlpMeta)
  48. }
  49. }
  50. // PutReceipts stores the receipts in the current database
  51. func PutReceipts(db common.Database, receipts types.Receipts) error {
  52. for _, receipt := range receipts {
  53. storageReceipt := (*types.ReceiptForStorage)(receipt)
  54. bytes, err := rlp.EncodeToBytes(storageReceipt)
  55. if err != nil {
  56. return err
  57. }
  58. err = db.Put(append(receiptsPre, receipt.TxHash[:]...), bytes)
  59. if err != nil {
  60. return err
  61. }
  62. }
  63. return nil
  64. }
  65. // GetReceipt returns a receipt by hash
  66. func GetReceipt(db common.Database, txHash common.Hash) *types.Receipt {
  67. data, _ := db.Get(append(receiptsPre, txHash[:]...))
  68. if len(data) == 0 {
  69. return nil
  70. }
  71. var receipt types.Receipt
  72. err := rlp.DecodeBytes(data, &receipt)
  73. if err != nil {
  74. glog.V(logger.Core).Infoln("GetReceipt err:", err)
  75. }
  76. return &receipt
  77. }
  78. // GetReceiptFromBlock returns all receipts with the given block
  79. func GetReceiptsFromBlock(db common.Database, block *types.Block) types.Receipts {
  80. // at some point we want:
  81. //receipts := make(types.Receipts, len(block.Transactions()))
  82. // but since we need to support legacy, we can't (yet)
  83. var receipts types.Receipts
  84. for _, tx := range block.Transactions() {
  85. if receipt := GetReceipt(db, tx.Hash()); receipt != nil {
  86. receipts = append(receipts, receipt)
  87. }
  88. }
  89. return receipts
  90. }