Example #1
0
    def stream_deserialize(cls, f):
        self = super(Block, cls).stream_deserialize(f)
        txs = VectorSerializer.stream_deserialize(Transaction, f)
        stxs = VectorSerializer.stream_deserialize(Transaction, f)

        self.txs = list(txs)
        self.stxs = list(stxs)
        return self
Example #2
0
    def stream_deserialize(cls, f):
        self = super(Block, cls).stream_deserialize(f)
        txs = VectorSerializer.stream_deserialize(Transaction, f)
        stxs = VectorSerializer.stream_deserialize(Transaction, f)

        self.txs = list(txs)
        self.stxs = list(stxs)
        return self
Example #3
0
    def serialize_prefix(self, f):
        VarIntSerializer.stream_serialize(len(self.txins), f)
        for txin in self.txins:
            txin.serialize_prefix(f)

        VectorSerializer.stream_serialize(TxOut, self.txouts, f)
        f.write(struct.pack(b'<I', self.locktime))
        f.write(struct.pack(b'<I', self.expiry))
Example #4
0
 def serialize_field(self, tx, attr, fmt, num_bytes, f):
     if fmt not in ['inputs', 'outputs', 'bytes']:
         f.write(struct.pack(fmt, getattr(tx, attr)))
     elif fmt == 'inputs':
         VectorSerializer.stream_serialize(CMutableTxIn, tx.vin, f)
     elif fmt == 'outputs':
         VectorSerializer.stream_serialize(CMutableTxOut, tx.vout, f)
     elif fmt == 'bytes':
         BytesSerializer.stream_serialize(getattr(tx, attr), f)
Example #5
0
 def stream_serialize(self, f):
     super(Block, self).stream_serialize(f)
     for attr, fmt, num_bytes, _ in self.block_fields:
         if fmt not in ['bytes', 'vectortx']:
             f.write(struct.pack(fmt, getattr(self, attr)))
         elif fmt == 'bytes':
             BytesSerializer.stream_serialize(getattr(self, attr), f)
         elif fmt == 'vectortx':
             VectorSerializer.stream_serialize(Transaction, getattr(self, attr), f)
Example #6
0
 def deserialize_field(self, tx, kwargs, attr, fmt, num_bytes, f):
     if fmt not in ['inputs', 'outputs', 'bytes']:
         kwargs[attr] = struct.unpack(fmt, ser_read(f, num_bytes))[0]
     elif fmt == 'inputs':
         kwargs[attr] = VectorSerializer.stream_deserialize(CMutableTxIn, f)
     elif fmt == 'outputs':
         kwargs[attr] = VectorSerializer.stream_deserialize(CMutableTxOut, f)
     elif fmt == 'bytes':
         kwargs[attr] =  BytesSerializer.stream_deserialize(f)
Example #7
0
 def stream_serialize(self, f):
     for attr, fmt, num_bytes, _ in self.fields:
         if fmt not in ["inputs", "outputs", "bytes"]:
             f.write(struct.pack(fmt, getattr(self, attr)))
         elif fmt == "inputs":
             VectorSerializer.stream_serialize(CTxIn, self.vin, f)
         elif fmt == "outputs":
             VectorSerializer.stream_serialize(CTxOut, self.vout, f)
         elif fmt == "bytes":
             BytesSerializer.stream_serialize(getattr(self, attr), f)
Example #8
0
 def stream_serialize(self, f):
     for attr, fmt, num_bytes, _ in self.fields:
         if fmt not in ['inputs', 'outputs', 'bytes']:
             f.write(struct.pack(fmt, getattr(self, attr)))
         elif fmt == 'inputs':
             VectorSerializer.stream_serialize(CTxIn, self.vin, f)
         elif fmt == 'outputs':
             VectorSerializer.stream_serialize(CTxOut, self.vout, f)
         elif fmt == 'bytes':
             BytesSerializer.stream_serialize(getattr(self, attr), f)
