-
Notifications
You must be signed in to change notification settings - Fork 166
feat: Local ledger implementation in typescript #2623
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
Draft
bleepbloopsify
wants to merge
1
commit into
main
Choose a base branch
from
leon/reimpl-local-ledger
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
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 hidden or 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 hidden or 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 hidden or 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,140 @@ | ||
| import { PublicKey } from '../../../provable/crypto/signature.js'; | ||
| import { Int64, UInt64 } from '../../../provable/int.js'; | ||
| import { Field } from '../../../provable/wrapped.js'; | ||
| import { AccountUpdate as AccountUpdateV2 } from '../../v2/account-update.js'; | ||
| import { Account as AccountV2 } from '../../v2/account.js'; | ||
| import { ZkappFeePayment } from '../../v2/transaction.js'; | ||
| import { ChainView } from '../../v2/views.js'; | ||
| import { | ||
| AccountUpdateApplyResult, | ||
| ApplyState, | ||
| checkAndApplyAccountUpdate, | ||
| checkAndApplyFeePayment, | ||
| } from '../../v2/zkapp-logic.js'; | ||
| import { TokenId, ZkappCommand } from '../account-update.js'; | ||
| import { Account as AccountV1, newAccount } from '../account.js'; | ||
|
|
||
| export const DefaultTokenId = 1n; | ||
|
|
||
| export type AccountId = { publicKey: PublicKey; tokenId?: Field }; | ||
|
|
||
| export class LocalLedger { | ||
| _nextLocation: bigint; | ||
| _accounts: Map<bigint, any>; | ||
| _locations: Map<string, bigint>; // keyed by "pubkey|tokenId" | ||
|
|
||
| constructor() { | ||
| this._nextLocation = 1n; | ||
| this._accounts = new Map<bigint, any>(); | ||
| this._locations = new Map<string, bigint>(); | ||
| } | ||
|
|
||
| static create(): LocalLedger { | ||
| return new LocalLedger(); | ||
| } | ||
|
|
||
| _key(publicKey: PublicKey, tokenId: Field) { | ||
| return `${publicKey.toBase58()}|${tokenId}`; | ||
| } | ||
|
|
||
| saveAccount(publicKey: PublicKey, account: AccountV1) { | ||
| const location = (() => { | ||
| const key = this._key(publicKey, TokenId.default); | ||
| const existing = this._locations.get(key); | ||
| if (existing === undefined) throw new Error('account with public key does not exist'); | ||
| return existing; | ||
| })(); | ||
| this._accounts.set(location, account); | ||
| } | ||
|
|
||
| addAccount(publicKey: PublicKey, balance: bigint | number | string): void { | ||
| const accountId = { publicKey, tokenId: TokenId.default }; | ||
| const location = (() => { | ||
| const key = this._key(publicKey, TokenId.default); | ||
| const existing = this._locations.get(key); | ||
| if (existing !== undefined) throw new Error('account with public key already exists'); | ||
| const newLocation = this._nextLocation; | ||
| this._nextLocation += 1n; | ||
| return newLocation; | ||
| })(); | ||
|
|
||
| const account = newAccount(accountId); | ||
| account.balance = UInt64.from(balance); | ||
| const key = this._key(publicKey, TokenId.default); | ||
| this._locations.set(key, location); | ||
| this._accounts.set(location, account); | ||
| } | ||
|
|
||
| getAccount(publicKey: PublicKey, tokenId: Field = TokenId.default): AccountV1 | undefined { | ||
| const key = this._key(publicKey, tokenId); | ||
| const location = this._locations.get(key); | ||
| if (location === undefined) return undefined; | ||
| return this._accounts.get(location); | ||
| } | ||
|
|
||
| getOrCreateAccount(publicKey: PublicKey, tokenId: Field = TokenId.default): AccountV1 { | ||
| const account = this.getAccount(publicKey, tokenId); | ||
| if (account != undefined) { | ||
| return account; | ||
| } | ||
|
|
||
| this.addAccount(publicKey, 0); | ||
| return this.getAccount(publicKey, tokenId)!; | ||
| } | ||
|
|
||
| applyTransaction(transaction: ZkappCommand, fee: UInt64, networkState: ChainView): void { | ||
| const feePayerAccount = this.getAccount(transaction.feePayer.body.publicKey); | ||
| if (!feePayerAccount) { | ||
| throw new Error('fee payer account not found'); | ||
| } | ||
|
|
||
| const feePayerAccountV2 = AccountV2.fromV1(feePayerAccount); | ||
| const feePayerUpdate = checkAndApplyFeePayment( | ||
| networkState, | ||
| feePayerAccountV2, | ||
| new ZkappFeePayment({ | ||
| publicKey: feePayerAccount.publicKey, | ||
| nonce: feePayerAccount.nonce, | ||
| fee, | ||
| validUntil: transaction.feePayer.body.validUntil, | ||
| }) | ||
| ); | ||
| if (feePayerUpdate.status == 'Failed') { | ||
| throw new Error(`failed to apply fee payment with errors: ${feePayerUpdate.errors}`); | ||
| } | ||
| this.saveAccount(feePayerAccount.publicKey, feePayerUpdate.updatedAccount.toV1()); | ||
|
|
||
| let feeExcessState: ApplyState<Int64> = { status: 'Alive', value: Int64.zero }; | ||
|
|
||
| for (const update of transaction.accountUpdates) { | ||
| const { body, authorization } = update; | ||
| if (body.authorizationKind.isProved.toBoolean() && !authorization.proof) { | ||
| throw Error( | ||
| `The actual authorization does not match the expected authorization kind. Did you forget to invoke \`await tx.prove()\`?` | ||
| ); | ||
| } | ||
| const account = this.getOrCreateAccount(body.publicKey, body.tokenId); | ||
| const accountUpdateV2 = AccountUpdateV2.fromInternalRepr(update.body); | ||
| const accountV2 = AccountV2.fromV1(account); | ||
| try { | ||
| const applied: AccountUpdateApplyResult = checkAndApplyAccountUpdate( | ||
| networkState, | ||
| accountV2, | ||
| accountUpdateV2, | ||
| feeExcessState | ||
| ); | ||
| if (applied.status == 'Failed') { | ||
| throw new Error(`Failed to apply account update with errors: ${applied.errors}`); | ||
| } | ||
| this.saveAccount(body.publicKey, applied.updatedAccount.toV1()); | ||
| feeExcessState = applied.updatedFeeExcessState; | ||
| } catch (e) { | ||
| console.error(e); | ||
| console.log(JSON.stringify(account, null, 2)); | ||
| console.log(JSON.stringify(accountV2, null, 2)); | ||
| console.log(JSON.stringify(update, null, 2)); | ||
| throw e; | ||
| } | ||
| } | ||
| } | ||
| } |
This file contains hidden or 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
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. only sorting imports |
This file contains hidden or 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.
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.
this breaks when using the local ledger because our error messages are slightly different. This is probably bad :((
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.
No this should actually be fine! Because we also want to improve the error messages and overall error handling. That being said, we should be able to make the error messages "backwards compatible" via something like this
throw Error('Account_delegate_precondition_unsatisfied: ....');