common.go 2.68 KB
Newer Older
obscuren's avatar
obscuren committed
1
package vm
2 3

import (
4 5
	"math/big"

6
	"github.com/ethereum/go-ethereum/ethutil"
obscuren's avatar
obscuren committed
7
	"github.com/ethereum/go-ethereum/logger"
8 9
)

obscuren's avatar
obscuren committed
10
var vmlogger = logger.NewLogger("VM")
11

12 13 14
// Global Debug flag indicating Debug VM (full logging)
var Debug bool

obscuren's avatar
obscuren committed
15
type Type byte
16 17

const (
obscuren's avatar
obscuren committed
18
	StdVmTy Type = iota
19
	JitVmTy
20 21 22 23

	MaxVmTy
)

24 25 26 27 28 29 30 31 32 33 34 35
func NewVm(env Environment) VirtualMachine {
	switch env.VmType() {
	case JitVmTy:
		return NewJitVm(env)
	default:
		vmlogger.Infoln("unsupported vm type %d", env.VmType())
		fallthrough
	case StdVmTy:
		return New(env)
	}
}

36
var (
obscuren's avatar
obscuren committed
37 38 39 40 41 42 43 44 45 46
	GasQuickStep   = big.NewInt(2)
	GasFastestStep = big.NewInt(3)
	GasFastStep    = big.NewInt(5)
	GasMidStep     = big.NewInt(8)
	GasSlowStep    = big.NewInt(10)
	GasExtStep     = big.NewInt(20)

	GasStorageGet        = big.NewInt(50)
	GasStorageAdd        = big.NewInt(20000)
	GasStorageMod        = big.NewInt(5000)
obscuren's avatar
obscuren committed
47 48
	GasLogBase           = big.NewInt(375)
	GasLogTopic          = big.NewInt(375)
obscuren's avatar
obscuren committed
49
	GasLogByte           = big.NewInt(8)
obscuren's avatar
obscuren committed
50
	GasCreate            = big.NewInt(32000)
obscuren's avatar
obscuren committed
51
	GasCreateByte        = big.NewInt(200)
obscuren's avatar
obscuren committed
52
	GasCall              = big.NewInt(40)
obscuren's avatar
obscuren committed
53 54
	GasCallValueTransfer = big.NewInt(9000)
	GasStipend           = big.NewInt(2300)
obscuren's avatar
obscuren committed
55 56 57 58 59 60 61 62 63
	GasCallNewAccount    = big.NewInt(25000)
	GasReturn            = big.NewInt(0)
	GasStop              = big.NewInt(0)
	GasJumpDest          = big.NewInt(1)

	RefundStorage = big.NewInt(15000)
	RefundSuicide = big.NewInt(24000)

	GasMemWord           = big.NewInt(3)
obscuren's avatar
obscuren committed
64
	GasQuadCoeffDenom    = big.NewInt(512)
obscuren's avatar
obscuren committed
65 66
	GasContractByte      = big.NewInt(200)
	GasTransaction       = big.NewInt(21000)
obscuren's avatar
obscuren committed
67 68 69
	GasTxDataNonzeroByte = big.NewInt(68)
	GasTxDataZeroByte    = big.NewInt(4)
	GasTx                = big.NewInt(21000)
obscuren's avatar
obscuren committed
70 71
	GasExp               = big.NewInt(10)
	GasExpByte           = big.NewInt(10)
obscuren's avatar
obscuren committed
72 73 74 75 76 77 78 79 80 81 82

	GasSha3Base     = big.NewInt(30)
	GasSha3Word     = big.NewInt(6)
	GasSha256Base   = big.NewInt(60)
	GasSha256Word   = big.NewInt(12)
	GasRipemdBase   = big.NewInt(600)
	GasRipemdWord   = big.NewInt(12)
	GasEcrecover    = big.NewInt(3000)
	GasIdentityBase = big.NewInt(15)
	GasIdentityWord = big.NewInt(3)
	GasCopyWord     = big.NewInt(3)
83 84 85 86 87

	Pow256 = ethutil.BigPow(2, 256)

	LogTyPretty byte = 0x1
	LogTyDiff   byte = 0x2
88

obscuren's avatar
obscuren committed
89 90
	U256 = ethutil.U256
	S256 = ethutil.S256
obscuren's avatar
obscuren committed
91 92

	Zero = ethutil.Big0
93
)
94

obscuren's avatar
obscuren committed
95
const MaxCallDepth = 1025
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119

func calcMemSize(off, l *big.Int) *big.Int {
	if l.Cmp(ethutil.Big0) == 0 {
		return ethutil.Big0
	}

	return new(big.Int).Add(off, l)
}

// Simple helper
func u256(n int64) *big.Int {
	return big.NewInt(n)
}

// Mainly used for print variables and passing to Print*
func toValue(val *big.Int) interface{} {
	// Let's assume a string on right padded zero's
	b := val.Bytes()
	if b[0] != 0 && b[len(b)-1] == 0x0 && b[len(b)-2] == 0x0 {
		return string(b)
	}

	return val
}