Browse Source

Tests working for BasicAuthority and Ethash.

cl-refactor
Gav Wood 10 years ago
parent
commit
4302afdf2c
  1. 32
      CMakeLists.txt
  2. 20
      exp/CMakeLists.txt
  3. 40
      exp/main.cpp
  4. 1
      libdevcore/Common.h
  5. 18
      libethcore/BasicAuthority.cpp
  6. 7
      libethcore/BasicAuthority.h
  7. 2
      libethcore/BlockInfo.h
  8. 3
      libethcore/Ethash.cpp
  9. 1
      libethcore/Ethash.h
  10. 23
      libethcore/EthashAux.cpp
  11. 2
      libethcore/EthashAux.h
  12. 12
      libethcore/Sealer.h

32
CMakeLists.txt

@ -400,13 +400,13 @@ if (TOOLS)
endif () endif ()
if (JSONRPC AND GENERAL) if (JSONRPC AND GENERAL)
add_subdirectory(libweb3jsonrpc) # add_subdirectory(libweb3jsonrpc)
endif () endif ()
if (JSCONSOLE) if (JSCONSOLE)
add_subdirectory(libjsengine) # add_subdirectory(libjsengine)
add_subdirectory(libjsconsole) # add_subdirectory(libjsconsole)
add_subdirectory(ethconsole) # add_subdirectory(ethconsole)
endif () endif ()
if (NOT WIN32) if (NOT WIN32)
@ -432,33 +432,33 @@ endif ()
add_subdirectory(libethcore) add_subdirectory(libethcore)
if (GENERAL) if (GENERAL)
add_subdirectory(libevm) # add_subdirectory(libevm)
add_subdirectory(libethereum) # add_subdirectory(libethereum)
add_subdirectory(libwebthree) # add_subdirectory(libwebthree)
endif () endif ()
if (MINER OR TOOLS) if (MINER OR TOOLS)
add_subdirectory(ethminer) # add_subdirectory(ethminer)
endif () endif ()
if (ETHKEY OR TOOLS) if (ETHKEY OR TOOLS)
add_subdirectory(ethkey) # add_subdirectory(ethkey)
endif () endif ()
if (TESTS) if (TESTS)
add_subdirectory(libtestutils) # add_subdirectory(libtestutils)
add_subdirectory(test) # add_subdirectory(test)
if (JSONRPC) if (JSONRPC)
add_subdirectory(ethrpctest) # add_subdirectory(ethrpctest)
endif () endif ()
endif () endif ()
if (TOOLS) if (TOOLS)
add_subdirectory(rlp) # add_subdirectory(rlp)
add_subdirectory(abi) # add_subdirectory(abi)
add_subdirectory(ethvm) # add_subdirectory(ethvm)
add_subdirectory(eth) # add_subdirectory(eth)
if("x${CMAKE_BUILD_TYPE}" STREQUAL "xDebug") if("x${CMAKE_BUILD_TYPE}" STREQUAL "xDebug")
add_subdirectory(exp) add_subdirectory(exp)

20
exp/CMakeLists.txt

@ -21,15 +21,21 @@ if (READLINE_FOUND)
endif() endif()
if (JSONRPC) if (JSONRPC)
target_link_libraries(${EXECUTABLE} web3jsonrpc) # target_link_libraries(${EXECUTABLE} web3jsonrpc)
endif() endif()
target_link_libraries(${EXECUTABLE} webthree) #target_link_libraries(${EXECUTABLE} webthree)
target_link_libraries(${EXECUTABLE} ethereum) #target_link_libraries(${EXECUTABLE} ethereum)
target_link_libraries(${EXECUTABLE} p2p) #target_link_libraries(${EXECUTABLE} p2p)
if (ETHASHCL) if (ETHASHCL)
target_link_libraries(${EXECUTABLE} ethash-cl) # target_link_libraries(${EXECUTABLE} ethash-cl)
target_link_libraries(${EXECUTABLE} ethash) # target_link_libraries(${EXECUTABLE} ethash)
target_link_libraries(${EXECUTABLE} ${OpenCL_LIBRARIES}) # target_link_libraries(${EXECUTABLE} ${OpenCL_LIBRARIES})
endif() endif()
target_link_libraries(${EXECUTABLE} ethcore)
install( TARGETS ${EXECUTABLE} DESTINATION bin) install( TARGETS ${EXECUTABLE} DESTINATION bin)

40
exp/main.cpp

