forked from sfffaaa/peaq-bc-test
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #76 from peaqnetwork/feat/1208539891229773_typo
Fix typo
- Loading branch information
Showing
7 changed files
with
269 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -52,7 +52,7 @@ | |
"type": "bytes" | ||
} | ||
], | ||
"name": "RemoveAttribte", | ||
"name": "RemoveAttribute", | ||
"type": "event" | ||
}, | ||
{ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,130 @@ | ||
import sys | ||
sys.path.append('./') | ||
|
||
|
||
from substrateinterface import SubstrateInterface | ||
from peaq.utils import get_block_height, get_block_hash | ||
import argparse | ||
from collections import Counter | ||
from tools.utils import get_all_events | ||
from peaq.utils import get_account_balance | ||
import pprint | ||
pp = pprint.PrettyPrinter(indent=4) | ||
|
||
|
||
POT_ADDR = '5EYCAe5cKPAoFh2HnQQvpKqRYZGqBpaA87u4Zzw89qPE58is' | ||
ALLOW_ERROR_PERCENTAGE = 1e-6 | ||
|
||
|
||
def get_current_collator(substrate, num=80): | ||
now_block_height = get_block_height(substrate) | ||
collators = [] | ||
for i in range(num): | ||
print(f'get author in block height: {now_block_height - i}') | ||
block_hash = get_block_hash(substrate, now_block_height - i) | ||
block_info = substrate.get_block(block_hash, include_author=True) | ||
collators.append(block_info['author']) | ||
return Counter(collators) | ||
|
||
|
||
def get_session_validator(substrate): | ||
session_info = substrate.query( | ||
module='Session', | ||
storage_function='Validators', | ||
params=[], | ||
) | ||
return set(session_info.value) | ||
|
||
|
||
# Only for the token economy V1 | ||
# flake8: noqa: C901 | ||
if __name__ == '__main__': | ||
parser = argparse.ArgumentParser(description='Get storage and constants from a Substrate chain') | ||
parser.add_argument('-r', '--runtime', type=str, required=True, help='Your runtime websocket endpoint') | ||
parser.add_argument('-n', '--num', type=int, default=5, help='Number of blocks to check') | ||
parser.add_argument('-p', '--percentage', type=int, required=True, help='Collator Delegator rate') | ||
parser.add_argument('-c', '--coefficient', type=int, required=True, help='Collator/Delegator coefficient') | ||
|
||
args = parser.parse_args() | ||
|
||
if args.percentage < 0 or args.percentage > 100: | ||
raise ValueError(f'Percentage should be between 0 and 100, but got {args.percentage}') | ||
|
||
substrate = SubstrateInterface( | ||
url=args.runtime, | ||
) | ||
|
||
target_block_num = args.num | ||
now_block_height = get_block_height(substrate) | ||
now_block_hash = get_block_hash(substrate, now_block_height) | ||
if now_block_height < target_block_num: | ||
raise ValueError(f'Block height is {now_block_height}, less than target block number {target_block_num}') | ||
|
||
# get collator/delegator issuance number | ||
total_reward = substrate.query( | ||
module='InflationManager', | ||
storage_function='BlockRewards', | ||
params=[], | ||
) | ||
collator_delegator_issuance = total_reward.value * args.percentage / 100 | ||
|
||
for block_height in range(now_block_height - target_block_num, now_block_height): | ||
print(f'Block height: {block_height}') | ||
# get block data | ||
block_hash = get_block_hash(substrate, block_height) | ||
previous_block_hash = get_block_hash(substrate, block_height - 1) | ||
block_author = substrate.get_block(block_hash, include_author=True)['author'] | ||
print(f'block hash: {block_hash}, previous block hash: {previous_block_hash} block author: {block_author}') | ||
|
||
pot = get_account_balance(substrate, POT_ADDR, previous_block_hash) | ||
|
||
# Get staking info | ||
staking_info = {} | ||
info = substrate.query( | ||
module='ParachainStaking', | ||
storage_function='CandidatePool', | ||
params=[block_author], | ||
block_hash=previous_block_hash, | ||
) | ||
staking_info[block_author] = info.value | ||
staking_info[block_author]['delegators'] = { | ||
delegator['owner']: delegator['amount'] | ||
for delegator in info.value['delegators'] | ||
} | ||
print(f'staking info: {staking_info}') | ||
|
||
# get block reward | ||
result = get_all_events( | ||
substrate, | ||
block_hash, | ||
'ParachainStaking', 'Rewarded') | ||
reward_info = { | ||
event.value['attributes'][0]: event.value['attributes'][1] | ||
for event in result | ||
} | ||
print(f'Block height: {block_height}, block author: {block_author}, reward info: {reward_info}, staking info: {staking_info}, pot: {pot}') | ||
|
||
# Check total | ||
total_reward = sum(reward_info.values()) | ||
# Note float comparing | ||
if float(abs(total_reward - pot)) / pot > ALLOW_ERROR_PERCENTAGE: | ||
raise ValueError(f'Total reward is not equal to collator/delegator issuance {collator_delegator_issuance}, got {total_reward}') | ||
|
||
denominator = args.coefficient * \ | ||
staking_info[block_author]['stake'] + \ | ||
sum(staking_info[block_author]['delegators'].values()) | ||
# Calculate the collator reward | ||
real_collator_reward = reward_info[block_author] | ||
target_collator_reward = collator_delegator_issuance * \ | ||
float(args.coefficient) * staking_info[block_author]['stake'] / denominator | ||
if float(abs(real_collator_reward - target_collator_reward)) / target_collator_reward > ALLOW_ERROR_PERCENTAGE: | ||
raise ValueError(f'Collator reward is not equal to target reward {target_collator_reward}, got {real_collator_reward}') | ||
# Calculate the delegator reward | ||
for addr, value in reward_info.items(): | ||
if addr == block_author: | ||
continue | ||
real_delegator_reward = value | ||
target_delegator_reward = collator_delegator_issuance * \ | ||
float(staking_info[block_author]['delegators'][addr]) / denominator | ||
if float(abs(real_delegator_reward - target_delegator_reward)) / target_delegator_reward > ALLOW_ERROR_PERCENTAGE: | ||
raise ValueError(f'Delegator reward is not equal to target reward {target_delegator_reward}, got {real_delegator_reward}') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
import sys | ||
sys.path.append('./') | ||
import time | ||
|
||
from substrateinterface import SubstrateInterface, Keypair | ||
from peaq.sudo_extrinsic import funds | ||
from peaq.utils import ExtrinsicBatch | ||
from tools.utils import get_collators | ||
from tools.constants import KP_GLOBAL_SUDO, BLOCK_GENERATE_TIME | ||
from tools.runtime_upgrade import wait_until_block_height | ||
import argparse | ||
|
||
|
||
def fund_delegators(substrate: SubstrateInterface, delegators: list, amount: int, batch_num: int = 500): | ||
delegators = [kp.ss58_address for kp in delegators] | ||
for i in range(0, len(delegators), batch_num): | ||
print(f'Funding {i} / {len(delegators)}') | ||
funds(substrate, KP_GLOBAL_SUDO, delegators[i:i + batch_num], amount) | ||
print(f'Funded {i} / {len(delegators)}') | ||
|
||
|
||
def generate_delegators(number: int): | ||
return [Keypair.create_from_mnemonic(Keypair.generate_mnemonic()) for _ in range(number)] | ||
|
||
|
||
def get_collator_stake(substrate: SubstrateInterface, validator: str) -> int: | ||
key = Keypair(ss58_address=validator) | ||
collator_info = get_collators(substrate, key) | ||
return int(collator_info['stake'].value) | ||
|
||
|
||
def delegate_join_delegators(substrate: SubstrateInterface, delegators: list, collator_addr: str, collator_stake: int): | ||
for i, kp in enumerate(delegators): | ||
batch = ExtrinsicBatch(substrate, kp) | ||
batch.compose_call( | ||
'ParachainStaking', | ||
'join_delegators', | ||
{ | ||
'collator': collator_addr, | ||
'amount': collator_stake | ||
} | ||
) | ||
batch.execute() | ||
|
||
|
||
def delegate_delegators(substrate: SubstrateInterface, delegators: list, collator_addr: str, collator_stake: int): | ||
for i, kp in enumerate(delegators): | ||
batch = ExtrinsicBatch(substrate, kp) | ||
batch.compose_call( | ||
'ParachainStaking', | ||
'delegate_another_candidate', | ||
{ | ||
'collator': collator_addr, | ||
'amount': collator_stake | ||
} | ||
) | ||
batch.execute() | ||
|
||
|
||
def get_validators_info(substrate): | ||
validators = substrate.query('Session', 'Validators', []) | ||
return [validator.value for validator in validators] | ||
|
||
|
||
def main(): | ||
parser = argparse.ArgumentParser(description='Setup the delegator') | ||
parser.add_argument('--number', type=int, required=True, help='Number of collators one delegator want to delegate') | ||
parser.add_argument('--url', type=str, required=True, help='websocket URL') | ||
|
||
args = parser.parse_args() | ||
substrate = SubstrateInterface(url=args.url) | ||
|
||
print('Wait until block height 1') | ||
wait_until_block_height(substrate, 1) | ||
validators = get_validators_info(substrate) | ||
if len(validators) == 0: | ||
print('No validators found') | ||
return | ||
if len(validators) < args.number: | ||
print(f'Number of validators {len(validators)} is less than {args.number}') | ||
return | ||
|
||
print(f'Number of validators are {len(validators)}') | ||
# Get default staking number | ||
total_collator_stake = sum(get_collator_stake(substrate, validator) for validator in validators[:args.number]) | ||
fund_value = total_collator_stake * 3 | ||
if fund_value < 2 * 10 ** 18: | ||
fund_value = 2 * 10 ** 18 | ||
print(f'Collator stake {total_collator_stake} is less than {fund_value}, so we will fund it with {fund_value}') | ||
|
||
# Fund the delegators | ||
kps = generate_delegators(1) | ||
fund_delegators(substrate, kps, fund_value) | ||
time.sleep(BLOCK_GENERATE_TIME) | ||
|
||
# Delegate the first | ||
delegate_join_delegators(substrate, kps, validators[0], get_collator_stake(substrate, validators[0])) | ||
if args.number == 1: | ||
return | ||
print('Wait for one session') | ||
time.sleep(6 * 10) | ||
|
||
# Delegate | ||
validators = validators[1:] | ||
for idx, validator in enumerate(validators[:args.number]): | ||
print(F'Setup delegators for {validator} start, {idx} / {len(validators)}') | ||
delegate_delegators( | ||
substrate, | ||
kps, | ||
validator, | ||
get_collator_stake(substrate, validator)) | ||
print(f'Setup delegators for {validator} successfully, {idx} / {len(validators)}') | ||
|
||
while True: | ||
pending_tx = substrate.retrieve_pending_extrinsics() | ||
if len(pending_tx) < 5: | ||
print(f'The pending transactions are {len(pending_tx)}, we can continue') | ||
break | ||
else: | ||
print(f'Waiting for {len(pending_tx)} pending transactions') | ||
print('Wait for one session') | ||
time.sleep(6 * 10) | ||
|
||
|
||
if __name__ == '__main__': | ||
main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters