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"
|
2023-01-13 18:12:18 +00:00
|
|
|
"github.com/ledgerwatch/erigon-lib/chain"
|
|
|
|
libcommon "github.com/ledgerwatch/erigon-lib/common"
|
|
|
|
|
2021-05-20 18:25:53 +00:00
|
|
|
"github.com/ledgerwatch/erigon/consensus"
|
2021-12-01 12:41:31 +00:00
|
|
|
"github.com/ledgerwatch/erigon/consensus/serenity"
|
2021-05-20 18:25:53 +00:00
|
|
|
"github.com/ledgerwatch/erigon/core/types"
|
2022-11-30 01:31:13 +00:00
|
|
|
"github.com/ledgerwatch/erigon/core/vm/evmtypes"
|
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.
|
2023-01-13 18:12:18 +00:00
|
|
|
func NewEVMBlockContext(header *types.Header, blockHashFunc func(n uint64) libcommon.Hash, engine consensus.EngineReader, author *libcommon.Address) evmtypes.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
|
2023-01-13 18:12:18 +00:00
|
|
|
var beneficiary libcommon.Address
|
2017-04-12 13:38:31 +00:00
|
|
|
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
|
2022-10-21 10:43:44 +00:00
|
|
|
if header.BaseFee != nil {
|
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-11-15 20:23:56 +00:00
|
|
|
|
2023-01-13 18:12:18 +00:00
|
|
|
var prevRandDao *libcommon.Hash
|
2021-12-01 12:41:31 +00:00
|
|
|
if header.Difficulty.Cmp(serenity.SerenityDifficulty) == 0 {
|
|
|
|
// EIP-4399. We use SerenityDifficulty (i.e. 0) as a telltale of Proof-of-Stake blocks.
|
2022-07-26 07:35:38 +00:00
|
|
|
prevRandDao = &header.MixDigest
|
2021-11-15 20:23:56 +00:00
|
|
|
}
|
2022-07-26 07:35:38 +00:00
|
|
|
|
2022-11-30 01:31:13 +00:00
|
|
|
var transferFunc evmtypes.TransferFunc
|
2023-01-13 18:12:18 +00:00
|
|
|
if engine != nil && engine.Type() == chain.BorConsensus {
|
2022-02-07 21:30:46 +00:00
|
|
|
transferFunc = BorTransfer
|
|
|
|
} else {
|
|
|
|
transferFunc = Transfer
|
|
|
|
}
|
|
|
|
|
2022-11-30 01:31:13 +00:00
|
|
|
return evmtypes.BlockContext{
|
2022-09-01 18:49:29 +00:00
|
|
|
CanTransfer: CanTransfer,
|
|
|
|
Transfer: transferFunc,
|
|
|
|
GetHash: blockHashFunc,
|
|
|
|
Coinbase: beneficiary,
|
|
|
|
BlockNumber: header.Number.Uint64(),
|
|
|
|
Time: header.Time,
|
|
|
|
Difficulty: new(big.Int).Set(header.Difficulty),
|
|
|
|
BaseFee: &baseFee,
|
|
|
|
GasLimit: header.GasLimit,
|
|
|
|
PrevRanDao: prevRandDao,
|
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.
|
2022-11-30 01:31:13 +00:00
|
|
|
func NewEVMTxContext(msg Message) evmtypes.TxContext {
|
|
|
|
return evmtypes.TxContext{
|
2020-11-13 12:42:19 +00:00
|
|
|
Origin: msg.From(),
|
2022-12-04 04:44:50 +00:00
|
|
|
GasPrice: msg.GasPrice(),
|
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
|
2023-01-13 18:12:18 +00:00
|
|
|
func GetHashFn(ref *types.Header, getHeader func(hash libcommon.Hash, number uint64) *types.Header) func(n uint64) libcommon.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, ...]
|
2023-01-13 18:12:18 +00:00
|
|
|
var cache []libcommon.Hash
|
2018-03-26 09:28:46 +00:00
|
|
|
|
2023-01-13 18:12:18 +00:00
|
|
|
return func(n uint64) libcommon.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
|
|
|
}
|
|
|
|
}
|
2023-01-13 18:12:18 +00:00
|
|
|
return libcommon.Hash{}
|
2016-12-06 01:16:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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.
|
2023-01-13 18:12:18 +00:00
|
|
|
func CanTransfer(db evmtypes.IntraBlockState, addr libcommon.Address, amount *uint256.Int) bool {
|
2020-05-26 16:53:50 +00:00
|
|
|
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
|
2023-01-13 18:12:18 +00:00
|
|
|
func Transfer(db evmtypes.IntraBlockState, sender, recipient libcommon.Address, amount *uint256.Int, bailout bool) {
|
2021-02-12 16:47:32 +00:00
|
|
|
if !bailout {
|
|
|
|
db.SubBalance(sender, amount)
|
|
|
|
}
|
2016-12-06 01:16:03 +00:00
|
|
|
db.AddBalance(recipient, amount)
|
|
|
|
}
|
2022-02-07 21:30:46 +00:00
|
|
|
|
|
|
|
// BorTransfer transfer in Bor
|
2023-01-13 18:12:18 +00:00
|
|
|
func BorTransfer(db evmtypes.IntraBlockState, sender, recipient libcommon.Address, amount *uint256.Int, bailout bool) {
|
2022-02-07 21:30:46 +00:00
|
|
|
// get inputs before
|
|
|
|
input1 := db.GetBalance(sender).Clone()
|
|
|
|
input2 := db.GetBalance(recipient).Clone()
|
|
|
|
|
|
|
|
if !bailout {
|
|
|
|
db.SubBalance(sender, amount)
|
|
|
|
}
|
|
|
|
db.AddBalance(recipient, amount)
|
|
|
|
|
|
|
|
// get outputs after
|
|
|
|
output1 := db.GetBalance(sender).Clone()
|
|
|
|
output2 := db.GetBalance(recipient).Clone()
|
|
|
|
|
|
|
|
// add transfer log
|
|
|
|
AddTransferLog(db, sender, recipient, amount, input1, input2, output1, output2)
|
|
|
|
}
|