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.

82 lines
2.1 KiB

  1. package p2p
  2. // This file defines the structure for the protocol which is a msgp encoded ( which is standard)
  3. // msgp would cause an easy rewrite of p2p layer even in c, ruby or rust etc as future may demand
  4. // the protocol is length prefixed msgp payload
  5. // though we can http2 stream features, they may become compilcated as the project evolve
  6. // the prefix length is 4 bytes, little endian encoded ( so a frame can be 4GB in size)
  7. //import "net"
  8. // the protocol is completely asyncronous, except for first handshake, so the node remain undetectable to external network scans, the detection cost is to atleast send a handshake packet*/
  9. // used to parse incoming packet for for command , so as a repective command command could be triggered
  10. type Common struct {
  11. Command uint64
  12. Height uint64
  13. Cumulative_Difficulty uint64
  14. Top_ID [32]byte // 32 bytes of Top block
  15. Top_Version uint64 // this basically represents the hard fork version
  16. }
  17. // at start, client sends handshake and server will respond to handshake
  18. type Handshake struct {
  19. Common // add all fields of Common
  20. Local_Time uint64
  21. Local_Port uint32
  22. ID uint64
  23. Network_ID []byte // 16 bytes
  24. PeerList []Peer_Info
  25. Extension_List []string
  26. }
  27. type Sync struct {
  28. Common // add all fields of common
  29. }
  30. const V2_COMMAND_HANDSHAKE=1 // commands are syncronous and must be responded within 10 secs
  31. const V2_COMMAND_SYNC=2
  32. const V2_COMMAND_CHAIN_REQUEST=3
  33. const V2_COMMAND_CHAIN_RESPONSE=4
  34. const V2_COMMAND_OBJECTS_REQUEST=5
  35. const V2_COMMAND_OBJECTS_RESPONSE=6
  36. const V2_NOTIFY_NEW_OBJECTS=0x80000001 // all notifications come here, such as new block, new txs
  37. type Chain_Request struct {
  38. Block_list [][32]byte
  39. }
  40. type Chain_Response struct {
  41. Block_list [][32]byte
  42. }
  43. type Object_Request struct {
  44. Block_list [][32]byte
  45. Tx_list [][32]byte
  46. }
  47. type Complete_Block struct {
  48. Block []byte
  49. Tx [][]byte
  50. }
  51. type Object_Response struct {
  52. Blocks []Complete_Block
  53. Tx_list [][32]byte
  54. }
  55. type Notify_New_Objects struct {
  56. Block []byte
  57. Tx_list [][]byte
  58. }
  59. // each packet has to be parsed twice
  60. // the following