Browse Source

Merge pull request #1579 from arkpar/core_pow

Allow mining without POW check
cl-refactor
Gav Wood 10 years ago
parent
commit
c5aaabd9a9
  1. 11
      libethcore/Common.h
  2. 12
      libethereum/BlockChain.cpp
  3. 10
      libethereum/BlockChain.h
  4. 14
      libethereum/State.cpp
  5. 6
      libethereum/State.h
  6. 6
      mix/MixClient.cpp
  7. 1
      mix/MixClient.h

11
libethcore/Common.h

@ -93,6 +93,17 @@ enum class ImportResult
BadChain BadChain
}; };
struct ImportRequirements
{
using value = unsigned;
enum
{
ValidNonce = 1, ///< Validate Nonce
DontHave = 2, ///< Avoid old blocks
Default = ValidNonce | DontHave
};
};
/// Super-duper signal mechanism. TODO: replace with somthing a bit heavier weight. /// Super-duper signal mechanism. TODO: replace with somthing a bit heavier weight.
class Signal class Signal
{ {

12
libethereum/BlockChain.cpp

@ -238,7 +238,7 @@ void BlockChain::rebuild(std::string const& _path, std::function<void(unsigned,
return; return;
} }
lastHash = bi.hash(); lastHash = bi.hash();
import(b, s.db(), Aversion::ImportOldBlocks); import(b, s.db(), ImportRequirements::Default);
} }
catch (...) catch (...)
{ {
@ -326,11 +326,11 @@ tuple<h256s, h256s, bool> BlockChain::sync(BlockQueue& _bq, OverlayDB const& _st
return make_tuple(fresh, dead, _bq.doneDrain(badBlocks)); return make_tuple(fresh, dead, _bq.doneDrain(badBlocks));
} }
ImportRoute BlockChain::attemptImport(bytes const& _block, OverlayDB const& _stateDB, Aversion _force) noexcept ImportRoute BlockChain::attemptImport(bytes const& _block, OverlayDB const& _stateDB, ImportRequirements::value _ir) noexcept
{ {
try try
{ {
return import(_block, _stateDB, _force); return import(_block, _stateDB, _ir);
} }
catch (...) catch (...)
{ {
@ -339,7 +339,7 @@ ImportRoute BlockChain::attemptImport(bytes const& _block, OverlayDB const& _sta
} }
} }
ImportRoute BlockChain::import(bytes const& _block, OverlayDB const& _db, Aversion _force) ImportRoute BlockChain::import(bytes const& _block, OverlayDB const& _db, ImportRequirements::value _ir)
{ {
//@tidy This is a behemoth of a method - could do to be split into a few smaller ones. //@tidy This is a behemoth of a method - could do to be split into a few smaller ones.
@ -378,7 +378,7 @@ ImportRoute BlockChain::import(bytes const& _block, OverlayDB const& _db, Aversi
#endif #endif
// Check block doesn't already exist first! // Check block doesn't already exist first!
if (isKnown(bi.hash()) && _force == Aversion::AvoidOldBlocks) if (isKnown(bi.hash()) && (_ir & ImportRequirements::DontHave))
{ {
clog(BlockChainNote) << bi.hash() << ": Not new."; clog(BlockChainNote) << bi.hash() << ": Not new.";
BOOST_THROW_EXCEPTION(AlreadyHaveBlock()); BOOST_THROW_EXCEPTION(AlreadyHaveBlock());
@ -424,7 +424,7 @@ ImportRoute BlockChain::import(bytes const& _block, OverlayDB const& _db, Aversi
// Check transactions are valid and that they result in a state equivalent to our state_root. // Check transactions are valid and that they result in a state equivalent to our state_root.
// Get total difficulty increase and update state, checking it. // Get total difficulty increase and update state, checking it.
State s(_db); //, bi.coinbaseAddress State s(_db); //, bi.coinbaseAddress
auto tdIncrease = s.enactOn(&_block, bi, *this); auto tdIncrease = s.enactOn(&_block, bi, *this, _ir);
BlockLogBlooms blb; BlockLogBlooms blb;
BlockReceipts br; BlockReceipts br;

10
libethereum/BlockChain.h

@ -81,12 +81,6 @@ enum {
using ProgressCallback = std::function<void(unsigned, unsigned)>; using ProgressCallback = std::function<void(unsigned, unsigned)>;
enum class Aversion
{
AvoidOldBlocks,
ImportOldBlocks
};
/** /**
* @brief Implements the blockchain database. All data this gives is disk-backed. * @brief Implements the blockchain database. All data this gives is disk-backed.
* @threadsafe * @threadsafe
@ -109,11 +103,11 @@ public:
/// Attempt to import the given block directly into the CanonBlockChain and sync with the state DB. /// Attempt to import the given block directly into the CanonBlockChain and sync with the state DB.
/// @returns the block hashes of any blocks that came into/went out of the canonical block chain. /// @returns the block hashes of any blocks that came into/went out of the canonical block chain.
ImportRoute attemptImport(bytes const& _block, OverlayDB const& _stateDB, Aversion _force = Aversion::AvoidOldBlocks) noexcept; ImportRoute attemptImport(bytes const& _block, OverlayDB const& _stateDB, ImportRequirements::value _ir = ImportRequirements::Default) noexcept;
/// Import block into disk-backed DB /// Import block into disk-backed DB
/// @returns the block hashes of any blocks that came into/went out of the canonical block chain. /// @returns the block hashes of any blocks that came into/went out of the canonical block chain.
ImportRoute import(bytes const& _block, OverlayDB const& _stateDB, Aversion _force = Aversion::AvoidOldBlocks); ImportRoute import(bytes const& _block, OverlayDB const& _stateDB, ImportRequirements::value _ir = ImportRequirements::Default);
/// Returns true if the given block is known (though not necessarily a part of the canon chain). /// Returns true if the given block is known (though not necessarily a part of the canon chain).
bool isKnown(h256 const& _hash) const; bool isKnown(h256 const& _hash) const;

14
libethereum/State.cpp

@ -274,7 +274,7 @@ bool State::sync(BlockChain const& _bc)
return sync(_bc, _bc.currentHash()); return sync(_bc, _bc.currentHash());
} }
bool State::sync(BlockChain const& _bc, h256 _block, BlockInfo const& _bi) bool State::sync(BlockChain const& _bc, h256 _block, BlockInfo const& _bi, ImportRequirements::value _ir)
{ {
bool ret = false; bool ret = false;
// BLOCK // BLOCK
@ -337,7 +337,7 @@ bool State::sync(BlockChain const& _bc, h256 _block, BlockInfo const& _bi)
for (auto it = chain.rbegin(); it != chain.rend(); ++it) for (auto it = chain.rbegin(); it != chain.rend(); ++it)
{ {
auto b = _bc.block(*it); auto b = _bc.block(*it);
enact(&b, _bc); enact(&b, _bc, _ir);
cleanup(true); cleanup(true);
} }
} }
@ -355,7 +355,7 @@ bool State::sync(BlockChain const& _bc, h256 _block, BlockInfo const& _bi)
return ret; return ret;
} }
u256 State::enactOn(bytesConstRef _block, BlockInfo const& _bi, BlockChain const& _bc) u256 State::enactOn(bytesConstRef _block, BlockInfo const& _bi, BlockChain const& _bc, ImportRequirements::value _ir)
{ {
#if ETH_TIMED_ENACTMENTS #if ETH_TIMED_ENACTMENTS
boost::timer t; boost::timer t;
@ -383,7 +383,7 @@ u256 State::enactOn(bytesConstRef _block, BlockInfo const& _bi, BlockChain const
t.restart(); t.restart();
#endif #endif
sync(_bc, _bi.parentHash); sync(_bc, _bi.parentHash, BlockInfo(), _ir);
resetCurrent(); resetCurrent();
#if ETH_TIMED_ENACTMENTS #if ETH_TIMED_ENACTMENTS
@ -392,7 +392,7 @@ u256 State::enactOn(bytesConstRef _block, BlockInfo const& _bi, BlockChain const
#endif #endif
m_previousBlock = biParent; m_previousBlock = biParent;
auto ret = enact(_block, _bc); auto ret = enact(_block, _bc, _ir);
#if ETH_TIMED_ENACTMENTS #if ETH_TIMED_ENACTMENTS
enactment = t.elapsed(); enactment = t.elapsed();
@ -538,11 +538,11 @@ TransactionReceipts State::sync(BlockChain const& _bc, TransactionQueue& _tq, Ga
return ret; return ret;
} }
u256 State::enact(bytesConstRef _block, BlockChain const& _bc, bool _checkNonce) u256 State::enact(bytesConstRef _block, BlockChain const& _bc, ImportRequirements::value _ir)
{ {
// m_currentBlock is assumed to be prepopulated and reset. // m_currentBlock is assumed to be prepopulated and reset.
BlockInfo bi(_block, _checkNonce ? CheckEverything : IgnoreNonce); BlockInfo bi(_block, (_ir & ImportRequirements::ValidNonce) ? CheckEverything : IgnoreNonce);
#if !ETH_RELEASE #if !ETH_RELEASE
assert(m_previousBlock.hash() == bi.parentHash); assert(m_previousBlock.hash() == bi.parentHash);

6
libethereum/State.h

@ -314,11 +314,11 @@ public:
bool sync(BlockChain const& _bc); bool sync(BlockChain const& _bc);
/// Sync with the block chain, but rather than synching to the latest block, instead sync to the given block. /// 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 _blockHash, BlockInfo const& _bi = BlockInfo()); bool sync(BlockChain const& _bc, h256 _blockHash, BlockInfo const& _bi = BlockInfo(), ImportRequirements::value _ir = ImportRequirements::Default);
/// Execute all transactions within a given block. /// Execute all transactions within a given block.
/// @returns the additional total difficulty. /// @returns the additional total difficulty.
u256 enactOn(bytesConstRef _block, BlockInfo const& _bi, BlockChain const& _bc); u256 enactOn(bytesConstRef _block, BlockInfo const& _bi, BlockChain const& _bc, ImportRequirements::value _ir = ImportRequirements::Default);
/// Returns back to a pristine state after having done a playback. /// 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 /// @arg _fullCommit if true flush everything out to disk. If false, this effectively only validates
@ -346,7 +346,7 @@ private:
/// Execute the given block, assuming it corresponds to m_currentBlock. /// Execute the given block, assuming it corresponds to m_currentBlock.
/// Throws on failure. /// Throws on failure.
u256 enact(bytesConstRef _block, BlockChain const& _bc, bool _checkNonce = true); u256 enact(bytesConstRef _block, BlockChain const& _bc, ImportRequirements::value _ir = ImportRequirements::Default);
/// Finalise the block, applying the earned rewards. /// Finalise the block, applying the earned rewards.
void applyRewards(std::vector<BlockInfo> const& _uncleBlockHeaders); void applyRewards(std::vector<BlockInfo> const& _uncleBlockHeaders);

6
mix/MixClient.cpp

@ -55,7 +55,7 @@ bytes MixBlockChain::createGenesisBlock(h256 _stateRoot)
} }
MixClient::MixClient(std::string const& _dbPath): MixClient::MixClient(std::string const& _dbPath):
m_dbPath(_dbPath), m_miningThreads(0) m_dbPath(_dbPath)
{ {
std::map<Secret, u256> account; std::map<Secret, u256> account;
account.insert(std::make_pair(c_defaultUserAccountSecret, 1000000 * ether)); account.insert(std::make_pair(c_defaultUserAccountSecret, 1000000 * ether));
@ -249,6 +249,9 @@ void MixClient::mine()
{ {
WriteGuard l(x_state); WriteGuard l(x_state);
m_state.commitToMine(bc()); m_state.commitToMine(bc());
m_state.completeMine();
bc().import(m_state.blockData(), m_stateDB, ImportRequirements::Default & ~ImportRequirements::ValidNonce);
/*
GenericFarm<ProofOfWork> f; GenericFarm<ProofOfWork> f;
bool completed = false; bool completed = false;
f.onSolutionFound([&](ProofOfWork::Solution sol) f.onSolutionFound([&](ProofOfWork::Solution sol)
@ -261,6 +264,7 @@ void MixClient::mine()
this_thread::sleep_for(chrono::milliseconds(20)); this_thread::sleep_for(chrono::milliseconds(20));
bc().import(m_state.blockData(), m_stateDB); bc().import(m_state.blockData(), m_stateDB);
*/
m_state.sync(bc()); m_state.sync(bc());
m_startState = m_state; m_startState = m_state;
h256Set changed { dev::eth::PendingChangedFilter, dev::eth::ChainChangedFilter }; h256Set changed { dev::eth::PendingChangedFilter, dev::eth::ChainChangedFilter };

1
mix/MixClient.h

@ -101,7 +101,6 @@ private:
mutable boost::shared_mutex x_executions; mutable boost::shared_mutex x_executions;
ExecutionResults m_executions; ExecutionResults m_executions;
std::string m_dbPath; std::string m_dbPath;
unsigned m_miningThreads;
}; };
} }

Loading…
Cancel
Save