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

Concurrent Balance modification in ConsolidateRewards when all rewards consolidated #3001

Closed
kuegi opened this issue Aug 17, 2024 · 0 comments

Comments

@kuegi
Copy link
Contributor

kuegi commented Aug 17, 2024

ConsolidateRewards expects the List to contain each address only once. If it occurs multiple times, the same address might be consolidated multiple times "at once" (on different threads) which can lead to rewards being added multiple times in error.

The method doesn't even need a List of owner:balance as input but should rather receive a set which in itself already guarantees the uniqueness.

This is most likely also the reason for the chain split a few weeks ago where a MN used consolidaterewards and had an address get the rewards added multiple times.

ain/src/init.cpp

Lines 2207 to 2215 in cd46ef7

LogPrintf("Consolidate rewards for all addresses..\n");
std::vector<std::pair<CScript, CAmount>> balancesToMigrate;
pcustomcsview->ForEachBalance([&](CScript const& owner, CTokenAmount balance) {
if (balance.nValue > 0) {
balancesToMigrate.emplace_back(owner, balance.nValue);
}
return true;
});
ConsolidateRewards(*pcustomcsview, ::ChainActive().Height(), balancesToMigrate, true);

kuegi added a commit to kuegi/ain that referenced this issue Aug 17, 2024
Bushstar added a commit that referenced this issue Sep 6, 2024
* started test file
TD not working yet, no idea why

* fixed test setup

* fmt

* adapted test

* added LockToken function to validation and trigger on fork height

already doing paybackwithcollateral

* added SC address for TokenLock

* added DB entries for TokenLock UserBalances

* split up ProcessTokenLock for easier reading

* splitting loantokens

leads to EVM-block hash mismatch. no idea why yet.

* first shot on adding coinbase txs and pool splits.

* got pool "splits" and tokens working

EVM hash still wrong. no idea why.

* added collateral to tokenSplit

* added todos

* locking away tokens from pools, collateral and balances

* refactor: extract lockup method

* added rpc calls, improved amount minted

* added function to payback loans directly or from collateral

DUSD is burned directly for dToken loans

* refactor and added swapping of collateral if there is a pool

* fmt

* fmt

* improved tests

* fix payback in DUSD case with DUSD collateral

* added further testcase, fix missing interest on paybackswaps

* first shot on lock-release tx

* tx working, tests not updated yet.

need to figure out why locked tokens sometimes have fi difference

* improved poolsplit logic

fixed tests

* added composite swaps on payback, also considering fees now

* added cancelation of Auctions

* added lock during TD

* added handling of locks to stocksplits

* fix TD lock to only lock when old token is transferred

* fix: locked tokens must not be upgraded on EVM

* refactor and fmt

* improvements from review

* review refactor, updated test values

* prevent circular dep warning

* refactor: remove dependency and mark circular as expected

* reenable blockhash error

still fails. still no idea why.

* moved restart logic to DF25

* lint fixes

* fmt

* revert unneeded changes

* Fix compilation error. Use snapshots.

* remove new poolsplit logic into seperate PR

* Miner logic, increment ID for each token and add rename for DUSD

* adapted test data to seperated split logic

* Remove code repetition

* Remove copy pasted comment

* Move USDD creation to be last miner side

* Remove rename of DUSD. Unlock new tokens.

* Update closing of vault auctions

* Trigger dToken restart via attributes

* Allow dToken restart to fail

* Format CPP

* added check to only allow lock if not locked before

* Fix broken attribute logic

* fix payback return value, added early returns on failure

* Add lock ratio to attributes

* added attribute check and improved logs and comments

* Add missing setting of attributes. Format CPP.

* Print subLoan without debug

* Payback balance available

* SubLoanToken after amount calculated

* added check for owner balance at the start to reduce complexity later

* fix handling of owner balance in case of multiple vaults per owner

* improved logging

* refactor to consolidate rewards before locking and cache pools

and better logging

* fix handling of payback if no DUSD collateral left

* fixed floating errors on payback, removed unnecessary logging

* fail if loans left after payback

* fix calc of rewards before token split

* add reward consolidation before loan payback

* remove unnecessary log

* move to DF24

* fix logs

* Add missing forks to test

* Add prefix checks and fix overlaps

* Add another missing fork

* fixed tests

* lint: format CPP

* added helper for fork params in tests

added fixme with weird double-commission reference

* fix: added ensure uniqueness of owners in ConsolidateRewards

* fix cncurrency issue of ConsolidateRewards

fixes #3001

* improve history: only 1 entry per address for tokenlock

added tests for history entries

* Post merge changes

* Do not add EVM TXs if prices are invalid

* added test checks for history. currently weird behaviour

* fix comment in test

* Update test

* lint: format Python

* lint: format CPP

* Calculate static rewards by getting start balance from the block before

* lint: format Python

* lint: Format CPP

* Migrate commission on split

* Get tokens direct from attributes

* Fix commission bug

* Push failing test

* fix rounding bug on collateral swap estimation

* fix test values after rounding fix

* Push failing minimal balances test

* adapt locking logic: in case of rounding errors, add the extra fi to lock.

* Add interest test

* lint: remove unused import

* Avoid clash with other pending changes

* Update member outside of static function

---------

Co-authored-by: Prasanna Loganathar <pvl@prasannavl.com>
Co-authored-by: Bushstar <bushsolo@gmail.com>
This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant