main.go 2.86 KB
Newer Older
Felix Lange's avatar
Felix Lange committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
// Copyright (c) 2013-2014, Jeffrey Wilcke. All rights reserved.
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
// General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this library; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
// MA 02110-1301  USA

18 19 20
package main

import (
obscuren's avatar
obscuren committed
21
	"fmt"
obscuren's avatar
obscuren committed
22 23
	"os"
	"runtime"
obscuren's avatar
obscuren committed
24
	"time"
obscuren's avatar
obscuren committed
25

26
	"github.com/ethereum/go-ethereum"
obscuren's avatar
obscuren committed
27
	"github.com/ethereum/go-ethereum/cmd/utils"
obscuren's avatar
obscuren committed
28
	"github.com/ethereum/go-ethereum/logger"
29
	"gopkg.in/qml.v1"
30 31
)

zelig's avatar
zelig committed
32
const (
33
	ClientIdentifier = "Mist"
obscuren's avatar
obscuren committed
34
	Version          = "0.7.5"
zelig's avatar
zelig committed
35 36
)

37
var ethereum *eth.Ethereum
zelig's avatar
zelig committed
38

39
func run() error {
40 41
	// precedence: code-internal flag default < config file < environment variables < command line
	Init() // parsing command line
zelig's avatar
zelig committed
42

obscuren's avatar
obscuren committed
43
	tstart := time.Now()
obscuren's avatar
obscuren committed
44
	config := utils.InitConfig(VmType, ConfigFile, Datadir, "ETH")
45 46 47

	utils.InitDataDir(Datadir)

48
	stdLog := utils.InitLogging(Datadir, LogFile, LogLevel, DebugFile)
49

50
	db := utils.NewDatabase()
obscuren's avatar
obscuren committed
51 52 53
	err := utils.DBSanityCheck(db)
	if err != nil {
		ErrorWindow(err)
obscuren's avatar
obscuren committed
54

obscuren's avatar
obscuren committed
55 56
		os.Exit(1)
	}
57
	keyManager := utils.NewKeyManager(KeyStore, Datadir, db)
58 59

	// create, import, export keys
60
	utils.KeyTasks(keyManager, KeyRing, GenAddr, SecretFile, ExportDir, NonInteractive)
zelig's avatar
zelig committed
61
	clientIdentity := utils.NewClientIdentity(ClientIdentifier, Version, Identifier)
62
	ethereum = utils.NewEthereum(db, clientIdentity, keyManager, UseUPnP, OutboundPort, MaxPeer)
63

zelig's avatar
zelig committed
64 65 66
	if ShowGenesis {
		utils.ShowGenesis(ethereum)
	}
67 68 69 70 71

	if StartRpc {
		utils.StartRpc(ethereum, RpcPort)
	}

zelig's avatar
zelig committed
72
	gui := NewWindow(ethereum, config, clientIdentity, KeyRing, LogLevel)
73
	gui.stdLog = stdLog
zelig's avatar
zelig committed
74

zelig's avatar
zelig committed
75
	utils.RegisterInterrupt(func(os.Signal) {
zelig's avatar
zelig committed
76 77
		gui.Stop()
	})
obscuren's avatar
obscuren committed
78 79 80 81
	go utils.StartEthereum(ethereum, UseSeed)

	fmt.Println("ETH stack took", time.Since(tstart))

zelig's avatar
zelig committed
82 83
	// gui blocks the main thread
	gui.Start(AssetPath)
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102

	return nil
}

func main() {
	runtime.GOMAXPROCS(runtime.NumCPU())

	// This is a bit of a cheat, but ey!
	os.Setenv("QTWEBKIT_INSPECTOR_SERVER", "127.0.0.1:99999")

	qml.Run(run)

	var interrupted = false
	utils.RegisterInterrupt(func(os.Signal) {
		interrupted = true
	})

	utils.HandleInterrupt()

103 104 105 106
	if StartWebSockets {
		utils.StartWebSockets(ethereum)
	}

zelig's avatar
zelig committed
107 108
	// we need to run the interrupt callbacks in case gui is closed
	// this skips if we got here by actual interrupt stopping the GUI
zelig's avatar
zelig committed
109 110
	if !interrupted {
		utils.RunInterruptCallbacks(os.Interrupt)
zelig's avatar
zelig committed
111 112 113
	}
	// this blocks the thread
	ethereum.WaitForShutdown()
obscuren's avatar
obscuren committed
114
	logger.Flush()
115
}