peer.go 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892
  1. package eth
  2. import (
  3. "bytes"
  4. "container/list"
  5. "fmt"
  6. "math"
  7. "math/big"
  8. "net"
  9. "strconv"
  10. "strings"
  11. "sync/atomic"
  12. "time"
  13. "github.com/ethereum/go-ethereum/chain"
  14. "github.com/ethereum/go-ethereum/ethutil"
  15. "github.com/ethereum/go-ethereum/logger"
  16. "github.com/ethereum/go-ethereum/wire"
  17. )
  18. var peerlogger = logger.NewLogger("PEER")
  19. const (
  20. // The size of the output buffer for writing messages
  21. outputBufferSize = 50
  22. // Current protocol version
  23. ProtocolVersion = 37
  24. // Current P2P version
  25. P2PVersion = 2
  26. // Ethereum network version
  27. NetVersion = 0
  28. // Interval for ping/pong message
  29. pingPongTimer = 2 * time.Second
  30. )
  31. type DiscReason byte
  32. const (
  33. // Values are given explicitly instead of by iota because these values are
  34. // defined by the wire protocol spec; it is easier for humans to ensure
  35. // correctness when values are explicit.
  36. DiscRequested DiscReason = iota
  37. DiscReTcpSysErr
  38. DiscBadProto
  39. DiscBadPeer
  40. DiscTooManyPeers
  41. DiscConnDup
  42. DiscGenesisErr
  43. DiscProtoErr
  44. DiscQuitting
  45. )
  46. var discReasonToString = []string{
  47. "requested",
  48. "TCP sys error",
  49. "bad protocol",
  50. "useless peer",
  51. "too many peers",
  52. "already connected",
  53. "wrong genesis block",
  54. "incompatible network",
  55. "quitting",
  56. }
  57. func (d DiscReason) String() string {
  58. if len(discReasonToString) < int(d) {
  59. return "Unknown"
  60. }
  61. return discReasonToString[d]
  62. }
  63. // Peer capabilities
  64. type Caps byte
  65. const (
  66. CapPeerDiscTy Caps = 1 << iota
  67. CapTxTy
  68. CapChainTy
  69. CapDefault = CapChainTy | CapTxTy | CapPeerDiscTy
  70. )
  71. var capsToString = map[Caps]string{
  72. CapPeerDiscTy: "Peer discovery",
  73. CapTxTy: "Transaction relaying",
  74. CapChainTy: "Block chain relaying",
  75. }
  76. func (c Caps) IsCap(cap Caps) bool {
  77. return c&cap > 0
  78. }
  79. func (c Caps) String() string {
  80. var caps []string
  81. if c.IsCap(CapPeerDiscTy) {
  82. caps = append(caps, capsToString[CapPeerDiscTy])
  83. }
  84. if c.IsCap(CapChainTy) {
  85. caps = append(caps, capsToString[CapChainTy])
  86. }
  87. if c.IsCap(CapTxTy) {
  88. caps = append(caps, capsToString[CapTxTy])
  89. }
  90. return strings.Join(caps, " | ")
  91. }
  92. type Peer struct {
  93. // Ethereum interface
  94. ethereum *Ethereum
  95. // Net connection
  96. conn net.Conn
  97. // Output queue which is used to communicate and handle messages
  98. outputQueue chan *wire.Msg
  99. // Quit channel
  100. quit chan bool
  101. // Determines whether it's an inbound or outbound peer
  102. inbound bool
  103. // Flag for checking the peer's connectivity state
  104. connected int32
  105. disconnect int32
  106. // Last known message send
  107. lastSend time.Time
  108. // Indicated whether a verack has been send or not
  109. // This flag is used by writeMessage to check if messages are allowed
  110. // to be send or not. If no version is known all messages are ignored.
  111. versionKnown bool
  112. statusKnown bool
  113. // Last received pong message
  114. lastPong int64
  115. lastBlockReceived time.Time
  116. doneFetchingHashes bool
  117. host []byte
  118. port uint16
  119. caps Caps
  120. td *big.Int
  121. bestHash []byte
  122. lastReceivedHash []byte
  123. requestedHashes [][]byte
  124. // This peer's public key
  125. pubkey []byte
  126. // Indicated whether the node is catching up or not
  127. catchingUp bool
  128. diverted bool
  129. blocksRequested int
  130. version string
  131. // We use this to give some kind of pingtime to a node, not very accurate, could be improved.
  132. pingTime time.Duration
  133. pingStartTime time.Time
  134. lastRequestedBlock *chain.Block
  135. protocolCaps *ethutil.Value
  136. }
  137. func NewPeer(conn net.Conn, ethereum *Ethereum, inbound bool) *Peer {
  138. pubkey := ethereum.KeyManager().PublicKey()[1:]
  139. return &Peer{
  140. outputQueue: make(chan *wire.Msg, outputBufferSize),
  141. quit: make(chan bool),
  142. ethereum: ethereum,
  143. conn: conn,
  144. inbound: inbound,
  145. disconnect: 0,
  146. connected: 1,
  147. port: 30303,
  148. pubkey: pubkey,
  149. blocksRequested: 10,
  150. caps: ethereum.ServerCaps(),
  151. version: ethereum.ClientIdentity().String(),
  152. protocolCaps: ethutil.NewValue(nil),
  153. td: big.NewInt(0),
  154. doneFetchingHashes: true,
  155. }
  156. }
  157. func NewOutboundPeer(addr string, ethereum *Ethereum, caps Caps) *Peer {
  158. p := &Peer{
  159. outputQueue: make(chan *wire.Msg, outputBufferSize),
  160. quit: make(chan bool),
  161. ethereum: ethereum,
  162. inbound: false,
  163. connected: 0,
  164. disconnect: 0,
  165. port: 30303,
  166. caps: caps,
  167. version: ethereum.ClientIdentity().String(),
  168. protocolCaps: ethutil.NewValue(nil),
  169. td: big.NewInt(0),
  170. doneFetchingHashes: true,
  171. }
  172. // Set up the connection in another goroutine so we don't block the main thread
  173. go func() {
  174. conn, err := p.Connect(addr)
  175. if err != nil {
  176. //peerlogger.Debugln("Connection to peer failed. Giving up.", err)
  177. p.Stop()
  178. return
  179. }
  180. p.conn = conn
  181. // Atomically set the connection state
  182. atomic.StoreInt32(&p.connected, 1)
  183. atomic.StoreInt32(&p.disconnect, 0)
  184. p.Start()
  185. }()
  186. return p
  187. }
  188. func (self *Peer) Connect(addr string) (conn net.Conn, err error) {
  189. const maxTries = 3
  190. for attempts := 0; attempts < maxTries; attempts++ {
  191. conn, err = net.DialTimeout("tcp", addr, 10*time.Second)
  192. if err != nil {
  193. time.Sleep(time.Duration(attempts*20) * time.Second)
  194. continue
  195. }
  196. // Success
  197. return
  198. }
  199. return
  200. }
  201. // Getters
  202. func (p *Peer) PingTime() string {
  203. return p.pingTime.String()
  204. }
  205. func (p *Peer) Inbound() bool {
  206. return p.inbound
  207. }
  208. func (p *Peer) LastSend() time.Time {
  209. return p.lastSend
  210. }
  211. func (p *Peer) LastPong() int64 {
  212. return p.lastPong
  213. }
  214. func (p *Peer) Host() []byte {
  215. return p.host
  216. }
  217. func (p *Peer) Port() uint16 {
  218. return p.port
  219. }
  220. func (p *Peer) Version() string {
  221. return p.version
  222. }
  223. func (p *Peer) Connected() *int32 {
  224. return &p.connected
  225. }
  226. // Setters
  227. func (p *Peer) SetVersion(version string) {
  228. p.version = version
  229. }
  230. // Outputs any RLP encoded data to the peer
  231. func (p *Peer) QueueMessage(msg *wire.Msg) {
  232. if atomic.LoadInt32(&p.connected) != 1 {
  233. return
  234. }
  235. p.outputQueue <- msg
  236. }
  237. func (p *Peer) writeMessage(msg *wire.Msg) {
  238. // Ignore the write if we're not connected
  239. if atomic.LoadInt32(&p.connected) != 1 {
  240. return
  241. }
  242. if !p.versionKnown {
  243. switch msg.Type {
  244. case wire.MsgHandshakeTy: // Ok
  245. default: // Anything but ack is allowed
  246. return
  247. }
  248. } else {
  249. /*
  250. if !p.statusKnown {
  251. switch msg.Type {
  252. case wire.MsgStatusTy: // Ok
  253. default: // Anything but ack is allowed
  254. return
  255. }
  256. }
  257. */
  258. }
  259. peerlogger.DebugDetailf("(%v) <= %v\n", p.conn.RemoteAddr(), formatMessage(msg))
  260. err := wire.WriteMessage(p.conn, msg)
  261. if err != nil {
  262. peerlogger.Debugln(" Can't send message:", err)
  263. // Stop the client if there was an error writing to it
  264. p.Stop()
  265. return
  266. }
  267. }
  268. // Outbound message handler. Outbound messages are handled here
  269. func (p *Peer) HandleOutbound() {
  270. // The ping timer. Makes sure that every 2 minutes a ping is send to the peer
  271. pingTimer := time.NewTicker(pingPongTimer)
  272. serviceTimer := time.NewTicker(10 * time.Second)
  273. out:
  274. for {
  275. skip:
  276. select {
  277. // Main message queue. All outbound messages are processed through here
  278. case msg := <-p.outputQueue:
  279. if !p.statusKnown {
  280. switch msg.Type {
  281. case wire.MsgTxTy, wire.MsgGetBlockHashesTy, wire.MsgBlockHashesTy, wire.MsgGetBlocksTy, wire.MsgBlockTy:
  282. break skip
  283. }
  284. }
  285. p.writeMessage(msg)
  286. p.lastSend = time.Now()
  287. // Ping timer
  288. case <-pingTimer.C:
  289. /*
  290. timeSince := time.Since(time.Unix(p.lastPong, 0))
  291. if !p.pingStartTime.IsZero() && p.lastPong != 0 && timeSince > (pingPongTimer+30*time.Second) {
  292. peerlogger.Infof("Peer did not respond to latest pong fast enough, it took %s, disconnecting.\n", timeSince)
  293. p.Stop()
  294. return
  295. }
  296. */
  297. p.writeMessage(wire.NewMessage(wire.MsgPingTy, ""))
  298. p.pingStartTime = time.Now()
  299. // Service timer takes care of peer broadcasting, transaction
  300. // posting or block posting
  301. case <-serviceTimer.C:
  302. p.QueueMessage(wire.NewMessage(wire.MsgGetPeersTy, ""))
  303. case <-p.quit:
  304. // Break out of the for loop if a quit message is posted
  305. break out
  306. }
  307. }
  308. clean:
  309. // This loop is for draining the output queue and anybody waiting for us
  310. for {
  311. select {
  312. case <-p.outputQueue:
  313. // TODO
  314. default:
  315. break clean
  316. }
  317. }
  318. }
  319. func formatMessage(msg *wire.Msg) (ret string) {
  320. ret = fmt.Sprintf("%v %v", msg.Type, msg.Data)
  321. /*
  322. XXX Commented out because I need the log level here to determine
  323. if i should or shouldn't generate this message
  324. */
  325. /*
  326. switch msg.Type {
  327. case wire.MsgPeersTy:
  328. ret += fmt.Sprintf("(%d entries)", msg.Data.Len())
  329. case wire.MsgBlockTy:
  330. b1, b2 := chain.NewBlockFromRlpValue(msg.Data.Get(0)), ethchain.NewBlockFromRlpValue(msg.Data.Get(msg.Data.Len()-1))
  331. ret += fmt.Sprintf("(%d entries) %x - %x", msg.Data.Len(), b1.Hash()[0:4], b2.Hash()[0:4])
  332. case wire.MsgBlockHashesTy:
  333. h1, h2 := msg.Data.Get(0).Bytes(), msg.Data.Get(msg.Data.Len()-1).Bytes()
  334. ret += fmt.Sprintf("(%d entries) %x - %x", msg.Data.Len(), h1, h2)
  335. }
  336. */
  337. return
  338. }
  339. // Inbound handler. Inbound messages are received here and passed to the appropriate methods
  340. func (p *Peer) HandleInbound() {
  341. for atomic.LoadInt32(&p.disconnect) == 0 {
  342. // HMM?
  343. time.Sleep(50 * time.Millisecond)
  344. // Wait for a message from the peer
  345. msgs, err := wire.ReadMessages(p.conn)
  346. if err != nil {
  347. peerlogger.Debugln(err)
  348. }
  349. for _, msg := range msgs {
  350. peerlogger.DebugDetailf("(%v) => %v\n", p.conn.RemoteAddr(), formatMessage(msg))
  351. switch msg.Type {
  352. case wire.MsgHandshakeTy:
  353. // Version message
  354. p.handleHandshake(msg)
  355. //if p.caps.IsCap(CapPeerDiscTy) {
  356. p.QueueMessage(wire.NewMessage(wire.MsgGetPeersTy, ""))
  357. //}
  358. case wire.MsgDiscTy:
  359. p.Stop()
  360. peerlogger.Infoln("Disconnect peer: ", DiscReason(msg.Data.Get(0).Uint()))
  361. case wire.MsgPingTy:
  362. // Respond back with pong
  363. p.QueueMessage(wire.NewMessage(wire.MsgPongTy, ""))
  364. case wire.MsgPongTy:
  365. // If we received a pong back from a peer we set the
  366. // last pong so the peer handler knows this peer is still
  367. // active.
  368. p.lastPong = time.Now().Unix()
  369. p.pingTime = time.Since(p.pingStartTime)
  370. case wire.MsgTxTy:
  371. // If the message was a transaction queue the transaction
  372. // in the TxPool where it will undergo validation and
  373. // processing when a new block is found
  374. for i := 0; i < msg.Data.Len(); i++ {
  375. tx := chain.NewTransactionFromValue(msg.Data.Get(i))
  376. p.ethereum.TxPool().QueueTransaction(tx)
  377. }
  378. case wire.MsgGetPeersTy:
  379. // Peer asked for list of connected peers
  380. //p.pushPeers()
  381. case wire.MsgPeersTy:
  382. // Received a list of peers (probably because MsgGetPeersTy was send)
  383. data := msg.Data
  384. // Create new list of possible peers for the ethereum to process
  385. peers := make([]string, data.Len())
  386. // Parse each possible peer
  387. for i := 0; i < data.Len(); i++ {
  388. value := data.Get(i)
  389. peers[i] = unpackAddr(value.Get(0), value.Get(1).Uint())
  390. }
  391. // Connect to the list of peers
  392. p.ethereum.ProcessPeerList(peers)
  393. case wire.MsgStatusTy:
  394. // Handle peer's status msg
  395. p.handleStatus(msg)
  396. }
  397. // TMP
  398. if p.statusKnown {
  399. switch msg.Type {
  400. /*
  401. case wire.MsgGetTxsTy:
  402. // Get the current transactions of the pool
  403. txs := p.ethereum.TxPool().CurrentTransactions()
  404. // Get the RlpData values from the txs
  405. txsInterface := make([]interface{}, len(txs))
  406. for i, tx := range txs {
  407. txsInterface[i] = tx.RlpData()
  408. }
  409. // Broadcast it back to the peer
  410. p.QueueMessage(wire.NewMessage(wire.MsgTxTy, txsInterface))
  411. */
  412. case wire.MsgGetBlockHashesTy:
  413. if msg.Data.Len() < 2 {
  414. peerlogger.Debugln("err: argument length invalid ", msg.Data.Len())
  415. }
  416. hash := msg.Data.Get(0).Bytes()
  417. amount := msg.Data.Get(1).Uint()
  418. hashes := p.ethereum.ChainManager().GetChainHashesFromHash(hash, amount)
  419. p.QueueMessage(wire.NewMessage(wire.MsgBlockHashesTy, ethutil.ByteSliceToInterface(hashes)))
  420. case wire.MsgGetBlocksTy:
  421. // Limit to max 300 blocks
  422. max := int(math.Min(float64(msg.Data.Len()), 300.0))
  423. var blocks []interface{}
  424. for i := 0; i < max; i++ {
  425. hash := msg.Data.Get(i).Bytes()
  426. block := p.ethereum.ChainManager().GetBlock(hash)
  427. if block != nil {
  428. blocks = append(blocks, block.Value().Raw())
  429. }
  430. }
  431. p.QueueMessage(wire.NewMessage(wire.MsgBlockTy, blocks))
  432. case wire.MsgBlockHashesTy:
  433. p.catchingUp = true
  434. blockPool := p.ethereum.blockPool
  435. foundCommonHash := false
  436. it := msg.Data.NewIterator()
  437. for it.Next() {
  438. hash := it.Value().Bytes()
  439. p.lastReceivedHash = hash
  440. if blockPool.HasCommonHash(hash) {
  441. foundCommonHash = true
  442. break
  443. }
  444. blockPool.AddHash(hash, p)
  445. }
  446. if !foundCommonHash {
  447. //if !p.FetchHashes() {
  448. // p.doneFetchingHashes = true
  449. //}
  450. p.FetchHashes()
  451. } else {
  452. peerlogger.Infof("Found common hash (%x...)\n", p.lastReceivedHash[0:4])
  453. p.doneFetchingHashes = true
  454. }
  455. case wire.MsgBlockTy:
  456. p.catchingUp = true
  457. blockPool := p.ethereum.blockPool
  458. it := msg.Data.NewIterator()
  459. for it.Next() {
  460. block := chain.NewBlockFromRlpValue(it.Value())
  461. blockPool.Add(block, p)
  462. p.lastBlockReceived = time.Now()
  463. }
  464. case wire.MsgNewBlockTy:
  465. var (
  466. blockPool = p.ethereum.blockPool
  467. block = chain.NewBlockFromRlpValue(msg.Data.Get(0))
  468. td = msg.Data.Get(1).BigInt()
  469. )
  470. if td.Cmp(blockPool.td) > 0 {
  471. p.ethereum.blockPool.AddNew(block, p)
  472. }
  473. }
  474. }
  475. }
  476. }
  477. p.Stop()
  478. }
  479. func (self *Peer) FetchBlocks(hashes [][]byte) {
  480. if len(hashes) > 0 {
  481. peerlogger.Debugf("Fetching blocks (%d)\n", len(hashes))
  482. self.QueueMessage(wire.NewMessage(wire.MsgGetBlocksTy, ethutil.ByteSliceToInterface(hashes)))
  483. }
  484. }
  485. func (self *Peer) FetchHashes() bool {
  486. blockPool := self.ethereum.blockPool
  487. return blockPool.FetchHashes(self)
  488. }
  489. func (self *Peer) FetchingHashes() bool {
  490. return !self.doneFetchingHashes
  491. }
  492. // General update method
  493. func (self *Peer) update() {
  494. serviceTimer := time.NewTicker(100 * time.Millisecond)
  495. out:
  496. for {
  497. select {
  498. case <-serviceTimer.C:
  499. if self.IsCap("eth") {
  500. var (
  501. sinceBlock = time.Since(self.lastBlockReceived)
  502. )
  503. if sinceBlock > 5*time.Second {
  504. self.catchingUp = false
  505. }
  506. }
  507. case <-self.quit:
  508. break out
  509. }
  510. }
  511. serviceTimer.Stop()
  512. }
  513. func (p *Peer) Start() {
  514. peerHost, peerPort, _ := net.SplitHostPort(p.conn.LocalAddr().String())
  515. servHost, servPort, _ := net.SplitHostPort(p.conn.RemoteAddr().String())
  516. if p.inbound {
  517. p.host, p.port = packAddr(peerHost, peerPort)
  518. } else {
  519. p.host, p.port = packAddr(servHost, servPort)
  520. }
  521. err := p.pushHandshake()
  522. if err != nil {
  523. peerlogger.Debugln("Peer can't send outbound version ack", err)
  524. p.Stop()
  525. return
  526. }
  527. go p.HandleOutbound()
  528. // Run the inbound handler in a new goroutine
  529. go p.HandleInbound()
  530. // Run the general update handler
  531. go p.update()
  532. // Wait a few seconds for startup and then ask for an initial ping
  533. time.Sleep(2 * time.Second)
  534. p.writeMessage(wire.NewMessage(wire.MsgPingTy, ""))
  535. p.pingStartTime = time.Now()
  536. }
  537. func (p *Peer) Stop() {
  538. p.StopWithReason(DiscRequested)
  539. }
  540. func (p *Peer) StopWithReason(reason DiscReason) {
  541. if atomic.AddInt32(&p.disconnect, 1) != 1 {
  542. return
  543. }
  544. // Pre-emptively remove the peer; don't wait for reaping. We already know it's dead if we are here
  545. p.ethereum.RemovePeer(p)
  546. close(p.quit)
  547. if atomic.LoadInt32(&p.connected) != 0 {
  548. p.writeMessage(wire.NewMessage(wire.MsgDiscTy, reason))
  549. p.conn.Close()
  550. }
  551. }
  552. func (p *Peer) peersMessage() *wire.Msg {
  553. outPeers := make([]interface{}, len(p.ethereum.InOutPeers()))
  554. // Serialise each peer
  555. for i, peer := range p.ethereum.InOutPeers() {
  556. // Don't return localhost as valid peer
  557. if !net.ParseIP(peer.conn.RemoteAddr().String()).IsLoopback() {
  558. outPeers[i] = peer.RlpData()
  559. }
  560. }
  561. // Return the message to the peer with the known list of connected clients
  562. return wire.NewMessage(wire.MsgPeersTy, outPeers)
  563. }
  564. // Pushes the list of outbound peers to the client when requested
  565. func (p *Peer) pushPeers() {
  566. p.QueueMessage(p.peersMessage())
  567. }
  568. func (self *Peer) pushStatus() {
  569. msg := wire.NewMessage(wire.MsgStatusTy, []interface{}{
  570. uint32(ProtocolVersion),
  571. uint32(NetVersion),
  572. self.ethereum.ChainManager().TD,
  573. self.ethereum.ChainManager().CurrentBlock.Hash(),
  574. self.ethereum.ChainManager().Genesis().Hash(),
  575. })
  576. self.QueueMessage(msg)
  577. }
  578. func (self *Peer) handleStatus(msg *wire.Msg) {
  579. c := msg.Data
  580. var (
  581. //protoVersion = c.Get(0).Uint()
  582. netVersion = c.Get(1).Uint()
  583. td = c.Get(2).BigInt()
  584. bestHash = c.Get(3).Bytes()
  585. genesis = c.Get(4).Bytes()
  586. )
  587. if bytes.Compare(self.ethereum.ChainManager().Genesis().Hash(), genesis) != 0 {
  588. loggerger.Warnf("Invalid genisis hash %x. Disabling [eth]\n", genesis)
  589. return
  590. }
  591. if netVersion != NetVersion {
  592. loggerger.Warnf("Invalid network version %d. Disabling [eth]\n", netVersion)
  593. return
  594. }
  595. /*
  596. if protoVersion != ProtocolVersion {
  597. loggerger.Warnf("Invalid protocol version %d. Disabling [eth]\n", protoVersion)
  598. return
  599. }
  600. */
  601. // Get the td and last hash
  602. self.td = td
  603. self.bestHash = bestHash
  604. self.lastReceivedHash = bestHash
  605. self.statusKnown = true
  606. // Compare the total TD with the blockchain TD. If remote is higher
  607. // fetch hashes from highest TD node.
  608. self.FetchHashes()
  609. loggerger.Infof("Peer is [eth] capable. (TD = %v ~ %x)", self.td, self.bestHash)
  610. }
  611. func (p *Peer) pushHandshake() error {
  612. pubkey := p.ethereum.KeyManager().PublicKey()
  613. msg := wire.NewMessage(wire.MsgHandshakeTy, []interface{}{
  614. P2PVersion, []byte(p.version), []interface{}{[]interface{}{"eth", ProtocolVersion}}, p.port, pubkey[1:],
  615. })
  616. p.QueueMessage(msg)
  617. return nil
  618. }
  619. func (p *Peer) handleHandshake(msg *wire.Msg) {
  620. c := msg.Data
  621. var (
  622. p2pVersion = c.Get(0).Uint()
  623. clientId = c.Get(1).Str()
  624. caps = c.Get(2)
  625. port = c.Get(3).Uint()
  626. pub = c.Get(4).Bytes()
  627. )
  628. // Check correctness of p2p protocol version
  629. if p2pVersion != P2PVersion {
  630. fmt.Println(p)
  631. peerlogger.Debugf("Invalid P2P version. Require protocol %d, received %d\n", P2PVersion, p2pVersion)
  632. p.Stop()
  633. return
  634. }
  635. // Handle the pub key (validation, uniqueness)
  636. if len(pub) == 0 {
  637. peerlogger.Warnln("Pubkey required, not supplied in handshake.")
  638. p.Stop()
  639. return
  640. }
  641. // Self connect detection
  642. pubkey := p.ethereum.KeyManager().PublicKey()
  643. if bytes.Compare(pubkey[1:], pub) == 0 {
  644. p.Stop()
  645. return
  646. }
  647. // Check for blacklisting
  648. for _, pk := range p.ethereum.blacklist {
  649. if bytes.Compare(pk, pub) == 0 {
  650. peerlogger.Debugf("Blacklisted peer tried to connect (%x...)\n", pubkey[0:4])
  651. p.StopWithReason(DiscBadPeer)
  652. return
  653. }
  654. }
  655. usedPub := 0
  656. // This peer is already added to the peerlist so we expect to find a double pubkey at least once
  657. eachPeer(p.ethereum.Peers(), func(peer *Peer, e *list.Element) {
  658. if bytes.Compare(pub, peer.pubkey) == 0 {
  659. usedPub++
  660. }
  661. })
  662. if usedPub > 0 {
  663. peerlogger.Debugf("Pubkey %x found more then once. Already connected to client.", p.pubkey)
  664. p.Stop()
  665. return
  666. }
  667. p.pubkey = pub
  668. // If this is an inbound connection send an ack back
  669. if p.inbound {
  670. p.port = uint16(port)
  671. }
  672. p.SetVersion(clientId)
  673. p.versionKnown = true
  674. p.ethereum.PushPeer(p)
  675. p.ethereum.eventMux.Post(PeerListEvent{p.ethereum.Peers()})
  676. p.protocolCaps = caps
  677. it := caps.NewIterator()
  678. var capsStrs []string
  679. for it.Next() {
  680. cap := it.Value().Get(0).Str()
  681. ver := it.Value().Get(1).Uint()
  682. switch cap {
  683. case "eth":
  684. if ver != ProtocolVersion {
  685. loggerger.Warnf("Invalid protocol version %d. Disabling [eth]\n", ver)
  686. continue
  687. }
  688. p.pushStatus()
  689. }
  690. capsStrs = append(capsStrs, cap)
  691. }
  692. peerlogger.Infof("Added peer (%s) %d / %d (%v)\n", p.conn.RemoteAddr(), p.ethereum.Peers().Len(), p.ethereum.MaxPeers, capsStrs)
  693. peerlogger.Debugln(p)
  694. }
  695. func (self *Peer) IsCap(cap string) bool {
  696. capsIt := self.protocolCaps.NewIterator()
  697. for capsIt.Next() {
  698. if capsIt.Value().Str() == cap {
  699. return true
  700. }
  701. }
  702. return false
  703. }
  704. func (self *Peer) Caps() *ethutil.Value {
  705. return self.protocolCaps
  706. }
  707. func (p *Peer) String() string {
  708. var strBoundType string
  709. if p.inbound {
  710. strBoundType = "inbound"
  711. } else {
  712. strBoundType = "outbound"
  713. }
  714. var strConnectType string
  715. if atomic.LoadInt32(&p.disconnect) == 0 {
  716. strConnectType = "connected"
  717. } else {
  718. strConnectType = "disconnected"
  719. }
  720. return fmt.Sprintf("[%s] (%s) %v %s [%s]", strConnectType, strBoundType, p.conn.RemoteAddr(), p.version, p.caps)
  721. }
  722. func (p *Peer) RlpData() []interface{} {
  723. return []interface{}{p.host, p.port, p.pubkey}
  724. }
  725. func packAddr(address, _port string) (host []byte, port uint16) {
  726. p, _ := strconv.Atoi(_port)
  727. port = uint16(p)
  728. h := net.ParseIP(address)
  729. if ip := h.To4(); ip != nil {
  730. host = []byte(ip)
  731. } else {
  732. host = []byte(h)
  733. }
  734. return
  735. }
  736. func unpackAddr(value *ethutil.Value, p uint64) string {
  737. host, _ := net.IP(value.Bytes()).MarshalText()
  738. prt := strconv.Itoa(int(p))
  739. return net.JoinHostPort(string(host), prt)
  740. }