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

fix(dashmate)!: payment queue and extend enabled count #1505

Merged
merged 6 commits into from
Oct 23, 2023
Merged
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
5 changes: 3 additions & 2 deletions packages/dashmate/src/commands/status/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,16 +75,17 @@ class StatusCommand extends ConfigBaseCommand {

if (masternode.state === MasternodeStateEnum.READY) {
const {
enabledCount,
poSePenalty,
lastPaidHeight,
lastPaidTime,
paymentQueuePosition,
nextPaymentTime,
} = masternode.nodeState;
const { evonodeEnabled, masternodeEnabled } = masternode;

plain['Masternode ProTX'] = masternode.proTxHash || 'n/a';
plain['PoSe Penalty'] = colors.poSePenalty(poSePenalty, enabledCount)(`${poSePenalty}`) || 'n/a';
plain['PoSe Penalty'] = colors.poSePenalty(poSePenalty,
masternodeEnabled, evonodeEnabled)(`${poSePenalty}`) || 'n/a';
plain['Last paid block'] = lastPaidHeight || 'n/a';
plain['Last paid time'] = lastPaidHeight === 0 ? 'Never' : (lastPaidTime || 'n/a');
plain['Payment queue position'] = paymentQueuePosition || 'n/a';
Expand Down
15 changes: 10 additions & 5 deletions packages/dashmate/src/commands/status/masternode.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,20 +56,25 @@ class MasternodeStatusCommand extends ConfigBaseCommand {
plain['Masternode Sync Status'] = scope.syncAsset ? chalk.yellow(scope.syncAsset) : 'n/a';
}

plain['Total Masternodes'] = scope.masternodeTotal ?? 'n/a';
plain['Enabled Masternodes'] = scope.masternodeEnabled ?? 'n/a';
plain['Total Evonodes'] = scope.evonodeTotal ?? 'n/a';
plain['Enabled Evonodes'] = scope.evonodeEnabled ?? 'n/a';

if (scope.state === MasternodeStateEnum.READY) {
const {
lastPaidHeight, lastPaidTime,
paymentQueuePosition, nextPaymentTime,
poSePenalty, enabledCount,
poSePenalty,
} = scope.nodeState;

plain['ProTx Hash'] = scope.proTxHash || 'n/a';
plain['PoSe Penalty'] = colors.poSePenalty(poSePenalty, enabledCount)(`${poSePenalty}`) || 'n/a';
plain['Last paid block'] = lastPaidHeight || 'n/a';
plain['PoSe Penalty'] = colors.poSePenalty(poSePenalty,
scope.masternodeEnabled, scope.evonodeEnabled)(`${poSePenalty}`) || 'n/a';
plain['Last paid block'] = lastPaidHeight ?? 'n/a';
plain['Last paid time'] = lastPaidHeight === 0 ? 'Never' : (lastPaidTime || 'n/a');
plain['Payment queue position'] = paymentQueuePosition || 'n/a';
plain['Payment queue position'] = paymentQueuePosition ?? 'n/a';
plain['Next payment time'] = `in ${nextPaymentTime}` || 'n/a';
plain['Enabled count'] = enabledCount || 'n/a';
}

return printObject(plain, flags.format);
Expand Down
18 changes: 14 additions & 4 deletions packages/dashmate/src/core/calculatePaymentQueuePosition.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,29 @@
function calculatePaymentQueuePosition(dmnState, masternodeEnabledCount, coreBlocks) {
/**
*
* @param dmnState {dmnState}
* @param enabledMasternodes {number}
* @param enabledEvonodes {number}
* @param coreBlocks {number} current block height
* @return {number}
*/
function calculatePaymentQueuePosition(dmnState, enabledMasternodes, enabledEvonodes, coreBlocks) {
const enabledCount = enabledMasternodes + enabledEvonodes * 4;

let paymentQueuePosition;
// Masternode has been unbanned recently
if (dmnState.PoSeRevivedHeight > dmnState.lastPaidHeight) {
paymentQueuePosition = dmnState.PoSeRevivedHeight
+ masternodeEnabledCount
+ enabledCount
- coreBlocks;
// Masternode has never been paid
} else if (dmnState.lastPaidHeight === 0) {
paymentQueuePosition = dmnState.registeredHeight
+ masternodeEnabledCount
+ enabledCount
- coreBlocks;
// Masternode was previously paid and is in normal queue
} else {
paymentQueuePosition = dmnState.lastPaidHeight
+ masternodeEnabledCount
+ enabledCount
- coreBlocks;
}
return paymentQueuePosition;
Expand Down
4 changes: 2 additions & 2 deletions packages/dashmate/src/status/colors.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,11 @@ module.exports = {
}
return chalk.red;
},
poSePenalty: (poSePenalty, enabledCount) => {
poSePenalty: (poSePenalty, masternodeEnabled, evonodeEnabled) => {
if (poSePenalty === 0) {
return chalk.green;
}
if (poSePenalty < enabledCount) {
if (poSePenalty < masternodeEnabled + evonodeEnabled * 4) {
return chalk.yellow;
}
return chalk.red;
Expand Down
25 changes: 21 additions & 4 deletions packages/dashmate/src/status/scopes/masternode.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,17 @@ function getMasternodeScopeFactory(dockerCompose, createRpcClient, getConnection
proTxHash: null,
state: null,
status: null,
masternodeTotal: null,
masternodeEnabled: null,
evonodeTotal: null,
evonodeEnabled: null,
nodeState: {
dmnState: null,
poSePenalty: null,
lastPaidHeight: null,
lastPaidTime: null,
paymentQueuePosition: null,
nextPaymentTime: null,
enabledCount: null,
},
};

Expand All @@ -55,7 +58,13 @@ function getMasternodeScopeFactory(dockerCompose, createRpcClient, getConnection
const { blocks: coreBlocks } = blockchainInfo.result;

const countInfo = masternodeCount.result;
const { enabled } = countInfo;
const { detailed } = countInfo;
const { regular, evo } = detailed;

info.masternodeTotal = regular.total;
info.masternodeEnabled = regular.enabled;
info.evonodeTotal = evo.total;
info.evonodeEnabled = evo.enabled;

const { state, status, proTxHash } = masternodeStatus.result;

Expand All @@ -68,15 +77,15 @@ function getMasternodeScopeFactory(dockerCompose, createRpcClient, getConnection

const { PoSePenalty: poSePenalty, lastPaidHeight } = dmnState;

const paymentQueuePosition = calculatePaymentQueuePosition(dmnState, enabled, coreBlocks);
const paymentQueuePosition = calculatePaymentQueuePosition(dmnState,
info.masternodeEnabled, info.evonodeEnabled, coreBlocks);
const lastPaidTime = lastPaidHeight ? blocksToTime(coreBlocks - lastPaidHeight) : null;
const nextPaymentTime = `${blocksToTime(paymentQueuePosition)}`;

info.nodeState.dmnState = dmnState;
info.nodeState.poSePenalty = poSePenalty;
info.nodeState.lastPaidHeight = lastPaidHeight;
info.nodeState.lastPaidTime = lastPaidTime;
info.nodeState.enabledCount = enabled;
info.nodeState.paymentQueuePosition = paymentQueuePosition;
info.nodeState.nextPaymentTime = nextPaymentTime;
}
Expand All @@ -97,6 +106,10 @@ function getMasternodeScopeFactory(dockerCompose, createRpcClient, getConnection
proTxHash: null,
state: MasternodeStateEnum.UNKNOWN,
status: null,
masternodeTotal: null,
masternodeEnabled: null,
evonodeTotal: null,
evonodeEnabled: null,
nodeState: {
dmnState: null,
poSePenalty: null,
Expand All @@ -121,6 +134,10 @@ function getMasternodeScopeFactory(dockerCompose, createRpcClient, getConnection
const masternodeInfo = await getMasternodeInfo(config);

scope.proTxHash = masternodeInfo.proTxHash;
scope.masternodeTotal = masternodeInfo.masternodeTotal;
scope.masternodeEnabled = masternodeInfo.masternodeEnabled;
scope.evonodeEnabled = masternodeInfo.evonodeEnabled;
scope.evonodeTotal = masternodeInfo.evonodeTotal;
scope.state = masternodeInfo.state;
scope.status = masternodeInfo.status;
scope.nodeState = masternodeInfo.nodeState;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,13 @@ function writeServiceConfigsFactory(homeDir) {
* @return {void}
*/
function writeServiceConfigs(configName, configFiles) {
// Drop all files from configs directory
const configDir = homeDir.joinPath(configName);

for (const filePath of Object.keys(configFiles)) {
const absoluteFilePath = path.join(configDir, filePath);
const absoluteFileDir = path.dirname(absoluteFilePath);

// Recreate it
// Ensure dir
fs.mkdirSync(absoluteFileDir, { recursive: true });

// Write specified config files
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
const calculatePaymentQueuePosition = require('../../../src/core/calculatePaymentQueuePosition');

describe('calculatePaymentQueuePosition', () => {
it('should just work', async () => {
const mockDmnState = { lastPaidHeight: 1, PoSeRevivedHeight: 0, registeredHeight: 1 };

const position = calculatePaymentQueuePosition(mockDmnState, 0, 3, 10);

expect(position).to.equal(3);
});
});
4 changes: 2 additions & 2 deletions packages/dashmate/test/unit/status/colors.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,11 +87,11 @@ describe('colors.js', () => {
});

it('should color yellow', async () => {
expect(colors.poSePenalty(2, 10)).to.be.equal(chalk.yellow);
expect(colors.poSePenalty(2, 10, 10)).to.be.equal(chalk.yellow);
});

it('should color red', async () => {
expect(colors.poSePenalty(20, 2)).to.be.equal(chalk.red);
expect(colors.poSePenalty(20, 2, 2)).to.be.equal(chalk.red);
});
});
});
11 changes: 9 additions & 2 deletions packages/dashmate/test/unit/status/scopes/masternode.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,11 @@ describe('getMasternodeScopeFactory', () => {
};

mockRpcClient.getBlockchainInfo.returns({ result: { blocks: 1337 } });
mockRpcClient.masternode.withArgs('count').returns({ result: { enabled: 666 } });
mockRpcClient.masternode.withArgs('count').returns({
result: {
detailed: { regular: { total: 1337, enabled: 777 }, evo: { total: 1337, enabled: 777 } },
},
});
mockRpcClient.masternode.withArgs('status').returns({
result: {
dmnState: mockDmnState,
Expand All @@ -64,10 +68,13 @@ describe('getMasternodeScopeFactory', () => {
proTxHash: mockProTxHash,
state: MasternodeStateEnum.READY,
status: 'Ready',
masternodeTotal: 1337,
masternodeEnabled: 777,
evonodeTotal: 1337,
evonodeEnabled: 777,
nodeState: {
dmnState: mockDmnState,
poSePenalty: mockDmnState.PoSePenalty,
enabledCount: 666,
lastPaidHeight: mockDmnState.lastPaidHeight,
// ignore these 3
lastPaidTime: scope.nodeState.lastPaidTime,
Expand Down
Loading