Skip to content

Policy refactoring#1459

Merged
cygnusv merged 16 commits intonucypher:masterfrom
vzotova:policy
Jan 10, 2020
Merged

Policy refactoring#1459
cygnusv merged 16 commits intonucypher:masterfrom
vzotova:policy

Conversation

@vzotova
Copy link
Member

@vzotova vzotova commented Nov 16, 2019

Based on #1470
Refs #1188
Closes #1063
Refs #1406
Refs #1492

WARNING: Not upgradeable changes
  • discrete payment for all policy periods: if policy starts today, ends tomorrow then fee must be for 2 periods
  • flexible end timestamp
  • Ursula pays more for confirmActivity (~15k-20k gas) to minimize Alice gas fee (save ~15k per node)
  • split creator role into creator and owner
  • revocation using owner's signature

PolicyManager v1.1.2 -> v 2.1.1
StakingEscrow v1.4.1 -> v2.1.1

@codecov
Copy link

codecov bot commented Nov 17, 2019

Codecov Report

Merging #1459 into master will increase coverage by 0.17%.
The diff coverage is 90.67%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master    #1459      +/-   ##
==========================================
+ Coverage   84.31%   84.48%   +0.17%     
==========================================
  Files          72       72              
  Lines       11225    11293      +68     
==========================================
+ Hits         9464     9541      +77     
+ Misses       1761     1752       -9
Impacted Files Coverage Δ
nucypher/config/characters.py 96.42% <ø> (-0.05%) ⬇️
nucypher/characters/lawful.py 90.05% <ø> (+0.36%) ⬆️
nucypher/characters/control/specifications.py 92.72% <ø> (ø) ⬆️
nucypher/characters/control/interfaces.py 96.29% <ø> (ø) ⬆️
nucypher/utilities/sandbox/blockchain.py 79.22% <100%> (ø) ⬆️
nucypher/blockchain/eth/agents.py 91.15% <100%> (ø) ⬆️
nucypher/blockchain/eth/deployers.py 89.35% <100%> (+0.86%) ⬆️
nucypher/cli/deploy.py 83.41% <100%> (+0.15%) ⬆️
nucypher/characters/control/serializers.py 86.29% <100%> (ø) ⬆️
nucypher/cli/painting.py 80.11% <100%> (ø) ⬆️
... and 13 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 61f749e...221c667. Read the comment docs.

@vzotova vzotova self-assigned this Nov 18, 2019
@vzotova vzotova force-pushed the policy branch 2 times, most recently from 6f4f2b4 to 38e9fa3 Compare December 11, 2019 16:24
@cygnusv cygnusv added this to the Testnet v2 milestone Dec 11, 2019
@vzotova vzotova force-pushed the policy branch 3 times, most recently from 5de4194 to 09d11e3 Compare December 14, 2019 13:05
@vzotova vzotova changed the title [WIP] Refactoring start/end policy time and fee calculation [WIP] Policy refactoring Dec 14, 2019
@vzotova vzotova marked this pull request as ready for review December 17, 2019 11:01
@vzotova vzotova changed the title [WIP] Policy refactoring Policy refactoring Dec 17, 2019
@vzotova vzotova added Alice 👩 Effects the "Alice" development area Enhancement New or improved features labels Dec 17, 2019
@vzotova vzotova mentioned this pull request Dec 20, 2019
@vzotova vzotova force-pushed the policy branch 2 times, most recently from 27193e1 to c3b995f Compare December 26, 2019 10:40
duration_periods = calculate_period_duration(now=maya.MayaDT(now),
future_time=expiration,
seconds_per_period=self.economics.seconds_per_period)
duration_periods += 1 # Number of all included periods
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are we using +=1 because the current day counts as one? Should calculate_period_duration make this adjustment, instead of having to manually do it?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, you're right, calculatePeriodDuration returns periodAt(expiration) - periodAt(now) and we add current period.

Should calculate_period_duration make this adjustment, instead of having to manually do it?

Good question, I'm still not sure what behaviour is more straightforward

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would seem that it should to me, but it may depend on where else it is used, and whether that same +=1 logic applies.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

calculatePeriodDuratio used in initialize_stake without adding 1

vzotova added a commit to vzotova/nucypher that referenced this pull request Jan 3, 2020
Co-Authored-By: Derek Pierre <derek.pierre@gmail.com>
vzotova added a commit to vzotova/nucypher that referenced this pull request Jan 3, 2020
Co-Authored-By: Derek Pierre <derek.pierre@gmail.com>
Copy link
Member

