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

switch to websockets #358

Open
wants to merge 11 commits into
base: master
Choose a base branch
from
15 changes: 14 additions & 1 deletion scripts/__mocks__/global.js
Original file line number Diff line number Diff line change
@@ -1 +1,14 @@
export const blockCount = 1504903;
import { getEventEmitter } from '../event_bus.js';
import { vi } from 'vitest';

export let blockCount = 1504903;

export const start = vi.fn(() => {
subscribeToNetworkEvents();
});

const subscribeToNetworkEvents = vi.fn(() => {
getEventEmitter().on('new-block', (block) => {
blockCount = block;
});
});
11 changes: 10 additions & 1 deletion scripts/__mocks__/network.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { vi } from 'vitest';
import { Transaction } from '../transaction.js';
import { getEventEmitter } from '../event_bus.js';
import { sleep } from '../utils.js';

export const getNetwork = vi.fn(() => {
return globalNetwork;
Expand Down Expand Up @@ -85,7 +87,7 @@
* @param{number} blockHeight
* @param{boolean} skipCoinstake
*/
(blockHeight, skipCoinstake = false) => {

Check warning on line 90 in scripts/__mocks__/network.js

View workflow job for this annotation

GitHub Actions / Run linters

'skipCoinstake' is assigned a value but never used
if (!this.#mapBlocks.has(blockHeight)) {
throw new Error('Requested block does not exist!');
}
Expand All @@ -106,6 +108,10 @@
this.#nextBlock.reset();
this.#nextBlock.blockHeight = this.#blockHeight + 1;
}
mintAndEmit() {
this.mintBlock();
getEventEmitter().emit('new-block', this.#blockHeight);
}
}

/**
Expand Down Expand Up @@ -157,6 +163,9 @@

let globalNetwork = new TestNetwork();

export function resetNetwork() {
export async function resetNetwork() {
globalNetwork = new TestNetwork();
// Update the global variable blockCount
getEventEmitter().emit('new-block', globalNetwork.getBlockCount());
await sleep(100);
}
2 changes: 1 addition & 1 deletion scripts/chain_params.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export const COIN = 10 ** 8;
export const MAX_ACCOUNT_GAP = 20;

/** The batch size of Shield block synchronisation */
export const SHIELD_BATCH_SYNC_SIZE = 32;
export const SHIELD_BATCH_SYNC_SIZE = 16;

/** Transaction Sapling Version */
export const SAPLING_TX_VERSION = 3;
Expand Down
31 changes: 10 additions & 21 deletions scripts/global.js
Original file line number Diff line number Diff line change
Expand Up @@ -247,16 +247,13 @@ export async function start() {

subscribeToNetworkEvents();
// Make sure we know the correct number of blocks
await refreshChainData();
await updateBlockCount();
// Load the price manager
cOracle.load();

// If allowed by settings: submit a simple 'hit' (app load) to Labs Analytics
getNetwork().submitAnalytics('hit');
setInterval(() => {
// Refresh blockchain data
refreshChainData();

// Fetch the PIVX prices
refreshPriceDisplay();
}, 15000);
Expand All @@ -274,6 +271,14 @@ export async function start() {
doms.domDashboard.click();
}

/**
* Updates the blockcount
* Must be called only on start and when toggling network
* @returns {Promise<void>}
*/
export async function updateBlockCount() {
blockCount = await getNetwork().getBlockCount();
}
async function refreshPriceDisplay() {
await cOracle.getPrice(strCurrency);
getEventEmitter().emit('balance-update');
Expand All @@ -287,7 +292,7 @@ function subscribeToNetworkEvents() {

getEventEmitter().on('new-block', (block) => {
debugLog(DebugTopics.GLOBAL, `New block detected! ${block}`);

blockCount = block;
// If it's open: update the Governance Dashboard
if (doms.domGovTab.classList.contains('active')) {
updateGovernanceTab();
Expand Down Expand Up @@ -1654,22 +1659,6 @@ export async function createProposal() {
}
}

export async function refreshChainData() {
const cNet = getNetwork();
// If in offline mode: don't sync ANY data or connect to the internet
if (!cNet.enabled)
return console.warn(
'Offline mode active: For your security, the wallet will avoid ALL internet requests.'
);

// Fetch block count
const newBlockCount = await cNet.getBlockCount();
if (newBlockCount !== blockCount) {
blockCount = newBlockCount;
getEventEmitter().emit('new-block', blockCount);
}
}

// A safety mechanism enabled if the user attempts to leave without encrypting/saving their keys
export const beforeUnloadListener = (evt) => {
evt.preventDefault();
Expand Down
Loading