@ -70,6 +70,7 @@ namespace fs = boost::filesystem;
#include <libethcore/BasicAuthority.h> #include <libethcore/BasicAuthority.h>
#include <libethcore/BlockInfo.h> #include <libethcore/BlockInfo.h>
#include <libethcore/Ethash.h> #include <libethcore/Ethash.h>
#include <libethcore/Params.h>
using namespace std; using namespace std;
using namespace dev; using namespace dev;
using namespace eth; using namespace eth;
@ -80,24 +81,43 @@ using namespace eth;
int main() int main()
{ {
BlockInfo bi; BlockInfo bi;
bi.difficulty = c_genesisDifficulty;
bi.gasLimit = c_genesisGasLimit;
bi.number = 1;
bi.parentHash = sha3("parentHash");
bytes sealedData; bytes sealedData;
SealEngineFace* se = BasicAuthority::createSealEngine();
cdebug << se->sealers();
se->onSealGenerated([&](SealFace const* seal){ sealedData = seal->sealedHeader(bi); });
se->generateSeal(bi);
{ {
KeyPair kp(sha3("test"));
SealEngineFace* se = BasicAuthority::createSealEngine();
se->setOption("authority", rlp(kp.secret()));
se->setOption("authorities", rlpList(kp.address()));
cdebug << se->sealers();
bool done = false;
se->onSealGenerated([&](SealFace const* seal){
sealedData = seal->sealedHeader(bi);
done = true;
});
se->generateSeal(bi);
while (!done)
this_thread::sleep_for(chrono::milliseconds(50));
BasicAuthority::BlockHeader sealed = BasicAuthority::BlockHeader::fromHeader(sealedData, CheckEverything); BasicAuthority::BlockHeader sealed = BasicAuthority::BlockHeader::fromHeader(sealedData, CheckEverything);
cdebug << sealed.sig(); cdebug << sealed.sig();
} }
SealEngineFace* se = Ethash::createSealEngine();
cdebug << se->sealers();
se->onSealGenerated([&](SealFace const* seal){ sealedData = seal->sealedHeader(bi); done = true; });
se->generateSeal(bi);
while (!done)
this_thread::sleep_for(chrono::milliseconds(50));
{ {
SealEngineFace* se = Ethash::createSealEngine();
cdebug << se->sealers();
bool done = false;
se->setSealer("cpu");
se->onSealGenerated([&](SealFace const* seal){
sealedData = seal->sealedHeader(bi);
done = true;
});
se->generateSeal(bi);
while (!done)
this_thread::sleep_for(chrono::milliseconds(50));
Ethash::BlockHeader sealed = Ethash::BlockHeader::fromHeader(sealedData, CheckEverything); Ethash::BlockHeader sealed = Ethash::BlockHeader::fromHeader(sealedData, CheckEverything);
cdebug << sealed.nonce(); cdebug << sealed.nonce();
} }

1
libdevcore/Common.h

@ -65,6 +65,7 @@ using byte = uint8_t;
#define DEV_IGNORE_EXCEPTIONS(X) try { X; } catch (...) {} #define DEV_IGNORE_EXCEPTIONS(X) try { X; } catch (...) {}
#define DEV_IF_NO_ELSE(X) if(!(X)){}else #define DEV_IF_NO_ELSE(X) if(!(X)){}else
#define DEV_IF_THROWS(X) try{X;}catch(...)
namespace dev namespace dev
{ {

18
libethcore/BasicAuthority.cpp

@ -26,11 +26,11 @@ using namespace std;
using namespace dev; using namespace dev;
using namespace eth; using namespace eth;
const Address BasicAuthority::Authority = Address("1234567890123456789012345678901234567890"); AddressHash BasicAuthority::s_authorities;
bool BasicAuthority::BlockHeaderRaw::verify() const bool BasicAuthority::BlockHeaderRaw::verify() const
{ {
return toAddress(recover(m_sig, hashWithout())) == Authority; return s_authorities.count(toAddress(recover(m_sig, hashWithout())));
} }
bool BasicAuthority::BlockHeaderRaw::preVerify() const bool BasicAuthority::BlockHeaderRaw::preVerify() const
@ -94,11 +94,23 @@ public:
MiningProgress miningProgress() const { return MiningProgress(); } MiningProgress miningProgress() const { return MiningProgress(); }
private: private:
virtual bool onOptionChanging(std::string const& _name, bytes const& _value)
{
RLP rlp(_value);
if (_name == "authorities")
BasicAuthority::s_authorities = rlp.toUnorderedSet<Address>();
else if (_name == "authority")
m_secret = rlp.toHash<Secret>();
else
return false;
return true;
}
Secret m_secret; Secret m_secret;
std::function<void(SealFace const* s)> m_onSealGenerated; std::function<void(SealFace const* s)> m_onSealGenerated;
}; };
SealEngineFace* createSealEngine() SealEngineFace* BasicAuthority::createSealEngine()
{ {
return new BasicAuthoritySealEngine; return new BasicAuthoritySealEngine;
} }

7
libethcore/BasicAuthority.h

@ -30,6 +30,7 @@
#include "Sealer.h" #include "Sealer.h"
class BasicAuthoritySeal; class BasicAuthoritySeal;
class BasicAuthoritySealEngine;
namespace dev namespace dev
{ {
@ -48,6 +49,8 @@ namespace eth
*/ */
class BasicAuthority class BasicAuthority
{ {
friend class ::BasicAuthoritySealEngine;
public: public:
// TODO: remove // TODO: remove
struct Result {}; struct Result {};
@ -70,6 +73,7 @@ public:
Signature sig() const { return m_sig; } Signature sig() const { return m_sig; }
protected: protected:
BlockHeaderRaw() = default;
BlockHeaderRaw(BlockInfo const& _bi): BlockInfo(_bi) {} BlockHeaderRaw(BlockInfo const& _bi): BlockInfo(_bi) {}
static const unsigned SealFields = 1; static const unsigned SealFields = 1;
@ -84,7 +88,8 @@ public:
}; };
using BlockHeader = BlockHeaderPolished<BlockHeaderRaw>; using BlockHeader = BlockHeaderPolished<BlockHeaderRaw>;
static const Address Authority; private:
static AddressHash s_authorities;
}; };
} }

