def load_user(record_hash):
    """
    Load a user record from the storage implementation with the given hex string hash,
    The user record hash should have been loaded from the blockchain, and thereby be the
    authentic hash.

    Return the user record on success
    Return None on error
    """

    user_json = storage.get_immutable_data(record_hash)
    if user_json is None:
        log.error("Failed to load user record '%s'" % record_hash)
        return None

    # verify integrity
    user_record_hash = storage.get_data_hash(user_json)
    if user_record_hash != record_hash:
        log.error(
            "Profile hash mismatch: expected '%s', got '%s'" % record_hash,
            user_record_hash)
        return None

    user = user_db.parse_user(user_json)
    return user
Exemplo n.º 2
0
def read_file(file_name):
    """ Read CSV file containing users
        :param file_name: input file name
        :returns: dictionary with user ids as keys, and User objects as values
    """
    dict = {}
    # read file, split lines
    with codecs.open(file_name, "r", "utf-8") as f:
        lines = f.read().split('\n')
        for line in lines:
            if line.strip():
                try:
                    # parse user
                    u = user.parse_user(line)
                    # put it in dictionary
                    dict[u.id] = u
                except ValueError, e:
                    print 'Error: ' + str(e) # just print and continue
        return UserGraph(dict)
Exemplo n.º 3
0
    def test_users(self):
        users = [user.parse_user(x) for x in self.user_strings]
        # test __str__
        self.assertEqual([str(u) for u in users], self.user_strings)
        # test user graph
        dict = {u.id:u for u in users}
        graph = user_graph.UserGraph(dict)
        self.assertEqual(graph.shortest_distance(1,2), [2])
        self.assertEqual(graph.shortest_distance(1,4), [2,3,4])
        self.assertEqual(graph.shortest_distance(4,1), [3,2,1])
        self.assertEqual(graph.shortest_distance(5,1), None)
        # test shortest_distance2
        #for i in range(1,6):
        #    for j in range(1,6):
        #        self.assertEqual(graph.shortest_distance(i,j), graph.shortest_distance2(i,j))
        # test friends
        self.assertEqual(graph.is_friend(1,2), True)
        self.assertEqual(graph.is_friend(1,3), False)

        graph2 = user_graph.read_file('data.txt')
        for i in range(1,20):
            for j in reversed(range(1,20)):
                self.assertEqual(graph2.shortest_distance(i,j), graph2.shortest_distance_old(i,j))
def load_user(record_hash):
    """
    Load a user record from the storage implementation with the given hex string hash,
    The user record hash should have been loaded from the blockchain, and thereby be the
    authentic hash.

    Return the user record on success
    Return None on error
    """

    user_json = storage.get_immutable_data(record_hash)
    if user_json is None:
        log.error("Failed to load user record '%s'" % record_hash)
        return None

    # verify integrity
    user_record_hash = storage.get_data_hash(user_json)
    if user_record_hash != record_hash:
        log.error("Profile hash mismatch: expected '%s', got '%s'" % record_hash, user_record_hash)
        return None

    user = user_db.parse_user(user_json)
    return user
