Fix other tests

pull/2623/head
vzotova 2021-03-13 17:27:58 +03:00 committed by Kieran Prasch
parent fc2ff03e35
commit 3136a97aa6
15 changed files with 55 additions and 53 deletions

View File

@ -394,7 +394,7 @@ class StandardTokenEconomics(BaseEconomics):
lock_duration_coefficient_2=lock_duration_coefficient_2,
maximum_rewarded_periods=int(maximum_rewarded_periods),
hours_per_period=hours_per_period,
**kwargs)
**kwargs)
def first_phase_final_period(self) -> int:
"""

View File

@ -60,7 +60,8 @@ def test_staker_divides_stake(staker, token_economics):
new_stake_value = NU(token_economics.minimum_allowed_locked * 2, 'NuNit')
stake_index = 0
staker.initialize_stake(amount=stake_value, lock_periods=int(token_economics.minimum_locked_periods))
duration = int(token_economics.minimum_locked_periods)
staker.initialize_stake(amount=stake_value, lock_periods=duration)
stake = staker.stakes[stake_index + 1]
# Can't use additional periods and expiration together
@ -70,8 +71,8 @@ def test_staker_divides_stake(staker, token_economics):
staker.divide_stake(target_value=new_stake_value, stake=stake, additional_periods=2)
current_period = staker.staking_agent.get_current_period()
expected_old_stake = (current_period + 1, current_period + 30, stake_value - new_stake_value)
expected_new_stake = (current_period + 1, current_period + 32, new_stake_value)
expected_old_stake = (current_period + 1, current_period + duration, stake_value - new_stake_value)
expected_new_stake = (current_period + 1, current_period + duration + 2, new_stake_value)
assert 3 == len(staker.stakes), 'A new stake was not added to this stakers stakes'
assert expected_old_stake == staker.stakes[stake_index + 1].to_stake_info(), 'Old stake values are invalid'
@ -102,9 +103,9 @@ def test_staker_divides_stake(staker, token_economics):
start_of_period=True)
staker.divide_stake(target_value=yet_another_stake_value, stake=stake, expiration=new_expiration)
expected_new_stake = (current_period + 1, current_period + 32, new_stake_value)
expected_new_stake = (current_period + 1, current_period + duration + 2, new_stake_value)
expected_yet_another_stake = Stake(first_locked_period=current_period + 1,
final_locked_period=current_period + 34,
final_locked_period=current_period + duration + 4,
value=yet_another_stake_value,
checksum_address=staker.checksum_address,
index=3,

View File

@ -187,7 +187,7 @@ def test_successful_claim(testerchain, agency, token_economics, test_registry):
tpower = TransactingPower(account=bidder, signer=Web3Signer(testerchain.client))
# Ensure that the bidder is not staking.
locked_tokens = staking_agent.get_locked_tokens(staker_address=bidder, periods=10)
locked_tokens = staking_agent.get_locked_tokens(staker_address=bidder, periods=5)
assert locked_tokens == 0
receipt = agent.claim(transacting_power=tpower)
@ -198,5 +198,5 @@ def test_successful_claim(testerchain, agency, token_economics, test_registry):
_receipt = agent.claim(transacting_power=tpower)
# Ensure that the claimant is now the holder of a stake.
locked_tokens = staking_agent.get_locked_tokens(staker_address=bidder, periods=10)
locked_tokens = staking_agent.get_locked_tokens(staker_address=bidder, periods=5)
assert locked_tokens > 0

View File

@ -64,7 +64,7 @@ def test_staking_escrow_preparation(testerchain,
# Data is still old, because there is no upgrade yet
staking_agent = ContractAgency.get_agent(StakingEscrowAgent, registry=test_registry)
assert staking_agent.contract.functions.secondsPerPeriod().call() == token_economics.seconds_per_period
assert staking_agent.contract.functions.genesisSecondsPerPeriod().call() == token_economics.seconds_per_period
assert staking_agent.contract.functions.genesisSecondsPerPeriod().call() == token_economics.genesis_seconds_per_period
def test_policy_manager_preparation(testerchain,
@ -79,7 +79,7 @@ def test_policy_manager_preparation(testerchain,
# Data is still old, because there is no upgrade yet
policy_manager_agent = ContractAgency.get_agent(PolicyManagerAgent, registry=test_registry)
assert policy_manager_agent.contract.functions.secondsPerPeriod().call() == token_economics.seconds_per_period
assert policy_manager_agent.contract.functions.genesisSecondsPerPeriod().call() == token_economics.seconds_per_period
assert policy_manager_agent.contract.functions.genesisSecondsPerPeriod().call() == token_economics.genesis_seconds_per_period
def test_staking_escrow_migration_upgrade(testerchain,

View File

@ -95,7 +95,7 @@ def create_policy_control_request(blockchain_bob):
'label': b64encode(bytes(b'test')).decode(),
'm': 2,
'n': 3,
'expiration': (maya.now() + datetime.timedelta(days=3)).iso8601(),
'expiration': (maya.now() + datetime.timedelta(days=35)).iso8601(),
'value': 3 * 3 * 10 ** 16
}
return method_name, params
@ -111,7 +111,7 @@ def grant_control_request(blockchain_bob):
'label': 'test',
'm': 2,
'n': 3,
'expiration': (maya.now() + datetime.timedelta(days=3)).iso8601(),
'expiration': (maya.now() + datetime.timedelta(days=35)).iso8601(),
'value': 3 * 3 * 10 ** 16
}
return method_name, params

View File

@ -123,7 +123,7 @@ def test_alice_character_control_revoke(alice_web_controller_test_client, blockc
'label': 'test-revoke',
'm': 2,
'n': 3,
'expiration': (maya.now() + datetime.timedelta(days=3)).iso8601(),
'expiration': (maya.now() + datetime.timedelta(days=35)).iso8601(),
'value': 100500 * 3 * 3,
}
response = alice_web_controller_test_client.put('/grant', data=json.dumps(grant_request_data))
@ -288,7 +288,7 @@ def test_web_character_control_lifecycle(alice_web_controller_test_client,
'm': 1,
'n': 1,
'label': random_label,
'expiration': (maya.now() + datetime.timedelta(days=3)).iso8601(),
'expiration': (maya.now() + datetime.timedelta(days=35)).iso8601(),
'value': 3 * 10 ** 10
}

View File

@ -28,7 +28,7 @@ from nucypher.policy.collections import SignedTreasureMap as DecentralizedTreasu
def test_decentralized_grant(blockchain_alice, blockchain_bob, blockchain_ursulas):
# Setup the policy details
n = 3
policy_end_datetime = maya.now() + datetime.timedelta(days=5)
policy_end_datetime = maya.now() + datetime.timedelta(days=35)
label = b"this_is_the_path_to_which_access_is_being_granted"
# Create the Policy, Granting access to Bob

View File

@ -32,7 +32,7 @@ def test_policy_simple_sinpa(blockchain_ursulas, blockchain_alice, blockchain_bo
amonia = Amonia.from_lawful_alice(blockchain_alice)
# Setup the policy details
n = 3
policy_end_datetime = maya.now() + datetime.timedelta(days=5)
policy_end_datetime = maya.now() + datetime.timedelta(days=35)
label = b"this_is_the_path_to_which_access_is_being_granted"
with pytest.raises(amonia.NotEnoughNodes):
@ -62,7 +62,7 @@ def test_try_to_post_free_arrangement_by_hacking_enact(blockchain_ursulas, block
amonia = Amonia.from_lawful_alice(blockchain_alice)
# Setup the policy details
n = 3
policy_end_datetime = maya.now() + datetime.timedelta(days=5)
policy_end_datetime = maya.now() + datetime.timedelta(days=35)
label = b"another_path"
bupkiss_policy = amonia.circumvent_safegaurds_and_grant_without_paying(bob=blockchain_bob,
@ -103,7 +103,7 @@ def test_pay_a_flunky_instead_of_the_arranged_ursula(blockchain_alice, blockchai
# Setup the policy details
n = 3
policy_end_datetime = maya.now() + datetime.timedelta(days=5)
policy_end_datetime = maya.now() + datetime.timedelta(days=35)
label = b"back_and_forth_forever"
bupkiss_policy = amonia.grant_while_paying_the_wrong_nodes(ursulas_to_trick_into_working_for_free=target_ursulas,
@ -142,7 +142,7 @@ def test_put_additional_treasure_map_on_network(blockchain_ursulas, blockchain_a
amonia = Amonia.from_lawful_alice(blockchain_alice)
# Setup the policy details
n = 3
policy_end_datetime = maya.now() + datetime.timedelta(days=5)
policy_end_datetime = maya.now() + datetime.timedelta(days=35)
label = b"this_is_the_path_to_which_access_is_being_granted"
policy = amonia.grant(bob=blockchain_bob,

View File

@ -139,7 +139,7 @@ def test_blockchain_ursulas_reencrypt(blockchain_ursulas, blockchain_alice, bloc
# TODO: Make sample selection buffer configurable - #1061
m = n = 10
expiration = maya.now() + datetime.timedelta(days=5)
expiration = maya.now() + datetime.timedelta(days=35)
_policy = blockchain_alice.grant(bob=blockchain_bob,
label=label,

View File

@ -298,7 +298,7 @@ def run_entire_cli_lifecycle(click_runner,
bob_keys = side_channel.fetch_bob_public_keys()
bob_encrypting_key = bob_keys.bob_encrypting_key
bob_verifying_key = bob_keys.bob_verifying_key
expiration = (maya.now() + datetime.timedelta(days=3)).datetime().strftime("%Y-%m-%d %H:%M:%S")
expiration = (maya.now() + datetime.timedelta(days=35)).datetime().strftime("%Y-%m-%d %H:%M:%S")
grant_args = ('alice', 'grant',
'--mock-networking',

View File

@ -557,10 +557,9 @@ def test_collect_rewards_integration(click_runner,
manual_staker,
manual_worker,
token_economics,
policy_value,
policy_rate):
policy_value):
half_stake_time = token_economics.minimum_locked_periods // 2 # Test setup
half_stake_time = 2 * token_economics.minimum_locked_periods # Test setup
logger = Logger("Test-CLI") # Enter the Teacher's Logger, and
current_period = 0 # State the initial period for incrementing
@ -606,9 +605,10 @@ def test_collect_rewards_integration(click_runner,
blockchain_alice.selection_buffer = 1
M, N = 1, 1
days = 3
duration_in_periods = 3
days = (duration_in_periods - 1) * (token_economics.hours_per_period // 24)
now = testerchain.w3.eth.getBlock('latest').timestamp
expiration = maya.MayaDT(now).add(days=days-1)
expiration = maya.MayaDT(now).add(days=days)
blockchain_policy = blockchain_alice.grant(bob=blockchain_bob,
label=random_policy_label,
m=M, n=N,
@ -691,7 +691,7 @@ def test_collect_rewards_integration(click_runner,
# Policy Fee
collected_policy_fee = testerchain.client.get_balance(burner_wallet.address)
expected_collection = policy_rate * 30
expected_collection = policy_value
assert collected_policy_fee == expected_collection
# Finish the passage of time... once and for all

View File

@ -62,7 +62,7 @@ def test_blockchain_alice_finds_ursula_via_rest(blockchain_alice, blockchain_urs
def test_treasure_map_cannot_be_duplicated(blockchain_ursulas, blockchain_alice, blockchain_bob, agency):
# Setup the policy details
n = 3
policy_end_datetime = maya.now() + datetime.timedelta(days=5)
policy_end_datetime = maya.now() + datetime.timedelta(days=35)
label = b"this_is_the_path_to_which_access_is_being_granted"
# Create the Policy, Granting access to Bob
@ -160,7 +160,7 @@ def test_treasure_map_cannot_be_duplicated_again(blockchain_ursulas,
agency):
# Setup the policy details
n = 3
policy_end_datetime = maya.now() + datetime.timedelta(days=5)
policy_end_datetime = maya.now() + datetime.timedelta(days=35)
label = b"this_is_the_path_to_which_access_is_being_granted"
# Create the Policy, Granting access to Bob

View File

@ -252,7 +252,8 @@ def idle_blockchain_policy(testerchain, blockchain_alice, blockchain_bob, token_
Creates a Policy, in a manner typical of how Alice might do it, with a unique label
"""
random_label = generate_random_label()
days = token_economics.minimum_locked_periods // 2
periods = token_economics.minimum_locked_periods // 2
days = periods * (token_economics.hours_per_period // 24)
now = testerchain.w3.eth.getBlock('latest').timestamp
expiration = maya.MayaDT(now).add(days=days - 1)
n = 3
@ -260,7 +261,7 @@ def idle_blockchain_policy(testerchain, blockchain_alice, blockchain_bob, token_
policy = blockchain_alice.create_policy(blockchain_bob,
label=random_label,
m=m, n=n,
value=n * days * 100,
value=n * periods * 100,
expiration=expiration)
return policy
@ -440,7 +441,7 @@ def make_token_economics(blockchain):
economics = StandardTokenEconomics(
worklock_boosting_refund_rate=200,
worklock_commitment_duration=60, # periods
worklock_commitment_duration=60, # genesis periods
worklock_supply=10 * BaseEconomics._default_maximum_allowed_locked,
bidding_start_date=bidding_start_date,
bidding_end_date=bidding_end_date,

View File

@ -30,7 +30,6 @@ import time
from twisted.logger import ILogObserver, globalLogPublisher, jsonFileLogObserver
from web3.contract import Contract
from nucypher.blockchain.eth.deployers import StakingEscrowDeployer, PolicyManagerDeployer
from nucypher.blockchain.eth.registry import InMemoryContractRegistry
from nucypher.blockchain.eth.signers.software import Web3Signer
from nucypher.crypto.powers import TransactingPower
@ -62,7 +61,7 @@ except ImportError:
ALGORITHM_SHA256 = 1
TOKEN_ECONOMICS = StandardTokenEconomics()
MIN_ALLOWED_LOCKED = TOKEN_ECONOMICS.minimum_allowed_locked
MIN_LOCKED_PERIODS = TOKEN_ECONOMICS.minimum_locked_periods
LOCKED_PERIODS = 30
MAX_ALLOWED_LOCKED = TOKEN_ECONOMICS.maximum_allowed_locked
MAX_MINTING_PERIODS = TOKEN_ECONOMICS.maximum_rewarded_periods
@ -233,7 +232,7 @@ def estimate_gas(analyzer: AnalyzeGas = None) -> None:
# First deposit ever is the most expensive, make it to remove unusual gas spending
transact(token_functions.approve(staking_agent.contract_address, MIN_ALLOWED_LOCKED * 10), {'from': origin})
transact(staker_functions.deposit(everyone_else[0], MIN_ALLOWED_LOCKED, MIN_LOCKED_PERIODS), {'from': origin})
transact(staker_functions.deposit(everyone_else[0], MIN_ALLOWED_LOCKED, LOCKED_PERIODS), {'from': origin})
testerchain.time_travel(periods=1)
#
@ -256,12 +255,12 @@ def estimate_gas(analyzer: AnalyzeGas = None) -> None:
# Ursula and Alice transfer some tokens to the escrow and lock them
#
transact_and_log("Initial deposit tokens, first",
staker_functions.deposit(staker1, MIN_ALLOWED_LOCKED * 3, MIN_LOCKED_PERIODS),
staker_functions.deposit(staker1, MIN_ALLOWED_LOCKED * 3, LOCKED_PERIODS),
{'from': staker1})
transact_and_log("Initial deposit tokens, other",
staker_functions.deposit(staker2, MIN_ALLOWED_LOCKED * 3, MIN_LOCKED_PERIODS),
staker_functions.deposit(staker2, MIN_ALLOWED_LOCKED * 3, LOCKED_PERIODS),
{'from': staker2})
transact(staker_functions.deposit(staker3, MIN_ALLOWED_LOCKED * 3, MIN_LOCKED_PERIODS), {'from': staker3})
transact(staker_functions.deposit(staker3, MIN_ALLOWED_LOCKED * 3, LOCKED_PERIODS), {'from': staker3})
transact(staker_functions.bondWorker(staker1), {'from': staker1})
transact(staker_functions.bondWorker(staker2), {'from': staker2})
@ -332,7 +331,7 @@ def estimate_gas(analyzer: AnalyzeGas = None) -> None:
transact(staker_functions.setReStake(True), {'from': staker1})
transact(staker_functions.setReStake(True), {'from': staker2})
# Used to remove spending for first call in a day for mint and commitToNextPeriod
# Used to remove spending for first call in a period for mint and commitToNextPeriod
transact(staker_functions.commitToNextPeriod(), {'from': staker3})
transact_and_log("Make a commitment + mint + re-stake",
@ -359,9 +358,9 @@ def estimate_gas(analyzer: AnalyzeGas = None) -> None:
# Ursula and Alice deposit some tokens to the escrow again
#
transact_and_log("Deposit tokens after making a commitment",
staker_functions.deposit(staker1, MIN_ALLOWED_LOCKED * 2, MIN_LOCKED_PERIODS),
staker_functions.deposit(staker1, MIN_ALLOWED_LOCKED * 2, LOCKED_PERIODS),
{'from': staker1})
transact(staker_functions.deposit(staker2, MIN_ALLOWED_LOCKED * 2, MIN_LOCKED_PERIODS), {'from': staker2})
transact(staker_functions.deposit(staker2, MIN_ALLOWED_LOCKED * 2, LOCKED_PERIODS), {'from': staker2})
#
# Revoke policy
@ -458,7 +457,7 @@ def estimate_gas(analyzer: AnalyzeGas = None) -> None:
# Check regular deposit
#
transact_and_log("Deposit tokens to new sub-stake",
staker_functions.deposit(staker1, MIN_ALLOWED_LOCKED, MIN_LOCKED_PERIODS),
staker_functions.deposit(staker1, MIN_ALLOWED_LOCKED, LOCKED_PERIODS),
{'from': staker1})
transact_and_log("Deposit tokens using existing sub-stake",
staker_functions.depositAndIncrease(0, MIN_ALLOWED_LOCKED),
@ -473,7 +472,7 @@ def estimate_gas(analyzer: AnalyzeGas = None) -> None:
transact_and_log("ApproveAndCall",
token_functions.approveAndCall(staking_agent.contract_address,
MIN_ALLOWED_LOCKED * 2,
web3.toBytes(MIN_LOCKED_PERIODS)),
web3.toBytes(LOCKED_PERIODS)),
{'from': staker1})
#
@ -482,7 +481,7 @@ def estimate_gas(analyzer: AnalyzeGas = None) -> None:
testerchain.time_travel(periods=1)
transact(staker_functions.commitToNextPeriod(), {'from': staker1})
transact_and_log("Locking tokens and creating new sub-stake",
staker_functions.lockAndCreate(MIN_ALLOWED_LOCKED, MIN_LOCKED_PERIODS),
staker_functions.lockAndCreate(MIN_ALLOWED_LOCKED, LOCKED_PERIODS),
{'from': staker1})
transact_and_log("Locking tokens using existing sub-stake",
staker_functions.lockAndIncrease(0, MIN_ALLOWED_LOCKED),
@ -545,7 +544,7 @@ def estimate_gas(analyzer: AnalyzeGas = None) -> None:
transact(staker_functions.commitToNextPeriod(), {'from': staker1})
testerchain.time_travel(periods=1)
transact(staker_functions.lockAndCreate(MIN_ALLOWED_LOCKED, MIN_LOCKED_PERIODS), {'from': staker1})
transact(staker_functions.lockAndCreate(MIN_ALLOWED_LOCKED, LOCKED_PERIODS), {'from': staker1})
deposit = staker_functions.stakerInfo(staker1).call()[0]
unlocked = deposit - staker_functions.getLockedTokens(staker1, 1).call()
transact(staker_functions.withdraw(unlocked), {'from': staker1})
@ -574,12 +573,12 @@ def estimate_gas(analyzer: AnalyzeGas = None) -> None:
transact(token_functions.approve(staking_agent.contract_address, MIN_ALLOWED_LOCKED * number_of_sub_stakes),
{'from': origin})
for i in range(number_of_sub_stakes):
transact(staker_functions.deposit(staker4, MIN_ALLOWED_LOCKED, MIN_LOCKED_PERIODS),
transact(staker_functions.deposit(staker4, MIN_ALLOWED_LOCKED, LOCKED_PERIODS),
{'from': origin})
transact(staker_functions.bondWorker(staker4), {'from': staker4})
transact(staker_functions.setWindDown(True), {'from': staker4})
# Used to remove spending for first call in a day for mint and commitToNextPeriod
# Used to remove spending for first call in a period for mint and commitToNextPeriod
transact(staker_functions.commitToNextPeriod(), {'from': staker1})
transact_and_log(f"Make a commitment ({number_of_sub_stakes} sub-stakes)",
@ -590,7 +589,7 @@ def estimate_gas(analyzer: AnalyzeGas = None) -> None:
transact(staker_functions.commitToNextPeriod(), {'from': staker4})
testerchain.time_travel(periods=1)
# Used to remove spending for first call in a day for mint and commitToNextPeriod
# Used to remove spending for first call in a period for mint and commitToNextPeriod
transact(staker_functions.commitToNextPeriod(), {'from': staker1})
transact_and_log(f"Make a commitment + mint + re-stake ({number_of_sub_stakes} sub-stakes)",

View File

@ -47,8 +47,9 @@ def test_rough_economics():
assert float(round(e.erc20_total_supply / Decimal(1e9), 2)) == 3.89 # As per economics paper
# Check that we have correct numbers in day 1 of the second phase
one_year_in_periods = Decimal(365 / 7)
initial_rate = (e.erc20_total_supply - int(e.first_phase_total_supply)) \
* (e.lock_duration_coefficient_1 + 52) \
* (e.lock_duration_coefficient_1 + one_year_in_periods) \
/ (e.issuance_decay_coefficient * e.lock_duration_coefficient_2)
assert int(initial_rate) == int(e.first_phase_max_issuance)
@ -58,14 +59,14 @@ def test_rough_economics():
assert int(initial_rate_small) == int(initial_rate / 2)
# Sanity check that total and reward supply calculated correctly
assert int(LOG2 / (e.token_halving * 52) * (e.erc20_total_supply - int(e.first_phase_total_supply))) == int(initial_rate)
assert int(LOG2 / (e.token_halving * one_year_in_periods) * (e.erc20_total_supply - int(e.first_phase_total_supply))) == int(initial_rate)
assert int(e.reward_supply) == int(e.erc20_total_supply - Decimal(int(1e9)))
with localcontext() as ctx: # TODO: Needs follow up - why the sudden failure (python 3.8.0)?
ctx.prec = 18 # Perform a high precision calculation
# Sanity check for lock_duration_coefficient_1 (k1), issuance_decay_coefficient (d) and lock_duration_coefficient_2 (k2)
expected = e.lock_duration_coefficient_1 * e.token_halving
result = e.issuance_decay_coefficient * e.lock_duration_coefficient_2 * LOG2 * e.small_stake_multiplier / 52
result = e.issuance_decay_coefficient * e.lock_duration_coefficient_2 * LOG2 * e.small_stake_multiplier / one_year_in_periods
assert expected == result
@ -73,8 +74,8 @@ def test_economic_parameter_aliases():
e = StandardTokenEconomics()
assert e.lock_duration_coefficient_1 == 52
assert e.lock_duration_coefficient_2 == 2 * 52
assert int(e.lock_duration_coefficient_1) == 52
assert int(e.lock_duration_coefficient_2) == 2 * 52
assert int(e.issuance_decay_coefficient) == 150
assert e.maximum_rewarded_periods == 52