common.go 3.83 KB
Newer Older
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 6 7 8
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
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 13 14
// GNU Lesser General Public License for more details.
//
// 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

obscuren's avatar
obscuren committed
17
package vm
18 19

import (
20
	"math"
21 22
	"math/big"

obscuren's avatar
obscuren committed
23
	"github.com/ethereum/go-ethereum/common"
obscuren's avatar
obscuren committed
24
	"github.com/ethereum/go-ethereum/logger/glog"
25
	"github.com/ethereum/go-ethereum/params"
26 27
)

28 29 30
// Global Debug flag indicating Debug VM (full logging)
var Debug bool

31
// Type is the VM type accepted by **NewVm**
obscuren's avatar
obscuren committed
32
type Type byte
33 34

const (
35 36
	StdVmTy Type = iota // Default standard VM
	JitVmTy             // LLVM JIT VM
37
	MaxVmTy
obscuren's avatar
obscuren committed
38 39 40
)

var (
41
	Pow256 = common.BigPow(2, 256) // Pow256 is 2**256
obscuren's avatar
obscuren committed
42

43 44
	U256 = common.U256 // Shortcut to common.U256
	S256 = common.S256 // Shortcut to common.S256
obscuren's avatar
obscuren committed
45

46 47
	Zero = common.Big0 // Shortcut to common.Big0
	One  = common.Big1 // Shortcut to common.Big1
obscuren's avatar
obscuren committed
48

49
	max = big.NewInt(math.MaxInt64) // Maximum 64 bit integer
50 51
)

52
// NewVm returns a new VM based on the Environment
53 54 55 56 57
func NewVm(env Environment) VirtualMachine {
	switch env.VmType() {
	case JitVmTy:
		return NewJitVm(env)
	default:
obscuren's avatar
obscuren committed
58
		glog.V(0).Infoln("unsupported vm type %d", env.VmType())
59 60 61 62 63 64
		fallthrough
	case StdVmTy:
		return New(env)
	}
}

65
// calculates the memory size required for a step
66
func calcMemSize(off, l *big.Int) *big.Int {
obscuren's avatar
obscuren committed
67 68
	if l.Cmp(common.Big0) == 0 {
		return common.Big0
69 70 71 72 73
	}

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

74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
// calculates the quadratic gas
func quadMemGas(mem *Memory, newMemSize, gas *big.Int) {
	if newMemSize.Cmp(common.Big0) > 0 {
		newMemSizeWords := toWordSize(newMemSize)
		newMemSize.Mul(newMemSizeWords, u256(32))

		if newMemSize.Cmp(u256(int64(mem.Len()))) > 0 {
			// be careful reusing variables here when changing.
			// The order has been optimised to reduce allocation
			oldSize := toWordSize(big.NewInt(int64(mem.Len())))
			pow := new(big.Int).Exp(oldSize, common.Big2, Zero)
			linCoef := oldSize.Mul(oldSize, params.MemoryGas)
			quadCoef := new(big.Int).Div(pow, params.QuadCoeffDiv)
			oldTotalFee := new(big.Int).Add(linCoef, quadCoef)

			pow.Exp(newMemSizeWords, common.Big2, Zero)
			linCoef = linCoef.Mul(newMemSizeWords, params.MemoryGas)
			quadCoef = quadCoef.Div(pow, params.QuadCoeffDiv)
			newTotalFee := linCoef.Add(linCoef, quadCoef)

			fee := newTotalFee.Sub(newTotalFee, oldTotalFee)
			gas.Add(gas, fee)
		}
	}
}

100 101 102 103 104 105 106 107 108 109 110 111 112 113 114
// 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
}
115

116 117
// getData returns a slice from the data based on the start and size and pads
// up to size with zero's. This function is overflow safe.
obscuren's avatar
obscuren committed
118 119
func getData(data []byte, start, size *big.Int) []byte {
	dlen := big.NewInt(int64(len(data)))
120

obscuren's avatar
obscuren committed
121 122 123
	s := common.BigMin(start, dlen)
	e := common.BigMin(new(big.Int).Add(s, size), dlen)
	return common.RightPadBytes(data[s.Uint64():e.Uint64()], int(size.Uint64()))
124
}
obscuren's avatar
obscuren committed
125

126 127 128
// useGas attempts to subtract the amount of gas and returns whether it was
// successful
func useGas(gas, amount *big.Int) bool {
obscuren's avatar
obscuren committed
129 130 131 132 133 134 135 136
	if gas.Cmp(amount) < 0 {
		return false
	}

	// Sub the amount of gas from the remaining
	gas.Sub(gas, amount)
	return true
}