Esempio n. 1
0
// CalcDifficulty is the difficulty adjustment algorithm. It returns
// the difficulty that a new block should have when created at time
// given the parent block's time and difficulty.
func CalcDifficulty(time, parentTime uint64, parentNumber, parentDiff *big.Int) *big.Int {
	if params.IsHomestead(new(big.Int).Add(parentNumber, common.Big1)) {
		return calcDifficultyHomestead(time, parentTime, parentNumber, parentDiff)
	} else {
		return calcDifficultyFrontier(time, parentTime, parentNumber, parentDiff)
	}
}
Esempio n. 2
0
func (jt vmJumpTable) init(blockNumber *big.Int) {
	// when initialising a new VM execution we must first check the homestead
	// changes.
	if params.IsHomestead(blockNumber) {
		jumpTable[DELEGATECALL] = jumpPtr{opDelegateCall, true}
	} else {
		jumpTable[DELEGATECALL] = jumpPtr{nil, false}
	}
}
Esempio n. 3
0
func (self *StateTransition) transitionDb() (ret []byte, usedGas *big.Int, err error) {
	if err = self.preCheck(); err != nil {
		return
	}
	msg := self.msg
	sender, _ := self.from() // err checked in preCheck

	homestead := params.IsHomestead(self.env.BlockNumber())
	contractCreation := MessageCreatesContract(msg)
	// Pay intrinsic gas
	if err = self.useGas(IntrinsicGas(self.data, contractCreation, homestead)); err != nil {
		return nil, nil, InvalidTxError(err)
	}

	vmenv := self.env
	//var addr common.Address
	if contractCreation {
		ret, _, err = vmenv.Create(sender, self.data, self.gas, self.gasPrice, self.value)
		if homestead && err == vm.CodeStoreOutOfGasError {
			self.gas = Big0
		}

		if err != nil {
			ret = nil
			glog.V(logger.Core).Infoln("VM create err:", err)
		}
	} else {
		// Increment the nonce for the next transaction
		self.state.SetNonce(sender.Address(), self.state.GetNonce(sender.Address())+1)
		ret, err = vmenv.Call(sender, self.to().Address(), self.data, self.gas, self.gasPrice, self.value)
		if err != nil {
			glog.V(logger.Core).Infoln("VM call err:", err)
		}
	}

	if err != nil && IsValueTransferErr(err) {
		return nil, nil, InvalidTxError(err)
	}

	// We aren't interested in errors here. Errors returned by the VM are non-consensus errors and therefor shouldn't bubble up
	if err != nil {
		err = nil
	}

	if vm.Debug {
		vm.StdErrFormat(vmenv.StructLogs())
	}

	self.refundGas()
	self.state.AddBalance(self.env.Coinbase(), new(big.Int).Mul(self.gasUsed(), self.gasPrice))

	return ret, self.gasUsed(), err
}
Esempio n. 4
0
func (self *StateTransition) from() (vm.Account, error) {
	var (
		f   common.Address
		err error
	)
	if params.IsHomestead(self.env.BlockNumber()) {
		f, err = self.msg.From()
	} else {
		f, err = self.msg.FromFrontier()
	}
	if err != nil {
		return nil, err
	}
	if !self.state.Exist(f) {
		return self.state.CreateAccount(f), nil
	}
	return self.state.GetAccount(f), nil
}
Esempio n. 5
0
func opCreate(instr instruction, pc *uint64, env Environment, contract *Contract, memory *Memory, stack *stack) {
	var (
		value        = stack.pop()
		offset, size = stack.pop(), stack.pop()
		input        = memory.Get(offset.Int64(), size.Int64())
		gas          = new(big.Int).Set(contract.Gas)
	)
	contract.UseGas(contract.Gas)
	_, addr, suberr := env.Create(contract, input, gas, contract.Price, value)
	// Push item on the stack based on the returned error. If the ruleset is
	// homestead we must check for CodeStoreOutOfGasError (homestead only
	// rule) and treat as an error, if the ruleset is frontier we must
	// ignore this error and pretend the operation was successful.
	if params.IsHomestead(env.BlockNumber()) && suberr == CodeStoreOutOfGasError {
		stack.push(new(big.Int))
	} else if suberr != nil && suberr != CodeStoreOutOfGasError {
		stack.push(new(big.Int))
	} else {
		stack.push(addr.Big())
	}
}
Esempio n. 6
0
func runProgram(program *Program, pcstart uint64, mem *Memory, stack *stack, env Environment, contract *Contract, input []byte) ([]byte, error) {
	contract.Input = input

	var (
		pc         uint64 = program.mapping[pcstart]
		instrCount        = 0
	)

	if glog.V(logger.Debug) {
		glog.Infof("running JIT program %x\n", program.Id[:4])
		tstart := time.Now()
		defer func() {
			glog.Infof("JIT program %x done. time: %v instrc: %v\n", program.Id[:4], time.Since(tstart), instrCount)
		}()
	}

	homestead := params.IsHomestead(env.BlockNumber())
	for pc < uint64(len(program.instructions)) {
		instrCount++

		instr := program.instructions[pc]
		if instr.Op() == DELEGATECALL && !homestead {
			return nil, fmt.Errorf("Invalid opcode 0x%x", instr.Op())
		}

		ret, err := instr.do(program, &pc, env, contract, mem, stack)
		if err != nil {
			return nil, err
		}

		if instr.halts() {
			return ret, nil
		}
	}

	contract.Input = nil

	return nil, nil
}
Esempio n. 7
0
func (pool *TxPool) eventLoop() {
	// Track chain events. When a chain events occurs (new chain canon block)
	// we need to know the new state. The new state will help us determine
	// the nonces in the managed state
	for ev := range pool.events.Chan() {
		switch ev := ev.Data.(type) {
		case ChainHeadEvent:
			pool.mu.Lock()
			if ev.Block != nil && params.IsHomestead(ev.Block.Number()) {
				pool.homestead = true
			}

			pool.resetState()
			pool.mu.Unlock()
		case GasPriceChanged:
			pool.mu.Lock()
			pool.minGasPrice = ev.Price
			pool.mu.Unlock()
		case RemovedTransactionEvent:
			pool.AddTransactions(ev.Txs)
		}
	}
}
Esempio n. 8
0
func newJumpTable(blockNumber *big.Int) vmJumpTable {
	var jumpTable vmJumpTable

	// when initialising a new VM execution we must first check the homestead
	// changes.
	if params.IsHomestead(blockNumber) {
		jumpTable[DELEGATECALL] = jumpPtr{opDelegateCall, true}
	}

	jumpTable[ADD] = jumpPtr{opAdd, true}
	jumpTable[SUB] = jumpPtr{opSub, true}
	jumpTable[MUL] = jumpPtr{opMul, true}
	jumpTable[DIV] = jumpPtr{opDiv, true}
	jumpTable[SDIV] = jumpPtr{opSdiv, true}
	jumpTable[MOD] = jumpPtr{opMod, true}
	jumpTable[SMOD] = jumpPtr{opSmod, true}
	jumpTable[EXP] = jumpPtr{opExp, true}
	jumpTable[SIGNEXTEND] = jumpPtr{opSignExtend, true}
	jumpTable[NOT] = jumpPtr{opNot, true}
	jumpTable[LT] = jumpPtr{opLt, true}
	jumpTable[GT] = jumpPtr{opGt, true}
	jumpTable[SLT] = jumpPtr{opSlt, true}
	jumpTable[SGT] = jumpPtr{opSgt, true}
	jumpTable[EQ] = jumpPtr{opEq, true}
	jumpTable[ISZERO] = jumpPtr{opIszero, true}
	jumpTable[AND] = jumpPtr{opAnd, true}
	jumpTable[OR] = jumpPtr{opOr, true}
	jumpTable[XOR] = jumpPtr{opXor, true}
	jumpTable[BYTE] = jumpPtr{opByte, true}
	jumpTable[ADDMOD] = jumpPtr{opAddmod, true}
	jumpTable[MULMOD] = jumpPtr{opMulmod, true}
	jumpTable[SHA3] = jumpPtr{opSha3, true}
	jumpTable[ADDRESS] = jumpPtr{opAddress, true}
	jumpTable[BALANCE] = jumpPtr{opBalance, true}
	jumpTable[ORIGIN] = jumpPtr{opOrigin, true}
	jumpTable[CALLER] = jumpPtr{opCaller, true}
	jumpTable[CALLVALUE] = jumpPtr{opCallValue, true}
	jumpTable[CALLDATALOAD] = jumpPtr{opCalldataLoad, true}
	jumpTable[CALLDATASIZE] = jumpPtr{opCalldataSize, true}
	jumpTable[CALLDATACOPY] = jumpPtr{opCalldataCopy, true}
	jumpTable[CODESIZE] = jumpPtr{opCodeSize, true}
	jumpTable[EXTCODESIZE] = jumpPtr{opExtCodeSize, true}
	jumpTable[CODECOPY] = jumpPtr{opCodeCopy, true}
	jumpTable[EXTCODECOPY] = jumpPtr{opExtCodeCopy, true}
	jumpTable[GASPRICE] = jumpPtr{opGasprice, true}
	jumpTable[BLOCKHASH] = jumpPtr{opBlockhash, true}
	jumpTable[COINBASE] = jumpPtr{opCoinbase, true}
	jumpTable[TIMESTAMP] = jumpPtr{opTimestamp, true}
	jumpTable[NUMBER] = jumpPtr{opNumber, true}
	jumpTable[DIFFICULTY] = jumpPtr{opDifficulty, true}
	jumpTable[GASLIMIT] = jumpPtr{opGasLimit, true}
	jumpTable[POP] = jumpPtr{opPop, true}
	jumpTable[MLOAD] = jumpPtr{opMload, true}
	jumpTable[MSTORE] = jumpPtr{opMstore, true}
	jumpTable[MSTORE8] = jumpPtr{opMstore8, true}
	jumpTable[SLOAD] = jumpPtr{opSload, true}
	jumpTable[SSTORE] = jumpPtr{opSstore, true}
	jumpTable[JUMPDEST] = jumpPtr{opJumpdest, true}
	jumpTable[PC] = jumpPtr{nil, true}
	jumpTable[MSIZE] = jumpPtr{opMsize, true}
	jumpTable[GAS] = jumpPtr{opGas, true}
	jumpTable[CREATE] = jumpPtr{opCreate, true}
	jumpTable[CALL] = jumpPtr{opCall, true}
	jumpTable[CALLCODE] = jumpPtr{opCallCode, true}
	jumpTable[LOG0] = jumpPtr{makeLog(0), true}
	jumpTable[LOG1] = jumpPtr{makeLog(1), true}
	jumpTable[LOG2] = jumpPtr{makeLog(2), true}
	jumpTable[LOG3] = jumpPtr{makeLog(3), true}
	jumpTable[LOG4] = jumpPtr{makeLog(4), true}
	jumpTable[SWAP1] = jumpPtr{makeSwap(1), true}
	jumpTable[SWAP2] = jumpPtr{makeSwap(2), true}
	jumpTable[SWAP3] = jumpPtr{makeSwap(3), true}
	jumpTable[SWAP4] = jumpPtr{makeSwap(4), true}
	jumpTable[SWAP5] = jumpPtr{makeSwap(5), true}
	jumpTable[SWAP6] = jumpPtr{makeSwap(6), true}
	jumpTable[SWAP7] = jumpPtr{makeSwap(7), true}
	jumpTable[SWAP8] = jumpPtr{makeSwap(8), true}
	jumpTable[SWAP9] = jumpPtr{makeSwap(9), true}
	jumpTable[SWAP10] = jumpPtr{makeSwap(10), true}
	jumpTable[SWAP11] = jumpPtr{makeSwap(11), true}
	jumpTable[SWAP12] = jumpPtr{makeSwap(12), true}
	jumpTable[SWAP13] = jumpPtr{makeSwap(13), true}
	jumpTable[SWAP14] = jumpPtr{makeSwap(14), true}
	jumpTable[SWAP15] = jumpPtr{makeSwap(15), true}
	jumpTable[SWAP16] = jumpPtr{makeSwap(16), true}
	jumpTable[PUSH1] = jumpPtr{makePush(1, big.NewInt(1)), true}
	jumpTable[PUSH2] = jumpPtr{makePush(2, big.NewInt(2)), true}
	jumpTable[PUSH3] = jumpPtr{makePush(3, big.NewInt(3)), true}
	jumpTable[PUSH4] = jumpPtr{makePush(4, big.NewInt(4)), true}
	jumpTable[PUSH5] = jumpPtr{makePush(5, big.NewInt(5)), true}
	jumpTable[PUSH6] = jumpPtr{makePush(6, big.NewInt(6)), true}
	jumpTable[PUSH7] = jumpPtr{makePush(7, big.NewInt(7)), true}
	jumpTable[PUSH8] = jumpPtr{makePush(8, big.NewInt(8)), true}
	jumpTable[PUSH9] = jumpPtr{makePush(9, big.NewInt(9)), true}
	jumpTable[PUSH10] = jumpPtr{makePush(10, big.NewInt(10)), true}
	jumpTable[PUSH11] = jumpPtr{makePush(11, big.NewInt(11)), true}
	jumpTable[PUSH12] = jumpPtr{makePush(12, big.NewInt(12)), true}
	jumpTable[PUSH13] = jumpPtr{makePush(13, big.NewInt(13)), true}
	jumpTable[PUSH14] = jumpPtr{makePush(14, big.NewInt(14)), true}
	jumpTable[PUSH15] = jumpPtr{makePush(15, big.NewInt(15)), true}
	jumpTable[PUSH16] = jumpPtr{makePush(16, big.NewInt(16)), true}
	jumpTable[PUSH17] = jumpPtr{makePush(17, big.NewInt(17)), true}
	jumpTable[PUSH18] = jumpPtr{makePush(18, big.NewInt(18)), true}
	jumpTable[PUSH19] = jumpPtr{makePush(19, big.NewInt(19)), true}
	jumpTable[PUSH20] = jumpPtr{makePush(20, big.NewInt(20)), true}
	jumpTable[PUSH21] = jumpPtr{makePush(21, big.NewInt(21)), true}
	jumpTable[PUSH22] = jumpPtr{makePush(22, big.NewInt(22)), true}
	jumpTable[PUSH23] = jumpPtr{makePush(23, big.NewInt(23)), true}
	jumpTable[PUSH24] = jumpPtr{makePush(24, big.NewInt(24)), true}
	jumpTable[PUSH25] = jumpPtr{makePush(25, big.NewInt(25)), true}
	jumpTable[PUSH26] = jumpPtr{makePush(26, big.NewInt(26)), true}
	jumpTable[PUSH27] = jumpPtr{makePush(27, big.NewInt(27)), true}
	jumpTable[PUSH28] = jumpPtr{makePush(28, big.NewInt(28)), true}
	jumpTable[PUSH29] = jumpPtr{makePush(29, big.NewInt(29)), true}
	jumpTable[PUSH30] = jumpPtr{makePush(30, big.NewInt(30)), true}
	jumpTable[PUSH31] = jumpPtr{makePush(31, big.NewInt(31)), true}
	jumpTable[PUSH32] = jumpPtr{makePush(32, big.NewInt(32)), true}
	jumpTable[DUP1] = jumpPtr{makeDup(1), true}
	jumpTable[DUP2] = jumpPtr{makeDup(2), true}
	jumpTable[DUP3] = jumpPtr{makeDup(3), true}
	jumpTable[DUP4] = jumpPtr{makeDup(4), true}
	jumpTable[DUP5] = jumpPtr{makeDup(5), true}
	jumpTable[DUP6] = jumpPtr{makeDup(6), true}
	jumpTable[DUP7] = jumpPtr{makeDup(7), true}
	jumpTable[DUP8] = jumpPtr{makeDup(8), true}
	jumpTable[DUP9] = jumpPtr{makeDup(9), true}
	jumpTable[DUP10] = jumpPtr{makeDup(10), true}
	jumpTable[DUP11] = jumpPtr{makeDup(11), true}
	jumpTable[DUP12] = jumpPtr{makeDup(12), true}
	jumpTable[DUP13] = jumpPtr{makeDup(13), true}
	jumpTable[DUP14] = jumpPtr{makeDup(14), true}
	jumpTable[DUP15] = jumpPtr{makeDup(15), true}
	jumpTable[DUP16] = jumpPtr{makeDup(16), true}

	jumpTable[RETURN] = jumpPtr{nil, true}
	jumpTable[SUICIDE] = jumpPtr{nil, true}
	jumpTable[JUMP] = jumpPtr{nil, true}
	jumpTable[JUMPI] = jumpPtr{nil, true}
	jumpTable[STOP] = jumpPtr{nil, true}

	return jumpTable
}
Esempio n. 9
0
func exec(env vm.Environment, caller vm.ContractRef, address, codeAddr *common.Address, input, code []byte, gas, gasPrice, value *big.Int) (ret []byte, addr common.Address, err error) {
	evm := env.Vm()
	// Depth check execution. Fail if we're trying to execute above the
	// limit.
	if env.Depth() > int(params.CallCreateDepth.Int64()) {
		caller.ReturnGas(gas, gasPrice)

		return nil, common.Address{}, vm.DepthError
	}

	if !env.CanTransfer(caller.Address(), value) {
		caller.ReturnGas(gas, gasPrice)

		return nil, common.Address{}, ValueTransferErr("insufficient funds to transfer value. Req %v, has %v", value, env.Db().GetBalance(caller.Address()))
	}

	var createAccount bool
	if address == nil {
		// Create a new account on the state
		nonce := env.Db().GetNonce(caller.Address())
		env.Db().SetNonce(caller.Address(), nonce+1)
		addr = crypto.CreateAddress(caller.Address(), nonce)
		address = &addr
		createAccount = true
	}

	snapshotPreTransfer := env.MakeSnapshot()
	var (
		from = env.Db().GetAccount(caller.Address())
		to   vm.Account
	)
	if createAccount {
		to = env.Db().CreateAccount(*address)
	} else {
		if !env.Db().Exist(*address) {
			to = env.Db().CreateAccount(*address)
		} else {
			to = env.Db().GetAccount(*address)
		}
	}
	env.Transfer(from, to, value)

	// initialise a new contract and set the code that is to be used by the
	// EVM. The contract is a scoped environment for this execution context
	// only.
	contract := vm.NewContract(caller, to, value, gas, gasPrice)
	contract.SetCallCode(codeAddr, code)
	defer contract.Finalise()

	ret, err = evm.Run(contract, input)
	// if the contract creation ran successfully and no errors were returned
	// calculate the gas required to store the code. If the code could not
	// be stored due to not enough gas set an error and let it be handled
	// by the error checking condition below.
	if err == nil && createAccount {
		dataGas := big.NewInt(int64(len(ret)))
		dataGas.Mul(dataGas, params.CreateDataGas)
		if contract.UseGas(dataGas) {
			env.Db().SetCode(*address, ret)
		} else {
			err = vm.CodeStoreOutOfGasError
		}
	}

	// When an error was returned by the EVM or when setting the creation code
	// above we revert to the snapshot and consume any gas remaining. Additionally
	// when we're in homestead this also counts for code storage gas errors.
	if err != nil && (params.IsHomestead(env.BlockNumber()) || err != vm.CodeStoreOutOfGasError) {
		contract.UseGas(contract.Gas)

		env.SetSnapshot(snapshotPreTransfer)
	}

	return ret, addr, err
}