forked from TrueCloudLab/neoneo-go
_pkg.dev: drop server
Nothing useful here for master, it has it all implemented already. Refs. #307.
This commit is contained in:
parent
eb95758ddc
commit
46c02de2f3
8 changed files with 0 additions and 345 deletions
|
@ -1,7 +0,0 @@
|
|||
package server
|
||||
|
||||
// etAddress will return a viable address to connect to
|
||||
// Currently it is hardcoded to be one neo node until address manager is implemented
|
||||
func (s *Server) getAddress() (string, error) {
|
||||
return "seed1.ngd.network:10333", nil
|
||||
}
|
|
@ -1,15 +0,0 @@
|
|||
package server
|
||||
|
||||
import (
|
||||
"github.com/CityOfZion/neo-go/pkg/chain"
|
||||
"github.com/CityOfZion/neo-go/pkg/database"
|
||||
"github.com/CityOfZion/neo-go/pkg/wire/protocol"
|
||||
)
|
||||
|
||||
func setupChain(db database.Database, net protocol.Magic) (*chain.Chain, error) {
|
||||
chain, err := chain.New(db, net)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return chain, nil
|
||||
}
|
|
@ -1,47 +0,0 @@
|
|||
package server
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net"
|
||||
"strconv"
|
||||
|
||||
"github.com/CityOfZion/neo-go/pkg/connmgr"
|
||||
|
||||
"github.com/CityOfZion/neo-go/pkg/peer"
|
||||
iputils "github.com/CityOfZion/neo-go/pkg/wire/util/ip"
|
||||
)
|
||||
|
||||
func setupConnManager(s *Server, port uint16) (*connmgr.Connmgr, error) {
|
||||
cfg := connmgr.Config{
|
||||
GetAddress: s.getAddress,
|
||||
OnAccept: s.onAccept,
|
||||
OnConnection: s.onConnection,
|
||||
AddressPort: iputils.GetLocalIP().String() + ":" + strconv.FormatUint(uint64(port), 10),
|
||||
}
|
||||
return connmgr.New(cfg)
|
||||
}
|
||||
|
||||
func (s *Server) onConnection(conn net.Conn, addr string) {
|
||||
fmt.Println("We have connected successfully to: ", addr)
|
||||
|
||||
p := peer.NewPeer(conn, false, *s.peerCfg)
|
||||
err := p.Run()
|
||||
if err != nil {
|
||||
fmt.Println("Error running peer" + err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
s.pmg.AddPeer(p)
|
||||
}
|
||||
|
||||
func (s *Server) onAccept(conn net.Conn) {
|
||||
fmt.Println("A peer with address: ", conn.RemoteAddr().String(), "has connect to us")
|
||||
|
||||
p := peer.NewPeer(conn, true, *s.peerCfg)
|
||||
err := p.Run()
|
||||
if err != nil {
|
||||
fmt.Println("Error running peer" + err.Error())
|
||||
return
|
||||
}
|
||||
s.pmg.AddPeer(p)
|
||||
}
|
|
@ -1,14 +0,0 @@
|
|||
package server
|
||||
|
||||
import (
|
||||
"github.com/CityOfZion/neo-go/pkg/database"
|
||||
"github.com/CityOfZion/neo-go/pkg/wire/protocol"
|
||||
)
|
||||
|
||||
func setupDatabase(net protocol.Magic) (database.Database, error) {
|
||||
db, err := database.New(net.String())
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return db, nil
|
||||
}
|
|
@ -1,23 +0,0 @@
|
|||
package server
|
||||
|
||||
import (
|
||||
"math/rand"
|
||||
|
||||
"github.com/CityOfZion/neo-go/pkg/peer"
|
||||
"github.com/CityOfZion/neo-go/pkg/wire/protocol"
|
||||
)
|
||||
|
||||
func setupPeerConfig(s *Server, port uint16, net protocol.Magic) *peer.LocalConfig {
|
||||
return &peer.LocalConfig{
|
||||
Net: net,
|
||||
UserAgent: "NEO-GO",
|
||||
Services: protocol.NodePeerService,
|
||||
Nonce: rand.Uint32(),
|
||||
ProtocolVer: 0,
|
||||
Relay: false,
|
||||
Port: port,
|
||||
StartHeight: s.chain.CurrentHeight,
|
||||
OnHeader: s.onHeader,
|
||||
OnBlock: s.onBlock,
|
||||
}
|
||||
}
|
|
@ -1,9 +0,0 @@
|
|||
package server
|
||||
|
||||
import (
|
||||
"github.com/CityOfZion/neo-go/pkg/peermgr"
|
||||
)
|
||||
|
||||
func setupPeerManager() *peermgr.PeerMgr {
|
||||
return peermgr.New()
|
||||
}
|
|
@ -1,120 +0,0 @@
|
|||
package server
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/CityOfZion/neo-go/pkg/peermgr"
|
||||
|
||||
"github.com/CityOfZion/neo-go/pkg/chain"
|
||||
"github.com/CityOfZion/neo-go/pkg/connmgr"
|
||||
"github.com/CityOfZion/neo-go/pkg/peer"
|
||||
"github.com/CityOfZion/neo-go/pkg/syncmgr"
|
||||
|
||||
"github.com/CityOfZion/neo-go/pkg/database"
|
||||
|
||||
"github.com/CityOfZion/neo-go/pkg/wire/protocol"
|
||||
)
|
||||
|
||||
// Server orchestrates all of the modules
|
||||
type Server struct {
|
||||
net protocol.Magic
|
||||
stopCh chan error
|
||||
|
||||
// Modules
|
||||
db database.Database
|
||||
smg *syncmgr.Syncmgr
|
||||
cmg *connmgr.Connmgr
|
||||
pmg *peermgr.PeerMgr
|
||||
chain *chain.Chain
|
||||
|
||||
peerCfg *peer.LocalConfig
|
||||
}
|
||||
|
||||
//New creates a new server object for a particular network and sets up each module
|
||||
func New(net protocol.Magic, port uint16) (*Server, error) {
|
||||
s := &Server{
|
||||
net: net,
|
||||
stopCh: make(chan error, 0),
|
||||
}
|
||||
|
||||
// Setup database
|
||||
db, err := setupDatabase(net)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
s.db = db
|
||||
|
||||
// setup peermgr
|
||||
peermgr := setupPeerManager()
|
||||
s.pmg = peermgr
|
||||
|
||||
// Setup chain
|
||||
chain, err := setupChain(db, net)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
s.chain = chain
|
||||
|
||||
// Setup sync manager
|
||||
syncmgr, err := setupSyncManager(s)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
s.smg = syncmgr
|
||||
|
||||
// Setup connection manager
|
||||
connmgr, err := setupConnManager(s, port)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
s.cmg = connmgr
|
||||
|
||||
// Setup peer config
|
||||
peerCfg := setupPeerConfig(s, port, net)
|
||||
s.peerCfg = peerCfg
|
||||
|
||||
return s, nil
|
||||
}
|
||||
|
||||
// Run starts the daemon by connecting to previously nodes or connectng to seed nodes.
|
||||
// This should be called once all modules have been setup
|
||||
func (s *Server) Run() error {
|
||||
fmt.Println("Server is starting up")
|
||||
|
||||
// start the connmgr
|
||||
err := s.cmg.Run()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// Attempt to connect to a peer
|
||||
err = s.cmg.NewRequest()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// Request header to start synchronisation
|
||||
bestHeader, err := s.chain.Db.GetLastHeader()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
err = s.pmg.RequestHeaders(bestHeader.Hash)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
fmt.Println("Server Successfully started")
|
||||
return s.wait()
|
||||
}
|
||||
|
||||
func (s *Server) wait() error {
|
||||
err := <-s.stopCh
|
||||
return err
|
||||
}
|
||||
|
||||
// Stop stops the server
|
||||
func (s *Server) Stop(err error) error {
|
||||
fmt.Println("Server is shutting down")
|
||||
s.stopCh <- err
|
||||
return nil
|
||||
}
|
|
@ -1,110 +0,0 @@
|
|||
package server
|
||||
|
||||
import (
|
||||
"encoding/binary"
|
||||
|
||||
"github.com/CityOfZion/neo-go/pkg/peermgr"
|
||||
|
||||
"github.com/CityOfZion/neo-go/pkg/peer"
|
||||
"github.com/CityOfZion/neo-go/pkg/syncmgr"
|
||||
"github.com/CityOfZion/neo-go/pkg/wire/payload"
|
||||
"github.com/CityOfZion/neo-go/pkg/wire/util"
|
||||
)
|
||||
|
||||
func setupSyncManager(s *Server) (*syncmgr.Syncmgr, error) {
|
||||
|
||||
cfg := &syncmgr.Config{
|
||||
ProcessBlock: s.processBlock,
|
||||
ProcessHeaders: s.processHeaders,
|
||||
|
||||
RequestBlock: s.requestBlock,
|
||||
RequestHeaders: s.requestHeaders,
|
||||
|
||||
GetNextBlockHash: s.getNextBlockHash,
|
||||
AskForNewBlocks: s.askForNewBlocks,
|
||||
|
||||
FetchHeadersAgain: s.fetchHeadersAgain,
|
||||
FetchBlockAgain: s.fetchBlockAgain,
|
||||
}
|
||||
|
||||
// Add nextBlockIndex in syncmgr
|
||||
lastBlock, err := s.chain.Db.GetLastBlock()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
nextBlockIndex := lastBlock.Index + 1
|
||||
|
||||
return syncmgr.New(cfg, nextBlockIndex), nil
|
||||
}
|
||||
|
||||
func (s *Server) onHeader(peer *peer.Peer, hdrsMessage *payload.HeadersMessage) {
|
||||
s.pmg.MsgReceived(peer, hdrsMessage.Command())
|
||||
s.smg.OnHeader(peer, hdrsMessage)
|
||||
}
|
||||
|
||||
func (s *Server) onBlock(peer *peer.Peer, blockMsg *payload.BlockMessage) {
|
||||
s.pmg.BlockMsgReceived(peer, peermgr.BlockInfo{
|
||||
BlockHash: blockMsg.Hash,
|
||||
BlockIndex: blockMsg.Index,
|
||||
})
|
||||
s.smg.OnBlock(peer, blockMsg)
|
||||
}
|
||||
|
||||
func (s *Server) processBlock(block payload.Block) error {
|
||||
return s.chain.ProcessBlock(block)
|
||||
}
|
||||
|
||||
func (s *Server) processHeaders(hdrs []*payload.BlockBase) error {
|
||||
return s.chain.ProcessHeaders(hdrs)
|
||||
}
|
||||
|
||||
func (s *Server) requestHeaders(hash util.Uint256) error {
|
||||
return s.pmg.RequestHeaders(hash)
|
||||
}
|
||||
|
||||
func (s *Server) requestBlock(hash util.Uint256, index uint32) error {
|
||||
return s.pmg.RequestBlock(peermgr.BlockInfo{
|
||||
BlockHash: hash,
|
||||
BlockIndex: index,
|
||||
})
|
||||
}
|
||||
|
||||
// getNextBlockHash searches the database for the blockHash
|
||||
// that is the height above our best block. The hash will be taken from a header.
|
||||
func (s *Server) getNextBlockHash() (util.Uint256, error) {
|
||||
bestBlock, err := s.chain.Db.GetLastBlock()
|
||||
if err != nil {
|
||||
// Panic!
|
||||
// XXX: One alternative, is to get the network, erase the database and then start again from scratch.
|
||||
// This should never happen. The latest block will always be atleast the genesis block
|
||||
panic("could not get best block from database" + err.Error())
|
||||
}
|
||||
|
||||
index := make([]byte, 4)
|
||||
binary.BigEndian.PutUint32(index, bestBlock.Index+1)
|
||||
|
||||
hdr, err := s.chain.Db.GetHeaderFromHeight(index)
|
||||
if err != nil {
|
||||
return util.Uint256{}, err
|
||||
}
|
||||
return hdr.Hash, nil
|
||||
}
|
||||
|
||||
func (s *Server) getBestBlockHash() (util.Uint256, error) {
|
||||
return util.Uint256{}, nil
|
||||
}
|
||||
|
||||
func (s *Server) askForNewBlocks() {
|
||||
// send a getblocks message with the latest block saved
|
||||
|
||||
// when we receive something then send get data
|
||||
}
|
||||
|
||||
func (s *Server) fetchHeadersAgain(util.Uint256) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *Server) fetchBlockAgain(util.Uint256) error {
|
||||
return nil
|
||||
}
|
Loading…
Reference in a new issue