github.com/ethereumproject/go-ethereum@v5.5.2+incompatible/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/big"
    21  
    22  	"github.com/ethereumproject/go-ethereum/common"
    23  )
    24  
    25  // Type is the VM type accepted by **NewVm**
    26  type Type byte
    27  
    28  const (
    29  	StdVmTy Type = iota // Default standard VM
    30  	JitVmTy             // LLVM JIT VM
    31  	MaxVmTy
    32  )
    33  
    34  var (
    35  	Pow256 = common.BigPow(2, 256) // Pow256 is 2**256
    36  
    37  	U256 = common.U256 // Shortcut to common.U256
    38  	S256 = common.S256 // Shortcut to common.S256
    39  
    40  	One = common.Big1 // Shortcut to common.Big1
    41  )
    42  
    43  // calculates the memory size required for a step
    44  func calcMemSize(off, l *big.Int) *big.Int {
    45  	if l.Sign() == 0 {
    46  		return new(big.Int)
    47  	}
    48  
    49  	return new(big.Int).Add(off, l)
    50  }
    51  
    52  // calculates the quadratic gas
    53  func quadMemGas(mem *Memory, newMemSize, gas *big.Int) {
    54  	if newMemSize.Sign() > 0 {
    55  		newMemSizeWords := toWordSize(newMemSize)
    56  		newMemSize.Mul(newMemSizeWords, u256(32))
    57  
    58  		if newMemSize.Cmp(u256(int64(mem.Len()))) > 0 {
    59  			// be careful reusing variables here when changing.
    60  			// The order has been optimised to reduce allocation
    61  			oldSize := toWordSize(big.NewInt(int64(mem.Len())))
    62  			pow := new(big.Int).Exp(oldSize, common.Big2, new(big.Int))
    63  			linCoef := oldSize.Mul(oldSize, big.NewInt(3))
    64  			quadCoef := new(big.Int).Div(pow, big.NewInt(512))
    65  			oldTotalFee := new(big.Int).Add(linCoef, quadCoef)
    66  
    67  			pow.Exp(newMemSizeWords, common.Big2, new(big.Int))
    68  			linCoef = linCoef.Mul(newMemSizeWords, big.NewInt(3))
    69  			quadCoef = quadCoef.Div(pow, big.NewInt(512))
    70  			newTotalFee := linCoef.Add(linCoef, quadCoef)
    71  
    72  			fee := newTotalFee.Sub(newTotalFee, oldTotalFee)
    73  			gas.Add(gas, fee)
    74  		}
    75  	}
    76  }
    77  
    78  // Simple helper
    79  func u256(n int64) *big.Int {
    80  	return big.NewInt(n)
    81  }
    82  
    83  // getData returns a slice from the data based on the start and size and pads
    84  // up to size with zero's. This function is overflow safe.
    85  func getData(data []byte, start, size *big.Int) []byte {
    86  	dlen := big.NewInt(int64(len(data)))
    87  
    88  	s := common.BigMin(start, dlen)
    89  	e := common.BigMin(new(big.Int).Add(s, size), dlen)
    90  	return common.RightPadBytes(data[s.Uint64():e.Uint64()], int(size.Uint64()))
    91  }
    92  
    93  // useGas attempts to subtract the amount of gas and returns whether it was
    94  // successful
    95  func useGas(gas, amount *big.Int) bool {
    96  	if gas.Cmp(amount) < 0 {
    97  		return false
    98  	}
    99  
   100  	// Sub the amount of gas from the remaining
   101  	gas.Sub(gas, amount)
   102  	return true
   103  }