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

Add consistent deserialization and serialization - Closes #4131 #4391

Merged
merged 11 commits into from Oct 16, 2019
28 changes: 28 additions & 0 deletions framework/src/modules/chain/bft/bft.js
Expand Up @@ -93,6 +93,34 @@ class BFT extends EventEmitter {
});
}

/**
* Serialize common properties to the JSON format
* @param {*} blockInstance Instance of the block
* @returns JSON format of the block
*/
// eslint-disable-next-line class-methods-use-this
serialize(blockInstance) {
michielmulders marked this conversation as resolved.
Show resolved Hide resolved
return {
...blockInstance,
maxHeightPreviouslyForged: blockInstance.maxHeightPreviouslyForged || 0,
prevotedConfirmedUptoHeight:
blockInstance.prevotedConfirmedUptoHeight || 0,
};
}

/**
* Deserialize common properties to instance format
* @param {*} blockJSON JSON format of the block
*/
// eslint-disable-next-line class-methods-use-this
deserialize(blockJSON) {
shuse2 marked this conversation as resolved.
Show resolved Hide resolved
return {
...blockJSON,
maxHeightPreviouslyForged: blockJSON.maxHeightPreviouslyForged || 0,
prevotedConfirmedUptoHeight: blockJSON.prevotedConfirmedUptoHeight || 0,
};
}

/**
* When blocks deleted send those to BFT to update BFT state
*
Expand Down
6 changes: 6 additions & 0 deletions framework/src/modules/chain/block_processor_v0.js
Expand Up @@ -116,6 +116,12 @@ class BlockProcessorV0 extends BaseBlockProcessor {
this.constants = constants;
this.exceptions = exceptions;

this.deserialize.pipe([
({ block }) => this.blocksModule.deserialize(block),
]);

this.serialize.pipe([({ block }) => this.blocksModule.serialize(block)]);

this.validate.pipe([
data => this._validateVersion(data),
data => validateSchema(data),
Expand Down
10 changes: 10 additions & 0 deletions framework/src/modules/chain/block_processor_v2.js
Expand Up @@ -184,6 +184,16 @@ class BlockProcessorV2 extends BaseBlockProcessor {

this.init.pipe([() => this.bftModule.init()]);

this.deserialize.pipe([
({ block }) => this.blocksModule.deserialize(block),
(_, updatedBlock) => this.bftModule.deserialize(updatedBlock),
michielmulders marked this conversation as resolved.
Show resolved Hide resolved
]);

this.serialize.pipe([
({ block }) => this.blocksModule.serialize(block),
(_, updatedBlock) => this.bftModule.serialize(updatedBlock),
]);

this.validate.pipe([
data => this._validateVersion(data),
data => validateSchema(data),
Expand Down
148 changes: 0 additions & 148 deletions framework/src/modules/chain/blocks/block.js

This file was deleted.

63 changes: 0 additions & 63 deletions framework/src/modules/chain/blocks/block_v1.js

This file was deleted.

65 changes: 0 additions & 65 deletions framework/src/modules/chain/blocks/block_v2.js

This file was deleted.