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.

176 lines
7.0 KiB

  1. package p2p
  2. import "bytes"
  3. import "encoding/binary"
  4. import "github.com/romana/rlog"
  5. // this file sends a timed sync packet and parses response
  6. /* below is a timed sync request
  7. 000001DE 01 21 01 01 01 01 01 01 87 00 00 00 00 00 00 00 .!...... ........
  8. 000001EE 01 ea 03 00 00 00 00 00 00 01 00 00 00 01 00 00 ........ ........
  9. 000001FE 00 .
  10. 000001FF 01 11 01 01 01 01 02 01 01 04 0c 70 61 79 6c 6f ........ ...paylo
  11. 0000020F 61 64 5f 64 61 74 61 0c 10 15 63 75 6d 75 6c 61 ad_data. ..cumula
  12. 0000021F 74 69 76 65 5f 64 69 66 66 69 63 75 6c 74 79 05 tive_dif ficulty.
  13. 0000022F 8e 4a f1 09 da 00 00 00 0e 63 75 72 72 65 6e 74 .J...... .current
  14. 0000023F 5f 68 65 69 67 68 74 05 53 4b 00 00 00 00 00 00 _height. SK......
  15. 0000024F 06 74 6f 70 5f 69 64 0a 80 1d ca fd 23 08 50 54 .top_id. ....#.PT
  16. 0000025F 16 e0 41 c8 33 e6 db 91 ca 33 6b b1 fb af 9d a1 ..A.3... .3k.....
  17. 0000026F 99 f2 77 85 5d a1 e8 2a fa 0b 74 6f 70 5f 76 65 ..w.]..* ..top_ve
  18. 0000027F 72 73 69 6f 6e 08 06 rsion..
  19. 00000000 01 21 01 01 01 01 01 01 87 00 00 00 00 00 00 00 .!...... ........
  20. 00000010 01 ea 03 00 00 00 00 00 00 01 00 00 00 01 00 00 ........ ........
  21. 00000020 00 .
  22. 00000021 01 11 01 01 01 01 02 01 01 04 0c 70 61 79 6c 6f ........ ...paylo
  23. 00000031 61 64 5f 64 61 74 61 0c 10 15 63 75 6d 75 6c 61 ad_data. ..cumula
  24. 00000041 74 69 76 65 5f 64 69 66 66 69 63 75 6c 74 79 05 tive_dif ficulty.
  25. 00000051 2e 9d 54 af fd 00 00 00 0e 63 75 72 72 65 6e 74 ..T..... .current
  26. 00000061 5f 68 65 69 67 68 74 05 b6 4c 00 00 00 00 00 00 _height. .L......
  27. 00000071 06 74 6f 70 5f 69 64 0a 80 64 52 25 11 88 5a ca .top_id. .dR%..Z.
  28. 00000081 10 c8 f9 28 c5 ad a4 03 e1 4f 6d 68 23 8c c5 ea ...(.... .Omh#...
  29. 00000091 73 e6 16 58 37 d1 96 22 07 0b 74 6f 70 5f 76 65 s..X7.." ..top_ve
  30. 000000A1 72 73 69 6f 6e 08 06
  31. 01 21 01 01 01 01 01 01 87 00 00 00 00 00 00 00
  32. 01 ea 03 00 00 00 00 00 00 01 00 00 00 01 00 00
  33. 00
  34. 01 11 01 01 01 01 02 01 01 04 0c 70 61 79 6c 6f
  35. 61 64 5f 64 61 74 61 0c 10 15 63 75 6d 75 6c 61
  36. 74 69 76 65 5f 64 69 66 66 69 63 75 6c 74 79 05
  37. 2e 9d 54 af fd 00 00 00 0e 63 75 72 72 65 6e 74
  38. 5f 68 65 69 67 68 74 b6 4c00000000000000
  39. 06 74 6f 70 64 52 25 11 88 5a ca 10 c8 f9 28 c5
  40. ada403e14f6d68238cc5ea73e6165837
  41. d19622075da1e82afa0b746f705f7665
  42. 72 73 69 6f 6e 08 06
  43. 01210101010101018700000000000000
  44. 01ea0300000000000001000000010000
  45. 00
  46. 011101010101020101040c7061796c6f
  47. 61645f646174610c101563756d756c61
  48. 746976655f646966666963756c747905
  49. 2e9d54affd0000000e63757272656e74
  50. 5f 68 65 69 67 68 74 05 b6 4c 00 00 00 00 00 00
  51. 06 74 6f 70 5f 69 64 0a 80 1d ca fd 23 08 50 64
  52. 522511885aca10c8f928c5ada403e14f
  53. 6d68238cc5ea73e6165837d196220765
  54. 7273696f6e0806
  55. */
  56. func Send_Timed_Sync(connection *Connection) {
  57. request_packet:=[]byte { 0x01, 0x21, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
  58. 0x87, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  59. 0x01, 0xea, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00,
  60. 0x00, 0x01, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00,
  61. 0x00,
  62. 0x01, 0x11, 0x01, 0x01, 0x01, 0x01, 0x02, 0x01,
  63. 0x01, 0x04, 0x0c, 0x70, 0x61, 0x79, 0x6c, 0x6f,
  64. 0x61, 0x64, 0x5f, 0x64, 0x61, 0x74, 0x61, 0x0c,
  65. 0x10, 0x15, 0x63, 0x75, 0x6d, 0x75, 0x6c, 0x61,
  66. 0x74, 0x69, 0x76, 0x65, 0x5f, 0x64, 0x69, 0x66,
  67. 0x66, 0x69, 0x63, 0x75, 0x6c, 0x74, 0x79, 0x05,
  68. 0x8e, 0x4a, 0xf1, 0x09, 0xda, 0x00, 0x00, 0x00,
  69. 0x0e, 0x63, 0x75, 0x72, 0x72, 0x65, 0x6e, 0x74,
  70. 0x5f, 0x68, 0x65, 0x69, 0x67, 0x68, 0x74, 0x05,
  71. 0x53, 0x4b, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  72. 0x06, 0x74, 0x6f, 0x70, 0x5f, 0x69, 0x64, 0x0a,
  73. 0x80, 0x1d, 0xca, 0xfd, 0x23, 0x08, 0x50, 0x54,
  74. 0x16, 0xe0, 0x41, 0xc8, 0x33, 0xe6, 0xdb, 0x91,
  75. 0xca, 0x33, 0x6b, 0xb1, 0xfb, 0xaf, 0x9d, 0xa1,
  76. 0x99, 0xf2, 0x77, 0x85, 0x5d, 0xa1, 0xe8, 0x2a,
  77. 0xfa, 0x0b, 0x74, 0x6f, 0x70, 0x5f, 0x76, 0x65,
  78. 0x72, 0x73, 0x69, 0x6f, 0x6e, 0x08, 0x06, };
  79. // write our data into the packet
  80. top_id := chain.Get_Top_ID()
  81. cumulative_diff := chain.Load_Block_Cumulative_Difficulty(top_id)
  82. height := chain.Get_Height()
  83. top_version := byte(6)
  84. // now lets write that into the buffer
  85. binary.LittleEndian.PutUint64(request_packet[81:],cumulative_diff)
  86. binary.LittleEndian.PutUint64(request_packet[105:],height)
  87. copy(request_packet[122:],top_id[:])
  88. request_packet[len(request_packet)-1]= top_version
  89. //connection.logger.Infof("sync request packet %x top_id %x", request_packet, top_id)
  90. connection.Lock()
  91. defer connection.Unlock()
  92. connection.Conn.Write(request_packet)
  93. connection.Command_queue.PushBack(uint32(P2P_COMMAND_TIMED_SYNC))
  94. }
  95. // handle response of above command
  96. // response comes as below
  97. /*
  98. 00000000 01 21 01 01 01 01 01 01 9b 00 00 00 00 00 00 00 .!...... ........
  99. 00000010 00 ea 03 00 00 00 00 00 00 02 00 00 00 01 00 00 ........ ........
  100. 00000020 00 .
  101. 00000021 0c 70 61 79 6c 6f 61 64 5f 64 61 74 61 0c 10 15 .payload _data...
  102. 00000031 63 75 6d 75 6c 61 74 69 76 65 5f 64 69 66 66 69 cumulati ve_diffi
  103. 00000041 63 75 6c 74 79 05 8e 4a f1 09 da 00 00 00 0e 63 culty..J .......c
  104. 00000051 75 72 72 65 6e 74 5f 68 65 69 67 68 74 05 52 4b urrent_h eight.RK
  105. 00000061 00 00 00 00 00 00 06 74 6f 70 5f 69 64 0a 80 1d .......t op_id...
  106. 00000071 ca fd 23 08 50 54 16 e0 41 c8 33 e6 db 91 ca 33 ..#.PT.. A.3....3
  107. 00000081 6b b1 fb af 9d a1 99 f2 77 85 5d a1 e8 2a fa 0b k....... w.]..*..
  108. 00000091 74 6f 70 5f 76 65 72 73 69 6f 6e 08 06 01 11 01 top_vers ion.....
  109. 000000A1 01 01 01 02 01 01 08 0a 6c 6f 63 61 6c 5f 74 69 ........ local_ti
  110. 000000B1 6d 65 05 df 3d 5b 5a 00 00 00 00 me..=[Z. ...
  111. */
  112. func Handle_P2P_Timed_Sync_Response(connection *Connection,
  113. i_command_header *Levin_Header, buf []byte) {
  114. var i_data_header Levin_Data_Header // incoming data header, deserialize it
  115. var peer_core_data CORE_DATA
  116. err := i_data_header.DeSerialize(buf)
  117. if err != nil {
  118. connection.logger.Debugf("Invalid Levin Data header, disconnecting peer")
  119. connection.Exit = true
  120. return
  121. }
  122. // parse incoming core data
  123. pos := bytes.Index(i_data_header.Data, []byte("payload_data")) // at this point to node data and should be parsed as such
  124. if pos < 0 {
  125. connection.logger.Debugf("Invalid P2P_COMMAND_TIMED_SYNC_T, could not find payload_data, disconnecting peer")
  126. connection.Exit = true
  127. return
  128. }
  129. err = peer_core_data.DeSerialize(i_data_header.Data[pos-1:])
  130. if err != nil {
  131. connection.logger.Debugf("Invalid P2P_COMMAND_TIMED_SYNC_T, could not deserialize core_data, disconnecting peer")
  132. connection.Exit = true
  133. return
  134. }
  135. rlog.Trace(5, "Incoming core data %+v \n", peer_core_data)
  136. // lets check whether we need to resync with this peer
  137. if chain.IsLagging(peer_core_data.Cumulative_Difficulty, peer_core_data.Current_Height, peer_core_data.Top_ID) {
  138. connection.logger.Debugf("We need to resync with the peer")
  139. // set mode to syncronising
  140. Send_BC_Notify_Chain_Command(connection)
  141. }
  142. }