Skip to content
This repository has been archived by the owner on Jun 11, 2024. It is now read-only.

Commit

Permalink
Merge pull request #5457 from LiskHQ/5278-genesis-block-processor
Browse files Browse the repository at this point in the history
Create BlockProcessorV0 which handles the genesis block - Closes #5278
  • Loading branch information
shuse2 authored Jun 24, 2020
2 parents f9cdba3 + f34f9d8 commit 580080b
Show file tree
Hide file tree
Showing 38 changed files with 7,125 additions and 12,575 deletions.
2 changes: 1 addition & 1 deletion elements/lisk-bft/src/bft.ts
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ export class BFT extends EventEmitter {

const finalizedHeightStored =
storedFinalizedHeightBuffer === undefined
? 1
? 0
: codec.decode<BFTPersistedValues>(
BFTFinalizedHeightCodecSchema,
storedFinalizedHeightBuffer,
Expand Down
20 changes: 6 additions & 14 deletions elements/lisk-chain/src/chain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ import { DataAccess } from './data_access';
import { Slots } from './slots';
import { StateStore } from './state_store';
import {
applyGenesisTransactions,
applyTransactions,
checkAllowedTransactions,
undoTransactions,
Expand Down Expand Up @@ -222,7 +221,9 @@ export class Chain {
// Check mem tables
let genesisBlock: BlockHeader;
try {
genesisBlock = await this.dataAccess.getBlockHeaderByHeight(1);
genesisBlock = await this.dataAccess.getBlockHeaderByID(
this.genesisBlock.header.id,
);
} catch (error) {
throw new Error('Failed to load genesis block');
}
Expand Down Expand Up @@ -253,7 +254,7 @@ export class Chain {

public async newStateStore(skipLastHeights = 0): Promise<StateStore> {
const fromHeight = Math.max(
1,
0,
this._lastBlock.header.height -
this.constants.stateBlockSize -
skipLastHeights,
Expand Down Expand Up @@ -363,15 +364,6 @@ export class Chain {
await applyFeeAndRewards(block, stateStore);
}

// eslint-disable-next-line class-methods-use-this
public async applyGenesis(
block: Block,
stateStore: StateStore,
): Promise<void> {
await applyGenesisTransactions(block.payload, stateStore);
await applyFeeAndRewards(block, stateStore);
}

public async save(
block: Block,
stateStore: StateStore,
Expand Down Expand Up @@ -416,7 +408,7 @@ export class Chain {
stateStore: StateStore,
{ saveTempBlock } = { saveTempBlock: false },
): Promise<void> {
if (block.header.height === 1) {
if (block.header.version === this.genesisBlock.header.version) {
throw new Error('Cannot delete genesis block');
}
let secondLastBlock: Block;
Expand Down Expand Up @@ -533,7 +525,7 @@ export class Chain {
// Cache the block headers (size=DEFAULT_MAX_BLOCK_HEADER_CACHE)
const fromHeight = Math.max(
storageLastBlock.header.height - DEFAULT_MAX_BLOCK_HEADER_CACHE,
1,
0,
);
const toHeight = storageLastBlock.header.height;

Expand Down
1 change: 0 additions & 1 deletion elements/lisk-chain/src/transactions/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,4 @@ export {
applyTransactions,
checkAllowedTransactions,
undoTransactions,
applyGenesisTransactions,
} from './transactions_handlers';
23 changes: 1 addition & 22 deletions elements/lisk-chain/src/transactions/transactions_handlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,34 +20,13 @@ import {
} from '@liskhq/lisk-transactions';

import { StateStore } from '../state_store';
import {
Contexter,
MatcherTransaction,
WriteableTransactionResponse,
} from '../types';
import { Contexter, MatcherTransaction } from '../types';

export const validateTransactions = (
transactions: ReadonlyArray<BaseTransaction>,
): ReadonlyArray<TransactionResponse> =>
transactions.map(transaction => transaction.validate());

export const applyGenesisTransactions = async (
transactions: ReadonlyArray<BaseTransaction>,
stateStore: StateStore,
): Promise<TransactionResponse[]> => {
const transactionsResponses: TransactionResponse[] = [];
for (const transaction of transactions) {
const transactionResponse = await transaction.apply(stateStore);

// We are overriding the status of transaction because it's from genesis block
(transactionResponse as WriteableTransactionResponse).status =
TransactionStatus.OK;
transactionsResponses.push(transactionResponse);
}

return transactionsResponses;
};

export const applyTransactions = async (
transactions: ReadonlyArray<BaseTransaction>,
stateStore: StateStore,
Expand Down
7 changes: 2 additions & 5 deletions elements/lisk-chain/src/validate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,10 @@ export const validatePreviousBlockProperty = (
): void => {
const isGenesisBlock =
block.header.id.equals(genesisBlock.header.id) &&
block.header.previousBlockID.length === 0 &&
block.header.height === 1;
block.header.version === 0;
const propertyIsValid =
isGenesisBlock ||
(!block.header.id.equals(genesisBlock.header.id) &&
block.header.previousBlockID.length > 0 &&
block.header.height !== 1);
(block.header.previousBlockID.length > 0 && block.header.version !== 0);

if (!propertyIsValid) {
throw new Error('Invalid previous block');
Expand Down
Loading

0 comments on commit 580080b

Please sign in to comment.