common.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. package vm
  2. import (
  3. "math"
  4. "math/big"
  5. "github.com/ethereum/go-ethereum/ethutil"
  6. "github.com/ethereum/go-ethereum/logger"
  7. )
  8. var vmlogger = logger.NewLogger("VM")
  9. // Global Debug flag indicating Debug VM (full logging)
  10. var Debug bool
  11. type Type byte
  12. const (
  13. StdVmTy Type = iota
  14. JitVmTy
  15. MaxVmTy
  16. )
  17. func NewVm(env Environment) VirtualMachine {
  18. switch env.VmType() {
  19. case JitVmTy:
  20. return NewJitVm(env)
  21. default:
  22. vmlogger.Infoln("unsupported vm type %d", env.VmType())
  23. fallthrough
  24. case StdVmTy:
  25. return New(env)
  26. }
  27. }
  28. var (
  29. GasQuickStep = big.NewInt(2)
  30. GasFastestStep = big.NewInt(3)
  31. GasFastStep = big.NewInt(5)
  32. GasMidStep = big.NewInt(8)
  33. GasSlowStep = big.NewInt(10)
  34. GasExtStep = big.NewInt(20)
  35. GasStorageGet = big.NewInt(50)
  36. GasStorageAdd = big.NewInt(20000)
  37. GasStorageMod = big.NewInt(5000)
  38. GasLogBase = big.NewInt(375)
  39. GasLogTopic = big.NewInt(375)
  40. GasLogByte = big.NewInt(8)
  41. GasCreate = big.NewInt(32000)
  42. GasCreateByte = big.NewInt(200)
  43. GasCall = big.NewInt(40)
  44. GasCallValueTransfer = big.NewInt(9000)
  45. GasStipend = big.NewInt(2300)
  46. GasCallNewAccount = big.NewInt(25000)
  47. GasReturn = big.NewInt(0)
  48. GasStop = big.NewInt(0)
  49. GasJumpDest = big.NewInt(1)
  50. RefundStorage = big.NewInt(15000)
  51. RefundSuicide = big.NewInt(24000)
  52. GasMemWord = big.NewInt(3)
  53. GasQuadCoeffDenom = big.NewInt(512)
  54. GasContractByte = big.NewInt(200)
  55. GasTransaction = big.NewInt(21000)
  56. GasTxDataNonzeroByte = big.NewInt(68)
  57. GasTxDataZeroByte = big.NewInt(4)
  58. GasTx = big.NewInt(21000)
  59. GasExp = big.NewInt(10)
  60. GasExpByte = big.NewInt(10)
  61. GasSha3Base = big.NewInt(30)
  62. GasSha3Word = big.NewInt(6)
  63. GasSha256Base = big.NewInt(60)
  64. GasSha256Word = big.NewInt(12)
  65. GasRipemdBase = big.NewInt(600)
  66. GasRipemdWord = big.NewInt(12)
  67. GasEcrecover = big.NewInt(3000)
  68. GasIdentityBase = big.NewInt(15)
  69. GasIdentityWord = big.NewInt(3)
  70. GasCopyWord = big.NewInt(3)
  71. Pow256 = ethutil.BigPow(2, 256)
  72. LogTyPretty byte = 0x1
  73. LogTyDiff byte = 0x2
  74. U256 = ethutil.U256
  75. S256 = ethutil.S256
  76. Zero = ethutil.Big0
  77. )
  78. const MaxCallDepth = 1025
  79. func calcMemSize(off, l *big.Int) *big.Int {
  80. if l.Cmp(ethutil.Big0) == 0 {
  81. return ethutil.Big0
  82. }
  83. return new(big.Int).Add(off, l)
  84. }
  85. // Simple helper
  86. func u256(n int64) *big.Int {
  87. return big.NewInt(n)
  88. }
  89. // Mainly used for print variables and passing to Print*
  90. func toValue(val *big.Int) interface{} {
  91. // Let's assume a string on right padded zero's
  92. b := val.Bytes()
  93. if b[0] != 0 && b[len(b)-1] == 0x0 && b[len(b)-2] == 0x0 {
  94. return string(b)
  95. }
  96. return val
  97. }
  98. func getCode(code []byte, start, size uint64) []byte {
  99. x := uint64(math.Min(float64(start), float64(len(code))))
  100. y := uint64(math.Min(float64(x+size), float64(len(code))))
  101. return ethutil.RightPadBytes(code[x:y], int(size))
  102. }