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] Network simulator and some initial tests #364

Merged
merged 10 commits into from
May 6, 2024

Conversation

panleone
Copy link
Member

@panleone panleone commented May 1, 2024

Abstract

The aim of this PR is improving the unit test coverage by adding a mock to the network class.

How to use it in unit tests:

  1. Mock the file vi.mock('../../../scripts/network.js')
  2. Create a bunch of wallets with the functions setUpLegacyMainnetWallet(). Those wallets have a non-zero balance and are synced.
  3. After creating txs with any wallet call getNetwork().sendTransaction(txHex)
  4. Once enough txs are added mint a new block with getNetwork().mintBlock()
  5. Trigger download of the block for all wallets in the test by calling the global.js function refreshChainData();

Notice that calling refreshChainData() will emit a signal that is received by wallets as

 getEventEmitter().on('new-block', async (block) => {
            if (this.#isSynced) {
                await this.getLatestBlocks(block);
                getEventEmitter().emit('new-tx');
            }
        });

which makes tests reliable, in the sense that the normal sync flow is followed, with the only difference that instead of blockbook, blocks are provided by the mock class.

I have also added a couple of tests:

  • Create a bunch of txs between a HD and a Legacy wallets and verify that txs are received and balance is updated as expected
  • Verify that HD wallets are aware of at least MAX_ACCOUNT_GAP addresses after the last used

Testing

Tests working as expected

@panleone panleone added the Test Addition of unit or functional test label May 1, 2024
@panleone panleone requested review from JSKitty and Duddino May 1, 2024 16:21
@panleone panleone self-assigned this May 1, 2024
Copy link

netlify bot commented May 1, 2024

Deploy Preview for cheery-moxie-4f1121 ready!

Name Link
🔨 Latest commit 899e48a
🔍 Latest deploy log https://app.netlify.com/sites/cheery-moxie-4f1121/deploys/6634fdea0566e10008c86b8b
😎 Deploy Preview https://deploy-preview-364--cheery-moxie-4f1121.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site configuration.

@panleone
Copy link
Member Author

panleone commented May 1, 2024

new tests will fail until #363 is merged (which will solve the sync bug)

Copy link
Member

@Duddino Duddino left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These looks good, my only nit is that these are no longer unit tests, so just move them in a different folder

@panleone
Copy link
Member Author

panleone commented May 3, 2024

These looks good, my only nit is that these are no longer unit tests, so just move them in a different folder

done

Copy link
Member

@Duddino Duddino left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ACK nice tests

Copy link
Member

@JSKitty JSKitty left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

tACK 899e48a

@JSKitty JSKitty merged commit ea2ade7 into PIVX-Labs:master May 6, 2024
5 checks passed
@panleone panleone deleted the network_mocks branch October 1, 2024 07:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Test Addition of unit or functional test
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants