2021-01-25 16:36:39 +03:00
|
|
|
// Copyright 2021 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/>.
|
|
|
|
|
|
|
|
package les
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"github.com/ethereum/go-ethereum/core"
|
|
|
|
"github.com/ethereum/go-ethereum/core/state"
|
|
|
|
"github.com/ethereum/go-ethereum/core/types"
|
|
|
|
"github.com/ethereum/go-ethereum/core/vm"
|
2022-09-07 21:25:58 +03:00
|
|
|
"github.com/ethereum/go-ethereum/eth/tracers"
|
2021-01-25 16:36:39 +03:00
|
|
|
"github.com/ethereum/go-ethereum/light"
|
|
|
|
)
|
|
|
|
|
2022-09-07 21:25:58 +03:00
|
|
|
// noopReleaser is returned in case there is no operation expected
|
|
|
|
// for releasing state.
|
|
|
|
var noopReleaser = tracers.StateReleaseFunc(func() {})
|
|
|
|
|
2021-01-25 16:36:39 +03:00
|
|
|
// stateAtBlock retrieves the state database associated with a certain block.
|
2022-09-07 21:25:58 +03:00
|
|
|
func (leth *LightEthereum) stateAtBlock(ctx context.Context, block *types.Block, reexec uint64) (*state.StateDB, tracers.StateReleaseFunc, error) {
|
|
|
|
return light.NewState(ctx, block.Header(), leth.odr), noopReleaser, nil
|
2021-01-25 16:36:39 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// stateAtTransaction returns the execution environment of a certain transaction.
|
core, core/types: plain Message struct (#25977)
Here, the core.Message interface turns into a plain struct and
types.Message gets removed.
This is a breaking change to packages core and core/types. While we do
not promise API stability for package core, we do for core/types. An
exception can be made for types.Message, since it doesn't have any
purpose apart from invoking the state transition in package core.
types.Message was also marked deprecated by the same commit it
got added in, 4dca5d4db7 (November 2016).
The core.Message interface was added in December 2014, in commit
db494170dc, for the purpose of 'testing' state transitions. It's the
same change that made transaction struct fields private. Before that,
the state transition used *types.Transaction directly.
Over time, multiple implementations of the interface accrued across
different packages, since constructing a Message is required whenever
one wants to invoke the state transition. These implementations all
looked very similar, a struct with private fields exposing the fields
as accessor methods.
By changing Message into a struct with public fields we can remove all
these useless interface implementations. It will also hopefully
simplify future changes to the type with less updates to apply across
all of go-ethereum when a field is added to Message.
---------
Co-authored-by: Felix Lange <fjl@twurst.com>
2023-03-09 16:19:12 +03:00
|
|
|
func (leth *LightEthereum) stateAtTransaction(ctx context.Context, block *types.Block, txIndex int, reexec uint64) (*core.Message, vm.BlockContext, *state.StateDB, tracers.StateReleaseFunc, error) {
|
2021-01-25 16:36:39 +03:00
|
|
|
// Short circuit if it's genesis block.
|
|
|
|
if block.NumberU64() == 0 {
|
2022-09-07 21:25:58 +03:00
|
|
|
return nil, vm.BlockContext{}, nil, nil, errors.New("no transaction in genesis")
|
2021-01-25 16:36:39 +03:00
|
|
|
}
|
|
|
|
// Create the parent state database
|
|
|
|
parent, err := leth.blockchain.GetBlock(ctx, block.ParentHash(), block.NumberU64()-1)
|
|
|
|
if err != nil {
|
2022-09-07 21:25:58 +03:00
|
|
|
return nil, vm.BlockContext{}, nil, nil, err
|
2021-01-25 16:36:39 +03:00
|
|
|
}
|
2022-09-07 21:25:58 +03:00
|
|
|
statedb, release, err := leth.stateAtBlock(ctx, parent, reexec)
|
2021-01-25 16:36:39 +03:00
|
|
|
if err != nil {
|
2022-09-07 21:25:58 +03:00
|
|
|
return nil, vm.BlockContext{}, nil, nil, err
|
2021-01-25 16:36:39 +03:00
|
|
|
}
|
|
|
|
if txIndex == 0 && len(block.Transactions()) == 0 {
|
2022-09-07 21:25:58 +03:00
|
|
|
return nil, vm.BlockContext{}, statedb, release, nil
|
2021-01-25 16:36:39 +03:00
|
|
|
}
|
|
|
|
// Recompute transactions up to the target index.
|
2023-04-21 12:52:02 +03:00
|
|
|
signer := types.MakeSigner(leth.blockchain.Config(), block.Number(), block.Time())
|
2021-01-25 16:36:39 +03:00
|
|
|
for idx, tx := range block.Transactions() {
|
|
|
|
// Assemble the transaction call message and return if the requested offset
|
core, core/types: plain Message struct (#25977)
Here, the core.Message interface turns into a plain struct and
types.Message gets removed.
This is a breaking change to packages core and core/types. While we do
not promise API stability for package core, we do for core/types. An
exception can be made for types.Message, since it doesn't have any
purpose apart from invoking the state transition in package core.
types.Message was also marked deprecated by the same commit it
got added in, 4dca5d4db7 (November 2016).
The core.Message interface was added in December 2014, in commit
db494170dc, for the purpose of 'testing' state transitions. It's the
same change that made transaction struct fields private. Before that,
the state transition used *types.Transaction directly.
Over time, multiple implementations of the interface accrued across
different packages, since constructing a Message is required whenever
one wants to invoke the state transition. These implementations all
looked very similar, a struct with private fields exposing the fields
as accessor methods.
By changing Message into a struct with public fields we can remove all
these useless interface implementations. It will also hopefully
simplify future changes to the type with less updates to apply across
all of go-ethereum when a field is added to Message.
---------
Co-authored-by: Felix Lange <fjl@twurst.com>
2023-03-09 16:19:12 +03:00
|
|
|
msg, _ := core.TransactionToMessage(tx, signer, block.BaseFee())
|
2021-01-25 16:36:39 +03:00
|
|
|
txContext := core.NewEVMTxContext(msg)
|
|
|
|
context := core.NewEVMBlockContext(block.Header(), leth.blockchain, nil)
|
2022-11-16 12:18:52 +03:00
|
|
|
statedb.SetTxContext(tx.Hash(), idx)
|
2021-01-25 16:36:39 +03:00
|
|
|
if idx == txIndex {
|
2022-09-07 21:25:58 +03:00
|
|
|
return msg, context, statedb, release, nil
|
2021-01-25 16:36:39 +03:00
|
|
|
}
|
|
|
|
// Not yet the searched for transaction, execute on top of the current state
|
|
|
|
vmenv := vm.NewEVM(context, txContext, statedb, leth.blockchain.Config(), vm.Config{})
|
|
|
|
if _, err := core.ApplyMessage(vmenv, msg, new(core.GasPool).AddGas(tx.Gas())); err != nil {
|
2022-09-07 21:25:58 +03:00
|
|
|
return nil, vm.BlockContext{}, nil, nil, fmt.Errorf("transaction %#x failed: %v", tx.Hash(), err)
|
2021-01-25 16:36:39 +03:00
|
|
|
}
|
|
|
|
// Ensure any modifications are committed to the state
|
|
|
|
// Only delete empty objects if EIP158/161 (a.k.a Spurious Dragon) is in effect
|
|
|
|
statedb.Finalise(vmenv.ChainConfig().IsEIP158(block.Number()))
|
|
|
|
}
|
2022-09-07 21:25:58 +03:00
|
|
|
return nil, vm.BlockContext{}, nil, nil, fmt.Errorf("transaction index %d out of range for block %#x", txIndex, block.Hash())
|
2021-01-25 16:36:39 +03:00
|
|
|
}
|