Skip to content

Commit

Permalink
Ignore immature txs (#234)
Browse files Browse the repository at this point in the history
* Add coinbase/coinstake checker

* Add coinBaseMaturity chain param

* Use coinBaseMaturity instead of the harcoded 100

* Skip non-mature txs on all operations

* Fix unconfirmed activity

* fix confirmed balance again

* review fix
  • Loading branch information
panleone committed Oct 16, 2023
1 parent 65dc91c commit 55a74a9
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 3 deletions.
5 changes: 3 additions & 2 deletions scripts/Activity.vue
Expand Up @@ -161,9 +161,10 @@ async function parseTXs(arrTXs) {
// Update the time cache
prevTimestamp = cTx.time * 1000;
// Coinbase Transactions (rewards) require 100 confs
// Coinbase Transactions (rewards) require coinbaseMaturity confs
const fConfirmed =
cNet.cachedBlockCount - cTx.blockHeight >= props.rewards ? 100 : 6;
cNet.cachedBlockCount - cTx.blockHeight >=
(props.rewards ? cChainParams.current.coinbaseMaturity : 6);
// Choose the content type, for the Dashboard; use a generative description, otherwise, a TX-ID
// let txContent = props.rewards ? cTx.id : 'Block Reward';
Expand Down
2 changes: 2 additions & 0 deletions scripts/chain_params.js
Expand Up @@ -46,6 +46,7 @@ export const cChainParams = reactive({
// Network upgrades
UPGRADE_V6_0: undefined,
},
coinbaseMaturity: 100,
budgetCycleBlocks: 43200,
proposalFee: 50 * COIN,
proposalFeeConfirmRequirement: 6,
Expand Down Expand Up @@ -77,6 +78,7 @@ export const cChainParams = reactive({
// Network upgrades
UPGRADE_V6_0: undefined,
},
coinbaseMaturity: 15,
budgetCycleBlocks: 144,
proposalFee: 50 * COIN,
proposalFeeConfirmRequirement: 3,
Expand Down
27 changes: 26 additions & 1 deletion scripts/mempool.js
Expand Up @@ -7,7 +7,7 @@ import {
import { getEventEmitter } from './event_bus.js';
import Multimap from 'multimap';
import { wallet } from './wallet.js';
import { COIN } from './chain_params.js';
import { COIN, cChainParams } from './chain_params.js';

export class CTxOut {
/**
Expand All @@ -27,6 +27,9 @@ export class CTxOut {
* @type {Number} */
this.value = value;
}
isEmpty() {
return this.value == 0 && this.script == 'f8';
}
}
export class CTxIn {
/**
Expand Down Expand Up @@ -69,6 +72,22 @@ export class Transaction {
isConfirmed() {
return this.blockHeight != -1;
}
isCoinStake() {
return this.vout.length >= 2 && this.vout[0].isEmpty();
}
isCoinBase() {
// txid undefined happens only for coinbase inputs
return this.vin.length == 1 && !this.vin[0].outpoint.txid;
}
isMature() {
if (!(this.isCoinBase() || this.isCoinStake())) {
return true;
}
return (
getNetwork().cachedBlockCount - this.blockHeight >
cChainParams.current.coinbaseMaturity
);
}
}
/** An Unspent Transaction Output, used as Inputs of future transactions */
export class COutpoint {
Expand Down Expand Up @@ -228,6 +247,9 @@ export class Mempool {
getBalance(filter) {
let totBalance = 0;
for (const [_, tx] of this.txmap) {
if (!tx.isMature()) {
continue;
}
for (const vout of tx.vout) {
if (this.isSpent(vout.outpoint)) {
continue;
Expand Down Expand Up @@ -285,6 +307,9 @@ export class Mempool {
if (onlyConfirmed && !tx.isConfirmed()) {
continue;
}
if (!tx.isMature()) {
continue;
}
for (const vout of tx.vout) {
if (this.isSpent(vout.outpoint)) {
continue;
Expand Down

0 comments on commit 55a74a9

Please sign in to comment.