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 454
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #6899 from LiskHQ/6752-add-stateroot-block-gen
Add stateRoot when generating a block - Closes #6752
- Loading branch information
Showing
17 changed files
with
663 additions
and
121 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
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,80 @@ | ||
/* | ||
* 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 { BatchChain, NotFoundError as DBNotFoundError } from '@liskhq/lisk-db'; | ||
import { SparseMerkleTree } from '@liskhq/lisk-tree'; | ||
import { dataStructures } from '@liskhq/lisk-utils'; | ||
import { StateStore } from './state_store'; | ||
import { DB_KEY_STATE_SMT } from '../db_keys'; | ||
import { StateDiff } from '../types'; | ||
import { NotFoundError } from './errors'; | ||
import { DatabaseReader, DatabaseWriter } from './types'; | ||
|
||
export interface CurrentState { | ||
batch: BatchChain; | ||
diff: StateDiff; | ||
stateStore: StateStore; | ||
smt: SparseMerkleTree; | ||
smtStore: SMTStore; | ||
} | ||
|
||
export class SMTStore { | ||
private readonly _db: DatabaseReader; | ||
private readonly _data: dataStructures.BufferMap<Buffer>; | ||
|
||
public constructor(db: DatabaseReader, data?: dataStructures.BufferMap<Buffer>) { | ||
this._db = db; | ||
this._data = data ?? new dataStructures.BufferMap(); | ||
} | ||
|
||
public async get(key: Buffer): Promise<Buffer> { | ||
const prefixedKey = this._getKey(key); | ||
const cachedValue = this._data.get(prefixedKey); | ||
if (cachedValue) { | ||
return cachedValue; | ||
} | ||
try { | ||
const storedValue = await this._db.get(prefixedKey); | ||
this._data.set(prefixedKey, storedValue); | ||
return storedValue; | ||
} catch (error) { | ||
if (error instanceof DBNotFoundError) { | ||
throw new NotFoundError(key); | ||
} | ||
throw error; | ||
} | ||
} | ||
|
||
// eslint-disable-next-line @typescript-eslint/require-await | ||
public async set(key: Buffer, value: Buffer): Promise<void> { | ||
const prefixedKey = this._getKey(key); | ||
this._data.set(prefixedKey, value); | ||
} | ||
|
||
// eslint-disable-next-line @typescript-eslint/require-await | ||
public async del(key: Buffer): Promise<void> { | ||
const prefixedKey = this._getKey(key); | ||
this._data.delete(prefixedKey); | ||
} | ||
|
||
public finalize(batch: DatabaseWriter): void { | ||
for (const [key, value] of this._data.entries()) { | ||
batch.put(key, value); | ||
} | ||
} | ||
|
||
private _getKey(key: Buffer): Buffer { | ||
return Buffer.concat([DB_KEY_STATE_SMT, key]); | ||
} | ||
} |
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.