Ejemplo n.º 1
0
class BlockIndexSerializer():
    BLOCKINDEX = Structure([Field("<I", "version"),
                            Uint256Serializer("hash_next"),
                            Field("<I", "file"),
                            Field("<I", "blockpos"),
                            Field("<I", "height"),
                            BlockheaderSerializer()], "txindex")
    
    def __init__(self):
        pass
        
    def serialize(self, blockindex_obj):
        return (self.BLOCKINDEX.serialize([blockindex_obj.version,
                                           blockindex_obj.hash_next,
                                           blockindex_obj.file,
                                           blockindex_obj.blockpos,
                                           blockindex_obj.height,
                                           blockindex_obj.blockheader]))

    def deserialize(self, data, cursor=0):
        result, cursor = self.BLOCKINDEX.deserialize(data, cursor)
        (version, hash_next, file, blockpos, height, blockheader) = result
        return (DbBlockIndex(version, hash_next, file, blockpos, height, blockheader), cursor)

        
Ejemplo n.º 2
0
class MessageSerializer(Serializer):
    MESSAGE_HEADER = Structure(
        [Field("<I", "magic"),
         Field("12s", "command"),
         Field("<I", "length")], "message")

    def __init__(self, runmode):
        self.runmode = runmode

    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)
Ejemplo n.º 3
0
 def __init__(self, flags=0):
     self.BLOCKHEADER = Structure([
         Field("<I", "version"),
         Uint256Serializer("hash_prev"),
         Uint256Serializer("hash_merkle"),
         Field("<I", "time"),
         Field("<I", "bits"),
         Field("<I", "nonce")
     ], "txindex")
Ejemplo n.º 4
0
class DiskTxPosSerializer():
    DISKTXPOS = Structure([Field("<I", "file"),
                           Field("<I", "blockpos"),
                           Field("<I", "txpos")], "disktxpos")

    def serialize(self, disktxpos_obj):
        return (self.DISKTXPOS.serialize([disktxpos_obj.file,
                                          disktxpos_obj.blockpos,
                                          disktxpos_obj.txpos]))

    def deserialize(self, data, cursor=0):
        (file, nblockpos, ntxpos), cursor = self.DISKTXPOS.deserialize(data, cursor)
        return (DiskTxPos(file, nblockpos, ntxpos), cursor)
Ejemplo n.º 5
0
class TimenetaddrSerializer(Serializer):
    TIME_NETADDR = Structure([Field("<I", "timestamp"),
                              NetAddrSerializer("addr")], "timestamped_netaddr")

    def serialize(self, timenetaddr):
        return (self.TIME_NETADDR.serialize([timenetaddr.timestamp, timenetaddr.netaddr]))

    def deserialize(self, data, cursor=0):
        (timestamp, netaddr), cursor = self.TIME_NETADDR.deserialize(data, cursor)
        return (Timenetaddr(timestamp, netaddr), cursor)
Ejemplo n.º 6
0
class OutpointSerializer(Serializer):
    OUTPOINT = Structure([Uint256Serializer("hash"),
                          Field("<I","index")], "outpoint")

    def get_size(self, outpoint):
        return (self.OUTPOINT.get_size([outpoint.hash, outpoint.index]))
    
    def serialize(self, outpoint):
        return (self.OUTPOINT.serialize([outpoint.hash, outpoint.index]))

    def deserialize(self, data, cursor=0):
        (hash, index), cursor = self.OUTPOINT.deserialize(data, cursor)
        return (Outpoint(hash, index), cursor)
Ejemplo n.º 7
0
class VersionMessageSerializer(Serializer):
    VERSION_STRUCT = Structure([
        Field("<I", "version"),
        Field("<Q", "services"),
        Field("<Q", "timestamp"),
        NetAddrSerializer("addr_me"),
        NetAddrSerializer("addr_you"),
        Field("<Q", "nonce"),
        VarstrSerializer("sub_version_num"),
        Field("<I", "start_height")
    ], "version_message")

    def serialize(self, version_msg):
        return (self.VERSION_STRUCT.serialize([
            version_msg.version, version_msg.services, version_msg.timestamp,
            version_msg.addr_me, version_msg.addr_you, version_msg.nonce,
            version_msg.sub_version_num, version_msg.start_height
        ]))

    def deserialize(self, data, cursor=0):
        result, cursor = self.VERSION_STRUCT.deserialize(data, cursor)
        return (VersionMessage(*result), cursor)
