예제 #1
0
 def deserialize(raw):
     message = Message
     decoded = message.ParseFromString(raw)
     is_full = (decoded.wantlist and decoded.wantlist.full) or False
     bitswap_message = BitswapMessage(is_full)
     if decoded.wantlist:
         for entry in decoded.wantlist.entries:
             cid = py_cid.make_cid(entry.block)
             bitswap_message.add_entry(cid, entry.priority, entry.cancel)
     # Bitswap 1.0.0
     if len(decoded.blocks) > 0:
         for block in decoded.blocks:
             bitswap_message.add_block(
                 Block(block,
                       py_cid.make_cid(multihashing(block, 'sha2-256'))))
     # Bitswap 1.1.0
     if len(decoded.payload) > 0:
         for payload in decoded.payload:
             values = varint_decoder(payload.prefix)
             cid_version = values[0]
             multicodec = values[1]
             hash_algo = values[2]
             # hash_len = values[3]  # not needed, as yet
             hashed = multihashing(payload.data, hash_algo)
             cid = py_cid.make_cid(cid_version, CODE_TABLE[multicodec],
                                   hashed)
             bitswap_message.add_block(Block(payload.data, cid))
     return bitswap_message
예제 #2
0
def test_len():
    wl = WantList()
    cid = py_cid.make_cid('QmaozNR7DZHQK1ZcU9p7QdrshMvXqWK6gpu5rmrkPdT3L4')
    assert 0 == len(wl)
    wl.add(cid)
    wl.add(cid)
    assert 1 == len(wl)
    wl.add(py_cid.make_cid('QmaozNR7DZddK1ZcU9p7QdrshMvXqWK6gpu5rmrkPdTPdT'))
    assert 2 == len(wl)
예제 #3
0
    def cid_is_valid(self, cid):
        if not cid:
            return False

        try:
            make_cid(cid)
            return True
        except Exception:
            return False
예제 #4
0
    def cid_is_valid(self, cid):
        if not cid:
            return False

        try:
            make_cid(cid)
            return True
        except Exception as e:
            logger.error(f'IPFSCLIENT | Error in cid_is_valid {str(e)}')
            return False
예제 #5
0
def test_contains():
    wl = WantList()
    cids = [
        py_cid.make_cid('QmaozNR7DZHQK1ZcU9p7QdrshMvXqWK6gpu5rmrkPdT3L4'),
        py_cid.make_cid('QmaozNR7DZddK1ZcU9p7QdrshMvXqWK6gpu5rmrkPdTPdT')
    ]
    wl.add(cids[0])
    assert cids[0] in wl
    assert not (cids[1] in wl)
    wl.add(cids[1])
    assert cids[1] in wl
예제 #6
0
def test_iter():
    wl = WantList()
    cids = [
        py_cid.make_cid('QmaozNR7DZHQK1ZcU9p7QdrshMvXqWK6gpu5rmrkPdT3L4'),
        py_cid.make_cid('QmaozNR7DZddK1ZcU9p7QdrshMvXqWK6gpu5rmrkPdTPdT')
    ]
    wl.add(cids[0])
    wl.add(cids[1])
    assert len(wl) == 2
    for i in wl:
        assert i in [str(cid) for cid in cids]
예제 #7
0
def test_eq():
    cid = py_cid.make_cid('QmaozNR7DZHQK1ZcU9p7QdrshMvXqWK6gpu5rmrkPdT3L4')
    e = WantListEntry(cid, 5)
    e2 = WantListEntry(cid, 5)
    assert e == e2
    e2.inc_ref_count()
    assert not (e == e2)  # different _ref_count
    e2 = WantListEntry(cid, 3)
    assert not (e == e2)  # different priority
    cid2 = py_cid.make_cid('QmaozNR7DZHQK1ZcU9p7QdrshMvXqW71ZcU9p7QdrshMpa')
    e2 = WantListEntry(cid2, 5)
    assert not (e == e2)  # different cid
def test_eq():
    cid = py_cid.make_cid('QmaozNR7DZHQK1ZcU9p7QdrshMvXqWK6gpu5rmrkPdT3L4')
    m = MessageEntry(cid, True, 5)
    m2 = MessageEntry(cid, True, 5)
    assert m.equals(m2)
    assert m == m2
    m2.cancel = False
    assert not (m.equals(m2))  # different cancel
    assert not (m == m2)  # different cancel
    cid2 = py_cid.make_cid('QmaozNR7DZHQK1ZcU9p7QdrshMvXqW71ZcU9p7QdrshMpa')
    m2 = MessageEntry(cid2, True, 5)
    assert not (m.equals(m2))  # different cid
    assert not (m == m2)  # different cid
예제 #9
0
def test_force_remove():
    wl = WantList()
    cid = py_cid.make_cid('QmaozNR7DZHQK1ZcU9p7QdrshMvXqWK6gpu5rmrkPdT3L4')
    wl.add(cid)
    wl.add(cid)
    wl.force_remove(cid)
    assert not (str(cid) in wl.entries)
예제 #10
0
def test_dec():
    cid = py_cid.make_cid('QmaozNR7DZHQK1ZcU9p7QdrshMvXqWK6gpu5rmrkPdT3L4')
    e = WantListEntry(cid)
    assert e._ref_count == 1
    e.dec_ref_count()
    assert e._ref_count == 0
    e.dec_ref_count()
    assert e._ref_count == 0
예제 #11
0
def test_has_refs():
    cid = py_cid.make_cid('QmaozNR7DZHQK1ZcU9p7QdrshMvXqWK6gpu5rmrkPdT3L4')
    e = WantListEntry(cid)
    assert e.has_refs()
    e.dec_ref_count()
    assert not (e.has_refs())
    e.dec_ref_count()
    assert not (e.has_refs())
예제 #12
0
def test_cancel_want():
    ledger = Ledger(peer_id)
    cid = py_cid.make_cid('QmaozNR7DZHQK1ZcU9p7QdrshMvXqWK6gpu5rmrkPdT3L4')
    ledger.wants(cid, 1)
    ledger.wants(cid, 1)
    ledger.cancel_want(cid)
    assert cid in ledger.wantlist
    ledger.cancel_want(cid)
    assert cid not in ledger.wantlist
예제 #13
0
def test_add():
    wl = WantList()
    cid = py_cid.make_cid('QmaozNR7DZHQK1ZcU9p7QdrshMvXqWK6gpu5rmrkPdT3L4')
    wl.add(cid)
    assert str(cid) in wl.entries
    assert 1 == wl.entries[str(cid)].priority
    wl.add(cid, 3)
    assert str(cid) in wl.entries
    assert 3 == wl.entries[str(cid)].priority
    assert 2 == wl.entries[str(cid)]._ref_count
예제 #14
0
def test_eq():
    m = MessageEntry(cid, True, 5)
    m2 = MessageEntry(cid, True, 5)
    assert m == m2
    m2.cancel = False
    assert m != m2  # different cancel
    cid2 = py_cid.make_cid('QmaozNR7DZHQK1ZcU9p7QdrshMvXqW71ZcU9p7QdrshMpa')
    m2 = MessageEntry(cid2, True, 5)
    assert m != m2  # different cid
    m2 = MessageEntry(cid, True, 4)
    assert m != m2  # different priority
예제 #15
0
def test_sorted_entries():
    wl = WantList()
    wl.add(py_cid.make_cid('QmaozNR7DZHQK1ZcU9p7QdrshMvXqWK6gpu5rmrkPdT3L4'))
    # TODO add some more cids
    sorted_entries = wl.sorted_entries()
    c = None
    for index, entry in enumerate(sorted_entries.keys()):
        if index == 0:
            c = entry
        else:
            assert entry >= c
            c = entry
예제 #16
0
def decode(value):
    """
    Decode B58 multi hash.

    :param bytes value: an encoded content

    :return: the decoded content
    :rtype: str
    """

    cid = make_cid(value)
    return multihash.to_b58_string(cid.multihash)
예제 #17
0
def encode(value):
    """
    Encode IPFS.

    :param bytes value: a decoded content

    :return: the encoded content
    :rtype: str
    """

    mhash = multihash.from_b58_string(value)
    return make_cid(1, 'dag-pb', mhash).buffer
예제 #18
0
def test_init():
    cid = py_cid.make_cid('QmaozNR7DZHQK1ZcU9p7QdrshMvXqWK6gpu5rmrkPdT3L4')
    e = WantListEntry(cid)
    assert e.cid == cid
    assert e._ref_count == 1
    assert e.priority == 1
    e = WantListEntry(cid, 5)
    assert e.cid == cid
    assert e._ref_count == 1
    assert e.priority == 5
    with pytest.raises(ValueError):
        WantListEntry('df')
예제 #19
0
def encode(value):
    """
    Encode Swarm.

    :param bytes value: a decoded content

    :return: the encoded content
    :rtype: str
    """

    mhash = multihash.encode(multihash.from_hex_string(value), 'keccak-256')
    return make_cid(1, 'swarm-manifest', mhash).buffer
예제 #20
0
def decode(value):
    """
    Decode HEX multi hash.

    :param bytes value: an encoded content

    :return: the decoded content
    :rtype: str
    """

    cid = make_cid(value)
    return multihash.to_hex_string(multihash.decode(cid.multihash).digest)
def test_init_cid_priority():
    cid = py_cid.make_cid('QmaozNR7DZHQK1ZcU9p7QdrshMvXqWK6gpu5rmrkPdT3L4')
    m = MessageEntry(cid, False)
    assert m.entry == WantListEntry(cid)
    assert m.cid == cid
    assert m.cancel == False
    assert m.priority == 1
    m = MessageEntry(cid, True, 5)
    assert m.entry == WantListEntry(cid, 5)
    assert m.cid == cid
    assert m.cancel == True
    assert m.priority == 5
    with pytest.raises(ValueError):
        MessageEntry('df', True)
