|
| 1 | +import sys |
| 2 | +sys.path.append('./') |
| 3 | + |
| 4 | + |
| 5 | +from substrateinterface import SubstrateInterface |
| 6 | +from peaq.utils import get_block_height, get_block_hash |
| 7 | +import argparse |
| 8 | +from collections import Counter |
| 9 | +from tools.utils import get_all_events |
| 10 | +from peaq.utils import get_account_balance |
| 11 | +import pprint |
| 12 | +pp = pprint.PrettyPrinter(indent=4) |
| 13 | + |
| 14 | + |
| 15 | +POT_ADDR = '5EYCAe5cKPAoFh2HnQQvpKqRYZGqBpaA87u4Zzw89qPE58is' |
| 16 | +ALLOW_ERROR_PERCENTAGE = 1e-6 |
| 17 | + |
| 18 | + |
| 19 | +def get_current_collator(substrate, num=80): |
| 20 | + now_block_height = get_block_height(substrate) |
| 21 | + collators = [] |
| 22 | + for i in range(num): |
| 23 | + print(f'get author in block height: {now_block_height - i}') |
| 24 | + block_hash = get_block_hash(substrate, now_block_height - i) |
| 25 | + block_info = substrate.get_block(block_hash, include_author=True) |
| 26 | + collators.append(block_info['author']) |
| 27 | + return Counter(collators) |
| 28 | + |
| 29 | + |
| 30 | +def get_session_validator(substrate): |
| 31 | + session_info = substrate.query( |
| 32 | + module='Session', |
| 33 | + storage_function='Validators', |
| 34 | + params=[], |
| 35 | + ) |
| 36 | + return set(session_info.value) |
| 37 | + |
| 38 | + |
| 39 | +# Only for the token economy V1 |
| 40 | +# flake8: noqa: C901 |
| 41 | +if __name__ == '__main__': |
| 42 | + parser = argparse.ArgumentParser(description='Get storage and constants from a Substrate chain') |
| 43 | + parser.add_argument('-r', '--runtime', type=str, required=True, help='Your runtime websocket endpoint') |
| 44 | + parser.add_argument('-n', '--num', type=int, default=5, help='Number of blocks to check') |
| 45 | + parser.add_argument('-p', '--percentage', type=int, required=True, help='Collator Delegator rate') |
| 46 | + parser.add_argument('-c', '--coefficient', type=int, required=True, help='Collator/Delegator coefficient') |
| 47 | + |
| 48 | + args = parser.parse_args() |
| 49 | + |
| 50 | + if args.percentage < 0 or args.percentage > 100: |
| 51 | + raise ValueError(f'Percentage should be between 0 and 100, but got {args.percentage}') |
| 52 | + |
| 53 | + substrate = SubstrateInterface( |
| 54 | + url=args.runtime, |
| 55 | + ) |
| 56 | + |
| 57 | + target_block_num = args.num |
| 58 | + now_block_height = get_block_height(substrate) |
| 59 | + now_block_hash = get_block_hash(substrate, now_block_height) |
| 60 | + if now_block_height < target_block_num: |
| 61 | + raise ValueError(f'Block height is {now_block_height}, less than target block number {target_block_num}') |
| 62 | + |
| 63 | + # get collator/delegator issuance number |
| 64 | + total_reward = substrate.query( |
| 65 | + module='InflationManager', |
| 66 | + storage_function='BlockRewards', |
| 67 | + params=[], |
| 68 | + ) |
| 69 | + collator_delegator_issuance = total_reward.value * args.percentage / 100 |
| 70 | + |
| 71 | + for block_height in range(now_block_height - target_block_num, now_block_height): |
| 72 | + print(f'Block height: {block_height}') |
| 73 | + # get block data |
| 74 | + block_hash = get_block_hash(substrate, block_height) |
| 75 | + previous_block_hash = get_block_hash(substrate, block_height - 1) |
| 76 | + block_author = substrate.get_block(block_hash, include_author=True)['author'] |
| 77 | + print(f'block hash: {block_hash}, previous block hash: {previous_block_hash} block author: {block_author}') |
| 78 | + |
| 79 | + pot = get_account_balance(substrate, POT_ADDR, previous_block_hash) |
| 80 | + |
| 81 | + # Get staking info |
| 82 | + staking_info = {} |
| 83 | + info = substrate.query( |
| 84 | + module='ParachainStaking', |
| 85 | + storage_function='CandidatePool', |
| 86 | + params=[block_author], |
| 87 | + block_hash=previous_block_hash, |
| 88 | + ) |
| 89 | + staking_info[block_author] = info.value |
| 90 | + staking_info[block_author]['delegators'] = { |
| 91 | + delegator['owner']: delegator['amount'] |
| 92 | + for delegator in info.value['delegators'] |
| 93 | + } |
| 94 | + print(f'staking info: {staking_info}') |
| 95 | + |
| 96 | + # get block reward |
| 97 | + result = get_all_events( |
| 98 | + substrate, |
| 99 | + block_hash, |
| 100 | + 'ParachainStaking', 'Rewarded') |
| 101 | + reward_info = { |
| 102 | + event.value['attributes'][0]: event.value['attributes'][1] |
| 103 | + for event in result |
| 104 | + } |
| 105 | + print(f'Block height: {block_height}, block author: {block_author}, reward info: {reward_info}, staking info: {staking_info}, pot: {pot}') |
| 106 | + |
| 107 | + # Check total |
| 108 | + total_reward = sum(reward_info.values()) |
| 109 | + # Note float comparing |
| 110 | + if float(abs(total_reward - pot)) / pot > ALLOW_ERROR_PERCENTAGE: |
| 111 | + raise ValueError(f'Total reward is not equal to collator/delegator issuance {collator_delegator_issuance}, got {total_reward}') |
| 112 | + |
| 113 | + denominator = args.coefficient * \ |
| 114 | + staking_info[block_author]['stake'] + \ |
| 115 | + sum(staking_info[block_author]['delegators'].values()) |
| 116 | + # Calculate the collator reward |
| 117 | + real_collator_reward = reward_info[block_author] |
| 118 | + target_collator_reward = collator_delegator_issuance * \ |
| 119 | + float(args.coefficient) * staking_info[block_author]['stake'] / denominator |
| 120 | + if float(abs(real_collator_reward - target_collator_reward)) / target_collator_reward > ALLOW_ERROR_PERCENTAGE: |
| 121 | + raise ValueError(f'Collator reward is not equal to target reward {target_collator_reward}, got {real_collator_reward}') |
| 122 | + # Calculate the delegator reward |
| 123 | + for addr, value in reward_info.items(): |
| 124 | + if addr == block_author: |
| 125 | + continue |
| 126 | + real_delegator_reward = value |
| 127 | + target_delegator_reward = collator_delegator_issuance * \ |
| 128 | + float(staking_info[block_author]['delegators'][addr]) / denominator |
| 129 | + if float(abs(real_delegator_reward - target_delegator_reward)) / target_delegator_reward > ALLOW_ERROR_PERCENTAGE: |
| 130 | + raise ValueError(f'Delegator reward is not equal to target reward {target_delegator_reward}, got {real_delegator_reward}') |
0 commit comments