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.

91 lines
2.4 KiB

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