threefoldtech / tfchain

Threefold Chain.
Apache License 2.0
15 stars 11 forks source link

Refactor/remove lock balance logic in billing flow #990

Open sameh-farouk opened 2 months ago

sameh-farouk commented 2 months ago

Choosing between locking and distributing less frequently, or not locking and distributing more frequently could be debatable as both actions require modifications to the account balances storage, which involves computational resources.

Locking Tokens: Each lock is recorded in the state, and removing or updating a lock involves additional state changes.

Transferring Tokens: Each transfer results in changes to the state (balance updates) for both the sender and the receiver. Also, distributing tokens in our logic involves multiple transfers to (foundation, sales, staking pool, and service provider) accounts.

The advantages of the current flow:

The disadvantages:

I believe that the current approach, if works correctly (which is not the case), is generally more efficient in terms of network usage. However, I propose simplifying the logic by transferring overdue amounts directly every cycle. This is because the added complexity of locking brings many flaws that cannot be justified by the potential savings in network usage.

We can still benefit from both worlds by changing the billing cycle to occur every 24 hours instead of hourly, aligning it with the cultivation distribution schedule. This approach consolidates multiple transfers into fewer transactions, reducing the computational load on the network to even lower levels than what we have currently.

Additional context

sameh-farouk commented 1 month ago

We may also consider replacing locks with reserves/holds, which would simplify the flow and make it less error-prone.

sameh-farouk commented 1 month ago

Update: Here is a sum of the changes so far

Bug Fixes

completed: [x] Reserve Mechanism [X] overdraft tracker [x] better logging [x] better audibility [x] lazy migration [X] Adjust billing tests

Still WIP: