ethereum.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. package main
  2. import (
  3. "fmt"
  4. "github.com/ethereum/eth-go"
  5. "github.com/ethereum/eth-go/ethchain"
  6. "github.com/ethereum/eth-go/ethutil"
  7. "github.com/ethereum/eth-go/ethwire"
  8. "github.com/ethereum/go-ethereum/utils"
  9. "log"
  10. "os"
  11. "os/signal"
  12. "runtime"
  13. )
  14. const Debug = true
  15. // Register interrupt handlers so we can stop the ethereum
  16. func RegisterInterupts(s *eth.Ethereum) {
  17. // Buffered chan of one is enough
  18. c := make(chan os.Signal, 1)
  19. // Notify about interrupts for now
  20. signal.Notify(c, os.Interrupt)
  21. go func() {
  22. for sig := range c {
  23. fmt.Printf("Shutting down (%v) ... \n", sig)
  24. s.Stop()
  25. }
  26. }()
  27. }
  28. func main() {
  29. Init()
  30. runtime.GOMAXPROCS(runtime.NumCPU())
  31. ethchain.InitFees()
  32. ethutil.ReadConfig(DataDir)
  33. ethutil.Config.Seed = UseSeed
  34. // Instantiated a eth stack
  35. ethereum, err := eth.New(eth.CapDefault, UseUPnP)
  36. if err != nil {
  37. log.Println("eth start err:", err)
  38. return
  39. }
  40. ethereum.Port = OutboundPort
  41. if GenAddr {
  42. fmt.Println("This action overwrites your old private key. Are you sure? (y/n)")
  43. var r string
  44. fmt.Scanln(&r)
  45. for ; ; fmt.Scanln(&r) {
  46. if r == "n" || r == "y" {
  47. break
  48. } else {
  49. fmt.Printf("Yes or no?", r)
  50. }
  51. }
  52. if r == "y" {
  53. utils.CreateKeyPair(true)
  54. }
  55. os.Exit(0)
  56. } else {
  57. if len(ImportKey) > 0 {
  58. fmt.Println("This action overwrites your old private key. Are you sure? (y/n)")
  59. var r string
  60. fmt.Scanln(&r)
  61. for ; ; fmt.Scanln(&r) {
  62. if r == "n" || r == "y" {
  63. break
  64. } else {
  65. fmt.Printf("Yes or no?", r)
  66. }
  67. }
  68. if r == "y" {
  69. utils.ImportPrivateKey(ImportKey)
  70. os.Exit(0)
  71. }
  72. } else {
  73. utils.CreateKeyPair(false)
  74. }
  75. }
  76. if ExportKey {
  77. key := ethutil.Config.Db.GetKeys()[0]
  78. fmt.Printf("%x\n", key.PrivateKey)
  79. os.Exit(0)
  80. }
  81. if ShowGenesis {
  82. fmt.Println(ethereum.BlockChain().Genesis())
  83. os.Exit(0)
  84. }
  85. log.Printf("Starting Ethereum v%s\n", ethutil.Config.Ver)
  86. // Set the max peers
  87. ethereum.MaxPeers = MaxPeer
  88. if StartConsole {
  89. err := os.Mkdir(ethutil.Config.ExecPath, os.ModePerm)
  90. // Error is OK if the error is ErrExist
  91. if err != nil && !os.IsExist(err) {
  92. log.Panic("Unable to create EXECPATH:", err)
  93. }
  94. console := NewConsole(ethereum)
  95. go console.Start()
  96. }
  97. RegisterInterupts(ethereum)
  98. ethereum.Start()
  99. if StartMining {
  100. log.Printf("Miner started\n")
  101. // Fake block mining. It broadcasts a new block every 5 seconds
  102. go func() {
  103. pow := &ethchain.EasyPow{}
  104. data, _ := ethutil.Config.Db.Get([]byte("KeyRing"))
  105. keyRing := ethutil.NewValueFromBytes(data)
  106. addr := keyRing.Get(1).Bytes()
  107. for {
  108. txs := ethereum.TxPool().Flush()
  109. // Create a new block which we're going to mine
  110. block := ethereum.BlockChain().NewBlock(addr, txs)
  111. log.Println("Mining on new block. Includes", len(block.Transactions()), "transactions")
  112. // Apply all transactions to the block
  113. ethereum.StateManager().ApplyTransactions(block, block.Transactions())
  114. ethereum.StateManager().Prepare(block.State(), block.State())
  115. ethereum.StateManager().AccumelateRewards(block)
  116. // Search the nonce
  117. block.Nonce = pow.Search(block)
  118. ethereum.Broadcast(ethwire.MsgBlockTy, []interface{}{block.Value().Val})
  119. ethereum.StateManager().PrepareDefault(block)
  120. err := ethereum.StateManager().ProcessBlock(block)
  121. if err != nil {
  122. log.Println(err)
  123. } else {
  124. log.Println("\n+++++++ MINED BLK +++++++\n", ethereum.BlockChain().CurrentBlock)
  125. log.Printf("🔨 Mined block %x\n", block.Hash())
  126. }
  127. }
  128. }()
  129. }
  130. // Wait for shutdown
  131. ethereum.WaitForShutdown()
  132. }