Updated server and peers and mining processing

pull/2/head
obscuren 11 years ago
parent 849408dda6
commit bd0abe2a81
  1. 46
      dagger.go
  2. 31
      ethereum.go
  3. 40
      peer.go
  4. 45
      server.go

@ -2,7 +2,6 @@ package main
import ( import (
"math/big" "math/big"
"fmt"
"math/rand" "math/rand"
"time" "time"
"github.com/obscuren/sha3" "github.com/obscuren/sha3"
@ -26,8 +25,6 @@ func (dag *Dagger) Find(obj *big.Int, resChan chan int64) {
resChan <- rnd resChan <- rnd
// Notify other threads we've found a valid nonce // Notify other threads we've found a valid nonce
Found = true Found = true
} else {
fmt.Printf(".")
} }
// Break out if found // Break out if found
@ -37,17 +34,15 @@ func (dag *Dagger) Find(obj *big.Int, resChan chan int64) {
resChan <- 0 resChan <- 0
} }
func (dag *Dagger) Search(diff *big.Int) *big.Int { func (dag *Dagger) Search(hash, diff *big.Int) *big.Int {
// TODO fix multi threading. Somehow it results in the wrong nonce // TODO fix multi threading. Somehow it results in the wrong nonce
amountOfRoutines := 1 amountOfRoutines := 1
dag.hash = big.NewInt(0) dag.hash = hash
obj := BigPow(2, 256) obj := BigPow(2, 256)
obj = obj.Div(obj, diff) obj = obj.Div(obj, diff)
fmt.Println("diff", diff, "< objective", obj)
Found = false Found = false
resChan := make(chan int64, 3) resChan := make(chan int64, 3)
var res int64 var res int64
@ -64,8 +59,6 @@ func (dag *Dagger) Search(diff *big.Int) *big.Int {
} }
} }
fmt.Println("\n")
return big.NewInt(res) return big.NewInt(res)
} }
@ -128,32 +121,21 @@ func (dag *Dagger) Eval(N *big.Int) *big.Int {
sha.Reset() sha.Reset()
ret := new(big.Int) ret := new(big.Int)
//doneChan := make(chan bool, 3)
for k := 0; k < 4; k++ { for k := 0; k < 4; k++ {
//go func(_k int) { d := sha3.NewKeccak224()
_k := k b := new(big.Int)
d := sha3.NewKeccak224()
b := new(big.Int)
d.Reset()
d.Write(dag.hash.Bytes())
d.Write(dag.xn.Bytes())
d.Write(N.Bytes())
d.Write(big.NewInt(int64(_k)).Bytes())
b.SetBytes(Sum(d))
pk := (b.Uint64() & 0x1ffffff)
sha.Write(dag.Node(9, pk).Bytes())
//doneChan <- true
//}(k)
}
//for k := 0; k < 4; k++ { d.Reset()
// <- doneChan d.Write(dag.hash.Bytes())
//} d.Write(dag.xn.Bytes())
d.Write(N.Bytes())
d.Write(big.NewInt(int64(k)).Bytes())
b.SetBytes(Sum(d))
pk := (b.Uint64() & 0x1ffffff)
sha.Write(dag.Node(9, pk).Bytes())
}
return ret.SetBytes(Sum(sha)) return ret.SetBytes(Sum(sha))
} }

