state_accessor.go 3.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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/state"
  23. "github.com/ethereum/go-ethereum/core/types"
  24. "github.com/ethereum/go-ethereum/core/vm"
  25. "github.com/ethereum/go-ethereum/light"
  26. )
  27. // stateAtBlock retrieves the state database associated with a certain block.
  28. func (leth *LightEthereum) stateAtBlock(ctx context.Context, block *types.Block, reexec uint64) (*state.StateDB, func(), error) {
  29. return light.NewState(ctx, block.Header(), leth.odr), func() {}, nil
  30. }
  31. // statesInRange retrieves a batch of state databases associated with the specific
  32. // block ranges.
  33. func (leth *LightEthereum) statesInRange(ctx context.Context, fromBlock *types.Block, toBlock *types.Block, reexec uint64) ([]*state.StateDB, func(), error) {
  34. var states []*state.StateDB
  35. for number := fromBlock.NumberU64(); number <= toBlock.NumberU64(); number++ {
  36. header, err := leth.blockchain.GetHeaderByNumberOdr(ctx, number)
  37. if err != nil {
  38. return nil, nil, err
  39. }
  40. states = append(states, light.NewState(ctx, header, leth.odr))
  41. }
  42. return states, nil, nil
  43. }
  44. // stateAtTransaction returns the execution environment of a certain transaction.
  45. func (leth *LightEthereum) stateAtTransaction(ctx context.Context, block *types.Block, txIndex int, reexec uint64) (core.Message, vm.BlockContext, *state.StateDB, func(), error) {
  46. // Short circuit if it's genesis block.
  47. if block.NumberU64() == 0 {
  48. return nil, vm.BlockContext{}, nil, nil, errors.New("no transaction in genesis")
  49. }
  50. // Create the parent state database
  51. parent, err := leth.blockchain.GetBlock(ctx, block.ParentHash(), block.NumberU64()-1)
  52. if err != nil {
  53. return nil, vm.BlockContext{}, nil, nil, err
  54. }
  55. statedb, _, err := leth.stateAtBlock(ctx, parent, reexec)
  56. if err != nil {
  57. return nil, vm.BlockContext{}, nil, nil, err
  58. }
  59. if txIndex == 0 && len(block.Transactions()) == 0 {
  60. return nil, vm.BlockContext{}, statedb, func() {}, nil
  61. }
  62. // Recompute transactions up to the target index.
  63. signer := types.MakeSigner(leth.blockchain.Config(), block.Number())
  64. for idx, tx := range block.Transactions() {
  65. // Assemble the transaction call message and return if the requested offset
  66. msg, _ := tx.AsMessage(signer)
  67. txContext := core.NewEVMTxContext(msg)
  68. context := core.NewEVMBlockContext(block.Header(), leth.blockchain, nil)
  69. statedb.Prepare(tx.Hash(), block.Hash(), idx)
  70. if idx == txIndex {
  71. return msg, context, statedb, func() {}, nil
  72. }
  73. // Not yet the searched for transaction, execute on top of the current state
  74. vmenv := vm.NewEVM(context, txContext, statedb, leth.blockchain.Config(), vm.Config{})
  75. if _, err := core.ApplyMessage(vmenv, msg, new(core.GasPool).AddGas(tx.Gas())); err != nil {
  76. return nil, vm.BlockContext{}, nil, nil, fmt.Errorf("transaction %#x failed: %v", tx.Hash(), err)
  77. }
  78. // Ensure any modifications are committed to the state
  79. // Only delete empty objects if EIP158/161 (a.k.a Spurious Dragon) is in effect
  80. statedb.Finalise(vmenv.ChainConfig().IsEIP158(block.Number()))
  81. }
  82. return nil, vm.BlockContext{}, nil, nil, fmt.Errorf("transaction index %d out of range for block %#x", txIndex, block.Hash())
  83. }