ethereum.go 2.88 KB
Newer Older
obscuren's avatar
obscuren committed
1 2 3
package main

import (
obscuren's avatar
obscuren committed
4
	"encoding/hex"
obscuren's avatar
obscuren committed
5
	"fmt"
obscuren's avatar
obscuren committed
6 7
	"github.com/ethereum/eth-go"
	"github.com/ethereum/ethchain-go"
obscuren's avatar
obscuren committed
8
	"github.com/ethereum/ethutil-go"
9
	"github.com/obscuren/secp256k1-go"
obscuren's avatar
obscuren committed
10 11 12 13
	"log"
	"os"
	"os/signal"
	"runtime"
obscuren's avatar
obscuren committed
14 15
)

16
const Debug = true
obscuren's avatar
obscuren committed
17

obscuren's avatar
obscuren committed
18 19
// Register interrupt handlers so we can stop the ethereum
func RegisterInterupts(s *eth.Ethereum) {
obscuren's avatar
obscuren committed
20 21 22 23 24 25 26 27 28 29 30
	// Buffered chan of one is enough
	c := make(chan os.Signal, 1)
	// Notify about interrupts for now
	signal.Notify(c, os.Interrupt)
	go func() {
		for sig := range c {
			fmt.Printf("Shutting down (%v) ... \n", sig)

			s.Stop()
		}
	}()
obscuren's avatar
obscuren committed
31 32
}

33 34 35 36 37 38 39
func CreateKeyPair(force bool) {
	data, _ := ethutil.Config.Db.Get([]byte("KeyRing"))
	if len(data) == 0 || force {
		log.Println("Generating new address and keypair")

		pub, prv := secp256k1.GenerateKeyPair()

obscuren's avatar
obscuren committed
40 41
		log.Printf("Your new address is %x\n", pub[12:])

42 43 44 45
		ethutil.Config.Db.Put([]byte("KeyRing"), prv)
	}
}

obscuren's avatar
obscuren committed
46
func main() {
obscuren's avatar
obscuren committed
47 48 49
	runtime.GOMAXPROCS(runtime.NumCPU())
	Init()

50
	ethchain.InitFees()
51 52
	ethutil.ReadConfig()

obscuren's avatar
obscuren committed
53
	// Instantiated a eth stack
obscuren's avatar
obscuren committed
54
	ethereum, err := eth.New(eth.CapDefault, UseUPnP)
55
	if err != nil {
56
		log.Println("eth start err:", err)
57 58 59
		return
	}

60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
	if GenAddr {
		fmt.Println("This action overwrites your old private key. Are you sure? (y/n)")

		var r string
		fmt.Scanln(&r)
		for ; ; fmt.Scanln(&r) {
			if r == "n" || r == "y" {
				break
			} else {
				fmt.Println("Yes or no?", r)
			}
		}

		if r == "y" {
			CreateKeyPair(true)
		}
		os.Exit(0)
	} else {
		CreateKeyPair(false)
	}

obscuren's avatar
obscuren committed
81 82 83 84 85 86 87
	if ShowGenesis {
		fmt.Println(ethereum.BlockManager.BlockChain().Genesis())
		os.Exit(0)
	}

	log.Printf("Starting Ethereum v%s\n", ethutil.Config.Ver)

88 89 90
	// Set the max peers
	ethereum.MaxPeers = MaxPeer

obscuren's avatar
obscuren committed
91
	if StartConsole {
92 93 94
		err := os.Mkdir(ethutil.Config.ExecPath, os.ModePerm)
		// Error is OK if the error is ErrExist
		if err != nil && !os.IsExist(err) {
obscuren's avatar
obscuren committed
95
			log.Panic("Unable to create EXECPATH:", err)
96
		}
obscuren's avatar
obscuren committed
97

obscuren's avatar
obscuren committed
98
		console := NewConsole(ethereum)
99 100
		go console.Start()
	}
obscuren's avatar
obscuren committed
101

obscuren's avatar
obscuren committed
102
	RegisterInterupts(ethereum)
obscuren's avatar
obscuren committed
103

obscuren's avatar
obscuren committed
104 105
	ethereum.Start()

106
	if StartMining {
obscuren's avatar
obscuren committed
107
		log.Printf("Dev Test Mining started...\n")
obscuren's avatar
obscuren committed
108

obscuren's avatar
obscuren committed
109
		// Fake block mining. It broadcasts a new block every 5 seconds
110
		go func() {
obscuren's avatar
obscuren committed
111
			pow := &ethchain.EasyPow{}
obscuren's avatar
obscuren committed
112
			addr, _ := hex.DecodeString("82c3b0b72cf62f1a9ce97c64da8072efa28225d8")
obscuren's avatar
obscuren committed
113

obscuren's avatar
obscuren committed
114
			for {
obscuren's avatar
obscuren committed
115
				txs := ethereum.TxPool.Flush()
obscuren's avatar
obscuren committed
116
				// Create a new block which we're going to mine
obscuren's avatar
obscuren committed
117
				block := ethereum.BlockManager.BlockChain().NewBlock(addr, txs)
obscuren's avatar
obscuren committed
118
				// Apply all transactions to the block
obscuren's avatar
obscuren committed
119
				ethereum.BlockManager.ApplyTransactions(block, block.Transactions())
120 121 122

				ethereum.BlockManager.AccumelateRewards(block, block)

obscuren's avatar
obscuren committed
123
				// Search the nonce
obscuren's avatar
obscuren committed
124
				block.Nonce = pow.Search(block)
125 126 127 128
				err := ethereum.BlockManager.ProcessBlock(block)
				if err != nil {
					log.Println(err)
				} else {
129
					log.Println("\n+++++++ MINED BLK +++++++\n", ethereum.BlockManager.BlockChain().CurrentBlock)
130
				}
131 132
			}
		}()
obscuren's avatar
obscuren committed
133
	}
134 135

	// Wait for shutdown
obscuren's avatar
obscuren committed
136
	ethereum.WaitForShutdown()
obscuren's avatar
obscuren committed
137
}