common.go 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. package vm
  2. import (
  3. "math"
  4. "math/big"
  5. "github.com/ethereum/go-ethereum/common"
  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. LogTyPretty byte = 0x1
  17. LogTyDiff byte = 0x2
  18. )
  19. var (
  20. Pow256 = common.BigPow(2, 256)
  21. U256 = common.U256
  22. S256 = common.S256
  23. Zero = common.Big0
  24. One = common.Big1
  25. max = big.NewInt(math.MaxInt64)
  26. )
  27. func NewVm(env Environment) VirtualMachine {
  28. switch env.VmType() {
  29. case JitVmTy:
  30. return NewJitVm(env)
  31. default:
  32. vmlogger.Infoln("unsupported vm type %d", env.VmType())
  33. fallthrough
  34. case StdVmTy:
  35. return New(env)
  36. }
  37. }
  38. func calcMemSize(off, l *big.Int) *big.Int {
  39. if l.Cmp(common.Big0) == 0 {
  40. return common.Big0
  41. }
  42. return new(big.Int).Add(off, l)
  43. }
  44. // Simple helper
  45. func u256(n int64) *big.Int {
  46. return big.NewInt(n)
  47. }
  48. // Mainly used for print variables and passing to Print*
  49. func toValue(val *big.Int) interface{} {
  50. // Let's assume a string on right padded zero's
  51. b := val.Bytes()
  52. if b[0] != 0 && b[len(b)-1] == 0x0 && b[len(b)-2] == 0x0 {
  53. return string(b)
  54. }
  55. return val
  56. }
  57. func getData(data []byte, start, size *big.Int) []byte {
  58. dlen := big.NewInt(int64(len(data)))
  59. s := common.BigMin(start, dlen)
  60. e := common.BigMin(new(big.Int).Add(s, size), dlen)
  61. return common.RightPadBytes(data[s.Uint64():e.Uint64()], int(size.Uint64()))
  62. }
  63. func UseGas(gas, amount *big.Int) bool {
  64. if gas.Cmp(amount) < 0 {
  65. return false
  66. }
  67. // Sub the amount of gas from the remaining
  68. gas.Sub(gas, amount)
  69. return true
  70. }