@@ -17,52 +17,112 @@ namespace jit
1717namespace // Helper functions
1818{
1919
20- int64_t const c_stepGas = 1 ;
21- int64_t const c_balanceGas = 20 ;
22- int64_t const c_sha3Gas = 10 ;
23- int64_t const c_sha3WordGas = 10 ;
24- int64_t const c_sloadGas = 20 ;
25- int64_t const c_sstoreSetGas = 300 ;
26- int64_t const c_sstoreResetGas = 100 ;
27- int64_t const c_sstoreRefundGas = 100 ;
28- int64_t const c_createGas = 100 ;
29- int64_t const c_createDataGas = 5 ;
30- int64_t const c_callGas = 20 ;
31- int64_t const c_expGas = 1 ;
32- int64_t const c_expByteGas = 1 ;
33- int64_t const c_memoryGas = 1 ;
34- int64_t const c_txDataZeroGas = 1 ;
35- int64_t const c_txDataNonZeroGas = 5 ;
36- int64_t const c_txGas = 500 ;
37- int64_t const c_logGas = 32 ;
38- int64_t const c_logDataGas = 1 ;
39- int64_t const c_logTopicGas = 32 ;
40- int64_t const c_copyGas = 1 ;
20+ int64_t const c_stepGas[] = {0 , 2 , 3 , 5 , 8 , 10 , 20 };
21+ int64_t const c_expByteGas = 10 ;
22+ int64_t const c_sha3Gas = 30 ;
23+ int64_t const c_sha3WordGas = 6 ;
24+ int64_t const c_sloadGas = 50 ;
25+ int64_t const c_sstoreSetGas = 20000 ;
26+ int64_t const c_sstoreResetGas = 5000 ;
27+ int64_t const c_jumpdestGas = 1 ;
28+ int64_t const c_logGas = 375 ;
29+ int64_t const c_logTopicGas = 375 ;
30+ int64_t const c_logDataGas = 8 ;
31+ int64_t const c_callGas = 40 ;
32+ int64_t const c_createGas = 23000 ;
33+ int64_t const c_memoryGas = 3 ;
34+ int64_t const c_copyGas = 3 ;
4135
4236int64_t getStepCost (Instruction inst)
4337{
4438 switch (inst)
4539 {
46- default : // Assumes instruction code is valid
47- return c_stepGas;
48-
40+ // Tier 0
4941 case Instruction::STOP :
42+ case Instruction::RETURN :
5043 case Instruction::SUICIDE :
5144 case Instruction::SSTORE : // Handle cost of SSTORE separately in GasMeter::countSStore()
52- return 0 ;
53-
54- case Instruction::EXP : return c_expGas;
55-
56- case Instruction::SLOAD : return c_sloadGas;
57-
58- case Instruction::SHA3 : return c_sha3Gas;
59-
60- case Instruction::BALANCE : return c_balanceGas;
61-
62- case Instruction::CALL :
63- case Instruction::CALLCODE : return c_callGas;
64-
65- case Instruction::CREATE : return c_createGas;
45+ return c_stepGas[0 ];
46+
47+ // Tier 1
48+ case Instruction::ADDRESS :
49+ case Instruction::ORIGIN :
50+ case Instruction::CALLER :
51+ case Instruction::CALLVALUE :
52+ case Instruction::CALLDATASIZE :
53+ case Instruction::CODESIZE :
54+ case Instruction::GASPRICE :
55+ case Instruction::COINBASE :
56+ case Instruction::TIMESTAMP :
57+ case Instruction::NUMBER :
58+ case Instruction::DIFFICULTY :
59+ case Instruction::GASLIMIT :
60+ case Instruction::POP :
61+ case Instruction::PC :
62+ case Instruction::MSIZE :
63+ case Instruction::GAS :
64+ return c_stepGas[1 ];
65+
66+ // Tier 2
67+ case Instruction::ADD :
68+ case Instruction::SUB :
69+ case Instruction::LT :
70+ case Instruction::GT :
71+ case Instruction::SLT :
72+ case Instruction::SGT :
73+ case Instruction::EQ :
74+ case Instruction::ISZERO :
75+ case Instruction::AND :
76+ case Instruction::OR :
77+ case Instruction::XOR :
78+ case Instruction::NOT :
79+ case Instruction::BYTE :
80+ case Instruction::CALLDATALOAD :
81+ case Instruction::CALLDATACOPY :
82+ case Instruction::CODECOPY :
83+ case Instruction::MLOAD :
84+ case Instruction::MSTORE :
85+ case Instruction::MSTORE8 :
86+ case Instruction::ANY_PUSH :
87+ case Instruction::ANY_DUP :
88+ case Instruction::ANY_SWAP :
89+ return c_stepGas[2 ];
90+
91+ // Tier 3
92+ case Instruction::MUL :
93+ case Instruction::DIV :
94+ case Instruction::SDIV :
95+ case Instruction::MOD :
96+ case Instruction::SMOD :
97+ case Instruction::SIGNEXTEND :
98+ return c_stepGas[3 ];
99+
100+ // Tier 4
101+ case Instruction::ADDMOD :
102+ case Instruction::MULMOD :
103+ case Instruction::JUMP :
104+ return c_stepGas[4 ];
105+
106+ // Tier 5
107+ case Instruction::EXP :
108+ case Instruction::JUMPI :
109+ return c_stepGas[5 ];
110+
111+ // Tier 6
112+ case Instruction::BALANCE :
113+ case Instruction::EXTCODESIZE :
114+ case Instruction::EXTCODECOPY :
115+ case Instruction::BLOCKHASH :
116+ return c_stepGas[6 ];
117+
118+ case Instruction::SHA3 :
119+ return c_sha3Gas;
120+
121+ case Instruction::SLOAD :
122+ return c_sloadGas;
123+
124+ case Instruction::JUMPDEST :
125+ return c_jumpdestGas;
66126
67127 case Instruction::LOG0 :
68128 case Instruction::LOG1 :
@@ -73,7 +133,16 @@ int64_t getStepCost(Instruction inst)
73133 auto numTopics = static_cast <int64_t >(inst) - static_cast <int64_t >(Instruction::LOG0 );
74134 return c_logGas + numTopics * c_logTopicGas;
75135 }
136+
137+ case Instruction::CALL :
138+ case Instruction::CALLCODE :
139+ return c_callGas;
140+
141+ case Instruction::CREATE :
142+ return c_createGas;
76143 }
144+
145+ return 0 ; // TODO: Add UNREACHABLE macro
77146}
78147
79148}
@@ -152,7 +221,7 @@ void GasMeter::countExp(llvm::Value* _exponent)
152221 auto lz = m_builder.CreateTrunc (lz256, Type::Gas, " lz" );
153222 auto sigBits = m_builder.CreateSub (m_builder.getInt64 (256 ), lz, " sigBits" );
154223 auto sigBytes = m_builder.CreateUDiv (m_builder.CreateAdd (sigBits, m_builder.getInt64 (7 )), m_builder.getInt64 (8 ));
155- count (sigBytes);
224+ count (m_builder. CreateNUWMul ( sigBytes, m_builder. getInt64 (c_expByteGas)) );
156225}
157226
158227void GasMeter::countSStore (Ext& _ext, llvm::Value* _index, llvm::Value* _newValue)
@@ -173,8 +242,8 @@ void GasMeter::countLogData(llvm::Value* _dataLength)
173242{
174243 assert (m_checkCall);
175244 assert (m_blockCost > 0 ); // LOGn instruction is already counted
176- static_assert (c_logDataGas = = 1 , " Log data gas cost has changed. Update GasMeter." );
177- count (_dataLength);
245+ static_assert (c_logDataGas ! = 1 , " Log data gas cost has changed. Update GasMeter." );
246+ count (m_builder. CreateNUWMul ( _dataLength, Constant::get (c_logDataGas))); // TODO: Use i64
178247}
179248
180249void GasMeter::countSha3Data (llvm::Value* _dataLength)
@@ -217,14 +286,14 @@ void GasMeter::commitCostBlock()
217286
218287void GasMeter::countMemory (llvm::Value* _additionalMemoryInWords, llvm::Value* _jmpBuf, llvm::Value* _gasPtr)
219288{
220- static_assert (c_memoryGas = = 1 , " Memory gas cost has changed. Update GasMeter." );
289+ static_assert (c_memoryGas ! = 1 , " Memory gas cost has changed. Update GasMeter." );
221290 count (_additionalMemoryInWords, _jmpBuf, _gasPtr);
222291}
223292
224293void GasMeter::countCopy (llvm::Value* _copyWords)
225294{
226- static_assert (c_copyGas = = 1 , " Copy gas cost has changed. Update GasMeter." );
227- count (_copyWords);
295+ static_assert (c_copyGas ! = 1 , " Copy gas cost has changed. Update GasMeter." );
296+ count (m_builder. CreateNUWMul ( _copyWords, m_builder. getInt64 (c_copyGas)) );
228297}
229298
230299}
0 commit comments