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.

190 lines
6.0 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 p2p
  17. import "github.com/romana/rlog"
  18. import "github.com/arnaucode/derosuite/globals"
  19. import "github.com/arnaucode/derosuite/crypto"
  20. // when 2 peers communiate either both are in sync or async
  21. // if async, reply to the request below, with your state and other will supply you list of block ids
  22. // handle BC_NOTIFY_REQUEST_CHAIN
  23. func Handle_BC_Notify_Chain(connection *Connection,
  24. i_command_header *Levin_Header, buf []byte) {
  25. // deserialize data header
  26. var i_data_header Levin_Data_Header // incoming data header
  27. err := i_data_header.DeSerialize(buf)
  28. if err != nil {
  29. connection.logger.Debugf("We should destroy connection here, data header cnot deserialized")
  30. connection.Exit = true
  31. return
  32. }
  33. buf = i_data_header.Data[11:] // 11 bytes boost header, ignore it
  34. // decode remain data length ( though we know it from buffer size, but still verify it )
  35. data_length, done := Decode_Boost_Varint(buf)
  36. buf = buf[done:]
  37. if data_length == 0 {
  38. rlog.Tracef(4, "Peer says it does not have even genesis block, so disconnect")
  39. connection.Exit = true
  40. return
  41. }
  42. if (data_length % 32) != 0 { // sanity check
  43. rlog.Tracef(4, "We should destroy connection here, packet mismatch")
  44. connection.Exit = true
  45. return
  46. }
  47. rlog.Tracef(4, "Number of hashes %d \n", data_length/32)
  48. var block_list []crypto.Hash
  49. for i := uint64(0); i < data_length/32; i++ {
  50. var bhash crypto.Hash
  51. copy(bhash[:], buf[i*32:(i+1)*32])
  52. block_list = append(block_list, bhash)
  53. rlog.Tracef(5, "%2d hash %x\n", i, bhash[:])
  54. }
  55. // the data is like this, first 10 blocks, then block are in 2^n power and the last block is genesis
  56. // make sure the genesis block is same
  57. if block_list[len(block_list)-1] != globals.Config.Genesis_Block_Hash {
  58. connection.logger.Debugf("Peer's genesis block is different from our, so disconnect")
  59. connection.Exit = true
  60. return
  61. }
  62. // we must give user our version of the chain
  63. start_height := uint64(0)
  64. for i := 0; i < len(block_list); i++ { // find the common point in our chain
  65. if chain.Block_Exists(block_list[i]) {
  66. start_height = chain.Load_Height_for_BL_ID(block_list[i])
  67. rlog.Tracef(4, "Found common point in chain at hash %x\n", block_list[i])
  68. break
  69. }
  70. }
  71. // send atleast 16001 block or till the top
  72. stop_height := chain.Get_Height()
  73. if (stop_height - start_height) > 1001 { // send MAX 512 KB block hashes
  74. stop_height = start_height + 1002
  75. }
  76. block_list = block_list[:0]
  77. for i := start_height; i < stop_height; i++ {
  78. hash, _ := chain.Load_BL_ID_at_Height(i)
  79. block_list = append(block_list, hash)
  80. }
  81. rlog.Tracef(2, "Prepared list of %d block header to send \n", len(block_list))
  82. Send_BC_Notify_Response_Chain_Entry(connection, block_list, start_height, chain.Get_Height(), 1)
  83. }
  84. // header from boost packet
  85. //0060 00 00 00 01 11 01 01 01 01 02 01 [] 01 04 09 62 6c ..............bl
  86. //0070 6f 63 6b 5f 69 64 73 0a [] 81 0a 9b a2 3e fe 50 5f ock_ids.....>.P_
  87. // send the Peer our chain status, so he can give us the latest chain or updated block_ids
  88. // this is only sent when we are in different from peers
  89. func Send_BC_Notify_Chain_Command(connection *Connection) {
  90. connection.Lock()
  91. header_bytes := []byte{ /*0x01, 0x04,*/ 0x09, 0x62, 0x6c, 0x6f, 0x63, 0x6b, 0x5f, 0x69, 0x64, 0x73, 0x0a}
  92. _ = header_bytes
  93. // now append boost variant length, and then append all the hashes
  94. var block_list []crypto.Hash
  95. // add your blocks here
  96. our_height := chain.Get_Height()
  97. if our_height < 20 { // if height is less than 20, fetch from genesis block
  98. } else { // send blocks in reverse
  99. for i := uint64(1); our_height < 11; i++ {
  100. hash, err := chain.Load_BL_ID_at_Height(our_height - i)
  101. _ = err
  102. block_list = append(block_list, hash)
  103. }
  104. our_height = our_height - 11
  105. // now seend all our block id in log, 2nd, 4th, 8th, etc
  106. for ; our_height > 0; our_height = our_height >> 1 {
  107. hash, err := chain.Load_BL_ID_at_Height(our_height)
  108. _ = err
  109. block_list = append(block_list, hash)
  110. }
  111. }
  112. // final block is always genesis block
  113. block_list = append(block_list, globals.Config.Genesis_Block_Hash)
  114. buf := make([]byte, 8, 8)
  115. done := Encode_Boost_Varint(buf, uint64(len(block_list)*32)) // encode length of buffer
  116. buf = buf[:done]
  117. var o_command_header Levin_Header
  118. var o_data_header Levin_Data_Header
  119. o_data_header.Data = append(header_bytes, buf...)
  120. // convert and append all hashes to bytes
  121. for _, hash := range block_list {
  122. o_data_header.Data = append(o_data_header.Data, hash[:32]...)
  123. }
  124. o_data_bytes, _ := o_data_header.Serialize()
  125. o_data_bytes[9] = 0x4
  126. o_command_header.CB = uint64(len(o_data_bytes))
  127. o_command_header.Command = BC_NOTIFY_REQUEST_CHAIN
  128. o_command_header.ReturnData = false
  129. o_command_header.Flags = LEVIN_PACKET_REQUEST
  130. o_command_header_bytes, _ := o_command_header.Serialize()
  131. connection.Conn.Write(o_command_header_bytes)
  132. connection.Conn.Write(o_data_bytes)
  133. //fmt.Printf("len of command header %d\n", len(o_command_header_bytes))
  134. //fmt.Printf("len of data header %d\n", len(o_data_bytes))
  135. connection.Unlock()
  136. }