Fabcoin Core  0.16.2
P2P Digital Currency
fascDGP.cpp
Go to the documentation of this file.
1 #include <fasc/fascDGP.h>
2 
4  std::vector<uint32_t> tempData = {dev::eth::EIP158Schedule.tierStepGas[0], dev::eth::EIP158Schedule.tierStepGas[1], dev::eth::EIP158Schedule.tierStepGas[2],
5  dev::eth::EIP158Schedule.tierStepGas[3], dev::eth::EIP158Schedule.tierStepGas[4], dev::eth::EIP158Schedule.tierStepGas[5],
6  dev::eth::EIP158Schedule.tierStepGas[6], dev::eth::EIP158Schedule.tierStepGas[7], dev::eth::EIP158Schedule.expGas,
7  dev::eth::EIP158Schedule.expByteGas, dev::eth::EIP158Schedule.sha3Gas, dev::eth::EIP158Schedule.sha3WordGas,
8  dev::eth::EIP158Schedule.sloadGas, dev::eth::EIP158Schedule.sstoreSetGas, dev::eth::EIP158Schedule.sstoreResetGas,
9  dev::eth::EIP158Schedule.sstoreRefundGas, dev::eth::EIP158Schedule.jumpdestGas, dev::eth::EIP158Schedule.logGas,
10  dev::eth::EIP158Schedule.logDataGas, dev::eth::EIP158Schedule.logTopicGas, dev::eth::EIP158Schedule.createGas,
11  dev::eth::EIP158Schedule.callGas, dev::eth::EIP158Schedule.callStipend, dev::eth::EIP158Schedule.callValueTransferGas,
12  dev::eth::EIP158Schedule.callNewAccountGas, dev::eth::EIP158Schedule.suicideRefundGas, dev::eth::EIP158Schedule.memoryGas,
13  dev::eth::EIP158Schedule.quadCoeffDiv, dev::eth::EIP158Schedule.createDataGas, dev::eth::EIP158Schedule.txGas,
14  dev::eth::EIP158Schedule.txCreateGas, dev::eth::EIP158Schedule.txDataZeroGas, dev::eth::EIP158Schedule.txDataNonZeroGas,
15  dev::eth::EIP158Schedule.copyGas, dev::eth::EIP158Schedule.extcodesizeGas, dev::eth::EIP158Schedule.extcodecopyGas,
16  dev::eth::EIP158Schedule.balanceGas, dev::eth::EIP158Schedule.suicideGas, dev::eth::EIP158Schedule.maxCodeSize
17  };
18  dataEIP158Schedule = tempData;
19 }
20 
21 bool FascDGP::checkLimitSchedule(const std::vector<uint32_t>& defaultData, const std::vector<uint32_t>& checkData) {
22  if(defaultData.size() == 39 && checkData.size() == 39) {
23  for(size_t i = 0; i < defaultData.size(); i++) {
24  uint32_t max = defaultData[i] * 1000 > 0 ? defaultData[i] * 1000 : 1 * 1000;
25  uint32_t min = defaultData[i] / 100 > 0 ? defaultData[i] / 100 : 1;
26  if(checkData[i] > max || checkData[i] < min) {
27  return false;
28  }
29  }
30  return true;
31  }
32  return false;
33 }
34 
36  clear();
37  dev::eth::EVMSchedule schedule = dev::eth::EIP158Schedule;
38  if(initStorages(GasScheduleDGP, blockHeight, ParseHex("26fadbe2"))) {
39  schedule = createEVMSchedule();
40  }
41  return schedule;
42 }
43 
44 uint64_t FascDGP::getUint64FromDGP(unsigned int blockHeight, const dev::Address& contract, std::vector<unsigned char> data) {
45  uint64_t value = 0;
46  if(initStorages(contract, blockHeight, data)) {
47  if(!dgpevm) {
48  parseStorageOneUint64(value);
49  } else {
50  parseDataOneUint64(value);
51  }
52  }
53  return value;
54 }
55 
56 uint32_t FascDGP::getBlockSize(unsigned int blockHeight) {
57  clear();
58  uint32_t result = DEFAULT_BLOCK_SIZE_DGP;
59  uint32_t blockSize = getUint64FromDGP(blockHeight, BlockSizeDGP, ParseHex("92ac3c62"));
60  if(blockSize <= MAX_BLOCK_SIZE_DGP && blockSize >= MIN_BLOCK_SIZE_DGP) {
61  result = blockSize;
62  }
63  return result;
64 }
65 
66 uint64_t FascDGP::getMinGasPrice(unsigned int blockHeight) {
67  clear();
68  uint64_t result = DEFAULT_MIN_GAS_PRICE_DGP;
69  uint64_t minGasPrice = getUint64FromDGP(blockHeight, GasPriceDGP, ParseHex("3fb58819"));
70  if(minGasPrice <= MAX_MIN_GAS_PRICE_DGP && minGasPrice >= MIN_MIN_GAS_PRICE_DGP) {
71  result = minGasPrice;
72  }
73  return result;
74 }
75 
76 uint64_t FascDGP::getBlockGasLimit(unsigned int blockHeight) {
77  clear();
78  uint64_t result = DEFAULT_BLOCK_GAS_LIMIT_DGP;
79  auto phex = ParseHex("2cc8377d");
80  uint64_t blockGasLimit = getUint64FromDGP(blockHeight, BlockGasLimitDGP, phex);
81  if(blockGasLimit <= MAX_BLOCK_GAS_LIMIT_DGP && blockGasLimit >= MIN_BLOCK_GAS_LIMIT_DGP) {
82  result = blockGasLimit;
83  }
84  return result;
85 }
86 
87 bool FascDGP::initStorages(const dev::Address& addr, unsigned int blockHeight, std::vector<unsigned char> data) {
88  initStorageDGP(addr);
91  if(address != dev::Address()) {
92  if(!dgpevm) {
93  initStorageTemplate(address);
94  } else {
95  initDataTemplate(address, data);
96  }
97  return true;
98  }
99  return false;
100 }
101 
103  storageDGP = state->storage(addr);
104 }
105 
107  storageTemplate = state->storage(addr);
108 }
109 
110 void FascDGP::initDataTemplate(const dev::Address& addr, std::vector<unsigned char>& data) {
111  dataTemplate = CallContract(addr, data)[0].execRes.output;
112 }
113 
115  dev::h256 paramsInstanceHash = sha3(dev::h256("0000000000000000000000000000000000000000000000000000000000000000"));
116  if(storageDGP.count(paramsInstanceHash)) {
117  dev::u256 paramsInstanceSize = storageDGP.find(paramsInstanceHash)->second.second;
118  for(size_t i = 0; i < size_t(paramsInstanceSize); i++) {
119  std::pair<unsigned int, dev::Address> params;
120  params.first = dev::toUint64(storageDGP.find(sha3(paramsInstanceHash))->second.second);
121  ++paramsInstanceHash;
122  params.second = dev::right160(dev::h256(storageDGP.find(sha3(paramsInstanceHash))->second.second));
123  ++paramsInstanceHash;
124  paramsInstance.push_back(params);
125  }
126  }
127 }
128 
129 dev::Address FascDGP::getAddressForBlock(unsigned int blockHeight) {
130  for(auto i = paramsInstance.rbegin(); i != paramsInstance.rend(); i++) {
131  if(i->first <= blockHeight)
132  return i->second;
133  }
134  return dev::Address();
135 }
136 
137 static inline bool sortPairs(const std::pair<dev::u256, dev::u256>& a, const std::pair<dev::u256, dev::u256>& b) {
138  return a.first < b.first;
139 }
140 
141 void FascDGP::parseStorageScheduleContract(std::vector<uint32_t>& uint32Values) {
142  std::vector<std::pair<dev::u256, dev::u256>> data;
143  for(size_t i = 0; i < 5; i++) {
144  dev::h256 gasScheduleHash = sha3(dev::h256(dev::u256(i)));
145  if(storageTemplate.count(gasScheduleHash)) {
146  dev::u256 key = storageTemplate.find(gasScheduleHash)->second.first;
147  dev::u256 value = storageTemplate.find(gasScheduleHash)->second.second;
148  data.push_back(std::make_pair(key, value));
149  }
150  }
151 
152  std::sort(data.begin(), data.end(), sortPairs);
153 
154  for(std::pair<dev::u256, dev::u256> d : data) {
155  dev::u256 value = d.second;
156  for(size_t i = 0; i < 4; i++) {
157  uint64_t uint64Value = dev::toUint64(value);
158  value = value >> 64;
159 
160  uint32Values.push_back(uint32_t(uint64Value));
161  uint64Value = uint64Value >> 32;
162  uint32Values.push_back(uint32_t(uint64Value));
163  }
164  }
165 }
166 
167 void FascDGP::parseDataScheduleContract(std::vector<uint32_t>& uint32Values) {
168  size_t size = dataTemplate.size() / 32;
169  for(size_t i = 0; i < size; i++) {
170  std::vector<unsigned char> value = std::vector<unsigned char>(dataTemplate.begin() + (i * 32), dataTemplate.begin() + ((i+1) * 32));
171  dev::h256 valueTemp(value);
172  uint32Values.push_back(dev::toUint64(dev::u256(valueTemp)));
173  }
174 }
175 
176 void FascDGP::parseStorageOneUint64(uint64_t& value) {
177  dev::h256 blockSizeHash = sha3(dev::h256(dev::u256(0)));
178  if(storageTemplate.count(blockSizeHash)) {
179  value = dev::toUint64(storageTemplate.find(blockSizeHash)->second.second);
180  }
181 }
182 
183 void FascDGP::parseDataOneUint64(uint64_t& value) {
184  if(dataTemplate.size() == 32) {
186  }
187 }
188 
190  dev::eth::EVMSchedule schedule = dev::eth::EIP158Schedule;
191  std::vector<uint32_t> uint32Values;
192 
193  if(!dgpevm) {
194  parseStorageScheduleContract(uint32Values);
195  } else {
196  parseDataScheduleContract(uint32Values);
197  }
198 
199  if(!checkLimitSchedule(dataEIP158Schedule, uint32Values))
200  return schedule;
201 
202  if(uint32Values.size() >= 39) {
203  schedule.tierStepGas = {{
204  uint32Values[0], uint32Values[1], uint32Values[2], uint32Values[3],
205  uint32Values[4], uint32Values[5], uint32Values[6], uint32Values[7]
206  }
207  };
208  schedule.expGas = uint32Values[8];
209  schedule.expByteGas = uint32Values[9];
210  schedule.sha3Gas = uint32Values[10];
211  schedule.sha3WordGas = uint32Values[11];
212  schedule.sloadGas = uint32Values[12];
213  schedule.sstoreSetGas = uint32Values[13];
214  schedule.sstoreResetGas = uint32Values[14];
215  schedule.sstoreRefundGas = uint32Values[15];
216  schedule.jumpdestGas = uint32Values[16];
217  schedule.logGas = uint32Values[17];
218  schedule.logDataGas = uint32Values[18];
219  schedule.logTopicGas = uint32Values[19];
220  schedule.createGas = uint32Values[20];
221  schedule.callGas = uint32Values[21];
222  schedule.callStipend = uint32Values[22];
223  schedule.callValueTransferGas = uint32Values[23];
224  schedule.callNewAccountGas = uint32Values[24];
225  schedule.suicideRefundGas = uint32Values[25];
226  schedule.memoryGas = uint32Values[26];
227  schedule.quadCoeffDiv = uint32Values[27];
228  schedule.createDataGas = uint32Values[28];
229  schedule.txGas = uint32Values[29];
230  schedule.txCreateGas = uint32Values[30];
231  schedule.txDataZeroGas = uint32Values[31];
232  schedule.txDataNonZeroGas = uint32Values[32];
233  schedule.copyGas = uint32Values[33];
234  schedule.extcodesizeGas = uint32Values[34];
235  schedule.extcodecopyGas = uint32Values[35];
236  schedule.balanceGas = uint32Values[36];
237  schedule.suicideGas = uint32Values[37];
238  schedule.maxCodeSize = uint32Values[38];
239  }
240  return schedule;
241 }
242 
245  storageDGP.clear();
246  storageTemplate.clear();
247  paramsInstance.clear();
248 }
uint64_t toUint64(T _u)
Converts given multiprecision number to standard number type.
Definition: Common.h:200
void parseDataScheduleContract(std::vector< uint32_t > &uint32Values)
Definition: fascDGP.cpp:167
std::vector< uint32_t > dataEIP158Schedule
Definition: fascDGP.h:91
dev::Address templateContract
Definition: fascDGP.h:81
h160 right160(h256 const &_t)
Convert the given value into h160 (160-bit unsigned integer) using the right 20 bytes.
Definition: FixedHash.h:353
bool checkLimitSchedule(const std::vector< uint32_t > &defaultData, const std::vector< uint32_t > &checkData)
Definition: fascDGP.cpp:21
uint64_t getUint64FromDGP(unsigned int blockHeight, const dev::Address &contract, std::vector< unsigned char > data)
Definition: fascDGP.cpp:44
h160 Address
An Ethereum address: 20 bytes.
Definition: Common.h:62
unsigned txDataNonZeroGas
Definition: EVMSchedule.h:66
ExecStats::duration min
Definition: ExecStats.cpp:35
void initDataTemplate(const dev::Address &addr, std::vector< unsigned char > &data)
Definition: fascDGP.cpp:110
#define a(i)
void initStorageTemplate(const dev::Address &addr)
Definition: fascDGP.cpp:106
std::vector< std::pair< unsigned int, dev::Address > > paramsInstance
Definition: fascDGP.h:89
ExecStats::duration max
Definition: ExecStats.cpp:36
std::array< unsigned, 8 > tierStepGas
Definition: EVMSchedule.h:41
uint64_t getBlockGasLimit(unsigned int blockHeight)
Definition: fascDGP.cpp:76
unsigned suicideRefundGas
Definition: EVMSchedule.h:59
bool dgpevm
Definition: fascDGP.h:77
dev::eth::EVMSchedule getGasSchedule(unsigned int blockHeight)
Definition: fascDGP.cpp:35
u256 storage(Address const &_contract, u256 const &_memory) const
Get the value of a storage position of an account.
Definition: State.cpp:353
std::map< dev::h256, std::pair< dev::u256, dev::u256 > > storageTemplate
Definition: fascDGP.h:85
Fixed-size raw-byte array container type, with an API optimised for storing hashes.
Definition: FixedHash.h:47
unsigned callValueTransferGas
Definition: EVMSchedule.h:57
void clear()
Definition: fascDGP.cpp:243
boost::multiprecision::number< boost::multiprecision::cpp_int_backend< 256, 256, boost::multiprecision::unsigned_magnitude, boost::multiprecision::unchecked, void >> u256
Definition: Common.h:125
void parseStorageScheduleContract(std::vector< uint32_t > &uint32Values)
Definition: fascDGP.cpp:141
#define b(i, j)
std::map< dev::h256, std::pair< dev::u256, dev::u256 > > storageDGP
Definition: fascDGP.h:83
std::vector< ResultExecute > CallContract(const dev::Address &addrContract, std::vector< unsigned char > opcode, const dev::Address &sender, uint64_t gasLimit)
unsigned sstoreRefundGas
Definition: EVMSchedule.h:49
std::vector< unsigned char > dataTemplate
Definition: fascDGP.h:87
void parseStorageOneUint64(uint64_t &blockSize)
Definition: fascDGP.cpp:176
void initDataEIP158()
Definition: fascDGP.cpp:3
uint8_t const size_t const size
Definition: sha3.h:20
dev::eth::EVMSchedule createEVMSchedule()
Definition: fascDGP.cpp:189
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
uint32_t getBlockSize(unsigned int blockHeight)
Definition: fascDGP.cpp:56
dev::Address getAddressForBlock(unsigned int blockHeight)
Definition: fascDGP.cpp:129
#define d(i)
Definition: sha.cpp:732
struct evm_uint160be address(struct evm_env *env)
Definition: capi.c:13
bool initStorages(const dev::Address &addr, unsigned int blockHeight, std::vector< unsigned char > data=std::vector< unsigned char >())
Definition: fascDGP.cpp:87
const FascState * state
Definition: fascDGP.h:79
void initStorageDGP(const dev::Address &addr)
Definition: fascDGP.cpp:102
void parseDataOneUint64(uint64_t &value)
Definition: fascDGP.cpp:183
uint8_t const * data
Definition: sha3.h:19
unsigned callNewAccountGas
Definition: EVMSchedule.h:58
uint64_t getMinGasPrice(unsigned int blockHeight)
Definition: fascDGP.cpp:66
void createParamsInstance()
Definition: fascDGP.cpp:114
std::vector< unsigned char > ParseHex(const char *psz)