Skip to content

Commit

Permalink
Merge branch 'master' into X-Content-Length
Browse files Browse the repository at this point in the history
  • Loading branch information
Duddino authored Dec 6, 2024
2 parents f3b172f + d029156 commit d46f2ec
Show file tree
Hide file tree
Showing 8 changed files with 64 additions and 24 deletions.
6 changes: 4 additions & 2 deletions chain_params.prod.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@
"proposalFeeConfirmRequirement": 6,
"maxPaymentCycles": 6,
"maxPayment": 43200000000000,
"defaultColdStakingAddress": "SdgQDpS8jDRJDX8yK8m9KnTMarsE84zdsy"
"defaultColdStakingAddress": "SdgQDpS8jDRJDX8yK8m9KnTMarsE84zdsy",
"stakeSplitTarget": 50000000000
},
"testnet": {
"name": "testnet",
Expand Down Expand Up @@ -64,6 +65,7 @@
"proposalFeeConfirmRequirement": 3,
"maxPaymentCycles": 20,
"maxPayment": 144000000000,
"defaultColdStakingAddress": "WmNziUEPyhnUkiVdfsiNX93H6rSJnios44"
"defaultColdStakingAddress": "WmNziUEPyhnUkiVdfsiNX93H6rSJnios44",
"stakeSplitTarget": 50000000000
}
}
6 changes: 4 additions & 2 deletions chain_params.test.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@
"proposalFeeConfirmRequirement": 6,
"maxPaymentCycles": 6,
"maxPayment": 43200000000000,
"defaultColdStakingAddress": "SdgQDpS8jDRJDX8yK8m9KnTMarsE84zdsy"
"defaultColdStakingAddress": "SdgQDpS8jDRJDX8yK8m9KnTMarsE84zdsy",
"stakeSplitTarget": 50000000000
},
"testnet": {
"name": "testnet",
Expand Down Expand Up @@ -60,6 +61,7 @@
"proposalFeeConfirmRequirement": 3,
"maxPaymentCycles": 20,
"maxPayment": 144000000000,
"defaultColdStakingAddress": "WmNziUEPyhnUkiVdfsiNX93H6rSJnios44"
"defaultColdStakingAddress": "WmNziUEPyhnUkiVdfsiNX93H6rSJnios44",
"stakeSplitTarget": 50000000000
}
}
3 changes: 2 additions & 1 deletion debug_config.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,6 @@
"governance": true,
"i18n": true,
"ledger": true,
"masternode": true
"masternode": true,
"vanity_gen": true
}
9 changes: 7 additions & 2 deletions scripts/dashboard/VanityGen.vue
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { ref, computed, watch, nextTick } from 'vue';
import { cChainParams } from '../chain_params.js';
import { MAP_B58 } from '../misc.js';
import { useAlerts } from '../composables/use_alerts.js';
import { debugLog, DebugTopics } from '../debug.js';
const { createAlert } = useAlerts();
const addressPrefix = ref('');
Expand Down Expand Up @@ -72,7 +73,10 @@ function generate() {
Math.floor(window.navigator.hardwareConcurrency * 0.75),
1
);
console.log('Spawning ' + nThreads + ' vanity search threads!');
debugLog(
DebugTopics.VANITY_GEN,
'Spawning ' + nThreads + ' vanity search threads!'
);
for (let i = 0; i < nThreads; i++) {
const worker = new Worker(
new URL('../vanitygen_worker.js', import.meta.url)
Expand All @@ -83,7 +87,8 @@ function generate() {
if (data.pub.substr(1, prefix.length).toLowerCase() === prefix) {
try {
emit('import-wallet', data.priv);
console.log(
debugLog(
DebugTopics.VANITY_GEN,
`VANITY: Found an address after ${attempts.value} attempts!`
);
} finally {
Expand Down
1 change: 1 addition & 0 deletions scripts/debug.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ export const DebugTopics = {
I18N: new DebugTopic('[I18N]', 1 << 7),
LEDGER: new DebugTopic('[LEDGER]', 1 << 8),
MASTERNODE: new DebugTopic('[MASTERNODE]', 1 << 9),
VANITY_GEN: new DebugTopic('[VANITY_GEN]', 1 << 10),
};

let enabledDebug = 0;
Expand Down
28 changes: 22 additions & 6 deletions scripts/wallet.js
Original file line number Diff line number Diff line change
Expand Up @@ -1164,11 +1164,26 @@ export class Wallet {
// Add primary output
if (isDelegation) {
if (!returnAddress) returnAddress = this.getNewChangeAddress();
transactionBuilder.addColdStakeOutput({
address: returnAddress,
addressColdStake: address,
value,
});
// The per-output target for maximum staking efficiency
const nTarget = cChainParams.current.stakeSplitTarget;
// Generate optimal staking outputs
if (value < COIN) {
throw new Error('below consensus');
} else if (value < nTarget) {
transactionBuilder.addColdStakeOutput({
address: returnAddress,
addressColdStake: address,
value,
});
} else {
for (let i = 0; i < Math.floor(value / nTarget); i++) {
transactionBuilder.addColdStakeOutput({
address: returnAddress,
addressColdStake: address,
value: i === 0 ? nTarget + (value % nTarget) : nTarget,
});
}
}
} else if (isProposal) {
transactionBuilder.addProposalOutput({
hash: address,
Expand Down Expand Up @@ -1198,7 +1213,8 @@ export class Wallet {
}

const fee = transactionBuilder.getFee();
const changeValue = transactionBuilder.valueIn - value - fee;
const changeValue =
transactionBuilder.valueIn - transactionBuilder.valueOut - fee;
if (changeValue < 0) {
if (!subtractFeeFromAmt) {
throw new Error('Not enough balance');
Expand Down
3 changes: 3 additions & 0 deletions test_setup.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import { vi } from 'vitest';
import 'fake-indexeddb/auto';
vi.spyOn(console, 'warn').mockImplementation((message) => {
if (message?.includes && message?.includes('[vue]')) return;
});

// We need to attach the component to a HTML,
// or .isVisible() function does not work
Expand Down
32 changes: 21 additions & 11 deletions tests/unit/wallet/transactions.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {

import 'fake-indexeddb/auto';
import { TransactionBuilder } from '../../../scripts/transaction_builder.js';
import { cChainParams } from '../../../scripts/chain_params.js';

vi.stubGlobal('localStorage', { length: 0 });
vi.mock('../../../scripts/global.js');
Expand Down Expand Up @@ -169,9 +170,11 @@ describe('Wallet transaction tests', () => {
});

it('Creates a cold stake tx correctly', async () => {
// Delegate 5250 PIV to test Stake Pre-Splitting
const value = 5250 * 10 ** 8;
const tx = wallet.createTransaction(
'SR3L4TFUKKGNsnv2Q4hWTuET2a4vHpm1b9',
0.05 * 10 ** 8,
value,
{ isDelegation: true }
);
expect(tx.version).toBe(1);
Expand All @@ -184,26 +187,32 @@ describe('Wallet transaction tests', () => {
scriptSig: '76a914f49b25384b79685227be5418f779b98a6be4c73888ac', // Script sig must be the UTXO script since it's not signed
})
);
expect(tx.vout[1]).toStrictEqual(
new CTxOut({
script: '76a914f49b25384b79685227be5418f779b98a6be4c73888ac',
value: 4997470,
})
);
// The 'after split' output
expect(tx.vout[0]).toStrictEqual(
new CTxOut({
script: '76a97b63d114291a25b5b4d1802e0611e9bf724a1e57d9210e826714f49b25384b79685227be5418f779b98a6be4c7386888ac',
value: 5000000,
value:
cChainParams.current.stakeSplitTarget +
(value % cChainParams.current.stakeSplitTarget),
})
);
// The split outputs (depending on chainparam 'stakeSplitTarget')
for (const cOut of tx.vout.slice(1, tx.vout.length - 1)) {
expect(cOut).toStrictEqual(
new CTxOut({
script: '76a97b63d114291a25b5b4d1802e0611e9bf724a1e57d9210e826714f49b25384b79685227be5418f779b98a6be4c7386888ac',
value: cChainParams.current.stakeSplitTarget,
})
);
}
await checkFees(wallet, tx, MIN_FEE_PER_BYTE);
});

it('creates a tx with max balance', async () => {
it('Creates a tx with max balance', async () => {
const tx = wallet.createTransaction(
'SR3L4TFUKKGNsnv2Q4hWTuET2a4vHpm1b9',
legacyMainnetInitialBalance(),
{ isDelegation: true }
{ isDelegation: false }
);
expect(tx.version).toBe(1);
expect(tx.vin).toHaveLength(2);
Expand All @@ -218,9 +227,10 @@ describe('Wallet transaction tests', () => {
);
expect(tx.vout).toHaveLength(1);
const fees = await checkFees(wallet, tx, MIN_FEE_PER_BYTE);
// The 'after split' output
expect(tx.vout[0]).toStrictEqual(
new CTxOut({
script: '76a97b63d114291a25b5b4d1802e0611e9bf724a1e57d9210e826714f49b25384b79685227be5418f779b98a6be4c7386888ac',
script: '76a914291a25b5b4d1802e0611e9bf724a1e57d9210e8288ac',
value: legacyMainnetInitialBalance() - fees,
})
);
Expand Down

0 comments on commit d46f2ec

Please sign in to comment.