go-ethereum/server.go

209 lines
4.1 KiB
Go
Raw Normal View History

package main
import (
2014-01-11 08:27:08 -06:00
"container/list"
"github.com/ethereum/ethdb-go"
"github.com/ethereum/ethutil-go"
2014-01-12 09:50:59 -06:00
"github.com/ethereum/ethwire-go"
2014-01-11 08:27:08 -06:00
"log"
"net"
2014-01-12 16:46:03 -06:00
"sync/atomic"
2014-01-17 09:57:18 -06:00
"time"
)
2014-01-12 16:46:03 -06:00
func eachPeer(peers *list.List, callback func(*Peer, *list.Element)) {
2014-01-12 15:14:19 -06:00
// Loop thru the peers and close them (if we had them)
for e := peers.Front(); e != nil; e = e.Next() {
if peer, ok := e.Value.(*Peer); ok {
2014-01-12 16:46:03 -06:00
callback(peer, e)
2014-01-12 15:14:19 -06:00
}
}
}
2014-01-17 17:19:29 -06:00
const (
processReapingTimeout = 60 // TODO increase
)
type Server struct {
2014-01-11 08:27:08 -06:00
// Channel for shutting down the server
shutdownChan chan bool
// DB interface
2014-01-12 09:50:59 -06:00
//db *ethdb.LDBDatabase
db *ethdb.MemDatabase
2014-01-11 08:27:08 -06:00
// Block manager for processing new blocks and managing the block chain
blockManager *BlockManager
// The transaction pool. Transaction can be pushed on this pool
// for later including in the blocks
txPool *TxPool
2014-01-11 08:27:08 -06:00
// Peers (NYI)
peers *list.List
2014-01-12 09:50:59 -06:00
// Nonce
Nonce uint64
}
func NewServer() (*Server, error) {
2014-01-12 09:50:59 -06:00
//db, err := ethdb.NewLDBDatabase()
db, err := ethdb.NewMemDatabase()
2014-01-11 08:27:08 -06:00
if err != nil {
return nil, err
}
ethutil.Config.Db = db
2014-01-08 16:43:20 -06:00
2014-01-12 09:50:59 -06:00
nonce, _ := ethutil.RandomUint64()
2014-01-11 08:27:08 -06:00
server := &Server{
shutdownChan: make(chan bool),
db: db,
peers: list.New(),
2014-01-12 09:50:59 -06:00
Nonce: nonce,
2014-01-11 08:27:08 -06:00
}
server.txPool = NewTxPool(server)
server.blockManager = NewBlockManager(server)
2014-01-11 08:27:08 -06:00
return server, nil
}
2014-01-09 16:15:51 -06:00
func (s *Server) AddPeer(conn net.Conn) {
2014-01-12 09:50:59 -06:00
peer := NewPeer(conn, s, true)
if peer != nil {
s.peers.PushBack(peer)
peer.Start()
2014-01-12 09:50:59 -06:00
log.Println("Peer connected ::", conn.RemoteAddr())
}
}
2014-01-17 17:19:29 -06:00
func (s *Server) ProcessPeerList(addrs []string) {
for _, addr := range addrs {
// TODO Probably requires some sanity checks
s.ConnectToPeer(addr)
}
}
func (s *Server) ConnectToPeer(addr string) error {
2014-01-12 15:14:19 -06:00
peer := NewOutboundPeer(addr, s)
2014-01-11 08:27:08 -06:00
s.peers.PushBack(peer)
2014-01-12 15:14:19 -06:00
2014-01-11 08:27:08 -06:00
return nil
}
2014-01-17 17:19:29 -06:00
func (s *Server) OutboundPeers() []*Peer {
// Create a new peer slice with at least the length of the total peers
outboundPeers := make([]*Peer, s.peers.Len())
length := 0
eachPeer(s.peers, func(p *Peer, e *list.Element) {
if !p.inbound {
outboundPeers[length] = p
length++
}
})
return outboundPeers[:length]
}
func (s *Server) InboundPeers() []*Peer {
// Create a new peer slice with at least the length of the total peers
inboundPeers := make([]*Peer, s.peers.Len())
length := 0
eachPeer(s.peers, func(p *Peer, e *list.Element) {
if p.inbound {
inboundPeers[length] = p
length++
}
})
return inboundPeers[:length]
}
2014-01-17 09:57:18 -06:00
func (s *Server) Broadcast(msgType ethwire.MsgType, data []byte) {
2014-01-12 16:46:03 -06:00
eachPeer(s.peers, func(p *Peer, e *list.Element) {
2014-01-17 09:57:18 -06:00
p.QueueMessage(ethwire.NewMessage(msgType, data))
2014-01-12 15:14:19 -06:00
})
2014-01-09 16:15:51 -06:00
}
2014-01-12 16:46:03 -06:00
func (s *Server) ReapDeadPeers() {
for {
eachPeer(s.peers, func(p *Peer, e *list.Element) {
2014-01-17 10:14:59 -06:00
if atomic.LoadInt32(&p.disconnect) == 1 || (p.inbound && (time.Now().Unix()-p.lastPong) > int64(5*time.Minute)) {
2014-01-12 16:46:03 -06:00
log.Println("Dead peer found .. reaping")
s.peers.Remove(e)
}
})
time.Sleep(processReapingTimeout * time.Second)
}
}
// Start the server
func (s *Server) Start() {
2014-01-11 08:27:08 -06:00
// For now this function just blocks the main thread
ln, err := net.Listen("tcp", ":12345")
if err != nil {
2014-01-12 16:46:03 -06:00
// This is mainly for testing to create a "network"
if Debug {
log.Println("Connection listening disabled. Acting as client")
err = s.ConnectToPeer("localhost:12345")
if err != nil {
log.Println("Error starting server", err)
s.Stop()
}
} else {
log.Fatal(err)
}
} else {
// Starting accepting connections
go func() {
for {
conn, err := ln.Accept()
if err != nil {
log.Println(err)
continue
}
go s.AddPeer(conn)
}
}()
2014-01-11 08:27:08 -06:00
}
2014-01-12 16:46:03 -06:00
// Start the reaping processes
go s.ReapDeadPeers()
// Start the tx pool
s.txPool.Start()
2014-01-11 08:27:08 -06:00
// TMP
2014-01-12 18:22:33 -06:00
/*
2014-01-17 09:57:18 -06:00
go func() {
for {
s.Broadcast("block", s.blockManager.bc.GenesisBlock().RlpEncode())
2014-01-12 09:50:59 -06:00
2014-01-17 09:57:18 -06:00
time.Sleep(1000 * time.Millisecond)
}
}()
2014-01-12 18:22:33 -06:00
*/
}
func (s *Server) Stop() {
2014-01-11 08:27:08 -06:00
// Close the database
defer s.db.Close()
2014-01-12 16:46:03 -06:00
eachPeer(s.peers, func(p *Peer, e *list.Element) {
2014-01-17 09:57:18 -06:00
p.Stop()
2014-01-12 15:14:19 -06:00
})
2014-01-11 08:27:08 -06:00
s.shutdownChan <- true
s.txPool.Stop()
}
// This function will wait for a shutdown and resumes main thread execution
func (s *Server) WaitForShutdown() {
2014-01-11 08:27:08 -06:00
<-s.shutdownChan
}