Skip to content
This repository has been archived by the owner on Nov 15, 2021. It is now read-only.

Commit

Permalink
Merge pull request #148 from ixje/document_neo_network
Browse files Browse the repository at this point in the history
Document /neo/Network/ & /neo/Network/Payload/
  • Loading branch information
localhuman committed Jan 3, 2018
2 parents 8412c0a + cb96835 commit fdc0e10
Show file tree
Hide file tree
Showing 12 changed files with 381 additions and 85 deletions.
15 changes: 12 additions & 3 deletions neo/Network/IPEndpoint.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,24 @@


class IPEndpoint():

ANY = '0.0.0.0'

Address = None
Port = None

def __init__(self, addr, port):
"""
Create instance.
Args:
addr (str):
port (int):
"""
self.Address = addr
self.Port = port

def ToAddress(self):
"""
Get the string representation of the endpoint.
Returns:
str: address:port
"""
return '%s:%s' % (self.Address, self.Port)
9 changes: 9 additions & 0 deletions neo/Network/Inventory.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,19 @@ class Inventory(object):
"""docstring for Inventory"""

def __init__(self):
"""
Create an instance
"""
super(Inventory, self).__init__()
self.hash = None

def GetHashData(self):
"""
Get the hashable data.
Returns:
bytes:
"""
ms = MemoryStream()
w = BinaryWriter(ms)
self.SerializeUnsigned(w)
Expand Down
10 changes: 8 additions & 2 deletions neo/Network/InventoryType.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,18 @@


class InventoryType(object):
TX = b'\x01' # Transaction
Block = b'\x02' # Block
TX = b'\x01' # Transaction
Block = b'\x02' # Block
Consensus = b'\xe0' # Consensus information

@staticmethod
def AllInventoriesInt():
"""
Get all inventory types as ints.
Returns:
list: of int formatted inventory types.
"""
return [int.from_bytes(InventoryType.TX, 'little'),
int.from_bytes(InventoryType.Block, 'little'),
int.from_bytes(InventoryType.Consensus, 'little')]
56 changes: 37 additions & 19 deletions neo/Network/Message.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,5 @@
import ctypes
import asyncio
import binascii
import pympler

from logzero import logger

from neo.IO.Mixins import SerializableMixin
from neo.IO.BinaryReader import BinaryReader
from neo.IO.BinaryWriter import BinaryWriter
from neo.IO.MemoryStream import MemoryStream, StreamManager
from neo.Settings import settings
from neo.Core.Helper import Helper
from neo.Cryptography.Helper import *
Expand All @@ -19,7 +10,6 @@ class ChecksumException(Exception):


class Message(SerializableMixin):

PayloadMaxSize = b'\x02000000'
PayloadMaxSizeInt = int.from_bytes(PayloadMaxSize, 'big')

Expand All @@ -34,7 +24,14 @@ class Message(SerializableMixin):
Length = 0

def __init__(self, command=None, payload=None, print_payload=False):

"""
Create an instance.
Args:
command (str): payload command i.e. "inv", "getdata". See NeoNode.MessageReceived() for more commands.
payload (bytes): raw bytes of the payload.
print_payload: UNUSED
"""
self.Command = command
self.Magic = settings.MAGIC

Expand All @@ -46,42 +43,63 @@ def __init__(self, command=None, payload=None, print_payload=False):
self.Checksum = Message.GetChecksum(payload)
self.Payload = payload

# if print_payload:
# logger.info("PAYLOAD: %s " % self.Payload)
# if print_payload:
# logger.info("PAYLOAD: %s " % self.Payload)

def Size(self):
return ctypes.sizeof(ctypes.c_uint) + 12 + ctypes.sizeof(ctypes.c_int) + ctypes.sizeof(ctypes.c_uint) + len(self.Payload)
"""
Get the total size in bytes of the object.
Returns:
int: size.
"""
return ctypes.sizeof(ctypes.c_uint) + 12 + ctypes.sizeof(ctypes.c_int) + ctypes.sizeof(ctypes.c_uint) + len(
self.Payload)

def Deserialize(self, reader):
self.Magic = reader.ReadUInt32()
"""
Deserialize full object.
Args:
reader (neo.IO.BinaryReader):
"""
self.Magic = reader.ReadUInt32()
self.Command = reader.ReadFixedString(12).decode('utf-8')

self.Length = reader.ReadUInt32()

if self.Length > self.PayloadMaxSizeInt:
raise Exception("invalid format- payload too large")

self.Checksum = reader.ReadUInt32()

self.Payload = reader.ReadBytes(self.Length)

checksum = Message.GetChecksum(self.Payload)

if checksum != self.Checksum:
raise ChecksumException("checksum mismatch")

# logger.info("Deserialized Message %s " % self.Command)

@staticmethod
def GetChecksum(value):
"""
Get the double SHA256 hash of the value.
Args:
value (obj): a payload
Returns:
"""
uint32 = bin_dbl_sha256(value)[:4]

return int.from_bytes(uint32, 'little')

def Serialize(self, writer):
"""
Serialize object.
Args:
writer (neo.IO.BinaryWriter):
"""
writer.WriteUInt32(self.Magic)
writer.WriteFixedString(self.Command, 12)
writer.WriteUInt32(len(self.Payload))
Expand Down
Loading

0 comments on commit fdc0e10

Please sign in to comment.