This repository has been archived by the owner on Jun 11, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 458
Update block schema - Closes #6739 #6765
Merged
shuse2
merged 6 commits into
feature/6554-improve-framework-architecture
from
6739-update_block_schema
Sep 15, 2021
Merged
Changes from 4 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
ccebc05
:seedling: Update block header in lisk-chain
shuse2 89589fc
:seedling: Update block header in lisk-framework
shuse2 aaa0036
:recycle: Fix wrong error message
shuse2 9a62fd9
Merge branch 'feature/6554-improve-framework-architecture' into 6739-…
shuse2 de30f6d
:recycle: Fix typos
shuse2 7d494cb
:recycle: Remove invalid test description
shuse2 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
@@ -13,49 +13,61 @@ | |||||||||
*/ | ||||||||||
|
||||||||||
import { codec } from '@liskhq/lisk-codec'; | ||||||||||
import { BlockAssets } from './block_assets'; | ||||||||||
import { BlockHeader } from './block_header'; | ||||||||||
import { blockSchema } from './schema'; | ||||||||||
import { Transaction } from './transaction'; | ||||||||||
|
||||||||||
interface BlockAttrs { | ||||||||||
header: Buffer; | ||||||||||
payload: Buffer[]; | ||||||||||
assets: Buffer[]; | ||||||||||
} | ||||||||||
|
||||||||||
export class Block { | ||||||||||
// eslint-disable-next-line no-useless-constructor | ||||||||||
public constructor(public readonly header: BlockHeader, public readonly payload: Transaction[]) { | ||||||||||
public constructor( | ||||||||||
public readonly header: BlockHeader, | ||||||||||
public readonly payload: Transaction[], | ||||||||||
public readonly assets: BlockAssets, | ||||||||||
) { | ||||||||||
// No body necessary | ||||||||||
} | ||||||||||
|
||||||||||
public static fromBytes(value: Buffer): Block { | ||||||||||
const { header, payload } = codec.decode<BlockAttrs>(blockSchema, value); | ||||||||||
const { header, payload, assets } = codec.decode<BlockAttrs>(blockSchema, value); | ||||||||||
|
||||||||||
return new Block( | ||||||||||
BlockHeader.fromBytes(header), | ||||||||||
payload.map(v => Transaction.fromBytes(v)), | ||||||||||
BlockAssets.fromBytes(assets), | ||||||||||
); | ||||||||||
} | ||||||||||
|
||||||||||
public static fromJSON(value: Record<string, unknown>): Block { | ||||||||||
const { header, payload } = value; | ||||||||||
const { header, payload, assets } = value; | ||||||||||
if (typeof header !== 'object') { | ||||||||||
throw new Error('Invalid block format. header must be an object.'); | ||||||||||
} | ||||||||||
if (!Array.isArray(payload)) { | ||||||||||
throw new Error('Invalid block format. payload must be an array.'); | ||||||||||
} | ||||||||||
if (!Array.isArray(assets)) { | ||||||||||
throw new Error('Invalid block format. assets must be an array.'); | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this is following the style of other error message? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I saw further below the property name was in double quotes but I'm ok with either way. |
||||||||||
} | ||||||||||
|
||||||||||
return new Block( | ||||||||||
BlockHeader.fromJSON(value.header as Record<string, unknown>), | ||||||||||
payload.map(v => Transaction.fromBytes(v)), | ||||||||||
BlockAssets.fromJSON(assets), | ||||||||||
); | ||||||||||
} | ||||||||||
|
||||||||||
public getBytes(): Buffer { | ||||||||||
return codec.encode(blockSchema, { | ||||||||||
header: this.header.getBytes(), | ||||||||||
payload: this.payload.map(p => p.getBytes()), | ||||||||||
assets: this.assets.getBytes(), | ||||||||||
}); | ||||||||||
} | ||||||||||
|
||||||||||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
/* | ||
* Copyright © 2021 Lisk Foundation | ||
* | ||
* See the LICENSE file at the top-level directory of this distribution | ||
* for licensing information. | ||
* | ||
* Unless otherwise agreed in a custom licensing agreement with the Lisk Foundation, | ||
* no part of this software, including this file, may be copied, modified, | ||
* propagated, or distributed except according to the terms contained in the | ||
* LICENSE file. | ||
* | ||
* Removal or modification of this copyright notice is prohibited. | ||
*/ | ||
|
||
import { codec } from '@liskhq/lisk-codec'; | ||
import { blockAssetSchema } from './schema'; | ||
|
||
export interface BlockAsset { | ||
moduleID: number; | ||
data: Buffer; | ||
} | ||
|
||
export class BlockAssets { | ||
private readonly _assets: BlockAsset[] = []; | ||
|
||
public constructor(assets: BlockAsset[] = []) { | ||
this._assets = assets; | ||
} | ||
|
||
public static fromBytes(values: ReadonlyArray<Buffer>): BlockAssets { | ||
const assets = values.map(val => codec.decode<BlockAsset>(blockAssetSchema, val)); | ||
const blockAssets = new BlockAssets(assets); | ||
return blockAssets; | ||
} | ||
|
||
public static fromJSON(values: Record<string, unknown>[]): BlockAssets { | ||
const assets = values.map(val => codec.fromJSON<BlockAsset>(blockAssetSchema, val)); | ||
return new BlockAssets(assets); | ||
} | ||
|
||
public getBytes(): Buffer[] { | ||
return this._assets.map(asset => codec.encode(blockAssetSchema, asset)); | ||
} | ||
|
||
public getAsset(moduleID: number): Buffer | undefined { | ||
return this._assets.find(a => a.moduleID === moduleID)?.data; | ||
} | ||
|
||
public setAsset(moduleID: number, value: Buffer): void { | ||
const asset = this.getAsset(moduleID); | ||
if (asset) { | ||
throw new Error(`Module asset for "${moduleID}" is already set.`); | ||
} | ||
|
||
this._assets.push({ moduleID, data: value }); | ||
} | ||
|
||
public sort(): void { | ||
this._assets.sort((a1, a2) => a1.moduleID - a2.moduleID); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A bit unsure here if it can be redefined to be something like
BlockAsset[]
. In the LIP it saysassets
is an array of objects and also since payload is an array of objects too.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
other properties are also class instance, and we could create
BlockAsset
class, but we want to haveblockAssets.get
andblockAssets.set
. Therefore it's better to beBlockAssets
class