Ejemplo n.º 8
0
class TxoutSerializer(Serializer):
    TXOUT = Structure(
        [Field("<q", "value"), VarstrScriptSerializer()], "outpoint")

    def get_size(self, txout):
        return (self.TXOUT.get_size([txout.value, txout.script]))

    def serialize(self, outpoint):
        return (self.TXOUT.serialize([outpoint.value, outpoint.script]))

    def deserialize(self, data, cursor=0):
        (value, script), cursor = self.TXOUT.deserialize(data, cursor)
        return (TxOut(value, script), cursor)
Ejemplo n.º 9
0
class MasterKeySerializer():
    MASTER_KEY = Structure([
        VarstrSerializer(),
        VarstrSerializer(),
        Field("<I"),
        Field("<I"),
        VarstrSerializer()
    ])

    def serialize(self, master_key):
        return (self.MASTER_KEY.serialize([
            master_key.crypted_key, master_key.salt,
            master_key.derivation_method, master_key.derive_iterations,
            master_key.other_derivation_parameters
        ]))

    def deserialize(self, data, cursor):
        master_key_fields, cursor = self.MASTER_KEY.deserialize(data, cursor)
        (crypted_key, salt, derivation_method, derive_iterations,
         other_derivation_parameters) = master_key_fields
        return (MasterKey(crypted_key, salt, derivation_method,
                          derive_iterations,
                          other_derivation_parameters), cursor)
Ejemplo n.º 10
0
class TxSerializer(Serializer):
    TX = Structure([
        Field("<I", "version"),
        VarsizelistSerializer(VarintSerializer("txin_count"),
                              TxinSerializer()),
        VarsizelistSerializer(VarintSerializer("txout_count"),
                              TxoutSerializer()),
        Field("<I", "lock_time")
    ], "tx")

    def get_size(self, tx):
        return (self.TX.get_size(
            [tx.version, tx.in_list, tx.out_list, tx.locktime]))

    def serialize(self, tx):
        return (self.TX.serialize(
            [tx.version, tx.in_list, tx.out_list, tx.locktime]))

    def deserialize(self, data, cursor=0):
        (version, in_list, out_list,
         locktime), new_cursor = self.TX.deserialize(data, cursor)
        tx = Tx(version, in_list, out_list, locktime)
        tx.rawdata = data[cursor:new_cursor]
        return (tx, new_cursor)
Ejemplo n.º 11
0
class InvitemSerializer(Serializer):
    INVITEM_ENC = Structure(
        [Field("<I", "type"), Uint256Serializer("hash")], "getblocks")

    def get_size(self, invitem):
        return (self.INVITEM_ENC.get_size(invitem.type, invitem.hash))

    def serialize(self, invitem):
        return (self.INVITEM_ENC.serialize([invitem.type, invitem.hash]))

    def deserialize(self, data, cursor=0):
        (type, hash), cursor = self.INVITEM_ENC.deserialize(data, cursor)
        if (type not in INV_ITEMS):
            raise FormatErrorException("Unknown inventory item")
        return (Invitem(type, hash), cursor)
Ejemplo n.º 12
0
class TxIndexSerializer():
    TXINDEX = Structure([
        Field("<I", "version"),
        DiskTxPosSerializer(),
        VarsizelistSerializer(VarintSerializer(), DiskTxPosSerializer())
    ], "txindex")

    def __init__(self):
        pass

    def serialize(self, txindex_obj):
        return (self.TXINDEX.serialize(
            [txindex_obj.version, txindex_obj.pos, txindex_obj.spent]))

    def deserialize(self, data, cursor=0):
        (version, pos, spent), cursor = self.TXINDEX.deserialize(data, cursor)
        return (DbTxIndex(version, pos, spent), cursor)
