-
Notifications
You must be signed in to change notification settings - Fork 133
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
Fix: Keep track of changing permissions during transaction validation #1412
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
6212e81
simple ledger to record permissions changes throughout tx validation
mitschabaude c16d605
only use internal apply on existing accounts for now
mitschabaude 191f605
adapt upgradability test
mitschabaude ca46087
submodules
mitschabaude 6c9606d
don't rely on getters
mitschabaude 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 |
---|---|---|
|
@@ -43,6 +43,8 @@ import { | |
type ActionStates, | ||
type NetworkConstants, | ||
} from './mina/mina-instance.js'; | ||
import { SimpleLedger } from './mina/transaction-logic/ledger.js'; | ||
import { assert } from './gadgets/common.js'; | ||
|
||
export { | ||
createTransaction, | ||
|
@@ -395,12 +397,10 @@ function LocalBlockchain({ | |
|
||
if (enforceTransactionLimits) verifyTransactionLimits(txn.transaction); | ||
|
||
for (const update of txn.transaction.accountUpdates) { | ||
let accountJson = ledger.getAccount( | ||
Ml.fromPublicKey(update.body.publicKey), | ||
Ml.constFromField(update.body.tokenId) | ||
); | ||
// create an ad-hoc ledger to record changes to accounts within the transaction | ||
let simpleLedger = SimpleLedger.create(); | ||
|
||
for (const update of txn.transaction.accountUpdates) { | ||
let authIsProof = !!update.authorization.proof; | ||
let kindIsProof = update.body.authorizationKind.isProved.toBoolean(); | ||
// checks and edge case where a proof is expected, but the developer forgot to invoke await tx.prove() | ||
|
@@ -411,16 +411,31 @@ function LocalBlockchain({ | |
); | ||
} | ||
|
||
if (accountJson) { | ||
let account = Account.fromJSON(accountJson); | ||
let account = simpleLedger.load(update.body); | ||
|
||
// the first time we encounter an account, use it from the persistent ledger | ||
if (account === undefined) { | ||
let accountJson = ledger.getAccount( | ||
Ml.fromPublicKey(update.body.publicKey), | ||
Ml.constFromField(update.body.tokenId) | ||
); | ||
if (accountJson !== undefined) { | ||
let storedAccount = Account.fromJSON(accountJson); | ||
simpleLedger.store(storedAccount); | ||
account = storedAccount; | ||
} | ||
} | ||
|
||
// TODO: verify account update even if the account doesn't exist yet, using a default initial account | ||
if (account !== undefined) { | ||
await verifyAccountUpdate( | ||
account, | ||
update, | ||
commitments, | ||
this.proofsEnabled, | ||
this.getNetworkId() | ||
); | ||
simpleLedger.apply(update); | ||
} | ||
} | ||
|
||
|
@@ -1239,15 +1254,20 @@ async function verifyAccountUpdate( | |
publicOutput: [], | ||
}; | ||
|
||
let verificationKey = account.zkapp?.verificationKey?.data!; | ||
let verificationKey = account.zkapp?.verificationKey?.data; | ||
assert( | ||
verificationKey !== undefined, | ||
'Account does not have a verification key' | ||
); | ||
|
||
isValidProof = await verify(proof, verificationKey); | ||
if (!isValidProof) { | ||
throw Error( | ||
`Invalid proof for account update\n${JSON.stringify(update)}` | ||
); | ||
} | ||
} catch (error) { | ||
errorTrace += '\n\n' + (error as Error).message; | ||
errorTrace += '\n\n' + (error as Error).stack; | ||
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. much better to also include the stack trace here - that way you can see where the code failed |
||
isValidProof = false; | ||
} | ||
} | ||
|
@@ -1261,7 +1281,7 @@ async function verifyAccountUpdate( | |
networkId | ||
); | ||
} catch (error) { | ||
errorTrace += '\n\n' + (error as Error).message; | ||
errorTrace += '\n\n' + (error as Error).stack; | ||
isValidSignature = false; | ||
} | ||
} | ||
|
@@ -1289,7 +1309,7 @@ async function verifyAccountUpdate( | |
if (!verified) { | ||
throw Error( | ||
`Transaction verification failed: Cannot update field '${field}' because permission for this field is '${p}', but the required authorization was not provided or is invalid. | ||
${errorTrace !== '' ? 'Error trace: ' + errorTrace : ''}` | ||
${errorTrace !== '' ? 'Error trace: ' + errorTrace : ''}\n\n` | ||
); | ||
} | ||
} | ||
|
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,30 @@ | ||
/** | ||
* Apply transactions to a ledger of accounts. | ||
*/ | ||
import { type AccountUpdate } from '../../account_update.js'; | ||
import { Account } from '../account.js'; | ||
|
||
export { applyAccountUpdate }; | ||
|
||
/** | ||
* Apply a single account update to update an account. | ||
* | ||
* TODO: | ||
* - This must receive and return some context global to the transaction, to check validity | ||
* - Should operate on the value / bigint type, not the provable type | ||
*/ | ||
function applyAccountUpdate(account: Account, update: AccountUpdate): Account { | ||
account.publicKey.assertEquals(update.publicKey); | ||
account.tokenId.assertEquals(update.tokenId, 'token id mismatch'); | ||
|
||
// clone account (TODO: do this efficiently) | ||
let json = Account.toJSON(account); | ||
account = Account.fromJSON(json); | ||
|
||
// update permissions | ||
if (update.update.permissions.isSome.toBoolean()) { | ||
account.permissions = update.update.permissions.value; | ||
} | ||
|
||
return account; | ||
} |
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,60 @@ | ||
/** | ||
* A ledger of accounts - simple model of a local blockchain. | ||
*/ | ||
import { PublicKey } from '../../signature.js'; | ||
import { type AccountUpdate, TokenId } from '../../account_update.js'; | ||
import { Account, newAccount } from '../account.js'; | ||
import { Field } from '../../field.js'; | ||
import { applyAccountUpdate } from './apply.js'; | ||
|
||
export { SimpleLedger }; | ||
|
||
class SimpleLedger { | ||
accounts: Map<bigint, Account>; | ||
|
||
constructor() { | ||
this.accounts = new Map(); | ||
} | ||
|
||
static create(): SimpleLedger { | ||
return new SimpleLedger(); | ||
} | ||
|
||
exists({ publicKey, tokenId = TokenId.default }: InputAccountId): boolean { | ||
return this.accounts.has(accountId({ publicKey, tokenId })); | ||
} | ||
|
||
store(account: Account): void { | ||
this.accounts.set(accountId(account), account); | ||
} | ||
|
||
load({ | ||
publicKey, | ||
tokenId = TokenId.default, | ||
}: InputAccountId): Account | undefined { | ||
let id = accountId({ publicKey, tokenId }); | ||
let account = this.accounts.get(id); | ||
return account; | ||
} | ||
|
||
apply(update: AccountUpdate): void { | ||
let id = accountId(update.body); | ||
let account = this.accounts.get(id); | ||
account ??= newAccount(update.body); | ||
|
||
let updated = applyAccountUpdate(account, update); | ||
this.accounts.set(id, updated); | ||
} | ||
} | ||
|
||
type AccountId = { publicKey: PublicKey; tokenId: Field }; | ||
type InputAccountId = { publicKey: PublicKey; tokenId?: Field }; | ||
|
||
function accountId(account: AccountId): bigint { | ||
let id = account.publicKey.x.toBigInt(); | ||
id <<= 1n; | ||
id |= BigInt(account.publicKey.isOdd.toBoolean()); | ||
id <<= BigInt(Field.sizeInBits); | ||
id |= account.tokenId.toBigInt(); | ||
return id; | ||
} |
Submodule mina
updated
6 files
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.
ran into this being
undefined
in a test and failed down the line - better to have an explicit error about it right away