def calculate(self, reward_data3, total_amount): new_rewards = [] # move skipped records to next phase for rl3 in self.iterateskipped(reward_data3): new_rewards.append(rl3) for rl3 in self.filterskipped(reward_data3): if rl3.type == TYPE_FOUNDERS_PARENT: for addr, ratio in self.founders_map.items(): rl4 = RewardLog(addr, TYPE_FOUNDER, 0, 0) # new ratio is parent ratio * ratio of the founder rl4.ratio = ratio * rl3.ratio rl4.ratio4 = rl4.ratio rl4.service_fee_ratio = 0 rl4.service_fee_rate = 0 rl4.parent = rl3 new_rewards.append(rl4) # if no founders, add parent object to rewards list if not self.founders_map.items(): new_rewards.append(rl3) elif rl3.type == TYPE_OWNERS_PARENT: for addr, ratio in self.owners_map.items(): rl4 = RewardLog(addr, TYPE_OWNER, ratio * rl3.staking_balance, 0) # new ratio is parent ratio * ratio of the owner rl4.ratio = ratio * rl3.ratio rl4.ratio4 = rl4.ratio rl4.service_fee_ratio = 0 rl4.service_fee_rate = 0 rl4.parent = rl3 new_rewards.append(rl4) # if no owners, add parent object to rewards list if not self.owners_map.items(): new_rewards.append(rl3) else: rl3.ratio4 = rl3.ratio new_rewards.append(rl3) return new_rewards, total_amount
def test_calculate(self): rewards = [] ratios = [0.25, 0.05, 0.3, 0.15, 0.25] total_reward = 1000 for i, ratio in enumerate(ratios, start=1): rl0 = RewardLog( address="addr" + str(i), type="D", staking_balance=total_reward * ratio, current_balance=0, ) rl0.ratio = ratio rl0.ratio4 = ratio rewards.append(rl0) rewards.append(RewardLog("addrdummy", "D", 0, 0).skip("skipped for testing", 4)) phaseMapping = CalculatePhaseMapping() new_rewards = phaseMapping.calculate(rewards, {"addr2": "addr1"}) # filter out skipped records new_rewards = list(rl for rl in new_rewards if not rl.skipped) # check new ratios sum up to 1 # old and new reward amount is the same ratio_sum = sum(rl.ratio5 for rl in new_rewards) # payment address for address2 is address1 payment_address_set = set(rl.paymentaddress for rl in new_rewards) self.assertEqual(4, len(payment_address_set)) self.assertAlmostEqual(1.0, ratio_sum, delta=ALMOST_ZERO) # ratio of records having payment address addr1 must be 0.30 (0.25+0.05) self.assertAlmostEqual( 0.30, sum( rl.ratio for rl in list( filter( lambda rl: rl.paymentaddress == "addr1", filter(lambda rl: not rl.skipped, new_rewards), ) ) ), delta=ALMOST_ZERO, )
def test_calculate(self): rewards = [] ratios = [0.25, 0.05, 0.3, 0.15, 0.25] total_reward = 1000 for i, ratio in enumerate(ratios, start=1): rl0 = RewardLog(address="addr" + str(i), type="D", balance=total_reward * ratio) rl0.ratio4 = ratio rewards.append(rl0) rewards[0].address = "addr1" rewards[1].address = "addr1" rewards.append( RewardLog("addrdummy", "D", 0).skip("skipped for testing", 4)) phase5 = CalculatePhase5() new_rewards, new_total_reward = phase5.calculate(rewards, total_reward) # filter out skipped records new_rewards = list(rl for rl in new_rewards if not rl.skipped) # new_total_reward = total_reward self.assertEqual(total_reward, new_total_reward) # check new ratios sum up to 1 # old and new reward amount is the same ratio_sum = sum(rl.ratio5 for rl in new_rewards) # 2 records are merged self.assertEqual(4, len(new_rewards)) self.assertAlmostEqual(1.0, ratio_sum, delta=1e-6) # ratio of merged record must be 0.30 (0.25+0.05) self.assertAlmostEqual(0.30, list( filter( lambda rl: rl.type == TYPE_MERGED, filter(lambda rl: not rl.skipped, new_rewards)))[0].ratio5, delta=1e-6)