Ejemplo n.º 13
0
class TxinSerializer(Serializer):
    TXIN = Structure([
        OutpointSerializer(),
        VarstrScriptSerializer(),
        Field("<I", "sequence")
    ], "outpoint")

    def get_size(self, txin):
        return (self.TXIN.get_size(
            [txin.previous_output, txin.script, txin.sequence]))

    def serialize(self, txin):
        return (self.TXIN.serialize(
            [txin.previous_output, txin.script, txin.sequence]))

    def deserialize(self, data, cursor=0):
        (previous_output, script,
         sequence), cursor = self.TXIN.deserialize(data, cursor)
        return (TxIn(previous_output, script, sequence), cursor)
Ejemplo n.º 14
0
class MerkleTxSerializer(Serializer):
    MERKLE_TX = Structure([
        TxSerializer(),
        Uint256Serializer(),
        VarsizelistSerializer(VarintSerializer(), Uint256Serializer()),
        Field("<I", "nindex")
    ], "tx")

    def get_size(self, tx):
        return (self.MERKLE_TX.get_size(
            [tx.tx, tx.blockhash, tx.merkle_branch, tx.nindex]))

    def serialize(self, tx):
        return (self.MERKLE_TX.serialize(
            [tx.tx, tx.blockhash, tx.merkle_branch, tx.nindex]))

    def deserialize(self, data, cursor=0):
        (tx, blockhash, merkle_branch,
         nindex), cursor = self.MERKLE_TX.deserialize(data, cursor)
        return (MerkleTx(tx, blockhash, merkle_branch, nindex), cursor)
Ejemplo n.º 15
0
class AlertPayloadSerializer(Serializer):
    PAYLOAD = Structure([Field("<I", "version"),
                         Field("<Q", "relay_until"),
                         Field("<Q", "expiration"),
                         Field("<I", "id"),
                         Field("<I", "cancel"),
                         VarsizelistSerializer(VarintSerializer("count"), VarintSerializer("set_cancel")),
                         Field("<I", "min_ver"),
                         Field("<I", "max_ver"),
                         VarsizelistSerializer(VarintSerializer("count"), VarstrSerializer("set_sub_ver")),
                         Field("<I", "priority"),
                         VarstrSerializer("comment"),
                         VarstrSerializer("statusbar"),
                         VarstrSerializer("reserved")])
    
    def serialize(self, alert_payload):
        return (self.PAYLOAD.serialize([alert_payload.version, 
                                        alert_payload.relay_until,
                                        alert_payload.expiration,
                                        alert_payload.id,
                                        alert_payload.cancel,
                                        alert_payload.set_cancel,
                                        alert_payload.min_ver,
                                        alert_payload.max_ver,
                                        alert_payload.set_sub_ver,
                                        alert_payload.priority,
                                        alert_payload.comment,
                                        alert_payload.statusbar,
                                        alert_payload.reserved]))

    def deserialize(self, data, cursor=0):
        payload_fields, cursor = self.PAYLOAD.deserialize(data, cursor)
        (version, relay_until, expiration, id, cancel, set_cancel, min_ver, max_ver, \
         set_sub_ver, priority,  comment, statusbar, reserved) = payload_fields
        return (AlertPayload(version, relay_until, expiration, id, cancel, 
                             set(set_cancel), min_ver, max_ver, set(set_sub_ver), priority,
                             comment, statusbar, reserved), cursor)
Ejemplo n.º 16
0
 def __init__(self, desc=""):
     self.NETADDR = Structure([
         Field("<Q", "services"),
         IPAddrSerializer("ip"),
         Field(">H", "port")
     ], "netaddr")
Ejemplo n.º 17
0
 def __init__(self, flags=0):
     self.BLOCKLOCATOR = Structure([Field("<I", "version"),  
                                   VarsizelistSerializer(VarintSerializer("count"), Uint256Serializer("locator"))])