I am trying to implement the archive functionality for ICRC3
I was learning form the ICDev implementation: GitHub - PanIndustrial-Org/icrc3.mo: ICRC3 in motoko
In this implementation, they start to archive the ledger state by starting a timer with a period of 0 seconds:
f(Vec.size(state.ledger) > state.constants.archiveProperties.maxActiveRecords){
state.cleaningTimer := ?Timer.setTimer<system>(#seconds(0), check_clean_up); '
Inside the timer function “check_clean_up” , they do everything necessary to create an archive canister: create an instance of archive canister, give cycles, and stop the timer once all ledger blocks are archived.
I cannot understand why they use a timer. Why can’t they just call a function?
add_record is a synchronous function and we did not want to convert it to async so that it could be used in other sync functions. The timer makes the clean_up run out of band so that the user sending a transaction doesn’t have to wait for potentially a new canister to be created and then records sent to it.
If we had called it directly we would have had to make the return type async.
What is the motivation of doing add_record synchronous? Do you want to make sure that the record is introduced inthe ledger before the client gets a response? if this is the case, why?
Synchronous is almost always better from a atomicity point of view.
We want to return to the user quickly and the archiving is an offline operation that no one needs to wait for.
With ICRC-4 you may hit the archive threshold much more often(sometime maybe even twice in the same transaction). Doing in a different execution context lets the archivers settle it self out during high volume situations.