common.go 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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. )
  23. // Global Debug flag indicating Debug VM (full logging)
  24. var Debug bool
  25. type Type byte
  26. const (
  27. StdVmTy Type = iota
  28. JitVmTy
  29. MaxVmTy
  30. LogTyPretty byte = 0x1
  31. LogTyDiff byte = 0x2
  32. )
  33. var (
  34. Pow256 = common.BigPow(2, 256)
  35. U256 = common.U256
  36. S256 = common.S256
  37. Zero = common.Big0
  38. One = common.Big1
  39. max = big.NewInt(math.MaxInt64)
  40. )
  41. func NewVm(env Environment) VirtualMachine {
  42. switch env.VmType() {
  43. case JitVmTy:
  44. return NewJitVm(env)
  45. default:
  46. glog.V(0).Infoln("unsupported vm type %d", env.VmType())
  47. fallthrough
  48. case StdVmTy:
  49. return New(env)
  50. }
  51. }
  52. func calcMemSize(off, l *big.Int) *big.Int {
  53. if l.Cmp(common.Big0) == 0 {
  54. return common.Big0
  55. }
  56. return new(big.Int).Add(off, l)
  57. }
  58. // Simple helper
  59. func u256(n int64) *big.Int {
  60. return big.NewInt(n)
  61. }
  62. // Mainly used for print variables and passing to Print*
  63. func toValue(val *big.Int) interface{} {
  64. // Let's assume a string on right padded zero's
  65. b := val.Bytes()
  66. if b[0] != 0 && b[len(b)-1] == 0x0 && b[len(b)-2] == 0x0 {
  67. return string(b)
  68. }
  69. return val
  70. }
  71. func getData(data []byte, start, size *big.Int) []byte {
  72. dlen := big.NewInt(int64(len(data)))
  73. s := common.BigMin(start, dlen)
  74. e := common.BigMin(new(big.Int).Add(s, size), dlen)
  75. return common.RightPadBytes(data[s.Uint64():e.Uint64()], int(size.Uint64()))
  76. }
  77. func UseGas(gas, amount *big.Int) bool {
  78. if gas.Cmp(amount) < 0 {
  79. return false
  80. }
  81. // Sub the amount of gas from the remaining
  82. gas.Sub(gas, amount)
  83. return true
  84. }