Fabcoin Core  0.16.2
P2P Digital Currency
storageresults.cpp
Go to the documentation of this file.
1 #include <fasc/storageresults.h>
2 
3 StorageResults::StorageResults(std::string const& _path)
4 {
5  path = _path + "/resultsDB";
6  options.create_if_missing = true;
7  leveldb::Status status = leveldb::DB::Open(options, path, &db);
8  assert(status.ok());
9  LogPrintf("Opened LevelDB successfully\n");
10 }
11 
13 {
14  delete db;
15  db = NULL;
16 }
17 
18 void StorageResults::addResult(dev::h256 hashTx, std::vector<TransactionReceiptInfo>& result)
19 {
20  m_cache_result.insert(std::make_pair(hashTx, result));
21 }
22 
24 {
25  m_cache_result.clear();
26 }
27 
29 {
30  LogPrintf("Wiping LevelDB in %s\n", path);
31  leveldb::Status result = leveldb::DestroyDB(path, leveldb::Options());
32 }
33 
34 void StorageResults::deleteResults(std::vector<CTransactionRef> const& txs)
35 {
36  for (CTransactionRef tx : txs) {
37  dev::h256 hashTx = uintToh256(tx->GetHash());
38  m_cache_result.erase(hashTx);
39 
40  std::string keyTemp = hashTx.hex();
41  leveldb::Slice key(keyTemp);
42  leveldb::Status status = db->Delete(leveldb::WriteOptions(), key);
43  assert(status.ok());
44  }
45 }
46 
47 std::vector<TransactionReceiptInfo> StorageResults::getResult(dev::h256 const& hashTx)
48 {
49  std::vector<TransactionReceiptInfo> result;
50  auto it = m_cache_result.find(hashTx);
51  if (it == m_cache_result.end()) {
52  if (readResult(hashTx, result))
53  m_cache_result.insert(std::make_pair(hashTx, result));
54  } else {
55  result = it->second;
56  }
57  return result;
58 }
59 
61 {
62  if (m_cache_result.size()) {
63  for (auto const& i : m_cache_result) {
64  std::string valueTemp;
65  std::string keyTemp = i.first.hex();
66  leveldb::Slice key(keyTemp);
67  leveldb::Status status = db->Get(leveldb::ReadOptions(), key, &valueTemp);
68 
69  if (status.IsNotFound()) {
71 
72  for (size_t j = 0; j < i.second.size(); j++) {
73  tris.blockHashes.push_back(uintToh256(i.second[j].blockHash));
74  tris.blockNumbers.push_back(i.second[j].blockNumber);
75  tris.transactionHashes.push_back(uintToh256(i.second[j].transactionHash));
76  tris.transactionIndexes.push_back(i.second[j].transactionIndex);
77  tris.senders.push_back(i.second[j].from);
78  tris.receivers.push_back(i.second[j].to);
79  tris.cumulativeGasUsed.push_back(dev::u256(i.second[j].cumulativeGasUsed));
80  tris.gasUsed.push_back(dev::u256(i.second[j].gasUsed));
81  tris.contractAddresses.push_back(i.second[j].contractAddress);
82  tris.logs.push_back(logEntriesSerialization(i.second[j].logs));
83  tris.excepted.push_back(uint32_t(static_cast<int>(i.second[j].excepted)));
84  }
85 
86  dev::RLPStream streamRLP(11);
87  streamRLP << tris.blockHashes << tris.blockNumbers << tris.transactionHashes << tris.transactionIndexes << tris.senders;
88  streamRLP << tris.receivers << tris.cumulativeGasUsed << tris.gasUsed << tris.contractAddresses << tris.logs << tris.excepted;
89 
90  dev::bytes data = streamRLP.out();
91  std::string stringData(data.begin(), data.end());
92  leveldb::Slice value(stringData);
93  status = db->Put(leveldb::WriteOptions(), key, value);
94  assert(status.ok());
95  }
96  }
97  m_cache_result.clear();
98  }
99 }
100 
101 bool StorageResults::readResult(dev::h256 const& _key, std::vector<TransactionReceiptInfo>& _result)
102 {
103  std::string value;
104  std::string keyTemp = _key.hex();
105  ;
106  leveldb::Slice key(keyTemp);
107  leveldb::Status s = db->Get(leveldb::ReadOptions(), key, &value);
108 
109  if (!s.IsNotFound() && s.ok()) {
111 
112  dev::RLP state(value);
113  tris.blockHashes = state[0].toVector<dev::h256>();
114  tris.blockNumbers = state[1].toVector<uint32_t>();
115  tris.transactionHashes = state[2].toVector<dev::h256>();
116  tris.transactionIndexes = state[3].toVector<uint32_t>();
117  tris.senders = state[4].toVector<dev::h160>();
118  tris.receivers = state[5].toVector<dev::h160>();
119  tris.cumulativeGasUsed = state[6].toVector<dev::u256>();
120  tris.gasUsed = state[7].toVector<dev::u256>();
121  tris.contractAddresses = state[8].toVector<dev::h160>();
122  tris.logs = state[9].toVector<logEntriesSerializ>();
123  if (state.itemCount() == 11)
124  tris.excepted = state[10].toVector<uint32_t>();
125 
126  for (size_t j = 0; j < tris.blockHashes.size(); j++) {
128  tris.receivers[j], uint64_t(tris.cumulativeGasUsed[j]), uint64_t(tris.gasUsed[j]), tris.contractAddresses[j], logEntriesDeserialize(tris.logs[j]),
130  _result.push_back(tri);
131  }
132  return true;
133  }
134  return false;
135 }
136 
138 {
139  logEntriesSerializ result;
140  for (dev::eth::LogEntry i : _logs) {
141  result.push_back(std::make_pair(i.address, std::make_pair(i.topics, i.data)));
142  }
143  return result;
144 }
145 
147 {
148  dev::eth::LogEntries result;
149  for (std::pair<dev::Address, std::pair<dev::h256s, dev::bytes>> i : _logs) {
150  result.push_back(dev::eth::LogEntry(i.first, i.second.first, dev::bytes(i.second.second)));
151  }
152  return result;
153 }
logEntriesSerializ logEntriesSerialization(dev::eth::LogEntries const &_logs)
std::vector< dev::h160 > senders
std::vector< TransactionReceiptInfo > getResult(dev::h256 const &hashTx)
std::vector< std::pair< dev::Address, std::pair< dev::h256s, dev::bytes >>> logEntriesSerializ
Definition: storageresults.h:7
size_t itemCount() const
Definition: RLP.h:118
std::vector< T > toVector(int _flags=LaissezFaire) const
Definition: RLP.h:204
std::vector< dev::h160 > contractAddresses
bytes const & out() const
Read the byte stream.
Definition: RLP.h:433
dev::eth::LogEntries logEntriesDeserialize(logEntriesSerializ const &_logs)
std::vector< dev::u256 > cumulativeGasUsed
std::unordered_map< dev::h256, std::vector< TransactionReceiptInfo > > m_cache_result
assert(len-trim+(2 *lenIndices)<=WIDTH)
std::vector< dev::h256 > transactionHashes
std::shared_ptr< const CTransaction > CTransactionRef
Definition: transaction.h:437
std::vector< uint32_t > blockNumbers
StorageResults(std::string const &_path)
std::vector< uint32_t > transactionIndexes
#define LogPrintf(...)
Definition: util.h:153
dev::h256 uintToh256(const uint256 &in)
Definition: uint256.h:171
std::vector< dev::h160 > receivers
std::vector< logEntriesSerializ > logs
std::vector< dev::u256 > gasUsed
std::vector< byte > bytes
Definition: Common.h:75
leveldb::Options options
std::vector< uint32_t > excepted
boost::multiprecision::number< boost::multiprecision::cpp_int_backend< 256, 256, boost::multiprecision::unsigned_magnitude, boost::multiprecision::unchecked, void >> u256
Definition: Common.h:125
std::string path
std::vector< dev::h256 > blockHashes
leveldb::DB * db
void addResult(dev::h256 hashTx, std::vector< TransactionReceiptInfo > &result)
bool readResult(dev::h256 const &_key, std::vector< TransactionReceiptInfo > &_result)
void deleteResults(std::vector< CTransactionRef > const &txs)
Class for writing to an RLP bytestream.
Definition: RLP.h:383
uint8_t const * data
Definition: sha3.h:19
Class for interpreting Recursive Linear-Prefix Data.
Definition: RLP.h:64
TransactionException
Definition: Transaction.h:35
uint256 h256Touint(const dev::h256 &in)
Definition: uint256.h:178
std::vector< LogEntry > LogEntries
Definition: ExtVMFace.h:110
std::string hex() const
Definition: FixedHash.h:130
Definition: ExtVMFace.h:88