-
Notifications
You must be signed in to change notification settings - Fork 1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Example client block cache #26
Merged
Merged
Changes from 5 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
642ae9d
Add client cache + stub out account processor
dgca 8c19b4c
Merge branch 'main' into client-block-cache
dgca 5d287cd
Move cache to BlockProcessor
dgca 86cd7f2
Encode keys as zero-padded for lexicographic sorting
dgca de3cbce
Bring back the cache
dgca 48a7c3e
Convert leveldb block buffer data to LightBlock
dgca 6b9a953
Store notes in account manager
dgca 3a7d166
Merge main, fix conflicts
dgca 81d641f
Remove testdb
dgca f83c8f7
Fix note struct
dgca 66af536
Use @ironfish/rust-nodejs over sdk
dgca 561e98c
Move SDK dependency to merkle tree
dgca File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
import { generateKeyFromPrivateKey, Key } from "@ironfish/rust-nodejs"; | ||
import { NoteEncrypted } from "@ironfish/sdk/build/src/primitives/noteEncrypted"; | ||
import { LightBlock } from "../../../src/models/lightstreamer"; | ||
|
||
interface AccountData { | ||
key: Key; | ||
assetBalances: Map<string, bigint>; | ||
} | ||
|
||
/** | ||
* Mapping of private key to account data including keys and asset balances | ||
*/ | ||
type TAccounts = Map< | ||
string, | ||
{ | ||
key: Key; | ||
assetBalances: Map<string, bigint>; | ||
} | ||
>; | ||
|
||
export class AccountsManager { | ||
private accounts: TAccounts = new Map(); | ||
|
||
public addAccount(privateKey: string) { | ||
this.accounts.set(...this._makeAccountData(privateKey)); | ||
} | ||
|
||
public getPublicAddresses() { | ||
return Array.from(this.accounts.keys()); | ||
} | ||
|
||
private _makeAccountData(privateKey: string): [string, AccountData] { | ||
const key = generateKeyFromPrivateKey(privateKey); | ||
return [ | ||
key.publicAddress, | ||
{ | ||
key, | ||
assetBalances: new Map(), | ||
}, | ||
]; | ||
} | ||
|
||
public processBlockForTransactions(block: LightBlock) { | ||
block.transactions.forEach((tx) => { | ||
tx.outputs.forEach((output) => { | ||
this._processNote(new NoteEncrypted(output.note)); | ||
}); | ||
|
||
// @todo: Process spends | ||
}); | ||
} | ||
|
||
private _processNote(note: NoteEncrypted) { | ||
for (const publicKey of this.accounts.keys()) { | ||
const result = note.decryptNoteForOwner(publicKey); | ||
if (!result) return; | ||
|
||
const account = this.accounts.get(publicKey); | ||
if (!account) return; | ||
|
||
const assetId = result.assetId().toString("hex"); | ||
const amount = result.value(); | ||
|
||
const currentBalance = account.assetBalances.get(assetId) ?? BigInt(0); | ||
account.assetBalances.set(assetId, currentBalance + amount); | ||
} | ||
} | ||
} |
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,54 @@ | ||
import levelup, { LevelUp } from "levelup"; | ||
import leveldown from "leveldown"; | ||
import path from "path"; | ||
import { LightBlock } from "../../../src/models/lightstreamer"; | ||
|
||
const KNOWN_KEYS = { | ||
HEAD_SEQUENCE: "__HEAD_SEQUENCE__", | ||
}; | ||
|
||
// Storing keys as zero-padded numbers to avoid lexicographic ordering. | ||
// At one minute block times, this gives us ~1,900 years of blocks. | ||
const KEY_LENGTH = 9; | ||
|
||
export class BlockCache { | ||
private db: LevelUp; | ||
|
||
constructor() { | ||
this.db = levelup( | ||
leveldown(path.join(__dirname, "..", "client-block-cache")), | ||
); | ||
} | ||
|
||
public async getHeadSequence() { | ||
try { | ||
const headSequence = await this.db.get(KNOWN_KEYS.HEAD_SEQUENCE); | ||
const asNumber = Number(headSequence); | ||
if (isNaN(asNumber)) { | ||
throw new Error("Head sequence is not a number"); | ||
} | ||
return asNumber; | ||
} catch (_err) { | ||
return 0; | ||
} | ||
} | ||
|
||
public cacheBlock(block: LightBlock) { | ||
const sequence = block.sequence; | ||
console.log(`Caching block ${sequence}`); | ||
|
||
this.db | ||
.batch() | ||
.put(this.encodeKey(sequence), block) | ||
.put(KNOWN_KEYS.HEAD_SEQUENCE, sequence) | ||
.write(); | ||
} | ||
|
||
public encodeKey(num: number) { | ||
return num.toString().padStart(KEY_LENGTH, "0"); | ||
} | ||
|
||
public decodeKey(key: string) { | ||
return Number(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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Instead of just storing balances here, can we store notes? This will make spending possible, since we need to fund the spends with owned notes. Otherwise we would need to rescan the blockchain for owned notes. Balance could then be calculated at runtime by summing the value of the owned notes.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
FYI probably simplest to store serialized note buffer
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Addressed here: https://github.com/iron-fish/wallet-server/pull/26/files#diff-100d016004f580d38d2fcdcc9afbc0c4fe0693099a7e78ce238a0c66ba6696d0R12