Fabcoin Core  0.16.2
P2P Digital Currency
BasicAuthority.cpp
Go to the documentation of this file.
1 /*
2  This file is part of cpp-ethereum.
3 
4  cpp-ethereum is free software: you can redistribute it and/or modify
5  it under the terms of the GNU General Public License as published by
6  the Free Software Foundation, either version 3 of the License, or
7  (at your option) any later version.
8 
9  cpp-ethereum is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  GNU General Public License for more details.
13 
14  You should have received a copy of the GNU General Public License
15  along with cpp-ethereum. If not, see <http://www.gnu.org/licenses/>.
16 */
22 #include "BasicAuthority.h"
23 #include <libdevcore/CommonJS.h>
24 #include <libdevcore/Log.h>
25 #include <libethereum/Interface.h>
26 #include "Exceptions.h"
27 #include "BlockHeader.h"
28 using namespace std;
29 using namespace dev;
30 using namespace eth;
31 
32 
33 void BasicAuthority::init()
34 {
36 }
37 
38 StringHashMap BasicAuthority::jsInfo(BlockHeader const& _bi) const
39 {
40  return { { "sig", toJS(sig(_bi)) } };
41 }
42 
43 bool BasicAuthority::shouldSeal(Interface* _i)
44 {
45 // cdebug << "Comparing: " << _i->pendingInfo().timestamp() << " to " << utcTime();
46  return _i->pendingInfo().timestamp() + 5 <= utcTime() || (_i->pendingInfo().timestamp() <= utcTime() && !_i->pending().empty());
47 }
48 
49 void BasicAuthority::generateSeal(BlockHeader const& _bi)
50 {
51  BlockHeader bi = _bi;
52  h256 h = bi.hash(WithoutSeal);
53  Signature s = sign(m_secret, h);
54  setSig(bi, s);
55  SealEngineBase::generateSeal(bi);
56 }
57 
58 bool BasicAuthority::onOptionChanging(std::string const& _name, bytes const& _value)
59 {
60  RLP rlp(_value);
61  if (_name == "authorities")
62  m_authorities = rlp.toUnorderedSet<Address>();
63  else if (_name == "authority")
64  m_secret = Secret(rlp.toHash<h256>());
65  else
66  return false;
67  return true;
68 }
69 
70 void BasicAuthority::populateFromParent(BlockHeader& _bi, BlockHeader const& _parent) const
71 {
72  SealEngineFace::populateFromParent(_bi, _parent);
73  // pseudo-random difficulty to facilitate fork reduction.
74  _bi.setDifficulty(fromBigEndian<uint32_t>(sha3(sha3(m_secret) ^ _bi.parentHash()).ref().cropped(0, 4)));
75 }
76 
77 void BasicAuthority::verify(Strictness _s, BlockHeader const& _bi, BlockHeader const& _parent, bytesConstRef _block) const
78 {
79  SealEngineFace::verify(_s, _bi, _parent, _block);
80  // check it hashes according to proof of work or that it's the genesis block.
81  Signature s = sig(_bi);
82  h256 h = _bi.hash(WithoutSeal);
83  Address a = toAddress(recover(s, h));
84  if (_s == CheckEverything && _bi.parentHash() && !m_authorities.count(a))
85  {
86  InvalidBlockNonce ex;
87  ex << errinfo_hash256(_bi.hash(WithoutSeal));
88  BOOST_THROW_EXCEPTION(ex);
89  }
90  else if (_s == QuickNonce && _bi.parentHash() && !SignatureStruct(sig(_bi)).isValid())
91  {
92  InvalidBlockNonce ex;
93  ex << errinfo_hash256(_bi.hash(WithoutSeal));
94  BOOST_THROW_EXCEPTION(ex);
95  }
96 }
Adapted from code found on http://stackoverflow.com/questions/180947/base64-decode-snippet-in-c Origi...
Definition: Arith256.cpp:15
vector_ref< _T const > ref(_T const &_t)
Definition: vector_ref.h:115
bytes rlp(_T _t)
Export a single item in RLP format, returning a byte array.
Definition: RLP.h:467
h256 hash(IncludeSeal _i=WithSeal) const
Definition: BlockHeader.cpp:64
uint64_t utcTime()
Get the current time in seconds since the epoch in UTC.
Definition: Common.cpp:64
Encapsulation of a block header.
Definition: BlockHeader.h:95
#define h(i)
Definition: sha.cpp:736
bool verify(Public const &_k, Signature const &_s, h256 const &_hash)
Verify signature.
Definition: Common.cpp:255
SecureFixedHash< 32 > Secret
Definition: Common.h:35
std::hash for asio::adress
Definition: Common.h:323
h256 const & parentHash() const
Definition: BlockHeader.h:154
#define a(i)
boost::error_info< struct tag_hash, h256 > errinfo_hash256
Definition: Exceptions.h:81
_N toHash(int _flags=Strict) const
Definition: RLP.h:298
std::string toJS(FixedHash< S > const &_h)
Definition: CommonJS.h:34
Public recover(Signature const &_sig, h256 const &_hash)
Recovers Public key from signed message hash.
Definition: Common.cpp:203
std::vector< byte > bytes
Definition: Common.h:75
virtual Transactions pending() const =0
Get a map containing each of the pending transactions.
std::unordered_set< T > toUnorderedSet(int _flags=LaissezFaire) const
Definition: RLP.h:231
std::unordered_map< std::string, std::string > StringHashMap
Definition: Common.h:143
Address toAddress(Public const &_public)
Convert a public key to address.
Definition: Common.cpp:87
Main API hub for interfacing with Ethereum.
Definition: Interface.h:67
u256 const & timestamp() const
Definition: BlockHeader.h:156
Signature sign(Secret const &_k, h256 const &_hash)
Returns siganture of message hash.
Definition: Common.cpp:233
bool sha3(bytesConstRef _input, bytesRef o_output)
Calculate SHA3-256 hash of the given input and load it into the given output.
Definition: SHA3.cpp:214
Class for interpreting Recursive Linear-Prefix Data.
Definition: RLP.h:64
void setDifficulty(u256 const &_v)
Definition: BlockHeader.h:150
bool isValid() const noexcept
Definition: Common.cpp:57
virtual BlockHeader pendingInfo() const
Definition: Interface.h:176
#define ETH_REGISTER_SEAL_ENGINE(Name)
Definition: SealEngine.h:148