Exemple #1
0
def serialize_address_data(received_count, received_amount, coins, sent_count,
                           sent_amount, coins_destroyed):
    if sent_count:
        return b"".join(
            (int_to_c_int(received_count), int_to_c_int(received_amount),
             int_to_c_int(coins), int_to_c_int(sent_count),
             int_to_c_int(sent_amount), int_to_c_int(coins_destroyed)))
    else:
        return b"".join((int_to_c_int(received_count),
                         int_to_c_int(received_amount), int_to_c_int(coins)))
Exemple #2
0
def serialize_tx_data(tx):
    data = b""
    h = 0 if not tx["coinbase"] else 128
    if tx["coinbase"]:
        if tx["segwit"]:
            h = h + 64
            data += tx["hash"]
    if tx["version"] > 14:
        h += 48
        data += int_to_c_int(tx["version"])
    else:
        h = h + (tx["version"] << 2)
    if tx["lockTime"]:
        h += 2
        data += int_to_c_int(tx["lockTime"])
    if "data" in tx and tx["data"]:
        h += 1
        data += tx["data"]
    return h.to_bytes(1, "little") + data
    async def block_batch_handler(self, block):
        size = block["size"]
        size_c_int = 80 + len(int_to_c_int(len(block["rawTx"])))

        for t in block["rawTx"]:
            size_c_int += len(serialize_cint(block["rawTx"][t]))

        self.timeline_size_c_int += size_c_int
        self.timeline_size_v_int += size
        self.block_batch.append(
            (block["height"], int(time.time()), size, self.timeline_size_v_int,
             size_c_int, self.timeline_size_c_int))
        if block["height"] % 10000 == 0:
            self.log.info("Blockchain blocks %s size %s cint_size %s " %
                          (block["height"], self.timeline_size_v_int,
                           self.timeline_size_c_int))
            self.log.info(
                "diff  %s -> %s %%  " %
                (self.timeline_size_v_int - self.timeline_size_c_int,
                 round((self.timeline_size_v_int - self.timeline_size_c_int) /
                       self.timeline_size_v_int * 100, 2)))
            print(len(self.block_batch))
    def __init__(self,
                 raw_tx=None,
                 format="decoded",
                 version=1,
                 lock_time=0,
                 testnet=False,
                 auto_commit=True,
                 keep_raw_tx=False,
                 c_int=False):
        if format not in ("decoded", "raw"):
            raise ValueError("format error, raw or decoded allowed")
        self.auto_commit = auto_commit
        self["format"] = format
        self["testnet"] = testnet
        self["segwit"] = False
        self["txId"] = None
        self["hash"] = None
        self["version"] = version
        self["size"] = 0
        self["vSize"] = 0
        self["bSize"] = 0
        self["lockTime"] = lock_time
        self["vIn"] = dict()
        self["vOut"] = dict()
        self["rawTx"] = None
        self["blockHash"] = None
        self["confirmations"] = None
        self["time"] = None
        self["blockTime"] = None
        self["blockIndex"] = None
        self["coinbase"] = False
        self["fee"] = None
        self["data"] = None
        self["amount"] = None
        if raw_tx is None:
            return

        self["rawTx"] = deque()
        rtx = self["rawTx"].append
        self["amount"] = 0
        sw = sw_len = 0
        stream = self.get_stream(raw_tx)
        start = stream.tell()
        read = stream.read
        tell = stream.tell
        if not c_int:
            # start deserialization
            t = read(4)
            rtx(t)
            self["version"] = unpack('<L', t)[0]
            n = read_var_int(stream)
            rtx(n)
            if n == b'\x00':
                # segwit format
                sw = 1
                self["flag"] = read(1)
                rtx(self["flag"])
                n = read_var_int(stream)
                rtx(n)
            # inputs
            ic = var_int_to_int(n)

            for k in range(ic):
                self["vIn"][k] = dict()
                self["vIn"][k]["txId"] = read(32)
                rtx(self["vIn"][k]["txId"])
                t = read(4)
                rtx(t)
                self["vIn"][k]["vOut"] = unpack('<L', t)[0]
                t = read_var_int(stream)
                rtx(t)
                self["vIn"][k]["scriptSig"] = read(var_int_to_int(t))
                rtx(self["vIn"][k]["scriptSig"])
                t = read(4)
                rtx(t)
                self["vIn"][k]["sequence"] = unpack('<L', t)[0]
            # outputs
            t = read_var_int(stream)
            rtx(t)
            for k in range(var_int_to_int(t)):
                self["vOut"][k] = dict()
                t = read(8)
                self["vOut"][k]["value"] = unpack('<Q', t)[0]
                rtx(t)
                self["amount"] += self["vOut"][k]["value"]
                t = read_var_int(stream)
                self["vOut"][k]["scriptPubKey"] = read(var_int_to_int(t))
                rtx(t)
                rtx(self["vOut"][k]["scriptPubKey"])
                s = parse_script(self["vOut"][k]["scriptPubKey"])
                self["vOut"][k]["nType"] = s["nType"]
                self["vOut"][k]["type"] = s["type"]
                if self["data"] is None:
                    if s["nType"] == 3:
                        self["data"] = s["data"]
                if s["nType"] not in (3, 4, 7, 8):
                    self["vOut"][k]["addressHash"] = s["addressHash"]
                    self["vOut"][k]["reqSigs"] = s["reqSigs"]
        else:
            # start deserialization
            n = read_c_int(stream)
            rtx(n)
            self["version"] = int_to_c_int(n)
            n = read_var_int(stream)
            rtx(n)
            if n == b'\x00':
                # segwit format
                sw = 1
                self["flag"] = read(1)
                rtx(self["flag"])
                n = read_c_int(stream)
                rtx(n)
            # inputs
            ic = int_to_c_int(n)

            for k in range(ic):
                self["vIn"][k] = dict()
                self["vIn"][k]["txId"] = read(32)
                rtx(self["vIn"][k]["txId"])
                n = read_c_int(stream)
                rtx(n)
                self["vIn"][k]["vOut"] = int_to_c_int(n)
                t = read_c_int(stream)
                rtx(t)
                self["vIn"][k]["scriptSig"] = read(int_to_c_int(t))
                rtx(self["vIn"][k]["scriptSig"])

                t = read_c_int(stream)
                rtx(t)
                self["vIn"][k]["sequence"] = 0xffffffff - int_to_c_int(t)
            # outputs
            t = read_c_int(stream)
            rtx(t)
            for k in range(int_to_c_int(t)):
                self["vOut"][k] = dict()
                t = read_c_int(stream)
                rtx(t)
                self["vOut"][k]["value"] = int_to_c_int(t)
                self["amount"] += self["vOut"][k]["value"]
                t = read_c_int(stream)
                self["vOut"][k]["scriptPubKey"] = read(int_to_c_int(t))
                rtx(t)
                rtx(self["vOut"][k]["scriptPubKey"])
                s = parse_script(self["vOut"][k]["scriptPubKey"])
                self["vOut"][k]["nType"] = s["nType"]
                self["vOut"][k]["type"] = s["type"]
                if self["data"] is None:
                    if s["nType"] == 3:
                        self["data"] = s["data"]
                if s["nType"] not in (3, 4, 7, 8):
                    self["vOut"][k]["addressHash"] = s["addressHash"]
                    self["vOut"][k]["reqSigs"] = s["reqSigs"]

        # witness
        if sw:
            sw = tell() - start
            for k in range(ic):
                self["vIn"][k]["txInWitness"] = []
                t = read_c_int(stream)
                rtx(t)
                for c in range(int_to_c_int(t)):
                    l = read_c_int(stream)
                    rtx(l)
                    d = read(int_to_c_int(l))
                    rtx(d)
                    self["vIn"][k]["txInWitness"].append(d)

            sw_len = (stream.tell() - start) - sw + 2
        t = read_c_int(stream)
        rtx(t)
        self["lockTime"] = int_to_c_int(t)

        end = tell()
        self["rawTx"] = b"".join(self["rawTx"])
        self["size"] = end - start
        self["bSize"] = end - start - sw_len
        self["weight"] = self["bSize"] * 3 + self["size"]
        self["vSize"] = ceil(self["weight"] / 4)
        if ic == 1 and \
                self["vIn"][0]["txId"] == b'\x00' * 32 and \
                self["vIn"][0]["vOut"] == 0xffffffff:
            self["coinbase"] = True
        else:
            self["coinbase"] = False
        if sw:
            self["segwit"] = True
            self["hash"] = double_sha256(self["rawTx"])
            self["txId"] = double_sha256(b"".join(
                (self["rawTx"][:4], self["rawTx"][6:sw], self["rawTx"][-4:])))
        else:
            self["segwit"] = False
            self["txId"] = double_sha256(self["rawTx"])
            self["hash"] = self["txId"]
        if not keep_raw_tx:
            self["rawTx"] = None

        if self["format"] == "decoded":
            self.decode()
