diff options
author | obscuren <geffobscura@gmail.com> | 2014-03-24 20:20:34 +0800 |
---|---|---|
committer | obscuren <geffobscura@gmail.com> | 2014-03-24 20:20:34 +0800 |
commit | e0b6091d7ef709902f534c1a4b57151f0171e03c (patch) | |
tree | 290fa26d9da264a5e365816a80c42f325adb51ef /ethchain/vm.go | |
parent | 6a86c517c4f4b372cad0ae1d92e926a482eac5ba (diff) | |
download | go-tangerine-e0b6091d7ef709902f534c1a4b57151f0171e03c.tar go-tangerine-e0b6091d7ef709902f534c1a4b57151f0171e03c.tar.gz go-tangerine-e0b6091d7ef709902f534c1a4b57151f0171e03c.tar.bz2 go-tangerine-e0b6091d7ef709902f534c1a4b57151f0171e03c.tar.lz go-tangerine-e0b6091d7ef709902f534c1a4b57151f0171e03c.tar.xz go-tangerine-e0b6091d7ef709902f534c1a4b57151f0171e03c.tar.zst go-tangerine-e0b6091d7ef709902f534c1a4b57151f0171e03c.zip |
Test fixes and removed old code. Added VM gas fees
Diffstat (limited to 'ethchain/vm.go')
-rw-r--r-- | ethchain/vm.go | 48 |
1 files changed, 45 insertions, 3 deletions
diff --git a/ethchain/vm.go b/ethchain/vm.go index 126592b25..9b6807925 100644 --- a/ethchain/vm.go +++ b/ethchain/vm.go @@ -10,6 +10,17 @@ import ( "math/big" ) +var ( + GasStep = big.NewInt(1) + GasSha = big.NewInt(20) + GasSLoad = big.NewInt(20) + GasSStore = big.NewInt(100) + GasBalance = big.NewInt(20) + GasCreate = big.NewInt(100) + GasCall = big.NewInt(20) + GasMemory = big.NewInt(1) +) + type Vm struct { txPool *TxPool // Stack for processing contracts @@ -70,10 +81,41 @@ func (vm *Vm) RunClosure(closure *Closure) []byte { } // TODO Get each instruction cost properly - fee := new(big.Int) - fee.Add(fee, big.NewInt(1000)) + gas := new(big.Int) + useGas := func(amount *big.Int) { + gas.Add(gas, amount) + } + + switch op { + case oSHA3: + useGas(GasSha) + case oSLOAD: + useGas(GasSLoad) + case oSSTORE: + var mult *big.Int + y, x := stack.Peekn() + val := closure.GetMem(x) + if val.IsEmpty() && len(y.Bytes()) > 0 { + mult = ethutil.Big2 + } else if !val.IsEmpty() && len(y.Bytes()) == 0 { + mult = ethutil.Big0 + } else { + mult = ethutil.Big1 + } + useGas(base.Mul(mult, GasSStore)) + case oBALANCE: + useGas(GasBalance) + case oCREATE: + useGas(GasCreate) + case oCALL: + useGas(GasCall) + case oMLOAD, oMSIZE, oMSTORE8, oMSTORE: + useGas(GasMemory) + default: + useGas(GasStep) + } - if closure.Gas.Cmp(fee) < 0 { + if closure.Gas.Cmp(gas) < 0 { return closure.Return(nil) } |