From 60b0c73fea796c70a4a1bdf2cd951a8de9c6f1ee Mon Sep 17 00:00:00 2001 From: Yang Chun Ung Date: Thu, 26 Sep 2024 13:42:08 +0900 Subject: [PATCH] Use avatar address instead of ranking --- tests/raid_test.py | 3 +-- world_boss/app/raid.py | 49 ++++++++++++++++++++++++++++++++--------- world_boss/app/tasks.py | 12 +++++++++- 3 files changed, 51 insertions(+), 13 deletions(-) diff --git a/tests/raid_test.py b/tests/raid_test.py index a26b9e1..715f070 100644 --- a/tests/raid_test.py +++ b/tests/raid_test.py @@ -395,7 +395,7 @@ def test_bulk_insert_transactions(fx_session): 3,25,0x01069aaf336e6aEE605a8A54D0734b43B62f8Fe4,5b65f5D0e23383FA18d74A62FbEa383c7D11F29d,560,RUNESTONE_FENRIR1,0,175 3,25,0x01069aaf336e6aEE605a8A54D0734b43B62f8Fe4,5b65f5D0e23383FA18d74A62FbEa383c7D11F29d,150,RUNESTONE_FENRIR2,0,175 3,25,0x01069aaf336e6aEE605a8A54D0734b43B62f8Fe4,5b65f5D0e23383FA18d74A62FbEa383c7D11F29d,40,RUNESTONE_FENRIR3,0,175 - 3,26,5b65f5D0e23383FA18d74A62FbEa383c7D11F29d,0x01069aaf336e6aEE605a8A54D0734b43B62f8Fe4,560,RUNESTONE_FENRIR1,0,175""" + 3,25,5b65f5D0e23383FA18d74A62FbEa383c7D11F29d,0x01069aaf336e6aEE605a8A54D0734b43B62f8Fe4,560,RUNESTONE_FENRIR1,0,175""" rows = [r.split(",") for r in content.split("\n")] nonce_rows_map = {175: rows} bulk_insert_transactions( @@ -423,7 +423,6 @@ def test_bulk_insert_transactions(fx_session): if i == 1: agent_address = "5b65f5D0e23383FA18d74A62FbEa383c7D11F29d" avatar_address = "0x01069aaf336e6aEE605a8A54D0734b43B62f8Fe4" - ranking = 26 amounts = [ ("RUNESTONE_FENRIR1", 560, 0), ] diff --git a/world_boss/app/raid.py b/world_boss/app/raid.py index 7de39bc..916702a 100644 --- a/world_boss/app/raid.py +++ b/world_boss/app/raid.py @@ -310,6 +310,15 @@ def create_unsigned_tx( plain_value: ActionPlainValue, timestamp: datetime.datetime, ) -> bytes: + """ + :param planet_id: planet id for genesis hash + :param public_key: signer public key + :param address: signer address + :param nonce: tx nonce + :param plain_value: tx actions plain value + :param timestamp: tx time stamp + :return: bencoded unsigned tx + """ if address.startswith("0x"): address = address[2:] return bencodex.dumps( @@ -340,6 +349,12 @@ def create_unsigned_tx( def append_signature_to_unsigned_tx(unsigned_tx: bytes, signature: bytes) -> bytes: + """ + sign unsigned tx + :param unsigned_tx: + :param signature: + :return: signed tx + """ decoded = bencodex.loads(unsigned_tx) decoded[b"S"] = signature return bencodex.dumps(decoded) @@ -373,6 +388,10 @@ def get_reward_count(db: Session, raid_id: int) -> int: def get_next_month_last_day() -> datetime.datetime: + """ + returns the last day of the next month following the call time to ensure tx validity. + if call this method 2024-09-xx return 2024-10-30 + """ # Today's date today = datetime.date.today() @@ -396,8 +415,17 @@ def bulk_insert_transactions( signer, memo: typing.Optional[str] = None, ): - # ranking : world_boss_reward - world_boss_rewards: dict[int, dict] = {} + """ + sign transfer-assets tx and save related models. + :param rows: target recipients. + :param nonce_rows_map: recipients group by tx nonce. + :param time_stamp: tx time stamp. + :param db: + :param signer: kms signer. + :param memo: tx memo. + """ + # avatar_address : world_boss_reward + world_boss_rewards: dict[str, dict] = {} signer_address = signer.address tx_values: List[dict] = [] tx_ids: dict[int, str] = {} @@ -425,7 +453,8 @@ def bulk_insert_transactions( exist_rankings = [ r for r, in db.query(WorldBossReward.ranking).filter_by(raid_id=raid_id) ] - world_boss_reward_amounts: dict[int, list[dict]] = {} + # avatar_address : list of world boss reward amount + world_boss_reward_amounts: dict[str, list[dict]] = {} # raid_id,ranking,agent_address,avatar_address,amount,ticker,decimal_places,target_nonce for row in rows: # parse row @@ -438,14 +467,14 @@ def bulk_insert_transactions( nonce = int(row[7]) # get or create world_boss_reward - if ranking not in exist_rankings and not world_boss_rewards.get(ranking): + if ranking not in exist_rankings and not world_boss_rewards.get(avatar_address): world_boss_reward = { "raid_id": raid_id, "ranking": ranking, "agent_address": agent_address, "avatar_address": avatar_address, } - world_boss_rewards[ranking] = world_boss_reward + world_boss_rewards[avatar_address] = world_boss_reward # create world_boss_reward_amount world_boss_reward_amount = { @@ -454,17 +483,17 @@ def bulk_insert_transactions( "ticker": ticker, "tx_id": tx_ids[nonce], } - if not world_boss_reward_amounts.get(ranking): - world_boss_reward_amounts[ranking] = [] - world_boss_reward_amounts[ranking].append(world_boss_reward_amount) + if not world_boss_reward_amounts.get(avatar_address): + world_boss_reward_amounts[avatar_address] = [] + world_boss_reward_amounts[avatar_address].append(world_boss_reward_amount) if world_boss_rewards: db.execute(insert(WorldBossReward), world_boss_rewards.values()) result = db.query(WorldBossReward).filter_by(raid_id=raid_id) values = [] for reward in result: exist_tickers = [i.ticker for i in reward.amounts] - if world_boss_rewards.get(reward.ranking): - for amounts in world_boss_reward_amounts[reward.ranking]: + if world_boss_rewards.get(reward.avatar_address): + for amounts in world_boss_reward_amounts[reward.avatar_address]: if amounts["ticker"] not in exist_tickers: amounts["reward_id"] = reward.id values.append(amounts) diff --git a/world_boss/app/tasks.py b/world_boss/app/tasks.py index 2b8f400..3952163 100644 --- a/world_boss/app/tasks.py +++ b/world_boss/app/tasks.py @@ -258,7 +258,13 @@ def check_season(): @celery.task() -def save_ranking_rewards(raid_id: int, size: int, offset: int, signer_address: str): +def save_ranking_rewards(raid_id: int, size: int, offset: int): + """ + + :param raid_id: + :param size: + :param offset: + """ results: List[RankingRewardWithAgentDictionary] = [] payload_size = size time_stamp = get_next_month_last_day() @@ -304,6 +310,10 @@ def save_ranking_rewards(raid_id: int, size: int, offset: int, signer_address: s @celery.task() def upload_tx_list(raid_id: int): + """ + upload signed tx csv data on Slack channel. + :param raid_id: target world boss season + """ cache_key = f"{raid_id}_uploaded" # 중복 업로드 방지 if cache_exists(cache_key):