forked from nspcc-dev/neo-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.go
178 lines (146 loc) · 3.83 KB
/
server.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
package main
import (
"errors"
"fmt"
"net"
"github.com/CityOfZion/neo-go/pkg/blockchain"
"github.com/CityOfZion/neo-go/pkg/database"
"github.com/CityOfZion/neo-go/pkg/syncmanager"
"github.com/CityOfZion/neo-go/pkg/connmgr"
"github.com/CityOfZion/neo-go/pkg/peer"
"github.com/CityOfZion/neo-go/pkg/wire/payload"
"github.com/CityOfZion/neo-go/pkg/wire/protocol"
"github.com/CityOfZion/neo-go/pkg/wire/util"
"github.com/CityOfZion/neo-go/pkg/wire/util/io"
)
// this file will act as a stub server
// Will create a server package
type Server struct {
chain *blockchain.Chain
db *database.LDB // TODO(Kev) change to database.Database
sm *syncmanager.Syncmanager
cm *connmgr.Connmgr
peercfg peer.LocalConfig
latestHash util.Uint256
}
func (s *Server) setupConnMgr() error {
// Connection Manager - Integrate
s.cm = connmgr.New(connmgr.Config{
GetAddress: nil,
OnConnection: s.OnConn,
OnAccept: nil,
Port: "10333",
})
return nil
}
func (s *Server) setupDatabase() error {
// Database -- Integrate
s.db = database.New("test")
return nil
}
func (s *Server) setupChain() error {
// Blockchain - Integrate
s.chain = blockchain.New(s.db, protocol.MainNet)
if s.chain != nil {
table := database.NewTable(s.db, database.HEADER)
resa, err := table.Get(database.LATESTHEADER)
s.latestHash, err = util.Uint256DecodeBytes(resa)
if err != nil {
return errors.New("Failed to get LastHeader " + err.Error())
}
} else {
return errors.New("Failed to add genesis block")
}
return nil
}
func (s *Server) setupSyncManager() error {
// Sync Manager - Integrate
s.sm = syncmanager.New(syncmanager.Config{
Chain: s.chain,
BestHash: s.latestHash,
})
return nil
}
func (s *Server) setupPeerConfig() error {
// Peer config struct - Integrate
s.peercfg = peer.LocalConfig{
Net: protocol.MainNet,
UserAgent: "DIG",
Services: protocol.NodePeerService,
Nonce: 1200,
ProtocolVer: 0,
Relay: false,
Port: 10332,
StartHeight: LocalHeight,
OnHeader: s.sm.OnHeaders,
OnBlock: s.sm.OnBlock,
}
return nil
}
func (s *Server) Run() error {
// Add all other run based methods for modules
// Connmgr - Run
s.cm.Run()
// Initial hardcoded nodes to connect to
err := s.cm.Connect(&connmgr.Request{
Addr: "seed1.ngd.network:10333",
})
return err
}
func main() {
setup()
}
func setup() {
server := Server{}
fmt.Println(server.sm)
err := server.setupConnMgr()
err = server.setupDatabase()
err = server.setupChain()
err = server.setupSyncManager()
err = server.setupPeerConfig()
fmt.Println(server.sm)
err = server.Run()
if err != nil {
fmt.Println(err)
}
<-make(chan struct{})
}
func OnHeader(peer *peer.Peer, msg *payload.HeadersMessage) {
for _, header := range msg.Headers {
if err := fileutils.UpdateFile("headers.txt", []byte(header.Hash.String())); err != nil {
fmt.Println("Error writing headers to file")
break
}
}
if len(msg.Headers) == 2000 { // reached tip
lastHeader := msg.Headers[len(msg.Headers)-1]
fmt.Println("Latest hash is", lastHeader.Hash.String())
fmt.Println("Latest Header height is", lastHeader.Index)
err := peer.RequestHeaders(lastHeader.Hash.Reverse())
if err != nil {
fmt.Println("Error getting more headers", err)
}
}
}
func LocalHeight() uint32 {
return 10
}
// OnConn is called when a successful connection has been made
func (s *Server) OnConn(conn net.Conn, addr string) {
fmt.Println(conn.RemoteAddr().String())
fmt.Println(addr)
p := peer.NewPeer(conn, false, s.peercfg)
err := p.Run()
if err != nil {
fmt.Println("Error running peer" + err.Error())
}
if err == nil {
s.sm.AddPeer(p)
}
// This is here just to quickly test the system
err = p.RequestHeaders(s.latestHash)
fmt.Println("For tests, we are only fetching first 2k batch")
if err != nil {
fmt.Println(err.Error())
}
}