@@ -812,6 +812,7 @@ var _baseCheck = map[OpCode]req{
812812 COINBASE : {0 , GasQuickStep },
813813 TIMESTAMP : {0 , GasQuickStep },
814814 NUMBER : {0 , GasQuickStep },
815+ CALLDATASIZE : {0 , GasQuickStep },
815816 DIFFICULTY : {0 , GasQuickStep },
816817 GASLIMIT : {0 , GasQuickStep },
817818 POP : {0 , GasQuickStep },
@@ -862,9 +863,11 @@ func (self *Vm) calculateGasAndSize(context *Context, caller ContextRef, op OpCo
862863 case SWAP1 , SWAP2 , SWAP3 , SWAP4 , SWAP5 , SWAP6 , SWAP7 , SWAP8 , SWAP9 , SWAP10 , SWAP11 , SWAP12 , SWAP13 , SWAP14 , SWAP15 , SWAP16 :
863864 n := int (op - SWAP1 + 2 )
864865 stack .require (n )
866+ gas .Set (GasFastestStep )
865867 case DUP1 , DUP2 , DUP3 , DUP4 , DUP5 , DUP6 , DUP7 , DUP8 , DUP9 , DUP10 , DUP11 , DUP12 , DUP13 , DUP14 , DUP15 , DUP16 :
866868 n := int (op - DUP1 + 1 )
867869 stack .require (n )
870+ gas .Set (GasFastestStep )
868871 case LOG0 , LOG1 , LOG2 , LOG3 , LOG4 :
869872 n := int (op - LOG0 )
870873 stack .require (n + 2 )
@@ -873,11 +876,11 @@ func (self *Vm) calculateGasAndSize(context *Context, caller ContextRef, op OpCo
873876
874877 gas .Add (gas , GasLogBase )
875878 gas .Add (gas , new (big.Int ).Mul (big .NewInt (int64 (n )), GasLogTopic ))
876- gas .Add (gas , new (big.Int ).Mul (mSize , GasLogData ))
879+ gas .Add (gas , new (big.Int ).Mul (mSize , GasLogByte ))
877880
878881 newMemSize = calcMemSize (mStart , mSize )
879882 case EXP :
880- gas .Add (gas , big .NewInt (int64 (len (stack .data [stack .Len ()- 2 ].Bytes ())+ 1 ) ))
883+ gas .Add (gas , new ( big.Int ). Mul ( big . NewInt (int64 (len (stack .data [stack .Len ()- 2 ].Bytes ()))), GasExpByte ))
881884 case SSTORE :
882885 stack .require (2 )
883886
@@ -935,7 +938,7 @@ func (self *Vm) calculateGasAndSize(context *Context, caller ContextRef, op OpCo
935938 gas .Add (gas , GasCallNewAccount )
936939 }
937940
938- if len (stack .data [stack .Len ()].Bytes ()) > 0 {
941+ if len (stack .data [stack .Len ()- 1 ].Bytes ()) > 0 {
939942 gas .Add (gas , GasCallValueTransfer )
940943 }
941944 }
@@ -953,11 +956,24 @@ func (self *Vm) calculateGasAndSize(context *Context, caller ContextRef, op OpCo
953956 newMemSize .Mul (newMemSize , u256 (32 ))
954957
955958 if newMemSize .Cmp (u256 (int64 (mem .Len ()))) > 0 {
956- memGasUsage := new (big.Int ).Sub (newMemSize , u256 (int64 (mem .Len ())))
957- memGasUsage .Mul (GasMemWord , memGasUsage )
958- memGasUsage .Div (memGasUsage , u256 (32 ))
959-
960- gas .Add (gas , memGasUsage )
959+ //memGasUsage := new(big.Int).Sub(newMemSize, u256(int64(mem.Len())))
960+ //memGasUsage.Mul(GasMemWord, memGasUsage)
961+ //memGasUsage.Div(memGasUsage, u256(32))
962+
963+ //Old: full_memory_gas_cost = W + floor(W*W / 1024), W = words in memory
964+ oldSize := toWordSize (big .NewInt (int64 (mem .Len ())))
965+ linCoef := new (big.Int ).Mul (oldSize , GasMemWord )
966+ pow := new (big.Int )
967+ pow .Exp (oldSize , ethutil .Big2 , Zero )
968+ quadCoef := new (big.Int ).Div (pow , GasQuadCoeffDenom )
969+ oldTotalFee := new (big.Int ).Add (linCoef , quadCoef )
970+
971+ linCoef = new (big.Int ).Mul (newMemSize , GasMemWord )
972+ pow .Exp (newMemSize , ethutil .Big2 , Zero )
973+ quadCoef = new (big.Int ).Div (pow , GasQuadCoeffDenom )
974+ newTotalFee := new (big.Int ).Add (linCoef , quadCoef )
975+
976+ gas .Add (gas , new (big.Int ).Sub (newTotalFee , oldTotalFee ))
961977 }
962978
963979 }
0 commit comments