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.

139 lines
4.5 KiB

  1. package p2p
  2. //import "fmt"
  3. import "bytes"
  4. import "time"
  5. import "encoding/binary"
  6. import "github.com/romana/rlog"
  7. import log "github.com/sirupsen/logrus"
  8. // outgoing response needs to be as follows
  9. /*
  10. 0020 01 11 01 01 01 01 02 01 01 08 0a 6c 6f 63 61 ............loca
  11. 0030 6c 5f 74 69 6d 65 05 c9 ea 45 5a 00 00 00 00 0c l_time...EZ.....
  12. 0040 70 61 79 6c 6f 61 64 5f 64 61 74 61 0c 10 15 63 payload_data...c
  13. 0050 75 6d 75 6c 61 74 69 76 65 5f 64 69 66 66 69 63 umulative_diffic
  14. 0060 75 6c 74 79 05 37 62 00 00 00 00 00 00 0e 63 75 ulty.7b.......cu
  15. 0070 72 72 65 6e 74 5f 68 65 69 67 68 74 05 ec 04 00 rrent_height....
  16. 0080 00 00 00 00 00 06 74 6f 70 5f 69 64 0a 80 85 d9 ......top_id....
  17. 0090 d2 f6 cd ee 1b 87 dd d1 ac 3d 15 db 4d 72 63 ca .........=..Mrc.
  18. 00a0 1c 43 37 db 53 78 7f 03 3b 74 f6 fc 45 0e 0b 74 .C7.Sx..;t..E..t
  19. 00b0 6f 70 5f 76 65 72 73 69 6f 6e 08 06 op_version..
  20. */
  21. // handle P2P_COMMAND_TIMED_SYNC_T
  22. func Handle_P2P_Timed_Sync(connection *Connection,
  23. i_command_header *Levin_Header, buf []byte) {
  24. // deserialize data header
  25. var i_data_header Levin_Data_Header // incoming data header
  26. err := i_data_header.DeSerialize(buf)
  27. if err != nil {
  28. log.Debugf("Invalid P2P_COMMAND_TIMED_SYNC_T, disconnecting peer")
  29. connection.Exit = true
  30. return
  31. }
  32. // parse incoming core data
  33. var peer_core_data CORE_DATA
  34. pos := bytes.Index(i_data_header.Data, []byte("payload_data")) // at this point to node data and should be parsed as such
  35. if pos < 0 {
  36. log.Debugf("Invalid P2P_COMMAND_TIMED_SYNC_T, disconnecting peer")
  37. connection.Exit = true
  38. return
  39. }
  40. err = peer_core_data.DeSerialize(i_data_header.Data[pos-1:])
  41. if err != nil {
  42. log.Debugf("Invalid P2P_COMMAND_TIMED_SYNC_T, disconnecting peer")
  43. connection.Exit = true
  44. return
  45. }
  46. rlog.Trace(5, "Incoming core data %+v \n", peer_core_data)
  47. // TODO if cumulative difficulty at this top mismatches ours start resync
  48. // if height is more than ours, start resync
  49. var our_core_data CORE_DATA
  50. // fill the structure with our chain data
  51. our_core_data.Top_ID = chain.Get_Top_ID()
  52. our_core_data.Cumulative_Difficulty = chain.Load_Block_Cumulative_Difficulty(our_core_data.Top_ID) // get cumulative difficulty for top block
  53. our_core_data.Current_Height = chain.Load_Height_for_BL_ID(our_core_data.Top_ID)
  54. our_core_data.Top_Version = 6
  55. serialised_bytes, _ := our_core_data.Serialize()
  56. header_bytes := []byte{0x01, 0x11, 0x01, 0x01, 0x01, 0x01, 0x02, 0x01, 0x01, 0x08, 0x0a, 0x6c, 0x6f, 0x63, 0x61,
  57. 0x6c, 0x5f, 0x74, 0x69, 0x6d, 0x65, 0x05,
  58. /* time bytes start here */ 0xc9, 0xea, 0x45, 0x5a, 0x00, 0x00, 0x00, 0x00}
  59. binary.LittleEndian.PutUint64(header_bytes[22:], uint64(time.Now().Unix()))
  60. //fmt.Printf("header %x serialised_bytes %x\n", header_bytes, serialised_bytes)
  61. response_bytes := append(serialised_bytes, header_bytes...)
  62. // create a new response header
  63. var o_command_header Levin_Header
  64. //var o_data_header Levin_Data_Header
  65. o_command_header.CB = uint64(len(response_bytes))
  66. o_command_header.Command = P2P_COMMAND_TIMED_SYNC
  67. o_command_header.ReturnData = false
  68. o_command_header.Flags = LEVIN_PACKET_RESPONSE
  69. o_command_header_bytes, _ := o_command_header.Serialize()
  70. connection.Conn.Write(o_command_header_bytes)
  71. connection.Conn.Write(response_bytes[:])
  72. connection.Last_Height = peer_core_data.Current_Height
  73. connection.Top_Version = uint64(peer_core_data.Top_Version)
  74. connection.Top_ID = peer_core_data.Top_ID
  75. connection.Cumulative_Difficulty = peer_core_data.Cumulative_Difficulty
  76. connection.State = ACTIVE
  77. // lets check whether we need to resync with this peer
  78. if chain.IsLagging(peer_core_data.Cumulative_Difficulty, peer_core_data.Current_Height, peer_core_data.Top_ID) {
  79. log.Debugf("We need to resync with the peer")
  80. // set mode to syncronising
  81. Send_BC_Notify_Chain_Command(connection)
  82. }
  83. }
  84. /* we will never send this request, so we donot need to parse response
  85. func Send_P2P_Timed_Sync(connection *Connection){
  86. connection.Lock()
  87. var o_command_header Levin_Header
  88. var o_data_header Levin_Data_Header
  89. o_data_bytes,_ := o_data_header.Serialize()
  90. o_command_header.CB = uint64(len(o_data_bytes))
  91. o_command_header.Command = P2P_COMMAND_REQUEST_SUPPORT_FLAGS
  92. o_command_header.ReturnData = true
  93. o_command_header.Flags = LEVIN_PACKET_REQUEST
  94. o_command_header_bytes,_ := o_command_header.Serialize()
  95. connection.Conn.Write(o_command_header_bytes)
  96. connection.Conn.Write(o_data_bytes)
  97. connection.Command_queue.PushBack(uint32(P2P_COMMAND_REQUEST_SUPPORT_FLAGS))
  98. connection.Unlock()
  99. }
  100. */