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.

107 lines
3.6 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 "bytes"
  18. import "github.com/romana/rlog"
  19. //import "github.com/arnaucode/derosuite/blockchain"
  20. import "github.com/arnaucode/derosuite/transaction"
  21. // if the incoming blob contains block with included transactions
  22. //00009F94 01 11 01 01 01 01 02 01 01 08 06 62 6c 6f 63 6b ........ ...block
  23. //00009FA4 73 8c 04 08 05 62 6c 6f 63 6b 0a fd 03 06 06 cd s....blo ck......
  24. // if the incoming blob contains block without any tx
  25. //00009EB4 01 11 01 01 01 01 02 01 01 08 06 62 6c 6f 63 6b ........ ...block
  26. //00009EC4 73 8c 08 04 05 62 6c 6f 63 6b 0a e5 01 01 00 00 s....blo ck......
  27. // if the incoming blob only contains a TX
  28. // FIXME this code can also be shared by NOTIFY_NEW_BLOCK, NOTIFY_NEW_TRANSACTIONS
  29. // we trigger this if we want to request any TX or block from the peer
  30. func Handle_BC_Notify_New_Transactions(connection *Connection,
  31. i_command_header *Levin_Header, buf []byte) {
  32. // deserialize data header
  33. var i_data_header Levin_Data_Header // incoming data header
  34. err := i_data_header.DeSerialize(buf)
  35. if err != nil {
  36. connection.logger.Debugf("We should destroy connection here, data header cnot deserialized")
  37. connection.Exit = true
  38. return
  39. }
  40. connection.logger.Debugf("Incoming NOTIFY_NEW_TRANSACTIONS")
  41. // check whether the response contains block
  42. pos := bytes.Index(i_data_header.Data, []byte("blocks")) // at this point to
  43. buf = i_data_header.Data
  44. pos = bytes.Index(buf, []byte("\x03txs\x8a")) // at this point to
  45. if pos > -1 {
  46. rlog.Tracef(3, "txt pos %d", pos)
  47. buf = buf[pos+5:]
  48. // decode remain data length ( though we know it from buffer size, but still verify it )
  49. tx_count, done := Decode_Boost_Varint(buf)
  50. buf = buf[done:]
  51. for i := uint64(0); i < tx_count; i++ {
  52. var tx transaction.Transaction
  53. tx_len, done := Decode_Boost_Varint(buf)
  54. buf = buf[done:]
  55. rlog.Tracef(3, "tx count %d i %d tx_len %d\n", tx_count, i, tx_len)
  56. tx_bytes := buf[:tx_len]
  57. // deserialize and verrify transaction
  58. err = tx.DeserializeHeader(tx_bytes)
  59. if err != nil {
  60. connection.logger.Warnf("Transaction could not be deserialized\n") // we should disconnect peer
  61. } else {
  62. hash := tx.GetHash()
  63. rlog.Tracef(2, "Transaction deserialised successfully hash %x\n", hash[:32])
  64. // add tx to mem pool, we must verify that the tx is valid at this point in time
  65. // we should check , whether we shoul add the tx to pool
  66. //chain.Add_TX(&tx)
  67. // we should add TX to pool
  68. // TODO check return status, then either replay tx,ignore tx or discard the connection
  69. chain.Add_TX_To_Pool(&tx)
  70. }
  71. buf = buf[tx_len:] // setup for next tx
  72. }
  73. }
  74. }