Official Go implementation of the Ethereum protocol
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
go-ethereum/ethereum.go

109 lines
2.4 KiB

11 years ago
package main
import (
11 years ago
"encoding/hex"
"flag"
"fmt"
"github.com/ethereum/eth-go"
"github.com/ethereum/ethchain-go"
"github.com/ethereum/ethutil-go"
_ "github.com/ethereum/ethwire-go"
"log"
"os"
"os/signal"
"runtime"
11 years ago
)
const Debug = true
11 years ago
var StartConsole bool
11 years ago
var StartMining bool
11 years ago
var UseUPnP bool
var OutboundPort string
func Init() {
flag.BoolVar(&StartConsole, "c", false, "debug and testing console")
flag.BoolVar(&StartMining, "m", false, "start dagger mining")
11 years ago
flag.BoolVar(&UseUPnP, "upnp", false, "enable UPnP support")
flag.StringVar(&OutboundPort, "port", "30303", "listening port")
flag.Parse()
}
// 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()
}
}()
11 years ago
}
11 years ago
func main() {
runtime.GOMAXPROCS(runtime.NumCPU())
Init()
ethchain.InitFees()
ethutil.ReadConfig()
11 years ago
log.Printf("Starting Ethereum v%s\n", ethutil.Config.Ver)
// Instantiated a eth stack
11 years ago
ethereum, err := eth.New(eth.CapDefault, UseUPnP)
if err != nil {
log.Println(err)
return
}
if StartConsole {
err := os.Mkdir(ethutil.Config.ExecPath, os.ModePerm)
// Error is OK if the error is ErrExist
if err != nil && !os.IsExist(err) {
log.Panic("Unable to create EXECPATH:", err)
}
console := NewConsole(ethereum)
go console.Start()
}
RegisterInterupts(ethereum)
11 years ago
ethereum.Start()
if StartMining {
11 years ago
log.Printf("Dev Test Mining started...\n")
11 years ago
// Fake block mining. It broadcasts a new block every 5 seconds
go func() {
11 years ago
pow := &ethchain.EasyPow{}
11 years ago
addr, _ := hex.DecodeString("82c3b0b72cf62f1a9ce97c64da8072efa28225d8")
11 years ago
11 years ago
for {
11 years ago
txs := ethereum.TxPool.Flush()
// Create a new block which we're going to mine
11 years ago
block := ethereum.BlockManager.BlockChain().NewBlock(addr, txs)
// Apply all transactions to the block
ethereum.BlockManager.ApplyTransactions(block, block.Transactions())
// Search the nonce
11 years ago
block.Nonce = pow.Search(block)
err := ethereum.BlockManager.ProcessBlock(block)
if err != nil {
log.Println(err)
} else {
//ethereum.Broadcast(ethwire.MsgBlockTy, []interface{}{block.RlpValue().Value})
log.Println("\n+++++++ MINED BLK +++++++\n", block.String())
}
}
}()
}
// Wait for shutdown
ethereum.WaitForShutdown()
11 years ago
}