Skip to content
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
24 changes: 11 additions & 13 deletions packages/js-drive/lib/abci/handlers/proposal/beginBlockFactory.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ function beginBlockFactory(
proposalBlockExecutionContext.setLastCommitInfo(lastCommitInfo);

// Update SML
const isSimplifiedMasternodeListUpdated = await updateSimplifiedMasternodeList(
await updateSimplifiedMasternodeList(
coreChainLockedHeight,
{
logger: contextLogger,
Expand Down Expand Up @@ -142,8 +142,7 @@ function beginBlockFactory(
validatorSetQuorumHash: quorumHash,
coreChainLockedHeight,
lastSyncedCoreHeight,
// TODO: Since we don't have HPMNs now and every masternode can be a validator,
// we pass the whole list
// TODO: We should pass only HPMNs
totalHpmns: simplifiedMasternodeList.getStore()
.getCurrentSML()
.getValidMasternodesList()
Expand Down Expand Up @@ -188,19 +187,18 @@ function beginBlockFactory(
}

// Synchronize masternode identities
const blockInfo = BlockInfo.createFromBlockExecutionContext(proposalBlockExecutionContext);

if (isSimplifiedMasternodeListUpdated) {
const blockInfo = BlockInfo.createFromBlockExecutionContext(proposalBlockExecutionContext);

const synchronizeMasternodeIdentitiesResult = await synchronizeMasternodeIdentities(
coreChainLockedHeight,
blockInfo,
);
const synchronizeMasternodeIdentitiesResult = await synchronizeMasternodeIdentities(
coreChainLockedHeight,
blockInfo,
);

const {
createdEntities, updatedEntities, removedEntities, fromHeight, toHeight,
} = synchronizeMasternodeIdentitiesResult;
const {
createdEntities, updatedEntities, removedEntities, fromHeight, toHeight,
} = synchronizeMasternodeIdentitiesResult;

if (fromHeight !== toHeight) {
contextLogger.info(
`Masternode identities are synced for heights from ${fromHeight} to ${toHeight}: ${createdEntities.length} created, ${updatedEntities.length} updated, ${removedEntities.length} removed`,
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,6 @@ function synchronizeMasternodeIdentitiesFactory(
lastSyncedCoreHeightRepository,
fetchSimplifiedMNList,
) {
let lastSyncedCoreHeight = 0;

/**
* @typedef synchronizeMasternodeIdentities
* @param {number} coreHeight
Expand All @@ -73,18 +71,23 @@ function synchronizeMasternodeIdentitiesFactory(
* }>}
*/
async function synchronizeMasternodeIdentities(coreHeight, blockInfo) {
// TODO: Must be moved outside of this function
const lastSyncedHeightResult = await lastSyncedCoreHeightRepository.fetch({
useTransaction: true,
});

const lastSyncedCoreHeight = lastSyncedHeightResult.getValue() || 0;

let result = {
createdEntities: [],
updatedEntities: [],
removedEntities: [],
fromHeight: lastSyncedCoreHeight,
toHeight: coreHeight,
};

if (!lastSyncedCoreHeight) {
const lastSyncedHeightResult = await lastSyncedCoreHeightRepository.fetch({
useTransaction: true,
});

lastSyncedCoreHeight = lastSyncedHeightResult.getValue() || 0;
if (lastSyncedCoreHeight === coreHeight) {
return result;
}

let newMasternodes;
Expand Down Expand Up @@ -242,9 +245,7 @@ function synchronizeMasternodeIdentitiesFactory(
result = mergeEntities(result, affectedEntities);
}

result.fromHeight = lastSyncedCoreHeight;
result.toHeight = coreHeight;

// TODO: Must be moved outside of this function
await lastSyncedCoreHeightRepository.store(coreHeight, {
useTransaction: true,
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -520,6 +520,8 @@ describe.skip('synchronizeMasternodeIdentitiesFactory', function main() {
}
});

it('should do nothing if last synced height is equal to the current height');

it('should create identities for all masternodes on the first sync', async () => {
const result = await synchronizeMasternodeIdentities(coreHeight, blockInfo);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,10 @@ describe('beginBlockFactory', () => {
coreChainLockedHeight, { logger: loggerMock },
);

expect(synchronizeMasternodeIdentitiesMock).to.not.been.called();
expect(synchronizeMasternodeIdentitiesMock).to.be.calledWithExactly(
coreChainLockedHeight,
blockInfo,
);

expect(executionTimerMock.clearTimer).to.be.calledTwice();
expect(executionTimerMock.clearTimer.getCall(1)).to.be.calledWithExactly('roundExecution');
Expand Down