Skip to content

Commit c1ef19b

Browse files
committed
quad mem
1 parent fe7ab66 commit c1ef19b

4 files changed

Lines changed: 54 additions & 17 deletions

File tree

‎tests/helper/vm.go‎

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ type Env struct {
2828
gasLimit *big.Int
2929

3030
logs state.Logs
31+
32+
vmTest bool
3133
}
3234

3335
func NewEnv(state *state.StateDB) *Env {
@@ -92,20 +94,38 @@ func (self *Env) vm(addr, data []byte, gas, price, value *big.Int) *core.Executi
9294
}
9395

9496
func (self *Env) Call(caller vm.ContextRef, addr, data []byte, gas, price, value *big.Int) ([]byte, error) {
97+
if self.vmTest && self.depth > 0 {
98+
caller.ReturnGas(gas, price)
99+
100+
return nil, nil
101+
}
95102
exe := self.vm(addr, data, gas, price, value)
96103
ret, err := exe.Call(addr, caller)
97104
self.Gas = exe.Gas
98105

99106
return ret, err
107+
100108
}
101109
func (self *Env) CallCode(caller vm.ContextRef, addr, data []byte, gas, price, value *big.Int) ([]byte, error) {
110+
if self.vmTest && self.depth > 0 {
111+
caller.ReturnGas(gas, price)
112+
}
102113
exe := self.vm(caller.Address(), data, gas, price, value)
103114
return exe.Call(addr, caller)
104115
}
105116

106117
func (self *Env) Create(caller vm.ContextRef, addr, data []byte, gas, price, value *big.Int) ([]byte, error, vm.ContextRef) {
107118
exe := self.vm(addr, data, gas, price, value)
108-
return exe.Create(caller)
119+
if self.vmTest {
120+
caller.ReturnGas(gas, price)
121+
122+
nonce := self.state.GetNonce(caller.Address())
123+
obj := self.state.GetOrNewStateObject(crypto.CreateAddress(caller.Address(), nonce))
124+
125+
return nil, nil, obj
126+
} else {
127+
return exe.Create(caller)
128+
}
109129
}
110130

111131
func RunVm(state *state.StateDB, env, exec map[string]string) ([]byte, state.Logs, *big.Int, error) {
@@ -123,6 +143,7 @@ func RunVm(state *state.StateDB, env, exec map[string]string) ([]byte, state.Log
123143
caller := state.GetOrNewStateObject(from)
124144

125145
vmenv := NewEnvFromMap(state, env, exec)
146+
vmenv.vmTest = true
126147
vmenv.skipTransfer = true
127148
vmenv.initial = true
128149
ret, err := vmenv.Call(caller, to, data, gas, price, value)

‎tests/vm/gh_test.go‎

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -79,12 +79,10 @@ func RunVmTest(p string, t *testing.T) {
7979
helper.CreateFileTests(t, p, &tests)
8080

8181
for name, test := range tests {
82-
/*
83-
helper.Logger.SetLogLevel(4)
84-
if name != "log1_nonEmptyMem_logMemSize1_logMemStart31" {
85-
continue
86-
}
87-
*/
82+
helper.Logger.SetLogLevel(4)
83+
if name != "env1" {
84+
continue
85+
}
8886
db, _ := ethdb.NewMemDatabase()
8987
statedb := state.New(nil, db)
9088
for addr, account := range test.Pre {

‎vm/common.go‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ var (
4343
GasStorageMod = big.NewInt(5000)
4444
GasLogBase = big.NewInt(2000)
4545
GasLogTopic = big.NewInt(2000)
46-
GasLogData = big.NewInt(8)
46+
GasLogByte = big.NewInt(8)
4747
GasCreate = big.NewInt(32000)
4848
GasCreateByte = big.NewInt(300)
4949
GasCall = big.NewInt(40)
@@ -57,11 +57,13 @@ var (
5757
RefundSuicide = big.NewInt(24000)
5858

5959
GasMemWord = big.NewInt(3)
60-
GasQuadCoeffWord = big.NewInt(1)
60+
GasQuadCoeffDenom = big.NewInt(512)
6161
GasContractByte = big.NewInt(200)
6262
GasTransaction = big.NewInt(21000)
6363
GasTxDataNonzeroByte = big.NewInt(37)
6464
GasTxZeroByte = big.NewInt(2)
65+
GasExp = big.NewInt(10)
66+
GasExpByte = big.NewInt(10)
6567

6668
GasSha3Base = big.NewInt(30)
6769
GasSha3Word = big.NewInt(6)

‎vm/vm.go‎

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)