common.go 3.9 KB

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