Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
blockchain: remove abstract-level usage in favor of DB interface and …
…MapDB
- Loading branch information
1 parent
510b0c6
commit 89f4741
Showing
10 changed files
with
84 additions
and
46 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,42 @@ | ||
import { DB, BatchDBOp } from '@ethereumjs/util' | ||
import { bytesToHex } from 'ethereum-cryptography/utils' | ||
|
||
export class MapDB<TKey extends Uint8Array | string, TValue extends Uint8Array | string> | ||
implements DB<TKey, TValue> | ||
{ | ||
_database: Map<TKey, TValue> | ||
|
||
constructor(database?: Map<TKey, TValue>) { | ||
this._database = database ?? new Map<TKey, TValue>() | ||
} | ||
|
||
async get(key: TKey): Promise<TValue | null> { | ||
const result = this._database.get(key) | ||
|
||
return result ?? null | ||
} | ||
|
||
async put(key: TKey, val: TValue): Promise<void> { | ||
this._database.set(key, val) | ||
} | ||
|
||
async del(key: TKey): Promise<void> { | ||
this._database.delete(key) | ||
} | ||
|
||
async batch(opStack: BatchDBOp<TKey, TValue>[]): Promise<void> { | ||
for (const op of opStack) { | ||
if (op.type === 'del') { | ||
await this.del(op.key) | ||
} | ||
|
||
if (op.type === 'put') { | ||
await this.put(op.key, op.value) | ||
} | ||
} | ||
} | ||
|
||
copy(): DB<TKey, TValue> { | ||
return new MapDB<TKey, TValue>(this._database) | ||
} | ||
} |
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