Fabcoin Core  0.16.2
P2P Digital Currency
SealEngine.h
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 */
24 #pragma once
25 
26 #include <functional>
27 #include <unordered_map>
28 #include <libdevcore/Guards.h>
29 #include <libdevcore/RLP.h>
30 #include "BlockHeader.h"
31 #include "Common.h"
32 
33 namespace dev
34 {
35 namespace eth
36 {
37 
38 class BlockHeader;
39 struct ChainOperationParams;
40 class Interface;
41 class PrecompiledFace;
42 class TransactionBase;
43 class EnvInfo;
44 
46 {
47 public:
48  virtual ~SealEngineFace() {}
49 
50  virtual std::string name() const = 0;
51  virtual unsigned revision() const { return 0; }
52  virtual unsigned sealFields() const { return 0; }
53  virtual bytes sealRLP() const { return bytes(); }
54  virtual StringHashMap jsInfo(BlockHeader const&) const { return StringHashMap(); }
55 
57  virtual void verify(Strictness _s, BlockHeader const& _bi, BlockHeader const& _parent = BlockHeader(), bytesConstRef _block = bytesConstRef()) const;
59  virtual void verifyTransaction(ImportRequirements::value _ir, TransactionBase const& _t, BlockHeader const& _bi) const;
61  virtual void populateFromParent(BlockHeader& _bi, BlockHeader const& _parent) const;
62 
63  bytes option(std::string const& _name) const { Guard l(x_options); return m_options.count(_name) ? m_options.at(_name) : bytes(); }
64  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; }
65 
66  virtual strings sealers() const { return { "default" }; }
67  virtual std::string sealer() const { return "default"; }
68  virtual void setSealer(std::string const&) {}
69 
70  virtual bool shouldSeal(Interface*) { return true; }
71  virtual void generateSeal(BlockHeader const& _bi) = 0;
72  virtual void onSealGenerated(std::function<void(bytes const& s)> const& _f) = 0;
73  virtual void cancelGeneration() {}
74 
75  ChainOperationParams const& chainParams() const { return m_params; }
76  void setChainParams(ChainOperationParams const& _params) { m_params = _params; }
77  SealEngineFace* withChainParams(ChainOperationParams const& _params) { setChainParams(_params); return this; }
78  virtual EVMSchedule const& evmSchedule(EnvInfo const&) const = 0;
79 
80  virtual bool isPrecompiled(Address const& _a, u256 const& _blockNumber) const
81  {
82  return m_params.precompiled.count(_a) != 0 && _blockNumber >= m_params.precompiled.at(_a).startingBlock();
83  }
84  virtual bigint costOfPrecompiled(Address const& _a, bytesConstRef _in, u256 const&) const { return m_params.precompiled.at(_a).cost(_in); }
85  virtual std::pair<bool, bytes> executePrecompiled(Address const& _a, bytesConstRef _in, u256 const&) const { return m_params.precompiled.at(_a).execute(_in); }
86 
88  void setFascSchedule(EVMSchedule _fascSchedule) const { fascSchedule = _fascSchedule; }
89 
91 
92  //deleteAddresses is a set that keeps track of accounts that were inserted as part of sending to pubkeyhash addresses
93  //This is added to when doing a CALL to a non-existent address (if the account does not exist, it assumes you're sending to pubkeyhash)
94  //It is also added to when a SUICIDE is done where all coins are sent to a non-existent address
95  //After contract execution, these accounts will be deleted from the ETH database, and their vins marked dead, to ensure future executions behave the same
96  mutable std::set<Address> deleteAddresses;
98 
99 protected:
100  virtual bool onOptionChanging(std::string const&, bytes const&) { return true; }
101 
102 private:
103  mutable Mutex x_options;
104  std::unordered_map<std::string, bytes> m_options;
105 
106  mutable EVMSchedule fascSchedule; // fasc
107 
109 };
110 
112 {
113 public:
114  void generateSeal(BlockHeader const& _bi) override
115  {
116  RLPStream ret;
117  _bi.streamRLP(ret);
118  if (m_onSealGenerated)
119  m_onSealGenerated(ret.out());
120  }
121  void onSealGenerated(std::function<void(bytes const&)> const& _f) override { m_onSealGenerated = _f; }
122  EVMSchedule const& evmSchedule(EnvInfo const&) const override;
123 
124 protected:
125  std::function<void(bytes const& s)> m_onSealGenerated;
126 };
127 
128 using SealEngineFactory = std::function<SealEngineFace*()>;
129 
131 {
132 public:
135  static SealEngineFace* create(ChainOperationParams const& _params);
136  static SealEngineFace* create(std::string const& _name) { if (!get()->m_sealEngines.count(_name)) return nullptr; return get()->m_sealEngines[_name](); }
137 
138  template <class SealEngine> static SealEngineFactory registerSealEngine(std::string const& _name) { return (get()->m_sealEngines[_name] = [](){return new SealEngine;}); }
139  static void unregisterSealEngine(std::string const& _name) { get()->m_sealEngines.erase(_name); }
140 
141 private:
142  static SealEngineRegistrar* get() { if (!s_this) s_this = new SealEngineRegistrar; return s_this; }
143 
144  std::unordered_map<std::string, SealEngineFactory> m_sealEngines;
146 };
147 
148 #define ETH_REGISTER_SEAL_ENGINE(Name) static SealEngineFactory __eth_registerSealEngineFactory ## Name = SealEngineRegistrar::registerSealEngine<Name>(#Name)
149 
151 {
152 public:
153  std::string name() const override { return "NoProof"; }
154  static void init();
155 };
156 
157 }
158 }
Adapted from code found on http://stackoverflow.com/questions/180947/base64-decode-snippet-in-c Origi...
Definition: Arith256.cpp:15
EVMSchedule & getFascSchedule() const
Definition: SealEngine.h:90
#define function(a, b, c, d, k, s)
ChainOperationParams const & chainParams() const
Definition: SealEngine.h:75
void streamRLP(RLPStream &_s, IncludeSeal _i=WithSeal) const
Definition: BlockHeader.cpp:83
std::function< SealEngineFace *()> SealEngineFactory
Definition: SealEngine.h:128
Encapsulation of a block header.
Definition: BlockHeader.h:95
boost::multiprecision::number< boost::multiprecision::cpp_int_backend<>> bigint
Definition: Common.h:121
bytes const & out() const
Read the byte stream.
Definition: RLP.h:433
virtual void generateSeal(BlockHeader const &_bi)=0
virtual StringHashMap jsInfo(BlockHeader const &) const
Definition: SealEngine.h:54
static SealEngineFace * create(std::string const &_name)
Definition: SealEngine.h:136
virtual void verifyTransaction(ImportRequirements::value _ir, TransactionBase const &_t, BlockHeader const &_bi) const
Additional verification for transactions in blocks.
Definition: SealEngine.cpp:46
std::vector< std::string > strings
Definition: Common.h:147
virtual std::pair< bool, bytes > executePrecompiled(Address const &_a, bytesConstRef _in, u256 const &) const
Definition: SealEngine.h:85
EVMSchedule fascSchedule
Definition: SealEngine.h:106
virtual std::string name() const =0
SealEngineFace * withChainParams(ChainOperationParams const &_params)
Definition: SealEngine.h:77
void generateSeal(BlockHeader const &_bi) override
Definition: SealEngine.h:114
std::lock_guard< std::mutex > Guard
Definition: Guards.h:41
ChainOperationParams m_params
Definition: SealEngine.h:108
static void unregisterSealEngine(std::string const &_name)
Definition: SealEngine.h:139
void onSealGenerated(std::function< void(bytes const &)> const &_f) override
Definition: SealEngine.h:121
virtual void onSealGenerated(std::function< void(bytes const &s)> const &_f)=0
std::unordered_map< Address, PrecompiledContract > precompiled
Precompiled contracts as specified in the chain params.
std::vector< byte > bytes
Definition: Common.h:75
vector_ref< byte const > bytesConstRef
Definition: Common.h:77
Fixed-size raw-byte array container type, with an API optimised for storing hashes.
Definition: FixedHash.h:47
virtual strings sealers() const
Definition: SealEngine.h:66
std::unordered_map< std::string, SealEngineFactory > m_sealEngines
Definition: SealEngine.h:144
boost::multiprecision::number< boost::multiprecision::cpp_int_backend< 256, 256, boost::multiprecision::unsigned_magnitude, boost::multiprecision::unchecked, void >> u256
Definition: Common.h:125
std::unordered_map< std::string, std::string > StringHashMap
Definition: Common.h:143
Main API hub for interfacing with Ethereum.
Definition: Interface.h:67
virtual void setSealer(std::string const &)
Definition: SealEngine.h:68
static SealEngineFactory registerSealEngine(std::string const &_name)
Definition: SealEngine.h:138
static SealEngineRegistrar * s_this
Definition: SealEngine.h:145
virtual EVMSchedule const & evmSchedule(EnvInfo const &) const =0
bool setOption(std::string const &_name, bytes const &_value)
Definition: SealEngine.h:64
std::set< Address > deleteAddresses
Definition: SealEngine.h:96
virtual bigint costOfPrecompiled(Address const &_a, bytesConstRef _in, u256 const &) const
Definition: SealEngine.h:84
std::function< void(bytes const &s)> m_onSealGenerated
Definition: SealEngine.h:125
void setChainParams(ChainOperationParams const &_params)
Definition: SealEngine.h:76
bytes option(std::string const &_name) const
Definition: SealEngine.h:63
virtual bool shouldSeal(Interface *)
Definition: SealEngine.h:70
virtual void populateFromParent(BlockHeader &_bi, BlockHeader const &_parent) const
Don&#39;t forget to call Super::populateFromParent when subclassing & overriding.
Definition: SealEngine.cpp:41
virtual bool isPrecompiled(Address const &_a, u256 const &_blockNumber) const
Definition: SealEngine.h:80
virtual unsigned revision() const
Definition: SealEngine.h:51
virtual bool onOptionChanging(std::string const &, bytes const &)
Definition: SealEngine.h:100
virtual bytes sealRLP() const
Definition: SealEngine.h:53
std::mutex Mutex
Definition: Guards.h:37
virtual unsigned sealFields() const
Definition: SealEngine.h:52
std::unordered_map< std::string, bytes > m_options
Definition: SealEngine.h:104
Class for writing to an RLP bytestream.
Definition: RLP.h:383
virtual std::string sealer() const
Definition: SealEngine.h:67
virtual void verify(Strictness _s, BlockHeader const &_bi, BlockHeader const &_parent=BlockHeader(), bytesConstRef _block=bytesConstRef()) const
Don&#39;t forget to call Super::verify when subclassing & overriding.
Definition: SealEngine.cpp:36
Encodes a transaction, ready to be exported to or freshly imported from RLP.
Definition: Transaction.h:50
virtual void cancelGeneration()
Definition: SealEngine.h:73
void setFascSchedule(EVMSchedule _fascSchedule) const
Definition: SealEngine.h:88
std::string name() const override
Definition: SealEngine.h:153