common.go 1.73 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

obscuren's avatar
obscuren committed
12
type Type byte
13 14

const (
obscuren's avatar
obscuren committed
15
	StdVmTy Type = iota
16
	JitVmTy
17 18 19 20

	MaxVmTy
)

21 22 23 24 25 26 27 28 29 30 31 32
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)
	}
}

33
var (
34
	GasStep         = big.NewInt(1)
35
	GasSha          = big.NewInt(10)
36 37 38 39 40 41
	GasSLoad        = big.NewInt(20)
	GasSStore       = big.NewInt(100)
	GasSStoreRefund = big.NewInt(100)
	GasBalance      = big.NewInt(20)
	GasCreate       = big.NewInt(100)
	GasCall         = big.NewInt(20)
obscuren's avatar
obscuren committed
42 43 44 45
	GasCreateByte   = big.NewInt(5)
	GasSha3Byte     = big.NewInt(10)
	GasSha256Byte   = big.NewInt(50)
	GasRipemdByte   = big.NewInt(50)
46 47 48 49
	GasMemory       = big.NewInt(1)
	GasData         = big.NewInt(5)
	GasTx           = big.NewInt(500)
	GasLog          = big.NewInt(32)
obscuren's avatar
obscuren committed
50 51
	GasSha256       = big.NewInt(50)
	GasRipemd       = big.NewInt(50)
obscuren's avatar
obscuren committed
52
	GasEcrecover    = big.NewInt(500)
53
	GasMemCpy       = big.NewInt(1)
54 55 56 57 58

	Pow256 = ethutil.BigPow(2, 256)

	LogTyPretty byte = 0x1
	LogTyDiff   byte = 0x2
59

obscuren's avatar
obscuren committed
60 61
	U256 = ethutil.U256
	S256 = ethutil.S256
62
)
63

obscuren's avatar
obscuren committed
64
const MaxCallDepth = 1025
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88

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
}