Ethereum  PoC-8
The C++ Implementation of Ethereum
TransactionReceipt.cpp
Go to the documentation of this file.
1 /*
2  This file is part of cpp-ethereum.
3 
4  cpp-ethereum is free software: you can redistribute it and/or modify
5  it under the terms of the GNU 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  cpp-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 General Public License for more details.
13 
14  You should have received a copy of the GNU General Public License
15  along with cpp-ethereum. If not, see <http://www.gnu.org/licenses/>.
16 */
22 #include "TransactionReceipt.h"
23 #include <libethcore/Exceptions.h>
24 
25 #include <boost/variant/get.hpp>
26 
27 using namespace std;
28 using namespace dev;
29 using namespace dev::eth;
30 
31 TransactionReceipt::TransactionReceipt(bytesConstRef _rlp)
32 {
33  RLP r(_rlp);
34  if (!r.isList() || r.itemCount() != 4)
35  BOOST_THROW_EXCEPTION(InvalidTransactionReceiptFormat());
36 
37  if (!r[0].isData())
38  BOOST_THROW_EXCEPTION(InvalidTransactionReceiptFormat());
39 
40  if (r[0].size() == 32)
41  m_statusCodeOrStateRoot = (h256)r[0];
42  else if (r[0].isInt())
43  m_statusCodeOrStateRoot = (uint8_t)r[0];
44  else
45  BOOST_THROW_EXCEPTION(InvalidTransactionReceiptFormat());
46 
47  m_gasUsed = (u256)r[1];
48  m_bloom = (LogBloom)r[2];
49  for (auto const& i : r[3])
50  m_log.emplace_back(i);
51 
52 }
53 
54 TransactionReceipt::TransactionReceipt(h256 const& _root, u256 const& _gasUsed, LogEntries const& _log):
55  m_statusCodeOrStateRoot(_root),
56  m_gasUsed(_gasUsed),
57  m_bloom(eth::bloom(_log)),
58  m_log(_log)
59 {}
60 
61 TransactionReceipt::TransactionReceipt(uint8_t _status, u256 const& _gasUsed, LogEntries const& _log):
62  m_statusCodeOrStateRoot(_status),
63  m_gasUsed(_gasUsed),
64  m_bloom(eth::bloom(_log)),
65  m_log(_log)
66 {}
67 
69 {
70  _s.appendList(4);
71  if (hasStatusCode())
72  _s << statusCode();
73  else
74  _s << stateRoot();
75  _s << m_gasUsed << m_bloom;
76  _s.appendList(m_log.size());
77  for (LogEntry const& l: m_log)
78  l.streamRLP(_s);
79 }
80 
82 {
83  return m_statusCodeOrStateRoot.which() == 0;
84 }
85 
87 {
88  if (hasStatusCode())
89  return boost::get<uint8_t>(m_statusCodeOrStateRoot);
90  else
91  BOOST_THROW_EXCEPTION(TransactionReceiptVersionError());
92 }
93 
95 {
96  if (hasStatusCode())
97  BOOST_THROW_EXCEPTION(TransactionReceiptVersionError());
98  else
99  return boost::get<h256>(m_statusCodeOrStateRoot);
100 }
101 
102 std::ostream& dev::eth::operator<<(std::ostream& _out, TransactionReceipt const& _r)
103 {
104  if (_r.hasStatusCode())
105  _out << "Status: " << _r.statusCode() << std::endl;
106  else
107  _out << "Root: " << _r.stateRoot() << std::endl;
108  _out << "Gas used: " << _r.cumulativeGasUsed() << std::endl;
109  _out << "Logs: " << _r.log().size() << " entries:" << std::endl;
110  for (LogEntry const& i: _r.log())
111  {
112  _out << "Address " << i.address << ". Topics:" << std::endl;
113  for (auto const& j: i.topics)
114  _out << " " << j << std::endl;
115  _out << " Data: " << toHex(i.data) << std::endl;
116  }
117  _out << "Bloom: " << _r.bloom() << std::endl;
118  return _out;
119 }
dev::eth::TransactionReceipt::stateRoot
h256 const & stateRoot() const
Definition: TransactionReceipt.cpp:94
dev::vector_ref< byte const >
dev::h256
FixedHash< 32 > h256
Definition: FixedHash.h:356
dev::eth::TransactionReceipt::cumulativeGasUsed
u256 const & cumulativeGasUsed() const
Definition: TransactionReceipt.h:54
Exceptions.h
dev::eth::TransactionReceipt::streamRLP
void streamRLP(RLPStream &_s) const
Definition: TransactionReceipt.cpp:68
dev::eth
Definition: BasicAuthority.h:32
dev::eth::bloom
LogBloom bloom(LogEntries const &_logs)
Definition: LogEntry.h:96
dev::RLP::isList
bool isList() const
List value.
Definition: RLP.h:95
dev::FixedHash< 32 >
dev::eth::LogBloom
h2048 LogBloom
The log bloom's size (2048-bit).
Definition: Common.h:50
dev::eth::LogEntries
std::vector< LogEntry > LogEntries
Definition: LogEntry.h:51
TransactionReceipt.h
dev::eth::TransactionReceipt::TransactionReceipt
TransactionReceipt(bytesConstRef _rlp)
Definition: TransactionReceipt.cpp:31
dev::eth::TransactionReceipt::statusCode
uint8_t statusCode() const
Definition: TransactionReceipt.cpp:86
dev::eth::LogEntry
Definition: LogEntry.h:35
dev::RLPStream
Class for writing to an RLP bytestream.
Definition: RLP.h:370
dev::eth::TransactionReceipt::hasStatusCode
bool hasStatusCode() const
Definition: TransactionReceipt.cpp:81
dev::RLP::itemCount
size_t itemCount() const
Definition: RLP.h:101
std
Definition: FixedHash.h:393
dev::u256
boost::multiprecision::number< boost::multiprecision::cpp_int_backend< 256, 256, boost::multiprecision::unsigned_magnitude, boost::multiprecision::unchecked, void > > u256
Definition: Common.h:121
dev::eth::operator<<
std::ostream & operator<<(std::ostream &_out, BlockHeader const &_bi)
Definition: BlockHeader.h:217
dev::eth::TransactionReceipt::log
LogEntries const & log() const
Definition: TransactionReceipt.h:56
dev::eth::TransactionReceipt
Definition: TransactionReceipt.h:40
dev
Definition: Address.cpp:21
dev::toHex
std::string toHex(Iterator _it, Iterator _end, std::string const &_prefix)
Definition: CommonData.h:46
dev::eth::TransactionReceipt::bloom
LogBloom const & bloom() const
Definition: TransactionReceipt.h:55
dev::RLP
Definition: RLP.h:48
dev::RLPStream::appendList
RLPStream & appendList(size_t _items)
Appends a list.
Definition: RLP.cpp:268