Fabcoin Core  0.16.2
P2P Digital Currency
fascstate.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include <libethereum/State.h>
4 #include <libevm/ExtVMFace.h>
5 #include <crypto/sha256.h>
6 #include <crypto/ripemd160.h>
7 #include <uint256.h>
9 #include <fasc/fasctransaction.h>
10 
11 #include <libethereum/Executive.h>
12 #include <libethcore/SealEngine.h>
13 
14 using OnOpFunc = std::function<void(uint64_t, uint64_t, dev::eth::Instruction, dev::bigint, dev::bigint,
16 using plusAndMinus = std::pair<dev::u256, dev::u256>;
17 using valtype = std::vector<unsigned char>;
18 
19 struct TransferInfo {
23 };
24 
25 struct Vin {
27  uint32_t nVout;
29  uint8_t alive;
30 };
31 
32 struct ResultExecute {
36 };
37 
38 namespace fasc {
39 template <class DB>
40 dev::AddressHash commit(std::unordered_map<dev::Address, Vin> const& _cache, dev::eth::SecureTrieDB<dev::Address, DB>& _state, std::unordered_map<dev::Address, dev::eth::Account> const& _cacheAcc)
41 {
42  dev::AddressHash ret;
43  for (auto const& i: _cache) {
44  if(i.second.alive == 0) {
45  _state.remove(i.first);
46  } else {
47  dev::RLPStream s(4);
48  s << i.second.hash << i.second.nVout << i.second.value << i.second.alive;
49  _state.insert(i.first, &s.out());
50  }
51  ret.insert(i.first);
52  }
53  return ret;
54 }
55 }
56 
57 class CondensingTX;
58 
59 class FascState : public dev::eth::State {
60 
61 public:
62 
63  FascState();
64 
65  FascState(dev::u256 const& _accountStartNonce, dev::OverlayDB const& _db, const std::string& _path, dev::eth::BaseState _bs = dev::eth::BaseState::PreExisting);
66 
68 
69  void setRootUTXO(dev::h256 const& _r) {
70  cacheUTXO.clear();
71  stateUTXO.setRoot(_r);
72  }
73 
74  void setCacheUTXO(dev::Address const& address, Vin const& vin) {
75  cacheUTXO.insert(std::make_pair(address, vin));
76  }
77 
79  return stateUTXO.root();
80  }
81 
82  std::unordered_map<dev::Address, Vin> vins() const; // temp
83 
84  dev::OverlayDB const& dbUtxo() const {
85  return dbUTXO;
86  }
87 
89  return dbUTXO;
90  }
91 
92  virtual ~FascState() {}
93 
94  friend CondensingTX;
95 
96 private:
97 
98  void transferBalance(dev::Address const& _from, dev::Address const& _to, dev::u256 const& _value);
99 
100  Vin const* vin(dev::Address const& _a) const;
101 
102  Vin* vin(dev::Address const& _addr);
103 
104  // void commit(CommitBehaviour _commitBehaviour);
105 
106  void kill(dev::Address _addr);
107 
108  void addBalance(dev::Address const& _id, dev::u256 const& _amount);
109 
110  dev::Address createFascAddress(dev::h256 hashTx, uint32_t voutNumber);
111 
112  void deleteAccounts(std::set<dev::Address>& addrs);
113 
114  void updateUTXO(const std::unordered_map<dev::Address, Vin>& vins);
115 
116  void printfErrorLog(const dev::eth::TransactionException er);
117 
119 
120  std::vector<TransferInfo> transfers;
121 
123 
125 
126  std::unordered_map<dev::Address, Vin> cacheUTXO;
127 };
128 
129 
131  std::unique_ptr<FascState>& globalStateRef;
134 
135  TemporaryState(std::unique_ptr<FascState>& _globalStateRef) :
136  globalStateRef(_globalStateRef),
137  oldHashStateRoot(globalStateRef->rootHash()),
138  oldHashUTXORoot(globalStateRef->rootHashUTXO()) {}
139 
140  void SetRoot(dev::h256 newHashStateRoot, dev::h256 newHashUTXORoot)
141  {
142  globalStateRef->setRoot(newHashStateRoot);
143  globalStateRef->setRootUTXO(newHashUTXORoot);
144  }
145 
147  globalStateRef->setRoot(oldHashStateRoot);
148  globalStateRef->setRootUTXO(oldHashUTXORoot);
149  }
150  TemporaryState() = delete;
151  TemporaryState(const TemporaryState&) = delete;
152  TemporaryState& operator=(const TemporaryState&) = delete;
153  TemporaryState(TemporaryState&&) = delete;
154  TemporaryState& operator=(TemporaryState&&) = delete;
155 };
156 
157 
160 
161 public:
162 
163  CondensingTX(FascState* _state, const std::vector<TransferInfo>& _transfers, const FascTransaction& _transaction, std::set<dev::Address> _deleteAddresses = std::set<dev::Address>()) : transfers(_transfers), deleteAddresses(_deleteAddresses), transaction(_transaction), state(_state) {}
164 
165  CTransaction createCondensingTX();
166 
167  std::unordered_map<dev::Address, Vin> createVin(const CTransaction& tx);
168 
170  return voutOverflow;
171  }
172 
173 private:
174 
175  void selectionVin();
176 
177  void calculatePlusAndMinus();
178 
179  bool createNewBalances();
180 
181  std::vector<CTxIn> createVins();
182 
183  std::vector<CTxOut> createVout();
184 
185  bool checkDeleteAddress(dev::Address addr);
186 
187  std::map<dev::Address, plusAndMinus> plusMinusInfo;
188 
189  std::map<dev::Address, dev::u256> balances;
190 
191  std::map<dev::Address, uint32_t> nVouts;
192 
193  std::map<dev::Address, Vin> vins;
194 
195  const std::vector<TransferInfo>& transfers;
196 
197  //We don't need the ordered nature of "set" here, but unordered_set's theoretical worst complexity is O(n), whereas set is O(log n)
198  //So, making this unordered_set could be an attack vector
199  const std::set<dev::Address> deleteAddresses;
200 
202 
204 
205  bool voutOverflow = false;
206 
207 };
dev::Address from
Definition: fascstate.h:20
const std::vector< TransferInfo > & transfers
Definition: fascstate.h:195
dev::eth::ExecutionResult execRes
Definition: fascstate.h:33
virtual ~FascState()
Definition: fascstate.h:92
void remove(KeyType _k)
Definition: TrieDB.h:335
std::function< void(uint64_t, uint64_t, dev::eth::Instruction, dev::bigint, dev::bigint, dev::bigint, dev::eth::VM *, dev::eth::ExtVMFace const *)> OnOpFunc
Definition: fascstate.h:15
#define function(a, b, c, d, k, s)
dev::OverlayDB const & dbUtxo() const
Definition: fascstate.h:84
dev::Address to
Definition: fascstate.h:21
std::map< dev::Address, dev::u256 > balances
Definition: fascstate.h:189
CTransaction tx
Definition: fascstate.h:35
Definition: fascstate.h:38
dev::OverlayDB dbUTXO
Definition: fascstate.h:122
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
const std::set< dev::Address > deleteAddresses
Definition: fascstate.h:199
CondensingTX(FascState *_state, const std::vector< TransferInfo > &_transfers, const FascTransaction &_transaction, std::set< dev::Address > _deleteAddresses=std::set< dev::Address >())
Definition: fascstate.h:163
dev::Address newAddress
Definition: fascstate.h:118
bool reachedVoutLimit()
Definition: fascstate.h:169
void setRootUTXO(dev::h256 const &_r)
Definition: fascstate.h:69
Different view on a GenericTrieDB that can use different key types.
Definition: TrieDB.h:320
Description of the result of executing a transaction.
Definition: Transaction.h:69
Model of an Ethereum state, essentially a facade for the trie.
Definition: State.h:161
Definition: fascstate.h:25
std::vector< TransferInfo > transfers
Definition: fascstate.h:120
dev::u256 value
Definition: fascstate.h:22
std::map< dev::Address, uint32_t > nVouts
Definition: fascstate.h:191
dev::u256 value
Definition: fascstate.h:28
dev::h256 oldHashStateRoot
Definition: fascstate.h:132
BaseState
Definition: State.h:76
std::unique_ptr< FascState > & globalStateRef
Definition: fascstate.h:131
void insert(KeyType _k, bytesConstRef _value)
Definition: TrieDB.h:333
dev::OverlayDB & dbUtxo()
Definition: fascstate.h:88
std::unordered_map< dev::Address, Vin > cacheUTXO
Definition: fascstate.h:126
Fixed-size raw-byte array container type, with an API optimised for storing hashes.
Definition: FixedHash.h:47
std::function< void(uint64_t, uint64_t, Instruction, bigint, bigint, bigint, VM *, ExtVMFace const *)> OnOpFunc
Definition: ExtVMFace.h:193
TemporaryState(std::unique_ptr< FascState > &_globalStateRef)
Definition: fascstate.h:135
FascState * state
Definition: fascstate.h:203
dev::h256 oldHashUTXORoot
Definition: fascstate.h:133
boost::multiprecision::number< boost::multiprecision::cpp_int_backend< 256, 256, boost::multiprecision::unsigned_magnitude, boost::multiprecision::unchecked, void >> u256
Definition: Common.h:125
uint8_t alive
Definition: fascstate.h:29
void setCacheUTXO(dev::Address const &address, Vin const &vin)
Definition: fascstate.h:74
dev::h256 rootHashUTXO() const
Definition: fascstate.h:78
Instruction
Virtual machine bytecode instruction.
Definition: Instruction.h:39
void SetRoot(dev::h256 newHashStateRoot, dev::h256 newHashUTXORoot)
Definition: fascstate.h:140
Interface and null implementation of the class for specifying VM externalities.
Definition: ExtVMFace.h:265
friend CondensingTX
Definition: fascstate.h:94
std::map< dev::Address, plusAndMinus > plusMinusInfo
Definition: fascstate.h:187
std::pair< dev::u256, dev::u256 > plusAndMinus
Definition: fascstate.h:16
dev::eth::SecureTrieDB< dev::Address, dev::OverlayDB > stateUTXO
Definition: fascstate.h:124
const FascTransaction & transaction
Definition: fascstate.h:201
dev::h256 hash
Definition: fascstate.h:26
std::vector< unsigned char > valtype
Definition: fascstate.h:17
std::map< dev::Address, Vin > vins
Definition: fascstate.h:193
Permanence
Definition: State.h:82
struct evm_uint160be address(struct evm_env *env)
Definition: capi.c:13
The basic transaction that is broadcasted on the network and contained in blocks. ...
Definition: transaction.h:275
Class for writing to an RLP bytestream.
Definition: RLP.h:383
uint32_t nVout
Definition: fascstate.h:27
TransactionException
Definition: Transaction.h:35
dev::AddressHash commit(std::unordered_map< dev::Address, Vin > const &_cache, dev::eth::SecureTrieDB< dev::Address, DB > &_state, std::unordered_map< dev::Address, dev::eth::Account > const &_cacheAcc)
Definition: fascstate.h:40
dev::eth::TransactionReceipt txRec
Definition: fascstate.h:34
std::unordered_set< h160 > AddressHash
A hash set of Ethereum addresses.
Definition: Common.h:71