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

Mempool refactor #291

Merged
merged 36 commits into from
Mar 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
36 commits
Select commit Hold shift + click to select a range
f6cdf58
Mempool refactor
Duddino Jan 22, 2024
96d5762
SPENDABLE->P2CS and P2PKH
Duddino Jan 22, 2024
de4e8d9
Remove debugger
Duddino Jan 22, 2024
257acd2
Move HistoricalTx to its own file
Duddino Jan 23, 2024
46d261c
Add mempool statuses unit tests
Duddino Jan 23, 2024
fa4b6ea
Improve debit test
Duddino Jan 23, 2024
e272e63
Change getUTXOs method to take a `filter` and `requirement`
Duddino Jan 23, 2024
399dda1
Fix masternodes
Duddino Jan 23, 2024
3cd2953
Fix mempool being slow
Duddino Feb 16, 2024
3426a55
Merge remote-tracking branch 'origin/master' into mempool_refactor
Duddino Feb 16, 2024
bf822ca
Merge branch 'master' into mempool_refactor
Duddino Feb 27, 2024
fe8495c
Post merge prettier
Duddino Feb 27, 2024
f137e34
Fix database
Duddino Feb 27, 2024
82b8f2f
Prettier
Duddino Feb 27, 2024
0b972c6
Merge branch 'master' into mempool_refactor
Duddino Feb 29, 2024
91cf1ea
Merge branch 'master' into mempool_refactor
Duddino Mar 4, 2024
deb0a82
Remove multimap dependency
Duddino Mar 5, 2024
b0bdd48
Fix wrong balance
Duddino Mar 5, 2024
ae2c7f1
Fix skipDatabase
Duddino Mar 6, 2024
e5dd411
Clean up comments and unnecessary imports
Duddino Mar 6, 2024
a237c27
Remove old mock
Duddino Mar 6, 2024
1b55bc7
Remove and add white spaces
Duddino Mar 6, 2024
0055760
Mock db in test
Duddino Mar 6, 2024
64e89f1
Merge remote-tracking branch 'origin/master' into mempool_refactor
Duddino Mar 6, 2024
8138b8a
Sort transactions
Duddino Mar 6, 2024
1402487
Fix fat finger keypress
Duddino Mar 7, 2024
752075a
Merge branch 'master' into mempool_refactor
Duddino Mar 7, 2024
007ead8
Update staking balance
Duddino Mar 7, 2024
312eb00
Simplify getCredit
Duddino Mar 7, 2024
31e2531
Fix undelegation and delegation balances
Duddino Mar 8, 2024
f119bc8
Merge branch 'master' into mempool_refactor
Duddino Mar 11, 2024
fa5513a
Merge remote-tracking branch 'origin/master' into mempool_refactor
Duddino Mar 11, 2024
85516f5
Stub localStorage
Duddino Mar 11, 2024
b9fbb35
Fix getUTXO call
Duddino Mar 12, 2024
b1c7bdc
Fix issue with requirement
Duddino Mar 12, 2024
bfde392
Fix trying to delegate <= 1 PIV
Duddino Mar 12, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@
"html-loader": "^4.2.0",
"html-webpack-plugin": "^5.5.0",
"mini-css-extract-plugin": "^2.7.2",
"multimap": "^1.1.0",
"node-polyfill-webpack-plugin": "^2.0.1",
"prettier": "^2.8.1",
"raw-loader": "^4.0.2",
Expand Down
24 changes: 0 additions & 24 deletions scripts/__mocks__/global.js

This file was deleted.

48 changes: 48 additions & 0 deletions scripts/__mocks__/mempool.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import { vi } from 'vitest';
import { UTXO, COutpoint } from '../transaction.js';

const Mempool = vi.fn();

