Gav Wood
10 years ago
15 changed files with 746 additions and 471 deletions
@ -0,0 +1,104 @@ |
|||||
|
/*
|
||||
|
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 BasicAuthority.cpp
|
||||
|
* @author Gav Wood <i@gavwood.com> |
||||
|
* @date 2014 |
||||
|
*/ |
||||
|
|
||||
|
#include "Exceptions.h" |
||||
|
#include "BasicAuthority.h" |
||||
|
#include "BlockInfo.h" |
||||
|
using namespace std; |
||||
|
using namespace dev; |
||||
|
using namespace eth; |
||||
|
|
||||
|
const Address BasicAuthority::Authority = Address("1234567890123456789012345678901234567890"); |
||||
|
|
||||
|
bool BasicAuthority::BlockHeaderRaw::verify() const |
||||
|
{ |
||||
|
return toAddress(recover(m_sig, hashWithout())) == Authority; |
||||
|
} |
||||
|
|
||||
|
bool BasicAuthority::BlockHeaderRaw::preVerify() const |
||||
|
{ |
||||
|
return SignatureStruct(m_sig).isValid(); |
||||
|
} |
||||
|
|
||||
|
void BasicAuthority::BlockHeaderRaw::populateFromHeader(RLP const& _header, Strictness _s) |
||||
|
{ |
||||
|
m_sig = _header[BlockInfo::BasicFields].toHash<Signature>(); |
||||
|
|
||||
|
// check it hashes according to proof of work or that it's the genesis block.
|
||||
|
if (_s == CheckEverything && parentHash && !verify()) |
||||
|
{ |
||||
|
InvalidBlockNonce ex; |
||||
|
ex << errinfo_hash256(hashWithout()); |
||||
|
ex << errinfo_difficulty(difficulty); |
||||
|
ex << errinfo_target(boundary()); |
||||
|
BOOST_THROW_EXCEPTION(ex); |
||||
|
} |
||||
|
else if (_s == QuickNonce && parentHash && !preVerify()) |
||||
|
{ |
||||
|
InvalidBlockNonce ex; |
||||
|
ex << errinfo_hash256(hashWithout()); |
||||
|
ex << errinfo_difficulty(difficulty); |
||||
|
BOOST_THROW_EXCEPTION(ex); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
|
||||
|
|
||||
|
class BasicAuthoritySeal: public SealFace |
||||
|
{ |
||||
|
public: |
||||
|
BasicAuthoritySeal(Signature const& _sig): m_sig(_sig) {} |
||||
|
|
||||
|
virtual bytes sealedHeader(BlockInfo const& _bi) const |
||||
|
{ |
||||
|
BasicAuthority::BlockHeader h(_bi); |
||||
|
h.m_sig = m_sig; |
||||
|
RLPStream ret; |
||||
|
h.streamRLP(ret); |
||||
|
return ret.out(); |
||||
|
} |
||||
|
|
||||
|
private: |
||||
|
Signature m_sig; |
||||
|
}; |
||||
|
|
||||
|
class BasicAuthoritySealEngine: public SealEngineFace |
||||
|
{ |
||||
|
public: |
||||
|
void setSecret(Secret const& _s) { m_secret = _s; } |
||||
|
void generateSeal(BlockInfo const& _bi) |
||||
|
{ |
||||
|
BasicAuthoritySeal s(sign(m_secret, _bi.hashWithout())); |
||||
|
m_onSealGenerated(&s); |
||||
|
} |
||||
|
void onSealGenerated(std::function<void(SealFace const* s)> const& _f) { m_onSealGenerated = _f; } |
||||
|
bool isMining() const { return false; } |
||||
|
MiningProgress miningProgress() const { return MiningProgress(); } |
||||
|
|
||||
|
private: |
||||
|
Secret m_secret; |
||||
|
std::function<void(SealFace const* s)> m_onSealGenerated; |
||||
|
}; |
||||
|
|
||||
|
SealEngineFace* createSealEngine() |
||||
|
{ |
||||
|
return new BasicAuthoritySealEngine; |
||||
|
} |
@ -0,0 +1,91 @@ |
|||||
|
/*
|
||||
|
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 BasicAuthority.h
|
||||
|
* @author Gav Wood <i@gavwood.com> |
||||
|
* @date 2014 |
||||
|
* |
||||
|
* Determines the PoW algorithm. |
||||
|
*/ |
||||
|
|
||||
|
#pragma once |
||||
|
|
||||
|
#include <libdevcore/RLP.h> |
||||
|
#include <libdevcrypto/Common.h> |
||||
|
#include "BlockInfo.h" |
||||
|
#include "Common.h" |
||||
|
#include "Sealer.h" |
||||
|
|
||||
|
class BasicAuthoritySeal; |
||||
|
|
||||
|
namespace dev |
||||
|
{ |
||||
|
namespace eth |
||||
|
{ |
||||
|
|
||||
|
/**
|
||||
|
* The proof of work algorithm base type. |
||||
|
* |
||||
|
* Must implement a basic templated interface, including: |
||||
|
* typename Result |
||||
|
* typename Solution |
||||
|
* typename CPUMiner |
||||
|
* typename GPUMiner |
||||
|
* and a few others. TODO |
||||
|
*/ |
||||
|
class BasicAuthority |
||||
|
{ |
||||
|
public: |
||||
|
// TODO: remove
|
||||
|
struct Result {}; |
||||
|
struct WorkPackage {}; |
||||
|
static const WorkPackage NullWorkPackage; |
||||
|
|
||||
|
static std::string name() { return "BasicAuthority"; } |
||||
|
static unsigned revision() { return 0; } |
||||
|
static SealEngineFace* createSealEngine(); |
||||
|
|
||||
|
class BlockHeaderRaw: public BlockInfo |
||||
|
{ |
||||
|
friend class ::BasicAuthoritySeal; |
||||
|
|
||||
|
public: |
||||
|
bool verify() const; |
||||
|
bool preVerify() const; |
||||
|
|
||||
|
WorkPackage package() const { return NullWorkPackage; } |
||||
|
Signature sig() const { return m_sig; } |
||||
|
|
||||
|
protected: |
||||
|
BlockHeaderRaw(BlockInfo const& _bi): BlockInfo(_bi) {} |
||||
|
|
||||
|
static const unsigned SealFields = 1; |
||||
|
|
||||
|
void populateFromHeader(RLP const& _header, Strictness _s); |
||||
|
void streamRLPFields(RLPStream& _s) const { _s << m_sig; } |
||||
|
void clear() { m_sig = Signature(); } |
||||
|
void noteDirty() const {} |
||||
|
|
||||
|
private: |
||||
|
Signature m_sig; |
||||
|
}; |
||||
|
using BlockHeader = BlockHeaderPolished<BlockHeaderRaw>; |
||||
|
|
||||
|
static const Address Authority; |
||||
|
}; |
||||
|
|
||||
|
} |
||||
|
} |
@ -0,0 +1,26 @@ |
|||||
|
/*
|
||||
|
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 Sealer.cpp
|
||||
|
* @author Gav Wood <i@gavwood.com> |
||||
|
* @date 2014 |
||||
|
*/ |
||||
|
|
||||
|
#include "Sealer.h" |
||||
|
using namespace std; |
||||
|
using namespace dev; |
||||
|
using namespace eth; |
||||
|
|
@ -0,0 +1,58 @@ |
|||||
|
/*
|
||||
|
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 Sealer.h
|
||||
|
* @author Gav Wood <i@gavwood.com> |
||||
|
* @date 2014 |
||||
|
* |
||||
|
* Determines the PoW algorithm. |
||||
|
*/ |
||||
|
|
||||
|
#pragma once |
||||
|
|
||||
|
#include <functional> |
||||
|
#include "Common.h" |
||||
|
|
||||
|
namespace dev |
||||
|
{ |
||||
|
namespace eth |
||||
|
{ |
||||
|
|
||||
|
class BlockInfo; |
||||
|
|
||||
|
class SealFace |
||||
|
{ |
||||
|
public: |
||||
|
virtual bool wouldSealHeader(BlockInfo const&) const { return true; } |
||||
|
virtual bytes sealedHeader(BlockInfo const& _bi) const = 0; |
||||
|
}; |
||||
|
|
||||
|
class SealEngineFace |
||||
|
{ |
||||
|
public: |
||||
|
virtual strings sealers() const { return { "default" }; } |
||||
|
virtual void setSealer(std::string const&) {} |
||||
|
virtual void generateSeal(BlockInfo const& _bi) = 0; |
||||
|
virtual void onSealGenerated(std::function<void(SealFace const* s)> const& _f) = 0; |
||||
|
virtual void disable() {} |
||||
|
|
||||
|
// TODO: rename & generalise
|
||||
|
virtual bool isMining() const { return false; } |
||||
|
virtual MiningProgress miningProgress() const { return MiningProgress(); } |
||||
|
}; |
||||
|
|
||||
|
} |
||||
|
} |
Loading…
Reference in new issue