github.com/deroproject/derosuite@v2.1.6-1.0.20200307070847-0f2e589c7a2b+incompatible/p2p/wire_structs.go (about)

     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  
    17  package p2p
    18  
    19  // This file defines the structure for the protocol which is a msgp encoded ( which is standard)
    20  // msgp  would cause an easy rewrite of p2p layer even in c, ruby or rust etc as future may demand
    21  // the protocol is length prefixed msgp payload
    22  // though we can  use http2 stream features,  they may become compilcated as the project evolves
    23  // the prefix length is 4 bytes, little endian encoded ( so a frame can be 4GB in size)
    24  // this is Work-In-Progress
    25  // the reason for writing it from scratch is the mess of boost serialisation
    26  // the p2p package is currently the most complex within the entire project
    27  
    28  // the protocol is partly syncronous, partly asyncronous , except for first handshake, so the node remain undetectable to external network scans, the detection cost is atleast send a handshake packet*/
    29  
    30  // these are the commands required to make it completely operational
    31  
    32  const V2_COMMAND_NULL = 0 // default value is zero  and is a null command
    33  //const V2_COMMAND_NULL = 0
    34  //first 40 are reserved for future use
    35  const V2_COMMAND_HANDSHAKE = 41 // commands are syncronous and must be responded within 10 secs
    36  const V2_COMMAND_SYNC = 42
    37  const V2_COMMAND_CHAIN_REQUEST = 43
    38  const V2_COMMAND_CHAIN_RESPONSE = 44
    39  const V2_COMMAND_OBJECTS_REQUEST = 45
    40  const V2_COMMAND_OBJECTS_RESPONSE = 46
    41  
    42  const V2_NOTIFY_NEW_BLOCK = 0xff // Notifications are asyncronous all notifications come here, such as new block, new txs
    43  const V2_NOTIFY_NEW_TX = 0xfe    // notify tx using this
    44  
    45  // used to parse incoming packet for for command , so as a repective command command could be triggered
    46  type Common_Struct struct {
    47  	Height                int64  `msgpack:"HEIGHT"`
    48  	TopoHeight            int64  `msgpack:"THEIGHT"`
    49  	StableHeight          int64  `msgpack:"SHEIGHT"`
    50  	Cumulative_Difficulty string `msgpack:"CDIFF"`
    51  	//	Top_ID                [32]byte `msgpack:"TOP"` // 32 bytes of Top block
    52  	Top_Version uint64 `msgpack:"HF"` // this basically represents the hard fork version
    53  }
    54  
    55  const FLAG_LOWCPURAM string = "LOWCPURAM"
    56  
    57  // at start, client sends handshake and server will respond to handshake
    58  type Handshake_Struct struct {
    59  	Command         uint64        `msgpack:"COMMAND"`
    60  	Common          Common_Struct `msgpack:"COMMON"`   // add all fields of Common
    61  	ProtocolVersion string        `msgpack:"PVERSION"` // version is a sematic version string semver
    62  	Tag             string        `msgpack:"TAG"`      // user specific tag
    63  	DaemonVersion   string        `msgpack:"DVERSION"`
    64  	UTC_Time        int64         `msgpack:"UTC"`
    65  	Local_Port      uint32        `msgpack:"LP"`
    66  	Peer_ID         uint64        `msgpack:"PID"`
    67  	Network_ID      [16]byte      `msgpack:"NID"` // 16 bytes
    68  	Flags           []string      `msgpack:"FLAGS"`
    69  	PeerList        []Peer_Info   `msgpack:"PLIST"`
    70  	Extension_List  []string      `msgpack:"EXT"`
    71  	Request         bool          `msgpack:"REQUEST"` //whether this is a request
    72  }
    73  
    74  type Peer_Info struct {
    75  	Addr  string `msgpack:"ADDR"` // ip:port pair
    76  	Miner bool   `msgpack:"MINER"`
    77  	//ID       uint64 `msgpack:"I"`
    78  	//LastSeen uint64 `msgpack:"LS"`
    79  }
    80  
    81  type Sync_Struct struct { // sync packets are sent every 2 seconds
    82  	Command  uint64        `msgpack:"COMMAND"`
    83  	Common   Common_Struct `msgpack:"COMMON"`  // add all fields of Common
    84  	PeerList []Peer_Info   `msgpack:"PLIST"`   // update peer list
    85  	Request  bool          `msgpack:"REQUEST"` //whether this is a request
    86  }
    87  
    88  type Chain_Request_Struct struct { // our version of chain
    89  	Command     uint64        `msgpack:"COMMAND"`
    90  	Common      Common_Struct `msgpack:"COMMON"` // add all fields of Common
    91  	Block_list  [][32]byte    `msgpack:"BLIST"`  // block list
    92  	TopoHeights []int64       `msgpack:"TOPO"`   // topo heights of added blocks
    93  }
    94  
    95  type Chain_Response_Struct struct { // peers gives us point where to get the chain
    96  	Command      uint64        `msgpack:"COMMAND"`
    97  	Common       Common_Struct `msgpack:"COMMON"` // add all fields of Common
    98  	Start_height int64         `msgpack:"SH"`
    99  	Start_topoheight int64     `msgpack:"STH"`
   100  	Block_list   [][32]byte    `msgpack:"BLIST"`
   101  	TopBlocks    [][32]byte    `msgpack:"TOPBLOCKS"` // top blocks used for faster syncronisation of alt-tips
   102  	// this contains all blocks hashes for the last 10 heights, heightwise ordered
   103  
   104  }
   105  
   106  type Object_Request_Struct struct {
   107  	Command    uint64        `msgpack:"COMMAND"`
   108  	Common     Common_Struct `msgpack:"COMMON"` // add all fields of Common
   109  	Block_list [][32]byte    `msgpack:"BLIST"`
   110  	Tx_list    [][32]byte    `msgpack:"TXLIST"`
   111  }
   112  
   113  type Object_Response_struct struct {
   114  	Command uint64           `msgpack:"COMMAND"`
   115  	Common  Common_Struct    `msgpack:"COMMON"` // add all fields of Common
   116  	CBlocks []Complete_Block `msgpack:"CBLOCKS"`
   117  	Txs     [][]byte         `msgpack:"TXS"`
   118  }
   119  
   120  type Complete_Block struct {
   121  	Block []byte   `msgpack:"BLOCK"`
   122  	Txs   [][]byte `msgpack:"TXS"`
   123  }
   124  
   125  type Notify_New_Objects_Struct struct {
   126  	Command uint64         `msgpack:"COMMAND"`
   127  	Common  Common_Struct  `msgpack:"COMMON"` // add all fields of Common
   128  	CBlock  Complete_Block `msgpack:"CBLOCK"`
   129  	Tx      []byte         `msgpack:"TX"`
   130  }
   131  
   132  // each packet has to be parsed twice once for extracting command and then a full parsing based on Command