main.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. // Copyright (c) 2013-2014, Jeffrey Wilcke. All rights reserved.
  2. //
  3. // This library is free software; you can redistribute it and/or
  4. // modify it under the terms of the GNU General Public
  5. // License as published by the Free Software Foundation; either
  6. // version 2.1 of the License, or (at your option) any later version.
  7. //
  8. // This library is distributed in the hope that it will be useful,
  9. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. // General Public License for more details.
  12. //
  13. // You should have received a copy of the GNU General Public License
  14. // along with this library; if not, write to the Free Software
  15. // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
  16. // MA 02110-1301 USA
  17. package main
  18. import (
  19. "fmt"
  20. "os"
  21. "runtime"
  22. "github.com/ethereum/go-ethereum/cmd/utils"
  23. "github.com/ethereum/go-ethereum/core/types"
  24. "github.com/ethereum/go-ethereum/ethutil"
  25. "github.com/ethereum/go-ethereum/logger"
  26. "github.com/ethereum/go-ethereum/rlp"
  27. )
  28. const (
  29. ClientIdentifier = "Ethereum(G)"
  30. Version = "0.7.10"
  31. )
  32. var clilogger = logger.NewLogger("CLI")
  33. func main() {
  34. runtime.GOMAXPROCS(runtime.NumCPU())
  35. defer func() {
  36. logger.Flush()
  37. }()
  38. utils.HandleInterrupt()
  39. // precedence: code-internal flag default < config file < environment variables < command line
  40. Init() // parsing command line
  41. // If the difftool option is selected ignore all other log output
  42. if DiffTool || Dump {
  43. LogLevel = 0
  44. }
  45. utils.InitConfig(VmType, ConfigFile, Datadir, "ETH")
  46. ethutil.Config.Diff = DiffTool
  47. ethutil.Config.DiffType = DiffType
  48. utils.InitDataDir(Datadir)
  49. utils.InitLogging(Datadir, LogFile, LogLevel, DebugFile)
  50. db := utils.NewDatabase()
  51. err := utils.DBSanityCheck(db)
  52. if err != nil {
  53. fmt.Println(err)
  54. os.Exit(1)
  55. }
  56. keyManager := utils.NewKeyManager(KeyStore, Datadir, db)
  57. // create, import, export keys
  58. utils.KeyTasks(keyManager, KeyRing, GenAddr, SecretFile, ExportDir, NonInteractive)
  59. clientIdentity := utils.NewClientIdentity(ClientIdentifier, Version, Identifier, string(keyManager.PublicKey()))
  60. ethereum := utils.NewEthereum(db, clientIdentity, keyManager, utils.NatType(NatType, PMPGateway), OutboundPort, MaxPeer)
  61. if Dump {
  62. var block *types.Block
  63. if len(DumpHash) == 0 && DumpNumber == -1 {
  64. block = ethereum.ChainManager().CurrentBlock()
  65. } else if len(DumpHash) > 0 {
  66. block = ethereum.ChainManager().GetBlock(ethutil.Hex2Bytes(DumpHash))
  67. } else {
  68. block = ethereum.ChainManager().GetBlockByNumber(uint64(DumpNumber))
  69. }
  70. if block == nil {
  71. fmt.Fprintln(os.Stderr, "block not found")
  72. // We want to output valid JSON
  73. fmt.Println("{}")
  74. os.Exit(1)
  75. }
  76. // block.GetRoot() does not exist
  77. //fmt.Printf("RLP: %x\nstate: %x\nhash: %x\n", ethutil.Rlp(block), block.GetRoot(), block.Hash())
  78. // Leave the Println. This needs clean output for piping
  79. fmt.Printf("%s\n", block.State().Dump())
  80. fmt.Println(block)
  81. os.Exit(0)
  82. }
  83. if ShowGenesis {
  84. utils.ShowGenesis(ethereum)
  85. }
  86. if StartMining {
  87. utils.StartMining(ethereum)
  88. }
  89. if len(ImportChain) > 0 {
  90. clilogger.Infof("importing chain '%s'\n", ImportChain)
  91. fh, err := os.OpenFile(ImportChain, os.O_RDONLY, os.ModePerm)
  92. if err != nil {
  93. clilogger.Infoln(err)
  94. return
  95. }
  96. var chain types.Blocks
  97. if err := rlp.Decode(fh, &chain); err != nil {
  98. clilogger.Infoln(err)
  99. return
  100. }
  101. ethereum.ChainManager().Reset()
  102. if err := ethereum.ChainManager().InsertChain(chain); err != nil {
  103. clilogger.Infoln(err)
  104. return
  105. }
  106. clilogger.Infof("imported %d blocks\n", len(chain))
  107. }
  108. // better reworked as cases
  109. if StartJsConsole {
  110. InitJsConsole(ethereum)
  111. } else if len(InputFile) > 0 {
  112. ExecJsFile(ethereum, InputFile)
  113. }
  114. if StartRpc {
  115. utils.StartRpc(ethereum, RpcPort)
  116. }
  117. if StartWebSockets {
  118. utils.StartWebSockets(ethereum)
  119. }
  120. utils.StartEthereum(ethereum, UseSeed)
  121. // this blocks the thread
  122. ethereum.WaitForShutdown()
  123. }