-
Notifications
You must be signed in to change notification settings - Fork 270
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: Initializer checks across txs (#4842)
The approach in #4807, where I pushed a note-hash for signalling initialization and then pushed a read-request to check initialization later, didn't work. It worked only when initialization is checked in the same tx as the initialization happens. If it happens on a different tx, the note-hash gets siloed and hashed with a nonce before getting added to the note hash tree. While we can silo from the contract (since the contract knows its own address), we cannot hash with the nonce since we don't know it. This nonce is typically stored as part of the associated note in the PXE db, but in this case, we are pusing a note commitment _without_ an associated note. Not only that, but the PXE today just doesn't support read requests not related to a note in the local db. So this PR changes that approach to just use the nullifier, which was the original idea, since it does not require emitting two pieces of data. It works because nullifiers are siloed but they are not hashed with a nonce, so it's possible to reconstruct them when checking initialization. However, since we don't yet support reading a nullifier in the same tx (or same block even) that it was created, we need to wait for the initialization tx to be mined before we can call any function in the contract.
- Loading branch information
1 parent
99bbaee
commit 747fc33
Showing
7 changed files
with
60 additions
and
41 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,29 +1,23 @@ | ||
use dep::protocol_types::hash::silo_nullifier; | ||
use crate::context::PrivateContext; | ||
use crate::history::nullifier_inclusion::prove_nullifier_inclusion; | ||
|
||
pub fn mark_as_initialized(context: &mut PrivateContext) { | ||
let init_nullifier = compute_unsiloed_contract_initialization_nullifier(context); | ||
let init_nullifier = compute_unsiloed_contract_initialization_nullifier(*context); | ||
context.push_new_nullifier(init_nullifier, 0); | ||
|
||
// We push a commitment as well and use this value to check initialization, | ||
// since we cannot yet read a nullifier from the same tx in which it was emitted. | ||
// Eventually, when that's supported, we should delete this note_hash and | ||
// have all checks rely on reading the nullifier directly. | ||
// TODO(@spalladino) Remove when possible. | ||
context.push_new_note_hash(init_nullifier); | ||
} | ||
|
||
// TODO(@spalladino): Add a variant using PublicContext once we can read nullifiers or note hashes from public-land. | ||
pub fn assert_is_initialized(context: &mut PrivateContext) { | ||
let init_nullifier = compute_contract_initialization_nullifier(context); | ||
context.push_read_request(init_nullifier); | ||
let init_nullifier = compute_contract_initialization_nullifier(*context); | ||
prove_nullifier_inclusion(init_nullifier, *context); | ||
} | ||
|
||
pub fn compute_contract_initialization_nullifier(context: &mut PrivateContext) -> Field { | ||
pub fn compute_contract_initialization_nullifier(context: PrivateContext) -> Field { | ||
let address = context.this_address(); | ||
silo_nullifier(address, compute_unsiloed_contract_initialization_nullifier(context)) | ||
} | ||
|
||
pub fn compute_unsiloed_contract_initialization_nullifier(context: &mut PrivateContext) -> Field { | ||
pub fn compute_unsiloed_contract_initialization_nullifier(context: PrivateContext) -> Field { | ||
context.this_address().to_field() | ||
} |
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