common.go 3.4 KB

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