-
Notifications
You must be signed in to change notification settings - Fork 457
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge feature/7136-support-legacy-blocks-and-transactions to developm…
- Loading branch information
Showing
31 changed files
with
2,114 additions
and
63 deletions.
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
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
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,94 @@ | ||
/* | ||
* Copyright © 2022 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, Schema } from '@liskhq/lisk-codec'; | ||
import { utils } from '@liskhq/lisk-cryptography'; | ||
import { blockHeaderSchemaV2, blockSchemaV2, legacyChainBracketInfoSchema } from './schemas'; | ||
import { | ||
LegacyBlock, | ||
LegacyBlockJSON, | ||
LegacyChainBracketInfo, | ||
RawLegacyBlock, | ||
LegacyBlockWithID, | ||
LegacyBlockHeaderWithID, | ||
} from './types'; | ||
|
||
interface LegacyBlockSchema { | ||
header: Schema; | ||
block: Schema; | ||
} | ||
|
||
export const blockSchemaMap: Record<number, LegacyBlockSchema> = { | ||
2: { | ||
block: blockSchemaV2, | ||
header: blockHeaderSchemaV2, | ||
}, | ||
}; | ||
|
||
// Implement read version logic when adding more versions | ||
const readVersion = (): number => 2; | ||
|
||
export const decodeBlock = ( | ||
data: Buffer, | ||
): { block: LegacyBlockWithID; schema: LegacyBlockSchema } => { | ||
const version = readVersion(); | ||
const blockSchema = blockSchemaMap[version]; | ||
if (!blockSchema) { | ||
throw new Error(`Legacy block version ${version} is not registered.`); | ||
} | ||
const rawBlock = codec.decode<RawLegacyBlock>(blockSchema.block, data); | ||
const id = utils.hash(rawBlock.header); | ||
return { | ||
block: { | ||
...rawBlock, | ||
header: { | ||
...codec.decode<LegacyBlockHeaderWithID>(blockSchema.header, rawBlock.header), | ||
id, | ||
}, | ||
}, | ||
schema: blockSchema, | ||
}; | ||
}; | ||
|
||
export const decodeBlockJSON = ( | ||
data: Buffer, | ||
): { block: LegacyBlockJSON; schema: LegacyBlockSchema } => { | ||
const { block, schema } = decodeBlock(data); | ||
return { | ||
block: { | ||
header: { | ||
...codec.toJSON(schema.header, block.header), | ||
id: block.header.id.toString('hex'), | ||
}, | ||
payload: block.payload.map(tx => tx.toString('hex')), | ||
}, | ||
schema, | ||
}; | ||
}; | ||
|
||
export const encodeBlock = (data: LegacyBlock): Buffer => { | ||
const blockSchema = blockSchemaMap[data.header.version]; | ||
if (!blockSchema) { | ||
throw new Error(`Legacy block version ${data.header.version} is not registered.`); | ||
} | ||
const headerBytes = codec.encode(blockSchema.header, data.header); | ||
|
||
return codec.encode(blockSchema.block, { | ||
header: headerBytes, | ||
payload: data.payload, | ||
}); | ||
}; | ||
|
||
export const encodeLegacyChainBracketInfo = (data: LegacyChainBracketInfo): Buffer => | ||
codec.encode(legacyChainBracketInfoSchema, data); |
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,17 @@ | ||
/* | ||
* Copyright © 2022 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. | ||
*/ | ||
|
||
export const DB_KEY_BLOCK_ID = Buffer.from([0]); | ||
export const DB_KEY_BLOCK_HEIGHT = Buffer.from([1]); | ||
export const DB_KEY_LEGACY_BRACKET = Buffer.from([2]); |
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,51 @@ | ||
/* | ||
* Copyright © 2022 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 { Database } from '@liskhq/lisk-db'; | ||
import { isHexString } from '@liskhq/lisk-validator'; | ||
import { RequestContext } from '../rpc/rpc_server'; | ||
import { LegacyBlockJSON } from './types'; | ||
import { Storage } from './storage'; | ||
import { decodeBlockJSON } from './codec'; | ||
|
||
interface EndpointArgs { | ||
db: Database; | ||
} | ||
|
||
export class LegacyEndpoint { | ||
[key: string]: unknown; | ||
public readonly storage: Storage; | ||
|
||
public constructor(args: EndpointArgs) { | ||
this.storage = new Storage(args.db); | ||
} | ||
|
||
public async getBlockByID(context: RequestContext): Promise<LegacyBlockJSON> { | ||
const { id } = context.params; | ||
if (!isHexString(id)) { | ||
throw new Error('Invalid parameters. `id` must be a valid hex string.'); | ||
} | ||
|
||
return decodeBlockJSON(await this.storage.getBlockByID(Buffer.from(id as string, 'hex'))).block; | ||
} | ||
|
||
public async getBlockByHeight(context: RequestContext): Promise<LegacyBlockJSON> { | ||
const { height } = context.params; | ||
if (typeof height !== 'number' || height < 0) { | ||
throw new Error('Invalid parameters. `height` must be zero or a positive number.'); | ||
} | ||
|
||
return decodeBlockJSON(await this.storage.getBlockByHeight(height)).block; | ||
} | ||
} |
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,20 @@ | ||
/* | ||
* Copyright © 2022 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. | ||
*/ | ||
|
||
export class PeerNotFoundWithLegacyInfo extends Error { | ||
public constructor(message: string) { | ||
super(message); | ||
this.name = this.constructor.name; | ||
} | ||
} |
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,13 @@ | ||
/* | ||
* Copyright © 2022 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. | ||
*/ |
Oops, something went wrong.