github.com/digdeepmining/go-atheios@v1.5.13-0.20180902133602-d5687a2e6f43/core/vm/common.go (about) 1 // Copyright 2014 The go-ethereum Authors 2 // This file is part of the go-ethereum library. 3 // 4 // The go-ethereum library is free software: you can redistribute it and/or modify 5 // it under the terms of the GNU Lesser General Public License as published by 6 // the Free Software Foundation, either version 3 of the License, or 7 // (at your option) any later version. 8 // 9 // The go-ethereum library is distributed in the hope that it will be useful, 10 // but WITHOUT ANY WARRANTY; without even the implied warranty of 11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 // GNU Lesser General Public License for more details. 13 // 14 // You should have received a copy of the GNU Lesser General Public License 15 // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>. 16 17 package vm 18 19 import ( 20 "math" 21 "math/big" 22 23 "github.com/atheioschain/go-atheios/common" 24 "github.com/atheioschain/go-atheios/params" 25 ) 26 27 // Type is the VM type accepted by **NewVm** 28 type Type byte 29 30 const ( 31 StdVmTy Type = iota // Default standard VM 32 JitVmTy // LLVM JIT VM 33 MaxVmTy 34 ) 35 36 var ( 37 Pow256 = common.BigPow(2, 256) // Pow256 is 2**256 38 39 U256 = common.U256 // Shortcut to common.U256 40 S256 = common.S256 // Shortcut to common.S256 41 42 Zero = common.Big0 // Shortcut to common.Big0 43 One = common.Big1 // Shortcut to common.Big1 44 45 max = big.NewInt(math.MaxInt64) // Maximum 64 bit integer 46 ) 47 48 // calculates the memory size required for a step 49 func calcMemSize(off, l *big.Int) *big.Int { 50 if l.Cmp(common.Big0) == 0 { 51 return common.Big0 52 } 53 54 return new(big.Int).Add(off, l) 55 } 56 57 // calculates the quadratic gas 58 func quadMemGas(mem *Memory, newMemSize, gas *big.Int) { 59 if newMemSize.Cmp(common.Big0) > 0 { 60 newMemSizeWords := toWordSize(newMemSize) 61 newMemSize.Mul(newMemSizeWords, u256(32)) 62 63 if newMemSize.Cmp(u256(int64(mem.Len()))) > 0 { 64 // be careful reusing variables here when changing. 65 // The order has been optimised to reduce allocation 66 oldSize := toWordSize(big.NewInt(int64(mem.Len()))) 67 pow := new(big.Int).Exp(oldSize, common.Big2, Zero) 68 linCoef := oldSize.Mul(oldSize, params.MemoryGas) 69 quadCoef := new(big.Int).Div(pow, params.QuadCoeffDiv) 70 oldTotalFee := new(big.Int).Add(linCoef, quadCoef) 71 72 pow.Exp(newMemSizeWords, common.Big2, Zero) 73 linCoef = linCoef.Mul(newMemSizeWords, params.MemoryGas) 74 quadCoef = quadCoef.Div(pow, params.QuadCoeffDiv) 75 newTotalFee := linCoef.Add(linCoef, quadCoef) 76 77 fee := newTotalFee.Sub(newTotalFee, oldTotalFee) 78 gas.Add(gas, fee) 79 } 80 } 81 } 82 83 // Simple helper 84 func u256(n int64) *big.Int { 85 return big.NewInt(n) 86 } 87 88 // Mainly used for print variables and passing to Print* 89 func toValue(val *big.Int) interface{} { 90 // Let's assume a string on right padded zero's 91 b := val.Bytes() 92 if b[0] != 0 && b[len(b)-1] == 0x0 && b[len(b)-2] == 0x0 { 93 return string(b) 94 } 95 96 return val 97 } 98 99 // getData returns a slice from the data based on the start and size and pads 100 // up to size with zero's. This function is overflow safe. 101 func getData(data []byte, start, size *big.Int) []byte { 102 dlen := big.NewInt(int64(len(data))) 103 104 s := common.BigMin(start, dlen) 105 e := common.BigMin(new(big.Int).Add(s, size), dlen) 106 return common.RightPadBytes(data[s.Uint64():e.Uint64()], int(size.Uint64())) 107 } 108 109 // useGas attempts to subtract the amount of gas and returns whether it was 110 // successful 111 func useGas(gas, amount *big.Int) bool { 112 if gas.Cmp(amount) < 0 { 113 return false 114 } 115 116 // Sub the amount of gas from the remaining 117 gas.Sub(gas, amount) 118 return true 119 }