Open fulldecent opened 11 months ago
Thanks for the Issue! I'm a new bee for Solidity(only about 1 ~ 2 months), so I prefer to use C-like algorithms to solve problems. I'm very inspired by your idea!
However, according to the documentation (doc/erc/ideas.md), items with the same score in the sorted list MUST return different rankings, which is related to the reward algorithm at reward cycle end. Therefore, if getRanking() returns the same rankings for the same values, which would cause an incorrect rewards.
I'll continue to refer to your thoughts on optimizing sorted lists.
Thanks again for your Issue!
This approach here also supports giving the lower ranking if there is a tie.
So 20, 20, 20, 30 would produce ranking of 3, 3, 3, 4.
In Solidity I have not seen projects that sort data on on-chain because it is so expensive. Let's benchmark it well and decide the acceptable gas. It may be necessary to pick a different game mechanism if the gas costs are prohibitive.
According to the gas cost reported by npm package hardhat-gas-reporter, if we insert 40 items which has random hash and score(0-1000) to 32 length list ,it costs: | Contract | Method | Min | Max | Avg | # calls |
---|---|---|---|---|---|---|
TestList | addScore | 27415 | 231323 | 147442 | 83 | |
TestList | setMaxLen | 29674 | 46774 | 38224 | 4 |
when insert into a 16 length list, it costs: | Contract | Method | Min | Max | Avg | # calls |
---|---|---|---|---|---|---|
TestList | addScore | 27415 | 155292 | 124574 | 83 | |
TestList | setMaxLen | 29674 | 46774 | 38224 | 4 |
I`m not familiar with gas limit but I think it may be acceptable?
Are those typical usage scenarios? We expecting 40 participants and each one only sets the score once?
Or will there be many participants and each one is updating the score frequently?
This new API is more efficient assuming
update
is run more often thanranking
.