api_backend.go 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624
  1. // Copyright 2015 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 eth
  17. import (
  18. "context"
  19. "errors"
  20. "fmt"
  21. "math/big"
  22. "time"
  23. "github.com/ethereum/go-ethereum/accounts"
  24. "github.com/ethereum/go-ethereum/common"
  25. "github.com/ethereum/go-ethereum/consensus"
  26. "github.com/ethereum/go-ethereum/core"
  27. "github.com/ethereum/go-ethereum/core/bloombits"
  28. "github.com/ethereum/go-ethereum/core/mps"
  29. "github.com/ethereum/go-ethereum/core/rawdb"
  30. "github.com/ethereum/go-ethereum/core/state"
  31. "github.com/ethereum/go-ethereum/core/types"
  32. "github.com/ethereum/go-ethereum/core/vm"
  33. "github.com/ethereum/go-ethereum/eth/downloader"
  34. "github.com/ethereum/go-ethereum/eth/gasprice"
  35. "github.com/ethereum/go-ethereum/eth/tracers"
  36. "github.com/ethereum/go-ethereum/ethdb"
  37. "github.com/ethereum/go-ethereum/event"
  38. "github.com/ethereum/go-ethereum/internal/ethapi"
  39. "github.com/ethereum/go-ethereum/miner"
  40. "github.com/ethereum/go-ethereum/p2p/enode"
  41. "github.com/ethereum/go-ethereum/params"
  42. pcore "github.com/ethereum/go-ethereum/permission/core"
  43. "github.com/ethereum/go-ethereum/rpc"
  44. "github.com/jpmorganchase/quorum-security-plugin-sdk-go/proto"
  45. )
  46. // EthAPIBackend implements ethapi.Backend for full nodes
  47. type EthAPIBackend struct {
  48. extRPCEnabled bool
  49. allowUnprotectedTxs bool
  50. eth *Ethereum
  51. gpo *gasprice.Oracle
  52. // Quorum
  53. //
  54. // node id from node public key
  55. nodeId enode.ID
  56. // timeout value for call
  57. evmCallTimeOut time.Duration
  58. // Quorum
  59. proxyClient *rpc.Client
  60. }
  61. var _ ethapi.Backend = &EthAPIBackend{}
  62. var _ tracers.Backend = &EthAPIBackend{}
  63. func (b *EthAPIBackend) ProxyEnabled() bool {
  64. return b.eth.config.QuorumLightClient.Enabled()
  65. }
  66. func (b *EthAPIBackend) ProxyClient() *rpc.Client {
  67. return b.proxyClient
  68. }
  69. // ChainConfig returns the active chain configuration.
  70. func (b *EthAPIBackend) ChainConfig() *params.ChainConfig {
  71. return b.eth.blockchain.Config()
  72. }
  73. // PSMR returns the private state metadata resolver.
  74. func (b *EthAPIBackend) PSMR() mps.PrivateStateMetadataResolver {
  75. return b.eth.blockchain.PrivateStateManager()
  76. }
  77. func (b *EthAPIBackend) CurrentBlock() *types.Block {
  78. return b.eth.blockchain.CurrentBlock()
  79. }
  80. func (b *EthAPIBackend) SetHead(number uint64) {
  81. b.eth.handler.downloader.Cancel()
  82. b.eth.blockchain.SetHead(number)
  83. }
  84. func (b *EthAPIBackend) HeaderByNumber(ctx context.Context, number rpc.BlockNumber) (*types.Header, error) {
  85. // Pending block is only known by the miner
  86. if number == rpc.PendingBlockNumber {
  87. block := b.eth.miner.PendingBlock()
  88. return block.Header(), nil
  89. }
  90. // Otherwise resolve and return the block
  91. if number == rpc.LatestBlockNumber {
  92. return b.eth.blockchain.CurrentBlock().Header(), nil
  93. }
  94. return b.eth.blockchain.GetHeaderByNumber(uint64(number)), nil
  95. }
  96. func (b *EthAPIBackend) HeaderByNumberOrHash(ctx context.Context, blockNrOrHash rpc.BlockNumberOrHash) (*types.Header, error) {
  97. if blockNr, ok := blockNrOrHash.Number(); ok {
  98. return b.HeaderByNumber(ctx, blockNr)
  99. }
  100. if hash, ok := blockNrOrHash.Hash(); ok {
  101. header := b.eth.blockchain.GetHeaderByHash(hash)
  102. if header == nil {
  103. return nil, errors.New("header for hash not found")
  104. }
  105. if blockNrOrHash.RequireCanonical && b.eth.blockchain.GetCanonicalHash(header.Number.Uint64()) != hash {
  106. return nil, errors.New("hash is not currently canonical")
  107. }
  108. return header, nil
  109. }
  110. return nil, errors.New("invalid arguments; neither block nor hash specified")
  111. }
  112. func (b *EthAPIBackend) HeaderByHash(ctx context.Context, hash common.Hash) (*types.Header, error) {
  113. return b.eth.blockchain.GetHeaderByHash(hash), nil
  114. }
  115. func (b *EthAPIBackend) BlockByNumber(ctx context.Context, number rpc.BlockNumber) (*types.Block, error) {
  116. // Pending block is only known by the miner
  117. if number == rpc.PendingBlockNumber {
  118. if b.eth.handler.raftMode {
  119. // Use latest instead.
  120. return b.eth.blockchain.CurrentBlock(), nil
  121. }
  122. block := b.eth.miner.PendingBlock()
  123. return block, nil
  124. }
  125. // Otherwise resolve and return the block
  126. if number == rpc.LatestBlockNumber {
  127. return b.eth.blockchain.CurrentBlock(), nil
  128. }
  129. return b.eth.blockchain.GetBlockByNumber(uint64(number)), nil
  130. }
  131. func (b *EthAPIBackend) BlockByHash(ctx context.Context, hash common.Hash) (*types.Block, error) {
  132. return b.eth.blockchain.GetBlockByHash(hash), nil
  133. }
  134. func (b *EthAPIBackend) BlockByNumberOrHash(ctx context.Context, blockNrOrHash rpc.BlockNumberOrHash) (*types.Block, error) {
  135. if blockNr, ok := blockNrOrHash.Number(); ok {
  136. return b.BlockByNumber(ctx, blockNr)
  137. }
  138. if hash, ok := blockNrOrHash.Hash(); ok {
  139. header := b.eth.blockchain.GetHeaderByHash(hash)
  140. if header == nil {
  141. return nil, errors.New("header for hash not found")
  142. }
  143. if blockNrOrHash.RequireCanonical && b.eth.blockchain.GetCanonicalHash(header.Number.Uint64()) != hash {
  144. return nil, errors.New("hash is not currently canonical")
  145. }
  146. block := b.eth.blockchain.GetBlock(hash, header.Number.Uint64())
  147. if block == nil {
  148. return nil, errors.New("header found, but block body is missing")
  149. }
  150. return block, nil
  151. }
  152. return nil, errors.New("invalid arguments; neither block nor hash specified")
  153. }
  154. func (b *EthAPIBackend) StateAndHeaderByNumber(ctx context.Context, number rpc.BlockNumber) (vm.MinimalApiState, *types.Header, error) {
  155. psm, err := b.PSMR().ResolveForUserContext(ctx)
  156. if err != nil {
  157. return nil, nil, err
  158. }
  159. // Pending state is only known by the miner
  160. if number == rpc.PendingBlockNumber {
  161. // Quorum
  162. if b.eth.handler.raftMode {
  163. // Use latest instead.
  164. header, err := b.HeaderByNumber(ctx, rpc.LatestBlockNumber)
  165. if header == nil || err != nil {
  166. return nil, nil, err
  167. }
  168. publicState, privateState, err := b.eth.BlockChain().StateAtPSI(header.Root, psm.ID)
  169. return EthAPIState{publicState, privateState}, header, err
  170. }
  171. block, publicState, privateState := b.eth.miner.Pending(psm.ID)
  172. if block == nil || publicState == nil || privateState == nil {
  173. return nil, nil, fmt.Errorf("Unable to retrieve the pending state from the miner.")
  174. }
  175. return EthAPIState{publicState, privateState}, block.Header(), nil
  176. }
  177. // Otherwise resolve the block number and return its state
  178. header, err := b.HeaderByNumber(ctx, number)
  179. if err != nil {
  180. return nil, nil, err
  181. }
  182. if header == nil {
  183. return nil, nil, errors.New("header not found")
  184. }
  185. stateDb, privateState, err := b.eth.BlockChain().StateAtPSI(header.Root, psm.ID)
  186. return EthAPIState{stateDb, privateState}, header, err
  187. }
  188. func (b *EthAPIBackend) StateAndHeaderByNumberOrHash(ctx context.Context, blockNrOrHash rpc.BlockNumberOrHash) (vm.MinimalApiState, *types.Header, error) {
  189. if blockNr, ok := blockNrOrHash.Number(); ok {
  190. return b.StateAndHeaderByNumber(ctx, blockNr)
  191. }
  192. if hash, ok := blockNrOrHash.Hash(); ok {
  193. header, err := b.HeaderByHash(ctx, hash)
  194. if err != nil {
  195. return nil, nil, err
  196. }
  197. if header == nil {
  198. return nil, nil, errors.New("header for hash not found")
  199. }
  200. if blockNrOrHash.RequireCanonical && b.eth.blockchain.GetCanonicalHash(header.Number.Uint64()) != hash {
  201. return nil, nil, errors.New("hash is not currently canonical")
  202. }
  203. psm, err := b.PSMR().ResolveForUserContext(ctx)
  204. if err != nil {
  205. return nil, nil, err
  206. }
  207. stateDb, privateState, err := b.eth.BlockChain().StateAtPSI(header.Root, psm.ID)
  208. return EthAPIState{stateDb, privateState}, header, err
  209. }
  210. return nil, nil, errors.New("invalid arguments; neither block nor hash specified")
  211. }
  212. // Modified for Quorum:
  213. // - If MPS is enabled then the list of receipts returned will contain all public receipts, plus the private receipts for this PSI.
  214. // - if MPS is not enabled, then list will contain all public and private receipts
  215. // Note that for a privacy marker transactions, the private receipts will remain under PSReceipts
  216. func (b *EthAPIBackend) GetReceipts(ctx context.Context, hash common.Hash) (types.Receipts, error) {
  217. receipts := b.eth.blockchain.GetReceiptsByHash(hash)
  218. psm, err := b.PSMR().ResolveForUserContext(ctx)
  219. if err != nil {
  220. return nil, err
  221. }
  222. psiReceipts := make([]*types.Receipt, len(receipts))
  223. for i := 0; i < len(receipts); i++ {
  224. psiReceipts[i] = receipts[i]
  225. if receipts[i].PSReceipts != nil {
  226. psReceipt, found := receipts[i].PSReceipts[psm.ID]
  227. // if PSReceipt found and this is not a privacy marker transaction receipt, then pull out the PSI receipt
  228. if found && receipts[i].TxHash == psReceipt.TxHash {
  229. psiReceipts[i] = psReceipt
  230. }
  231. }
  232. }
  233. return psiReceipts, nil
  234. }
  235. func (b *EthAPIBackend) GetLogs(ctx context.Context, hash common.Hash) ([][]*types.Log, error) {
  236. // Quorum
  237. // We should use the modified getReceipts to get the private receipts for PSI (MPS)
  238. receipts, err := b.GetReceipts(ctx, hash)
  239. if err != nil {
  240. return nil, err
  241. }
  242. // End Quorum
  243. if receipts == nil {
  244. return nil, nil
  245. }
  246. privateReceipts, err := b.eth.blockchain.GetPMTPrivateReceiptsByHash(ctx, hash)
  247. if err != nil {
  248. return nil, err
  249. }
  250. logs := make([][]*types.Log, len(receipts)+len(privateReceipts))
  251. for i, receipt := range receipts {
  252. logs[i] = receipt.Logs
  253. }
  254. for i, receipt := range privateReceipts {
  255. logs[len(receipts)+i] = receipt.Logs
  256. }
  257. return logs, nil
  258. }
  259. func (b *EthAPIBackend) GetTd(ctx context.Context, hash common.Hash) *big.Int {
  260. return b.eth.blockchain.GetTdByHash(hash)
  261. }
  262. func (b *EthAPIBackend) GetEVM(ctx context.Context, msg core.Message, state vm.MinimalApiState, header *types.Header, vmConfig *vm.Config) (*vm.EVM, func() error, error) {
  263. statedb := state.(EthAPIState)
  264. vmError := func() error { return nil }
  265. if vmConfig == nil {
  266. vmConfig = b.eth.blockchain.GetVMConfig()
  267. }
  268. txContext := core.NewEVMTxContext(msg)
  269. context := core.NewEVMBlockContext(header, b.eth.BlockChain(), nil)
  270. // Quorum
  271. // Set the private state to public state if contract address is not present in the private state
  272. to := common.Address{}
  273. if msg.To() != nil {
  274. to = *msg.To()
  275. }
  276. privateState := statedb.privateState
  277. if !privateState.Exist(to) {
  278. privateState = statedb.state
  279. }
  280. // End Quorum
  281. return vm.NewEVM(context, txContext, statedb.state, privateState, b.eth.blockchain.Config(), *vmConfig), vmError, nil
  282. }
  283. func (b *EthAPIBackend) SubscribeRemovedLogsEvent(ch chan<- core.RemovedLogsEvent) event.Subscription {
  284. return b.eth.BlockChain().SubscribeRemovedLogsEvent(ch)
  285. }
  286. func (b *EthAPIBackend) SubscribePendingLogsEvent(ch chan<- []*types.Log) event.Subscription {
  287. return b.eth.SubscribePendingLogs(ch) // Quorum
  288. }
  289. func (b *EthAPIBackend) SubscribeChainEvent(ch chan<- core.ChainEvent) event.Subscription {
  290. return b.eth.BlockChain().SubscribeChainEvent(ch)
  291. }
  292. func (b *EthAPIBackend) SubscribeChainHeadEvent(ch chan<- core.ChainHeadEvent) event.Subscription {
  293. return b.eth.BlockChain().SubscribeChainHeadEvent(ch)
  294. }
  295. func (b *EthAPIBackend) SubscribeChainSideEvent(ch chan<- core.ChainSideEvent) event.Subscription {
  296. return b.eth.BlockChain().SubscribeChainSideEvent(ch)
  297. }
  298. func (b *EthAPIBackend) SubscribeLogsEvent(ch chan<- []*types.Log) event.Subscription {
  299. return b.eth.BlockChain().SubscribeLogsEvent(ch)
  300. }
  301. func (b *EthAPIBackend) SendTx(ctx context.Context, signedTx *types.Transaction) error {
  302. // Quourum
  303. // validation for node need to happen here and cannot be done as a part of
  304. // validateTx in tx_pool.go as tx_pool validation will happen in every node
  305. if !pcore.ValidateNodeForTxn(b.nodeId, signedTx.From()) {
  306. return errors.New("cannot send transaction from this node")
  307. }
  308. // End Quorum
  309. return b.eth.txPool.AddLocal(signedTx)
  310. }
  311. func (b *EthAPIBackend) GetPoolTransactions() (types.Transactions, error) {
  312. pending, err := b.eth.txPool.Pending()
  313. if err != nil {
  314. return nil, err
  315. }
  316. var txs types.Transactions
  317. for _, batch := range pending {
  318. txs = append(txs, batch...)
  319. }
  320. return txs, nil
  321. }
  322. func (b *EthAPIBackend) GetPoolTransaction(hash common.Hash) *types.Transaction {
  323. return b.eth.txPool.Get(hash)
  324. }
  325. func (b *EthAPIBackend) GetTransaction(ctx context.Context, txHash common.Hash) (*types.Transaction, common.Hash, uint64, uint64, error) {
  326. tx, blockHash, blockNumber, index := rawdb.ReadTransaction(b.eth.ChainDb(), txHash)
  327. return tx, blockHash, blockNumber, index, nil
  328. }
  329. func (b *EthAPIBackend) GetPoolNonce(ctx context.Context, addr common.Address) (uint64, error) {
  330. return b.eth.txPool.Nonce(addr), nil
  331. }
  332. func (b *EthAPIBackend) Stats() (pending int, queued int) {
  333. return b.eth.txPool.Stats()
  334. }
  335. func (b *EthAPIBackend) TxPoolContent() (map[common.Address]types.Transactions, map[common.Address]types.Transactions) {
  336. return b.eth.TxPool().Content()
  337. }
  338. func (b *EthAPIBackend) TxPool() *core.TxPool {
  339. return b.eth.TxPool()
  340. }
  341. func (b *EthAPIBackend) SubscribeNewTxsEvent(ch chan<- core.NewTxsEvent) event.Subscription {
  342. return b.eth.TxPool().SubscribeNewTxsEvent(ch)
  343. }
  344. func (b *EthAPIBackend) Downloader() *downloader.Downloader {
  345. return b.eth.Downloader()
  346. }
  347. func (b *EthAPIBackend) SuggestPrice(ctx context.Context) (*big.Int, error) {
  348. if !b.ChainConfig().IsQuorum || b.ChainConfig().IsGasPriceEnabled(b.eth.blockchain.CurrentBlock().Number()) {
  349. return b.gpo.SuggestPrice(ctx)
  350. } else {
  351. return big.NewInt(0), nil
  352. }
  353. }
  354. func (b *EthAPIBackend) ChainDb() ethdb.Database {
  355. return b.eth.ChainDb()
  356. }
  357. func (b *EthAPIBackend) EventMux() *event.TypeMux {
  358. return b.eth.EventMux()
  359. }
  360. func (b *EthAPIBackend) AccountManager() *accounts.Manager {
  361. return b.eth.AccountManager()
  362. }
  363. func (b *EthAPIBackend) ExtRPCEnabled() bool {
  364. return b.extRPCEnabled
  365. }
  366. func (b *EthAPIBackend) UnprotectedAllowed() bool {
  367. return b.allowUnprotectedTxs
  368. }
  369. func (b *EthAPIBackend) RPCGasCap() uint64 {
  370. return b.eth.config.RPCGasCap
  371. }
  372. func (b *EthAPIBackend) RPCTxFeeCap() float64 {
  373. return b.eth.config.RPCTxFeeCap
  374. }
  375. func (b *EthAPIBackend) BloomStatus() (uint64, uint64) {
  376. sections, _, _ := b.eth.bloomIndexer.Sections()
  377. return params.BloomBitsBlocks, sections
  378. }
  379. func (b *EthAPIBackend) ServiceFilter(ctx context.Context, session *bloombits.MatcherSession) {
  380. for i := 0; i < bloomFilterThreads; i++ {
  381. go session.Multiplex(bloomRetrievalBatch, bloomRetrievalWait, b.eth.bloomRequests)
  382. }
  383. }
  384. func (b *EthAPIBackend) Engine() consensus.Engine {
  385. return b.eth.engine
  386. }
  387. func (b *EthAPIBackend) CurrentHeader() *types.Header {
  388. return b.eth.blockchain.CurrentHeader()
  389. }
  390. func (b *EthAPIBackend) Miner() *miner.Miner {
  391. return b.eth.Miner()
  392. }
  393. func (b *EthAPIBackend) StartMining(threads int) error {
  394. return b.eth.StartMining(threads)
  395. }
  396. func (b *EthAPIBackend) StateAtBlock(ctx context.Context, block *types.Block, reexec uint64, base *state.StateDB, checkLive bool) (*state.StateDB, mps.PrivateStateRepository, error) {
  397. return b.eth.stateAtBlock(block, reexec, base, checkLive)
  398. }
  399. func (b *EthAPIBackend) StateAtTransaction(ctx context.Context, block *types.Block, txIndex int, reexec uint64) (core.Message, vm.BlockContext, *state.StateDB, *state.StateDB, mps.PrivateStateRepository, error) {
  400. return b.eth.stateAtTransaction(ctx, block, txIndex, reexec)
  401. }
  402. // Quorum
  403. func (b *EthAPIBackend) CallTimeOut() time.Duration {
  404. return b.evmCallTimeOut
  405. }
  406. func (b *EthAPIBackend) GetBlockchain() *core.BlockChain {
  407. return b.eth.BlockChain()
  408. }
  409. // The validation of pre-requisite for multitenancy is done when EthService
  410. // is being created. So it's safe to use the config value here.
  411. func (b *EthAPIBackend) SupportsMultitenancy(rpcCtx context.Context) (*proto.PreAuthenticatedAuthenticationToken, bool) {
  412. authToken := rpc.PreauthenticatedTokenFromContext(rpcCtx)
  413. if authToken != nil && b.eth.config.MultiTenantEnabled() {
  414. return authToken, true
  415. }
  416. return nil, false
  417. }
  418. func (b *EthAPIBackend) AccountExtraDataStateGetterByNumber(ctx context.Context, number rpc.BlockNumber) (vm.AccountExtraDataStateGetter, error) {
  419. s, _, err := b.StateAndHeaderByNumber(ctx, number)
  420. return s, err
  421. }
  422. func (b *EthAPIBackend) IsPrivacyMarkerTransactionCreationEnabled() bool {
  423. return b.eth.config.QuorumChainConfig.PrivacyMarkerEnabled() && b.ChainConfig().IsPrivacyPrecompileEnabled(b.eth.blockchain.CurrentBlock().Number())
  424. }
  425. // used by Quorum
  426. type EthAPIState struct {
  427. state, privateState *state.StateDB
  428. }
  429. func (s EthAPIState) GetBalance(addr common.Address) *big.Int {
  430. if s.privateState.Exist(addr) {
  431. return s.privateState.GetBalance(addr)
  432. }
  433. return s.state.GetBalance(addr)
  434. }
  435. func (s EthAPIState) GetCode(addr common.Address) []byte {
  436. if s.privateState.Exist(addr) {
  437. return s.privateState.GetCode(addr)
  438. }
  439. return s.state.GetCode(addr)
  440. }
  441. func (s EthAPIState) SetNonce(addr common.Address, nonce uint64) {
  442. if s.privateState.Exist(addr) {
  443. s.privateState.SetNonce(addr, nonce)
  444. } else {
  445. s.state.SetNonce(addr, nonce)
  446. }
  447. }
  448. func (s EthAPIState) SetCode(addr common.Address, code []byte) {
  449. if s.privateState.Exist(addr) {
  450. s.privateState.SetCode(addr, code)
  451. } else {
  452. s.state.SetCode(addr, code)
  453. }
  454. }
  455. func (s EthAPIState) SetBalance(addr common.Address, balance *big.Int) {
  456. if s.privateState.Exist(addr) {
  457. s.privateState.SetBalance(addr, balance)
  458. } else {
  459. s.state.SetBalance(addr, balance)
  460. }
  461. }
  462. func (s EthAPIState) SetStorage(addr common.Address, storage map[common.Hash]common.Hash) {
  463. if s.privateState.Exist(addr) {
  464. s.privateState.SetStorage(addr, storage)
  465. } else {
  466. s.state.SetStorage(addr, storage)
  467. }
  468. }
  469. func (s EthAPIState) SetState(a common.Address, key common.Hash, value common.Hash) {
  470. if s.privateState.Exist(a) {
  471. s.privateState.SetState(a, key, value)
  472. } else {
  473. s.state.SetState(a, key, value)
  474. }
  475. }
  476. func (s EthAPIState) GetState(a common.Address, b common.Hash) common.Hash {
  477. if s.privateState.Exist(a) {
  478. return s.privateState.GetState(a, b)
  479. }
  480. return s.state.GetState(a, b)
  481. }
  482. func (s EthAPIState) GetNonce(addr common.Address) uint64 {
  483. if s.privateState.Exist(addr) {
  484. return s.privateState.GetNonce(addr)
  485. }
  486. return s.state.GetNonce(addr)
  487. }
  488. func (s EthAPIState) GetPrivacyMetadata(addr common.Address) (*state.PrivacyMetadata, error) {
  489. if s.privateState.Exist(addr) {
  490. return s.privateState.GetPrivacyMetadata(addr)
  491. }
  492. return nil, fmt.Errorf("%x: %w", addr, common.ErrNotPrivateContract)
  493. }
  494. func (s EthAPIState) GetManagedParties(addr common.Address) ([]string, error) {
  495. if s.privateState.Exist(addr) {
  496. return s.privateState.GetManagedParties(addr)
  497. }
  498. return nil, fmt.Errorf("%x: %w", addr, common.ErrNotPrivateContract)
  499. }
  500. func (s EthAPIState) GetRLPEncodedStateObject(addr common.Address) ([]byte, error) {
  501. getFunc := s.state.GetRLPEncodedStateObject
  502. if s.privateState.Exist(addr) {
  503. getFunc = s.privateState.GetRLPEncodedStateObject
  504. }
  505. return getFunc(addr)
  506. }
  507. func (s EthAPIState) GetProof(addr common.Address) ([][]byte, error) {
  508. if s.privateState.Exist(addr) {
  509. return s.privateState.GetProof(addr)
  510. }
  511. return s.state.GetProof(addr)
  512. }
  513. func (s EthAPIState) GetStorageProof(addr common.Address, h common.Hash) ([][]byte, error) {
  514. if s.privateState.Exist(addr) {
  515. return s.privateState.GetStorageProof(addr, h)
  516. }
  517. return s.state.GetStorageProof(addr, h)
  518. }
  519. func (s EthAPIState) StorageTrie(addr common.Address) state.Trie {
  520. if s.privateState.Exist(addr) {
  521. return s.privateState.StorageTrie(addr)
  522. }
  523. return s.state.StorageTrie(addr)
  524. }
  525. func (s EthAPIState) Error() error {
  526. if s.privateState.Error() != nil {
  527. return s.privateState.Error()
  528. }
  529. return s.state.Error()
  530. }
  531. func (s EthAPIState) GetCodeHash(addr common.Address) common.Hash {
  532. if s.privateState.Exist(addr) {
  533. return s.privateState.GetCodeHash(addr)
  534. }
  535. return s.state.GetCodeHash(addr)
  536. }