StakingToken icon indicating copy to clipboard operation
StakingToken copied to clipboard

Question about the array handling.

Open swtrse opened this issue 3 years ago • 4 comments

I see that the array will be searched every time- Is the position of the items inside the array different every time the contract get loaded into an client? If the position is fixed then maybe a more processor time friendly logic could be implemented.

swtrse avatar Sep 16 '21 15:09 swtrse

I agree. Using an array will lead to DOS attack.

SKFrozenCloud avatar Oct 10 '21 09:10 SKFrozenCloud

@swtrse position of item inside array is not fixed. When item is deleted, the last element is swapped with item to be deleted index and the last element is removed/pop. https://github.com/HQ20/StakingToken/blob/master/contracts/StakingToken.sol#L136

jaglinux avatar Oct 21 '21 07:10 jaglinux

Thanks, that clears it up for this specific implementation. That helps too but what I wanted to know is if there is something possible like a queue where you push new elements on the end and pop old elements of the front. I do not have an concrete implementation in mind but I want to get a feeling for the underlying concepts. So for example if I generate an array in C and fill the array with values. I can take it for granted that the order of the items is always the same for the lifetime of the array. However in an distributed environment this has not to be the case and I was wondering if the order of elements will be the same for each distributed client or if each client can hold a different order of array elements because an array is not ordered per se and order should not matter at least most of the times..

swtrse avatar Oct 24 '21 06:10 swtrse

Main issue is when someone wants to removeStake(). Instead of swapping, we can just make the value as 0. Then we can maintain the index position, new users will be pushed at the end as usual. Downside is that the array will keep growing.

jaglinux avatar Oct 25 '21 20:10 jaglinux