2
libethcore/BlockInfo.h

@ -158,7 +158,7 @@ public:
explicit BlockHeaderPolished(bytesConstRef _block, Strictness _s = IgnoreNonce, h256 const& _h = h256()) { populate(_block, _s, _h); } explicit BlockHeaderPolished(bytesConstRef _block, Strictness _s = IgnoreNonce, h256 const& _h = h256()) { populate(_block, _s, _h); }
static BlockHeaderPolished fromHeader(bytes const& _header, Strictness _s = IgnoreNonce, h256 const& _h = h256()) { return fromHeader(bytesConstRef(&_header), _s, _h); } static BlockHeaderPolished fromHeader(bytes const& _header, Strictness _s = IgnoreNonce, h256 const& _h = h256()) { return fromHeader(bytesConstRef(&_header), _s, _h); }
static BlockHeaderPolished fromHeader(bytesConstRef _header, Strictness _s = IgnoreNonce, h256 const& _h = h256()) { BlockHeaderPolished ret; ret.populateFromHeader(_header, _s, _h); return ret; } static BlockHeaderPolished fromHeader(bytesConstRef _header, Strictness _s = IgnoreNonce, h256 const& _h = h256()) { BlockHeaderPolished ret; ret.populateFromHeader(RLP(_header), _s, _h); return ret; }
void populate(bytesConstRef _block, Strictness _s, h256 const& _h = h256()) { populateFromHeader(BlockInfo::extractHeader(_block), _s, _h); } void populate(bytesConstRef _block, Strictness _s, h256 const& _h = h256()) { populateFromHeader(BlockInfo::extractHeader(_block), _s, _h); }

3
libethcore/Ethash.cpp

@ -119,7 +119,7 @@ bool Ethash::BlockHeaderRaw::verify() const
} }
#endif #endif
auto result = EthashAux::eval(*this); auto result = EthashAux::eval(seedHash(), hashWithout(), m_nonce);
bool slow = result.value <= boundary() && result.mixHash == m_mixHash; bool slow = result.value <= boundary() && result.mixHash == m_mixHash;
// cdebug << (slow ? "VERIFY" : "VERYBAD"); // cdebug << (slow ? "VERIFY" : "VERYBAD");
@ -281,6 +281,7 @@ public:
{ {
m_farm.onSolutionFound([=](Ethash::Solution const& sol) m_farm.onSolutionFound([=](Ethash::Solution const& sol)
{ {
cdebug << m_farm.work().seedHash << m_farm.work().headerHash << sol.nonce << EthashAux::eval(m_farm.work().seedHash, m_farm.work().headerHash, sol.nonce).value;
EthashSeal s(sol.mixHash, sol.nonce); EthashSeal s(sol.mixHash, sol.nonce);
_f(&s); _f(&s);
return true; return true;

1
libethcore/Ethash.h

@ -94,6 +94,7 @@ public:
h256 const& mixHash() const { return m_mixHash; } h256 const& mixHash() const { return m_mixHash; }
protected: protected:
BlockHeaderRaw() = default;
BlockHeaderRaw(BlockInfo const& _bi): BlockInfo(_bi) {} BlockHeaderRaw(BlockInfo const& _bi): BlockInfo(_bi) {}
static const unsigned SealFields = 2; static const unsigned SealFields = 2;

23
libethcore/EthashAux.cpp

@ -200,18 +200,6 @@ EthashAux::FullType EthashAux::full(h256 const& _seedHash, bool _createIfMissing
return ret; return ret;
} }
Ethash::Result EthashAux::eval(BlockInfo const& _header)
{
#if ETH_USING_ETHASH
return eval(_header, _header.proof.nonce);
#else
(void)_header;
return Ethash::Result();
#endif
}
#define DEV_IF_THROWS(X) try { X; } catch (...)
unsigned EthashAux::computeFull(h256 const& _seedHash, bool _createIfMissing) unsigned EthashAux::computeFull(h256 const& _seedHash, bool _createIfMissing)
{ {
Guard l(get()->x_fulls); Guard l(get()->x_fulls);
@ -260,17 +248,6 @@ Ethash::Result EthashAux::LightAllocation::compute(h256 const& _headerHash, Nonc
return Ethash::Result{h256((uint8_t*)&r.result, h256::ConstructFromPointer), h256((uint8_t*)&r.mix_hash, h256::ConstructFromPointer)}; return Ethash::Result{h256((uint8_t*)&r.result, h256::ConstructFromPointer), h256((uint8_t*)&r.mix_hash, h256::ConstructFromPointer)};
} }
Ethash::Result EthashAux::eval(BlockInfo const& _header, Nonce const& _nonce)
{
#if ETH_USING_ETHASH
return eval(_header.proofCache(), _header.headerHash(WithoutProof), _nonce);
#else
(void)_header;
(void)_nonce;
return Ethash::Result();
#endif
}
Ethash::Result EthashAux::eval(h256 const& _seedHash, h256 const& _headerHash, Nonce const& _nonce) Ethash::Result EthashAux::eval(h256 const& _seedHash, h256 const& _headerHash, Nonce const& _nonce)
{ {
DEV_GUARDED(get()->x_fulls) DEV_GUARDED(get()->x_fulls)

2
libethcore/EthashAux.h

@ -79,8 +79,6 @@ public:
/// Kicks off generation of DAG for @a _blocknumber and blocks until ready; @returns result or empty pointer if not existing and _createIfMissing is false. /// Kicks off generation of DAG for @a _blocknumber and blocks until ready; @returns result or empty pointer if not existing and _createIfMissing is false.
static FullType full(h256 const& _seedHash, bool _createIfMissing = false, std::function<int(unsigned)> const& _f = std::function<int(unsigned)>()); static FullType full(h256 const& _seedHash, bool _createIfMissing = false, std::function<int(unsigned)> const& _f = std::function<int(unsigned)>());
static Ethash::Result eval(BlockInfo const& _header);
static Ethash::Result eval(BlockInfo const& _header, Nonce const& _nonce);
static Ethash::Result eval(h256 const& _seedHash, h256 const& _headerHash, Nonce const& _nonce); static Ethash::Result eval(h256 const& _seedHash, h256 const& _headerHash, Nonce const& _nonce);
private: private:

12
libethcore/Sealer.h

@ -24,6 +24,7 @@
#pragma once #pragma once
#include <functional> #include <functional>
#include <libdevcore/Guards.h>
#include "Common.h" #include "Common.h"
namespace dev namespace dev
@ -43,6 +44,9 @@ public:
class SealEngineFace class SealEngineFace
{ {
public: public:
bytes option(std::string const& _name) const { Guard l(x_options); return m_options.count(_name) ? m_options.at(_name) : bytes(); }
bool setOption(std::string const& _name, bytes const& _value) { Guard l(x_options); try { if (onOptionChanging(_name, _value)) { m_options[_name] = _value; return true; } } catch (...) {} return false; }
virtual strings sealers() const { return { "default" }; } virtual strings sealers() const { return { "default" }; }
virtual void setSealer(std::string const&) {} virtual void setSealer(std::string const&) {}
virtual void generateSeal(BlockInfo const& _bi) = 0; virtual void generateSeal(BlockInfo const& _bi) = 0;
@ -52,6 +56,14 @@ public:
// TODO: rename & generalise // TODO: rename & generalise
virtual bool isMining() const { return false; } virtual bool isMining() const { return false; }
virtual MiningProgress miningProgress() const { return MiningProgress(); } virtual MiningProgress miningProgress() const { return MiningProgress(); }
protected:
virtual bool onOptionChanging(std::string const&, bytes const&) { return true; }
void injectOption(std::string const& _name, bytes const& _value) { Guard l(x_options); m_options[_name] = _value; }
private:
mutable Mutex x_options;
std::unordered_map<std::string, bytes> m_options;
}; };
} }

Loading…
Cancel
Save