Example #9
0
 def stream_deserialize(cls, f):
     self = cls()
     for attr, fmt, num_bytes, _ in self.fields:
         if fmt not in ['inputs', 'outputs', 'bytes']:
             setattr(self, attr, struct.unpack(fmt, ser_read(f, num_bytes))[0])
         elif fmt == 'inputs':
             setattr(self, attr, VectorSerializer.stream_deserialize(CTxIn, f))
         elif fmt == 'outputs':
             setattr(self, attr, VectorSerializer.stream_deserialize(CTxOut, f))
         elif fmt == 'bytes':
             setattr(self, attr, BytesSerializer.stream_deserialize(f))
     return self
Example #10
0
 def stream_deserialize(cls, f):
     self = super(Block, cls).stream_deserialize(f)
     vtx = VectorSerializer.stream_deserialize(Transaction, f)
     vMerkleTree = tuple(Block.build_merkle_tree_from_txs(vtx))
     setattr(self, 'vMerkleTree', vMerkleTree)
     setattr(self, 'vtx', tuple(vtx))
     return self
Example #11
0
 def stream_deserialize(cls, f):
     self = super(Block, cls).stream_deserialize(f)
     vtx = VectorSerializer.stream_deserialize(Transaction, f)
     vMerkleTree = tuple(Block.build_merkle_tree_from_txs(vtx))
     setattr(self, 'vMerkleTree', vMerkleTree)
     setattr(self, 'vtx', tuple(vtx))
     return self
Example #12
0
    def stream_deserialize(cls, f):
        self = super(CAltcoinBlock, cls).stream_deserialize(f)

        vtx = VectorSerializer.stream_deserialize(CTransaction, f)
        vMerkleTree = tuple(bitcoin.core.CBlock.build_merkle_tree_from_txs(vtx))
        object.__setattr__(self, 'vMerkleTree', vMerkleTree)
        object.__setattr__(self, 'vtx', tuple(vtx))

        return self
Example #13
0
    def stream_deserialize(cls, f):
        self = super(CAltcoinBlock, cls).stream_deserialize(f)

        vtx = VectorSerializer.stream_deserialize(CTransaction, f)
        vMerkleTree = tuple(bitcoin.core.CBlock.build_merkle_tree_from_txs(vtx))
        object.__setattr__(self, 'vMerkleTree', vMerkleTree)
        object.__setattr__(self, 'vtx', tuple(vtx))

        return self
Example #14
0
    def stream_deserialize(cls, f):
        self = super(Block, cls).stream_deserialize(f)
        for attr, fmt, num_bytes, _ in self.block_fields:
            if fmt not in ['bytes', 'vectortx']:
                setattr(self, attr, struct.unpack(fmt, ser_read(f, num_bytes))[0])
            elif fmt == 'bytes':
                setattr(self, attr, BytesSerializer.stream_deserialize(f))
            elif fmt == 'vectortx':
                setattr(self, attr, VectorSerializer.stream_deserialize(Transaction, f))

        setattr(self, 'vMerkleTree', tuple(Block.build_merkle_tree_from_txs(getattr(self, 'vtx'))))
        return self
Example #15
0
 def stream_serialize(self, f):
     VarStringSerializer.stream_serialize(self.name.encode('utf-8'), f)
     self.schema_ver.stream_serialize(f)
     self.prev_schema.stream_serialize(f)
     VectorSerializer.stream_serialize(FieldType, self.field_types, f)
     VectorSerializer.stream_serialize(SealType, self.seal_types, f)
     VectorSerializer.stream_serialize(ProofType, self.proof_types, f)
Example #16
0
    def deserialize_prefix(self, f):
        """Deserialize the transaction prefix."""
        txin_count = VarIntSerializer.stream_deserialize(f)
        txins = []
        for i in range(txin_count):
            txin = TxIn()
            txin.deserialize_prefix(f)
            txins.append(txin)

        txouts = VectorSerializer.stream_deserialize(TxOut, f)
        locktime = struct.unpack(b'<I', ser_read(f, 4))[0]
        expiry = struct.unpack(b'<I', ser_read(f, 4))[0]

        self.txins = list(txins)
        self.txouts = list(txouts)
        self.locktime = locktime
        self.expiry = expiry
