Official Go implementation of the Ethereum protocol
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
go-ethereum/core/vm/common.go

106 lines
2.3 KiB

// Copyright 2014 The go-ethereum Authors
// This file is part of the go-ethereum library.
//
// The go-ethereum library is free software: you can redistribute it and/or modify
// 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.
//
// The go-ethereum library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
10 years ago
package vm
import (
"math"
"math/big"
"github.com/ethereum/go-ethereum/common"
10 years ago
"github.com/ethereum/go-ethereum/logger/glog"
)
// Global Debug flag indicating Debug VM (full logging)
var Debug bool
10 years ago
type Type byte
const (
10 years ago
StdVmTy Type = iota
JitVmTy
MaxVmTy
10 years ago
LogTyPretty byte = 0x1
LogTyDiff byte = 0x2
)
var (
Pow256 = common.BigPow(2, 256)
U256 = common.U256
S256 = common.S256
Zero = common.Big0
One = common.Big1
10 years ago
max = big.NewInt(math.MaxInt64)
)
func NewVm(env Environment) VirtualMachine {
switch env.VmType() {
case JitVmTy:
return NewJitVm(env)
default:
10 years ago
glog.V(0).Infoln("unsupported vm type %d", env.VmType())
fallthrough
case StdVmTy:
return New(env)
}
}
func calcMemSize(off, l *big.Int) *big.Int {
if l.Cmp(common.Big0) == 0 {
return common.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
}
func getData(data []byte, start, size *big.Int) []byte {
dlen := big.NewInt(int64(len(data)))
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()))
}
10 years ago
func UseGas(gas, amount *big.Int) bool {
if gas.Cmp(amount) < 0 {
return false
}
// Sub the amount of gas from the remaining
gas.Sub(gas, amount)
return true
}