Skip to content

Commit

Permalink
Merge pull request #76 from peaqnetwork/feat/1208539891229773_typo
Browse files Browse the repository at this point in the history
Fix typo
  • Loading branch information
sfffaaa authored Nov 7, 2024
2 parents 9cdcba1 + b423388 commit fc7d892
Show file tree
Hide file tree
Showing 7 changed files with 269 additions and 5 deletions.
2 changes: 1 addition & 1 deletion ETH/did/did.sol.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@
"type": "bytes"
}
],
"name": "RemoveAttribte",
"name": "RemoveAttribute",
"type": "event"
},
{
Expand Down
2 changes: 1 addition & 1 deletion tests/bridge_did_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ def test_bridge_did(self):
block_idx = self._eth_remove_attribute(contract, eth_kp_src, eth_src, KEY)
self.assertRaises(ValueError, contract.functions.readAttribute(eth_src, KEY).call)

event = contract.events.RemoveAttribte.create_filter(fromBlock=block_idx, toBlock=block_idx)
event = contract.events.RemoveAttribute.create_filter(fromBlock=block_idx, toBlock=block_idx)
events = event.get_all_entries()
self.assertEqual(f"{events[0]['args']['did_account']}", f"{eth_src}")
self.assertEqual(f"0x{events[0]['args']['name'].hex()}", f"{KEY}")
Expand Down
4 changes: 2 additions & 2 deletions tests/reward_distribution_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
'max': 90 * 10 ** 9,
},
'peaq-dev': {
'min': 20 * 10 ** 9,
'min': 1 * 10 ** 9,
'max': 90 * 10 ** 9,
}
}
Expand Down Expand Up @@ -268,7 +268,7 @@ def test_transaction_fee_reward_v2(self):
self._check_transaction_fee_reward_from_sender(block_number)
self._check_transaction_fee_reward_from_collator(block_number)

def test_transaction_fee_reward(self):
def test_transaction_fee_reward_with_tip(self):
# Execute
receipt = transfer_with_tip(
self._substrate, self._kp_bob, self._kp_eve.ss58_address,
Expand Down
2 changes: 1 addition & 1 deletion tests/token_economy_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ def test_constants(self):
@pytest.mark.skipif(TestUtils.is_local_new_chain() is True, reason='Dont need to test on the new chain')
def test_block_reward(self):
block_reward = {
'peaq-dev-fork': int(3.805175038 * 10 ** 18),
'peaq-dev-fork': int(1.902587519 * 10 ** 18),
'krest-network-fork': int(3.805175038 * 10 ** 18),
'peaq-network-fork': int(27.96803653 * 10 ** 18),
}
Expand Down
130 changes: 130 additions & 0 deletions tools/check_reward_v1.py
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}')
126 changes: 126 additions & 0 deletions tools/setup_delegator_mulit_collators.py
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()
8 changes: 8 additions & 0 deletions tools/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -420,6 +420,14 @@ def get_event(substrate, block_hash, pallet, event_name):
return None


def get_all_events(substrate, block_hash, pallet, event_name):
return [
event['event']
for event in substrate.get_events(block_hash)
if event.value['module_id'] == pallet and event.value['event_id'] == event_name
]


def batch_fund(batch, kp_or_addr, amount):
addr = kp_or_addr
if isinstance(kp_or_addr, Keypair):
Expand Down

0 comments on commit fc7d892

Please sign in to comment.