def update(name, user_json_or_hash, privatekey, txid=None, proxy=None, tx_only=False, public_key=None, subsidy_key=None):
    """
    update

    Optionally supply a txid.  The reason for doing so is to try to replicate user
    data to new storage systems, or to recover from a transient error encountered
    earlier.
    """

    # sanity check
    if privatekey is None and public_key is None:
        return {'error': 'Missing public and private key'}

    if proxy is None:
        proxy = get_default_proxy()

    user_record_hash = None
    user_data = None

    # 160-bit hex string?
    if len(user_json_or_hash) == 40 and len(user_json_or_hash.translate(None, "0123456789ABCDEFabcdef")) == 0:

        user_record_hash = user_json_or_hash.lower()
    else:

        # user record json.  hash it
        user_data = user_db.parse_user(user_json_or_hash)
        if user_data is None:
            return {'error': 'Invalid user record JSON'}

        user_record_hash = pybitcoin.hash.hex_hash160(user_db.serialize_user(user_data))

    # go get the current user record
    current_user_record = get_name_record( name, create_if_absent=True )
    if current_user_record is None:
        return {'error': 'No such user'}

    if current_user_record.has_key('error'):
        # some other error
        return current_user_record

    result = {}

    old_hash = pybitcoin.hash.hex_hash160(user_db.serialize_user(user_data))

    # only want transaction?
    if tx_only:

        if privatekey is None and public_key is not None and subsidy_key is not None:
            return proxy.update_tx_subsidized( name, user_record_hash, public_key, subsidy_key )

        else:
            return proxy.update_tx( name, user_record_hash, privatekey )


    # no transaction: go put one
    if txid is None:

        if tx_only:
            result = proxy.update_tx( name, user_record_hash, privatekey )
            return result

        else:
            result = proxy.update(name, user_record_hash, privatekey)

        if 'error' in result:
            # failed
            return result

        if 'transaction_hash' not in result:
            # failed
            result['error'] = "No transaction hash given"
            return result

        txid = result['transaction_hash']

    else:

        # embed the txid into the result nevertheless
        result['transaction_hash'] = txid

    # store new user data
    rc = True
    new_data_hash = None
    if user_data is not None:
        rc, new_data_hash = store_name_record(user_data, txid)

    else:
        # was already a hash
        new_data_hash = user_json_or_hash

    if not rc:
        result['error'] = "Failed to store updated user record."
        return result

    result['status'] = True
    result['value_hash'] = new_data_hash
    result['transaction_hash'] = txid

    return result
def update(name,
           user_json_or_hash,
           privatekey,
           txid=None,
           proxy=None,
           tx_only=False,
           public_key=None,
           subsidy_key=None):
    """
    update

    Optionally supply a txid.  The reason for doing so is to try to replicate user
    data to new storage systems, or to recover from a transient error encountered
    earlier.
    """

    # sanity check
    if privatekey is None and public_key is None:
        return {'error': 'Missing public and private key'}

    if proxy is None:
        proxy = get_default_proxy()

    user_record_hash = None
    user_data = None

    # 160-bit hex string?
    if len(user_json_or_hash) == 40 and len(
            user_json_or_hash.translate(None, "0123456789ABCDEFabcdef")) == 0:

        user_record_hash = user_json_or_hash.lower()
    else:

        # user record json.  hash it
        user_data = user_db.parse_user(user_json_or_hash)
        if user_data is None:
            return {'error': 'Invalid user record JSON'}

        user_record_hash = pybitcoin.hash.hex_hash160(
            user_db.serialize_user(user_data))

    # go get the current user record
    current_user_record = get_name_record(name, create_if_absent=True)
    if current_user_record is None:
        return {'error': 'No such user'}

    if current_user_record.has_key('error'):
        # some other error
        return current_user_record

    result = {}

    old_hash = pybitcoin.hash.hex_hash160(user_db.serialize_user(user_data))

    # only want transaction?
    if tx_only:

        if privatekey is None and public_key is not None and subsidy_key is not None:
            return proxy.update_tx_subsidized(name, user_record_hash,
                                              public_key, subsidy_key)

        else:
            return proxy.update_tx(name, user_record_hash, privatekey)

    # no transaction: go put one
    if txid is None:

        if tx_only:
            result = proxy.update_tx(name, user_record_hash, privatekey)
            return result

        else:
            result = proxy.update(name, user_record_hash, privatekey)

        if 'error' in result:
            # failed
            return result

        if 'transaction_hash' not in result:
            # failed
            result['error'] = "No transaction hash given"
            return result

        txid = result['transaction_hash']

    else:

        # embed the txid into the result nevertheless
        result['transaction_hash'] = txid

    # store new user data
    rc = True
    new_data_hash = None
    if user_data is not None:
        rc, new_data_hash = store_name_record(user_data, txid)

    else:
        # was already a hash
        new_data_hash = user_json_or_hash

    if not rc:
        result['error'] = "Failed to store updated user record."
        return result

    result['status'] = True
    result['value_hash'] = new_data_hash
    result['transaction_hash'] = txid

    return result