BlockDetails.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. /*
  2. This file is part of cpp-ethereum.
  3. cpp-ethereum is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation, either version 3 of the License, or
  6. (at your option) any later version.
  7. cpp-ethereum is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with cpp-ethereum. If not, see <http://www.gnu.org/licenses/>.
  13. */
  14. /** @file BlockDetails.h
  15. * @author Gav Wood <i@gavwood.com>
  16. * @date 2014
  17. */
  18. #pragma once
  19. #include <unordered_map>
  20. #include <libdevcore/Log.h>
  21. #include <libdevcore/RLP.h>
  22. #include "TransactionReceipt.h"
  23. namespace dev
  24. {
  25. namespace eth
  26. {
  27. // TODO: OPTIMISE: constructors take bytes, RLP used only in necessary classes.
  28. static const unsigned c_bloomIndexSize = 16;
  29. static const unsigned c_bloomIndexLevels = 2;
  30. static const unsigned InvalidNumber = (unsigned)-1;
  31. struct BlockDetails
  32. {
  33. BlockDetails(): number(InvalidNumber), totalDifficulty(Invalid256) {}
  34. BlockDetails(unsigned _n, u256 _tD, h256 _p, h256s _c): number(_n), totalDifficulty(_tD), parent(_p), children(_c) {}
  35. BlockDetails(RLP const& _r);
  36. bytes rlp() const;
  37. bool isNull() const { return number == InvalidNumber; }
  38. explicit operator bool() const { return !isNull(); }
  39. unsigned number = InvalidNumber;
  40. u256 totalDifficulty = Invalid256;
  41. h256 parent;
  42. h256s children;
  43. mutable unsigned size;
  44. };
  45. struct BlockLogBlooms
  46. {
  47. BlockLogBlooms() {}
  48. BlockLogBlooms(RLP const& _r) { blooms = _r.toVector<LogBloom>(); size = _r.data().size(); }
  49. bytes rlp() const { bytes r = dev::rlp(blooms); size = r.size(); return r; }
  50. LogBlooms blooms;
  51. mutable unsigned size;
  52. };
  53. struct BlocksBlooms
  54. {
  55. BlocksBlooms() {}
  56. BlocksBlooms(RLP const& _r) { blooms = _r.toArray<LogBloom, c_bloomIndexSize>(); size = _r.data().size(); }
  57. bytes rlp() const { bytes r = dev::rlp(blooms); size = r.size(); return r; }
  58. std::array<LogBloom, c_bloomIndexSize> blooms;
  59. mutable unsigned size;
  60. };
  61. struct BlockReceipts
  62. {
  63. BlockReceipts() {}
  64. BlockReceipts(RLP const& _r) { for (auto const& i: _r) receipts.emplace_back(i.data()); size = _r.data().size(); }
  65. bytes rlp() const { RLPStream s(receipts.size()); for (TransactionReceipt const& i: receipts) i.streamRLP(s); size = s.out().size(); return s.out(); }
  66. TransactionReceipts receipts;
  67. mutable unsigned size = 0;
  68. };
  69. struct BlockHash
  70. {
  71. BlockHash() {}
  72. BlockHash(h256 const& _h): value(_h) {}
  73. BlockHash(RLP const& _r) { value = _r.toHash<h256>(); }
  74. bytes rlp() const { return dev::rlp(value); }
  75. h256 value;
  76. static const unsigned size = 65;
  77. };
  78. struct TransactionAddress
  79. {
  80. TransactionAddress() {}
  81. TransactionAddress(RLP const& _rlp) { blockHash = _rlp[0].toHash<h256>(); index = _rlp[1].toInt<unsigned>(); }
  82. bytes rlp() const { RLPStream s(2); s << blockHash << index; return s.out(); }
  83. explicit operator bool() const { return !!blockHash; }
  84. h256 blockHash;
  85. unsigned index = 0;
  86. static const unsigned size = 67;
  87. };
  88. using BlockDetailsHash = std::unordered_map<h256, BlockDetails>;
  89. using BlockLogBloomsHash = std::unordered_map<h256, BlockLogBlooms>;
  90. using BlockReceiptsHash = std::unordered_map<h256, BlockReceipts>;
  91. using TransactionAddressHash = std::unordered_map<h256, TransactionAddress>;
  92. using BlockHashHash = std::unordered_map<uint64_t, BlockHash>;
  93. using BlocksBloomsHash = std::unordered_map<h256, BlocksBlooms>;
  94. static const BlockDetails NullBlockDetails;
  95. static const BlockLogBlooms NullBlockLogBlooms;
  96. static const BlockReceipts NullBlockReceipts;
  97. static const TransactionAddress NullTransactionAddress;
  98. static const BlockHash NullBlockHash;
  99. static const BlocksBlooms NullBlocksBlooms;
  100. }
  101. }