Mempool.prototype.balance = 0.1 * 10 ** 8;
Mempool.prototype.coldBalance = 0;
Mempool.prototype.isSpent = vi.fn(() => false);
Mempool.prototype.setSpent = vi.fn();
Mempool.prototype.addTransaction = vi.fn();
Mempool.prototype.getUTXOs = vi.fn(() => [
new UTXO({
outpoint: new COutpoint({
txid: 'f8f968d80ac382a7b64591cc166489f66b7c4422f95fbd89f946a5041d285d7c',
n: 1,
}),
script: '76a914f49b25384b79685227be5418f779b98a6be4c73888ac',
value: 0.1 * 10 ** 8,
}),
]);
Mempool.prototype.outpointToUTXO = vi.fn((outpoint) => {
if (
outpoint.txid ===
'f8f968d80ac382a7b64591cc166489f66b7c4422f95fbd89f946a5041d285d7c' &&
outpoint.n === 1
) {
return new UTXO({
outpoint: new COutpoint({
txid: 'f8f968d80ac382a7b64591cc166489f66b7c4422f95fbd89f946a5041d285d7c',
n: 1,
}),
script: '76a914f49b25384b79685227be5418f779b98a6be4c73888ac',
value: 0.1 * 10 ** 8,
});
}
});
const OutpointState = {
OURS: 1 << 0, // This outpoint is ours

P2PKH: 1 << 1, // This is a P2PKH outpoint
P2CS: 1 << 2, // This is a P2CS outpoint

SPENT: 1 << 3, // This outpoint has been spent
IMMATURE: 1 << 4, // Coinbase/coinstake that it's not mature (hence not spendable) yet
LOCKED: 1 << 5, // Coins in the LOCK set
};

