def test_parse_fails_with_extra_bytes(self): with self.assertRaises(ParseError): parts = splitn(TX_BYTES, struct.calcsize('<4s12s')) isize = struct.calcsize('<I') newsize = struct.unpack('<I', parts[1][:isize])[0] + 1 (checksum, payload) = splitn(parts[1][isize:], 4) payload += '\x42' checksum = protocol.Message.calc_checksum(payload) bytes = parts[0] + struct.pack('<I', newsize) + checksum + payload (msg, bytes) = protocol.Message.parse(bytes)
def parse(cls, bytes, header=None): if header is None: (header, bytes) = MessageHeader.parse(bytes) log.debug('Parsing message %s' % (header.command,)) (payload, bytes) = splitn(bytes, header.payload_length) if header.checksum != cls.calc_checksum(payload): raise ParseError('Checksum is incorrect') if header.command not in COMMAND_CLASS_MAP: raise ParseError('Unknown command %s' % (header.command,)) (msg, empty) = COMMAND_CLASS_MAP[header.command].parse(payload, header) if empty != '': raise ParseError('Trailing bytes after parsing payload for command %s' % (header.command,)) return (msg, bytes)
def parse(cls, bytes, header=None): if header is None: (header, bytes) = MessageHeader.parse(bytes) log.debug('Parsing message %s' % (header.command, )) (payload, bytes) = splitn(bytes, header.payload_length) if header.checksum != cls.calc_checksum(payload): raise ParseError('Checksum is incorrect') if header.command not in COMMAND_CLASS_MAP: raise ParseError('Unknown command %s' % (header.command, )) (msg, empty) = COMMAND_CLASS_MAP[header.command].parse(payload, header) if empty != '': raise ParseError( 'Trailing bytes after parsing payload for command %s' % (header.command, )) return (msg, bytes)
def test_splitn_gt_len(self): with self.assertRaises(ParseError): (a, b) = splitn('12', 3)
def test_splitn_len(self): (a, b) = splitn('12', 2) self.assertEqual(a, '12') self.assertEqual(b, '')
def test_splitn(self): (a, b) = splitn('abcde', 2) self.assertEqual(len(a), 2) self.assertEqual(len(b), 3) self.assertEqual(a, 'ab') self.assertEqual(b, 'cde')
def test_parse_fails_with_bad_checksum(self): with self.assertRaises(ParseError): parts = splitn(TX_BYTES, struct.calcsize('<4s12sI')) bytes = parts[0] + '\x98' + parts[1][1:] (msg, bytes) = protocol.Message.parse(bytes)
def parse_varstr(bytes): (str_len, bytes) = parse_varint(bytes) return splitn(bytes, str_len)
def parse(cls, bytes): ((value, ), bytes) = parse(bytes, INT64_FMT) (script_len, bytes) = parse_varint(bytes) (pk_script, bytes) = splitn(bytes, script_len) return (cls(value, PubKeyScript(pk_script)), bytes)
def parse(cls, bytes): (previous_output, bytes) = parse(bytes, cls.OUTPOINT_FMT) (script_len, bytes) = parse_varint(bytes) (signature_script, bytes) = splitn(bytes, script_len) ((sequence, ), bytes) = parse(bytes, UINT32_FMT) return (cls(previous_output, signature_script, sequence), bytes)
def parse(cls, bytes): ((value,), bytes) = parse(bytes, INT64_FMT) (script_len, bytes) = parse_varint(bytes) (pk_script, bytes) = splitn(bytes, script_len) return (cls(value, PubKeyScript(pk_script)), bytes)
def parse(cls, bytes): (previous_output, bytes) = parse(bytes, cls.OUTPOINT_FMT) (script_len, bytes) = parse_varint(bytes) (signature_script, bytes) = splitn(bytes, script_len) ((sequence,), bytes) = parse(bytes, UINT32_FMT) return (cls(previous_output, signature_script, sequence), bytes)