From dc2cf9e061cee0246b3a6809d8b84e7bd879b95d Mon Sep 17 00:00:00 2001 From: ABW <andrzejl@syncad.com> Date: Tue, 11 Aug 2020 20:20:33 +0200 Subject: [PATCH] [ABW]: [Fix] payout_at/cashout_time changes with hardcoded 7 day cashout (compatible with current rules but not old - see issue #54) [Fix] basic fixes for bridge_api - added missing votes, removed extra selected fields, changed some field types etc. --- hive/db/schema.py | 20 ++++++------------ hive/indexer/posts.py | 3 --- hive/server/bridge_api/methods.py | 32 +++++++++++------------------ hive/server/bridge_api/objects.py | 23 ++++++++------------- hive/server/bridge_api/support.py | 4 ++-- hive/server/bridge_api/thread.py | 23 ++++++--------------- hive/server/database_api/methods.py | 3 +++ hive/version.py | 2 +- tests/tests_api | 2 +- 9 files changed, 39 insertions(+), 73 deletions(-) diff --git a/hive/db/schema.py b/hive/db/schema.py index 0b293d220..8017b60f9 100644 --- a/hive/db/schema.py +++ b/hive/db/schema.py @@ -451,7 +451,7 @@ def setup(db): category_id, root_author_id, root_permlink_id, is_muted, is_valid, - author_id, permlink_id, created_at, updated_at, active) + author_id, permlink_id, created_at, updated_at, active, payout_at, cashout_time) SELECT php.id AS parent_id, php.author_id as parent_author_id, php.permlink_id as parent_permlink_id, php.depth + 1 as depth, (CASE @@ -465,7 +465,7 @@ def setup(db): php.is_muted as is_muted, php.is_valid as is_valid, ha.id as author_id, hpd.id as permlink_id, _date as created_at, _date as updated_at, - _date as active + _date as active, (_date + INTERVAL '7 days') as payout_at, (_date + INTERVAL '7 days') as cashout_time FROM hive_accounts ha, hive_permlink_data hpd, hive_posts php @@ -482,11 +482,7 @@ def setup(db): active = _date, --- post undelete part (if was deleted) - is_deleted = (CASE hp.is_deleted - WHEN true THEN false - ELSE false - END - ), + is_deleted = false, is_pinned = (CASE hp.is_deleted WHEN true THEN false ELSE hp.is_pinned --- no change @@ -507,7 +503,7 @@ def setup(db): category_id, root_author_id, root_permlink_id, is_muted, is_valid, - author_id, permlink_id, created_at, updated_at, active) + author_id, permlink_id, created_at, updated_at, active, payout_at, cashout_time) SELECT 0 AS parent_id, 0 as parent_author_id, 0 as parent_permlink_id, 0 as depth, (CASE WHEN _date > _community_support_start_date THEN @@ -520,7 +516,7 @@ def setup(db): false as is_muted, true as is_valid, ha.id as author_id, hpd.id as permlink_id, _date as created_at, _date as updated_at, - _date as active + _date as active, (_date + INTERVAL '7 days') as payout_at, (_date + INTERVAL '7 days') as cashout_time FROM hive_accounts ha, hive_permlink_data hpd WHERE ha.name = _author and hpd.permlink = _permlink @@ -533,11 +529,7 @@ def setup(db): active = _date, --- post undelete part (if was deleted) - is_deleted = (CASE hp.is_deleted - WHEN true THEN false - ELSE false - END - ), + is_deleted = false, is_pinned = (CASE hp.is_deleted WHEN true THEN false ELSE hp.is_pinned --- no change diff --git a/hive/indexer/posts.py b/hive/indexer/posts.py index e008ecfb3..8d78609c9 100644 --- a/hive/indexer/posts.py +++ b/hive/indexer/posts.py @@ -297,9 +297,6 @@ class Posts: if ( is_paidout is not None ): payout_at = date cashout_time = "1969-12-31T23:59:59" - else: - if ( total_payout_value is not None ): - payout_at = date #Here should be `cashout_time` cls._comment_payout_ops.append("('{}', '{}', {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {})".format( author, diff --git a/hive/server/bridge_api/methods.py b/hive/server/bridge_api/methods.py index d1647ca60..99c906ba5 100644 --- a/hive/server/bridge_api/methods.py +++ b/hive/server/bridge_api/methods.py @@ -1,7 +1,8 @@ """Bridge API public endpoints for posts""" import hive.server.bridge_api.cursor as cursor -from hive.server.bridge_api.objects import load_posts, load_posts_reblogs, load_profiles, _condenser_post_object +from hive.server.bridge_api.objects import load_posts, load_posts_reblogs, load_profiles, _bridge_post_object +from hive.server.database_api.methods import find_votes, VotesPresentation from hive.server.common.helpers import ( return_error_info, valid_account, @@ -20,18 +21,15 @@ SQL_TEMPLATE = """ SELECT hp.id, hp.author, - hp.root_author, + hp.parent_author, hp.author_rep, - hp.allow_replies, - hp.allow_votes, - hp.allow_curation_rewards, hp.root_title, hp.beneficiaries, hp.max_accepted_payout, hp.percent_hbd, hp.url, hp.permlink, - hp.root_permlink, + hp.parent_permlink, hp.title, hp.body, hp.category, @@ -62,16 +60,6 @@ SQL_TEMPLATE = """ #pylint: disable=too-many-arguments, no-else-return -async def _get_post_id(db, author, permlink): - """Get post_id from hive db.""" - sql = """SELECT id FROM hive_posts - WHERE author_id = (SELECT id FROM hive_accounts WHERE name = :a) - AND permlink_id = (SELECT id FROM hive_permlink_data WHERE permlink = :p) - AND is_deleted = '0'""" - post_id = await db.query_one(sql, a=author, p=permlink) - assert post_id, 'invalid author/permlink' - return post_id - @return_error_info async def get_profile(context, account, observer=None): """Load account/profile data.""" @@ -120,7 +108,8 @@ async def get_post(context, author, permlink, observer=None): result = await db.query_all(sql, author=author, permlink=permlink) assert len(result) == 1, 'invalid author/permlink or post not found in cache' - post = _condenser_post_object(result[0]) + post = _bridge_post_object(result[0]) + post['active_votes'] = await find_votes({'db':db}, {'author':author, 'permlink':permlink}, VotesPresentation.BridgeApi) post = await append_statistics_to_post(post, result[0], False, blacklists_for_user) return post @@ -223,7 +212,8 @@ async def get_ranked_posts(context, sort, start_author='', start_permlink='', if pinned_sql: pinned_result = await db.query_all(pinned_sql, author=start_author, limit=limit, tag=tag, permlink=start_permlink, community_name=tag, observer=observer) for row in pinned_result: - post = _condenser_post_object(row) + post = _bridge_post_object(row) + post['active_votes'] = await find_votes({'db':db}, {'author':row['author'], 'permlink':row['permlink']}, VotesPresentation.BridgeApi) post = await append_statistics_to_post(post, row, True, blacklists_for_user) limit = limit - 1 posts.append(post) @@ -231,7 +221,8 @@ async def get_ranked_posts(context, sort, start_author='', start_permlink='', sql_result = await db.query_all(sql, author=start_author, limit=limit, tag=tag, permlink=start_permlink, community_name=tag, observer=observer) for row in sql_result: - post = _condenser_post_object(row) + post = _bridge_post_object(row) + post['active_votes'] = await find_votes({'db':db}, {'author':row['author'], 'permlink':row['permlink']}, VotesPresentation.BridgeApi) post = await append_statistics_to_post(post, row, False, blacklists_for_user) if post['post_id'] in pinned_post_ids: continue @@ -322,7 +313,8 @@ async def get_account_posts(context, sort, account, start_author='', start_perml blacklists_for_user = await Mutes.get_blacklists_for_observer(observer, context) sql_result = await db.query_all(sql, account=account, author=start_author, permlink=start_permlink, limit=limit) for row in sql_result: - post = _condenser_post_object(row) + post = _bridge_post_object(row) + post['active_votes'] = await find_votes({'db':db}, {'author':row['author'], 'permlink':row['permlink']}, VotesPresentation.BridgeApi) post = await append_statistics_to_post(post, row, False, blacklists_for_user) posts.append(post) return posts diff --git a/hive/server/bridge_api/objects.py b/hive/server/bridge_api/objects.py index ffe0232c8..693c66d8c 100644 --- a/hive/server/bridge_api/objects.py +++ b/hive/server/bridge_api/objects.py @@ -5,7 +5,7 @@ import ujson as json from hive.server.common.mutes import Mutes from hive.server.common.helpers import json_date -from hive.server.database_api.methods import find_votes +from hive.server.database_api.methods import find_votes, VotesPresentation from hive.utils.normalize import sbd_amount from hive.indexer.votes import Votes @@ -100,7 +100,7 @@ async def load_posts_keyed(db, ids, truncate_body=0): author_ids[author['id']] = author['name'] row['author_rep'] = author['reputation'] - post = _condenser_post_object(row, truncate_body=truncate_body) + post = _bridge_post_object(row, truncate_body=truncate_body) post['active_votes'] = await find_votes({'db':db}, {'author':row['author'], 'permlink':row['permlink']}) post['blacklists'] = Mutes.lists(post['author'], author['reputation']) @@ -219,7 +219,7 @@ def _condenser_profile_object(row): 'profile_image': row['profile_image'], }}} -def _condenser_post_object(row, truncate_body=0): +def _bridge_post_object(row, truncate_body=0): """Given a hive_posts row, create a legacy-style post object.""" paid = row['is_paidout'] @@ -231,7 +231,7 @@ def _condenser_post_object(row, truncate_body=0): post['title'] = row['title'] post['body'] = row['body'][0:truncate_body] if truncate_body else row['body'] - post['json_metadata'] = row['json'] + post['json_metadata'] = json.loads(row['json']) post['created'] = json_date(row['created_at']) post['updated'] = json_date(row['updated_at']) @@ -245,37 +245,30 @@ def _condenser_post_object(row, truncate_body=0): post['pending_payout_value'] = _amount(0 if paid else row['payout']) post['author_payout_value'] = _amount(row['payout'] if paid else 0) post['curator_payout_value'] = _amount(0) - post['promoted'] = float(row['promoted']) + post['promoted'] = _amount(row['promoted']) post['replies'] = [] +# ABW: missing post['active_votes'] = _hydrate_active_votes(row['votes']) post['author_reputation'] = float(row['author_rep']) post['stats'] = { 'hide': row['is_hidden'], 'gray': row['is_grayed'], - 'total_votes': Votes.get_vote_count(row['author'], row['permlink']), + 'total_votes': Votes.get_vote_count(row['author'], row['permlink']), # ABW: incorrect calculation (possibly needs to exclude blacklisted votes) 'flag_weight': float(row['flag_weight'])} # TODO: down_weight #post['author_reputation'] = rep_to_raw(row['author_rep']) - post['root_author'] = row['root_author'] - post['root_permlink'] = row['root_permlink'] - - post['allow_replies'] = row['allow_replies'] - post['allow_votes'] = row['allow_votes'] - post['allow_curation_rewards'] = row['allow_curation_rewards'] - post['url'] = row['url'] - post['root_title'] = row['root_title'] post['beneficiaries'] = row['beneficiaries'] post['max_accepted_payout'] = row['max_accepted_payout'] post['percent_hbd'] = row['percent_hbd'] if paid: curator_payout = sbd_amount(row['curator_payout_value']) + post['author_payout_value'] = _amount(row['payout'] - curator_payout) post['curator_payout_value'] = _amount(curator_payout) - post['total_payout_value'] = _amount(row['payout'] - curator_payout) # TODO: re-evaluate if row['depth'] > 0: diff --git a/hive/server/bridge_api/support.py b/hive/server/bridge_api/support.py index 77e81671a..b7c5f89c2 100644 --- a/hive/server/bridge_api/support.py +++ b/hive/server/bridge_api/support.py @@ -4,7 +4,7 @@ import logging #import ujson as json import traceback -from hive.server.bridge_api.objects import _condenser_post_object +from hive.server.bridge_api.objects import _bridge_post_object from hive.utils.post import post_to_internal from hive.utils.normalize import sbd_amount from hive.server.common.helpers import ( @@ -96,7 +96,7 @@ async def normalize_post(context, post): try: if 'promoted' not in row: row['promoted'] = 0 row['author_rep'] = author['reputation'] - ret = _condenser_post_object(row) + ret = _bridge_post_object(row) except Exception as e: log.error("post_to_internal: %s %s", repr(e), traceback.format_exc()) raise e diff --git a/hive/server/bridge_api/thread.py b/hive/server/bridge_api/thread.py index ad5a1e389..df547f193 100644 --- a/hive/server/bridge_api/thread.py +++ b/hive/server/bridge_api/thread.py @@ -2,8 +2,9 @@ import logging -from hive.server.bridge_api.objects import load_posts_keyed, _condenser_post_object +from hive.server.bridge_api.objects import load_posts_keyed, _bridge_post_object from hive.server.bridge_api.methods import append_statistics_to_post +from hive.server.database_api.methods import find_votes, VotesPresentation from hive.server.common.helpers import ( return_error_info, valid_account, @@ -91,7 +92,8 @@ async def get_discussion(context, author, permlink, observer=None): return {} root_id = rows[0]['id'] all_posts = {} - root_post = _condenser_post_object(rows[0]) + root_post = _bridge_post_object(rows[0]) + root_post['active_votes'] = await find_votes({'db':db}, {'author':rows[0]['author'], 'permlink':rows[0]['permlink']}, VotesPresentation.BridgeApi) root_post = await append_statistics_to_post(root_post, rows[0], False, blacklists_for_user) root_post['replies'] = [] all_posts[root_id] = root_post @@ -101,7 +103,8 @@ async def get_discussion(context, author, permlink, observer=None): for index in range(1, len(rows)): id_to_parent_id_map[rows[index]['id']] = rows[index]['parent_id'] - post = _condenser_post_object(rows[index]) + post = _bridge_post_object(rows[index]) + post['active_votes'] = await find_votes({'db':db}, {'author':rows[index]['author'], 'permlink':rows[index]['permlink']}, VotesPresentation.BridgeApi) post = await append_statistics_to_post(post, rows[index], False, blacklists_for_user) post['replies'] = [] all_posts[post['post_id']] = post @@ -138,20 +141,6 @@ def get_children(parent_id, posts): results.append(key) return results -async def _get_post_id(db, author, permlink): - """Given an author/permlink, retrieve the id from db.""" - sql = """ - SELECT - id - FROM hive_posts hp - INNER JOIN hive_accounts ha_a ON ha_a.id = hp.author_id - INNER JOIN hive_permlink_data hpd_p ON hpd_p.id = hp.permlink_id - WHERE ha_a.name = :author - AND hpd_p.permlink = :permlink - AND is_deleted = '0' - LIMIT 1""" - return await db.query_one(sql, a=author, p=permlink) - def _ref(post): return post['author'] + '/' + post['permlink'] diff --git a/hive/server/database_api/methods.py b/hive/server/database_api/methods.py index 1cdf4a934..3dc0b6cbb 100644 --- a/hive/server/database_api/methods.py +++ b/hive/server/database_api/methods.py @@ -143,6 +143,7 @@ class VotesPresentation(Enum): ActiveVotes = 1 DatabaseApi = 2 CondenserApi = 3 + BridgeApi = 4 @return_error_info async def find_votes(context, params: dict, votes_presentation = VotesPresentation.DatabaseApi): @@ -180,6 +181,8 @@ async def find_votes(context, params: dict, votes_presentation = VotesPresentati elif votes_presentation == VotesPresentation.CondenserApi: ret.append(dict(percent=str(row.percent), reputation=rep_to_raw(row.reputation), rshares=str(row.rshares), voter=row.voter)) + elif votes_presentation == VotesPresentation.BridgeApi: + ret.append(dict(rshares=str(row.rshares), voter=row.voter)) else: ret.append(dict(percent=row.percent, reputation=rep_to_raw(row.reputation), rshares=number_to_json_value(row.rshares), time=time_string_with_t(row.time), diff --git a/hive/version.py b/hive/version.py index ef185d101..1bf106371 100644 --- a/hive/version.py +++ b/hive/version.py @@ -1,4 +1,4 @@ # generated by setup.py # contents will be overwritten VERSION = '0.0.1' -GIT_REVISION = '0dfcad4' +GIT_REVISION = 'a1f4d0b' diff --git a/tests/tests_api b/tests/tests_api index 3cca13717..201f1b41a 160000 --- a/tests/tests_api +++ b/tests/tests_api @@ -1 +1 @@ -Subproject commit 3cca13717750c6032f2a1efbaae6f1bdf0d151c8 +Subproject commit 201f1b41ab44c6262292a388cde923c71d328350 -- GitLab