export { Mempool, OutpointState };
8 changes: 4 additions & 4 deletions scripts/charting.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
Tooltip,
} from 'chart.js';
import { cChainParams, COIN } from './chain_params.js';
import { doms, mempool } from './global.js';
import { doms } from './global.js';
import { Database } from './database.js';
import { translation } from './i18n.js';
import { wallet } from './wallet.js';
Expand Down Expand Up @@ -45,7 +45,7 @@ async function getWalletDataset() {
const arrBreakdown = [];

// Public (Available)
const spendable_bal = mempool.balance;
const spendable_bal = wallet.balance;
if (spendable_bal > 0) {
arrBreakdown.push({
type: translation.chartPublicAvailable,
Expand Down Expand Up @@ -74,7 +74,7 @@ async function getWalletDataset() {
});
}

const immature_bal = mempool.immatureBalance;
const immature_bal = wallet.immatureBalance;
if (immature_bal > 0) {
arrBreakdown.push({
type: translation.chartImmatureBalance,
Expand All @@ -83,7 +83,7 @@ async function getWalletDataset() {
});
}
// Staking (Locked)
const spendable_cold_bal = mempool.coldBalance;
const spendable_cold_bal = wallet.coldBalance;
if (spendable_cold_bal > 0) {
arrBreakdown.push({
type: 'Staking',
Expand Down
11 changes: 6 additions & 5 deletions scripts/composables/use_wallet.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { getEventEmitter } from '../event_bus.js';
import { hasEncryptedWallet, wallet } from '../wallet.js';
import { ref } from 'vue';
import { strCurrency } from '../settings.js';
import { mempool } from '../global.js';
import { cMarket } from '../settings.js';
import { ledgerSignTransaction } from '../ledger.js';

Expand All @@ -20,6 +19,7 @@ export function useWallet() {
const isViewOnly = ref(wallet.isViewOnly());
const getKeyToBackup = async () => await wallet.getKeyToBackup();
const isEncrypted = ref(true);
const loadFromDisk = () => wallet.loadFromDisk();
const hasShield = ref(wallet.hasShield());
// True only iff a shield transaction is being created
// Transparent txs are so fast that we don't need to keep track of them.
Expand Down Expand Up @@ -62,7 +62,7 @@ export function useWallet() {
const price = ref(0.0);
const sync = async () => {
await wallet.sync();
balance.value = mempool.balance;
balance.value = wallet.balance;
shieldBalance.value = await wallet.getShieldBalance();
pendingShieldBalance.value = await wallet.getPendingShieldBalance();
};
Expand All @@ -84,13 +84,13 @@ export function useWallet() {
}
const res = await network.sendTransaction(tx.serialize());
if (res) {
wallet.finalizeTransaction(tx);
wallet.addTransaction(tx);
}
};

getEventEmitter().on('balance-update', async () => {
balance.value = mempool.balance;
immatureBalance.value = mempool.immatureBalance;
balance.value = wallet.balance;
immatureBalance.value = wallet.immatureBalance;
currency.value = strCurrency.toUpperCase();
shieldBalance.value = await wallet.getShieldBalance();
pendingShieldBalance.value = await wallet.getPendingShieldBalance();
Expand Down Expand Up @@ -124,5 +124,6 @@ export function useWallet() {
price,
sync,
createAndSendTransaction,
loadFromDisk,
};
}
21 changes: 10 additions & 11 deletions scripts/dashboard/Activity.vue
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,10 @@
import { ref, computed, watch, onMounted } from 'vue';
import { getNetwork } from '../network.js';
import { wallet } from '../wallet.js';
import { mempool } from '../global.js';
import { COIN, cChainParams } from '../chain_params.js';
import { translation } from '../i18n.js';
import { Database } from '../database.js';
import { HistoricalTx, HistoricalTxType } from '../mempool';
import { HistoricalTx, HistoricalTxType } from '../historical_tx.js';
import { getNameOrAddress } from '../contacts-book.js';
import { getEventEmitter } from '../event_bus';

Expand Down Expand Up @@ -80,20 +79,20 @@ async function update(txToAdd = 0) {
if (txCount < 10 && txToAdd == 0) txToAdd = 10;

let found = 0;
const nHeights = Array.from(mempool.orderedTxmap.keys()).sort(
(a, b) => a - b
// Since ECMAScript 2019 .sort is stable.
// https://caniuse.com/mdn-javascript_builtins_array_sort_stable
const orderedTxs = Array.from(wallet.getTransactions()).sort(
(a, b) => a.blockHeight - b.blockHeight
);
while (found < txCount + txToAdd) {
if (nHeights.length == 0) {
if (orderedTxs.length == 0) {
isHistorySynced.value = true;
break;
}
const nHeight = nHeights.pop();
const txsAtnHeight = mempool.orderedTxmap.get(nHeight).filter((tx) => {
return props.rewards ? tx.isCoinStake() : true;
});
newTxs = newTxs.concat(txsAtnHeight);
found += txsAtnHeight.length;
const tx = orderedTxs.pop();
if (props.rewards && !tx.isCoinStake()) continue;
newTxs.push(tx);
found++;
}
const arrTXs = wallet.toHistoricalTXs(newTxs);
await parseTXs(arrTXs);
Expand Down
2 changes: 1 addition & 1 deletion scripts/dashboard/Dashboard.vue
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ import {
updateEncryptionGUI,
updateLogOutButton,
} from '../global';
import { mempool, refreshChainData } from '../global.js';
import { refreshChainData } from '../global.js';
import {
confirmPopup,
isXPub,
Expand Down
66 changes: 34 additions & 32 deletions scripts/database.js
Original file line number Diff line number Diff line change
Expand Up @@ -327,38 +327,40 @@ export class Database {
const store = this.#db
.transaction('txs', 'readonly')
.objectStore('txs');
return (await store.getAll()).map((tx) => {
const vin = tx.vin.map(
(x) =>
new CTxIn({
outpoint: new COutpoint({
txid: x.outpoint.txid,
n: x.outpoint.n,
}),
scriptSig: x.scriptSig,
sequence: x.sequence,
})
);
const vout = tx.vout.map(
(x) =>
new CTxOut({
script: x.script,
value: x.value,
})
);
return new Transaction({
version: tx.version,
blockHeight: tx.blockHeight,
blockTime: tx.blockTime,
vin: vin,
vout: vout,
valueBalance: tx.valueBalance,
shieldSpend: tx.shieldSpend,
shieldOutput: tx.shieldOutput,
bindingSig: tx.bindingSig,
lockTime: tx.lockTime,
});
});
return (await store.getAll())
.map((tx) => {
const vin = tx.vin.map(
(x) =>
new CTxIn({
outpoint: new COutpoint({
txid: x.outpoint.txid,
n: x.outpoint.n,
}),
scriptSig: x.scriptSig,
sequence: x.sequence,
})
);
const vout = tx.vout.map(
(x) =>
new CTxOut({
script: x.script,
value: x.value,
})
);
return new Transaction({
version: tx.version,
blockHeight: tx.blockHeight,
blockTime: tx.blockTime,
vin: vin,
vout: vout,
valueBalance: tx.valueBalance,
shieldSpend: tx.shieldSpend,
shieldOutput: tx.shieldOutput,
bindingSig: tx.bindingSig,
lockTime: tx.lockTime,
});
})
.sort((a, b) => a.blockHeight - b.blockHeight);
}
/**
* Remove all txs from db
Expand Down
Loading