state_prefetcher.go 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. // Copyright 2019 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 core
  17. import (
  18. "sync"
  19. "sync/atomic"
  20. "time"
  21. "github.com/ethereum/go-ethereum/consensus"
  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/params"
  26. // Quorum
  27. "github.com/ethereum/go-ethereum/common"
  28. "github.com/ethereum/go-ethereum/core/mps"
  29. "github.com/ethereum/go-ethereum/private"
  30. )
  31. // statePrefetcher is a basic Prefetcher, which blindly executes a block on top
  32. // of an arbitrary state with the goal of prefetching potentially useful state
  33. // data from disk before the main block processor start executing.
  34. type statePrefetcher struct {
  35. config *params.ChainConfig // Chain configuration options
  36. bc *BlockChain // Canonical block chain
  37. engine consensus.Engine // Consensus engine used for block rewards
  38. pend sync.WaitGroup // Quorum: wait for MPS prefetching
  39. }
  40. // newStatePrefetcher initialises a new statePrefetcher.
  41. func newStatePrefetcher(config *params.ChainConfig, bc *BlockChain, engine consensus.Engine) *statePrefetcher {
  42. return &statePrefetcher{
  43. config: config,
  44. bc: bc,
  45. engine: engine,
  46. }
  47. }
  48. // Prefetch processes the state changes according to the Ethereum rules by running
  49. // the transaction messages using the statedb, but any changes are discarded. The
  50. // only goal is to pre-cache transaction signatures and state trie nodes.
  51. // Quorum: Add privateStateDb argument
  52. func (p *statePrefetcher) Prefetch(block *types.Block, statedb *state.StateDB, privateStateRepo mps.PrivateStateRepository, cfg vm.Config, interrupt *uint32) {
  53. var (
  54. header = block.Header()
  55. gaspool = new(GasPool).AddGas(block.GasLimit())
  56. )
  57. // Iterate over and process the individual transactions
  58. byzantium := p.config.IsByzantium(block.Number())
  59. for i, tx := range block.Transactions() {
  60. // If block precaching was interrupted, abort
  61. if interrupt != nil && atomic.LoadUint32(interrupt) == 1 {
  62. return
  63. }
  64. // Quorum
  65. if tx.IsPrivate() && privateStateRepo.IsMPS() {
  66. p.prefetchMpsTransaction(block, tx, i, statedb.Copy(), privateStateRepo, cfg, interrupt)
  67. }
  68. privateStateDb, _ := privateStateRepo.DefaultState()
  69. privateStateDb.Prepare(tx.Hash(), block.Hash(), i)
  70. // End Quorum
  71. // Block precaching permitted to continue, execute the transaction
  72. statedb.Prepare(tx.Hash(), block.Hash(), i)
  73. innerApply := createInnerApply(block, tx, i, statedb, privateStateRepo, cfg, interrupt, p, privateStateDb)
  74. // Quorum: Add privateStateDb argument
  75. if err := precacheTransaction(p.config, p.bc, nil, gaspool, statedb, privateStateDb, header, tx, cfg, innerApply); err != nil {
  76. return // Ugh, something went horribly wrong, bail out
  77. }
  78. // If we're pre-byzantium, pre-load trie nodes for the intermediate root
  79. if !byzantium {
  80. statedb.IntermediateRoot(true)
  81. }
  82. }
  83. // If were post-byzantium, pre-load trie nodes for the final root hash
  84. if byzantium {
  85. statedb.IntermediateRoot(true)
  86. }
  87. }
  88. // precacheTransaction attempts to apply a transaction to the given state database
  89. // and uses the input parameters for its environment. The goal is not to execute
  90. // the transaction successfully, rather to warm up touched data slots.
  91. // Quorum: Add privateStateDb and isMPS arguments
  92. func precacheTransaction(config *params.ChainConfig, bc ChainContext, author *common.Address, gaspool *GasPool, statedb *state.StateDB, privateStateDb *state.StateDB, header *types.Header, tx *types.Transaction, cfg vm.Config, innerApply func(*types.Transaction) error) error {
  93. // Convert the transaction into an executable message and pre-cache its sender
  94. msg, err := tx.AsMessage(types.MakeSigner(config, header.Number))
  95. if err != nil {
  96. return err
  97. }
  98. // Quorum
  99. // Create the EVM and execute the transaction
  100. context := NewEVMBlockContext(header, bc, author)
  101. txContext := NewEVMTxContext(msg)
  102. var evm *vm.EVM
  103. // Quorum: Add privateStateDb argument
  104. if tx.IsPrivate() {
  105. evm = vm.NewEVM(context, txContext, statedb, privateStateDb, config, cfg)
  106. } else {
  107. evm = vm.NewEVM(context, txContext, statedb, statedb, config, cfg)
  108. }
  109. // End Quorum
  110. evm.SetCurrentTX(tx) // Quorum
  111. evm.InnerApply = innerApply
  112. // Add addresses to access list if applicable
  113. _, err = ApplyMessage(evm, msg, gaspool)
  114. return err
  115. }
  116. // Quorum
  117. func (p *statePrefetcher) prefetchMpsTransaction(block *types.Block, tx *types.Transaction, txIndex int, statedb *state.StateDB, privateStateRepo mps.PrivateStateRepository, cfg vm.Config, interrupt *uint32) {
  118. byzantium := p.config.IsByzantium(block.Number())
  119. // Block precaching permitted to continue, execute the transaction
  120. _, managedParties, _, _, err := private.P.Receive(common.BytesToEncryptedPayloadHash(tx.Data()))
  121. if err != nil {
  122. return
  123. }
  124. for _, managedParty := range managedParties {
  125. if interrupt != nil && atomic.LoadUint32(interrupt) == 1 {
  126. return
  127. }
  128. psMetadata, err := p.bc.PrivateStateManager().ResolveForManagedParty(managedParty)
  129. if err != nil {
  130. continue
  131. }
  132. privateStateDb, err := privateStateRepo.StatePSI(psMetadata.ID)
  133. if err != nil {
  134. continue
  135. }
  136. p.pend.Add(1)
  137. innerApply := createInnerApply(block, tx, txIndex, statedb, privateStateRepo, cfg, interrupt, p, privateStateDb)
  138. go func(start time.Time, followup *types.Block, statedb *state.StateDB, privateStateDb *state.StateDB, tx *types.Transaction, gaspool *GasPool) {
  139. privateStateDb.Prepare(tx.Hash(), block.Hash(), txIndex)
  140. if err := precacheTransaction(p.config, p.bc, nil, gaspool, statedb, privateStateDb, followup.Header(), tx, cfg, innerApply); err != nil {
  141. return
  142. }
  143. // If we're pre-byzantium, pre-load trie nodes for the intermediate root
  144. if !byzantium {
  145. privateStateDb.IntermediateRoot(true)
  146. }
  147. p.pend.Done()
  148. }(time.Now(), block, statedb, privateStateDb, tx, new(GasPool).AddGas(tx.Gas())) // TODO ricardolyn: which gas: block or Tx?
  149. }
  150. p.pend.Wait()
  151. }
  152. func createInnerApply(block *types.Block, tx *types.Transaction, txIndex int, statedb *state.StateDB, privateStateRepo mps.PrivateStateRepository, cfg vm.Config, interrupt *uint32, p *statePrefetcher, privateStateDb *state.StateDB) func(innerTx *types.Transaction) error {
  153. return func(innerTx *types.Transaction) error {
  154. if !tx.IsPrivacyMarker() {
  155. return nil
  156. } else if innerTx.IsPrivate() && privateStateRepo.IsMPS() {
  157. p.prefetchMpsTransaction(block, innerTx, txIndex, statedb.Copy(), privateStateRepo, cfg, interrupt)
  158. return nil
  159. } else {
  160. return precacheTransaction(p.config, p.bc, nil, new(GasPool).AddGas(innerTx.Gas()), statedb, privateStateDb, block.Header(), innerTx, cfg, nil)
  161. }
  162. }
  163. }
  164. // End Quorum