common.go 3.8 KB

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