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

Test Main Loop Function #61

Closed
NikolasHaimerl opened this issue Jan 31, 2025 · 1 comment · Fixed by #81
Closed

Test Main Loop Function #61

NikolasHaimerl opened this issue Jan 31, 2025 · 1 comment · Fixed by #81

Comments

@NikolasHaimerl
Copy link
Contributor

          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.js
const currentChainHead = await getChainHead(makeRpcRequest)
const currentFinalizedChainHead = currentChainHead.Height - finalityEpochs
const lastInsertedDeal = await fetchDealWithHighestActivatedEpoch(pgPool)
const startEpoch = getNextEpochToProcess(currentFinalizedChainHead, lastInsertedDeal)

for (let epoch = startEpoch; epoch <= currentFinalizedChainHead; epoch++) {
  await observeBuiltinActorEvents(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.)

// tests
describe('getNextEpochToProcess', () => {
  it('returns the finalized head when no deal was observed yet', async () => {
    const currentFinalizedChainHead = 123
    const lastInsertedDeal = undefined

    const nextEpoch = getNextEpochToProcess(currentFinalizedChainHead, lastInsertedDeal)

    assert.strictEqual(nextEpoch, currentFinalizedChainHead)
  })

  it('returns the finalized head when the DB is empty', async () => {
    const currentFinalizedChainHead = 123
    const lastInsertedDeal = /* create a deal object with activated_at_epoch set to 1 */

    const nextEpoch = getNextEpochToProcess(currentFinalizedChainHead, lastInsertedDeal)

    assert.strictEqual(nextEpoch, lastInsertedDeal.activated_at_epoch + 1)
  })

_Originally posted by @bajtos in https://github.com/CheckerNetwork/deal-observer/pull/55#pullrequestreview-2584239377_
            
@NikolasHaimerl
Copy link
Contributor Author

Implementation Plan:

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:

  1. The loop try and catch block
  2. The business logic of the loop
  3. 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).

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

Successfully merging a pull request may close this issue.

1 participant