@derekpierre derekpierre left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🎸 !

expiration = datetime_at_period(self.staking_agent.get_current_period() + duration_periods,
seconds_per_period=self.economics.seconds_per_period)
seconds_per_period=self.economics.seconds_per_period,
start_of_period=True)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The comment says "expiration date is the last second of the current period", but here we pass start_of_period=True - what am I missing?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We take start of the next period and subtract 1 second (next line)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm sorry, maybe I'm being thick here, but I still don't get it. How does subtracting one second from the MayaDT get us to the last second of the current period? And how does start_of_period=True relate to that?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe comment is misleading, current period will be only for case when duration_periods==1, sort of example.
In that case start_of_period=True will return zero/first second of the next period and subtracting 1 second we will come to the last second of the previous period.
If duration_periods==2 then start_of_period=True will be period after next period and expiration after subtracting will be last second of the next period
start_of_period=True is sort of rounding, returns the beggining of calculated period

@jMyles
Copy link
Contributor

jMyles commented Jan 7, 2020

OK, after a first full read-through, here's what I think I'm seeing - correct me where I'm wrong and add what I'm missing:

  • Policies don't have clients, they have creators
  • Policy creation and revocation events don't have clients, they have senders
  • ...no wait, policy creation doesn't have a sender, it has a creator and an owner
  • We use timestamps instead of periods for Policy beginnings and endings
  • We check revocation by checking the signature of policy owner.

@vzotova
Copy link
Member Author

vzotova commented Jan 7, 2020

@jMyles All good, I'll add some remarks:

  • client -> creator and client -> sender are just renames without changing meaning because client initially was not good name for this role
  • Before the changes, Alice/client/creator could choose whether she would pay for the current period and how much, now always have to pay the same price as for other periods.

Payment should be added to the transaction in ETH and the amount is `firstReward * stakers.length + rewardRate * periods * stakers.length`.
The reward rate must be greater than or equal to the minimum reward for each staker in the list. The first period's reward is not refundable, and can be zero.
In order to place the fee for a policy, Alice calls the method `PolicyManager.createPolicy(bytes16, address, uint64, address[])`,
specifying the stakers' addresses, the policy ID (off-chain generation), the policy owner (could be zero address), and the end timestamp of the policy.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The address argument before the policy ID is missing.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure about address, maybe question with order in description? I've put stakers addresses to the end of the sentence

event PolicyCreated(
bytes16 indexed policyId,
address indexed client
address indexed creator,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rather than creator, it should be something like sponsor, payer, etc.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✔️

* @return Revocation hash
*/
function getRevocationHash(bytes16 _policyId, address _node) public view returns (bytes32) {
return SignatureVerifier.hashEIP191(abi.encodePacked(_policyId, _node), byte(0x45));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this line deserves a comment explaining we're using EIP191 version 0x45 ('E') for personal_sign

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✔️

expiration = maya.now() + datetime.timedelta(days=3)
days = 3
now = testerchain.w3.eth.getBlock(block_identifier='latest').timestamp
expiration = maya.MayaDT(now).add(days=days-1)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This whole manually subtracting/adding a day is not intuitive at all.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about expiration = maya.now() - timedelta(days=1)?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

how about to rename days to days_to_pay or to something similar?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think I need to explain it better what is core of these changes:
Before this PR duration of policy was equal to periods for which Alice pays. Also was firstPartialReward, kind of alternative way to pay for current period.
Now duration in seconds and not in periods. But payment still in periods. Sort of border between Alice and staker/worker regarding fees. Also duration includes current period by default (start timestamp is block.timestamp), so we get payment periods == duration periods + 1 (I mean duration periods as ceiling of (endTimestamp - block.timestamp)/secondsPerPeriod). Of course it's possible that payment periods == duration periods in case when block.timestamp == first second of current period and end timestamp == last second of any period but this situation is not used in tests

vzotova added a commit to vzotova/nucypher that referenced this pull request Jan 9, 2020
Co-Authored-By: Derek Pierre <derek.pierre@gmail.com>
Co-Authored-By: David Núñez <david@nucypher.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Alice 👩 Effects the "Alice" development area Enhancement New or improved features

Projects

None yet

Development

Successfully merging this pull request may close these issues.

How to determine default first_period_value

5 participants