def serialize_cint(self, segwit=True):
    chunks = []
    append = chunks.append
    append(int_to_c_int(self["version"]))
    if segwit and self["segwit"]:
        append(b"\x00\x01")
    append(int_to_c_int(len(self["vIn"])))
    for i in self["vIn"]:
        append(self["vIn"][i]['txId'])
        append(int_to_c_int(self["vIn"][i]['vOut']))
        append(int_to_c_int(len(self["vIn"][i]['scriptSig'])))
        append(self["vIn"][i]['scriptSig'])
        append(int_to_c_int(0xffffffff - self["vIn"][i]['sequence']))
    append(int_to_c_int(len(self["vOut"])))
    for i in self["vOut"]:
        append(int_to_c_int(self["vOut"][i]['value']))
        append(int_to_c_int(len(self["vOut"][i]['scriptPubKey'])))
        append(self["vOut"][i]['scriptPubKey'])
    if segwit and self["segwit"]:
        for i in self["vIn"]:
            append(int_to_c_int(len(self["vIn"][i]['txInWitness'])))
            for w in self["vIn"][i]['txInWitness']:
                append(int_to_c_int(len(w)))
                append(w)

    append(int_to_c_int(self['lockTime']))
    return b''.join(chunks)
Exemple #6
0
def serialize_address_data(received_count, received_amount, coins, frp, lra,
                           lrp, sent_count, sent_amount, coins_destroyed, fsp,
                           lsa, lsp):
    if sent_count:
        return b"".join(
            (int_to_c_int(received_count), int_to_c_int(received_amount),
             int_to_c_int(coins), int_to_c_int(frp), int_to_c_int(lra),
             int_to_c_int(lrp), int_to_c_int(sent_count),
             int_to_c_int(sent_amount), int_to_c_int(coins_destroyed),
             int_to_c_int(fsp), int_to_c_int(lsa), int_to_c_int(lsp)))
    else:
        return b"".join(
            (int_to_c_int(received_count), int_to_c_int(received_amount),
             int_to_c_int(coins), int_to_c_int(frp), int_to_c_int(lra),
             int_to_c_int(lrp)))