@ -6,6 +6,7 @@ import (
"os/signal" "os/signal"
"flag" "flag"
"runtime" "runtime"
"log"
_"math/big" _"math/big"
) )
@ -45,23 +46,39 @@ func main() {
if StartConsole { if StartConsole {
console := NewConsole() console := NewConsole()
console.Start() console.Start()
} else if StartMining { } else{
dagger := &Dagger{} log.Println("Starting Ethereum")
res := dagger.Search(BigPow(2, 36))
fmt.Println("nonce =", res)
} else {
fmt.Println("[DBUG]: Starting Ethereum")
server, err := NewServer() server, err := NewServer()
if err != nil { if err != nil {
fmt.Println("error NewServer:", err) log.Println(err)
return return
} }
RegisterInterupts(server) RegisterInterupts(server)
if StartMining {
log.Println("Mining started")
dagger := &Dagger{}
go func() {
for {
res := dagger.Search(Big("0"), BigPow(2, 36))
server.Broadcast("foundblock", res.Bytes())
}
}()
}
server.Start() server.Start()
err = server.ConnectToPeer("localhost:12345")
if err != nil {
log.Println(err)
server.Stop()
return
}
// Wait for shutdown // Wait for shutdown
server.WaitForShutdown() server.WaitForShutdown()
} }

@ -11,6 +11,11 @@ type InMsg struct {
data []byte // RLP encoded data data []byte // RLP encoded data
} }
type OutMsg struct {
msgType string
data []byte
}
func ReadMessage(conn net.Conn) (*InMsg, error) { func ReadMessage(conn net.Conn) (*InMsg, error) {
buff := make([]byte, 4069) buff := make([]byte, 4069)
@ -23,6 +28,7 @@ func ReadMessage(conn net.Conn) (*InMsg, error) {
} }
// Read the header (MAX n) // Read the header (MAX n)
// XXX The data specification is made up. This will change once more details have been released on the specification of the format
decoder := NewRlpDecoder(buff[:n]) decoder := NewRlpDecoder(buff[:n])
t := decoder.Get(0).AsString() t := decoder.Get(0).AsString()
if t == "" { if t == "" {
@ -32,10 +38,6 @@ func ReadMessage(conn net.Conn) (*InMsg, error) {
return &InMsg{msgType: t, data: decoder.Get(1).AsBytes()}, nil return &InMsg{msgType: t, data: decoder.Get(1).AsBytes()}, nil
} }
type OutMsg struct {
data []byte
}
type Peer struct { type Peer struct {
server *Server server *Server
conn net.Conn conn net.Conn
@ -54,22 +56,34 @@ func NewPeer(conn net.Conn, server *Server) *Peer {
} }
// Outputs any RLP encoded data to the peer // Outputs any RLP encoded data to the peer
func (p *Peer) QueueMessage(data []byte) { func (p *Peer) QueueMessage(msgType string, data []byte) {
p.outputQueue <- OutMsg{data: data} p.outputQueue <- OutMsg{msgType: msgType, data: data}
} }
func (p *Peer) HandleOutbound() { func (p *Peer) HandleOutbound() {
out: out:
for { for {
switch { select {
case msg := <-p.outputQueue:
p.WriteMessage(msg)
case <- p.quit: case <- p.quit:
break out break out
} }
} }
} }
func (p *Peer) WriteMessage(msg OutMsg) {
encoded := Encode([]interface{}{ msg.msgType, msg.data })
_, err := p.conn.Write(encoded)
if err != nil {
log.Println(err)
p.Stop()
}
}
func (p *Peer) HandleInbound() { func (p *Peer) HandleInbound() {
defer p.conn.Close() defer p.Stop()
out: out:
for { for {
@ -80,7 +94,9 @@ out:
break out break out
} }
log.Println(msg) // TODO
data, _ := Decode(msg.data, 0)
log.Printf("%s, %s\n", msg.msgType, data)
} }
// Notify the out handler we're quiting // Notify the out handler we're quiting
@ -91,3 +107,9 @@ func (p *Peer) Start() {
go p.HandleOutbound() go p.HandleOutbound()
go p.HandleInbound() go p.HandleInbound()
} }
func (p *Peer) Stop() {
defer p.conn.Close()
p.quit <- true
}

@ -4,6 +4,7 @@ import (
"container/list" "container/list"
"net" "net"
"log" "log"
_"time"
) )
var Db *LDBDatabase var Db *LDBDatabase
@ -38,7 +39,36 @@ func NewServer() (*Server, error) {
} }
func (s *Server) AddPeer(conn net.Conn) { func (s *Server) AddPeer(conn net.Conn) {
s.peers.PushBack(NewPeer(conn, s)) peer := NewPeer(conn, s)
s.peers.PushBack(peer)
peer.Start()
log.Println("Peer connected ::", conn.RemoteAddr())
}
func (s *Server) ConnectToPeer(addr string) error {
conn, err := net.Dial("tcp", addr)
if err != nil {
return err
}
peer := NewPeer(conn, s)
s.peers.PushBack(peer)
peer.Start()
log.Println("Connected to peer ::", conn.RemoteAddr())
return nil
}
func (s *Server) Broadcast(msgType string, data []byte) {
for e := s.peers.Front(); e != nil; e = e.Next() {
if peer, ok := e.Value.(*Peer); ok {
peer.QueueMessage(msgType, data)
}
}
} }
// Start the server // Start the server
@ -60,6 +90,15 @@ func (s *Server) Start() {
go s.AddPeer(conn) go s.AddPeer(conn)
} }
}() }()
// TMP
//go func() {
// for {
// s.Broadcast("block", Encode("blockdata"))
//
// time.Sleep(100 * time.Millisecond)
// }
// }()
} }
func (s *Server) Stop() { func (s *Server) Stop() {
@ -68,7 +107,9 @@ func (s *Server) Stop() {
// Loop thru the peers and close them (if we had them) // Loop thru the peers and close them (if we had them)
for e := s.peers.Front(); e != nil; e = e.Next() { for e := s.peers.Front(); e != nil; e = e.Next() {
// peer close etc if peer, ok := e.Value.(*Peer); ok {
peer.Stop()
}
} }
s.shutdownChan <- true s.shutdownChan <- true

Loading…
Cancel
Save