common.go 2.7 KB

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