2017-04-14 08:29:00 +00:00
|
|
|
// Copyright 2016 The go-ethereum Authors
|
2016-12-06 01:16:03 +00:00
|
|
|
// 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/>.
|
|
|
|
|
|
|
|
package core
|
|
|
|
|
|
|
|
import (
|
2021-06-03 07:09:56 +00:00
|
|
|
"fmt"
|
2016-12-06 01:16:03 +00:00
|
|
|
"math/big"
|
|
|
|
|
2020-05-26 16:53:50 +00:00
|
|
|
"github.com/holiman/uint256"
|
2021-05-20 18:25:53 +00:00
|
|
|
"github.com/ledgerwatch/erigon/common"
|
|
|
|
"github.com/ledgerwatch/erigon/consensus"
|
|
|
|
"github.com/ledgerwatch/erigon/core/types"
|
|
|
|
"github.com/ledgerwatch/erigon/core/vm"
|
2016-12-06 01:16:03 +00:00
|
|
|
)
|
|
|
|
|
2020-11-13 12:42:19 +00:00
|
|
|
// NewEVMBlockContext creates a new context for use in the EVM.
|
2021-06-11 08:34:25 +00:00
|
|
|
func NewEVMBlockContext(header *types.Header, getHeader func(hash common.Hash, number uint64) *types.Header, engine consensus.Engine, author *common.Address, checkTEVM func(contractHash common.Hash) (bool, error)) vm.BlockContext {
|
2017-04-12 13:38:31 +00:00
|
|
|
// If we don't have an explicit author (i.e. not mining), extract from the header
|
|
|
|
var beneficiary common.Address
|
|
|
|
if author == nil {
|
2021-04-06 09:52:53 +00:00
|
|
|
beneficiary, _ = engine.Author(header) // Ignore error, we're past header validation
|
2017-04-12 13:38:31 +00:00
|
|
|
} else {
|
|
|
|
beneficiary = *author
|
|
|
|
}
|
2021-04-22 17:11:37 +00:00
|
|
|
var baseFee uint256.Int
|
2021-05-11 09:54:20 +00:00
|
|
|
if header.Eip1559 {
|
2021-06-03 07:09:56 +00:00
|
|
|
overflow := baseFee.SetFromBig(header.BaseFee)
|
|
|
|
if overflow {
|
|
|
|
panic(fmt.Errorf("header.BaseFee higher than 2^256-1"))
|
|
|
|
}
|
2021-04-22 17:11:37 +00:00
|
|
|
}
|
2021-05-27 13:54:55 +00:00
|
|
|
if checkTEVM == nil {
|
|
|
|
checkTEVM = func(_ common.Hash) (bool, error) {
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
}
|
2020-11-13 12:42:19 +00:00
|
|
|
return vm.BlockContext{
|
2016-12-06 01:16:03 +00:00
|
|
|
CanTransfer: CanTransfer,
|
|
|
|
Transfer: Transfer,
|
2021-04-06 09:52:53 +00:00
|
|
|
GetHash: GetHashFn(header, getHeader),
|
2017-04-12 13:38:31 +00:00
|
|
|
Coinbase: beneficiary,
|
2021-04-22 17:11:37 +00:00
|
|
|
BlockNumber: header.Number.Uint64(),
|
|
|
|
Time: header.Time,
|
2016-12-06 01:16:03 +00:00
|
|
|
Difficulty: new(big.Int).Set(header.Difficulty),
|
2021-04-22 17:11:37 +00:00
|
|
|
BaseFee: &baseFee,
|
2017-11-13 11:47:27 +00:00
|
|
|
GasLimit: header.GasLimit,
|
2021-05-27 13:54:55 +00:00
|
|
|
CheckTEVM: checkTEVM,
|
2016-12-06 01:16:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-13 12:42:19 +00:00
|
|
|
// NewEVMTxContext creates a new transaction context for a single transaction.
|
|
|
|
func NewEVMTxContext(msg Message) vm.TxContext {
|
|
|
|
return vm.TxContext{
|
|
|
|
Origin: msg.From(),
|
2021-03-12 17:26:06 +00:00
|
|
|
GasPrice: msg.GasPrice().ToBig(),
|
2020-11-13 12:42:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-06 01:16:03 +00:00
|
|
|
// GetHashFn returns a GetHashFunc which retrieves header hashes by number
|
2021-04-06 09:52:53 +00:00
|
|
|
func GetHashFn(ref *types.Header, getHeader func(hash common.Hash, number uint64) *types.Header) func(n uint64) common.Hash {
|
2020-02-04 10:32:31 +00:00
|
|
|
// Cache will initially contain [refHash.parent],
|
|
|
|
// Then fill up with [refHash.p, refHash.pp, refHash.ppp, ...]
|
|
|
|
var cache []common.Hash
|
2018-03-26 09:28:46 +00:00
|
|
|
|
2016-12-06 01:16:03 +00:00
|
|
|
return func(n uint64) common.Hash {
|
2018-03-26 09:28:46 +00:00
|
|
|
// If there's no hash cache yet, make one
|
2020-02-04 10:32:31 +00:00
|
|
|
if len(cache) == 0 {
|
|
|
|
cache = append(cache, ref.ParentHash)
|
2018-03-26 09:28:46 +00:00
|
|
|
}
|
2020-02-04 10:32:31 +00:00
|
|
|
if idx := ref.Number.Uint64() - n - 1; idx < uint64(len(cache)) {
|
|
|
|
return cache[idx]
|
2018-03-26 09:28:46 +00:00
|
|
|
}
|
2020-02-04 10:32:31 +00:00
|
|
|
// No luck in the cache, but we can start iterating from the last element we already know
|
|
|
|
lastKnownHash := cache[len(cache)-1]
|
|
|
|
lastKnownNumber := ref.Number.Uint64() - uint64(len(cache))
|
|
|
|
|
|
|
|
for {
|
2021-04-06 09:52:53 +00:00
|
|
|
header := getHeader(lastKnownHash, lastKnownNumber)
|
2020-02-04 10:32:31 +00:00
|
|
|
if header == nil {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
cache = append(cache, header.ParentHash)
|
|
|
|
lastKnownHash = header.ParentHash
|
|
|
|
lastKnownNumber = header.Number.Uint64() - 1
|
|
|
|
if n == lastKnownNumber {
|
|
|
|
return lastKnownHash
|
2016-12-06 01:16:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return common.Hash{}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-25 11:04:38 +00:00
|
|
|
// CanTransfer checks whether there are enough funds in the address' account to make a transfer.
|
2016-12-06 01:16:03 +00:00
|
|
|
// This does not take the necessary gas in to account to make the transfer valid.
|
2020-05-26 16:53:50 +00:00
|
|
|
func CanTransfer(db vm.IntraBlockState, addr common.Address, amount *uint256.Int) bool {
|
|
|
|
return !db.GetBalance(addr).Lt(amount)
|
2016-12-06 01:16:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Transfer subtracts amount from sender and adds amount to recipient using the given Db
|
2021-02-12 16:47:32 +00:00
|
|
|
func Transfer(db vm.IntraBlockState, sender, recipient common.Address, amount *uint256.Int, bailout bool) {
|
|
|
|
if !bailout {
|
|
|
|
db.SubBalance(sender, amount)
|
|
|
|
}
|
2016-12-06 01:16:03 +00:00
|
|
|
db.AddBalance(recipient, amount)
|
|
|
|
}
|