You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I find this code is becoming too complex to fully understand. Let's extract a helper function and write some unit test for it please. There is no need to test it extensively, but there should be a test reproducing the bug you discovered.
Illustration to show what I mean:
// in deal-observer-backend.jsconstcurrentChainHead=awaitgetChainHead(makeRpcRequest)constcurrentFinalizedChainHead=currentChainHead.Height-finalityEpochsconstlastInsertedDeal=awaitfetchDealWithHighestActivatedEpoch(pgPool)conststartEpoch=getNextEpochToProcess(currentFinalizedChainHead,lastInsertedDeal)for(letepoch=startEpoch;epoch<=currentFinalizedChainHead;epoch++){awaitobserveBuiltinActorEvents(epoch,pgPool,makeRpcRequest)}
(The trick is to create a pure function that does not interact with blockchain or the DB, which makes the function easy to test.)
// testsdescribe('getNextEpochToProcess',()=>{it('returns the finalized head when no deal was observed yet',async()=>{constcurrentFinalizedChainHead=123constlastInsertedDeal=undefinedconstnextEpoch=getNextEpochToProcess(currentFinalizedChainHead,lastInsertedDeal)assert.strictEqual(nextEpoch,currentFinalizedChainHead)})it('returns the finalized head when the DB is empty',async()=>{constcurrentFinalizedChainHead=123constlastInsertedDeal=/* create a deal object with activated_at_epoch set to 1 */constnextEpoch=getNextEpochToProcess(currentFinalizedChainHead,lastInsertedDeal)assert.strictEqual(nextEpoch,lastInsertedDeal.activated_at_epoch+1)})_Originallypostedby @bajtosinhttps://github.com/CheckerNetwork/deal-observer/pull/55#pullrequestreview-2584239377_
The text was updated successfully, but these errors were encountered:
The goal of the implementation is to extract the functionality of the deal observer loop into its own function so that it can be properly tested.
There are three components to the loop function as it currently exists:
The loop try and catch block
The business logic of the loop
Data collection through telemetry
Component number 2 is what needs testing.
The implementation will extract the business logic of the loop into its own function that is stored in the path /backend/lib/deal-observer.js.
The tests created will only test the business logic, especially the handling of the different block indices that are used to determine the next fetched block (lastStoredDeal.activated_at_peoch, maxPastEpochs,finalityEpochs).
Illustration to show what I mean:
(The trick is to create a pure function that does not interact with blockchain or the DB, which makes the function easy to test.)
The text was updated successfully, but these errors were encountered: