You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

105 lines
3.2 KiB

  1. // Copyright 2017-2018 DERO Project. All rights reserved.
  2. // Use of this source code in any form is governed by RESEARCH license.
  3. // license can be found in the LICENSE file.
  4. // GPG: 0F39 E425 8C65 3947 702A 8234 08B2 0360 A03A 9DE8
  5. //
  6. //
  7. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
  8. // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  9. // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
  10. // THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  11. // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  12. // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  13. // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  14. // STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
  15. // THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  16. package p2pv2
  17. //import "net"
  18. //import "sync"
  19. import "time"
  20. //import "container/list"
  21. //import log "github.com/sirupsen/logrus"
  22. import "github.com/vmihailenco/msgpack"
  23. //import "github.com/arnaucode/derosuite/crypto"
  24. import "github.com/arnaucode/derosuite/globals"
  25. //import "github.com/arnaucode/derosuite/blockchain"
  26. // This file defines what all needs to be responded to become a server ( handling incoming requests)
  27. // fill the common part from our chain
  28. func fill_common(common *Common) {
  29. common.Height = chain.Get_Height() - 1
  30. common.Top_ID, _ = chain.Load_BL_ID_at_Height(common.Height - 1)
  31. common.Cumulative_Difficulty = chain.Load_Block_Cumulative_Difficulty(common.Top_ID)
  32. common.Top_Version = 6 // this must be taken from the hardfork
  33. }
  34. // send the handshake command
  35. // when we initiate a connection, we immediately send a handshake
  36. func (conn *Connection) Send_Handshake_Command() {
  37. var h Handshake
  38. fill_common(&h.Common) // fill common information
  39. conn.Lock()
  40. defer conn.Unlock()
  41. // fill other information
  42. h.Local_Time = time.Now().UTC().Unix()
  43. h.Local_Port = 0
  44. h.PeerID = 0
  45. h.Network_ID = globals.Config.Network_ID
  46. //PeerList []Peer_Info `msgpack:"PLIST"`
  47. //Extension_List []string `msgpack:"EXT"`
  48. //serialize and send
  49. b, err := msgpack.Marshal(h)
  50. if err != nil {
  51. panic(err)
  52. }
  53. conn.Send_Message(b) // send the message to peer
  54. }
  55. // a handshake command has been received, make the most of it
  56. func (conn *Connection) Handle_Handshake_Command(h *Handshake) {
  57. conn.Lock()
  58. defer conn.Unlock()
  59. if h.Network_ID != globals.Config.Network_ID {
  60. logger.Debugf("Connection represents different network %x rejecting peer err:%s\n", h.Network_ID)
  61. conn.Exit = true
  62. }
  63. // TODO check whether the peer represents current hardfork, if not reject
  64. // TODO check if the peer time is +- 2hrs from the our time , reject him ,
  65. // fill other information
  66. h.Local_Time = time.Now().UTC().Unix()
  67. h.Local_Port = 0
  68. h.PeerID = 0
  69. h.Network_ID = globals.Config.Network_ID
  70. //PeerList []Peer_Info `msgpack:"PLIST"`
  71. //Extension_List []string `msgpack:"EXT"`
  72. //serialize and send
  73. b, err := msgpack.Marshal(h)
  74. if err != nil {
  75. panic(err)
  76. }
  77. conn.Send_Message(b) // send the message to peer
  78. }