github.com/codingfuture/orig-energi3@v0.8.4/eth/api_backend.go (about) 1 // Copyright 2018 The Energi Core Authors 2 // Copyright 2015 The go-ethereum Authors 3 // This file is part of the Energi Core library. 4 // 5 // The Energi Core library is free software: you can redistribute it and/or modify 6 // it under the terms of the GNU Lesser General Public License as published by 7 // the Free Software Foundation, either version 3 of the License, or 8 // (at your option) any later version. 9 // 10 // The Energi Core library is distributed in the hope that it will be useful, 11 // but WITHOUT ANY WARRANTY; without even the implied warranty of 12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 // GNU Lesser General Public License for more details. 14 // 15 // You should have received a copy of the GNU Lesser General Public License 16 // along with the Energi Core library. If not, see <http://www.gnu.org/licenses/>. 17 18 package eth 19 20 import ( 21 "context" 22 "math/big" 23 "time" 24 25 "github.com/ethereum/go-ethereum/accounts" 26 "github.com/ethereum/go-ethereum/common" 27 "github.com/ethereum/go-ethereum/common/math" 28 "github.com/ethereum/go-ethereum/core" 29 "github.com/ethereum/go-ethereum/core/bloombits" 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/ethdb" 36 "github.com/ethereum/go-ethereum/event" 37 "github.com/ethereum/go-ethereum/log" 38 "github.com/ethereum/go-ethereum/params" 39 "github.com/ethereum/go-ethereum/rpc" 40 ) 41 42 // EthAPIBackend implements ethapi.Backend for full nodes 43 type EthAPIBackend struct { 44 eth *Ethereum 45 gpo *gasprice.Oracle 46 } 47 48 // ChainConfig returns the active chain configuration. 49 func (b *EthAPIBackend) ChainConfig() *params.ChainConfig { 50 return b.eth.chainConfig 51 } 52 53 func (b *EthAPIBackend) CurrentBlock() *types.Block { 54 return b.eth.blockchain.CurrentBlock() 55 } 56 57 func (b *EthAPIBackend) SetHead(number uint64) { 58 b.eth.protocolManager.downloader.Cancel() 59 b.eth.blockchain.SetHead(number) 60 } 61 62 func (b *EthAPIBackend) HeaderByNumber(ctx context.Context, blockNr rpc.BlockNumber) (*types.Header, error) { 63 // Pending block is only known by the miner 64 if blockNr == rpc.PendingBlockNumber { 65 block := b.eth.miner.PendingBlock() 66 return block.Header(), nil 67 } 68 // Otherwise resolve and return the block 69 if blockNr == rpc.LatestBlockNumber { 70 return b.eth.blockchain.CurrentBlock().Header(), nil 71 } 72 return b.eth.blockchain.GetHeaderByNumber(uint64(blockNr)), nil 73 } 74 75 func (b *EthAPIBackend) HeaderByHash(ctx context.Context, hash common.Hash) (*types.Header, error) { 76 return b.eth.blockchain.GetHeaderByHash(hash), nil 77 } 78 79 func (b *EthAPIBackend) BlockByNumber(ctx context.Context, blockNr rpc.BlockNumber) (*types.Block, error) { 80 // Pending block is only known by the miner 81 if blockNr == rpc.PendingBlockNumber { 82 block := b.eth.miner.PendingBlock() 83 return block, nil 84 } 85 // Otherwise resolve and return the block 86 if blockNr == rpc.LatestBlockNumber { 87 return b.eth.blockchain.CurrentBlock(), nil 88 } 89 return b.eth.blockchain.GetBlockByNumber(uint64(blockNr)), nil 90 } 91 92 func (b *EthAPIBackend) StateAndHeaderByNumber(ctx context.Context, blockNr rpc.BlockNumber) (*state.StateDB, *types.Header, error) { 93 // Pending state is only known by the miner 94 if blockNr == rpc.PendingBlockNumber { 95 block, state := b.eth.miner.Pending() 96 return state, block.Header(), nil 97 } 98 // Otherwise resolve the block number and return its state 99 header, err := b.HeaderByNumber(ctx, blockNr) 100 if header == nil || err != nil { 101 return nil, nil, err 102 } 103 stateDb, err := b.eth.BlockChain().StateAt(header.Root) 104 return stateDb, header, err 105 } 106 107 func (b *EthAPIBackend) GetBlock(ctx context.Context, hash common.Hash) (*types.Block, error) { 108 return b.eth.blockchain.GetBlockByHash(hash), nil 109 } 110 111 func (b *EthAPIBackend) GetReceipts(ctx context.Context, hash common.Hash) (types.Receipts, error) { 112 return b.eth.blockchain.GetReceiptsByHash(hash), nil 113 } 114 115 func (b *EthAPIBackend) GetLogs(ctx context.Context, hash common.Hash) ([][]*types.Log, error) { 116 receipts := b.eth.blockchain.GetReceiptsByHash(hash) 117 if receipts == nil { 118 return nil, nil 119 } 120 logs := make([][]*types.Log, len(receipts)) 121 for i, receipt := range receipts { 122 logs[i] = receipt.Logs 123 } 124 return logs, nil 125 } 126 127 func (b *EthAPIBackend) GetTd(blockHash common.Hash) *big.Int { 128 return b.eth.blockchain.GetTdByHash(blockHash) 129 } 130 131 func (b *EthAPIBackend) GetEVM(ctx context.Context, msg core.Message, state *state.StateDB, header *types.Header) (*vm.EVM, func() error, error) { 132 state.SetBalance(msg.From(), math.MaxBig256) 133 vmError := func() error { return nil } 134 135 context := core.NewEVMContext(msg, header, b.eth.BlockChain(), nil) 136 return vm.NewEVM(context, state, b.eth.chainConfig, *b.eth.blockchain.GetVMConfig()), vmError, nil 137 } 138 139 func (b *EthAPIBackend) SubscribeRemovedLogsEvent(ch chan<- core.RemovedLogsEvent) event.Subscription { 140 return b.eth.BlockChain().SubscribeRemovedLogsEvent(ch) 141 } 142 143 func (b *EthAPIBackend) SubscribeChainEvent(ch chan<- core.ChainEvent) event.Subscription { 144 return b.eth.BlockChain().SubscribeChainEvent(ch) 145 } 146 147 func (b *EthAPIBackend) SubscribeChainHeadEvent(ch chan<- core.ChainHeadEvent) event.Subscription { 148 return b.eth.BlockChain().SubscribeChainHeadEvent(ch) 149 } 150 151 func (b *EthAPIBackend) SubscribeChainSideEvent(ch chan<- core.ChainSideEvent) event.Subscription { 152 return b.eth.BlockChain().SubscribeChainSideEvent(ch) 153 } 154 155 func (b *EthAPIBackend) SubscribeLogsEvent(ch chan<- []*types.Log) event.Subscription { 156 return b.eth.BlockChain().SubscribeLogsEvent(ch) 157 } 158 159 func (b *EthAPIBackend) SendTx(ctx context.Context, signedTx *types.Transaction) error { 160 return b.eth.txPool.AddLocal(signedTx) 161 } 162 163 func (b *EthAPIBackend) GetPoolTransactions() (types.Transactions, error) { 164 pending, err := b.eth.txPool.Pending() 165 if err != nil { 166 return nil, err 167 } 168 var txs types.Transactions 169 for _, batch := range pending { 170 txs = append(txs, batch...) 171 } 172 return txs, nil 173 } 174 175 func (b *EthAPIBackend) GetPoolTransaction(hash common.Hash) *types.Transaction { 176 return b.eth.txPool.Get(hash) 177 } 178 179 func (b *EthAPIBackend) GetPoolNonce(ctx context.Context, addr common.Address) (uint64, error) { 180 return b.eth.txPool.State().GetNonce(addr), nil 181 } 182 183 func (b *EthAPIBackend) Stats() (pending int, queued int) { 184 return b.eth.txPool.Stats() 185 } 186 187 func (b *EthAPIBackend) TxPoolContent() (map[common.Address]types.Transactions, map[common.Address]types.Transactions) { 188 return b.eth.TxPool().Content() 189 } 190 191 func (b *EthAPIBackend) SubscribeNewTxsEvent(ch chan<- core.NewTxsEvent) event.Subscription { 192 return b.eth.TxPool().SubscribeNewTxsEvent(ch) 193 } 194 195 func (b *EthAPIBackend) Downloader() *downloader.Downloader { 196 return b.eth.Downloader() 197 } 198 199 func (b *EthAPIBackend) ProtocolVersion() int { 200 return b.eth.EthVersion() 201 } 202 203 func (b *EthAPIBackend) SuggestPrice(ctx context.Context) (*big.Int, error) { 204 return b.gpo.SuggestPrice(ctx) 205 } 206 207 func (b *EthAPIBackend) ChainDb() ethdb.Database { 208 return b.eth.ChainDb() 209 } 210 211 func (b *EthAPIBackend) EventMux() *event.TypeMux { 212 return b.eth.EventMux() 213 } 214 215 func (b *EthAPIBackend) AccountManager() *accounts.Manager { 216 return b.eth.AccountManager() 217 } 218 219 func (b *EthAPIBackend) RPCGasCap() *big.Int { 220 return b.eth.config.RPCGasCap 221 } 222 223 func (b *EthAPIBackend) BloomStatus() (uint64, uint64) { 224 sections, _, _ := b.eth.bloomIndexer.Sections() 225 return params.BloomBitsBlocks, sections 226 } 227 228 func (b *EthAPIBackend) ServiceFilter(ctx context.Context, session *bloombits.MatcherSession) { 229 for i := 0; i < bloomFilterThreads; i++ { 230 go session.Multiplex(bloomRetrievalBatch, bloomRetrievalWait, b.eth.bloomRequests) 231 } 232 } 233 234 func (b *EthAPIBackend) AddLocalCheckpoint(num uint64, hash common.Hash) error { 235 return b.eth.blockchain.AddCheckpoint( 236 core.Checkpoint{ 237 Number: num, 238 Hash: hash, 239 }, 240 []core.CheckpointSignature{}, 241 true, 242 ) 243 } 244 245 func (b *EthAPIBackend) AddDynamicCheckpoint( 246 since uint64, 247 num uint64, 248 hash common.Hash, 249 sigs []core.CheckpointSignature, 250 ) error { 251 return b.eth.blockchain.AddCheckpoint( 252 core.Checkpoint{ 253 Since: since, 254 Number: num, 255 Hash: hash, 256 }, 257 sigs, 258 false, 259 ) 260 } 261 262 func (b *EthAPIBackend) ListCheckpoints() []core.CheckpointInfo { 263 return b.eth.blockchain.ListCheckpoints() 264 } 265 266 func (b *EthAPIBackend) CheckpointSignatures(cp core.Checkpoint) []core.CheckpointSignature { 267 return b.eth.blockchain.CheckpointSignatures(cp) 268 } 269 270 func (b *EthAPIBackend) IsPublicService() bool { 271 return b.eth.config.PublicService 272 } 273 274 const ( 275 syncedHeadToleranceDuration = time.Second * 60 276 syncedHeadChanSize = 16 277 ) 278 279 func (b *EthAPIBackend) OnSyncedHeadUpdates(cb func()) { 280 if !b.IsPublicService() { 281 return 282 } 283 284 go func() { 285 chainHeadCh := make(chan core.ChainHeadEvent, syncedHeadChanSize) 286 headSub := b.SubscribeChainHeadEvent(chainHeadCh) 287 defer headSub.Unsubscribe() 288 289 for { 290 select { 291 case ev := <-chainHeadCh: 292 blockTime := time.Unix(int64(ev.Block.Time()), 0) 293 timeNow := time.Now().UTC() 294 295 if blockTime.After(timeNow.Add(-syncedHeadToleranceDuration)) { 296 log.Debug("Firing OnSyncedHead") 297 go cb() 298 } 299 300 break 301 302 // Shutdown 303 case <-headSub.Err(): 304 return 305 } 306 } 307 }() 308 }