Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New staking - part 1 and 2 - SWIP-19 and 20 combined #252

Merged
merged 39 commits into from
Aug 26, 2024

Conversation

0xCardinalError
Copy link
Contributor

@0xCardinalError 0xCardinalError commented Apr 16, 2024

This implements the feature allowing staked nodes to change their overlay without restaking (called neighbourhood hopping, see SWIP-19)

Implemented by changing stake registry to be keyed with node address instead of node overlay.

@0xCardinalError 0xCardinalError changed the title New staking New staking with overaly hopping Apr 22, 2024
@0xCardinalError 0xCardinalError changed the base branch from master to staking_optimization May 6, 2024 08:22
src/Staking.sol Outdated Show resolved Hide resolved
src/Staking.sol Outdated Show resolved Hide resolved
src/Staking.sol Outdated Show resolved Hide resolved
src/Staking.sol Outdated Show resolved Hide resolved
test/Staking.test.ts Show resolved Hide resolved
test/Staking.test.ts Outdated Show resolved Hide resolved
test/Staking.test.ts Outdated Show resolved Hide resolved
@0xCardinalError 0xCardinalError changed the title New staking with overaly hopping Overlay hopping (new staking - part 1) May 16, 2024
Copy link
Member

@zelig zelig left a comment

Choose a reason for hiding this comment

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

fundamental issues with how stake is queried, in its current form allows for a critical exploit: it is possible to play with several overlays derived from the same address simultaneously while using only one stake. This is easily fixable by passing the overlay as well as an argument (to functions called from redis contract to validate stake of a node) and checking the overlay against the one recorded to the address currently.

The chosen implementation (see discussion on research internal) is probably the best option but it requires extensive changes in the interfaces therefore the most work for the bee team. Also it needs a thorough change to the SWIP-19

hardhat.config.ts Outdated Show resolved Hide resolved
src/Redistribution.sol Show resolved Hide resolved
src/Redistribution.sol Outdated Show resolved Hide resolved
src/Redistribution.sol Show resolved Hide resolved
src/Staking.sol Show resolved Hide resolved
src/Staking.sol Show resolved Hide resolved
src/Staking.sol Show resolved Hide resolved
src/Staking.sol Outdated Show resolved Hide resolved
src/Staking.sol Outdated Show resolved Hide resolved
src/Staking.sol Outdated Show resolved Hide resolved
@zelig zelig changed the title Overlay hopping (new staking - part 1) Neighbourhood hopping (new staking - part 1) May 17, 2024
@zelig zelig changed the title Neighbourhood hopping (new staking - part 1) Neighbourhood hopping (new staking - part 1 - SWIP-19) May 17, 2024
zelig
zelig previously requested changes May 17, 2024
Copy link
Member

@zelig zelig left a comment

Choose a reason for hiding this comment

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

i took back the attack story, the overlay is checked here

if (Stakes.overlayOfAddress(msg.sender) != _overlay) {

but still issues left

src/Redistribution.sol Show resolved Hide resolved
src/Redistribution.sol Show resolved Hide resolved
src/Redistribution.sol Outdated Show resolved Hide resolved
src/Redistribution.sol Outdated Show resolved Hide resolved
src/Staking.sol Show resolved Hide resolved
@0xCardinalError 0xCardinalError marked this pull request as ready for review May 24, 2024 19:25
Copy link
Member

@nugaon nugaon left a comment

Choose a reason for hiding this comment

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

Nothing critical but it would make difficulties with maintainability in a long run and confusion around keeping track overlay changes.

Also check the linter errors, the code has many because of the max-line-length rule which is set to 120.

src/Staking.sol Outdated Show resolved Hide resolved
src/Staking.sol Outdated

stakes[msg.sender].overlay = overlay;
stakes[msg.sender].lastUpdatedBlockNumber = block.number;
emit OverlayChanged(msg.sender, overlay);
Copy link
Member

Choose a reason for hiding this comment

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

overlay can be changed in depositStake function as well, but this event is not omitted. I would remove this function and if overlay changes in depositStake this event should be emitted. having two function to do the same and only one keeping track with one specific change leads to confusion.

src/Redistribution.sol Outdated Show resolved Hide resolved
src/Redistribution.sol Outdated Show resolved Hide resolved
src/Redistribution.sol Show resolved Hide resolved
uint64 cr = currentRound();
bytes32 _overlay = Stakes.overlayOfAddress(msg.sender);
Copy link
Member

Choose a reason for hiding this comment

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

same as above

src/Redistribution.sol Outdated Show resolved Hide resolved
src/Redistribution.sol Show resolved Hide resolved
Copy link
Member

@zelig zelig left a comment

Choose a reason for hiding this comment

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

I second @nugaon 's comments
and wonder why you removed the stake struct lookup to prevent repeated map lookups

@0xCardinalError
Copy link
Contributor Author

0xCardinalError commented May 28, 2024

I second @nugaon 's comments and wonder why you removed the stake struct lookup to prevent repeated map lookups

I made response on this earlier but seems it been missed so I will copy paste it here also at @nugaon @zelig

"Did some measurements...
Thing is that "getStake" is wasteful in cases where there is lookup for just one of the struct values.
Getter for each individual struct value by itself uses up to 2900 gas while getStake uses 9861 gas, as it fetches all values from storage, which means that getStake is only useful if we are operating with all values from struct, if not, then its less desirable option and we should use single getters for each value, which means all this single value getters are staying and we do use them in most cases."

Base automatically changed from staking_optimization to master June 10, 2024 14:40
@ldeffenb
Copy link

First, is there going to be a migration of the existing stakes from overlay-based to owner-based?

And if so, has anyone given thought (or better yet, a test) to what happens with multiple abandoned overlay-based stakes that are all owned by a single account? This happens when people keep their keys, but delete the rest of their data-dir causing a new overlay to be generated from the original account.

See this (short) conversation in Discord. Start at the link and follow the replies backwards in time.
https://discord.com/channels/799027393297514537/811553590170353685/1251173722245693460

@0xCardinalError
Copy link
Contributor Author

First, is there going to be a migration of the existing stakes from overlay-based to owner-based?

And if so, has anyone given thought (or better yet, a test) to what happens with multiple abandoned overlay-based stakes that are all owned by a single account? This happens when people keep their keys, but delete the rest of their data-dir causing a new overlay to be generated from the original account.

See this (short) conversation in Discord. Start at the link and follow the replies backwards in time. https://discord.com/channels/799027393297514537/811553590170353685/1251173722245693460

yes, there will be migration but basically it will be from users/operators side and we will need to write instructions on that and communicate them. Basically old contract would be paused, nodes would be taking funds out and could put them in new contract for staking.

About abandoned overlays, if the user has private key of the address used for that node he should be able to take funds out, if he knows the overlay, he can just input it and take funds out.
If ppl are forgetting what they own, we could take data from blockchain and make a document with which address has which overlay and what amount so they could search for it there and take the funds out for themselves.

@0xCardinalError 0xCardinalError changed the title Neighbourhood hopping (new staking - part 1 - SWIP-19) Neighbourhood hopping (new staking - part 1 and 2 - SWIP-19 and 20 combined) Aug 26, 2024
@0xCardinalError 0xCardinalError changed the title Neighbourhood hopping (new staking - part 1 and 2 - SWIP-19 and 20 combined) New staking - part 1 and 2 - SWIP-19 and 20 combined Aug 26, 2024
@0xCardinalError 0xCardinalError merged commit 1728975 into master Aug 26, 2024
1 check passed
@0xCardinalError 0xCardinalError deleted the new_staking branch August 26, 2024 16:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants