Skip to content
This repository has been archived by the owner on Oct 28, 2021. It is now read-only.

Commit

Permalink
read variants in a simpler way
Browse files Browse the repository at this point in the history
  • Loading branch information
pirapira committed Jul 20, 2017
1 parent 0ddd07a commit eca4fbf
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 57 deletions.
2 changes: 1 addition & 1 deletion libethereum/Block.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -868,7 +868,7 @@ h256 Block::stateRootBeforeTx(unsigned _i) const
{
return (_i > 0 ? receipt(_i - 1).stateRoot() : m_previousBlock.stateRoot());
}
catch(TransactionReceiptVersionError const&)
catch (TransactionReceiptVersionError const&)
{
return {};
}
Expand Down
62 changes: 15 additions & 47 deletions libethereum/TransactionReceipt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
#include "TransactionReceipt.h"
#include <libethcore/Exceptions.h>

#include <boost/variant/static_visitor.hpp>
#include <boost/variant/get.hpp>


Expand All @@ -38,10 +37,13 @@ TransactionReceipt::TransactionReceipt(bytesConstRef _rlp)

if (!r[0].isData())
BOOST_THROW_EXCEPTION(InvalidTransactionReceiptFormat());

if (r[0].size() == 32)
m_statusCodeOrStateRoot = (h256)r[0];
else if (r[0].size() == 1)
m_statusCodeOrStateRoot = (uint8_t)r[0];
else
BOOST_THROW_EXCEPTION(InvalidTransactionReceiptFormat());

m_gasUsed = (u256)r[1];
m_bloom = (LogBloom)r[2];
Expand All @@ -66,22 +68,11 @@ TransactionReceipt::TransactionReceipt(uint8_t _status, u256 const& _gasUsed, Lo

void TransactionReceipt::streamRLP(RLPStream& _s) const
{
struct appenderVisitor : boost::static_visitor<void>
{
appenderVisitor(RLPStream& _s) : m_stream(_s) {}
RLPStream& m_stream;
void operator()(uint8_t _statusCode) const
{
m_stream << _statusCode;
}
void operator()(h256 _stateRoot) const
{
m_stream << _stateRoot;
}
};

_s.appendList(4);
boost::apply_visitor(appenderVisitor(_s), m_statusCodeOrStateRoot);
if (hasStatusCode())
_s << statusCode();
else
_s << stateRoot();
_s << m_gasUsed << m_bloom;
_s.appendList(m_log.size());
for (LogEntry const& l: m_log)
Expand All @@ -90,46 +81,23 @@ void TransactionReceipt::streamRLP(RLPStream& _s) const

bool TransactionReceipt::hasStatusCode() const
{
struct hasStatusCodeVisitor : boost::static_visitor<bool>
{
bool operator()(uint8_t) const
{
return true;
}
bool operator()(h256) const
{
return false;
}
};
return boost::apply_visitor(hasStatusCodeVisitor(), m_statusCodeOrStateRoot);
return m_statusCodeOrStateRoot.which() == 0;
}

uint8_t TransactionReceipt::statusCode() const
{
struct statusCodeVisitor : boost::static_visitor<uint8_t>
{
uint8_t operator()(uint8_t _statusCode) const
{
return _statusCode;
}
uint8_t operator()(h256) const
{
BOOST_THROW_EXCEPTION(TransactionReceiptVersionError());
}
};
return boost::apply_visitor(statusCodeVisitor(), m_statusCodeOrStateRoot);
if (hasStatusCode())
return boost::get<uint8_t>(m_statusCodeOrStateRoot);
else
BOOST_THROW_EXCEPTION(TransactionReceiptVersionError());
}

h256 const& TransactionReceipt::stateRoot() const
{
try
{
return boost::get<h256>(m_statusCodeOrStateRoot);
}
catch(const boost::bad_get&)
{
if (hasStatusCode())
BOOST_THROW_EXCEPTION(TransactionReceiptVersionError());
}
else
return boost::get<h256>(m_statusCodeOrStateRoot);
}

std::ostream& dev::eth::operator<<(std::ostream& _out, TransactionReceipt const& _r)
Expand Down
4 changes: 2 additions & 2 deletions libethereum/TransactionReceipt.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,10 @@ class TransactionReceipt
/// @returns true if the receipt has a status code. Otherwise the receipt has a state root (pre-EIP658).
bool hasStatusCode() const;
/// @returns the state root.
/// @throw TransactionReceiptVersionError when m_hasStatusCode is true.
/// @throw TransactionReceiptVersionError when the receipt has a status code instead of a state root.
h256 const& stateRoot() const;
/// @returns the status code.
/// @throw TransactionReceiptVersionError when m_hasStatusCode is false.
/// @throw TransactionReceiptVersionError when the receipt has a state root instead of a status code.
uint8_t statusCode() const;
u256 const& gasUsed() const { return m_gasUsed; }
LogBloom const& bloom() const { return m_bloom; }
Expand Down
10 changes: 3 additions & 7 deletions libweb3jsonrpc/JsonHelper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -185,14 +185,10 @@ Json::Value toJson(dev::eth::TransactionSkeleton const& _t)
Json::Value toJson(dev::eth::TransactionReceipt const& _t)
{
Json::Value res;
try
{
res["stateRoot"] = toJS(_t.stateRoot());
}
catch (TransactionReceiptVersionError const&)
{
if (_t.hasStatusCode())
res["statusCode"] = toJS(_t.statusCode());
}
else
res["stateRoot"] = toJS(_t.stateRoot());
res["gasUsed"] = toJS(_t.gasUsed());
res["bloom"] = toJS(_t.bloom());
res["log"] = dev::toJson(_t.log());
Expand Down

0 comments on commit eca4fbf

Please sign in to comment.