예제 #22
0
def normalize(obj):
    # Text or binary string with a least a length of 7 characters may be a CID
    if isinstance(obj, (str, bytes)) and len(obj) >= 7:
        # Try to decode string as CID
        try:
            obj = cid.make_cid(obj)
        except ValueError:
            pass

    # Convert all CIDs to their version 1 representation
    if isinstance(obj, cid.cid.BaseCID):
        if obj.version != 1:
            obj = obj.to_v1()

    # Sequences (tuples, lists, …) and sets may contain CID values
    if isinstance(obj, (collections.abc.Sequence, collections.abc.Set)) \
       and not isinstance(obj, (str, bytes)):  # ← these are handled above
        # Try to substitute CID values in all sequence items
        mutated = False
        items = []
        for item in obj:
            item_new = normalize(item)
            mutated |= (item is not item_new)
            items.append(item_new)

        # Recreate sequence/set object with new values
        # NOTE: Recreating sequences in this way may not work for custom types!
        if mutated:
            obj = obj.__class__(items)
        items = None

    # Mappings (dict, OrderedDict, …), may also contain CID values
    if isinstance(obj, collections.abc.Mapping):
        # Try to substitute CID values in all sequence items
        mutated = False
        items = []
        for key, value in obj.items():
            key_new = normalize(key)
            value_new = normalize(value)
            mutated |= (key is not key_new) or (value is not value_new)
            items.append((key_new, value_new))

        # Recreate mapping object with new values
        # NOTE: Recreating mappings in this way may not work for custom types!
        if mutated:
            obj = obj.__class__(dict(items))
        items = None

    return obj
예제 #23
0
def test_remove():
    wl = WantList()
    cid = py_cid.make_cid('QmaozNR7DZHQK1ZcU9p7QdrshMvXqWK6gpu5rmrkPdT3L4')
    wl.remove(cid)
    assert not (str(cid) in wl.entries)
    wl.add(cid)
    wl.remove(cid)
    assert not (str(cid) in wl.entries)
    wl.add(cid)
    wl.add(cid)
    wl.add(cid)
    wl.remove(cid)
    assert str(cid) in wl.entries
    assert 2 == wl.entries[str(cid)]._ref_count
    wl.remove(cid)
    assert str(cid) in wl.entries
    assert 1 == wl.entries[str(cid)]._ref_count
    wl.remove(cid)
    assert not (str(cid) in wl.entries)
예제 #24
0
파일: cid_cli.py 프로젝트: weaming/cid-cli
def main():
    import argparse

    parser = argparse.ArgumentParser()
    parser.add_argument("cid")
    args = parser.parse_args()

    try:
        c = make_cid(args.cid)
    except ValueError as e:
        print(e, file=sys.stderr)
        sys.exit(1)

    if c.version == 0:
        mb_codec = 'base58btc'
    else:
        mb_codec = multibase.get_codec(args.cid).encoding
    mh = multihash.decode(c.multihash)
    print(
        f'{mb_codec} - cidv{c.version} - {c.codec} - {mh.name}-{len(mh.digest)*8}-{mh.digest.hex()}'
    )
예제 #25
0
def to_bytes(proto, string):
    expected_codec = PROTO_NAME_TO_CIDv1_CODEC.get(proto.name)

    if len(string) in CIDv0_PREFIX_TO_LENGTH.get(string[0:2], ()):  # CIDv0
        # Upgrade the wire (binary) representation of any received CIDv0 string
        # to CIDv1 if we can determine which multicodec value to use
        if expected_codec:
            return cid.make_cid(1, expected_codec,
                                base58.b58decode(string)).buffer

        return base58.b58decode(string)
    else:  # CIDv1+
        parsed = cid.from_string(string)

        # Ensure CID has correct codec for protocol
        if expected_codec and parsed.codec != expected_codec:
            raise ValueError(
                "“{0}” multiaddr CIDs must use the “{1}” multicodec".format(
                    proto.name, expected_codec))

        return parsed.buffer
예제 #26
0
 def test_cidv0_eq_cidv0(self, test_hash):
     """ check for equality for CIDv0 for same hash """
     assert CIDv0(test_hash) == make_cid(CIDv0(test_hash).encode())
예제 #27
0
 def test_invalid_arguments(self):
     """ make_cid: make_cid does not work if bad arguments are passed """
     with pytest.raises(ValueError) as excinfo:
         make_cid(1, 2, 3, 4)
     assert 'invalid number of arguments' in str(excinfo.value)
예제 #28
0
 def test_version_0_invalid_codec(self):
     """ make_cid: make_cid does not work if version 0 has incorrect codec """
     with pytest.raises(ValueError) as excinfo:
         make_cid(0, 'bin', b'multihash')
     assert 'codec for version 0' in str(excinfo.value)
예제 #29
0
 def test_multihash_invalid(self, value):
     """ make_cid: make_cid does not work if multihash type is invalid """
     with pytest.raises(ValueError) as excinfo:
         make_cid(1, 'dag-pb', value)
     assert 'invalid type for multihash' in str(excinfo.value)
예제 #30
0
 def test_codec_invalid(self):
     """ make_cid: make_cid does not work if codec is invalid """
     with pytest.raises(ValueError) as excinfo:
         make_cid(1, 'some-random-codec', b'multihash')
     assert 'invalid codec' in str(excinfo.value)