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 #4749 from LiskHQ/4136-update_dpos_using_state_store
Browse files Browse the repository at this point in the history
Update lisk-dpos to use state store consistently- Closes #4136
  • Loading branch information
ManuGowda authored Feb 3, 2020
2 parents 68c0682 + f167a6f commit 5a5337b
Show file tree
Hide file tree
Showing 56 changed files with 3,819 additions and 4,958 deletions.
70 changes: 46 additions & 24 deletions elements/lisk-blocks/src/blocks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,10 @@ export class Blocks extends EventEmitter {
this.dataAccess.resetBlockHeaderCache();
}

public newStateStore(): StateStore {
return new StateStore(this.storage);
}

private async _cacheBlockHeaders(
storageLastBlock: BlockInstance,
): Promise<void> {
Expand Down Expand Up @@ -318,6 +322,12 @@ export class Blocks extends EventEmitter {
block.id = blocksUtils.getId(blockBytes);
}

public async resetState(): Promise<void> {
await this.storage.entities.Account.resetMemTables();
await this.storage.entities.ChainState.delete();
this.dataAccess.resetBlockHeaderCache();
}

public verifyInMemory(block: BlockInstance, lastBlock: BlockInstance): void {
verifyPreviousBlockId(block, lastBlock, this.genesisBlock);
validateBlockSlot(block, lastBlock, this.slots);
Expand Down Expand Up @@ -351,26 +361,36 @@ export class Blocks extends EventEmitter {
stateStore: StateStore,
): Promise<void> {
await applyConfirmedStep(blockInstance, stateStore, this.exceptions);

this.dataAccess.addBlockHeader(blockInstance);
this._lastBlock = blockInstance;
}

// tslint:disable-next-line prefer-function-over-method
public async applyGenesis(
blockInstance: BlockInstance,
stateStore: StateStore,
): Promise<void> {
await applyConfirmedGenesisStep(blockInstance, stateStore);

this.dataAccess.addBlockHeader(blockInstance);
this._lastBlock = blockInstance;
}

public async save(
blockJSON: BlockJSON,
tx: StorageTransaction,
blockInstance: BlockInstance,
stateStore: StateStore,
{ saveOnlyState, removeFromTempTable } = {
saveOnlyState: false,
removeFromTempTable: false,
},
): Promise<void> {
await saveBlock(this.storage, blockJSON, tx);
return this.storage.entities.Block.begin('saveBlock', async tx => {
await stateStore.finalize(tx);
if (!saveOnlyState) {
const blockJSON = this.serialize(blockInstance);
await saveBlock(this.storage, blockJSON, tx);
}
if (removeFromTempTable) {
await this.removeBlockFromTempTable(blockInstance.id, tx);
}
this.dataAccess.addBlockHeader(blockInstance);
this._lastBlock = blockInstance;
});
}

public async undo(
Expand Down Expand Up @@ -402,23 +422,25 @@ export class Blocks extends EventEmitter {

public async remove(
block: BlockInstance,
blockJSON: BlockJSON,
tx: StorageTransaction,
stateStore: StateStore,
{ saveTempBlock } = { saveTempBlock: false },
): Promise<void> {
const secondLastBlock = await this._deleteLastBlock(block, tx);

if (saveTempBlock) {
const blockTempEntry = {
id: blockJSON.id,
height: blockJSON.height,
fullBlock: blockJSON,
};
await this.storage.entities.TempBlock.create(blockTempEntry, {}, tx);
}

this.dataAccess.removeBlockHeader(block.id);
this._lastBlock = secondLastBlock;
await this.storage.entities.Block.begin('revertBlock', async tx => {
const secondLastBlock = await this._deleteLastBlock(block, tx);

if (saveTempBlock) {
const blockJSON = this.serialize(block);
const blockTempEntry = {
id: blockJSON.id,
height: blockJSON.height,
fullBlock: blockJSON,
};
await this.storage.entities.TempBlock.create(blockTempEntry, {}, tx);
}
await stateStore.finalize(tx);
this.dataAccess.removeBlockHeader(block.id);
this._lastBlock = secondLastBlock;
});
}

public async removeBlockFromTempTable(
Expand Down
5 changes: 0 additions & 5 deletions elements/lisk-blocks/src/chain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,6 @@ export const applyConfirmedStep = async (
if (unappliableTransactionsResponse.length > 0) {
throw unappliableTransactionsResponse[0].errors;
}

await stateStore.finalize();
};

export const applyConfirmedGenesisStep = async (
Expand All @@ -90,7 +88,6 @@ export const applyConfirmedGenesisStep = async (
sortedTransactionInstances,
stateStore,
);
await stateStore.finalize();

return blockInstance;
};
Expand Down Expand Up @@ -121,6 +118,4 @@ export const undoConfirmedStep = async (
if (unappliedTransactionResponse) {
throw unappliedTransactionResponse.errors;
}

await stateStore.finalize();
};
21 changes: 9 additions & 12 deletions elements/lisk-blocks/src/data_access/data_access.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import {
BlockJSON,
BlockRound,
Storage as DBStorage,
StorageTransaction,
TempBlock,
} from '../types';

Expand Down Expand Up @@ -274,35 +273,33 @@ export class DataAccess {
}
/** End: Blocks */

/** Begin: ChainState */
public async getChainState(key: string): Promise<string | undefined> {
return this._storage.getChainState(key);
}
/** End: ChainState */

/** Begin: Accounts */
public async getAccountsByPublicKey(
arrayOfPublicKeys: ReadonlyArray<string>,
tx?: StorageTransaction,
): Promise<Account[]> {
const accounts = await this._storage.getAccountsByPublicKey(
arrayOfPublicKeys,
tx,
);

return accounts;
}

public async getAccountsByAddress(
arrayOfAddresses: ReadonlyArray<string>,
tx?: StorageTransaction,
): Promise<Account[]> {
const accounts = await this._storage.getAccountsByAddress(
arrayOfAddresses,
tx,
);
const accounts = await this._storage.getAccountsByAddress(arrayOfAddresses);

return accounts;
}

public async getDelegateAccounts(
tx?: StorageTransaction,
): Promise<Account[]> {
const accounts = await this._storage.getDelegateAccounts(tx);
public async getDelegateAccounts(limit: number): Promise<Account[]> {
const accounts = await this._storage.getDelegateAccounts(limit);

return accounts;
}
Expand Down
23 changes: 11 additions & 12 deletions elements/lisk-blocks/src/data_access/storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import {
BlockJSON,
BlockRound,
Storage as DBStorage,
StorageTransaction,
TempBlock,
} from '../types';

Expand Down Expand Up @@ -213,42 +212,43 @@ export class Storage {
}

/*
Accounts
ChainState
*/
public async getChainState(key: string): Promise<string | undefined> {
const value = await this._storage.entities.ChainState.getKey(key);

return value;
}

/*
Accounts
*/
public async getAccountsByPublicKey(
arrayOfPublicKeys: ReadonlyArray<string>,
tx?: StorageTransaction,
): Promise<Account[]> {
const accounts = await this._storage.entities.Account.get(
{ publicKey_in: arrayOfPublicKeys },
{ limit: arrayOfPublicKeys.length },
tx,
);

return accounts;
}

public async getAccountsByAddress(
arrayOfAddresses: ReadonlyArray<string>,
tx?: StorageTransaction,
): Promise<Account[]> {
const accounts = await this._storage.entities.Account.get(
{ address_in: arrayOfAddresses },
{ limit: arrayOfAddresses.length },
tx,
);

return accounts;
}

public async getDelegateAccounts(
tx?: StorageTransaction,
): Promise<Account[]> {
public async getDelegateAccounts(limit: number): Promise<Account[]> {
const accounts = await this._storage.entities.Account.get(
{ isDelegate: true },
{ limit: 101, sort: ['voteWeight:desc', 'publicKey:asc'] },
tx,
{ limit, sort: ['voteWeight:desc', 'publicKey:asc'] },
);

return accounts;
Expand All @@ -261,7 +261,6 @@ export class Storage {
/*
Transactions
*/

public async getTransactionsByIDs(
arrayOfTransactionIds: ReadonlyArray<string>,
): Promise<TransactionJSON[]> {
Expand Down
19 changes: 8 additions & 11 deletions elements/lisk-blocks/src/state_store/account_store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,25 +54,20 @@ export class AccountStore {
private _originalUpdatedKeys: { [key: number]: string[] } = {};
private readonly _primaryKey = 'address';
private readonly _name = 'Account';
private readonly _tx: StorageTransaction | undefined;

public constructor(
accountEntity: StorageEntity<Account>,
{ tx }: { readonly tx?: StorageTransaction } = { tx: undefined },
) {
public constructor(accountEntity: StorageEntity<Account>) {
this._account = accountEntity;
this._data = [];
this._updatedKeys = {};
this._primaryKey = 'address';
this._name = 'Account';
this._originalData = [];
this._originalUpdatedKeys = {};
this._tx = tx;
}

public async cache(filter: StorageFilters): Promise<ReadonlyArray<Account>> {
// tslint:disable-next-line no-null-keyword
const result = await this._account.get(filter, { limit: null }, this._tx);
const result = await this._account.get(filter, { limit: null });
this._data = uniqBy([...this._data, ...result], this._primaryKey);

return cloneDeep(result) as ReadonlyArray<Account>;
Expand Down Expand Up @@ -106,7 +101,6 @@ export class AccountStore {
{ [this._primaryKey]: primaryValue },
// tslint:disable-next-line no-null-keyword
{ limit: null },
this._tx,
);

if (elementFromDB) {
Expand Down Expand Up @@ -136,7 +130,6 @@ export class AccountStore {
{ [this._primaryKey]: primaryValue },
// tslint:disable-next-line no-null-keyword
{ limit: null },
this._tx,
);

if (elementFromDB) {
Expand All @@ -156,6 +149,10 @@ export class AccountStore {
return cloneDeep(defaultElement);
}

public getUpdated(): ReadonlyArray<Account> {
return [...this._data];
}

public find(
fn: (value: Account, index: number, obj: Account[]) => unknown,
): Account | undefined {
Expand Down Expand Up @@ -191,7 +188,7 @@ export class AccountStore {
: updatedKeys;
}

public async finalize(): Promise<void> {
public async finalize(tx: StorageTransaction): Promise<void> {
const affectedAccounts = Object.entries(this._updatedKeys).map(
([index, updatedKeys]) => ({
updatedItem: this._data[parseInt(index, 10)],
Expand All @@ -205,7 +202,7 @@ export class AccountStore {
const updatedData = pick(updatedItem, updatedKeys);

// tslint:disable-next-line no-null-keyword
return this._account.upsert(filter, updatedData, null, this._tx);
return this._account.upsert(filter, updatedData, null, tx);
},
);

Expand Down
Loading

0 comments on commit 5a5337b

Please sign in to comment.