github.com/aiyaya188/klaytn@v0.0.0-20220629133911-2c66fd5546f4/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 "strings" 26 "sync" 27 "sync/atomic" 28 "time" 29 30 "github.com/aiyaya188/klaytn/accounts" 31 "github.com/aiyaya188/klaytn/common" 32 "github.com/aiyaya188/klaytn/common/hexutil" 33 "github.com/aiyaya188/klaytn/consensus" 34 "github.com/aiyaya188/klaytn/consensus/beacon" 35 "github.com/aiyaya188/klaytn/consensus/clique" 36 "github.com/aiyaya188/klaytn/core" 37 "github.com/aiyaya188/klaytn/core/bloombits" 38 "github.com/aiyaya188/klaytn/core/rawdb" 39 "github.com/aiyaya188/klaytn/core/state/pruner" 40 "github.com/aiyaya188/klaytn/core/types" 41 "github.com/aiyaya188/klaytn/core/vm" 42 "github.com/aiyaya188/klaytn/eth/downloader" 43 "github.com/aiyaya188/klaytn/eth/ethconfig" 44 "github.com/aiyaya188/klaytn/eth/filters" 45 "github.com/aiyaya188/klaytn/eth/gasprice" 46 "github.com/aiyaya188/klaytn/eth/protocols/eth" 47 "github.com/aiyaya188/klaytn/eth/protocols/snap" 48 "github.com/aiyaya188/klaytn/ethdb" 49 "github.com/aiyaya188/klaytn/event" 50 "github.com/aiyaya188/klaytn/internal/ethapi" 51 "github.com/aiyaya188/klaytn/internal/shutdowncheck" 52 "github.com/aiyaya188/klaytn/log" 53 "github.com/aiyaya188/klaytn/miner" 54 "github.com/aiyaya188/klaytn/node" 55 "github.com/aiyaya188/klaytn/p2p" 56 "github.com/aiyaya188/klaytn/p2p/dnsdisc" 57 "github.com/aiyaya188/klaytn/p2p/enode" 58 "github.com/aiyaya188/klaytn/params" 59 "github.com/aiyaya188/klaytn/rlp" 60 "github.com/aiyaya188/klaytn/rpc" 61 ) 62 63 // Config contains the configuration options of the ETH protocol. 64 // Deprecated: use ethconfig.Config instead. 65 type Config = ethconfig.Config 66 67 // Ethereum implements the Ethereum full node service. 68 type Ethereum struct { 69 config *ethconfig.Config 70 71 // Handlers 72 txPool *core.TxPool 73 blockchain *core.BlockChain 74 handler *handler 75 ethDialCandidates enode.Iterator 76 snapDialCandidates enode.Iterator 77 merger *consensus.Merger 78 79 // DB interfaces 80 chainDb ethdb.Database // Block chain database 81 82 eventMux *event.TypeMux 83 engine consensus.Engine 84 accountManager *accounts.Manager 85 86 bloomRequests chan chan *bloombits.Retrieval // Channel receiving bloom data retrieval requests 87 bloomIndexer *core.ChainIndexer // Bloom indexer operating during block imports 88 closeBloomHandler chan struct{} 89 90 APIBackend *EthAPIBackend 91 92 miner *miner.Miner 93 gasPrice *big.Int 94 etherbase common.Address 95 96 networkID uint64 97 netRPCService *ethapi.NetAPI 98 99 p2pServer *p2p.Server 100 101 lock sync.RWMutex // Protects the variadic fields (e.g. gas price and etherbase) 102 103 shutdownTracker *shutdowncheck.ShutdownTracker // Tracks if and when the node has shutdown ungracefully 104 } 105 106 // New creates a new Ethereum object (including the 107 // initialisation of the common Ethereum object) 108 func New(stack *node.Node, config *ethconfig.Config) (*Ethereum, error) { 109 // Ensure configuration values are compatible and sane 110 if config.SyncMode == downloader.LightSync { 111 return nil, errors.New("can't run eth.Ethereum in light sync mode, use les.LightEthereum") 112 } 113 if !config.SyncMode.IsValid() { 114 return nil, fmt.Errorf("invalid sync mode %d", config.SyncMode) 115 } 116 if config.Miner.GasPrice == nil || config.Miner.GasPrice.Cmp(common.Big0) <= 0 { 117 log.Warn("Sanitizing invalid miner gas price", "provided", config.Miner.GasPrice, "updated", ethconfig.Defaults.Miner.GasPrice) 118 config.Miner.GasPrice = new(big.Int).Set(ethconfig.Defaults.Miner.GasPrice) 119 } 120 if config.NoPruning && config.TrieDirtyCache > 0 { 121 if config.SnapshotCache > 0 { 122 config.TrieCleanCache += config.TrieDirtyCache * 3 / 5 123 config.SnapshotCache += config.TrieDirtyCache * 2 / 5 124 } else { 125 config.TrieCleanCache += config.TrieDirtyCache 126 } 127 config.TrieDirtyCache = 0 128 } 129 log.Info("Allocated trie memory caches", "clean", common.StorageSize(config.TrieCleanCache)*1024*1024, "dirty", common.StorageSize(config.TrieDirtyCache)*1024*1024) 130 131 // Transfer mining-related config to the ethash config. 132 ethashConfig := config.Ethash 133 ethashConfig.NotifyFull = config.Miner.NotifyFull 134 135 // Assemble the Ethereum object 136 chainDb, err := stack.OpenDatabaseWithFreezer("chaindata", config.DatabaseCache, config.DatabaseHandles, config.DatabaseFreezer, "eth/db/chaindata/", false) 137 if err != nil { 138 return nil, err 139 } 140 chainConfig, genesisHash, genesisErr := core.SetupGenesisBlockWithOverride(chainDb, config.Genesis, config.OverrideGrayGlacier, config.OverrideTerminalTotalDifficulty) 141 if _, ok := genesisErr.(*params.ConfigCompatError); genesisErr != nil && !ok { 142 return nil, genesisErr 143 } 144 log.Info("") 145 log.Info(strings.Repeat("-", 153)) 146 for _, line := range strings.Split(chainConfig.String(), "\n") { 147 log.Info(line) 148 } 149 log.Info(strings.Repeat("-", 153)) 150 log.Info("") 151 152 if err := pruner.RecoverPruning(stack.ResolvePath(""), chainDb, stack.ResolvePath(config.TrieCleanCacheJournal)); err != nil { 153 log.Error("Failed to recover state", "error", err) 154 } 155 merger := consensus.NewMerger(chainDb) 156 eth := &Ethereum{ 157 config: config, 158 merger: merger, 159 chainDb: chainDb, 160 eventMux: stack.EventMux(), 161 accountManager: stack.AccountManager(), 162 engine: ethconfig.CreateConsensusEngine(stack, chainConfig, ðashConfig, config.Miner.Notify, config.Miner.Noverify, chainDb), 163 closeBloomHandler: make(chan struct{}), 164 networkID: config.NetworkId, 165 gasPrice: config.Miner.GasPrice, 166 etherbase: config.Miner.Etherbase, 167 bloomRequests: make(chan chan *bloombits.Retrieval), 168 bloomIndexer: core.NewBloomIndexer(chainDb, params.BloomBitsBlocks, params.BloomConfirms), 169 p2pServer: stack.Server(), 170 shutdownTracker: shutdowncheck.NewShutdownTracker(chainDb), 171 } 172 173 bcVersion := rawdb.ReadDatabaseVersion(chainDb) 174 var dbVer = "<nil>" 175 if bcVersion != nil { 176 dbVer = fmt.Sprintf("%d", *bcVersion) 177 } 178 log.Info("Initialising Ethereum protocol", "network", config.NetworkId, "dbversion", dbVer) 179 180 if !config.SkipBcVersionCheck { 181 if bcVersion != nil && *bcVersion > core.BlockChainVersion { 182 return nil, fmt.Errorf("database version is v%d, Geth %s only supports v%d", *bcVersion, params.VersionWithMeta, core.BlockChainVersion) 183 } else if bcVersion == nil || *bcVersion < core.BlockChainVersion { 184 if bcVersion != nil { // only print warning on upgrade, not on init 185 log.Warn("Upgrade blockchain database version", "from", dbVer, "to", core.BlockChainVersion) 186 } 187 rawdb.WriteDatabaseVersion(chainDb, core.BlockChainVersion) 188 } 189 } 190 var ( 191 vmConfig = vm.Config{ 192 EnablePreimageRecording: config.EnablePreimageRecording, 193 } 194 cacheConfig = &core.CacheConfig{ 195 TrieCleanLimit: config.TrieCleanCache, 196 TrieCleanJournal: stack.ResolvePath(config.TrieCleanCacheJournal), 197 TrieCleanRejournal: config.TrieCleanCacheRejournal, 198 TrieCleanNoPrefetch: config.NoPrefetch, 199 TrieDirtyLimit: config.TrieDirtyCache, 200 TrieDirtyDisabled: config.NoPruning, 201 TrieTimeLimit: config.TrieTimeout, 202 SnapshotLimit: config.SnapshotCache, 203 Preimages: config.Preimages, 204 } 205 ) 206 eth.blockchain, err = core.NewBlockChain(chainDb, cacheConfig, chainConfig, eth.engine, vmConfig, eth.shouldPreserve, &config.TxLookupLimit) 207 if err != nil { 208 return nil, err 209 } 210 // Rewind the chain in case of an incompatible config upgrade. 211 if compat, ok := genesisErr.(*params.ConfigCompatError); ok { 212 log.Warn("Rewinding chain to upgrade configuration", "err", compat) 213 eth.blockchain.SetHead(compat.RewindTo) 214 rawdb.WriteChainConfig(chainDb, genesisHash, chainConfig) 215 } 216 eth.bloomIndexer.Start(eth.blockchain) 217 218 if config.TxPool.Journal != "" { 219 config.TxPool.Journal = stack.ResolvePath(config.TxPool.Journal) 220 } 221 eth.txPool = core.NewTxPool(config.TxPool, chainConfig, eth.blockchain) 222 223 // Permit the downloader to use the trie cache allowance during fast sync 224 cacheLimit := cacheConfig.TrieCleanLimit + cacheConfig.TrieDirtyLimit + cacheConfig.SnapshotLimit 225 checkpoint := config.Checkpoint 226 if checkpoint == nil { 227 checkpoint = params.TrustedCheckpoints[genesisHash] 228 } 229 if eth.handler, err = newHandler(&handlerConfig{ 230 Database: chainDb, 231 Chain: eth.blockchain, 232 TxPool: eth.txPool, 233 Merger: merger, 234 Network: config.NetworkId, 235 Sync: config.SyncMode, 236 BloomCache: uint64(cacheLimit), 237 EventMux: eth.eventMux, 238 Checkpoint: checkpoint, 239 RequiredBlocks: config.RequiredBlocks, 240 }); err != nil { 241 return nil, err 242 } 243 244 eth.miner = miner.New(eth, &config.Miner, chainConfig, eth.EventMux(), eth.engine, eth.isLocalBlock) 245 eth.miner.SetExtra(makeExtraData(config.Miner.ExtraData)) 246 247 eth.APIBackend = &EthAPIBackend{stack.Config().ExtRPCEnabled(), stack.Config().AllowUnprotectedTxs, eth, nil} 248 if eth.APIBackend.allowUnprotectedTxs { 249 log.Info("Unprotected transactions allowed") 250 } 251 gpoParams := config.GPO 252 if gpoParams.Default == nil { 253 gpoParams.Default = config.Miner.GasPrice 254 } 255 eth.APIBackend.gpo = gasprice.NewOracle(eth.APIBackend, gpoParams) 256 257 // Setup DNS discovery iterators. 258 dnsclient := dnsdisc.NewClient(dnsdisc.Config{}) 259 eth.ethDialCandidates, err = dnsclient.NewIterator(eth.config.EthDiscoveryURLs...) 260 if err != nil { 261 return nil, err 262 } 263 eth.snapDialCandidates, err = dnsclient.NewIterator(eth.config.SnapDiscoveryURLs...) 264 if err != nil { 265 return nil, err 266 } 267 268 // Start the RPC service 269 eth.netRPCService = ethapi.NewNetAPI(eth.p2pServer, config.NetworkId) 270 271 // Register the backend on the node 272 stack.RegisterAPIs(eth.APIs()) 273 stack.RegisterProtocols(eth.Protocols()) 274 stack.RegisterLifecycle(eth) 275 276 // Successful startup; push a marker and check previous unclean shutdowns. 277 eth.shutdownTracker.MarkStartup() 278 279 return eth, nil 280 } 281 282 func makeExtraData(extra []byte) []byte { 283 if len(extra) == 0 { 284 // create default extradata 285 extra, _ = rlp.EncodeToBytes([]interface{}{ 286 uint(params.VersionMajor<<16 | params.VersionMinor<<8 | params.VersionPatch), 287 "geth", 288 runtime.Version(), 289 runtime.GOOS, 290 }) 291 } 292 if uint64(len(extra)) > params.MaximumExtraDataSize { 293 log.Warn("Miner extra data exceed limit", "extra", hexutil.Bytes(extra), "limit", params.MaximumExtraDataSize) 294 extra = nil 295 } 296 return extra 297 } 298 299 // APIs return the collection of RPC services the ethereum package offers. 300 // NOTE, some of these services probably need to be moved to somewhere else. 301 func (s *Ethereum) APIs() []rpc.API { 302 apis := ethapi.GetAPIs(s.APIBackend) 303 304 // Append any APIs exposed explicitly by the consensus engine 305 apis = append(apis, s.engine.APIs(s.BlockChain())...) 306 307 // Append all the local APIs and return 308 return append(apis, []rpc.API{ 309 { 310 Namespace: "eth", 311 Version: "1.0", 312 Service: NewEthereumAPI(s), 313 }, { 314 Namespace: "miner", 315 Version: "1.0", 316 Service: NewMinerAPI(s), 317 }, { 318 Namespace: "eth", 319 Version: "1.0", 320 Service: downloader.NewDownloaderAPI(s.handler.downloader, s.eventMux), 321 }, { 322 Namespace: "eth", 323 Version: "1.0", 324 Service: filters.NewFilterAPI(s.APIBackend, false, 5*time.Minute), 325 }, { 326 Namespace: "admin", 327 Version: "1.0", 328 Service: NewAdminAPI(s), 329 }, { 330 Namespace: "debug", 331 Version: "1.0", 332 Service: NewDebugAPI(s), 333 }, { 334 Namespace: "net", 335 Version: "1.0", 336 Service: s.netRPCService, 337 }, 338 }...) 339 } 340 341 func (s *Ethereum) ResetWithGenesisBlock(gb *types.Block) { 342 s.blockchain.ResetWithGenesisBlock(gb) 343 } 344 345 func (s *Ethereum) Etherbase() (eb common.Address, err error) { 346 s.lock.RLock() 347 etherbase := s.etherbase 348 s.lock.RUnlock() 349 350 if etherbase != (common.Address{}) { 351 return etherbase, nil 352 } 353 if wallets := s.AccountManager().Wallets(); len(wallets) > 0 { 354 if accounts := wallets[0].Accounts(); len(accounts) > 0 { 355 etherbase := accounts[0].Address 356 357 s.lock.Lock() 358 s.etherbase = etherbase 359 s.lock.Unlock() 360 361 log.Info("Etherbase automatically configured", "address", etherbase) 362 return etherbase, nil 363 } 364 } 365 return common.Address{}, fmt.Errorf("etherbase must be explicitly specified") 366 } 367 368 // isLocalBlock checks whether the specified block is mined 369 // by local miner accounts. 370 // 371 // We regard two types of accounts as local miner account: etherbase 372 // and accounts specified via `txpool.locals` flag. 373 func (s *Ethereum) isLocalBlock(header *types.Header) bool { 374 author, err := s.engine.Author(header) 375 if err != nil { 376 log.Warn("Failed to retrieve block author", "number", header.Number.Uint64(), "hash", header.Hash(), "err", err) 377 return false 378 } 379 // Check whether the given address is etherbase. 380 s.lock.RLock() 381 etherbase := s.etherbase 382 s.lock.RUnlock() 383 if author == etherbase { 384 return true 385 } 386 // Check whether the given address is specified by `txpool.local` 387 // CLI flag. 388 for _, account := range s.config.TxPool.Locals { 389 if account == author { 390 return true 391 } 392 } 393 return false 394 } 395 396 // shouldPreserve checks whether we should preserve the given block 397 // during the chain reorg depending on whether the author of block 398 // is a local account. 399 func (s *Ethereum) shouldPreserve(header *types.Header) bool { 400 // The reason we need to disable the self-reorg preserving for clique 401 // is it can be probable to introduce a deadlock. 402 // 403 // e.g. If there are 7 available signers 404 // 405 // r1 A 406 // r2 B 407 // r3 C 408 // r4 D 409 // r5 A [X] F G 410 // r6 [X] 411 // 412 // In the round5, the inturn signer E is offline, so the worst case 413 // is A, F and G sign the block of round5 and reject the block of opponents 414 // and in the round6, the last available signer B is offline, the whole 415 // network is stuck. 416 if _, ok := s.engine.(*clique.Clique); ok { 417 return false 418 } 419 return s.isLocalBlock(header) 420 } 421 422 // SetEtherbase sets the mining reward address. 423 func (s *Ethereum) SetEtherbase(etherbase common.Address) { 424 s.lock.Lock() 425 s.etherbase = etherbase 426 s.lock.Unlock() 427 428 s.miner.SetEtherbase(etherbase) 429 } 430 431 // StartMining starts the miner with the given number of CPU threads. If mining 432 // is already running, this method adjust the number of threads allowed to use 433 // and updates the minimum price required by the transaction pool. 434 func (s *Ethereum) StartMining(threads int) error { 435 // Update the thread count within the consensus engine 436 type threaded interface { 437 SetThreads(threads int) 438 } 439 if th, ok := s.engine.(threaded); ok { 440 log.Info("Updated mining threads", "threads", threads) 441 if threads == 0 { 442 threads = -1 // Disable the miner from within 443 } 444 th.SetThreads(threads) 445 } 446 // If the miner was not running, initialize it 447 if !s.IsMining() { 448 // Propagate the initial price point to the transaction pool 449 s.lock.RLock() 450 price := s.gasPrice 451 s.lock.RUnlock() 452 s.txPool.SetGasPrice(price) 453 454 // Configure the local mining address 455 eb, err := s.Etherbase() 456 if err != nil { 457 log.Error("Cannot start mining without etherbase", "err", err) 458 return fmt.Errorf("etherbase missing: %v", err) 459 } 460 var cli *clique.Clique 461 if c, ok := s.engine.(*clique.Clique); ok { 462 cli = c 463 } else if cl, ok := s.engine.(*beacon.Beacon); ok { 464 if c, ok := cl.InnerEngine().(*clique.Clique); ok { 465 cli = c 466 } 467 } 468 if cli != nil { 469 wallet, err := s.accountManager.Find(accounts.Account{Address: eb}) 470 if wallet == nil || err != nil { 471 log.Error("Etherbase account unavailable locally", "err", err) 472 return fmt.Errorf("signer missing: %v", err) 473 } 474 cli.Authorize(eb, wallet.SignData) 475 } 476 // If mining is started, we can disable the transaction rejection mechanism 477 // introduced to speed sync times. 478 atomic.StoreUint32(&s.handler.acceptTxs, 1) 479 480 go s.miner.Start(eb) 481 } 482 return nil 483 } 484 485 // StopMining terminates the miner, both at the consensus engine level as well as 486 // at the block creation level. 487 func (s *Ethereum) StopMining() { 488 // Update the thread count within the consensus engine 489 type threaded interface { 490 SetThreads(threads int) 491 } 492 if th, ok := s.engine.(threaded); ok { 493 th.SetThreads(-1) 494 } 495 // Stop the block creating itself 496 s.miner.Stop() 497 } 498 499 func (s *Ethereum) IsMining() bool { return s.miner.Mining() } 500 func (s *Ethereum) Miner() *miner.Miner { return s.miner } 501 502 func (s *Ethereum) AccountManager() *accounts.Manager { return s.accountManager } 503 func (s *Ethereum) BlockChain() *core.BlockChain { return s.blockchain } 504 func (s *Ethereum) TxPool() *core.TxPool { return s.txPool } 505 func (s *Ethereum) EventMux() *event.TypeMux { return s.eventMux } 506 func (s *Ethereum) Engine() consensus.Engine { return s.engine } 507 func (s *Ethereum) ChainDb() ethdb.Database { return s.chainDb } 508 func (s *Ethereum) IsListening() bool { return true } // Always listening 509 func (s *Ethereum) Downloader() *downloader.Downloader { return s.handler.downloader } 510 func (s *Ethereum) Synced() bool { return atomic.LoadUint32(&s.handler.acceptTxs) == 1 } 511 func (s *Ethereum) SetSynced() { atomic.StoreUint32(&s.handler.acceptTxs, 1) } 512 func (s *Ethereum) ArchiveMode() bool { return s.config.NoPruning } 513 func (s *Ethereum) BloomIndexer() *core.ChainIndexer { return s.bloomIndexer } 514 func (s *Ethereum) Merger() *consensus.Merger { return s.merger } 515 func (s *Ethereum) SyncMode() downloader.SyncMode { 516 mode, _ := s.handler.chainSync.modeAndLocalHead() 517 return mode 518 } 519 520 // Protocols returns all the currently configured 521 // network protocols to start. 522 func (s *Ethereum) Protocols() []p2p.Protocol { 523 protos := eth.MakeProtocols((*ethHandler)(s.handler), s.networkID, s.ethDialCandidates) 524 if s.config.SnapshotCache > 0 { 525 protos = append(protos, snap.MakeProtocols((*snapHandler)(s.handler), s.snapDialCandidates)...) 526 } 527 return protos 528 } 529 530 // Start implements node.Lifecycle, starting all internal goroutines needed by the 531 // Ethereum protocol implementation. 532 func (s *Ethereum) Start() error { 533 eth.StartENRUpdater(s.blockchain, s.p2pServer.LocalNode()) 534 535 // Start the bloom bits servicing goroutines 536 s.startBloomHandlers(params.BloomBitsBlocks) 537 538 // Regularly update shutdown marker 539 s.shutdownTracker.Start() 540 541 // Figure out a max peers count based on the server limits 542 maxPeers := s.p2pServer.MaxPeers 543 if s.config.LightServ > 0 { 544 if s.config.LightPeers >= s.p2pServer.MaxPeers { 545 return fmt.Errorf("invalid peer config: light peer count (%d) >= total peer count (%d)", s.config.LightPeers, s.p2pServer.MaxPeers) 546 } 547 maxPeers -= s.config.LightPeers 548 } 549 // Start the networking layer and the light server if requested 550 s.handler.Start(maxPeers) 551 return nil 552 } 553 554 // Stop implements node.Lifecycle, terminating all internal goroutines used by the 555 // Ethereum protocol. 556 func (s *Ethereum) Stop() error { 557 // Stop all the peer-related stuff first. 558 s.ethDialCandidates.Close() 559 s.snapDialCandidates.Close() 560 s.handler.Stop() 561 562 // Then stop everything else. 563 s.bloomIndexer.Close() 564 close(s.closeBloomHandler) 565 s.txPool.Stop() 566 s.miner.Close() 567 s.blockchain.Stop() 568 s.engine.Close() 569 570 // Clean shutdown marker as the last thing before closing db 571 s.shutdownTracker.Stop() 572 573 s.chainDb.Close() 574 s.eventMux.Stop() 575 576 return nil 577 }