Example #17
0
 def stream_serialize(self, f):
     f.write(struct.pack(b"<i", self.nVersion))
     VectorSerializer.stream_serialize(CInterested, self.vInterested, f)
Example #18
0
 def stream_deserialize(cls, f):
     nVersion = struct.unpack(b"<i", ser_read(f, 4))[0]
     vInterested = VectorSerializer.stream_deserialize(CInterested, f)
     return cls(vInterested=vInterested, protover=nVersion)
Example #19
0
 def stream_serialize(self, f):
     super(CAltcoinBlock, self).stream_serialize(f)
     VectorSerializer.stream_serialize(CTransaction, self.vtx, f)
Example #20
0
 def stream_serialize(self, f):
     super(Block, self).stream_serialize(f)
     VectorSerializer.stream_serialize(Transaction, self.vtx, f)
Example #21
0
    def stream_deserialize(cls, f, **kwargs):
        schema_obj = kwargs['schema_obj'] if 'schema_obj' in kwargs else None
        if not isinstance(schema_obj, Schema):
            raise ValueError(
                f'`schema_obj` parameter must be of Schema type; got `{schema_obj}` instead'
            )

        # Deserialize proof header
        # - version with flag
        (ver, flag) = FlagVarIntSerializer.stream_deserialize(f)
        # - fields common for root and upgrade proofs
        if flag:
            schema = Hash256Id.stream_deserialize(f)
            network = VarIntSerializer.stream_deserialize(f)
            if network is 0x00:
                format = ProofFormat.upgrade
            # - root-specific fields
            else:
                network = Network(network)
                format = ProofFormat.root
                root = OutPoint.stream_deserialize(f, short=False)
        else:
            format = ProofFormat.ordinary

        # Deserialize proof body
        # - reading proof type
        type_no = ser_read(f, 1)[0]

        # - reading `seal_sequence` structure
        seals = []
        seal_type_no = 0
        # -- we iterate over the seals until 0xFF (=FlagVarIntSerializer.Separator.EOF) byte is met
        while True:
            try:
                # -- reading seal with the current type number
                seal = Seal.stream_deserialize(f,
                                               type_no=seal_type_no,
                                               schema_obj=schema_obj)
            except BaseException as ex:
                # due to some strange bug, python 3 is unable to capture SeparatorByteSignal exception by its type,
                # and `isinstance(ex, SeparatorByteSignal)` returns False as well :(
                # so we have to capture generic exception and re-raise if it is not SeparatorByteSignal, which
                # can be determined only by the presence of its method
                if not callable(getattr(ex, "is_eol", None)):
                    raise
                if ex.is_eol():
                    # -- met 0xFE separator byte, increasing current type number
                    seal_type_no = seal_type_no + 1
                elif ex.is_eof():
                    # -- end of `seal_sequence` structure
                    break
            else:
                # -- otherwise append read seal to the list of seals
                seals.append(seal)

        # -- if we had zero seals implies proof of state destruction format
        if len(seals) is 0:
            format = ProofFormat.burn

        # - reading unparsed state and metadata bytes
        state = BytesSerializer.stream_deserialize(f)
        metadata = BytesSerializer.stream_deserialize(f)

        # Deserialize original public key
        pkcode = ser_read(f, 1)
        if pkcode is 0x00:
            pubkey = None
        else:
            buf = pkcode + ser_read(f, 32)
            pubkey = PubKey.deserialize(buf)

        # Deserialize prunable data
        try:
            pruned_flag = ser_read(f, 1)
        except:
            pruned_flag = 0x00

        txid, parents = None, None
        if pruned_flag & 0x01 > 0:
            txid = Hash256Id.stream_deserialize(f)
        if pruned_flag & 0x02 > 0:
            parents = VectorSerializer.stream_deserialize(Hash256Id, f)

        proof = Proof(schema_obj=schema_obj,
                      type_no=type_no,
                      ver=ver,
                      format=format,
                      schema=schema,
                      network=network,
                      root=root,
                      pubkey=pubkey,
                      fields=None,
                      seals=seals,
                      txid=txid,
                      parents=parents,
                      metadata=metadata,
                      state=state)

        # Parsing raw seals and metadata and resolving types against the provided Schema
        if 'schema_obj' in kwargs:
            schema_obj = kwargs['schema_obj']
        if isinstance(schema_obj, Schema):
            proof.resolve_schema(schema_obj)

        return proof
