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

Implement Stake Pre-Splitting #444

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
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 @@ -24,7 +24,8 @@
"proposalFeeConfirmRequirement": 3,
"maxPaymentCycles": 20,
"maxPayment": 144000000000,
"defaultColdStakingAddress": "WmNziUEPyhnUkiVdfsiNX93H6rSJnios44"
"defaultColdStakingAddress": "WmNziUEPyhnUkiVdfsiNX93H6rSJnios44",
"stakeSplitTarget": 50000000000
},
"testnet": {
"name": "testnet",
Expand Down Expand Up @@ -54,6 +55,7 @@
"proposalFeeConfirmRequirement": 3,
"maxPaymentCycles": 20,
"maxPayment": 144000000000,
"defaultColdStakingAddress": "WmNziUEPyhnUkiVdfsiNX93H6rSJnios44"
"defaultColdStakingAddress": "WmNziUEPyhnUkiVdfsiNX93H6rSJnios44",
"stakeSplitTarget": 50000000000
}
}
22 changes: 16 additions & 6 deletions scripts/wallet.js
Original file line number Diff line number Diff line change
Expand Up @@ -1100,11 +1100,20 @@ export class Wallet {
// Add primary output
if (isDelegation) {
if (!returnAddress) [returnAddress] = this.getNewAddress(1);
transactionBuilder.addColdStakeOutput({
address: returnAddress,
addressColdStake: address,
value,
});
// The per-output target for maximum staking efficiency
const nTarget = cChainParams.current.stakeSplitTarget;
// Generate optimal staking outputs
for (let i = 0; i <= Math.floor(value / nTarget); i++) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see the problem here, but I would prefer that the whole value is staked... what about something like this?

Suggested change
for (let i = 0; i <= Math.floor(value / nTarget); i++) {
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,
});
}
}

const nOutAmount = i === 0 ? value % nTarget : nTarget;
// Skip 'dust' outputs (TODO: this should be `1 * COIN` as it is the consensus minimum, but tests are below this and will break)
// ... it's still better than no check, or `Stake 5 PIV` would create a 0-value remainder and fail.
if (nOutAmount < 0.05 * COIN) continue;
transactionBuilder.addColdStakeOutput({
address: returnAddress,
addressColdStake: address,
value: nOutAmount,
});
}
} else if (isProposal) {
transactionBuilder.addProposalOutput({
hash: address,
Expand Down Expand Up @@ -1134,7 +1143,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
1 change: 1 addition & 0 deletions tests/unit/wallet/transactions.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,7 @@ describe('Wallet transaction tests', () => {
});

it('Creates a cold stake tx correctly', async () => {
// TODO: rewrite this test over >1 PIV to match PIVX Consensus, and to also test Stake Splitting
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

update the test in this PR

const tx = wallet.createTransaction(
'SR3L4TFUKKGNsnv2Q4hWTuET2a4vHpm1b9',
0.05 * 10 ** 8,
Expand Down
Loading