From ac5010b852b7f420d6112610a00abc5712160823 Mon Sep 17 00:00:00 2001
From: mtrela <mtrela@syncad.com>
Date: Fri, 4 Sep 2020 15:09:37 +0200
Subject: [PATCH] Avoiding a redundant loop during
 `effective_comment_vote_operation` processing

---
 hive/indexer/blocks.py | 16 ++++------------
 hive/indexer/votes.py  | 31 ++++++++++++++++++-------------
 2 files changed, 22 insertions(+), 25 deletions(-)

diff --git a/hive/indexer/blocks.py b/hive/indexer/blocks.py
index eb4f65975..67e2a6e8a 100644
--- a/hive/indexer/blocks.py
+++ b/hive/indexer/blocks.py
@@ -116,8 +116,6 @@ class Blocks:
 
     @staticmethod
     def prepare_vops(comment_payout_ops, vopsList, date, block_num):
-        vote_ops = {}
-
         ineffective_deleted_ops = {}
         registered_ops_stats = [ 'author_reward_operation', 'comment_reward_operation', 'effective_comment_vote_operation', 'comment_payout_update_operation', 'ineffective_delete_comment_operation']
 
@@ -146,8 +144,7 @@ class Blocks:
                 comment_payout_ops[key][op_type] = ( op_value, date )
 
             elif op_type == 'effective_comment_vote_operation':
-                key_vote = "{}/{}/{}".format(op_value['voter'], op_value['author'], op_value['permlink'])
-                vote_ops[ key_vote ] = op_value
+                Votes.effective_comment_vote_op( op_value )
 
                 if key not in comment_payout_ops:
                     comment_payout_ops[key] = { 'author_reward_operation':None, 'comment_reward_operation':None, 'effective_comment_vote_operation':None, 'comment_payout_update_operation':None }
@@ -166,7 +163,7 @@ class Blocks:
             if op_type in registered_ops_stats:
                 OPSM.op_stats(op_type, OPSM.stop(start))
 
-        return (vote_ops, ineffective_deleted_ops)
+        return ineffective_deleted_ops
 
 
     @classmethod
@@ -184,16 +181,15 @@ class Blocks:
         if cls._head_block_date is None:
             cls._head_block_date = cls._current_block_date
 
-        vote_ops                = None
         comment_payout_stats    = None
         ineffective_deleted_ops = None
 
         if is_initial_sync:
             if num in virtual_operations:
-                (vote_ops, ineffective_deleted_ops ) = Blocks.prepare_vops(Posts.comment_payout_ops, virtual_operations[num], cls._current_block_date, num)
+                ineffective_deleted_ops = Blocks.prepare_vops(Posts.comment_payout_ops, virtual_operations[num], cls._current_block_date, num)
         else:
             vops = hived.get_virtual_operations(num)
-            (vote_ops, ineffective_deleted_ops ) = Blocks.prepare_vops(Posts.comment_payout_ops, vops, cls._current_block_date, num)
+            ineffective_deleted_ops = Blocks.prepare_vops(Posts.comment_payout_ops, vops, cls._current_block_date, num)
 
         json_ops = []
         for tx_idx, tx in enumerate(block['transactions']):
@@ -258,10 +254,6 @@ class Blocks:
         if json_ops:
             CustomOp.process_ops(json_ops, num, cls._head_block_date)
 
-        if vote_ops is not None:
-            for k, v in vote_ops.items():
-                Votes.effective_comment_vote_op(k, v)
-
         cls._head_block_date = cls._current_block_date
 
         return num
diff --git a/hive/indexer/votes.py b/hive/indexer/votes.py
index a2d82e99e..07952303d 100644
--- a/hive/indexer/votes.py
+++ b/hive/indexer/votes.py
@@ -27,12 +27,11 @@ class Votes:
             log.exception("Adding new vote-info into '_votes_data' dict")
             raise RuntimeError("Fatal error")
 
-        key = voter + "/" + author + "/" + permlink
+        key = "{}/{}/{}".format(voter, author, permlink)
 
         if key in cls._votes_data:
             cls._votes_data[key]["vote_percent"] = weight
             cls._votes_data[key]["last_update"] = date
-            cls._votes_data[key]["block_num"] = block_num
         else:
             cls._votes_data[key] = dict(voter=voter,
                                         author=author,
@@ -45,20 +44,26 @@ class Votes:
                                         block_num=block_num)
 
     @classmethod
-    def effective_comment_vote_op(cls, key, vop):
+    def effective_comment_vote_op(cls, vop):
         """ Process effective_comment_vote_operation """
 
-        if cls.inside_flush:
-            log.exception("Updating data in '_votes_data' using effective comment")
-            raise RuntimeError("Fatal error")
-
-        assert key in cls._votes_data
-
-        cls._votes_data[key]["weight"]       = vop["weight"]
-        cls._votes_data[key]["rshares"]      = vop["rshares"]
-        cls._votes_data[key]["is_effective"] = True
-        cls._votes_data[key]["block_num"]    = vop['block_num']
+        key = "{}/{}/{}".format(vop['voter'], vop['author'], vop['permlink'])
 
+        if key in cls._votes_data:
+          cls._votes_data[key]["weight"]       = vop["weight"]
+          cls._votes_data[key]["rshares"]      = vop["rshares"]
+          cls._votes_data[key]["is_effective"] = True
+          cls._votes_data[key]["block_num"]    = vop['block_num']
+        else:
+            cls._votes_data[key] = dict(voter=vop['voter'],
+                                        author=vop['author'],
+                                        permlink=vop['permlink'],
+                                        vote_percent=0,
+                                        weight=vop["weight"],
+                                        rshares=vop["rshares"],
+                                        last_update='1970-01-01 00:00:00',
+                                        is_effective=True,
+                                        block_num=vop['block_num'])
     @classmethod
     def flush(cls):
         """ Flush vote data from cache to database """
-- 
GitLab