Example #22
0
 def stream_serialize(self, f):
     VarStringSerializer.stream_serialize(self.name.encode('utf-8'), f)
     VectorSerializer.stream_serialize(TypeRef, self.fields, f)
     VectorSerializer.stream_serialize(
         TypeRef, [] if self.unseals is None else self.unseals, f)
     VectorSerializer.stream_serialize(TypeRef, self.seals, f)
Example #23
0
 def msg_ser(self, f):
     VectorSerializer.stream_serialize(CInv, self.inv, f)
Example #24
0
 def stream_serialize(self, f):
     super(Block, self).stream_serialize(f)
     VectorSerializer.stream_serialize(Transaction, self.txs, f)
     VectorSerializer.stream_serialize(Transaction, self.stxs, f)
Example #25
0
 def msg_deser(cls, f, protover=PROTO_VERSION):
     inv = VectorSerializer.stream_deserialize(CInv, f)
     return cls(inv)
Example #26
0
    def stream_serialize(self, f, **kwargs):
        # Serialize proof header
        # - version with flag
        ver = self.ver if self.ver is not None else 0
        flag = self.format is ProofFormat.root or self.format is ProofFormat.upgrade
        FlagVarIntSerializer.stream_serialize((ver, flag), f)
        # - root proof fields
        if self.format is ProofFormat.root:
            self.schema.stream_serialize(f)
            VarIntSerializer.stream_serialize(self.network.value, f)
            self.root.stream_serialize(f, short_form=False)
        # - version upgrade proof fields
        elif self.format is ProofFormat.upgrade:
            if self.schema is not None:
                self.schema.stream_serialize(f)
            else:
                ZeroBytesSerializer.stream_serialize(32, f)
            ZeroBytesSerializer.stream_serialize(1, f)

        # Serialize proof body
        # - serializing proof type
        if self.type_no is None:
            raise ValueError(
                'proof consensus serialization requires `type_no` to be known')
        f.write(bytes([self.type_no]))

        # - writing `seal_sequence` structure
        current_type_no = None
        for seal in self.seals:
            if current_type_no is None:
                current_type_no = seal.type_no
            elif seal.type_no is not current_type_no:
                # -- writing EOL byte to signify the change of the type
                [
                    f.write(bytes([0x7F]))
                    for n in range(current_type_no, seal.type_no)
                ]
                current_type_no = seal.type_no
            seal.stream_serialize(f, state=False)
        f.write(bytes([0xFF]))

        # - writing raw data for the sealed state
        length = reduce(
            (lambda acc, seal: acc + len(seal.serialize({'state': True}))),
            [0] + self.seals)
        VarIntSerializer.stream_serialize(length, f)
        [seal.stream_serialize(f, state=True) for seal in self.seals]

        # - writing raw data for all metafields
        length = reduce((lambda acc, field: acc + len(field.serialize())),
                        [0] + self.fields)
        VarIntSerializer.stream_serialize(length, f)
        [field.stream_serialize(f) for field in self.fields]

        # Serialize original public key
        if self.pubkey is not None:
            self.pubkey.stream_serialize(f)
        else:
            ZeroBytesSerializer.stream_serialize(1, f)

        # Serialize prunable data
        if self.txid is not None:
            if self.parents is not None:
                f.write(bytes(0x03))
                self.txid.stream_serialize(f)
            else:
                f.write(bytes(0x01))
            VectorSerializer.stream_serialize(Hash256Id, self.parents, f)
        elif self.parents is not None:
            f.write(bytes(0x02))
            VectorSerializer.stream_serialize(Hash256Id, self.parents, f)
        else:
            f.write(bytes(0x00))