-
Notifications
You must be signed in to change notification settings - Fork 135
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 #1412 from o1-labs/fix/valid-permissions
Fix: Keep track of changing permissions during transaction validation
- Loading branch information
Showing
7 changed files
with
136 additions
and
15 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,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