subtly
10 years ago
93 changed files with 3179 additions and 2580 deletions
@ -0,0 +1,812 @@ |
|||
/*
|
|||
This file is part of cpp-ethereum. |
|||
|
|||
cpp-ethereum is free software: you can redistribute it and/or modify |
|||
it under the terms of the GNU General Public License as published by |
|||
the Free Software Foundation, either version 3 of the License, or |
|||
(at your option) any later version. |
|||
|
|||
cpp-ethereum is distributed in the hope that it will be useful, |
|||
but WITHOUT ANY WARRANTY; without even the implied warranty of |
|||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|||
GNU General Public License for more details. |
|||
|
|||
You should have received a copy of the GNU General Public License |
|||
along with cpp-ethereum. If not, see <http://www.gnu.org/licenses/>.
|
|||
*/ |
|||
/** @file Block.cpp
|
|||
* @author Gav Wood <i@gavwood.com> |
|||
* @date 2014 |
|||
*/ |
|||
|
|||
#include "Block.h" |
|||
|
|||
#include <ctime> |
|||
#include <boost/filesystem.hpp> |
|||
#include <boost/timer.hpp> |
|||
#include <libdevcore/CommonIO.h> |
|||
#include <libdevcore/Assertions.h> |
|||
#include <libdevcore/StructuredLogger.h> |
|||
#include <libdevcore/TrieHash.h> |
|||
#include <libevmcore/Instruction.h> |
|||
#include <libethcore/Exceptions.h> |
|||
#include <libethcore/Params.h> |
|||
#include <libevm/VMFactory.h> |
|||
#include "BlockChain.h" |
|||
#include "Defaults.h" |
|||
#include "ExtVM.h" |
|||
#include "Executive.h" |
|||
#include "CachedAddressState.h" |
|||
#include "CanonBlockChain.h" |
|||
#include "TransactionQueue.h" |
|||
using namespace std; |
|||
using namespace dev; |
|||
using namespace dev::eth; |
|||
namespace fs = boost::filesystem; |
|||
|
|||
#define ctrace clog(BlockTrace) |
|||
#define ETH_TIMED_ENACTMENTS 0 |
|||
|
|||
static const unsigned c_maxSyncTransactions = 256; |
|||
|
|||
const char* BlockSafeExceptions::name() { return EthViolet "⚙" EthBlue " ℹ"; } |
|||
const char* BlockDetail::name() { return EthViolet "⚙" EthWhite " ◌"; } |
|||
const char* BlockTrace::name() { return EthViolet "⚙" EthGray " ◎"; } |
|||
const char* BlockChat::name() { return EthViolet "⚙" EthWhite " ◌"; } |
|||
|
|||
Block::Block(OverlayDB const& _db, BaseState _bs, Address _coinbaseAddress): |
|||
m_state(_db, _bs), |
|||
m_beneficiary(_coinbaseAddress), |
|||
m_blockReward(c_blockReward) |
|||
{ |
|||
m_previousBlock.clear(); |
|||
m_currentBlock.clear(); |
|||
// assert(m_state.root() == m_previousBlock.stateRoot());
|
|||
} |
|||
|
|||
Block::Block(Block const& _s): |
|||
m_state(_s.m_state), |
|||
m_transactions(_s.m_transactions), |
|||
m_receipts(_s.m_receipts), |
|||
m_transactionSet(_s.m_transactionSet), |
|||
m_previousBlock(_s.m_previousBlock), |
|||
m_currentBlock(_s.m_currentBlock), |
|||
m_beneficiary(_s.m_beneficiary), |
|||
m_blockReward(_s.m_blockReward) |
|||
{ |
|||
m_precommit = m_state; |
|||
m_committedToMine = false; |
|||
} |
|||
|
|||
Block& Block::operator=(Block const& _s) |
|||
{ |
|||
m_state = _s.m_state; |
|||
m_transactions = _s.m_transactions; |
|||
m_receipts = _s.m_receipts; |
|||
m_transactionSet = _s.m_transactionSet; |
|||
m_previousBlock = _s.m_previousBlock; |
|||
m_currentBlock = _s.m_currentBlock; |
|||
m_beneficiary = _s.m_beneficiary; |
|||
m_blockReward = _s.m_blockReward; |
|||
|
|||
m_precommit = m_state; |
|||
m_committedToMine = false; |
|||
return *this; |
|||
} |
|||
|
|||
void Block::resetCurrent() |
|||
{ |
|||
m_transactions.clear(); |
|||
m_receipts.clear(); |
|||
m_transactionSet.clear(); |
|||
m_currentBlock = BlockInfo(); |
|||
m_currentBlock.setCoinbaseAddress(m_beneficiary); |
|||
m_currentBlock.setTimestamp(max(m_previousBlock.timestamp() + 1, (u256)time(0))); |
|||
m_currentBlock.populateFromParent(m_previousBlock); |
|||
|
|||
// TODO: check.
|
|||
|
|||
m_state.setRoot(m_previousBlock.stateRoot()); |
|||
m_precommit = m_state; |
|||
m_committedToMine = false; |
|||
} |
|||
|
|||
PopulationStatistics Block::populateFromChain(BlockChain const& _bc, h256 const& _h, ImportRequirements::value _ir) |
|||
{ |
|||
PopulationStatistics ret { 0.0, 0.0 }; |
|||
|
|||
if (!_bc.isKnown(_h)) |
|||
{ |
|||
// Might be worth throwing here.
|
|||
cwarn << "Invalid block given for state population: " << _h; |
|||
BOOST_THROW_EXCEPTION(BlockNotFound() << errinfo_target(_h)); |
|||
} |
|||
|
|||
auto b = _bc.block(_h); |
|||
BlockInfo bi(b); |
|||
if (bi.number()) |
|||
{ |
|||
// Non-genesis:
|
|||
|
|||
// 1. Start at parent's end state (state root).
|
|||
BlockInfo bip(_bc.block(bi.parentHash())); |
|||
sync(_bc, bi.parentHash(), bip); |
|||
|
|||
// 2. Enact the block's transactions onto this state.
|
|||
m_beneficiary = bi.beneficiary(); |
|||
Timer t; |
|||
auto vb = _bc.verifyBlock(&b, function<void(Exception&)>(), _ir | ImportRequirements::TransactionBasic); |
|||
ret.verify = t.elapsed(); |
|||
t.restart(); |
|||
enact(vb, _bc); |
|||
ret.enact = t.elapsed(); |
|||
} |
|||
else |
|||
{ |
|||
// Genesis required:
|
|||
// We know there are no transactions, so just populate directly.
|
|||
m_state = State(m_state.db(), BaseState::Empty); // TODO: try with PreExisting.
|
|||
sync(_bc, _h, bi); |
|||
} |
|||
|
|||
return ret; |
|||
} |
|||
|
|||
bool Block::sync(BlockChain const& _bc) |
|||
{ |
|||
return sync(_bc, _bc.currentHash()); |
|||
} |
|||
|
|||
bool Block::sync(BlockChain const& _bc, h256 const& _block, BlockInfo const& _bi) |
|||
{ |
|||
bool ret = false; |
|||
// BLOCK
|
|||
BlockInfo bi = _bi ? _bi : _bc.info(_block); |
|||
#if ETH_PARANOIA |
|||
if (!bi) |
|||
while (1) |
|||
{ |
|||
try |
|||
{ |
|||
auto b = _bc.block(_block); |
|||
bi.populate(b); |
|||
break; |
|||
} |
|||
catch (Exception const& _e) |
|||
{ |
|||
// TODO: Slightly nicer handling? :-)
|
|||
cerr << "ERROR: Corrupt block-chain! Delete your block-chain DB and restart." << endl; |
|||
cerr << diagnostic_information(_e) << endl; |
|||
} |
|||
catch (std::exception const& _e) |
|||
{ |
|||
// TODO: Slightly nicer handling? :-)
|
|||
cerr << "ERROR: Corrupt block-chain! Delete your block-chain DB and restart." << endl; |
|||
cerr << _e.what() << endl; |
|||
} |
|||
} |
|||
#endif |
|||
if (bi == m_currentBlock) |
|||
{ |
|||
// We mined the last block.
|
|||
// Our state is good - we just need to move on to next.
|
|||
m_previousBlock = m_currentBlock; |
|||
resetCurrent(); |
|||
ret = true; |
|||
} |
|||
else if (bi == m_previousBlock) |
|||
{ |
|||
// No change since last sync.
|
|||
// Carry on as we were.
|
|||
} |
|||
else |
|||
{ |
|||
// New blocks available, or we've switched to a different branch. All change.
|
|||
// Find most recent state dump and replay what's left.
|
|||
// (Most recent state dump might end up being genesis.)
|
|||
|
|||
if (m_state.db().lookup(bi.stateRoot()).empty()) // TODO: API in State for this?
|
|||
{ |
|||
cwarn << "Unable to sync to" << bi.hash() << "; state root" << bi.stateRoot() << "not found in database."; |
|||
cwarn << "Database corrupt: contains block without stateRoot:" << bi; |
|||
cwarn << "Try rescuing the database by running: eth --rescue"; |
|||
BOOST_THROW_EXCEPTION(InvalidStateRoot() << errinfo_target(bi.stateRoot())); |
|||
} |
|||
m_previousBlock = bi; |
|||
resetCurrent(); |
|||
ret = true; |
|||
} |
|||
#if ALLOW_REBUILD |
|||
else |
|||
{ |
|||
// New blocks available, or we've switched to a different branch. All change.
|
|||
// Find most recent state dump and replay what's left.
|
|||
// (Most recent state dump might end up being genesis.)
|
|||
|
|||
std::vector<h256> chain; |
|||
while (bi.number() != 0 && m_db.lookup(bi.stateRoot()).empty()) // while we don't have the state root of the latest block...
|
|||
{ |
|||
chain.push_back(bi.hash()); // push back for later replay.
|
|||
bi.populate(_bc.block(bi.parentHash())); // move to parent.
|
|||
} |
|||
|
|||
m_previousBlock = bi; |
|||
resetCurrent(); |
|||
|
|||
// Iterate through in reverse, playing back each of the blocks.
|
|||
try |
|||
{ |
|||
for (auto it = chain.rbegin(); it != chain.rend(); ++it) |
|||
{ |
|||
auto b = _bc.block(*it); |
|||
enact(&b, _bc, _ir); |
|||
cleanup(true); |
|||
} |
|||
} |
|||
catch (...) |
|||
{ |
|||
// TODO: Slightly nicer handling? :-)
|
|||
cerr << "ERROR: Corrupt block-chain! Delete your block-chain DB and restart." << endl; |
|||
cerr << boost::current_exception_diagnostic_information() << endl; |
|||
exit(1); |
|||
} |
|||
|
|||
resetCurrent(); |
|||
ret = true; |
|||
} |
|||
#endif |
|||
return ret; |
|||
} |
|||
|
|||
pair<TransactionReceipts, bool> Block::sync(BlockChain const& _bc, TransactionQueue& _tq, GasPricer const& _gp, unsigned msTimeout) |
|||
{ |
|||
// TRANSACTIONS
|
|||
pair<TransactionReceipts, bool> ret; |
|||
ret.second = false; |
|||
|
|||
auto ts = _tq.topTransactions(c_maxSyncTransactions); |
|||
|
|||
LastHashes lh; |
|||
|
|||
auto deadline = chrono::steady_clock::now() + chrono::milliseconds(msTimeout); |
|||
|
|||
for (int goodTxs = 1; goodTxs; ) |
|||
{ |
|||
goodTxs = 0; |
|||
for (auto const& t: ts) |
|||
if (!m_transactionSet.count(t.sha3())) |
|||
{ |
|||
try |
|||
{ |
|||
if (t.gasPrice() >= _gp.ask(*this)) |
|||
{ |
|||
// Timer t;
|
|||
if (lh.empty()) |
|||
lh = _bc.lastHashes(); |
|||
execute(lh, t); |
|||
ret.first.push_back(m_receipts.back()); |
|||
++goodTxs; |
|||
// cnote << "TX took:" << t.elapsed() * 1000;
|
|||
} |
|||
else if (t.gasPrice() < _gp.ask(*this) * 9 / 10) |
|||
{ |
|||
clog(StateTrace) << t.sha3() << "Dropping El Cheapo transaction (<90% of ask price)"; |
|||
_tq.drop(t.sha3()); |
|||
} |
|||
} |
|||
catch (InvalidNonce const& in) |
|||
{ |
|||
bigint const& req = *boost::get_error_info<errinfo_required>(in); |
|||
bigint const& got = *boost::get_error_info<errinfo_got>(in); |
|||
|
|||
if (req > got) |
|||
{ |
|||
// too old
|
|||
clog(StateTrace) << t.sha3() << "Dropping old transaction (nonce too low)"; |
|||
_tq.drop(t.sha3()); |
|||
} |
|||
else if (got > req + _tq.waiting(t.sender())) |
|||
{ |
|||
// too new
|
|||
clog(StateTrace) << t.sha3() << "Dropping new transaction (too many nonces ahead)"; |
|||
_tq.drop(t.sha3()); |
|||
} |
|||
else |
|||
_tq.setFuture(t.sha3()); |
|||
} |
|||
catch (BlockGasLimitReached const& e) |
|||
{ |
|||
bigint const& got = *boost::get_error_info<errinfo_got>(e); |
|||
if (got > m_currentBlock.gasLimit()) |
|||
{ |
|||
clog(StateTrace) << t.sha3() << "Dropping over-gassy transaction (gas > block's gas limit)"; |
|||
_tq.drop(t.sha3()); |
|||
} |
|||
else |
|||
{ |
|||
// Temporarily no gas left in current block.
|
|||
// OPTIMISE: could note this and then we don't evaluate until a block that does have the gas left.
|
|||
// for now, just leave alone.
|
|||
} |
|||
} |
|||
catch (Exception const& _e) |
|||
{ |
|||
// Something else went wrong - drop it.
|
|||
clog(StateTrace) << t.sha3() << "Dropping invalid transaction:" << diagnostic_information(_e); |
|||
_tq.drop(t.sha3()); |
|||
} |
|||
catch (std::exception const&) |
|||
{ |
|||
// Something else went wrong - drop it.
|
|||
_tq.drop(t.sha3()); |
|||
cwarn << t.sha3() << "Transaction caused low-level exception :("; |
|||
} |
|||
} |
|||
if (chrono::steady_clock::now() > deadline) |
|||
{ |
|||
ret.second = true; |
|||
break; |
|||
} |
|||
} |
|||
return ret; |
|||
} |
|||
|
|||
u256 Block::enactOn(VerifiedBlockRef const& _block, BlockChain const& _bc) |
|||
{ |
|||
#if ETH_TIMED_ENACTMENTS |
|||
Timer t; |
|||
double populateVerify; |
|||
double populateGrand; |
|||
double syncReset; |
|||
double enactment; |
|||
#endif |
|||
|
|||
// Check family:
|
|||
BlockInfo biParent = _bc.info(_block.info.parentHash()); |
|||
_block.info.verifyParent(biParent); |
|||
|
|||
#if ETH_TIMED_ENACTMENTS |
|||
populateVerify = t.elapsed(); |
|||
t.restart(); |
|||
#endif |
|||
|
|||
BlockInfo biGrandParent; |
|||
if (biParent.number()) |
|||
biGrandParent = _bc.info(biParent.parentHash()); |
|||
|
|||
#if ETH_TIMED_ENACTMENTS |
|||
populateGrand = t.elapsed(); |
|||
t.restart(); |
|||
#endif |
|||
|
|||
sync(_bc, _block.info.parentHash(), BlockInfo()); |
|||
resetCurrent(); |
|||
|
|||
#if ETH_TIMED_ENACTMENTS |
|||
syncReset = t.elapsed(); |
|||
t.restart(); |
|||
#endif |
|||
|
|||
m_previousBlock = biParent; |
|||
auto ret = enact(_block, _bc); |
|||
|
|||
#if ETH_TIMED_ENACTMENTS |
|||
enactment = t.elapsed(); |
|||
if (populateVerify + populateGrand + syncReset + enactment > 0.5) |
|||
clog(StateChat) << "popVer/popGrand/syncReset/enactment = " << populateVerify << "/" << populateGrand << "/" << syncReset << "/" << enactment; |
|||
#endif |
|||
return ret; |
|||
} |
|||
|
|||
u256 Block::enact(VerifiedBlockRef const& _block, BlockChain const& _bc) |
|||
{ |
|||
DEV_TIMED_FUNCTION_ABOVE(500); |
|||
|
|||
// m_currentBlock is assumed to be prepopulated and reset.
|
|||
#if !ETH_RELEASE |
|||
assert(m_previousBlock.hash() == _block.info.parentHash()); |
|||
assert(m_currentBlock.parentHash() == _block.info.parentHash()); |
|||
assert(rootHash() == m_previousBlock.stateRoot()); |
|||
#endif |
|||
|
|||
if (m_currentBlock.parentHash() != m_previousBlock.hash()) |
|||
// Internal client error.
|
|||
BOOST_THROW_EXCEPTION(InvalidParentHash()); |
|||
|
|||
// Populate m_currentBlock with the correct values.
|
|||
m_currentBlock.noteDirty(); |
|||
m_currentBlock = _block.info; |
|||
|
|||
// cnote << "playback begins:" << m_state.root();
|
|||
// cnote << m_state;
|
|||
|
|||
LastHashes lh; |
|||
DEV_TIMED_ABOVE("lastHashes", 500) |
|||
lh = _bc.lastHashes((unsigned)m_previousBlock.number()); |
|||
|
|||
RLP rlp(_block.block); |
|||
|
|||
vector<bytes> receipts; |
|||
|
|||
// All ok with the block generally. Play back the transactions now...
|
|||
unsigned i = 0; |
|||
DEV_TIMED_ABOVE("txExec", 500) |
|||
for (auto const& tr: _block.transactions) |
|||
{ |
|||
try |
|||
{ |
|||
LogOverride<ExecutiveWarnChannel> o(false); |
|||
execute(lh, tr); |
|||
} |
|||
catch (Exception& ex) |
|||
{ |
|||
ex << errinfo_transactionIndex(i); |
|||
throw; |
|||
} |
|||
|
|||
RLPStream receiptRLP; |
|||
m_receipts.back().streamRLP(receiptRLP); |
|||
receipts.push_back(receiptRLP.out()); |
|||
++i; |
|||
} |
|||
|
|||
h256 receiptsRoot; |
|||
DEV_TIMED_ABOVE(".receiptsRoot()", 500) |
|||
receiptsRoot = orderedTrieRoot(receipts); |
|||
|
|||
if (receiptsRoot != m_currentBlock.receiptsRoot()) |
|||
{ |
|||
InvalidReceiptsStateRoot ex; |
|||
ex << Hash256RequirementError(receiptsRoot, m_currentBlock.receiptsRoot()); |
|||
ex << errinfo_receipts(receipts); |
|||
// ex << errinfo_vmtrace(vmTrace(_block.block, _bc, ImportRequirements::None));
|
|||
BOOST_THROW_EXCEPTION(ex); |
|||
} |
|||
|
|||
if (m_currentBlock.logBloom() != logBloom()) |
|||
{ |
|||
InvalidLogBloom ex; |
|||
ex << LogBloomRequirementError(logBloom(), m_currentBlock.logBloom()); |
|||
ex << errinfo_receipts(receipts); |
|||
BOOST_THROW_EXCEPTION(ex); |
|||
} |
|||
|
|||
// Initialise total difficulty calculation.
|
|||
u256 tdIncrease = m_currentBlock.difficulty(); |
|||
|
|||
// Check uncles & apply their rewards to state.
|
|||
if (rlp[2].itemCount() > 2) |
|||
{ |
|||
TooManyUncles ex; |
|||
ex << errinfo_max(2); |
|||
ex << errinfo_got(rlp[2].itemCount()); |
|||
BOOST_THROW_EXCEPTION(ex); |
|||
} |
|||
|
|||
vector<BlockInfo> rewarded; |
|||
h256Hash excluded; |
|||
DEV_TIMED_ABOVE("allKin", 500) |
|||
excluded = _bc.allKinFrom(m_currentBlock.parentHash(), 6); |
|||
excluded.insert(m_currentBlock.hash()); |
|||
|
|||
unsigned ii = 0; |
|||
DEV_TIMED_ABOVE("uncleCheck", 500) |
|||
for (auto const& i: rlp[2]) |
|||
{ |
|||
try |
|||
{ |
|||
auto h = sha3(i.data()); |
|||
if (excluded.count(h)) |
|||
{ |
|||
UncleInChain ex; |
|||
ex << errinfo_comment("Uncle in block already mentioned"); |
|||
ex << errinfo_unclesExcluded(excluded); |
|||
ex << errinfo_hash256(sha3(i.data())); |
|||
BOOST_THROW_EXCEPTION(ex); |
|||
} |
|||
excluded.insert(h); |
|||
|
|||
// IgnoreSeal since it's a VerifiedBlock.
|
|||
BlockInfo uncle(i.data(), IgnoreSeal, h, HeaderData); |
|||
|
|||
BlockInfo uncleParent; |
|||
if (!_bc.isKnown(uncle.parentHash())) |
|||
BOOST_THROW_EXCEPTION(UnknownParent()); |
|||
uncleParent = BlockInfo(_bc.block(uncle.parentHash())); |
|||
|
|||
if ((bigint)uncleParent.number() < (bigint)m_currentBlock.number() - 7) |
|||
{ |
|||
UncleTooOld ex; |
|||
ex << errinfo_uncleNumber(uncle.number()); |
|||
ex << errinfo_currentNumber(m_currentBlock.number()); |
|||
BOOST_THROW_EXCEPTION(ex); |
|||
} |
|||
else if (uncle.number() == m_currentBlock.number()) |
|||
{ |
|||
UncleIsBrother ex; |
|||
ex << errinfo_uncleNumber(uncle.number()); |
|||
ex << errinfo_currentNumber(m_currentBlock.number()); |
|||
BOOST_THROW_EXCEPTION(ex); |
|||
} |
|||
uncle.verifyParent(uncleParent); |
|||
|
|||
rewarded.push_back(uncle); |
|||
++ii; |
|||
} |
|||
catch (Exception& ex) |
|||
{ |
|||
ex << errinfo_uncleIndex(ii); |
|||
throw; |
|||
} |
|||
} |
|||
|
|||
DEV_TIMED_ABOVE("applyRewards", 500) |
|||
applyRewards(rewarded); |
|||
|
|||
// Commit all cached state changes to the state trie.
|
|||
DEV_TIMED_ABOVE("commit", 500) |
|||
m_state.commit(); |
|||
|
|||
// Hash the state trie and check against the state_root hash in m_currentBlock.
|
|||
if (m_currentBlock.stateRoot() != m_previousBlock.stateRoot() && m_currentBlock.stateRoot() != rootHash()) |
|||
{ |
|||
auto r = rootHash(); |
|||
m_state.db().rollback(); // TODO: API in State for this?
|
|||
BOOST_THROW_EXCEPTION(InvalidStateRoot() << Hash256RequirementError(r, m_currentBlock.stateRoot())); |
|||
} |
|||
|
|||
if (m_currentBlock.gasUsed() != gasUsed()) |
|||
{ |
|||
// Rollback the trie.
|
|||
m_state.db().rollback(); // TODO: API in State for this?
|
|||
BOOST_THROW_EXCEPTION(InvalidGasUsed() << RequirementError(bigint(gasUsed()), bigint(m_currentBlock.gasUsed()))); |
|||
} |
|||
|
|||
return tdIncrease; |
|||
} |
|||
|
|||
ExecutionResult Block::execute(LastHashes const& _lh, Transaction const& _t, Permanence _p, OnOpFunc const& _onOp) |
|||
{ |
|||
// Uncommitting is a non-trivial operation - only do it once we've verified as much of the
|
|||
// transaction as possible.
|
|||
uncommitToMine(); |
|||
|
|||
std::pair<ExecutionResult, TransactionReceipt> resultReceipt = m_state.execute(EnvInfo(info(), _lh, gasUsed()), _t, _p, _onOp); |
|||
|
|||
if (_p == Permanence::Committed) |
|||
{ |
|||
// Add to the user-originated transactions that we've executed.
|
|||
m_transactions.push_back(_t); |
|||
m_receipts.push_back(resultReceipt.second); |
|||
m_transactionSet.insert(_t.sha3()); |
|||
} |
|||
|
|||
return resultReceipt.first; |
|||
} |
|||
|
|||
void Block::applyRewards(vector<BlockInfo> const& _uncleBlockHeaders) |
|||
{ |
|||
u256 r = m_blockReward; |
|||
for (auto const& i: _uncleBlockHeaders) |
|||
{ |
|||
m_state.addBalance(i.beneficiary(), m_blockReward * (8 + i.number() - m_currentBlock.number()) / 8); |
|||
r += m_blockReward / 32; |
|||
} |
|||
m_state.addBalance(m_currentBlock.beneficiary(), r); |
|||
} |
|||
|
|||
void Block::commitToSeal(BlockChain const& _bc, bytes const& _extraData) |
|||
{ |
|||
if (m_committedToMine) |
|||
uncommitToMine(); |
|||
else |
|||
m_precommit = m_state; |
|||
|
|||
vector<BlockInfo> uncleBlockHeaders; |
|||
|
|||
RLPStream unclesData; |
|||
unsigned unclesCount = 0; |
|||
if (m_previousBlock.number() != 0) |
|||
{ |
|||
// Find great-uncles (or second-cousins or whatever they are) - children of great-grandparents, great-great-grandparents... that were not already uncles in previous generations.
|
|||
clog(StateDetail) << "Checking " << m_previousBlock.hash() << ", parent=" << m_previousBlock.parentHash(); |
|||
h256Hash excluded = _bc.allKinFrom(m_currentBlock.parentHash(), 6); |
|||
auto p = m_previousBlock.parentHash(); |
|||
for (unsigned gen = 0; gen < 6 && p != _bc.genesisHash() && unclesCount < 2; ++gen, p = _bc.details(p).parent) |
|||
{ |
|||
auto us = _bc.details(p).children; |
|||
assert(us.size() >= 1); // must be at least 1 child of our grandparent - it's our own parent!
|
|||
for (auto const& u: us) |
|||
if (!excluded.count(u)) // ignore any uncles/mainline blocks that we know about.
|
|||
{ |
|||
uncleBlockHeaders.push_back(_bc.info(u)); |
|||
unclesData.appendRaw(_bc.headerData(u)); |
|||
++unclesCount; |
|||
if (unclesCount == 2) |
|||
break; |
|||
} |
|||
} |
|||
} |
|||
|
|||
BytesMap transactionsMap; |
|||
BytesMap receiptsMap; |
|||
|
|||
RLPStream txs; |
|||
txs.appendList(m_transactions.size()); |
|||
|
|||
for (unsigned i = 0; i < m_transactions.size(); ++i) |
|||
{ |
|||
RLPStream k; |
|||
k << i; |
|||
|
|||
RLPStream receiptrlp; |
|||
m_receipts[i].streamRLP(receiptrlp); |
|||
receiptsMap.insert(std::make_pair(k.out(), receiptrlp.out())); |
|||
|
|||
RLPStream txrlp; |
|||
m_transactions[i].streamRLP(txrlp); |
|||
transactionsMap.insert(std::make_pair(k.out(), txrlp.out())); |
|||
|
|||
txs.appendRaw(txrlp.out()); |
|||
} |
|||
|
|||
txs.swapOut(m_currentTxs); |
|||
|
|||
RLPStream(unclesCount).appendRaw(unclesData.out(), unclesCount).swapOut(m_currentUncles); |
|||
|
|||
// Apply rewards last of all.
|
|||
applyRewards(uncleBlockHeaders); |
|||
|
|||
// Commit any and all changes to the trie that are in the cache, then update the state root accordingly.
|
|||
m_state.commit(); |
|||
|
|||
clog(StateDetail) << "Post-reward stateRoot:" << m_state.rootHash(); |
|||
clog(StateDetail) << m_state; |
|||
clog(StateDetail) << *this; |
|||
|
|||
m_currentBlock.setLogBloom(logBloom()); |
|||
m_currentBlock.setGasUsed(gasUsed()); |
|||
m_currentBlock.setRoots(hash256(transactionsMap), hash256(receiptsMap), sha3(m_currentUncles), m_state.rootHash()); |
|||
|
|||
m_currentBlock.setParentHash(m_previousBlock.hash()); |
|||
m_currentBlock.setExtraData(_extraData); |
|||
if (m_currentBlock.extraData().size() > 32) |
|||
{ |
|||
auto ed = m_currentBlock.extraData(); |
|||
ed.resize(32); |
|||
m_currentBlock.setExtraData(ed); |
|||
} |
|||
|
|||
m_committedToMine = true; |
|||
} |
|||
|
|||
void Block::uncommitToMine() |
|||
{ |
|||
if (m_committedToMine) |
|||
{ |
|||
m_state = m_precommit; |
|||
m_committedToMine = false; |
|||
} |
|||
} |
|||
|
|||
bool Block::sealBlock(bytesConstRef _header) |
|||
{ |
|||
if (!m_committedToMine) |
|||
return false; |
|||
|
|||
if (BlockInfo(_header, CheckNothing, h256{}, HeaderData).hashWithout() != m_currentBlock.hashWithout()) |
|||
return false; |
|||
|
|||
clog(StateDetail) << "Sealing block!"; |
|||
|
|||
// Compile block:
|
|||
RLPStream ret; |
|||
ret.appendList(3); |
|||
ret.appendRaw(_header); |
|||
ret.appendRaw(m_currentTxs); |
|||
ret.appendRaw(m_currentUncles); |
|||
ret.swapOut(m_currentBytes); |
|||
m_currentBlock = BlockInfo(_header, CheckNothing, h256(), HeaderData); |
|||
cnote << "Mined " << m_currentBlock.hash() << "(parent: " << m_currentBlock.parentHash() << ")"; |
|||
// TODO: move into Sealer
|
|||
StructuredLogger::minedNewBlock( |
|||
m_currentBlock.hash().abridged(), |
|||
"", // Can't give the nonce here.
|
|||
"", //TODO: chain head hash here ??
|
|||
m_currentBlock.parentHash().abridged() |
|||
); |
|||
|
|||
// Quickly reset the transactions.
|
|||
// TODO: Leave this in a better state than this limbo, or at least record that it's in limbo.
|
|||
m_transactions.clear(); |
|||
m_receipts.clear(); |
|||
m_transactionSet.clear(); |
|||
m_precommit = m_state; |
|||
|
|||
return true; |
|||
} |
|||
|
|||
State Block::fromPending(unsigned _i) const |
|||
{ |
|||
State ret = m_state; |
|||
_i = min<unsigned>(_i, m_transactions.size()); |
|||
if (!_i) |
|||
ret.setRoot(m_previousBlock.stateRoot()); |
|||
else |
|||
ret.setRoot(m_receipts[_i - 1].stateRoot()); |
|||
return ret; |
|||
} |
|||
|
|||
LogBloom Block::logBloom() const |
|||
{ |
|||
LogBloom ret; |
|||
for (TransactionReceipt const& i: m_receipts) |
|||
ret |= i.bloom(); |
|||
return ret; |
|||
} |
|||
|
|||
void Block::cleanup(bool _fullCommit) |
|||
{ |
|||
if (_fullCommit) |
|||
{ |
|||
// Commit the new trie to disk.
|
|||
if (isChannelVisible<StateTrace>()) // Avoid calling toHex if not needed
|
|||
clog(StateTrace) << "Committing to disk: stateRoot" << m_currentBlock.stateRoot() << "=" << rootHash() << "=" << toHex(asBytes(db().lookup(rootHash()))); |
|||
|
|||
try |
|||
{ |
|||
EnforceRefs er(db(), true); |
|||
rootHash(); |
|||
} |
|||
catch (BadRoot const&) |
|||
{ |
|||
clog(StateChat) << "Trie corrupt! :-("; |
|||
throw; |
|||
} |
|||
|
|||
m_state.db().commit(); // TODO: State API for this?
|
|||
|
|||
if (isChannelVisible<StateTrace>()) // Avoid calling toHex if not needed
|
|||
clog(StateTrace) << "Committed: stateRoot" << m_currentBlock.stateRoot() << "=" << rootHash() << "=" << toHex(asBytes(db().lookup(rootHash()))); |
|||
|
|||
m_previousBlock = m_currentBlock; |
|||
m_currentBlock.populateFromParent(m_previousBlock); |
|||
|
|||
clog(StateTrace) << "finalising enactment. current -> previous, hash is" << m_previousBlock.hash(); |
|||
} |
|||
else |
|||
m_state.db().rollback(); // TODO: State API for this?
|
|||
|
|||
resetCurrent(); |
|||
} |
|||
|
|||
string Block::vmTrace(bytesConstRef _block, BlockChain const& _bc, ImportRequirements::value _ir) |
|||
{ |
|||
RLP rlp(_block); |
|||
|
|||
cleanup(false); |
|||
BlockInfo bi(_block, (_ir & ImportRequirements::ValidSeal) ? CheckEverything : IgnoreSeal); |
|||
m_currentBlock = bi; |
|||
m_currentBlock.verifyInternals(_block); |
|||
m_currentBlock.noteDirty(); |
|||
|
|||
LastHashes lh = _bc.lastHashes((unsigned)m_previousBlock.number()); |
|||
|
|||
string ret; |
|||
unsigned i = 0; |
|||
for (auto const& tr: rlp[1]) |
|||
{ |
|||
StandardTrace st; |
|||
st.setShowMnemonics(); |
|||
execute(lh, Transaction(tr.data(), CheckTransaction::Everything), Permanence::Committed, st.onOp()); |
|||
ret += (ret.empty() ? "[" : ",") + st.json(); |
|||
++i; |
|||
} |
|||
return ret.empty() ? "[]" : (ret + "]"); |
|||
} |
|||
|
|||
std::ostream& dev::eth::operator<<(std::ostream& _out, Block const& _s) |
|||
{ |
|||
(void)_s; |
|||
return _out; |
|||
} |
@ -0,0 +1,310 @@ |
|||
/*
|
|||
This file is part of cpp-ethereum. |
|||
|
|||
cpp-ethereum is free software: you can redistribute it and/or modify |
|||
it under the terms of the GNU General Public License as published by |
|||
the Free Software Foundation, either version 3 of the License, or |
|||
(at your option) any later version. |
|||
|
|||
cpp-ethereum is distributed in the hope that it will be useful, |
|||
but WITHOUT ANY WARRANTY; without even the implied warranty of |
|||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|||
GNU General Public License for more details. |
|||
|
|||
You should have received a copy of the GNU General Public License |
|||
along with cpp-ethereum. If not, see <http://www.gnu.org/licenses/>.
|
|||
*/ |
|||
/** @file Block.h
|
|||
* @author Gav Wood <i@gavwood.com> |
|||
* @date 2014 |
|||
*/ |
|||
|
|||
#pragma once |
|||
|
|||
#include <array> |
|||
#include <unordered_map> |
|||
#include <libdevcore/Common.h> |
|||
#include <libdevcore/RLP.h> |
|||
#include <libdevcore/TrieDB.h> |
|||
#include <libdevcrypto/OverlayDB.h> |
|||
#include <libethcore/Exceptions.h> |
|||
#include <libethcore/BlockInfo.h> |
|||
#include <libethcore/Miner.h> |
|||
#include <libevm/ExtVMFace.h> |
|||
#include "Account.h" |
|||
#include "Transaction.h" |
|||
#include "TransactionReceipt.h" |
|||
#include "AccountDiff.h" |
|||
#include "GasPricer.h" |
|||
#include "State.h" |
|||
|
|||
namespace dev |
|||
{ |
|||
|
|||
namespace test { class ImportTest; class StateLoader; } |
|||
|
|||
namespace eth |
|||
{ |
|||
|
|||
class BlockChain; |
|||
class State; |
|||
class TransactionQueue; |
|||
struct VerifiedBlockRef; |
|||
|
|||
struct BlockChat: public LogChannel { static const char* name(); static const int verbosity = 4; }; |
|||
struct BlockTrace: public LogChannel { static const char* name(); static const int verbosity = 5; }; |
|||
struct BlockDetail: public LogChannel { static const char* name(); static const int verbosity = 14; }; |
|||
struct BlockSafeExceptions: public LogChannel { static const char* name(); static const int verbosity = 21; }; |
|||
|
|||
struct PopulationStatistics |
|||
{ |
|||
double verify; |
|||
double enact; |
|||
}; |
|||
|
|||
/**
|
|||
* @brief Active model of a block within the block chain. |
|||
* Keeps track of all transactions, receipts and state for a particular block. Can apply all |
|||
* needed transforms of the state for rewards and contains logic for sealing the block. |
|||
*/ |
|||
class Block |
|||
{ |
|||
friend class ExtVM; |
|||
friend class dev::test::ImportTest; |
|||
friend class dev::test::StateLoader; |
|||
friend class Executive; |
|||
friend class BlockChain; |
|||
|
|||
public: |
|||
/// Default constructor; creates with a blank database prepopulated with the genesis block.
|
|||
Block(): m_state(OverlayDB(), BaseState::Empty) {} |
|||
|
|||
/// Basic state object from database.
|
|||
/// Use the default when you already have a database and you just want to make a Block object
|
|||
/// which uses it. If you have no preexisting database then set BaseState to something other
|
|||
/// than BaseState::PreExisting in order to prepopulate the Trie.
|
|||
/// You can also set the beneficiary address.
|
|||
explicit Block(OverlayDB const& _db, BaseState _bs = BaseState::PreExisting, Address _coinbaseAddress = Address()); |
|||
|
|||
/// Copy state object.
|
|||
Block(Block const& _s); |
|||
|
|||
/// Copy state object.
|
|||
Block& operator=(Block const& _s); |
|||
|
|||
/// Get the beneficiary address for any transactions we do and rewards we get.
|
|||
Address beneficiary() const { return m_beneficiary; } |
|||
|
|||
/// Set the beneficiary address for any transactions we do and rewards we get.
|
|||
/// This causes a complete reset of current block.
|
|||
void setBeneficiary(Address const& _id) { m_beneficiary = _id; resetCurrent(); } |
|||
|
|||
// Account-getters. All operate on the final state.
|
|||
|
|||
/// Get an account's balance.
|
|||
/// @returns 0 if the address has never been used.
|
|||
u256 balance(Address const& _address) const { return m_state.balance(_address); } |
|||
|
|||
/// Get the number of transactions a particular address has sent (used for the transaction nonce).
|
|||
/// @returns 0 if the address has never been used.
|
|||
u256 transactionsFrom(Address const& _address) const { return m_state.transactionsFrom(_address); } |
|||
|
|||
/// Check if the address is in use.
|
|||
bool addressInUse(Address const& _address) const { return m_state.addressInUse(_address); } |
|||
|
|||
/// Check if the address contains executable code.
|
|||
bool addressHasCode(Address const& _address) const { return m_state.addressHasCode(_address); } |
|||
|
|||
/// Get the root of the storage of an account.
|
|||
h256 storageRoot(Address const& _contract) const { return m_state.storageRoot(_contract); } |
|||
|
|||
/// Get the value of a storage position of an account.
|
|||
/// @returns 0 if no account exists at that address.
|
|||
u256 storage(Address const& _contract, u256 const& _memory) const { return m_state.storage(_contract, _memory); } |
|||
|
|||
/// Get the storage of an account.
|
|||
/// @note This is expensive. Don't use it unless you need to.
|
|||
/// @returns std::unordered_map<u256, u256> if no account exists at that address.
|
|||
std::unordered_map<u256, u256> storage(Address const& _contract) const { return m_state.storage(_contract); } |
|||
|
|||
/// Get the code of an account.
|
|||
/// @returns bytes() if no account exists at that address.
|
|||
bytes const& code(Address const& _contract) const { return m_state.code(_contract); } |
|||
|
|||
/// Get the code hash of an account.
|
|||
/// @returns EmptySHA3 if no account exists at that address or if there is no code associated with the address.
|
|||
h256 codeHash(Address const& _contract) const { return m_state.codeHash(_contract); } |
|||
|
|||
// General information from state
|
|||
|
|||
/// Get the backing state object.
|
|||
State const& state() const { return m_state; } |
|||
|
|||
/// Open a DB - useful for passing into the constructor & keeping for other states that are necessary.
|
|||
OverlayDB const& db() const { return m_state.db(); } |
|||
|
|||
/// The hash of the root of our state tree.
|
|||
h256 rootHash() const { return m_state.rootHash(); } |
|||
|
|||
/// @returns the set containing all addresses currently in use in Ethereum.
|
|||
/// @throws InterfaceNotSupported if compiled without ETH_FATDB.
|
|||
std::unordered_map<Address, u256> addresses() const { return m_state.addresses(); } |
|||
|
|||
// For altering accounts behind-the-scenes
|
|||
|
|||
/// Get a mutable State object which is backing this block.
|
|||
/// @warning Only use this is you know what you're doing. If you use it while constructing a
|
|||
/// normal sealable block, don't expect things to work right.
|
|||
State& mutableState() { return m_state; } |
|||
|
|||
// Information concerning ongoing transactions
|
|||
|
|||
/// Get the remaining gas limit in this block.
|
|||
u256 gasLimitRemaining() const { return m_currentBlock.gasLimit() - gasUsed(); } |
|||
|
|||
/// Get the list of pending transactions.
|
|||
Transactions const& pending() const { return m_transactions; } |
|||
|
|||
/// Get the list of hashes of pending transactions.
|
|||
h256Hash const& pendingHashes() const { return m_transactionSet; } |
|||
|
|||
/// Get the transaction receipt for the transaction of the given index.
|
|||
TransactionReceipt const& receipt(unsigned _i) const { return m_receipts[_i]; } |
|||
|
|||
/// Get the list of pending transactions.
|
|||
LogEntries const& log(unsigned _i) const { return m_receipts[_i].log(); } |
|||
|
|||
/// Get the bloom filter of all logs that happened in the block.
|
|||
LogBloom logBloom() const; |
|||
|
|||
/// Get the bloom filter of a particular transaction that happened in the block.
|
|||
LogBloom const& logBloom(unsigned _i) const { return m_receipts[_i].bloom(); } |
|||
|
|||
/// Get the State immediately after the given number of pending transactions have been applied.
|
|||
/// If (_i == 0) returns the initial state of the block.
|
|||
/// If (_i == pending().size()) returns the final state of the block, prior to rewards.
|
|||
State fromPending(unsigned _i) const; |
|||
|
|||
/// @returns the StateDiff caused by the pending transaction of index @a _i.
|
|||
StateDiff pendingDiff(unsigned _i) const { return fromPending(_i).diff(fromPending(_i + 1), true); } |
|||
|
|||
// State-change operations
|
|||
|
|||
/// Construct state object from arbitrary point in blockchain.
|
|||
PopulationStatistics populateFromChain(BlockChain const& _bc, h256 const& _hash, ImportRequirements::value _ir = ImportRequirements::None); |
|||
|
|||
/// Execute a given transaction.
|
|||
/// This will append @a _t to the transaction list and change the state accordingly.
|
|||
ExecutionResult execute(LastHashes const& _lh, Transaction const& _t, Permanence _p = Permanence::Committed, OnOpFunc const& _onOp = OnOpFunc()); |
|||
|
|||
/// Sync our transactions, killing those from the queue that we have and assimilating those that we don't.
|
|||
/// @returns a list of receipts one for each transaction placed from the queue into the state and bool, true iff there are more transactions to be processed.
|
|||
std::pair<TransactionReceipts, bool> sync(BlockChain const& _bc, TransactionQueue& _tq, GasPricer const& _gp, unsigned _msTimeout = 100); |
|||
|
|||
/// Sync our state with the block chain.
|
|||
/// This basically involves wiping ourselves if we've been superceded and rebuilding from the transaction queue.
|
|||
bool sync(BlockChain const& _bc); |
|||
|
|||
/// Sync with the block chain, but rather than synching to the latest block, instead sync to the given block.
|
|||
bool sync(BlockChain const& _bc, h256 const& _blockHash, BlockInfo const& _bi = BlockInfo()); |
|||
|
|||
/// Execute all transactions within a given block.
|
|||
/// @returns the additional total difficulty.
|
|||
u256 enactOn(VerifiedBlockRef const& _block, BlockChain const& _bc); |
|||
|
|||
/// Returns back to a pristine state after having done a playback.
|
|||
/// @arg _fullCommit if true flush everything out to disk. If false, this effectively only validates
|
|||
/// the block since all state changes are ultimately reversed.
|
|||
void cleanup(bool _fullCommit); |
|||
|
|||
/// Sets m_currentBlock to a clean state, (i.e. no change from m_previousBlock).
|
|||
void resetCurrent(); |
|||
|
|||
// Sealing
|
|||
|
|||
/// Prepares the current state for mining.
|
|||
/// Commits all transactions into the trie, compiles uncles and transactions list, applies all
|
|||
/// rewards and populates the current block header with the appropriate hashes.
|
|||
/// The only thing left to do after this is to actually mine().
|
|||
///
|
|||
/// This may be called multiple times and without issue.
|
|||
void commitToSeal(BlockChain const& _bc, bytes const& _extraData = {}); |
|||
|
|||
/// Pass in a solution to the proof-of-work.
|
|||
/// @returns true iff we were previously committed to mining.
|
|||
/// TODO: verify it prior to calling this.
|
|||
/** Commit to DB and build the final block if the previous call to mine()'s result is completion.
|
|||
* Typically looks like: |
|||
* @code |
|||
* while (notYetMined) |
|||
* { |
|||
* // lock
|
|||
* commitToSeal(_blockChain); // will call uncommitToMine if a repeat.
|
|||
* completeMine(); |
|||
* // unlock
|
|||
* @endcode |
|||
*/ |
|||
bool sealBlock(bytes const& _header) { return sealBlock(&_header); } |
|||
bool sealBlock(bytesConstRef _header); |
|||
|
|||
/// Get the complete current block, including valid nonce.
|
|||
/// Only valid after mine() returns true.
|
|||
bytes const& blockData() const { return m_currentBytes; } |
|||
|
|||
/// Get the header information on the present block.
|
|||
BlockInfo const& info() const { return m_currentBlock; } |
|||
|
|||
|
|||
private: |
|||
/// Undo the changes to the state for committing to mine.
|
|||
void uncommitToMine(); |
|||
|
|||
/// Retrieve all information about a given address into the cache.
|
|||
/// If _requireMemory is true, grab the full memory should it be a contract item.
|
|||
/// If _forceCreate is true, then insert a default item into the cache, in the case it doesn't
|
|||
/// exist in the DB.
|
|||
void ensureCached(Address const& _a, bool _requireCode, bool _forceCreate) const; |
|||
|
|||
/// Retrieve all information about a given address into a cache.
|
|||
void ensureCached(std::unordered_map<Address, Account>& _cache, Address const& _a, bool _requireCode, bool _forceCreate) const; |
|||
|
|||
/// Execute the given block, assuming it corresponds to m_currentBlock.
|
|||
/// Throws on failure.
|
|||
u256 enact(VerifiedBlockRef const& _block, BlockChain const& _bc); |
|||
|
|||
/// Finalise the block, applying the earned rewards.
|
|||
void applyRewards(std::vector<BlockInfo> const& _uncleBlockHeaders); |
|||
|
|||
/// @returns gas used by transactions thus far executed.
|
|||
u256 gasUsed() const { return m_receipts.size() ? m_receipts.back().gasUsed() : 0; } |
|||
|
|||
/// Provide a standard VM trace for debugging purposes.
|
|||
std::string vmTrace(bytesConstRef _block, BlockChain const& _bc, ImportRequirements::value _ir); |
|||
|
|||
State m_state; ///< Our state tree, as an OverlayDB DB.
|
|||
Transactions m_transactions; ///< The current list of transactions that we've included in the state.
|
|||
TransactionReceipts m_receipts; ///< The corresponding list of transaction receipts.
|
|||
h256Hash m_transactionSet; ///< The set of transaction hashes that we've included in the state.
|
|||
State m_precommit; ///< State at the point immediately prior to rewards.
|
|||
|
|||
BlockInfo m_previousBlock; ///< The previous block's information.
|
|||
BlockInfo m_currentBlock; ///< The current block's information.
|
|||
bytes m_currentBytes; ///< The current block.
|
|||
bool m_committedToMine = false; ///< Have we committed to mine on the present m_currentBlock?
|
|||
|
|||
bytes m_currentTxs; ///< The RLP-encoded block of transactions.
|
|||
bytes m_currentUncles; ///< The RLP-encoded block of uncles.
|
|||
|
|||
Address m_beneficiary; ///< Our address (i.e. the address to which fees go).
|
|||
|
|||
u256 m_blockReward; |
|||
|
|||
friend std::ostream& operator<<(std::ostream& _out, Block const& _s); |
|||
}; |
|||
|
|||
std::ostream& operator<<(std::ostream& _out, Block const& _s); |
|||
|
|||
|
|||
} |
|||
|
|||
} |
@ -1,57 +0,0 @@ |
|||
/*
|
|||
This file is part of cpp-ethereum. |
|||
|
|||
cpp-ethereum is free software: you can redistribute it and/or modify |
|||
it under the terms of the GNU General Public License as published by |
|||
the Free Software Foundation, either version 3 of the License, or |
|||
(at your option) any later version. |
|||
|
|||
cpp-ethereum is distributed in the hope that it will be useful, |
|||
but WITHOUT ANY WARRANTY; without even the implied warranty of |
|||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|||
GNU General Public License for more details. |
|||
|
|||
You should have received a copy of the GNU General Public License |
|||
along with cpp-ethereum. If not, see <http://www.gnu.org/licenses/>.
|
|||
*/ |
|||
/** @file StateLoader.cpp
|
|||
* @author Marek Kotewicz <marek@ethdev.com> |
|||
* @date 2015 |
|||
*/ |
|||
|
|||
#include "StateLoader.h" |
|||
|
|||
using namespace std; |
|||
using namespace dev; |
|||
using namespace dev::eth; |
|||
using namespace dev::test; |
|||
|
|||
StateLoader::StateLoader(Json::Value const& _json, std::string const& _dbPath): |
|||
m_state(State::openDB(_dbPath, h256{}, WithExisting::Kill), BaseState::Empty) |
|||
{ |
|||
for (string const& name: _json.getMemberNames()) |
|||
{ |
|||
Json::Value o = _json[name]; |
|||
|
|||
Address address = Address(name); |
|||
bytes code = fromHex(o["code"].asString().substr(2)); |
|||
|
|||
if (!code.empty()) |
|||
{ |
|||
m_state.m_cache[address] = Account(u256(o["balance"].asString()), Account::ContractConception); |
|||
m_state.m_cache[address].setCode(std::move(code)); |
|||
} |
|||
else |
|||
m_state.m_cache[address] = Account(u256(o["balance"].asString()), Account::NormalCreation); |
|||
|
|||
for (string const& j: o["storage"].getMemberNames()) |
|||
m_state.setStorage(address, u256(j), u256(o["storage"][j].asString())); |
|||
|
|||
for (auto i = 0; i < u256(o["nonce"].asString()); ++i) |
|||
m_state.noteSending(address); |
|||
|
|||
m_state.ensureCached(address, false, false); |
|||
} |
|||
|
|||
m_state.commit(); |
|||
} |
@ -1,49 +0,0 @@ |
|||
/*
|
|||
This file is part of cpp-ethereum. |
|||
|
|||
cpp-ethereum is free software: you can redistribute it and/or modify |
|||
it under the terms of the GNU General Public License as published by |
|||
the Free Software Foundation, either version 3 of the License, or |
|||
(at your option) any later version. |
|||
|
|||
cpp-ethereum is distributed in the hope that it will be useful, |
|||
but WITHOUT ANY WARRANTY; without even the implied warranty of |
|||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|||
GNU General Public License for more details. |
|||
|
|||
You should have received a copy of the GNU General Public License |
|||
along with cpp-ethereum. If not, see <http://www.gnu.org/licenses/>.
|
|||
*/ |
|||
/** @file StateLoader.h
|
|||
* @author Marek Kotewicz <marek@ethdev.com> |
|||
* @date 2015 |
|||
*/ |
|||
|
|||
#pragma once |
|||
|
|||
#include <json/json.h> |
|||
#include <libdevcore/TransientDirectory.h> |
|||
#include <libethereum/State.h> |
|||
#include <libethereum/BlockChain.h> |
|||
|
|||
namespace dev |
|||
{ |
|||
namespace test |
|||
{ |
|||
|
|||
/**
|
|||
* @brief Friend of State, loads State from given JSON object |
|||
*/ |
|||
class StateLoader |
|||
{ |
|||
public: |
|||
StateLoader(Json::Value const& _json, std::string const& _dbPath); |
|||
eth::State const& state() const { return m_state; } |
|||
eth::StateDefinition const& stateDefinition() const { return m_state.m_cache; } |
|||
|
|||
private: |
|||
eth::State m_state; |
|||
}; |
|||
|
|||
} |
|||
} |
@ -0,0 +1,497 @@ |
|||
/*
|
|||
This file is part of cpp-ethereum. |
|||
|
|||
cpp-ethereum is free software: you can redistribute it and/or modify |
|||
it under the terms of the GNU General Public License as published by |
|||
the Free Software Foundation, either version 3 of the License, or |
|||
(at your option) any later version. |
|||
|
|||
cpp-ethereum is distributed in the hope that it will be useful, |
|||
but WITHOUT ANY WARRANTY; without even the implied warranty of |
|||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|||
GNU General Public License for more details. |
|||
|
|||
You should have received a copy of the GNU General Public License |
|||
along with cpp-ethereum. If not, see <http://www.gnu.org/licenses/>.
|
|||
*/ |
|||
/**
|
|||
* @author Christian <c@ethdev.com> |
|||
* @date 2015 |
|||
* Tests for a fixed fee registrar contract. |
|||
*/ |
|||
|
|||
#include <string> |
|||
#include <tuple> |
|||
#include <boost/test/unit_test.hpp> |
|||
#include <libdevcore/Hash.h> |
|||
#include <libethcore/ABI.h> |
|||
#include <test/libsolidity/solidityExecutionFramework.h> |
|||
|
|||
using namespace std; |
|||
|
|||
namespace dev |
|||
{ |
|||
namespace solidity |
|||
{ |
|||
namespace test |
|||
{ |
|||
|
|||
namespace |
|||
{ |
|||
|
|||
static char const* registrarCode = R"DELIMITER( |
|||
//sol
|
|||
|
|||
contract NameRegister { |
|||
function addr(string _name) constant returns (address o_owner); |
|||
function name(address _owner) constant returns (string o_name); |
|||
} |
|||
|
|||
contract Registrar is NameRegister { |
|||
event Changed(string indexed name); |
|||
event PrimaryChanged(string indexed name, address indexed addr); |
|||
|
|||
function owner(string _name) constant returns (address o_owner); |
|||
function addr(string _name) constant returns (address o_address); |
|||
function subRegistrar(string _name) constant returns (address o_subRegistrar); |
|||
function content(string _name) constant returns (bytes32 o_content); |
|||
|
|||
function name(address _owner) constant returns (string o_name); |
|||
} |
|||
|
|||
contract AuctionSystem { |
|||
event AuctionEnded(string indexed _name, address _winner); |
|||
event NewBid(string indexed _name, address _bidder, uint _value); |
|||
|
|||
/// Function that is called once an auction ends.
|
|||
function onAuctionEnd(string _name) internal; |
|||
|
|||
function bid(string _name, address _bidder, uint _value) internal { |
|||
var auction = m_auctions[_name]; |
|||
if (auction.endDate > 0 && now > auction.endDate) |
|||
{ |
|||
AuctionEnded(_name, auction.highestBidder); |
|||
onAuctionEnd(_name); |
|||
delete m_auctions[_name]; |
|||
return; |
|||
} |
|||
if (msg.value > auction.highestBid) |
|||
{ |
|||
// new bid on auction
|
|||
auction.secondHighestBid = auction.highestBid; |
|||
auction.sumOfBids += _value; |
|||
auction.highestBid = _value; |
|||
auction.highestBidder = _bidder; |
|||
auction.endDate = now + c_biddingTime; |
|||
|
|||
NewBid(_name, _bidder, _value); |
|||
} |
|||
} |
|||
|
|||
uint constant c_biddingTime = 7 days; |
|||
|
|||
struct Auction { |
|||
address highestBidder; |
|||
uint highestBid; |
|||
uint secondHighestBid; |
|||
uint sumOfBids; |
|||
uint endDate; |
|||
} |
|||
mapping(string => Auction) m_auctions; |
|||
} |
|||
|
|||
contract GlobalRegistrar is Registrar, AuctionSystem { |
|||
struct Record { |
|||
address owner; |
|||
address primary; |
|||
address subRegistrar; |
|||
bytes32 content; |
|||
uint renewalDate; |
|||
} |
|||
|
|||
uint constant c_renewalInterval = 1 years; |
|||
uint constant c_freeBytes = 12; |
|||
|
|||
function Registrar() { |
|||
// TODO: Populate with hall-of-fame.
|
|||
} |
|||
|
|||
function() { |
|||
// prevent people from just sending funds to the registrar
|
|||
__throw(); |
|||
} |
|||
|
|||
function onAuctionEnd(string _name) internal { |
|||
var auction = m_auctions[_name]; |
|||
var record = m_toRecord[_name]; |
|||
if (record.owner != 0) |
|||
record.owner.send(auction.sumOfBids - auction.highestBid / 100); |
|||
else |
|||
auction.highestBidder.send(auction.highestBid - auction.secondHighestBid); |
|||
record.renewalDate = now + c_renewalInterval; |
|||
record.owner = auction.highestBidder; |
|||
Changed(_name); |
|||
} |
|||
|
|||
function reserve(string _name) external { |
|||
if (bytes(_name).length == 0) |
|||
__throw(); |
|||
bool needAuction = requiresAuction(_name); |
|||
if (needAuction) |
|||
{ |
|||
if (now < m_toRecord[_name].renewalDate) |
|||
__throw(); |
|||
bid(_name, msg.sender, msg.value); |
|||
} |
|||
else |
|||
{ |
|||
Record record = m_toRecord[_name]; |
|||
if (record.owner != 0) |
|||
__throw(); |
|||
m_toRecord[_name].owner = msg.sender; |
|||
Changed(_name); |
|||
} |
|||
} |
|||
|
|||
function requiresAuction(string _name) internal returns (bool) { |
|||
return bytes(_name).length < c_freeBytes; |
|||
} |
|||
|
|||
modifier onlyrecordowner(string _name) { if (m_toRecord[_name].owner == msg.sender) _ } |
|||
|
|||
function transfer(string _name, address _newOwner) onlyrecordowner(_name) { |
|||
m_toRecord[_name].owner = _newOwner; |
|||
Changed(_name); |
|||
} |
|||
|
|||
function disown(string _name) onlyrecordowner(_name) { |
|||
if (stringsEqual(m_toName[m_toRecord[_name].primary], _name)) |
|||
{ |
|||
PrimaryChanged(_name, m_toRecord[_name].primary); |
|||
m_toName[m_toRecord[_name].primary] = ""; |
|||
} |
|||
delete m_toRecord[_name]; |
|||
Changed(_name); |
|||
} |
|||
|
|||
function setAddress(string _name, address _a, bool _primary) onlyrecordowner(_name) { |
|||
m_toRecord[_name].primary = _a; |
|||
if (_primary) |
|||
{ |
|||
PrimaryChanged(_name, _a); |
|||
m_toName[_a] = _name; |
|||
} |
|||
Changed(_name); |
|||
} |
|||
function setSubRegistrar(string _name, address _registrar) onlyrecordowner(_name) { |
|||
m_toRecord[_name].subRegistrar = _registrar; |
|||
Changed(_name); |
|||
} |
|||
function setContent(string _name, bytes32 _content) onlyrecordowner(_name) { |
|||
m_toRecord[_name].content = _content; |
|||
Changed(_name); |
|||
} |
|||
|
|||
function stringsEqual(string storage _a, string memory _b) internal returns (bool) { |
|||
bytes storage a = bytes(_a); |
|||
bytes memory b = bytes(_b); |
|||
if (a.length != b.length) |
|||
return false; |
|||
// @todo unroll this loop
|
|||
for (uint i = 0; i < a.length; i ++) |
|||
if (a[i] != b[i]) |
|||
return false; |
|||
return true; |
|||
} |
|||
|
|||
function owner(string _name) constant returns (address) { return m_toRecord[_name].owner; } |
|||
function addr(string _name) constant returns (address) { return m_toRecord[_name].primary; } |
|||
function subRegistrar(string _name) constant returns (address) { return m_toRecord[_name].subRegistrar; } |
|||
function content(string _name) constant returns (bytes32) { return m_toRecord[_name].content; } |
|||
function name(address _addr) constant returns (string o_name) { return m_toName[_addr]; } |
|||
|
|||
function __throw() internal { |
|||
// workaround until we have "throw"
|
|||
uint[] x; x[1]; |
|||
} |
|||
|
|||
mapping (address => string) m_toName; |
|||
mapping (string => Record) m_toRecord; |
|||
} |
|||
)DELIMITER"; |
|||
|
|||
static unique_ptr<bytes> s_compiledRegistrar; |
|||
|
|||
class AuctionRegistrarTestFramework: public ExecutionFramework |
|||
{ |
|||
protected: |
|||
void deployRegistrar() |
|||
{ |
|||
if (!s_compiledRegistrar) |
|||
{ |
|||
m_optimize = true; |
|||
m_compiler.reset(false, m_addStandardSources); |
|||
m_compiler.addSource("", registrarCode); |
|||
ETH_TEST_REQUIRE_NO_THROW(m_compiler.compile(m_optimize, m_optimizeRuns), "Compiling contract failed"); |
|||
s_compiledRegistrar.reset(new bytes(m_compiler.getBytecode("GlobalRegistrar"))); |
|||
} |
|||
sendMessage(*s_compiledRegistrar, true); |
|||
BOOST_REQUIRE(!m_output.empty()); |
|||
} |
|||
|
|||
using ContractInterface = ExecutionFramework::ContractInterface; |
|||
class RegistrarInterface: public ContractInterface |
|||
{ |
|||
public: |
|||
RegistrarInterface(ExecutionFramework& _framework): ContractInterface(_framework) {} |
|||
void reserve(string const& _name) |
|||
{ |
|||
callString("reserve", _name); |
|||
} |
|||
u160 owner(string const& _name) |
|||
{ |
|||
return callStringReturnsAddress("owner", _name); |
|||
} |
|||
void setAddress(string const& _name, u160 const& _address, bool _primary) |
|||
{ |
|||
callStringAddressBool("setAddress", _name, _address, _primary); |
|||
} |
|||
u160 addr(string const& _name) |
|||
{ |
|||
return callStringReturnsAddress("addr", _name); |
|||
} |
|||
string name(u160 const& _addr) |
|||
{ |
|||
return callAddressReturnsString("name", _addr); |
|||
} |
|||
void setSubRegistrar(string const& _name, u160 const& _address) |
|||
{ |
|||
callStringAddress("setSubRegistrar", _name, _address); |
|||
} |
|||
u160 subRegistrar(string const& _name) |
|||
{ |
|||
return callStringReturnsAddress("subRegistrar", _name); |
|||
} |
|||
void setContent(string const& _name, h256 const& _content) |
|||
{ |
|||
callStringBytes32("setContent", _name, _content); |
|||
} |
|||
h256 content(string const& _name) |
|||
{ |
|||
return callStringReturnsBytes32("content", _name); |
|||
} |
|||
void transfer(string const& _name, u160 const& _target) |
|||
{ |
|||
return callStringAddress("transfer", _name, _target); |
|||
} |
|||
void disown(string const& _name) |
|||
{ |
|||
return callString("disown", _name); |
|||
} |
|||
}; |
|||
|
|||
u256 const m_biddingTime = u256(7 * 24 * 3600); |
|||
u256 const m_renewalInterval = u256(365 * 24 * 3600); |
|||
}; |
|||
|
|||
} |
|||
|
|||
/// This is a test suite that tests optimised code!
|
|||
BOOST_FIXTURE_TEST_SUITE(SolidityAuctionRegistrar, AuctionRegistrarTestFramework) |
|||
|
|||
BOOST_AUTO_TEST_CASE(creation) |
|||
{ |
|||
deployRegistrar(); |
|||
} |
|||
|
|||
BOOST_AUTO_TEST_CASE(reserve) |
|||
{ |
|||
// Test that reserving works for long strings
|
|||
deployRegistrar(); |
|||
vector<string> names{"abcabcabcabcabc", "defdefdefdefdef", "ghighighighighighighighighighighighighighighi"}; |
|||
m_sender = Address(0x123); |
|||
|
|||
RegistrarInterface registrar(*this); |
|||
|
|||
// should not work
|
|||
registrar.reserve(""); |
|||
BOOST_CHECK_EQUAL(registrar.owner(""), u160(0)); |
|||
|
|||
for (auto const& name: names) |
|||
{ |
|||
registrar.reserve(name); |
|||
BOOST_CHECK_EQUAL(registrar.owner(name), u160(0x123)); |
|||
} |
|||
} |
|||
|
|||
BOOST_AUTO_TEST_CASE(double_reserve_long) |
|||
{ |
|||
// Test that it is not possible to re-reserve from a different address.
|
|||
deployRegistrar(); |
|||
string name = "abcabcabcabcabcabcabcabcabcabca"; |
|||
m_sender = Address(0x123); |
|||
RegistrarInterface registrar(*this); |
|||
registrar.reserve(name); |
|||
BOOST_CHECK_EQUAL(registrar.owner(name), u160(0x123)); |
|||
|
|||
m_sender = Address(0x124); |
|||
registrar.reserve(name); |
|||
BOOST_CHECK_EQUAL(registrar.owner(name), u160(0x123)); |
|||
} |
|||
|
|||
BOOST_AUTO_TEST_CASE(properties) |
|||
{ |
|||
// Test setting and retrieving the various properties works.
|
|||
deployRegistrar(); |
|||
RegistrarInterface registrar(*this); |
|||
string names[] = {"abcaeouoeuaoeuaoeu", "defncboagufra,fui", "ghagpyajfbcuajouhaeoi"}; |
|||
size_t addr = 0x9872543; |
|||
for (string const& name: names) |
|||
{ |
|||
addr++; |
|||
size_t sender = addr + 10007; |
|||
m_sender = Address(sender); |
|||
// setting by sender works
|
|||
registrar.reserve(name); |
|||
BOOST_CHECK_EQUAL(registrar.owner(name), u160(sender)); |
|||
registrar.setAddress(name, addr, true); |
|||
BOOST_CHECK_EQUAL(registrar.addr(name), u160(addr)); |
|||
registrar.setSubRegistrar(name, addr + 20); |
|||
BOOST_CHECK_EQUAL(registrar.subRegistrar(name), u160(addr + 20)); |
|||
registrar.setContent(name, h256(u256(addr + 90))); |
|||
BOOST_CHECK_EQUAL(registrar.content(name), h256(u256(addr + 90))); |
|||
|
|||
// but not by someone else
|
|||
m_sender = Address(h256(addr + 10007 - 1)); |
|||
BOOST_CHECK_EQUAL(registrar.owner(name), u160(sender)); |
|||
registrar.setAddress(name, addr + 1, true); |
|||
BOOST_CHECK_EQUAL(registrar.addr(name), u160(addr)); |
|||
registrar.setSubRegistrar(name, addr + 20 + 1); |
|||
BOOST_CHECK_EQUAL(registrar.subRegistrar(name), u160(addr + 20)); |
|||
registrar.setContent(name, h256(u256(addr + 90 + 1))); |
|||
BOOST_CHECK_EQUAL(registrar.content(name), h256(u256(addr + 90))); |
|||
} |
|||
} |
|||
|
|||
BOOST_AUTO_TEST_CASE(transfer) |
|||
{ |
|||
deployRegistrar(); |
|||
string name = "abcaoeguaoucaeoduceo"; |
|||
m_sender = Address(0x123); |
|||
RegistrarInterface registrar(*this); |
|||
registrar.reserve(name); |
|||
registrar.setContent(name, h256(u256(123))); |
|||
registrar.transfer(name, u160(555)); |
|||
BOOST_CHECK_EQUAL(registrar.owner(name), u160(555)); |
|||
BOOST_CHECK_EQUAL(registrar.content(name), h256(u256(123))); |
|||
} |
|||
|
|||
BOOST_AUTO_TEST_CASE(disown) |
|||
{ |
|||
deployRegistrar(); |
|||
string name = "abcaoeguaoucaeoduceo"; |
|||
m_sender = Address(0x123); |
|||
RegistrarInterface registrar(*this); |
|||
registrar.reserve(name); |
|||
registrar.setContent(name, h256(u256(123))); |
|||
registrar.setAddress(name, u160(124), true); |
|||
registrar.setSubRegistrar(name, u160(125)); |
|||
BOOST_CHECK_EQUAL(registrar.name(u160(124)), name); |
|||
|
|||
// someone else tries disowning
|
|||
m_sender = Address(0x128); |
|||
registrar.disown(name); |
|||
BOOST_CHECK_EQUAL(registrar.owner(name), 0x123); |
|||
|
|||
m_sender = Address(0x123); |
|||
registrar.disown(name); |
|||
BOOST_CHECK_EQUAL(registrar.owner(name), 0); |
|||
BOOST_CHECK_EQUAL(registrar.addr(name), 0); |
|||
BOOST_CHECK_EQUAL(registrar.subRegistrar(name), 0); |
|||
BOOST_CHECK_EQUAL(registrar.content(name), h256()); |
|||
BOOST_CHECK_EQUAL(registrar.name(u160(124)), ""); |
|||
} |
|||
|
|||
BOOST_AUTO_TEST_CASE(auction_simple) |
|||
{ |
|||
deployRegistrar(); |
|||
string name = "x"; |
|||
m_sender = Address(0x123); |
|||
RegistrarInterface registrar(*this); |
|||
// initiate auction
|
|||
registrar.setNextValue(8); |
|||
registrar.reserve(name); |
|||
BOOST_CHECK_EQUAL(registrar.owner(name), 0); |
|||
// "wait" until auction end
|
|||
m_envInfo.setTimestamp(m_envInfo.timestamp() + m_biddingTime + 10); |
|||
// trigger auction again
|
|||
registrar.reserve(name); |
|||
BOOST_CHECK_EQUAL(registrar.owner(name), 0x123); |
|||
} |
|||
|
|||
BOOST_AUTO_TEST_CASE(auction_bidding) |
|||
{ |
|||
deployRegistrar(); |
|||
string name = "x"; |
|||
m_sender = Address(0x123); |
|||
RegistrarInterface registrar(*this); |
|||
// initiate auction
|
|||
registrar.setNextValue(8); |
|||
registrar.reserve(name); |
|||
BOOST_CHECK_EQUAL(registrar.owner(name), 0); |
|||
// overbid self
|
|||
m_envInfo.setTimestamp(m_biddingTime - 10); |
|||
registrar.setNextValue(12); |
|||
registrar.reserve(name); |
|||
// another bid by someone else
|
|||
m_sender = Address(0x124); |
|||
m_envInfo.setTimestamp(2 * m_biddingTime - 50); |
|||
registrar.setNextValue(13); |
|||
registrar.reserve(name); |
|||
BOOST_CHECK_EQUAL(registrar.owner(name), 0); |
|||
// end auction by first bidder (which is not highest) trying to overbid again (too late)
|
|||
m_sender = Address(0x123); |
|||
m_envInfo.setTimestamp(4 * m_biddingTime); |
|||
registrar.setNextValue(20); |
|||
registrar.reserve(name); |
|||
BOOST_CHECK_EQUAL(registrar.owner(name), 0x124); |
|||
} |
|||
|
|||
BOOST_AUTO_TEST_CASE(auction_renewal) |
|||
{ |
|||
deployRegistrar(); |
|||
string name = "x"; |
|||
RegistrarInterface registrar(*this); |
|||
// register name by auction
|
|||
m_sender = Address(0x123); |
|||
registrar.setNextValue(8); |
|||
registrar.reserve(name); |
|||
m_envInfo.setTimestamp(4 * m_biddingTime); |
|||
registrar.reserve(name); |
|||
BOOST_CHECK_EQUAL(registrar.owner(name), 0x123); |
|||
|
|||
// try to re-register before interval end
|
|||
m_sender = Address(0x222); |
|||
registrar.setNextValue(80); |
|||
m_envInfo.setTimestamp(m_envInfo.timestamp() + m_renewalInterval - 1); |
|||
registrar.reserve(name); |
|||
m_envInfo.setTimestamp(m_envInfo.timestamp() + m_biddingTime); |
|||
// if there is a bug in the renewal logic, this would transfer the ownership to 0x222,
|
|||
// but if there is no bug, this will initiate the auction, albeit with a zero bid
|
|||
registrar.reserve(name); |
|||
BOOST_CHECK_EQUAL(registrar.owner(name), 0x123); |
|||
|
|||
m_envInfo.setTimestamp(m_envInfo.timestamp() + 2); |
|||
registrar.setNextValue(80); |
|||
registrar.reserve(name); |
|||
BOOST_CHECK_EQUAL(registrar.owner(name), 0x123); |
|||
m_envInfo.setTimestamp(m_envInfo.timestamp() + m_biddingTime + 2); |
|||
registrar.reserve(name); |
|||
BOOST_CHECK_EQUAL(registrar.owner(name), 0x222); |
|||
} |
|||
|
|||
BOOST_AUTO_TEST_SUITE_END() |
|||
|
|||
} |
|||
} |
|||
} // end namespaces
|
@ -0,0 +1,169 @@ |
|||
/*
|
|||
This file is part of cpp-ethereum. |
|||
|
|||
cpp-ethereum is free software: you can redistribute it and/or modify |
|||
it under the terms of the GNU General Public License as published by |
|||
the Free Software Foundation, either version 3 of the License, or |
|||
(at your option) any later version. |
|||
|
|||
cpp-ethereum is distributed in the hope that it will be useful, |
|||
but WITHOUT ANY WARRANTY; without even the implied warranty of |
|||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|||
GNU General Public License for more details. |
|||
|
|||
You should have received a copy of the GNU General Public License |
|||
along with cpp-ethereum. If not, see <http://www.gnu.org/licenses/>.
|
|||
*/ |
|||
/** @file Transaction.cpp
|
|||
* @author Dmitrii Khokhlov <winsvega@mail.ru> |
|||
* @date 2015 |
|||
* Transaaction test functions. |
|||
*/ |
|||
|
|||
#include "test/TestHelper.h" |
|||
#include <libethcore/Exceptions.h> |
|||
#include <libevm/VMFace.h> |
|||
#include <libethcore/Common.h> |
|||
|
|||
using namespace dev; |
|||
using namespace eth; |
|||
|
|||
BOOST_AUTO_TEST_SUITE(libethereum) |
|||
|
|||
BOOST_AUTO_TEST_CASE(TransactionGasRequired) |
|||
{ |
|||
Transaction tr(fromHex("0xf86d800182521c94095e7baea6a6c7c4c2dfeb977efac326af552d870a8e0358ac39584bc98a7c979f984b031ba048b55bfa915ac795c431978d8a6a992b628d557da5ff759b307d495a36649353a0efffd310ac743f371de3b9f7f9cb56c0b28ad43601b4ab949f53faa07bd2c804"), CheckTransaction::None); |
|||
BOOST_CHECK_MESSAGE(tr.gasRequired() == 21952, "Transaction::GasRequired() has changed!"); |
|||
} |
|||
|
|||
BOOST_AUTO_TEST_CASE(TransactionConstructor) |
|||
{ |
|||
bool wasException = false; |
|||
try |
|||
{ |
|||
Transaction(fromHex("0xf86d800182521c94095e7baea6a6c7c4c2dfeb977efac326af552d870a8e0358ac39584bc98a7c979f984b031ba048b55bfa915ac795c431978d8a6a992b628d557da5ff759b307d495a36649353a0efffd310ac743f371de3b9f7f9cb56c0b28ad43601b4ab949f53faa07bd2c804"), CheckTransaction::Everything); |
|||
} |
|||
catch (OutOfGasIntrinsic) |
|||
{ |
|||
wasException = true; |
|||
} |
|||
catch (Exception) |
|||
{ |
|||
BOOST_ERROR("Exception thrown but expected OutOfGasIntrinsic instead"); |
|||
} |
|||
|
|||
BOOST_CHECK_MESSAGE(wasException, "Expected OutOfGasIntrinsic exception to be thrown at TransactionConstructor test"); |
|||
} |
|||
|
|||
BOOST_AUTO_TEST_CASE(ExecutionResultOutput) |
|||
{ |
|||
std::stringstream buffer; |
|||
ExecutionResult exRes; |
|||
|
|||
exRes.gasUsed = u256("12345"); |
|||
exRes.newAddress = Address("a94f5374fce5edbc8e2a8697c15331677e6ebf0b"); |
|||
exRes.output = fromHex("001122334455"); |
|||
|
|||
buffer << exRes; |
|||
BOOST_CHECK_MESSAGE(buffer.str() == "{12345, a94f5374fce5edbc8e2a8697c15331677e6ebf0b, 001122334455}", "Error ExecutionResultOutput"); |
|||
} |
|||
|
|||
BOOST_AUTO_TEST_CASE(transactionExceptionOutput) |
|||
{ |
|||
std::stringstream buffer; |
|||
buffer << TransactionException::BadInstruction; |
|||
BOOST_CHECK_MESSAGE(buffer.str() == "BadInstruction", "Error output TransactionException::BadInstruction"); |
|||
buffer.str(std::string()); |
|||
|
|||
buffer << TransactionException::None; |
|||
BOOST_CHECK_MESSAGE(buffer.str() == "None", "Error output TransactionException::None"); |
|||
buffer.str(std::string()); |
|||
|
|||
buffer << TransactionException::BadRLP; |
|||
BOOST_CHECK_MESSAGE(buffer.str() == "BadRLP", "Error output TransactionException::BadRLP"); |
|||
buffer.str(std::string()); |
|||
|
|||
buffer << TransactionException::InvalidFormat; |
|||
BOOST_CHECK_MESSAGE(buffer.str() == "InvalidFormat", "Error output TransactionException::InvalidFormat"); |
|||
buffer.str(std::string()); |
|||
|
|||
buffer << TransactionException::OutOfGasIntrinsic; |
|||
BOOST_CHECK_MESSAGE(buffer.str() == "OutOfGasIntrinsic", "Error output TransactionException::OutOfGasIntrinsic"); |
|||
buffer.str(std::string()); |
|||
|
|||
buffer << TransactionException::InvalidSignature; |
|||
BOOST_CHECK_MESSAGE(buffer.str() == "InvalidSignature", "Error output TransactionException::InvalidSignature"); |
|||
buffer.str(std::string()); |
|||
|
|||
buffer << TransactionException::InvalidNonce; |
|||
BOOST_CHECK_MESSAGE(buffer.str() == "InvalidNonce", "Error output TransactionException::InvalidNonce"); |
|||
buffer.str(std::string()); |
|||
|
|||
buffer << TransactionException::NotEnoughCash; |
|||
BOOST_CHECK_MESSAGE(buffer.str() == "NotEnoughCash", "Error output TransactionException::NotEnoughCash"); |
|||
buffer.str(std::string()); |
|||
|
|||
buffer << TransactionException::OutOfGasBase; |
|||
BOOST_CHECK_MESSAGE(buffer.str() == "OutOfGasBase", "Error output TransactionException::OutOfGasBase"); |
|||
buffer.str(std::string()); |
|||
|
|||
buffer << TransactionException::BlockGasLimitReached; |
|||
BOOST_CHECK_MESSAGE(buffer.str() == "BlockGasLimitReached", "Error output TransactionException::BlockGasLimitReached"); |
|||
buffer.str(std::string()); |
|||
|
|||
buffer << TransactionException::BadInstruction; |
|||
BOOST_CHECK_MESSAGE(buffer.str() == "BadInstruction", "Error output TransactionException::BadInstruction"); |
|||
buffer.str(std::string()); |
|||
|
|||
buffer << TransactionException::BadJumpDestination; |
|||
BOOST_CHECK_MESSAGE(buffer.str() == "BadJumpDestination", "Error output TransactionException::BadJumpDestination"); |
|||
buffer.str(std::string()); |
|||
|
|||
buffer << TransactionException::OutOfGas; |
|||
BOOST_CHECK_MESSAGE(buffer.str() == "OutOfGas", "Error output TransactionException::OutOfGas"); |
|||
buffer.str(std::string()); |
|||
|
|||
buffer << TransactionException::OutOfStack; |
|||
BOOST_CHECK_MESSAGE(buffer.str() == "OutOfStack", "Error output TransactionException::OutOfStack"); |
|||
buffer.str(std::string()); |
|||
|
|||
buffer << TransactionException::StackUnderflow; |
|||
BOOST_CHECK_MESSAGE(buffer.str() == "StackUnderflow", "Error output TransactionException::StackUnderflow"); |
|||
buffer.str(std::string()); |
|||
|
|||
buffer << TransactionException(-1); |
|||
BOOST_CHECK_MESSAGE(buffer.str() == "Unknown", "Error output TransactionException::StackUnderflow"); |
|||
buffer.str(std::string()); |
|||
} |
|||
|
|||
BOOST_AUTO_TEST_CASE(toTransactionExceptionConvert) |
|||
{ |
|||
RLPException rlpEx("exception");//toTransactionException(*(dynamic_cast<Exception*>
|
|||
BOOST_CHECK_MESSAGE(toTransactionException(rlpEx) == TransactionException::BadRLP, "RLPException !=> TransactionException"); |
|||
OutOfGasIntrinsic oogEx; |
|||
BOOST_CHECK_MESSAGE(toTransactionException(oogEx) == TransactionException::OutOfGasIntrinsic, "OutOfGasIntrinsic !=> TransactionException"); |
|||
InvalidSignature sigEx; |
|||
BOOST_CHECK_MESSAGE(toTransactionException(sigEx) == TransactionException::InvalidSignature, "InvalidSignature !=> TransactionException"); |
|||
OutOfGasBase oogbEx; |
|||
BOOST_CHECK_MESSAGE(toTransactionException(oogbEx) == TransactionException::OutOfGasBase, "OutOfGasBase !=> TransactionException"); |
|||
InvalidNonce nonceEx; |
|||
BOOST_CHECK_MESSAGE(toTransactionException(nonceEx) == TransactionException::InvalidNonce, "InvalidNonce !=> TransactionException"); |
|||
NotEnoughCash cashEx; |
|||
BOOST_CHECK_MESSAGE(toTransactionException(cashEx) == TransactionException::NotEnoughCash, "NotEnoughCash !=> TransactionException"); |
|||
BlockGasLimitReached blGasEx; |
|||
BOOST_CHECK_MESSAGE(toTransactionException(blGasEx) == TransactionException::BlockGasLimitReached, "BlockGasLimitReached !=> TransactionException"); |
|||
BadInstruction badInsEx; |
|||
BOOST_CHECK_MESSAGE(toTransactionException(badInsEx) == TransactionException::BadInstruction, "BadInstruction !=> TransactionException"); |
|||
BadJumpDestination badJumpEx; |
|||
BOOST_CHECK_MESSAGE(toTransactionException(badJumpEx) == TransactionException::BadJumpDestination, "BadJumpDestination !=> TransactionException"); |
|||
OutOfGas oogEx2; |
|||
BOOST_CHECK_MESSAGE(toTransactionException(oogEx2) == TransactionException::OutOfGas, "OutOfGas !=> TransactionException"); |
|||
OutOfStack oosEx; |
|||
BOOST_CHECK_MESSAGE(toTransactionException(oosEx) == TransactionException::OutOfStack, "OutOfStack !=> TransactionException"); |
|||
StackUnderflow stackEx; |
|||
BOOST_CHECK_MESSAGE(toTransactionException(stackEx) == TransactionException::StackUnderflow, "StackUnderflow !=> TransactionException"); |
|||
Exception notEx; |
|||
BOOST_CHECK_MESSAGE(toTransactionException(notEx) == TransactionException::Unknown, "Unexpected should be TransactionException::Unknown"); |
|||
} |
|||
|
|||
BOOST_AUTO_TEST_SUITE_END() |
@ -0,0 +1,264 @@ |
|||
/*
|
|||
This file is part of cpp-ethereum. |
|||
|
|||
cpp-ethereum is free software: you can redistribute it and/or modify |
|||
it under the terms of the GNU General Public License as published by |
|||
the Free Software Foundation, either version 3 of the License, or |
|||
(at your option) any later version. |
|||
|
|||
cpp-ethereum is distributed in the hope that it will be useful, |
|||
but WITHOUT ANY WARRANTY; without even the implied warranty of |
|||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|||
GNU General Public License for more details. |
|||
|
|||
You should have received a copy of the GNU General Public License |
|||
along with cpp-ethereum. If not, see <http://www.gnu.org/licenses/>.
|
|||
*/ |
|||
/** @file shhrpc.cpp
|
|||
* @author Vladislav Gluhovsky <vlad@ethdev.com> |
|||
* @date July 2015 |
|||
*/ |
|||
|
|||
#include <boost/test/unit_test.hpp> |
|||
#include <boost/lexical_cast.hpp> |
|||
#include <libdevcore/Log.h> |
|||
#include <libdevcore/CommonIO.h> |
|||
#include <libethcore/CommonJS.h> |
|||
#include <libwebthree/WebThree.h> |
|||
#include <libweb3jsonrpc/WebThreeStubServer.h> |
|||
#include <jsonrpccpp/server/connectors/httpserver.h> |
|||
#include <jsonrpccpp/client/connectors/httpclient.h> |
|||
#include <test/TestHelper.h> |
|||
#include <test/libweb3jsonrpc/webthreestubclient.h> |
|||
#include <libethcore/KeyManager.h> |
|||
#include <libp2p/Common.h> |
|||
#include <libwhisper/WhisperHost.h> |
|||
|
|||
using namespace std; |
|||
using namespace dev; |
|||
using namespace dev::eth; |
|||
using namespace dev::p2p; |
|||
using namespace dev::shh; |
|||
namespace js = json_spirit; |
|||
|
|||
WebThreeDirect* web3; |
|||
unique_ptr<WebThreeStubServer> jsonrpcServer; |
|||
unique_ptr<WebThreeStubClient> jsonrpcClient; |
|||
static uint16_t const web3port = 30333; |
|||
|
|||
struct Setup |
|||
{ |
|||
Setup() |
|||
{ |
|||
dev::p2p::NodeIPEndpoint::test_allowLocal = true; |
|||
|
|||
static bool setup = false; |
|||
if (!setup) |
|||
{ |
|||
setup = true; |
|||
NetworkPreferences nprefs(std::string(), web3port, false); |
|||
web3 = new WebThreeDirect("shhrpc-web3", "", WithExisting::Trust, {"eth", "shh"}, nprefs); |
|||
web3->setIdealPeerCount(1); |
|||
web3->ethereum()->setForceMining(false); |
|||
auto server = new jsonrpc::HttpServer(8080); |
|||
vector<KeyPair> v; |
|||
KeyManager keyMan; |
|||
TrivialGasPricer gp; |
|||
jsonrpcServer = unique_ptr<WebThreeStubServer>(new WebThreeStubServer(*server, *web3, nullptr, v, keyMan, gp)); |
|||
jsonrpcServer->setIdentities({}); |
|||
jsonrpcServer->StartListening(); |
|||
auto client = new jsonrpc::HttpClient("http://localhost:8080"); |
|||
jsonrpcClient = unique_ptr<WebThreeStubClient>(new WebThreeStubClient(*client)); |
|||
} |
|||
} |
|||
|
|||
~Setup() |
|||
{ |
|||
dev::p2p::NodeIPEndpoint::test_allowLocal = false; |
|||
} |
|||
}; |
|||
|
|||
|
|||
BOOST_FIXTURE_TEST_SUITE(shhrpc, Setup) |
|||
|
|||
BOOST_AUTO_TEST_CASE(basic) |
|||
{ |
|||
cnote << "Testing web3 basic functionality..."; |
|||
|
|||
web3->startNetwork(); |
|||
unsigned const step = 10; |
|||
for (unsigned i = 0; i < 3000 && !web3->haveNetwork(); i += step) |
|||
this_thread::sleep_for(chrono::milliseconds(step)); |
|||
|
|||
BOOST_REQUIRE(web3->haveNetwork()); |
|||
|
|||
uint16_t const port2 = 30334; |
|||
NetworkPreferences prefs2("127.0.0.1", port2, false); |
|||
string const version2 = "shhrpc-host2"; |
|||
Host host2(version2, prefs2); |
|||
auto whost2 = host2.registerCapability(new WhisperHost()); |
|||
host2.start(); |
|||
|
|||
for (unsigned i = 0; i < 3000 && !host2.haveNetwork(); i += step) |
|||
this_thread::sleep_for(chrono::milliseconds(step)); |
|||
|
|||
BOOST_REQUIRE(host2.haveNetwork()); |
|||
|
|||
web3->addNode(host2.id(), NodeIPEndpoint(bi::address::from_string("127.0.0.1"), port2, port2)); |
|||
|
|||
for (unsigned i = 0; i < 3000 && (!web3->peerCount() || !host2.peerCount()); i += step) |
|||
this_thread::sleep_for(chrono::milliseconds(step)); |
|||
|
|||
BOOST_REQUIRE_EQUAL(host2.peerCount(), 1); |
|||
BOOST_REQUIRE_EQUAL(web3->peerCount(), 1); |
|||
|
|||
vector<PeerSessionInfo> vpeers = web3->peers(); |
|||
BOOST_REQUIRE(!vpeers.empty()); |
|||
PeerSessionInfo const& peer = vpeers.back(); |
|||
BOOST_REQUIRE_EQUAL(peer.id, host2.id()); |
|||
BOOST_REQUIRE_EQUAL(peer.port, port2); |
|||
BOOST_REQUIRE_EQUAL(peer.clientVersion, version2); |
|||
|
|||
web3->stopNetwork(); |
|||
|
|||
for (unsigned i = 0; i < 3000 && (web3->haveNetwork() || host2.haveNetwork()); i += step) |
|||
this_thread::sleep_for(chrono::milliseconds(step)); |
|||
|
|||
BOOST_REQUIRE(!web3->peerCount()); |
|||
BOOST_REQUIRE(!host2.peerCount()); |
|||
} |
|||
|
|||
BOOST_AUTO_TEST_CASE(send) |
|||
{ |
|||
cnote << "Testing web3 send..."; |
|||
|
|||
bool sent = false; |
|||
bool ready = false; |
|||
unsigned result = 0; |
|||
unsigned const messageCount = 10; |
|||
unsigned const step = 10; |
|||
uint16_t port2 = 30337; |
|||
|
|||
Host host2("shhrpc-host2", NetworkPreferences("127.0.0.1", port2, false)); |
|||
host2.setIdealPeerCount(1); |
|||
auto whost2 = host2.registerCapability(new WhisperHost()); |
|||
host2.start(); |
|||
web3->startNetwork(); |
|||
|
|||
std::thread listener([&]() |
|||
{ |
|||
setThreadName("listener"); |
|||
ready = true; |
|||
auto w = whost2->installWatch(BuildTopicMask("odd")); |
|||
set<unsigned> received; |
|||
for (unsigned x = 0; x < 9000 && !sent; x += step) |
|||
this_thread::sleep_for(chrono::milliseconds(step)); |
|||
|
|||
for (unsigned x = 0, last = 0; x < 100 && received.size() < messageCount; ++x) |
|||
{ |
|||
this_thread::sleep_for(chrono::milliseconds(50)); |
|||
for (auto i: whost2->checkWatch(w)) |
|||
{ |
|||
Message msg = whost2->envelope(i).open(whost2->fullTopics(w)); |
|||
last = RLP(msg.payload()).toInt<unsigned>(); |
|||
if (received.insert(last).second) |
|||
result += last; |
|||
} |
|||
} |
|||
}); |
|||
|
|||
for (unsigned i = 0; i < 2000 && (!host2.haveNetwork() || !web3->haveNetwork()); i += step) |
|||
this_thread::sleep_for(chrono::milliseconds(step)); |
|||
|
|||
BOOST_REQUIRE(host2.haveNetwork()); |
|||
BOOST_REQUIRE(web3->haveNetwork()); |
|||
|
|||
web3->requirePeer(host2.id(), NodeIPEndpoint(bi::address::from_string("127.0.0.1"), port2, port2)); |
|||
|
|||
for (unsigned i = 0; i < 3000 && (!web3->peerCount() || !host2.peerCount()); i += step) |
|||
this_thread::sleep_for(chrono::milliseconds(step)); |
|||
|
|||
BOOST_REQUIRE_EQUAL(host2.peerCount(), 1); |
|||
BOOST_REQUIRE_EQUAL(web3->peerCount(), 1); |
|||
|
|||
KeyPair us = KeyPair::create(); |
|||
for (unsigned i = 0; i < messageCount; ++i) |
|||
{ |
|||
web3->whisper()->post(us.sec(), RLPStream().append(i * i).out(), BuildTopic(i)(i % 2 ? "odd" : "even"), 777000, 1); |
|||
this_thread::sleep_for(chrono::milliseconds(50)); |
|||
} |
|||
|
|||
sent = true; |
|||
auto messages = web3->whisper()->all(); |
|||
BOOST_REQUIRE_EQUAL(messages.size(), messageCount); |
|||
|
|||
listener.join(); |
|||
BOOST_REQUIRE_EQUAL(result, 1 + 9 + 25 + 49 + 81); |
|||
} |
|||
|
|||
BOOST_AUTO_TEST_CASE(receive) |
|||
{ |
|||
cnote << "Testing web3 receive..."; |
|||
|
|||
bool sent = false; |
|||
bool ready = false; |
|||
unsigned result = 0; |
|||
unsigned const messageCount = 6; |
|||
unsigned const step = 10; |
|||
uint16_t port2 = 30338; |
|||
Host host2("shhrpc-host2", NetworkPreferences("127.0.0.1", port2, false)); |
|||
host2.setIdealPeerCount(1); |
|||
auto whost2 = host2.registerCapability(new WhisperHost()); |
|||
host2.start(); |
|||
web3->startNetwork(); |
|||
|
|||
std::thread listener([&]() |
|||
{ |
|||
setThreadName("listener"); |
|||
ready = true; |
|||
auto w = web3->whisper()->installWatch(BuildTopicMask("odd")); |
|||
|
|||
set<unsigned> received; |
|||
for (unsigned x = 0; x < 9000 && !sent; x += step) |
|||
this_thread::sleep_for(chrono::milliseconds(step)); |
|||
|
|||
for (unsigned x = 0, last = 0; x < 100 && received.size() < messageCount; ++x) |
|||
{ |
|||
this_thread::sleep_for(chrono::milliseconds(50)); |
|||
for (auto i: web3->whisper()->checkWatch(w)) |
|||
{ |
|||
Message msg = web3->whisper()->envelope(i).open(web3->whisper()->fullTopics(w)); |
|||
last = RLP(msg.payload()).toInt<unsigned>(); |
|||
if (received.insert(last).second) |
|||
result += last; |
|||
} |
|||
} |
|||
}); |
|||
|
|||
for (unsigned i = 0; i < 2000 && (!host2.haveNetwork() || !web3->haveNetwork()); i += step) |
|||
this_thread::sleep_for(chrono::milliseconds(step)); |
|||
|
|||
BOOST_REQUIRE(host2.haveNetwork()); |
|||
BOOST_REQUIRE(web3->haveNetwork()); |
|||
|
|||
host2.addNode(web3->id(), NodeIPEndpoint(bi::address::from_string("127.0.0.1"), web3port, web3port)); |
|||
|
|||
for (unsigned i = 0; i < 3000 && (!web3->peerCount() || !host2.peerCount()); i += step) |
|||
this_thread::sleep_for(chrono::milliseconds(step)); |
|||
|
|||
BOOST_REQUIRE_EQUAL(host2.peerCount(), 1); |
|||
BOOST_REQUIRE_EQUAL(web3->peerCount(), 1); |
|||
|
|||
KeyPair us = KeyPair::create(); |
|||
for (unsigned i = 0; i < messageCount; ++i) |
|||
{ |
|||
web3->whisper()->post(us.sec(), RLPStream().append(i * i * i).out(), BuildTopic(i)(i % 2 ? "odd" : "even"), 777000, 1); |
|||
this_thread::sleep_for(chrono::milliseconds(50)); |
|||
} |
|||
|
|||
sent = true; |
|||
listener.join(); |
|||
BOOST_REQUIRE_EQUAL(result, 1 + 27 + 125); |
|||
} |
|||
|
|||
BOOST_AUTO_TEST_SUITE_END() |
Loading…
Reference in new issue