def deserialize(self, data, cursor=0): result, cursor = self.MESSAGE_HEADER.deserialize(data, cursor) magic, command, length = result pos = string.find(command, "\0") if (pos != -1): command = command[0:pos] if (command not in COMMANDS_TYPES): raise FormatErrorException("Error: unknown command : %s" % (command)) if ( ENCODERS[COMMANDS_TYPES[command]] == None): raise FormatErrorException("Error: Unsupported command : %s" % (command)) msg_type = COMMANDS_TYPES[command] #if (msg_type != MSG_VERSION and msg_type != MSG_VERACK): checksum = data[cursor:cursor+4] cursor += 4 startplayload = cursor #raise FormatErrorException("Checksum error in command: %s %s != %s" % (command, hexdump1(checksum,""), hexdump1(verify,""))) if (magic != MAGICS[self.runmode]): raise FormatErrorException("Error: wrong magic : expected:%s received:%s" % (MAGICS[self.runmode], magic)) if (len(data) - cursor < length): raise MissingDataException("Command incomplete: %s" % (command)) #self.log.debug("Decoding: %s" % (command)) res, cursor = ENCODERS[COMMANDS_TYPES[command]].deserialize(data, cursor) #verify checksum after decoding (required to identify message boundaries) #if (msg_type != MSG_VERSION and msg_type != MSG_VERACK): verify = sha256checksum(data[startplayload:cursor]) if (checksum != verify): #raise FormatErrorException("Checksum error in command: %s %s != %s" % (command, hexdump1(checksum,""), hexdump1(verify,""))) raise FormatErrorException( "Checksum error in command: %s %s != %s" % (command, hexstr(checksum), hexstr(verify))) return (res, cursor)
def serialize(self, msg): if (msg.type not in ENCODERS): raise FormatErrorException("Encoder not found for type: %d" % (msg.type)) payload = ENCODERS[msg.type].serialize(msg) result = self.MESSAGE_HEADER.serialize([MAGICS[self.runmode], COMMANDS[msg.type], len(payload)]) result += sha256checksum(payload) result += payload return (result)
def serialize(self, msg): if (msg.type not in ENCODERS): raise FormatErrorException("Encoder not found for type: %d" % (msg.type)) payload = ENCODERS[msg.type].serialize(msg) result = self.MESSAGE_HEADER.serialize( [MAGICS[self.runmode], COMMANDS[msg.type], len(payload)]) result += sha256checksum(payload) result += payload return (result)
def deserialize(self, data, cursor=0): result, cursor = self.MESSAGE_HEADER.deserialize(data, cursor) magic, command, length = result pos = string.find(command, "\0") if (pos != -1): command = command[0:pos] if (command not in COMMANDS_TYPES): raise FormatErrorException("Error: unknown command : %s" % (command)) if (ENCODERS[COMMANDS_TYPES[command]] == None): raise FormatErrorException("Error: Unsupported command : %s" % (command)) msg_type = COMMANDS_TYPES[command] #if (msg_type != MSG_VERSION and msg_type != MSG_VERACK): checksum = data[cursor:cursor + 4] cursor += 4 startplayload = cursor #raise FormatErrorException("Checksum error in command: %s %s != %s" % (command, hexdump1(checksum,""), hexdump1(verify,""))) if (magic != MAGICS[self.runmode]): raise FormatErrorException( "Error: wrong magic : expected:%s received:%s" % (MAGICS[self.runmode], magic)) if (len(data) - cursor < length): raise MissingDataException("Command incomplete: %s" % (command)) #self.log.debug("Decoding: %s" % (command)) res, cursor = ENCODERS[COMMANDS_TYPES[command]].deserialize( data, cursor) #verify checksum after decoding (required to identify message boundaries) #if (msg_type != MSG_VERSION and msg_type != MSG_VERACK): verify = sha256checksum(data[startplayload:cursor]) if (checksum != verify): #raise FormatErrorException("Checksum error in command: %s %s != %s" % (command, hexdump1(checksum,""), hexdump1(verify,""))) raise FormatErrorException( "Checksum error in command: %s %s != %s" % (command, hexstr(checksum), hexstr(verify))) return (res, cursor)