common.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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. // Global Debug flag indicating Debug VM (full logging)
  24. var Debug bool
  25. var GenerateStructLogs bool = false
  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. // calculates the memory size required for a step
  42. func calcMemSize(off, l *big.Int) *big.Int {
  43. if l.Cmp(common.Big0) == 0 {
  44. return common.Big0
  45. }
  46. return new(big.Int).Add(off, l)
  47. }
  48. // calculates the quadratic gas
  49. func quadMemGas(mem *Memory, newMemSize, gas *big.Int) {
  50. if newMemSize.Cmp(common.Big0) > 0 {
  51. newMemSizeWords := toWordSize(newMemSize)
  52. newMemSize.Mul(newMemSizeWords, u256(32))
  53. if newMemSize.Cmp(u256(int64(mem.Len()))) > 0 {
  54. // be careful reusing variables here when changing.
  55. // The order has been optimised to reduce allocation
  56. oldSize := toWordSize(big.NewInt(int64(mem.Len())))
  57. pow := new(big.Int).Exp(oldSize, common.Big2, Zero)
  58. linCoef := oldSize.Mul(oldSize, params.MemoryGas)
  59. quadCoef := new(big.Int).Div(pow, params.QuadCoeffDiv)
  60. oldTotalFee := new(big.Int).Add(linCoef, quadCoef)
  61. pow.Exp(newMemSizeWords, common.Big2, Zero)
  62. linCoef = linCoef.Mul(newMemSizeWords, params.MemoryGas)
  63. quadCoef = quadCoef.Div(pow, params.QuadCoeffDiv)
  64. newTotalFee := linCoef.Add(linCoef, quadCoef)
  65. fee := newTotalFee.Sub(newTotalFee, oldTotalFee)
  66. gas.Add(gas, fee)
  67. }
  68. }
  69. }
  70. // Simple helper
  71. func u256(n int64) *big.Int {
  72. return big.NewInt(n)
  73. }
  74. // Mainly used for print variables and passing to Print*
  75. func toValue(val *big.Int) interface{} {
  76. // Let's assume a string on right padded zero's
  77. b := val.Bytes()
  78. if b[0] != 0 && b[len(b)-1] == 0x0 && b[len(b)-2] == 0x0 {
  79. return string(b)
  80. }
  81. return val
  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. s := common.BigMin(start, dlen)
  88. e := common.BigMin(new(big.Int).Add(s, size), dlen)
  89. return common.RightPadBytes(data[s.Uint64():e.Uint64()], int(size.Uint64()))
  90. }
  91. // useGas attempts to subtract the amount of gas and returns whether it was
  92. // successful
  93. func useGas(gas, amount *big.Int) bool {
  94. if gas.Cmp(amount) < 0 {
  95. return false
  96. }
  97. // Sub the amount of gas from the remaining
  98. gas.Sub(gas, amount)
  99. return true
  100. }