state_accessor.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. // Copyright 2021 The go-ethereum Authors
  2. // This file is part of the go-ethereum library.
  3. //
  4. // The go-ethereum library is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU Lesser General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // The go-ethereum library is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU Lesser General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU Lesser General Public License
  15. // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
  16. package les
  17. import (
  18. "context"
  19. "errors"
  20. "fmt"
  21. "github.com/ethereum/go-ethereum/core"
  22. "github.com/ethereum/go-ethereum/core/mps"
  23. "github.com/ethereum/go-ethereum/core/state"
  24. "github.com/ethereum/go-ethereum/core/types"
  25. "github.com/ethereum/go-ethereum/core/vm"
  26. "github.com/ethereum/go-ethereum/light"
  27. )
  28. // stateAtBlock retrieves the state database associated with a certain block.
  29. func (leth *LightEthereum) stateAtBlock(ctx context.Context, block *types.Block, reexec uint64) (*state.StateDB, mps.PrivateStateRepository, error) {
  30. return light.NewState(ctx, block.Header(), leth.odr), nil, nil
  31. }
  32. // stateAtTransaction returns the execution environment of a certain transaction.
  33. func (leth *LightEthereum) stateAtTransaction(ctx context.Context, block *types.Block, txIndex int, reexec uint64) (core.Message, vm.BlockContext, *state.StateDB, *state.StateDB, mps.PrivateStateRepository, error) {
  34. // Short circuit if it's genesis block.
  35. if block.NumberU64() == 0 {
  36. return nil, vm.BlockContext{}, nil, nil, nil, errors.New("no transaction in genesis")
  37. }
  38. // Create the parent state database
  39. parent, err := leth.blockchain.GetBlock(ctx, block.ParentHash(), block.NumberU64()-1)
  40. if err != nil {
  41. return nil, vm.BlockContext{}, nil, nil, nil, err
  42. }
  43. statedb, private, err := leth.stateAtBlock(ctx, parent, reexec)
  44. if err != nil {
  45. return nil, vm.BlockContext{}, nil, nil, nil, err
  46. }
  47. privateStateDb, err := private.DefaultState()
  48. if err != nil {
  49. return nil, vm.BlockContext{}, nil, nil, nil, err
  50. }
  51. if txIndex == 0 && len(block.Transactions()) == 0 {
  52. return nil, vm.BlockContext{}, statedb, privateStateDb, private, nil
  53. }
  54. // Recompute transactions up to the target index.
  55. signer := types.MakeSigner(leth.blockchain.Config(), block.Number())
  56. for idx, tx := range block.Transactions() {
  57. // Assemble the transaction call message and return if the requested offset
  58. msg, _ := tx.AsMessage(signer)
  59. txContext := core.NewEVMTxContext(msg)
  60. context := core.NewEVMBlockContext(block.Header(), leth.blockchain, nil)
  61. statedb.Prepare(tx.Hash(), block.Hash(), idx)
  62. privateStateDb.Prepare(tx.Hash(), block.Hash(), idx)
  63. if idx == txIndex {
  64. return msg, context, statedb, statedb, nil, nil
  65. }
  66. // Not yet the searched for transaction, execute on top of the current state
  67. vmenv := vm.NewEVM(context, txContext, statedb, privateStateDb, leth.blockchain.Config(), vm.Config{})
  68. if _, err := core.ApplyMessage(vmenv, msg, new(core.GasPool).AddGas(tx.Gas())); err != nil {
  69. return nil, vm.BlockContext{}, nil, nil, nil, fmt.Errorf("transaction %#x failed: %v", tx.Hash(), err)
  70. }
  71. // Ensure any modifications are committed to the state
  72. // Only delete empty objects if EIP158/161 (a.k.a Spurious Dragon) is in effect
  73. statedb.Finalise(vmenv.ChainConfig().IsEIP158(block.Number()))
  74. }
  75. return nil, vm.BlockContext{}, nil, nil, nil, fmt.Errorf("transaction index %d out of range for block %#x", txIndex, block.Hash())
  76. }