Skip to content

Commit

Permalink
New Network sync model
Browse files Browse the repository at this point in the history
  • Loading branch information
panleone committed Oct 12, 2023
1 parent 0481fb9 commit 2b1fe20
Show file tree
Hide file tree
Showing 19 changed files with 426 additions and 500 deletions.
2 changes: 1 addition & 1 deletion locale/de/translation.js
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ export default {
activitySentToSelf: 'Umbuchung', //Self
activityShieldedAddress: 'Geschützte Adresse', //Shielded address
activityReceivedShield: 'Erhalten von geschützter Adresse', //Received from Shielded address
activityReceivedFrom: 'Erhalten von', //Received from
activityReceivedWith: '', //Received with {}
activityDelegatedTo: 'Delegiert an', //Delegated to
activityUndelegated: 'Abberufen', //Undelegated
activityUnknown: 'Unbekannte Trasaktion', //Unknown Tx
Expand Down
2 changes: 1 addition & 1 deletion locale/en/translation.js
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ export default {
activitySentTo: 'Sent to {r}',
activitySelf: 'self',
activityShieldedAddress: 'Shielded address',
activityReceivedFrom: 'Received from {s}',
activityReceivedWith: 'Received with {s}',
activityDelegatedTo: 'Delegated to {r}',
activityUndelegated: 'Undelegated',
activityUnknown: 'Unknown Tx',
Expand Down
2 changes: 1 addition & 1 deletion locale/es-mx/translation.js
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ export default {
activitySentTo: 'Enviar a {r}', //Sent to
activitySelf: 'ti mismo', //Self
activityShieldedAddress: 'Dirección Shielded', //Shielded address
activityReceivedFrom: 'Recibido de {s}', //Received from
activityReceivedWith: '', //Received with {}
activityDelegatedTo: 'Delegado a {r}', //Delegated to
activityUndelegated: 'No delegado', //Undelegated
activityUnknown: 'Tx desconocido', //Unknown Tx
Expand Down
2 changes: 1 addition & 1 deletion locale/fr/translation.js
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ export default {
activitySentTo: 'Envoyé à {r}', //Sent to
activitySelf: 'soi-même', //Self
activityShieldedAddress: 'Adresse protégée', //Shielded address
activityReceivedFrom: 'Reçu de {s}', //Received from
activityReceivedWith: '', //Received with {}
activityDelegatedTo: 'Délégué à {r}', //Delegated to
activityUndelegated: 'Non délégué', //Undelegated
activityUnknown: 'Transaction inconnu', //Unknown Tx
Expand Down
2 changes: 1 addition & 1 deletion locale/it/translation.js
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ export default {
activitySentTo: 'Inviato a {r}', //Sent to
activitySelf: 'te stesso', //Self
activityShieldedAddress: 'Indirizzo protetto', //Shielded address
activityReceivedFrom: 'Ricevuto da {s}', //Received from
activityReceivedWith: '', //Received with {}
activityDelegatedTo: 'Delegato a {r}', //Delegated to
activityUndelegated: 'Non delegato', //Undelegated
activityUnknown: 'Tx sconosciuta', //Unknown Tx
Expand Down
2 changes: 1 addition & 1 deletion locale/ph/translation.js
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ export default {
activitySentTo: 'Naipadala sa {r}', //Sent to
activitySelf: 'sarili', //Self
activityShieldedAddress: 'Shielded address', //Shielded address
activityReceivedFrom: 'Natanggap mula sa {s}', //Received from
activityReceivedWith: '', //Received with {}
activityDelegatedTo: 'Delegated to {r}', //Delegated to
activityUndelegated: 'Undeligated', //Undelegated
activityUnknown: 'Unknown Tx', //Unknown Tx
Expand Down
2 changes: 1 addition & 1 deletion locale/pt-br/translation.js
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ export default {
activitySentTo: 'Enviado para {r}', //Sent to
activitySelf: 'si mesmo', //Self
activityShieldedAddress: 'Endereço Protegido', //Shielded address
activityReceivedFrom: 'Recebido de {s}', //Received from
activityReceivedWith: '', //Received with {}
activityDelegatedTo: 'Delegado a {r}', //Delegated to
activityUndelegated: 'Não é Delegado', //Undelegated
activityUnknown: 'Tx desconhecido', //Unknown Tx
Expand Down
2 changes: 1 addition & 1 deletion locale/pt-pt/translation.js
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ export default {
activitySentTo: 'Enviado para {r}', //Sent to
activitySelf: 'si mesmo', //Self
activityShieldedAddress: 'Endereço Protegido', //Shielded address
activityReceivedFrom: 'Recebido de {s}', //Received from
activityReceivedWith: '', //Received with {}
activityDelegatedTo: 'Delegado a {r}', //Delegated to
activityUndelegated: 'Não é Delegado', //Undelegated
activityUnknown: 'Tx desconhecido', //Unknown Tx
Expand Down
2 changes: 1 addition & 1 deletion locale/template/translation.js
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ export default {
activitySentTo: '', //Sent to {}
activitySelf: '', // self
activityShieldedAddress: '', //Shielded address
activityReceivedFrom: '', //Received from {}
activityReceivedWith: '', //Received with {}
activityDelegatedTo: '', // Delegated to {}
activityUndelegated: '', //Undelegated
activityUnknown: '', //Unknown Tx
Expand Down
2 changes: 1 addition & 1 deletion locale/uwu/translation.js
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ export default {
activitySentTo: 'Sentu to {r}', //Sent to
activitySelf: 'selfu', //Self
activityReceivedShield: 'Shielded addwess', //Received from Shielded address
activityReceivedFrom: 'Recewived fwom {s}', //Received from
activityReceivedWith: '', //Received with {}
activityDelegatedTo: 'Delegwated to {r}', //Delegated to
activityUndelegated: 'Undelegwated', //Undelegated
activityUnknown: 'Unknown Tx', //Unknown Tx
Expand Down
103 changes: 38 additions & 65 deletions scripts/Activity.vue
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
<script setup>
import { ref, computed, watch } from 'vue';
import { getNetwork, HistoricalTxType } from './network.js';
import { getNetwork, Network } from './network.js';
import { wallet } from './wallet.js';
import { mempool } from './global.js';
import { cChainParams } from './chain_params.js';
import { translation } from './i18n.js';
import { Database } from './database.js';
import { HistoricalTx, HistoricalTxType } from './mempool';
import { getNameOrAddress } from './contacts-book.js';
const props = defineProps({
Expand Down Expand Up @@ -35,7 +36,7 @@ const txMap = computed(() => {
[HistoricalTxType.RECEIVED]: {
icon: 'fa-plus',
colour: '#5cff5c',
content: translation.activityReceivedFrom,
content: translation.activityReceivedWith,
},
[HistoricalTxType.DELEGATION]: {
icon: 'fa-snowflake',
Expand All @@ -55,64 +56,44 @@ const txMap = computed(() => {
};
});
async function update(fNewOnly = false, sync = true) {
async function update(txToAdd = 0) {
const cNet = getNetwork();
// If mempool is not loaded yet do not update the activity GUI
// or we might end up in a state where many of our addresses are considered invalid
if (!mempool.isLoaded) {
// Return if wallet is not synced yet
if (!cNet || !cNet.fullSynced) {
return;
}
if (!cNet) return;
// Prevent the user from spamming refreshes
if (updating.value) return;
let newTxs = [];
let arrTXs;
try {
// Set the updating animation
updating.value = true;
// If our txCount is lower than the tx history loaded
// Use the txhistory array, otherwise sync more
if (txCount !== cNet.arrTxHistory.length || !sync) {
arrTXs = cNet.arrTxHistory;
} else {
arrTXs = await cNet.syncTxHistoryChunk(fNewOnly);
}
// If the network has changed, or the sync has failed
// Ignore the array
if (!arrTXs || cNet !== getNetwork()) return;
txCount = arrTXs.length;
} finally {
updating.value = false;
}
// Check if all transactions are loaded
isHistorySynced.value = cNet.isHistorySynced;
// For Staking: Filter the list for only Stakes, display total rewards from known history
if (props.rewards) {
const arrStakes = arrTXs.filter(
(a) => a.type === HistoricalTxType.STAKE
);
if (arrStakes.length !== txs.length) {
const nRewards = arrStakes.reduce((a, b) => a + b.amount, 0);
rewardsText.value = `${cNet.isHistorySynced ? '' : ''}${nRewards}`;
parseTXs(arrStakes);
return;
// Set the updating animation
updating.value = true;
let found = 0;
const nHeights = Array.from(mempool.orderedTxmap.keys()).sort();
while (found < txCount + txToAdd) {
if (nHeights.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;
}
if (txs.length !== arrTXs.length) parseTXs(arrTXs);
const arrTXs = await wallet.toHistoricalTXs(newTxs);
parseTXs(arrTXs);
txCount = found;
updating.value = false;
}
watch(translation, async () => await update(false, false));
watch(translation, async () => await update());
/**
* Parse tx to list syntax
* @param {Array<HistoricalTx>} arrTXs
*/
async function parseTXs(arrTXs) {
const newTxs = [];
Expand Down Expand Up @@ -178,15 +159,12 @@ async function parseTXs(arrTXs) {
formattedAmt = cTx.amount.toFixed(2);
}
// For 'Send' or 'Receive' TXs: Check if this is a send-to-self transaction
// For 'Send' TXs: Check if this is a send-to-self transaction
let fSendToSelf = false;
if (
cTx.type === HistoricalTxType.SENT ||
cTx.type === HistoricalTxType.RECEIVED
) {
if (cTx.type === HistoricalTxType.SENT) {
fSendToSelf = true;
// Check all addresses to find our own, caching them for performance
for (const strAddr of cTx.receivers.concat(cTx.senders)) {
for (const strAddr of cTx.receivers) {
// If a previous Tx checked this address, skip it, otherwise, check it against our own address(es)
if (!(await wallet.isOwnAddress(strAddr))) {
// External address, this is not a self-only Tx
Expand All @@ -199,35 +177,30 @@ async function parseTXs(arrTXs) {
let { icon, colour, content } = txMap.value[cTx.type];
const match = content.match(/{(.)}/);
if (match) {
// Use the senders array if `s` was provided in the template
// Use the receivers array if `r` was provided
const where = {
r: 'receivers',
s: 'senders',
}[match[1]];
let who = '';
if (fSendToSelf) {
who = translation.activitySelf;
} else if (cTx.shieldedOutputs) {
who = translation.activityShieldedAddress;
} else {
const arrExternalAddresses = (
const arrAddresses = (
await Promise.all(
cTx[where].map(async (addr) => [
cTx.receivers.map(async (addr) => [
await wallet.isOwnAddress(addr),
addr,
])
)
)
.filter(([isOwnAddress, _]) => {
return !isOwnAddress;
return cTx.type === HistoricalTxType.RECEIVED
? isOwnAddress
: !isOwnAddress;
})
.map(([_, addr]) => getNameOrAddress(cAccount, addr));
who =
[
...new Set(
arrExternalAddresses.map((addr) =>
arrAddresses.map((addr) =>
addr?.length >= 32
? addr?.substring(0, 6)
: addr
Expand Down Expand Up @@ -255,7 +228,7 @@ async function parseTXs(arrTXs) {
function reset() {
txs.value = [];
txCount = 0;
update(false);
update(0);
}
function getTxCount() {
Expand Down Expand Up @@ -349,7 +322,7 @@ defineExpose({ update, reset, getTxCount });
<button
v-if="!isHistorySynced"
class="pivx-button-medium"
@click="update()"
@click="update(10)"
>
<span class="buttoni-icon"
><i
Expand Down
2 changes: 2 additions & 0 deletions scripts/chain_params.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ export const cChainParams = reactive({
PUBKEY_PREFIX: ['D'],
STAKING_PREFIX: 'S',
PUBKEY_ADDRESS: 30,
STAKING_ADDRESS: 63,
SECRET_KEY: 212,
BIP44_TYPE: 119,
BIP44_TYPE_LEDGER: 77,
Expand Down Expand Up @@ -61,6 +62,7 @@ export const cChainParams = reactive({
PUBKEY_PREFIX: ['x', 'y'],
STAKING_PREFIX: 'W',
PUBKEY_ADDRESS: 139,
STAKING_ADDRESS: 73,
SECRET_KEY: 239,
BIP44_TYPE: 1,
BIP44_TYPE_LEDGER: 1,
Expand Down
13 changes: 5 additions & 8 deletions scripts/global.js
Original file line number Diff line number Diff line change
Expand Up @@ -512,8 +512,8 @@ function subscribeToNetworkEvents() {
getEventEmitter().on('new-block', (block, oldBlock) => {
console.log(`New block detected! ${oldBlock} --> ${block}`);
// Fetch latest Activity
activityDashboard.update(true);
stakingDashboard.update(true);
activityDashboard.update();
stakingDashboard.update();

// If it's open: update the Governance Dashboard
if (doms.domGovTab.classList.contains('active')) {
Expand Down Expand Up @@ -582,13 +582,10 @@ export function openTab(evt, tabName) {
stakingDashboard.getTxCount() === 0
) {
// Refresh the TX list
stakingDashboard.update(false);
} else if (
tabName === 'keypair' &&
getNetwork().arrTxHistory.length === 0
) {
stakingDashboard.update();
} else if (tabName === 'keypair' && activityDashboard.getTxCount() === 0) {
// Refresh the TX list
activityDashboard.update(false);
activityDashboard.update();
}
}

Expand Down
Loading

0 comments on commit 2b1fe20

Please sign in to comment.