github.com/dotlike13/wemix30_go@v1.8.23/eth/backend.go (about) 1 // Copyright 2014 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 implements the Ethereum protocol. 18 package eth 19 20 import ( 21 "errors" 22 "fmt" 23 "math/big" 24 "runtime" 25 "sync" 26 "sync/atomic" 27 28 "github.com/ethereum/go-ethereum/accounts" 29 "github.com/ethereum/go-ethereum/common" 30 "github.com/ethereum/go-ethereum/common/hexutil" 31 "github.com/ethereum/go-ethereum/consensus" 32 "github.com/ethereum/go-ethereum/consensus/clique" 33 "github.com/ethereum/go-ethereum/consensus/ethash" 34 "github.com/ethereum/go-ethereum/core" 35 "github.com/ethereum/go-ethereum/core/bloombits" 36 "github.com/ethereum/go-ethereum/core/rawdb" 37 "github.com/ethereum/go-ethereum/core/types" 38 "github.com/ethereum/go-ethereum/core/vm" 39 "github.com/ethereum/go-ethereum/eth/downloader" 40 "github.com/ethereum/go-ethereum/eth/filters" 41 "github.com/ethereum/go-ethereum/eth/gasprice" 42 "github.com/ethereum/go-ethereum/ethdb" 43 "github.com/ethereum/go-ethereum/event" 44 "github.com/ethereum/go-ethereum/internal/ethapi" 45 "github.com/ethereum/go-ethereum/log" 46 "github.com/ethereum/go-ethereum/miner" 47 "github.com/ethereum/go-ethereum/node" 48 "github.com/ethereum/go-ethereum/p2p" 49 "github.com/ethereum/go-ethereum/params" 50 "github.com/ethereum/go-ethereum/rlp" 51 "github.com/ethereum/go-ethereum/rpc" 52 ) 53 54 type LesServer interface { 55 Start(srvr *p2p.Server) 56 Stop() 57 Protocols() []p2p.Protocol 58 SetBloomBitsIndexer(bbIndexer *core.ChainIndexer) 59 } 60 61 // Ethereum implements the Ethereum full node service. 62 type Ethereum struct { 63 config *Config 64 chainConfig *params.ChainConfig 65 66 // Channel for shutting down the service 67 shutdownChan chan bool // Channel for shutting down the Ethereum 68 69 // Handlers 70 txPool *core.TxPool 71 blockchain *core.BlockChain 72 protocolManager *ProtocolManager 73 lesServer LesServer 74 75 // DB interfaces 76 chainDb ethdb.Database // Block chain database 77 78 eventMux *event.TypeMux 79 engine consensus.Engine 80 accountManager *accounts.Manager 81 82 bloomRequests chan chan *bloombits.Retrieval // Channel receiving bloom data retrieval requests 83 bloomIndexer *core.ChainIndexer // Bloom indexer operating during block imports 84 85 APIBackend *EthAPIBackend 86 87 miner *miner.Miner 88 gasPrice *big.Int 89 etherbase common.Address 90 91 networkID uint64 92 netRPCService *ethapi.PublicNetAPI 93 94 lock sync.RWMutex // Protects the variadic fields (e.g. gas price and etherbase) 95 } 96 97 func (s *Ethereum) AddLesServer(ls LesServer) { 98 s.lesServer = ls 99 ls.SetBloomBitsIndexer(s.bloomIndexer) 100 } 101 102 // New creates a new Ethereum object (including the 103 // initialisation of the common Ethereum object) 104 func New(ctx *node.ServiceContext, config *Config) (*Ethereum, error) { 105 // Ensure configuration values are compatible and sane 106 if config.SyncMode == downloader.LightSync { 107 return nil, errors.New("can't run eth.Ethereum in light sync mode, use les.LightEthereum") 108 } 109 if !config.SyncMode.IsValid() { 110 return nil, fmt.Errorf("invalid sync mode %d", config.SyncMode) 111 } 112 if config.MinerGasPrice == nil || config.MinerGasPrice.Cmp(common.Big0) <= 0 { 113 log.Warn("Sanitizing invalid miner gas price", "provided", config.MinerGasPrice, "updated", DefaultConfig.MinerGasPrice) 114 config.MinerGasPrice = new(big.Int).Set(DefaultConfig.MinerGasPrice) 115 } 116 // Assemble the Ethereum object 117 chainDb, err := CreateDB(ctx, config, "chaindata") 118 if err != nil { 119 return nil, err 120 } 121 chainConfig, genesisHash, genesisErr := core.SetupGenesisBlockWithOverride(chainDb, config.Genesis, config.ConstantinopleOverride) 122 if _, ok := genesisErr.(*params.ConfigCompatError); genesisErr != nil && !ok { 123 return nil, genesisErr 124 } 125 log.Info("Initialised chain configuration", "config", chainConfig) 126 127 eth := &Ethereum{ 128 config: config, 129 chainDb: chainDb, 130 chainConfig: chainConfig, 131 eventMux: ctx.EventMux, 132 accountManager: ctx.AccountManager, 133 engine: CreateConsensusEngine(ctx, chainConfig, &config.Ethash, config.MinerNotify, config.MinerNoverify, chainDb), 134 shutdownChan: make(chan bool), 135 networkID: config.NetworkId, 136 gasPrice: config.MinerGasPrice, 137 etherbase: config.Etherbase, 138 bloomRequests: make(chan chan *bloombits.Retrieval), 139 bloomIndexer: NewBloomIndexer(chainDb, params.BloomBitsBlocks, params.BloomConfirms), 140 } 141 142 log.Info("Initialising Ethereum protocol", "versions", ProtocolVersions, "network", config.NetworkId) 143 144 if !config.SkipBcVersionCheck { 145 bcVersion := rawdb.ReadDatabaseVersion(chainDb) 146 if bcVersion != nil && *bcVersion > core.BlockChainVersion { 147 return nil, fmt.Errorf("database version is v%d, Geth %s only supports v%d", *bcVersion, params.VersionWithMeta, core.BlockChainVersion) 148 } else if bcVersion != nil && *bcVersion < core.BlockChainVersion { 149 log.Warn("Upgrade blockchain database version", "from", *bcVersion, "to", core.BlockChainVersion) 150 } 151 rawdb.WriteDatabaseVersion(chainDb, core.BlockChainVersion) 152 } 153 var ( 154 vmConfig = vm.Config{ 155 EnablePreimageRecording: config.EnablePreimageRecording, 156 EWASMInterpreter: config.EWASMInterpreter, 157 EVMInterpreter: config.EVMInterpreter, 158 } 159 cacheConfig = &core.CacheConfig{Disabled: config.NoPruning, TrieCleanLimit: config.TrieCleanCache, TrieDirtyLimit: config.TrieDirtyCache, TrieTimeLimit: config.TrieTimeout} 160 ) 161 eth.blockchain, err = core.NewBlockChain(chainDb, cacheConfig, eth.chainConfig, eth.engine, vmConfig, eth.shouldPreserve) 162 if err != nil { 163 return nil, err 164 } 165 // Rewind the chain in case of an incompatible config upgrade. 166 if compat, ok := genesisErr.(*params.ConfigCompatError); ok { 167 log.Warn("Rewinding chain to upgrade configuration", "err", compat) 168 eth.blockchain.SetHead(compat.RewindTo) 169 rawdb.WriteChainConfig(chainDb, genesisHash, chainConfig) 170 } 171 eth.bloomIndexer.Start(eth.blockchain) 172 173 if config.TxPool.Journal != "" { 174 config.TxPool.Journal = ctx.ResolvePath(config.TxPool.Journal) 175 } 176 eth.txPool = core.NewTxPool(config.TxPool, eth.chainConfig, eth.blockchain) 177 178 if eth.protocolManager, err = NewProtocolManager(eth.chainConfig, config.SyncMode, config.NetworkId, eth.eventMux, eth.txPool, eth.engine, eth.blockchain, chainDb, config.Whitelist); err != nil { 179 return nil, err 180 } 181 182 eth.miner = miner.New(eth, eth.chainConfig, eth.EventMux(), eth.engine, config.MinerRecommit, config.MinerGasFloor, config.MinerGasCeil, eth.isLocalBlock) 183 eth.miner.SetExtra(makeExtraData(config.MinerExtraData)) 184 185 eth.APIBackend = &EthAPIBackend{eth, nil} 186 gpoParams := config.GPO 187 if gpoParams.Default == nil { 188 gpoParams.Default = config.MinerGasPrice 189 } 190 eth.APIBackend.gpo = gasprice.NewOracle(eth.APIBackend, gpoParams) 191 192 return eth, nil 193 } 194 195 func makeExtraData(extra []byte) []byte { 196 if len(extra) == 0 { 197 // create default extradata 198 extra, _ = rlp.EncodeToBytes([]interface{}{ 199 uint(params.VersionMajor<<16 | params.VersionMinor<<8 | params.VersionPatch), 200 "geth", 201 runtime.Version(), 202 runtime.GOOS, 203 }) 204 } 205 if uint64(len(extra)) > params.MaximumExtraDataSize { 206 log.Warn("Miner extra data exceed limit", "extra", hexutil.Bytes(extra), "limit", params.MaximumExtraDataSize) 207 extra = nil 208 } 209 return extra 210 } 211 212 // CreateDB creates the chain database. 213 func CreateDB(ctx *node.ServiceContext, config *Config, name string) (ethdb.Database, error) { 214 db, err := ctx.OpenDatabase(name, config.DatabaseCache, config.DatabaseHandles) 215 if err != nil { 216 return nil, err 217 } 218 if db, ok := db.(*ethdb.LDBDatabase); ok { 219 db.Meter("eth/db/chaindata/") 220 } 221 return db, nil 222 } 223 224 // CreateConsensusEngine creates the required type of consensus engine instance for an Ethereum service 225 func CreateConsensusEngine(ctx *node.ServiceContext, chainConfig *params.ChainConfig, config *ethash.Config, notify []string, noverify bool, db ethdb.Database) consensus.Engine { 226 // If proof-of-authority is requested, set it up 227 if chainConfig.Clique != nil { 228 return clique.New(chainConfig.Clique, db) 229 } 230 // Otherwise assume proof-of-work 231 switch config.PowMode { 232 case ethash.ModeFake: 233 log.Warn("Ethash used in fake mode") 234 return ethash.NewFaker() 235 case ethash.ModeTest: 236 log.Warn("Ethash used in test mode") 237 return ethash.NewTester(nil, noverify) 238 case ethash.ModeShared: 239 log.Warn("Ethash used in shared mode") 240 return ethash.NewShared() 241 default: 242 engine := ethash.New(ethash.Config{ 243 CacheDir: ctx.ResolvePath(config.CacheDir), 244 CachesInMem: config.CachesInMem, 245 CachesOnDisk: config.CachesOnDisk, 246 DatasetDir: config.DatasetDir, 247 DatasetsInMem: config.DatasetsInMem, 248 DatasetsOnDisk: config.DatasetsOnDisk, 249 }, notify, noverify) 250 engine.SetThreads(-1) // Disable CPU mining 251 return engine 252 } 253 } 254 255 // APIs return the collection of RPC services the ethereum package offers. 256 // NOTE, some of these services probably need to be moved to somewhere else. 257 func (s *Ethereum) APIs() []rpc.API { 258 apis := ethapi.GetAPIs(s.APIBackend) 259 260 // Append any APIs exposed explicitly by the consensus engine 261 apis = append(apis, s.engine.APIs(s.BlockChain())...) 262 263 // Append all the local APIs and return 264 return append(apis, []rpc.API{ 265 { 266 Namespace: "eth", 267 Version: "1.0", 268 Service: NewPublicEthereumAPI(s), 269 Public: true, 270 }, { 271 Namespace: "eth", 272 Version: "1.0", 273 Service: NewPublicMinerAPI(s), 274 Public: true, 275 }, { 276 Namespace: "eth", 277 Version: "1.0", 278 Service: downloader.NewPublicDownloaderAPI(s.protocolManager.downloader, s.eventMux), 279 Public: true, 280 }, { 281 Namespace: "miner", 282 Version: "1.0", 283 Service: NewPrivateMinerAPI(s), 284 Public: false, 285 }, { 286 Namespace: "eth", 287 Version: "1.0", 288 Service: filters.NewPublicFilterAPI(s.APIBackend, false), 289 Public: true, 290 }, { 291 Namespace: "admin", 292 Version: "1.0", 293 Service: NewPrivateAdminAPI(s), 294 }, { 295 Namespace: "debug", 296 Version: "1.0", 297 Service: NewPublicDebugAPI(s), 298 Public: true, 299 }, { 300 Namespace: "debug", 301 Version: "1.0", 302 Service: NewPrivateDebugAPI(s.chainConfig, s), 303 }, { 304 Namespace: "net", 305 Version: "1.0", 306 Service: s.netRPCService, 307 Public: true, 308 }, 309 }...) 310 } 311 312 func (s *Ethereum) ResetWithGenesisBlock(gb *types.Block) { 313 s.blockchain.ResetWithGenesisBlock(gb) 314 } 315 316 func (s *Ethereum) Etherbase() (eb common.Address, err error) { 317 s.lock.RLock() 318 etherbase := s.etherbase 319 s.lock.RUnlock() 320 321 if etherbase != (common.Address{}) { 322 return etherbase, nil 323 } 324 if wallets := s.AccountManager().Wallets(); len(wallets) > 0 { 325 if accounts := wallets[0].Accounts(); len(accounts) > 0 { 326 etherbase := accounts[0].Address 327 328 s.lock.Lock() 329 s.etherbase = etherbase 330 s.lock.Unlock() 331 332 log.Info("Etherbase automatically configured", "address", etherbase) 333 return etherbase, nil 334 } 335 } 336 return common.Address{}, fmt.Errorf("etherbase must be explicitly specified") 337 } 338 339 // isLocalBlock checks whether the specified block is mined 340 // by local miner accounts. 341 // 342 // We regard two types of accounts as local miner account: etherbase 343 // and accounts specified via `txpool.locals` flag. 344 func (s *Ethereum) isLocalBlock(block *types.Block) bool { 345 author, err := s.engine.Author(block.Header()) 346 if err != nil { 347 log.Warn("Failed to retrieve block author", "number", block.NumberU64(), "hash", block.Hash(), "err", err) 348 return false 349 } 350 // Check whether the given address is etherbase. 351 s.lock.RLock() 352 etherbase := s.etherbase 353 s.lock.RUnlock() 354 if author == etherbase { 355 return true 356 } 357 // Check whether the given address is specified by `txpool.local` 358 // CLI flag. 359 for _, account := range s.config.TxPool.Locals { 360 if account == author { 361 return true 362 } 363 } 364 return false 365 } 366 367 // shouldPreserve checks whether we should preserve the given block 368 // during the chain reorg depending on whether the author of block 369 // is a local account. 370 func (s *Ethereum) shouldPreserve(block *types.Block) bool { 371 // The reason we need to disable the self-reorg preserving for clique 372 // is it can be probable to introduce a deadlock. 373 // 374 // e.g. If there are 7 available signers 375 // 376 // r1 A 377 // r2 B 378 // r3 C 379 // r4 D 380 // r5 A [X] F G 381 // r6 [X] 382 // 383 // In the round5, the inturn signer E is offline, so the worst case 384 // is A, F and G sign the block of round5 and reject the block of opponents 385 // and in the round6, the last available signer B is offline, the whole 386 // network is stuck. 387 if _, ok := s.engine.(*clique.Clique); ok { 388 return false 389 } 390 return s.isLocalBlock(block) 391 } 392 393 // SetEtherbase sets the mining reward address. 394 func (s *Ethereum) SetEtherbase(etherbase common.Address) { 395 s.lock.Lock() 396 s.etherbase = etherbase 397 s.lock.Unlock() 398 399 s.miner.SetEtherbase(etherbase) 400 } 401 402 // StartMining starts the miner with the given number of CPU threads. If mining 403 // is already running, this method adjust the number of threads allowed to use 404 // and updates the minimum price required by the transaction pool. 405 func (s *Ethereum) StartMining(threads int) error { 406 // Update the thread count within the consensus engine 407 type threaded interface { 408 SetThreads(threads int) 409 } 410 if th, ok := s.engine.(threaded); ok { 411 log.Info("Updated mining threads", "threads", threads) 412 if threads == 0 { 413 threads = -1 // Disable the miner from within 414 } 415 th.SetThreads(threads) 416 } 417 // If the miner was not running, initialize it 418 if !s.IsMining() { 419 // Propagate the initial price point to the transaction pool 420 s.lock.RLock() 421 price := s.gasPrice 422 s.lock.RUnlock() 423 s.txPool.SetGasPrice(price) 424 425 // Configure the local mining address 426 eb, err := s.Etherbase() 427 if err != nil { 428 log.Error("Cannot start mining without etherbase", "err", err) 429 return fmt.Errorf("etherbase missing: %v", err) 430 } 431 if clique, ok := s.engine.(*clique.Clique); ok { 432 wallet, err := s.accountManager.Find(accounts.Account{Address: eb}) 433 if wallet == nil || err != nil { 434 log.Error("Etherbase account unavailable locally", "err", err) 435 return fmt.Errorf("signer missing: %v", err) 436 } 437 clique.Authorize(eb, wallet.SignHash) 438 } 439 // If mining is started, we can disable the transaction rejection mechanism 440 // introduced to speed sync times. 441 atomic.StoreUint32(&s.protocolManager.acceptTxs, 1) 442 443 go s.miner.Start(eb) 444 } 445 return nil 446 } 447 448 // StopMining terminates the miner, both at the consensus engine level as well as 449 // at the block creation level. 450 func (s *Ethereum) StopMining() { 451 // Update the thread count within the consensus engine 452 type threaded interface { 453 SetThreads(threads int) 454 } 455 if th, ok := s.engine.(threaded); ok { 456 th.SetThreads(-1) 457 } 458 // Stop the block creating itself 459 s.miner.Stop() 460 } 461 462 func (s *Ethereum) IsMining() bool { return s.miner.Mining() } 463 func (s *Ethereum) Miner() *miner.Miner { return s.miner } 464 465 func (s *Ethereum) AccountManager() *accounts.Manager { return s.accountManager } 466 func (s *Ethereum) BlockChain() *core.BlockChain { return s.blockchain } 467 func (s *Ethereum) TxPool() *core.TxPool { return s.txPool } 468 func (s *Ethereum) EventMux() *event.TypeMux { return s.eventMux } 469 func (s *Ethereum) Engine() consensus.Engine { return s.engine } 470 func (s *Ethereum) ChainDb() ethdb.Database { return s.chainDb } 471 func (s *Ethereum) IsListening() bool { return true } // Always listening 472 func (s *Ethereum) EthVersion() int { return int(s.protocolManager.SubProtocols[0].Version) } 473 func (s *Ethereum) NetVersion() uint64 { return s.networkID } 474 func (s *Ethereum) Downloader() *downloader.Downloader { return s.protocolManager.downloader } 475 476 // Protocols implements node.Service, returning all the currently configured 477 // network protocols to start. 478 func (s *Ethereum) Protocols() []p2p.Protocol { 479 if s.lesServer == nil { 480 return s.protocolManager.SubProtocols 481 } 482 return append(s.protocolManager.SubProtocols, s.lesServer.Protocols()...) 483 } 484 485 // Start implements node.Service, starting all internal goroutines needed by the 486 // Ethereum protocol implementation. 487 func (s *Ethereum) Start(srvr *p2p.Server) error { 488 // Start the bloom bits servicing goroutines 489 s.startBloomHandlers(params.BloomBitsBlocks) 490 491 // Start the RPC service 492 s.netRPCService = ethapi.NewPublicNetAPI(srvr, s.NetVersion()) 493 494 // Figure out a max peers count based on the server limits 495 maxPeers := srvr.MaxPeers 496 if s.config.LightServ > 0 { 497 if s.config.LightPeers >= srvr.MaxPeers { 498 return fmt.Errorf("invalid peer config: light peer count (%d) >= total peer count (%d)", s.config.LightPeers, srvr.MaxPeers) 499 } 500 maxPeers -= s.config.LightPeers 501 } 502 // Start the networking layer and the light server if requested 503 s.protocolManager.Start(maxPeers) 504 if s.lesServer != nil { 505 s.lesServer.Start(srvr) 506 } 507 return nil 508 } 509 510 // Stop implements node.Service, terminating all internal goroutines used by the 511 // Ethereum protocol. 512 func (s *Ethereum) Stop() error { 513 s.bloomIndexer.Close() 514 s.blockchain.Stop() 515 s.engine.Close() 516 s.protocolManager.Stop() 517 if s.lesServer != nil { 518 s.lesServer.Stop() 519 } 520 s.txPool.Stop() 521 s.miner.Stop() 522 s.eventMux.Stop() 523 524 s.chainDb.Close() 525 close(s.shutdownChan) 526 return nil 527 }