Commit 1f2547b8 authored by obscuren's avatar obscuren

Major re-organisation.

The Ethereum node and Gui are now separated.
parent 22b4e9b6
package main
import (
"flag"
)
var StartConsole bool
var StartMining bool
var UseUPnP bool
var OutboundPort string
var ShowGenesis bool
var AddPeer string
var MaxPeer int
var GenAddr bool
var UseSeed bool
var ImportKey string
var ExportKey bool
var DataDir string
func Init() {
flag.BoolVar(&StartConsole, "c", false, "debug and testing console")
flag.BoolVar(&StartMining, "m", false, "start dagger mining")
flag.BoolVar(&ShowGenesis, "g", false, "prints genesis header and exits")
flag.BoolVar(&UseUPnP, "upnp", false, "enable UPnP support")
flag.BoolVar(&UseSeed, "seed", true, "seed peers")
flag.BoolVar(&GenAddr, "genaddr", false, "create a new priv/pub key")
flag.BoolVar(&ExportKey, "export", false, "export private key")
flag.StringVar(&OutboundPort, "p", "30303", "listening port")
flag.StringVar(&DataDir, "dir", ".ethereal", "ethereum data directory")
flag.StringVar(&ImportKey, "import", "", "imports the given private key (hex)")
flag.IntVar(&MaxPeer, "x", 5, "maximum desired peers")
flag.Parse()
}
package main
import (
"fmt"
"github.com/ethereum/eth-go"
"github.com/ethereum/eth-go/ethchain"
"github.com/ethereum/eth-go/ethutil"
"github.com/ethereum/go-ethereum/ethereal/ui"
"github.com/ethereum/go-ethereum/utils"
"github.com/niemeyer/qml"
"log"
"os"
"os/signal"
"runtime"
)
const Debug = true
// Register interrupt handlers so we can stop the ethereum
func RegisterInterupts(s *eth.Ethereum) {
// 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()
}
}()
}
func main() {
Init()
qml.Init(nil)
runtime.GOMAXPROCS(runtime.NumCPU())
ethchain.InitFees()
ethutil.ReadConfig(DataDir)
ethutil.Config.Seed = UseSeed
// Instantiated a eth stack
ethereum, err := eth.New(eth.CapDefault, UseUPnP)
if err != nil {
log.Println("eth start err:", err)
return
}
ethereum.Port = OutboundPort
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.Printf("Yes or no?", r)
}
}
if r == "y" {
utils.CreateKeyPair(true)
}
os.Exit(0)
} else {
if len(ImportKey) > 0 {
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.Printf("Yes or no?", r)
}
}
if r == "y" {
utils.ImportPrivateKey(ImportKey)
os.Exit(0)
}
} else {
utils.CreateKeyPair(false)
}
}
if ExportKey {
key := ethutil.Config.Db.GetKeys()[0]
fmt.Printf("%x\n", key.PrivateKey)
os.Exit(0)
}
if ShowGenesis {
fmt.Println(ethereum.BlockChain().Genesis())
os.Exit(0)
}
log.Printf("Starting Ethereum v%s\n", ethutil.Config.Ver)
// Set the max peers
ethereum.MaxPeers = MaxPeer
gui := ethui.New(ethereum)
gui.Start()
}
...@@ -55,8 +55,8 @@ func AssetPath(p string) string { ...@@ -55,8 +55,8 @@ func AssetPath(p string) string {
// assume a debug build and use the source directory as // assume a debug build and use the source directory as
// asset directory. // asset directory.
pwd, _ := os.Getwd() pwd, _ := os.Getwd()
if pwd == path.Join(os.Getenv("GOPATH"), "src", "github.com", "ethereum", "go-ethereum") { if pwd == path.Join(os.Getenv("GOPATH"), "src", "github.com", "ethereum", "go-ethereum", "ethereal") {
base = pwd base = path.Join(pwd, "assets")
} else { } else {
switch runtime.GOOS { switch runtime.GOOS {
case "darwin": case "darwin":
......
...@@ -15,14 +15,15 @@ var GenAddr bool ...@@ -15,14 +15,15 @@ var GenAddr bool
var UseSeed bool var UseSeed bool
var ImportKey string var ImportKey string
var ExportKey bool var ExportKey bool
var UseGui bool
//var UseGui bool
var DataDir string var DataDir string
func Init() { func Init() {
flag.BoolVar(&StartConsole, "c", false, "debug and testing console") flag.BoolVar(&StartConsole, "c", false, "debug and testing console")
flag.BoolVar(&StartMining, "m", false, "start dagger mining") flag.BoolVar(&StartMining, "m", false, "start dagger mining")
flag.BoolVar(&ShowGenesis, "g", false, "prints genesis header and exits") flag.BoolVar(&ShowGenesis, "g", false, "prints genesis header and exits")
flag.BoolVar(&UseGui, "gui", true, "use the gui") //flag.BoolVar(&UseGui, "gui", true, "use the gui")
flag.BoolVar(&UseUPnP, "upnp", false, "enable UPnP support") flag.BoolVar(&UseUPnP, "upnp", false, "enable UPnP support")
flag.BoolVar(&UseSeed, "seed", true, "seed peers") flag.BoolVar(&UseSeed, "seed", true, "seed peers")
flag.BoolVar(&GenAddr, "genaddr", false, "create a new priv/pub key") flag.BoolVar(&GenAddr, "genaddr", false, "create a new priv/pub key")
......
...@@ -6,9 +6,7 @@ import ( ...@@ -6,9 +6,7 @@ import (
"github.com/ethereum/eth-go/ethchain" "github.com/ethereum/eth-go/ethchain"
"github.com/ethereum/eth-go/ethutil" "github.com/ethereum/eth-go/ethutil"
"github.com/ethereum/eth-go/ethwire" "github.com/ethereum/eth-go/ethwire"
"github.com/ethereum/go-ethereum/ui" "github.com/ethereum/go-ethereum/utils"
"github.com/niemeyer/qml"
"github.com/obscuren/secp256k1-go"
"log" "log"
"os" "os"
"os/signal" "os/signal"
...@@ -32,58 +30,9 @@ func RegisterInterupts(s *eth.Ethereum) { ...@@ -32,58 +30,9 @@ func RegisterInterupts(s *eth.Ethereum) {
}() }()
} }
func CreateKeyPair(force bool) {
data, _ := ethutil.Config.Db.Get([]byte("KeyRing"))
if len(data) == 0 || force {
pub, prv := secp256k1.GenerateKeyPair()
pair := &ethutil.Key{PrivateKey: prv, PublicKey: pub}
ethutil.Config.Db.Put([]byte("KeyRing"), pair.RlpEncode())
fmt.Printf(`
Generating new address and keypair.
Please keep your keys somewhere save.
++++++++++++++++ KeyRing +++++++++++++++++++
addr: %x
prvk: %x
pubk: %x
++++++++++++++++++++++++++++++++++++++++++++
`, pair.Address(), prv, pub)
}
}
func ImportPrivateKey(prvKey string) {
key := ethutil.FromHex(prvKey)
msg := []byte("tmp")
// Couldn't think of a better way to get the pub key
sig, _ := secp256k1.Sign(msg, key)
pub, _ := secp256k1.RecoverPubkey(msg, sig)
pair := &ethutil.Key{PrivateKey: key, PublicKey: pub}
ethutil.Config.Db.Put([]byte("KeyRing"), pair.RlpEncode())
fmt.Printf(`
Importing private key
++++++++++++++++ KeyRing +++++++++++++++++++
addr: %x
prvk: %x
pubk: %x
++++++++++++++++++++++++++++++++++++++++++++
`, pair.Address(), key, pub)
}
func main() { func main() {
Init() Init()
// Qt has to be initialized in the main thread or it will throw errors
// It has to be called BEFORE setting the maximum procs.
if UseGui {
qml.Init(nil)
}
runtime.GOMAXPROCS(runtime.NumCPU()) runtime.GOMAXPROCS(runtime.NumCPU())
ethchain.InitFees() ethchain.InitFees()
...@@ -112,7 +61,7 @@ func main() { ...@@ -112,7 +61,7 @@ func main() {
} }
if r == "y" { if r == "y" {
CreateKeyPair(true) utils.CreateKeyPair(true)
} }
os.Exit(0) os.Exit(0)
} else { } else {
...@@ -129,11 +78,11 @@ func main() { ...@@ -129,11 +78,11 @@ func main() {
} }
if r == "y" { if r == "y" {
ImportPrivateKey(ImportKey) utils.ImportPrivateKey(ImportKey)
os.Exit(0) os.Exit(0)
} }
} else { } else {
CreateKeyPair(false) utils.CreateKeyPair(false)
} }
} }
...@@ -164,52 +113,46 @@ func main() { ...@@ -164,52 +113,46 @@ func main() {
go console.Start() go console.Start()
} }
if UseGui { RegisterInterupts(ethereum)
gui := ethui.New(ethereum) ethereum.Start()
gui.Start()
//ethereum.Stop() if StartMining {
} else { log.Printf("Miner started\n")
RegisterInterupts(ethereum)
ethereum.Start() // Fake block mining. It broadcasts a new block every 5 seconds
go func() {
if StartMining { pow := &ethchain.EasyPow{}
log.Printf("Miner started\n") data, _ := ethutil.Config.Db.Get([]byte("KeyRing"))
keyRing := ethutil.NewValueFromBytes(data)
// Fake block mining. It broadcasts a new block every 5 seconds addr := keyRing.Get(1).Bytes()
go func() {
pow := &ethchain.EasyPow{} for {
data, _ := ethutil.Config.Db.Get([]byte("KeyRing")) txs := ethereum.TxPool().Flush()
keyRing := ethutil.NewValueFromBytes(data) // Create a new block which we're going to mine
addr := keyRing.Get(1).Bytes() block := ethereum.BlockChain().NewBlock(addr, txs)
log.Println("Mining on new block. Includes", len(block.Transactions()), "transactions")
for { // Apply all transactions to the block
txs := ethereum.TxPool().Flush() ethereum.StateManager().ApplyTransactions(block, block.Transactions())
// Create a new block which we're going to mine
block := ethereum.BlockChain().NewBlock(addr, txs) ethereum.StateManager().Prepare(block.State(), block.State())
log.Println("Mining on new block. Includes", len(block.Transactions()), "transactions") ethereum.StateManager().AccumelateRewards(block)
// Apply all transactions to the block
ethereum.StateManager().ApplyTransactions(block, block.Transactions()) // Search the nonce
block.Nonce = pow.Search(block)
ethereum.StateManager().Prepare(block.State(), block.State()) ethereum.Broadcast(ethwire.MsgBlockTy, []interface{}{block.Value().Val})
ethereum.StateManager().AccumelateRewards(block)
ethereum.StateManager().PrepareDefault(block)
// Search the nonce err := ethereum.StateManager().ProcessBlock(block)
block.Nonce = pow.Search(block) if err != nil {
ethereum.Broadcast(ethwire.MsgBlockTy, []interface{}{block.Value().Val}) log.Println(err)
} else {
ethereum.StateManager().PrepareDefault(block) log.Println("\n+++++++ MINED BLK +++++++\n", ethereum.BlockChain().CurrentBlock)
err := ethereum.StateManager().ProcessBlock(block) log.Printf("🔨 Mined block %x\n", block.Hash())
if err != nil {
log.Println(err)
} else {
log.Println("\n+++++++ MINED BLK +++++++\n", ethereum.BlockChain().CurrentBlock)
log.Printf("🔨 Mined block %x\n", block.Hash())
}
} }
}() }
} }()
// Wait for shutdown
ethereum.WaitForShutdown()
} }
// Wait for shutdown
ethereum.WaitForShutdown()
} }
File added
package main
import (
"encoding/json"
"fmt"
"testing"
)
type TestSource struct {
Inputs map[string]string
Expectation string
}
func NewTestSource(source string) *TestSource {
s := &TestSource{}
err := json.Unmarshal([]byte(source), s)
if err != nil {
fmt.Println(err)
}
return s
}
type TestRunner struct {
source *TestSource
}
func NewTestRunner(t *testing.T) *TestRunner {
return &TestRunner{}
}
func (runner *TestRunner) RunFromString(input string, Cb func(*TestSource)) {
source := NewTestSource(input)
Cb(source)
}
package main
/*
import (
"encoding/hex"
_ "fmt"
"github.com/ethereum/ethdb-go"
"github.com/ethereum/ethutil-go"
"testing"
)
var testsource = `
{
"inputs":{
"doe": "reindeer",
"dog": "puppy",
"dogglesworth": "cat"
},
"expectation":"e378927bfc1bd4f01a2e8d9f59bd18db8a208bb493ac0b00f93ce51d4d2af76c"
}`
func TestTestRunner(t *testing.T) {
db, _ := ethdb.NewMemDatabase()
trie := ethutil.NewTrie(db, "")
runner := NewTestRunner(t)
runner.RunFromString(testsource, func(source *TestSource) {
for key, value := range source.Inputs {
trie.Update(key, value)
}
if hex.EncodeToString(trie.Root.([]byte)) != source.Expectation {
t.Error("trie root did not match")
}
})
}
*/
package main
/*
import (
_"fmt"
)
// This will eventually go away
var Db *MemDatabase
func Testing() {
db, _ := NewMemDatabase()
Db = db
bm := NewBlockManager()
tx := NewTransaction("\x00", 20, []string{"PUSH"})
txData := tx.RlpEncode()
//fmt.Printf("%q\n", txData)
copyTx := &Transaction{}
copyTx.RlpDecode(txData)
//fmt.Println(tx)
//fmt.Println(copyTx)
tx2 := NewTransaction("\x00", 20, []string{"SET 10 6", "LD 10 10"})
blck := CreateTestBlock([]*Transaction{tx2, tx})
bm.ProcessBlock( blck )
}
*/
package utils
import (
"fmt"
"github.com/ethereum/eth-go/ethutil"
"github.com/obscuren/secp256k1-go"
)
func CreateKeyPair(force bool) {
data, _ := ethutil.Config.Db.Get([]byte("KeyRing"))
if len(data) == 0 || force {
pub, prv := secp256k1.GenerateKeyPair()
pair := &ethutil.Key{PrivateKey: prv, PublicKey: pub}
ethutil.Config.Db.Put([]byte("KeyRing"), pair.RlpEncode())
fmt.Printf(`
Generating new address and keypair.
Please keep your keys somewhere save.
++++++++++++++++ KeyRing +++++++++++++++++++
addr: %x
prvk: %x
pubk: %x
++++++++++++++++++++++++++++++++++++++++++++
`, pair.Address(), prv, pub)
}
}
func ImportPrivateKey(prvKey string) {
key := ethutil.FromHex(prvKey)
msg := []byte("tmp")
// Couldn't think of a better way to get the pub key
sig, _ := secp256k1.Sign(msg, key)
pub, _ := secp256k1.RecoverPubkey(msg, sig)
pair := &ethutil.Key{PrivateKey: key, PublicKey: pub}
ethutil.Config.Db.Put([]byte("KeyRing"), pair.RlpEncode())
fmt.Printf(`
Importing private key
++++++++++++++++ KeyRing +++++++++++++++++++
addr: %x
prvk: %x
pubk: %x
++++++++++++++++++++++++++++++++++++++++++++
`, pair.Address(), key, pub)
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment