github.com/hyperion-hyn/go-ethereum@v2.4.0+incompatible/eth/api_backend.go (about) 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 17 package eth 18 19 import ( 20 "context" 21 "errors" 22 "math/big" 23 24 "github.com/ethereum/go-ethereum/accounts" 25 "github.com/ethereum/go-ethereum/common" 26 "github.com/ethereum/go-ethereum/common/math" 27 "github.com/ethereum/go-ethereum/core" 28 "github.com/ethereum/go-ethereum/core/bloombits" 29 "github.com/ethereum/go-ethereum/core/state" 30 "github.com/ethereum/go-ethereum/core/types" 31 "github.com/ethereum/go-ethereum/core/vm" 32 "github.com/ethereum/go-ethereum/eth/downloader" 33 "github.com/ethereum/go-ethereum/eth/gasprice" 34 "github.com/ethereum/go-ethereum/ethdb" 35 "github.com/ethereum/go-ethereum/event" 36 "github.com/ethereum/go-ethereum/params" 37 "github.com/ethereum/go-ethereum/rpc" 38 ) 39 40 // EthAPIBackend implements ethapi.Backend for full nodes 41 type EthAPIBackend struct { 42 eth *Ethereum 43 gpo *gasprice.Oracle 44 45 // Quorum 46 // 47 // hex node id from node public key 48 hexNodeId string 49 } 50 51 // ChainConfig returns the active chain configuration. 52 func (b *EthAPIBackend) ChainConfig() *params.ChainConfig { 53 return b.eth.chainConfig 54 } 55 56 func (b *EthAPIBackend) CurrentBlock() *types.Block { 57 return b.eth.blockchain.CurrentBlock() 58 } 59 60 func (b *EthAPIBackend) SetHead(number uint64) { 61 b.eth.protocolManager.downloader.Cancel() 62 b.eth.blockchain.SetHead(number) 63 } 64 65 func (b *EthAPIBackend) HeaderByNumber(ctx context.Context, blockNr rpc.BlockNumber) (*types.Header, error) { 66 // Pending block is only known by the miner 67 if blockNr == rpc.PendingBlockNumber { 68 block := b.eth.miner.PendingBlock() 69 return block.Header(), nil 70 } 71 // Otherwise resolve and return the block 72 if blockNr == rpc.LatestBlockNumber { 73 return b.eth.blockchain.CurrentBlock().Header(), nil 74 } 75 return b.eth.blockchain.GetHeaderByNumber(uint64(blockNr)), nil 76 } 77 78 func (b *EthAPIBackend) HeaderByHash(ctx context.Context, hash common.Hash) (*types.Header, error) { 79 return b.eth.blockchain.GetHeaderByHash(hash), nil 80 } 81 82 func (b *EthAPIBackend) BlockByNumber(ctx context.Context, blockNr rpc.BlockNumber) (*types.Block, error) { 83 // Pending block is only known by the miner 84 if blockNr == rpc.PendingBlockNumber { 85 if b.eth.protocolManager.raftMode { 86 // Use latest instead. 87 return b.eth.blockchain.CurrentBlock(), nil 88 } 89 block := b.eth.miner.PendingBlock() 90 return block, nil 91 } 92 // Otherwise resolve and return the block 93 if blockNr == rpc.LatestBlockNumber { 94 return b.eth.blockchain.CurrentBlock(), nil 95 } 96 return b.eth.blockchain.GetBlockByNumber(uint64(blockNr)), nil 97 } 98 99 func (b *EthAPIBackend) StateAndHeaderByNumber(ctx context.Context, blockNr rpc.BlockNumber) (vm.MinimalApiState, *types.Header, error) { 100 // Pending state is only known by the miner 101 if blockNr == rpc.PendingBlockNumber { 102 if b.eth.protocolManager.raftMode { 103 // Use latest instead. 104 header, err := b.HeaderByNumber(ctx, rpc.LatestBlockNumber) 105 if header == nil || err != nil { 106 return nil, nil, err 107 } 108 publicState, privateState, err := b.eth.BlockChain().StateAt(header.Root) 109 return EthAPIState{publicState, privateState}, header, err 110 } 111 block, publicState, privateState := b.eth.miner.Pending() 112 return EthAPIState{publicState, privateState}, block.Header(), nil 113 } 114 // Otherwise resolve the block number and return its state 115 header, err := b.HeaderByNumber(ctx, blockNr) 116 if header == nil || err != nil { 117 return nil, nil, err 118 } 119 stateDb, privateState, err := b.eth.BlockChain().StateAt(header.Root) 120 return EthAPIState{stateDb, privateState}, header, err 121 } 122 123 func (b *EthAPIBackend) GetBlock(ctx context.Context, hash common.Hash) (*types.Block, error) { 124 return b.eth.blockchain.GetBlockByHash(hash), nil 125 } 126 127 func (b *EthAPIBackend) GetReceipts(ctx context.Context, hash common.Hash) (types.Receipts, error) { 128 return b.eth.blockchain.GetReceiptsByHash(hash), nil 129 } 130 131 func (b *EthAPIBackend) GetLogs(ctx context.Context, hash common.Hash) ([][]*types.Log, error) { 132 receipts := b.eth.blockchain.GetReceiptsByHash(hash) 133 if receipts == nil { 134 return nil, nil 135 } 136 logs := make([][]*types.Log, len(receipts)) 137 for i, receipt := range receipts { 138 logs[i] = receipt.Logs 139 } 140 return logs, nil 141 } 142 143 func (b *EthAPIBackend) GetTd(blockHash common.Hash) *big.Int { 144 return b.eth.blockchain.GetTdByHash(blockHash) 145 } 146 147 func (b *EthAPIBackend) GetEVM(ctx context.Context, msg core.Message, state vm.MinimalApiState, header *types.Header, vmCfg vm.Config) (*vm.EVM, func() error, error) { 148 statedb := state.(EthAPIState) 149 from := statedb.state.GetOrNewStateObject(msg.From()) 150 from.SetBalance(math.MaxBig256) 151 vmError := func() error { return nil } 152 153 context := core.NewEVMContext(msg, header, b.eth.BlockChain(), nil) 154 155 // Set the private state to public state if contract address is not present in the private state 156 to := common.Address{} 157 if msg.To() != nil { 158 to = *msg.To() 159 } 160 161 privateState := statedb.privateState 162 if !privateState.Exist(to) { 163 privateState = statedb.state 164 } 165 166 return vm.NewEVM(context, statedb.state, privateState, b.eth.chainConfig, vmCfg), vmError, nil 167 } 168 169 func (b *EthAPIBackend) SubscribeRemovedLogsEvent(ch chan<- core.RemovedLogsEvent) event.Subscription { 170 return b.eth.BlockChain().SubscribeRemovedLogsEvent(ch) 171 } 172 173 func (b *EthAPIBackend) SubscribeChainEvent(ch chan<- core.ChainEvent) event.Subscription { 174 return b.eth.BlockChain().SubscribeChainEvent(ch) 175 } 176 177 func (b *EthAPIBackend) SubscribeChainHeadEvent(ch chan<- core.ChainHeadEvent) event.Subscription { 178 return b.eth.BlockChain().SubscribeChainHeadEvent(ch) 179 } 180 181 func (b *EthAPIBackend) SubscribeChainSideEvent(ch chan<- core.ChainSideEvent) event.Subscription { 182 return b.eth.BlockChain().SubscribeChainSideEvent(ch) 183 } 184 185 func (b *EthAPIBackend) SubscribeLogsEvent(ch chan<- []*types.Log) event.Subscription { 186 return b.eth.BlockChain().SubscribeLogsEvent(ch) 187 } 188 189 func (b *EthAPIBackend) SendTx(ctx context.Context, signedTx *types.Transaction) error { 190 // validation for node need to happen here and cannot be done as a part of 191 // validateTx in tx_pool.go as tx_pool validation will happen in every node 192 if b.hexNodeId != "" && !types.ValidateNodeForTxn(b.hexNodeId, signedTx.From()) { 193 return errors.New("cannot send transaction from this node") 194 } 195 return b.eth.txPool.AddLocal(signedTx) 196 } 197 198 func (b *EthAPIBackend) GetPoolTransactions() (types.Transactions, error) { 199 pending, err := b.eth.txPool.Pending() 200 if err != nil { 201 return nil, err 202 } 203 var txs types.Transactions 204 for _, batch := range pending { 205 txs = append(txs, batch...) 206 } 207 return txs, nil 208 } 209 210 func (b *EthAPIBackend) GetPoolTransaction(hash common.Hash) *types.Transaction { 211 return b.eth.txPool.Get(hash) 212 } 213 214 func (b *EthAPIBackend) GetPoolNonce(ctx context.Context, addr common.Address) (uint64, error) { 215 return b.eth.txPool.State().GetNonce(addr), nil 216 } 217 218 func (b *EthAPIBackend) Stats() (pending int, queued int) { 219 return b.eth.txPool.Stats() 220 } 221 222 func (b *EthAPIBackend) TxPoolContent() (map[common.Address]types.Transactions, map[common.Address]types.Transactions) { 223 return b.eth.TxPool().Content() 224 } 225 226 func (b *EthAPIBackend) SubscribeNewTxsEvent(ch chan<- core.NewTxsEvent) event.Subscription { 227 return b.eth.TxPool().SubscribeNewTxsEvent(ch) 228 } 229 230 func (b *EthAPIBackend) Downloader() *downloader.Downloader { 231 return b.eth.Downloader() 232 } 233 234 func (b *EthAPIBackend) ProtocolVersion() int { 235 return b.eth.EthVersion() 236 } 237 238 func (b *EthAPIBackend) SuggestPrice(ctx context.Context) (*big.Int, error) { 239 if b.ChainConfig().IsQuorum { 240 return big.NewInt(0), nil 241 } else { 242 return b.gpo.SuggestPrice(ctx) 243 } 244 } 245 246 func (b *EthAPIBackend) ChainDb() ethdb.Database { 247 return b.eth.ChainDb() 248 } 249 250 func (b *EthAPIBackend) EventMux() *event.TypeMux { 251 return b.eth.EventMux() 252 } 253 254 func (b *EthAPIBackend) AccountManager() *accounts.Manager { 255 return b.eth.AccountManager() 256 } 257 258 func (b *EthAPIBackend) BloomStatus() (uint64, uint64) { 259 sections, _, _ := b.eth.bloomIndexer.Sections() 260 return params.BloomBitsBlocks, sections 261 } 262 263 func (b *EthAPIBackend) ServiceFilter(ctx context.Context, session *bloombits.MatcherSession) { 264 for i := 0; i < bloomFilterThreads; i++ { 265 go session.Multiplex(bloomRetrievalBatch, bloomRetrievalWait, b.eth.bloomRequests) 266 } 267 } 268 269 // used by Quorum 270 type EthAPIState struct { 271 state, privateState *state.StateDB 272 } 273 274 func (s EthAPIState) GetBalance(addr common.Address) *big.Int { 275 if s.privateState.Exist(addr) { 276 return s.privateState.GetBalance(addr) 277 } 278 return s.state.GetBalance(addr) 279 } 280 281 func (s EthAPIState) GetCode(addr common.Address) []byte { 282 if s.privateState.Exist(addr) { 283 return s.privateState.GetCode(addr) 284 } 285 return s.state.GetCode(addr) 286 } 287 288 func (s EthAPIState) GetState(a common.Address, b common.Hash) common.Hash { 289 if s.privateState.Exist(a) { 290 return s.privateState.GetState(a, b) 291 } 292 return s.state.GetState(a, b) 293 } 294 295 func (s EthAPIState) GetNonce(addr common.Address) uint64 { 296 if s.privateState.Exist(addr) { 297 return s.privateState.GetNonce(addr) 298 } 299 return s.state.GetNonce(addr) 300 } 301 302 func (s EthAPIState) GetProof(addr common.Address) ([][]byte, error) { 303 if s.privateState.Exist(addr) { 304 return s.privateState.GetProof(addr) 305 } 306 return s.state.GetProof(addr) 307 } 308 309 func (s EthAPIState) GetStorageProof(addr common.Address, h common.Hash) ([][]byte, error) { 310 if s.privateState.Exist(addr) { 311 return s.privateState.GetStorageProof(addr, h) 312 } 313 return s.state.GetStorageProof(addr, h) 314 } 315 316 func (s EthAPIState) StorageTrie(addr common.Address) state.Trie { 317 if s.privateState.Exist(addr) { 318 return s.privateState.StorageTrie(addr) 319 } 320 return s.state.StorageTrie(addr) 321 } 322 323 func (s EthAPIState) Error() error { 324 if s.privateState.Error() != nil { 325 return s.privateState.Error() 326 } 327 return s.state.Error() 328 } 329 330 func (s EthAPIState) GetCodeHash(addr common.Address) common.Hash { 331 if s.privateState.Exist(addr) { 332 return s.privateState.GetCodeHash(addr) 333 } 334 return s.state.GetCodeHash(addr) 335 } 336 337 //func (s MinimalApiState) Error