@liskhq/lisk-chain
Advanced tools
Comparing version 0.3.0 to 0.3.1
@@ -52,3 +52,2 @@ /// <reference types="node" /> | ||
private readonly _blockRewardArgs; | ||
private readonly _genesisBlock; | ||
private readonly _accountSchema; | ||
@@ -61,3 +60,2 @@ private readonly _blockAssetSchema; | ||
get numberOfValidators(): number; | ||
get genesisBlock(): GenesisBlock; | ||
get accountSchema(): Schema; | ||
@@ -67,3 +65,3 @@ get blockAssetSchema(): { | ||
}; | ||
init(): Promise<void>; | ||
init(genesisBlock: GenesisBlock): Promise<void>; | ||
calculateDefaultReward(height: number): bigint; | ||
@@ -94,3 +92,4 @@ calculateExpectedReward(blockHeader: BlockHeader, stateStore: StateStore): bigint; | ||
private _getLastBootstrapHeight; | ||
private _getGenesisInfo; | ||
} | ||
export {}; |
@@ -44,3 +44,2 @@ "use strict"; | ||
this._networkIdentifier = networkIdentifier; | ||
this._genesisBlock = genesisBlock; | ||
this.slots = new slots_1.Slots({ | ||
@@ -72,5 +71,2 @@ genesisBlockTimestamp: genesisBlock.header.timestamp, | ||
} | ||
get genesisBlock() { | ||
return this._genesisBlock; | ||
} | ||
get accountSchema() { | ||
@@ -82,3 +78,3 @@ return this._accountSchema; | ||
} | ||
async init() { | ||
async init(genesisBlock) { | ||
let storageLastBlock; | ||
@@ -91,5 +87,12 @@ try { | ||
} | ||
if (storageLastBlock.header.height !== this.genesisBlock.header.height) { | ||
if (storageLastBlock.header.height !== genesisBlock.header.height) { | ||
await this._cacheBlockHeaders(storageLastBlock); | ||
} | ||
const genesisInfo = await this._getGenesisInfo(); | ||
if (!genesisInfo) { | ||
await this.dataAccess.setConsensusState(constants_1.CONSENSUS_STATE_GENESIS_INFO, lisk_codec_1.codec.encode(schema_1.genesisInfoSchema, { | ||
height: genesisBlock.header.height, | ||
initRounds: genesisBlock.header.asset.initRounds, | ||
})); | ||
} | ||
const validators = await this.getValidators(); | ||
@@ -111,7 +114,8 @@ this._numberOfValidators = validators.length; | ||
async newStateStore(skipLastHeights = 0) { | ||
var _a, _b; | ||
const fromHeight = Math.max(0, this._lastBlock.header.height - this.numberOfValidators * 3 - skipLastHeights); | ||
var _a, _b, _c; | ||
const genesisInfo = await this._getGenesisInfo(); | ||
const fromHeight = Math.max((_a = genesisInfo === null || genesisInfo === void 0 ? void 0 : genesisInfo.height) !== null && _a !== void 0 ? _a : 0, this._lastBlock.header.height - this.numberOfValidators * 3 - skipLastHeights); | ||
const toHeight = Math.max(this._lastBlock.header.height - skipLastHeights, 1); | ||
const lastBlockHeaders = await this.dataAccess.getBlockHeadersByHeightBetween(fromHeight, toHeight); | ||
const lastBlockReward = this.calculateDefaultReward((_b = (_a = lastBlockHeaders[0]) === null || _a === void 0 ? void 0 : _a.height) !== null && _b !== void 0 ? _b : 1); | ||
const lastBlockReward = this.calculateDefaultReward((_c = (_b = lastBlockHeaders[0]) === null || _b === void 0 ? void 0 : _b.height) !== null && _c !== void 0 ? _c : 1); | ||
return new state_store_1.StateStore(this.dataAccess, { | ||
@@ -125,11 +129,3 @@ networkIdentifier: this._networkIdentifier, | ||
async genesisBlockExist(genesisBlock) { | ||
let matchingGenesisBlock; | ||
try { | ||
matchingGenesisBlock = await this.dataAccess.getBlockHeaderByID(genesisBlock.header.id); | ||
} | ||
catch (error) { | ||
if (!(error instanceof lisk_db_1.NotFoundError)) { | ||
throw error; | ||
} | ||
} | ||
const matchingGenesisBlock = await this.dataAccess.blockHeaderExists(genesisBlock.header.id); | ||
let lastBlockHeader; | ||
@@ -168,2 +164,6 @@ try { | ||
await stateStore.consensus.set(constants_1.CONSENSUS_STATE_VALIDATORS_KEY, lisk_codec_1.codec.encode(schema_1.validatorsSchema, { validators: initialValidators })); | ||
await stateStore.consensus.set(constants_1.CONSENSUS_STATE_GENESIS_INFO, lisk_codec_1.codec.encode(schema_1.genesisInfoSchema, { | ||
height: block.header.height, | ||
initRounds: block.header.asset.initRounds, | ||
})); | ||
this._numberOfValidators = block.header.asset.initDelegates.length; | ||
@@ -215,3 +215,3 @@ } | ||
async removeBlock(block, stateStore, { saveTempBlock } = { saveTempBlock: false }) { | ||
if (block.header.version === this.genesisBlock.header.version) { | ||
if (block.header.version === constants_1.GENESIS_BLOCK_VERSION) { | ||
throw new Error('Cannot delete genesis block'); | ||
@@ -248,4 +248,5 @@ } | ||
async setValidators(validators, stateStore, blockHeader) { | ||
if (this._getLastBootstrapHeight() > blockHeader.height) { | ||
debug(`Skipping updating validator since current height ${blockHeader.height} is lower than last bootstrap height ${this._getLastBootstrapHeight()}`); | ||
const lastBootstrapHeight = await this._getLastBootstrapHeight(); | ||
if (lastBootstrapHeight > blockHeader.height) { | ||
debug(`Skipping updating validator since current height ${blockHeader.height} is lower than last bootstrap height ${lastBootstrapHeight}`); | ||
return; | ||
@@ -281,8 +282,18 @@ } | ||
} | ||
_getLastBootstrapHeight() { | ||
return (this._numberOfValidators * this._genesisBlock.header.asset.initRounds + | ||
this._genesisBlock.header.height); | ||
async _getLastBootstrapHeight() { | ||
const genesisInfo = await this._getGenesisInfo(); | ||
if (!genesisInfo) { | ||
throw new Error('genesis info not stored'); | ||
} | ||
return this._numberOfValidators * genesisInfo.initRounds + genesisInfo.height; | ||
} | ||
async _getGenesisInfo() { | ||
const genesisInfoBytes = await this.dataAccess.getConsensusState(constants_1.CONSENSUS_STATE_GENESIS_INFO); | ||
if (!genesisInfoBytes) { | ||
return undefined; | ||
} | ||
return lisk_codec_1.codec.decode(schema_1.genesisInfoSchema, genesisInfoBytes); | ||
} | ||
} | ||
exports.Chain = Chain; | ||
//# sourceMappingURL=chain.js.map |
@@ -5,2 +5,3 @@ /// <reference types="node" /> | ||
export declare const CONSENSUS_STATE_VALIDATORS_KEY = "validators"; | ||
export declare const CONSENSUS_STATE_GENESIS_INFO = "genesisInfo"; | ||
export declare const DEFAULT_MIN_BLOCK_HEADER_CACHE = 309; | ||
@@ -7,0 +8,0 @@ export declare const DEFAULT_MAX_BLOCK_HEADER_CACHE = 515; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.GENESIS_BLOCK_TRANSACTION_ROOT = exports.GENESIS_BLOCK_SIGNATURE = exports.GENESIS_BLOCK_REWARD = exports.GENESIS_BLOCK_GENERATOR_PUBLIC_KEY = exports.GENESIS_BLOCK_VERSION = exports.EMPTY_HASH = exports.EMPTY_BUFFER = exports.EVENT_VALIDATORS_CHANGED = exports.EVENT_DELETE_BLOCK = exports.EVENT_NEW_BLOCK = exports.DEFAULT_MAX_BLOCK_HEADER_CACHE = exports.DEFAULT_MIN_BLOCK_HEADER_CACHE = exports.CONSENSUS_STATE_VALIDATORS_KEY = exports.CONSENSUS_STATE_FINALIZED_HEIGHT_KEY = exports.CHAIN_STATE_BURNT_FEE = void 0; | ||
exports.GENESIS_BLOCK_TRANSACTION_ROOT = exports.GENESIS_BLOCK_SIGNATURE = exports.GENESIS_BLOCK_REWARD = exports.GENESIS_BLOCK_GENERATOR_PUBLIC_KEY = exports.GENESIS_BLOCK_VERSION = exports.EMPTY_HASH = exports.EMPTY_BUFFER = exports.EVENT_VALIDATORS_CHANGED = exports.EVENT_DELETE_BLOCK = exports.EVENT_NEW_BLOCK = exports.DEFAULT_MAX_BLOCK_HEADER_CACHE = exports.DEFAULT_MIN_BLOCK_HEADER_CACHE = exports.CONSENSUS_STATE_GENESIS_INFO = exports.CONSENSUS_STATE_VALIDATORS_KEY = exports.CONSENSUS_STATE_FINALIZED_HEIGHT_KEY = exports.CHAIN_STATE_BURNT_FEE = void 0; | ||
const lisk_cryptography_1 = require("@liskhq/lisk-cryptography"); | ||
@@ -8,2 +8,3 @@ exports.CHAIN_STATE_BURNT_FEE = 'burntFee'; | ||
exports.CONSENSUS_STATE_VALIDATORS_KEY = 'validators'; | ||
exports.CONSENSUS_STATE_GENESIS_INFO = 'genesisInfo'; | ||
exports.DEFAULT_MIN_BLOCK_HEADER_CACHE = 309; | ||
@@ -10,0 +11,0 @@ exports.DEFAULT_MAX_BLOCK_HEADER_CACHE = 515; |
@@ -27,2 +27,4 @@ /// <reference types="node" /> | ||
getBlockHeaderByID(id: Buffer): Promise<BlockHeader>; | ||
getRawBlockHeaderByID(id: Buffer): Promise<BlockHeader>; | ||
blockHeaderExists(id: Buffer): Promise<boolean>; | ||
getBlockHeadersByIDs(arrayOfBlockIds: ReadonlyArray<Buffer>): Promise<BlockHeader[]>; | ||
@@ -33,3 +35,3 @@ getBlockHeaderByHeight(height: number): Promise<BlockHeader>; | ||
getLastBlockHeader(): Promise<BlockHeader>; | ||
getHighestCommonBlockHeader(arrayOfBlockIds: ReadonlyArray<Buffer>): Promise<BlockHeader | undefined>; | ||
getHighestCommonBlockID(arrayOfBlockIds: ReadonlyArray<Buffer>): Promise<Buffer | undefined>; | ||
getBlockByID<T>(id: Buffer): Promise<Block<T>>; | ||
@@ -46,2 +48,3 @@ getBlocksByIDs(arrayOfBlockIds: ReadonlyArray<Buffer>): Promise<Block[]>; | ||
getConsensusState(key: string): Promise<Buffer | undefined>; | ||
setConsensusState(key: string, val: Buffer): Promise<void>; | ||
getAccountsByPublicKey(arrayOfPublicKeys: ReadonlyArray<Buffer>): Promise<Account[]>; | ||
@@ -65,3 +68,4 @@ getAccountByAddress<T>(address: Buffer): Promise<Account<T>>; | ||
private _decodeRawBlock; | ||
private _getRawBlockHeaderByID; | ||
} | ||
export {}; |
@@ -50,2 +50,18 @@ "use strict"; | ||
} | ||
async getRawBlockHeaderByID(id) { | ||
return this._getRawBlockHeaderByID(id); | ||
} | ||
async blockHeaderExists(id) { | ||
const cachedBlock = this._blocksCache.getByID(id); | ||
if (cachedBlock) { | ||
return true; | ||
} | ||
try { | ||
await this._storage.getBlockHeaderByID(id); | ||
return true; | ||
} | ||
catch (error) { | ||
return false; | ||
} | ||
} | ||
async getBlockHeadersByIDs(arrayOfBlockIds) { | ||
@@ -91,3 +107,4 @@ const cachedBlocks = this._blocksCache.getByIDs(arrayOfBlockIds); | ||
} | ||
async getHighestCommonBlockHeader(arrayOfBlockIds) { | ||
async getHighestCommonBlockID(arrayOfBlockIds) { | ||
var _a; | ||
const headers = this._blocksCache.getByIDs(arrayOfBlockIds); | ||
@@ -97,3 +114,3 @@ headers.sort((a, b) => b.height - a.height); | ||
if (cachedBlockHeader) { | ||
return cachedBlockHeader; | ||
return cachedBlockHeader.id; | ||
} | ||
@@ -103,3 +120,3 @@ const storageBlockHeaders = []; | ||
try { | ||
const blockHeader = await this.getBlockHeaderByID(id); | ||
const blockHeader = await this._getRawBlockHeaderByID(id); | ||
storageBlockHeaders.push(blockHeader); | ||
@@ -114,3 +131,3 @@ } | ||
storageBlockHeaders.sort((a, b) => b.height - a.height); | ||
return storageBlockHeaders[0]; | ||
return (_a = storageBlockHeaders[0]) === null || _a === void 0 ? void 0 : _a.id; | ||
} | ||
@@ -158,2 +175,5 @@ async getBlockByID(id) { | ||
} | ||
async setConsensusState(key, val) { | ||
return this._storage.setConsensusState(key, val); | ||
} | ||
async getAccountsByPublicKey(arrayOfPublicKeys) { | ||
@@ -268,4 +288,15 @@ const accounts = await this._storage.getAccountsByPublicKey(arrayOfPublicKeys); | ||
} | ||
async _getRawBlockHeaderByID(id) { | ||
const cachedBlock = this._blocksCache.getByID(id); | ||
if (cachedBlock) { | ||
return cachedBlock; | ||
} | ||
const blockHeaderBuffer = await this._storage.getBlockHeaderByID(id); | ||
return { | ||
...lisk_codec_1.codec.decode(schema_1.blockHeaderSchema, blockHeaderBuffer), | ||
id, | ||
}; | ||
} | ||
} | ||
exports.DataAccess = DataAccess; | ||
//# sourceMappingURL=data_access.js.map |
@@ -25,2 +25,3 @@ /// <reference types="node" /> | ||
getConsensusState(key: string): Promise<Buffer | undefined>; | ||
setConsensusState(key: string, val: Buffer): Promise<void>; | ||
getAccountByAddress(address: Buffer): Promise<Buffer>; | ||
@@ -27,0 +28,0 @@ getAccountsByPublicKey(arrayOfPublicKeys: ReadonlyArray<Buffer>): Promise<Buffer[]>; |
@@ -228,2 +228,5 @@ "use strict"; | ||
} | ||
async setConsensusState(key, val) { | ||
await this._db.put(`${constants_1.DB_KEY_CONSENSUS_STATE}:${key}`, val); | ||
} | ||
async getAccountByAddress(address) { | ||
@@ -230,0 +233,0 @@ const account = await this._db.get(`${constants_1.DB_KEY_ACCOUNTS_ADDRESS}:${utils_1.keyString(address)}`); |
@@ -235,2 +235,17 @@ import { Schema } from '@liskhq/lisk-codec'; | ||
}; | ||
export declare const genesisInfoSchema: { | ||
$id: string; | ||
type: string; | ||
properties: { | ||
height: { | ||
dataType: string; | ||
fieldNumber: number; | ||
}; | ||
initRounds: { | ||
dataType: string; | ||
fieldNumber: number; | ||
}; | ||
}; | ||
required: string[]; | ||
}; | ||
export declare const getGenesisBlockHeaderAssetSchema: (accountSchema: Schema) => Schema; | ||
@@ -237,0 +252,0 @@ export declare const getRegisteredBlockAssetSchema: (accountSchema: Schema) => { |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.getRegisteredBlockAssetSchema = exports.getGenesisBlockHeaderAssetSchema = exports.validatorsSchema = exports.stateDiffSchema = exports.blockHeaderAssetSchema = exports.baseAccountSchema = exports.baseGenesisBlockHeaderAssetSchema = exports.blockHeaderSchema = exports.signingBlockHeaderSchema = exports.blockSchema = void 0; | ||
exports.getRegisteredBlockAssetSchema = exports.getGenesisBlockHeaderAssetSchema = exports.genesisInfoSchema = exports.validatorsSchema = exports.stateDiffSchema = exports.blockHeaderAssetSchema = exports.baseAccountSchema = exports.baseGenesisBlockHeaderAssetSchema = exports.blockHeaderSchema = exports.signingBlockHeaderSchema = exports.blockSchema = void 0; | ||
const lisk_utils_1 = require("@liskhq/lisk-utils"); | ||
@@ -185,2 +185,17 @@ exports.blockSchema = { | ||
}; | ||
exports.genesisInfoSchema = { | ||
$id: '/state/genesisInfo', | ||
type: 'object', | ||
properties: { | ||
height: { | ||
dataType: 'uint32', | ||
fieldNumber: 1, | ||
}, | ||
initRounds: { | ||
dataType: 'uint32', | ||
fieldNumber: 2, | ||
}, | ||
}, | ||
required: ['height', 'initRounds'], | ||
}; | ||
const getGenesisBlockHeaderAssetSchema = (accountSchema) => lisk_utils_1.objects.mergeDeep({}, exports.baseGenesisBlockHeaderAssetSchema, { | ||
@@ -187,0 +202,0 @@ properties: { |
@@ -88,1 +88,5 @@ /// <reference types="node" /> | ||
} | ||
export interface GenesisInfo { | ||
height: number; | ||
initRounds: number; | ||
} |
{ | ||
"name": "@liskhq/lisk-chain", | ||
"version": "0.3.0", | ||
"version": "0.3.1", | ||
"description": "Blocks and state management implementation that are used for block processing according to the Lisk protocol", | ||
@@ -5,0 +5,0 @@ "author": "Lisk Foundation <admin@lisk.io>, lightcurve GmbH <admin@lightcurve.io>", |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
211276
3262