-
Notifications
You must be signed in to change notification settings - Fork 598
refactor: call requests #7483
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
Merged
Merged
refactor: call requests #7483
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
4e5da0e
Don't export counters of public call requests from private.
LeilaWang c5ac2f7
Use PublicCallRequest struct in private.
LeilaWang 20530b8
Refactor call requests.
LeilaWang e62b0c0
Merge remote-tracking branch 'origin/master' into lw/public_call_requ…
LeilaWang 1e34f64
fmt
LeilaWang fcf1508
Fix tests.
LeilaWang fed5fc7
Fix tests.
LeilaWang 8a13200
Fix.
LeilaWang e38ce1d
Ensure no unexpected data in public inputs.
LeilaWang bd0f782
Merge remote-tracking branch 'origin/master' into lw/public_call_requ…
LeilaWang 81deaed
Merge remote-tracking branch 'origin/master' into lw/public_call_requ…
LeilaWang 59574fb
Small update.
LeilaWang 42bdb4d
Check tx data.
LeilaWang a0b297e
Merge remote-tracking branch 'origin/master' into lw/public_call_requ…
LeilaWang 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
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 |
|---|---|---|
|
|
@@ -6,20 +6,18 @@ use crate::{ | |
| key_validation_request::get_key_validation_request, arguments, returns::pack_returns, | ||
| call_private_function::call_private_function_internal, header::get_header_at, | ||
| logs::{emit_encrypted_note_log, emit_encrypted_event_log}, | ||
| enqueue_public_function_call::{ | ||
| enqueue_public_function_call_internal, set_public_teardown_function_call_internal, | ||
| parse_public_call_stack_item_from_oracle | ||
| } | ||
| enqueue_public_function_call::{enqueue_public_function_call_internal, set_public_teardown_function_call_internal} | ||
| } | ||
| }; | ||
| use dep::protocol_types::{ | ||
| abis::{ | ||
| caller_context::CallerContext, function_selector::FunctionSelector, | ||
| call_context::CallContext, function_selector::FunctionSelector, gas::Gas, | ||
| max_block_number::MaxBlockNumber, | ||
| validation_requests::{KeyValidationRequest, KeyValidationRequestAndGenerator}, | ||
| private_call_request::PrivateCallRequest, private_circuit_public_inputs::PrivateCircuitPublicInputs, | ||
| public_call_stack_item::PublicCallStackItem, read_request::ReadRequest, note_hash::NoteHash, | ||
| nullifier::Nullifier, log_hash::{LogHash, NoteLogHash, EncryptedLogHash} | ||
| public_call_request::PublicCallRequest, public_call_stack_item::PublicCallStackItem, | ||
| public_call_stack_item_compressed::PublicCallStackItemCompressed, read_request::ReadRequest, | ||
| note_hash::NoteHash, nullifier::Nullifier, log_hash::{LogHash, NoteLogHash, EncryptedLogHash} | ||
| }, | ||
| address::{AztecAddress, EthAddress}, | ||
| constants::{ | ||
|
|
@@ -54,8 +52,8 @@ struct PrivateContext { | |
| nullifiers: BoundedVec<Nullifier, MAX_NULLIFIERS_PER_CALL>, | ||
|
|
||
| private_call_requests : BoundedVec<PrivateCallRequest, MAX_PRIVATE_CALL_STACK_LENGTH_PER_CALL>, | ||
| public_call_stack_hashes : BoundedVec<Field, MAX_PUBLIC_CALL_STACK_LENGTH_PER_CALL>, | ||
| public_teardown_function_hash: Field, | ||
| public_call_requests : BoundedVec<PublicCallRequest, MAX_PUBLIC_CALL_STACK_LENGTH_PER_CALL>, | ||
| public_teardown_call_request: PublicCallRequest, | ||
| l2_to_l1_msgs : BoundedVec<L2ToL1Message, MAX_L2_TO_L1_MSGS_PER_CALL>, | ||
| // docs:end:private-context | ||
|
|
||
|
|
@@ -89,8 +87,8 @@ impl PrivateContext { | |
| nullifiers: BoundedVec::new(), | ||
| historical_header: inputs.historical_header, | ||
| private_call_requests: BoundedVec::new(), | ||
| public_call_stack_hashes: BoundedVec::new(), | ||
| public_teardown_function_hash: 0, | ||
| public_call_requests: BoundedVec::new(), | ||
| public_teardown_call_request: PublicCallRequest::empty(), | ||
| l2_to_l1_msgs: BoundedVec::new(), | ||
| note_encrypted_logs_hashes: BoundedVec::new(), | ||
| encrypted_logs_hashes: BoundedVec::new(), | ||
|
|
@@ -164,8 +162,8 @@ impl PrivateContext { | |
| note_hashes: self.note_hashes.storage, | ||
| nullifiers: self.nullifiers.storage, | ||
| private_call_requests: self.private_call_requests.storage, | ||
| public_call_stack_hashes: self.public_call_stack_hashes.storage, | ||
| public_teardown_function_hash: self.public_teardown_function_hash, | ||
| public_call_requests: self.public_call_requests.storage, | ||
| public_teardown_call_request: self.public_teardown_call_request, | ||
| l2_to_l1_msgs: self.l2_to_l1_msgs.storage, | ||
| start_side_effect_counter: self.inputs.start_side_effect_counter, | ||
| end_side_effect_counter: self.side_effect_counter, | ||
|
|
@@ -353,7 +351,7 @@ impl PrivateContext { | |
| ) -> PackedReturns { | ||
| let mut is_static_call = is_static_call | self.inputs.call_context.is_static_call; | ||
| let start_side_effect_counter = self.side_effect_counter; | ||
| let item = call_private_function_internal( | ||
| let (end_side_effect_counter, returns_hash) = call_private_function_internal( | ||
| contract_address, | ||
| function_selector, | ||
| args_hash, | ||
|
|
@@ -362,8 +360,6 @@ impl PrivateContext { | |
| is_delegate_call | ||
| ); | ||
|
|
||
| assert_eq(item.public_inputs.start_side_effect_counter, start_side_effect_counter); | ||
| let end_side_effect_counter = item.public_inputs.end_side_effect_counter; | ||
| self.side_effect_counter = end_side_effect_counter + 1; | ||
|
|
||
| // TODO (fees) figure out why this crashes the prover and enable it | ||
|
|
@@ -379,49 +375,25 @@ impl PrivateContext { | |
| // self.min_revertible_side_effect_counter = item.public_inputs.min_revertible_side_effect_counter; | ||
| // } | ||
|
|
||
| assert(contract_address.eq(item.contract_address)); | ||
| assert(function_selector.eq(item.function_data.selector)); | ||
|
|
||
| assert(args_hash == item.public_inputs.args_hash); | ||
|
|
||
| // Assert that the call context of the call generated by the oracle matches our request. | ||
| assert(item.public_inputs.call_context.is_delegate_call == is_delegate_call); | ||
| assert(item.public_inputs.call_context.is_static_call == is_static_call); | ||
|
|
||
| if (is_delegate_call) { | ||
| // For delegate calls, we also constrain the execution context address for the nested call to be equal to our address. | ||
| assert( | ||
| item.public_inputs.call_context.storage_contract_address.eq(self.inputs.call_context.storage_contract_address) | ||
| ); | ||
| assert(item.public_inputs.call_context.msg_sender.eq(self.inputs.call_context.msg_sender)); | ||
| } else { | ||
| // For non-delegate calls, we also constrain the execution context address for the nested call to be equal to the address we called. | ||
| assert(item.public_inputs.call_context.storage_contract_address.eq(contract_address)); | ||
| assert( | ||
| item.public_inputs.call_context.msg_sender.eq(self.inputs.call_context.storage_contract_address) | ||
| ); | ||
| } | ||
| let call_context = self.generate_call_context( | ||
| contract_address, | ||
| function_selector, | ||
| is_static_call, | ||
| is_delegate_call | ||
| ); | ||
|
|
||
| let mut caller_context = CallerContext::empty(); | ||
| caller_context.is_static_call = self.inputs.call_context.is_static_call; | ||
| if is_delegate_call { | ||
| caller_context.msg_sender = self.inputs.call_context.msg_sender; | ||
| caller_context.storage_contract_address = self.inputs.call_context.storage_contract_address; | ||
| } | ||
| self.private_call_requests.push( | ||
| PrivateCallRequest { | ||
| target: item.contract_address, | ||
| call_context: item.public_inputs.call_context, | ||
| function_data: item.function_data, | ||
| args_hash: item.public_inputs.args_hash, | ||
| returns_hash: item.public_inputs.returns_hash, | ||
| caller_context, | ||
| contract_address, | ||
| call_context, | ||
| args_hash, | ||
| returns_hash, | ||
| start_side_effect_counter, | ||
| end_side_effect_counter | ||
| } | ||
| ); | ||
|
|
||
| PackedReturns::new(item.public_inputs.returns_hash) | ||
| PackedReturns::new(returns_hash) | ||
| } | ||
|
|
||
| pub fn call_public_function<let ARGS_COUNT: u32>( | ||
|
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. With the changes, it's 5k less to call/enqueue a public function. |
||
|
|
@@ -489,28 +461,37 @@ impl PrivateContext { | |
| is_static_call: bool, | ||
| is_delegate_call: bool | ||
| ) { | ||
| let counter = self.next_counter(); | ||
MirandaWood marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| let mut is_static_call = is_static_call | self.inputs.call_context.is_static_call; | ||
| let fields = enqueue_public_function_call_internal( | ||
| enqueue_public_function_call_internal( | ||
| contract_address, | ||
| function_selector, | ||
| args_hash, | ||
| self.side_effect_counter, | ||
| counter, | ||
| is_static_call, | ||
| is_delegate_call | ||
| ); | ||
|
|
||
| let item = parse_public_call_stack_item_from_oracle(fields); | ||
| self.validate_call_stack_item_from_oracle( | ||
| item, | ||
| let call_context = self.generate_call_context( | ||
| contract_address, | ||
| function_selector, | ||
| args_hash, | ||
| is_static_call, | ||
| is_delegate_call | ||
| ); | ||
|
|
||
| self.side_effect_counter = self.side_effect_counter + 1; | ||
| self.public_call_stack_hashes.push(item.get_compressed().hash()); | ||
| let item = PublicCallStackItemCompressed { | ||
| contract_address, | ||
| call_context, | ||
| args_hash, | ||
| returns_hash: 0, | ||
| revert_code: 0, | ||
| start_gas_left: Gas::empty(), | ||
| end_gas_left: Gas::empty() | ||
| }; | ||
|
|
||
| let call_request = PublicCallRequest { item, counter }; | ||
| self.public_call_requests.push(call_request); | ||
| } | ||
|
|
||
| pub fn set_public_teardown_function<let ARGS_COUNT: u32>( | ||
|
|
@@ -532,63 +513,59 @@ impl PrivateContext { | |
| is_static_call: bool, | ||
| is_delegate_call: bool | ||
| ) { | ||
| let counter = self.next_counter(); | ||
|
|
||
| let mut is_static_call = is_static_call | self.inputs.call_context.is_static_call; | ||
| let fields = set_public_teardown_function_call_internal( | ||
| set_public_teardown_function_call_internal( | ||
| contract_address, | ||
| function_selector, | ||
| args_hash, | ||
| self.side_effect_counter, | ||
| counter, | ||
| is_static_call, | ||
| is_delegate_call | ||
| ); | ||
|
|
||
| let item = parse_public_call_stack_item_from_oracle(fields); | ||
| self.validate_call_stack_item_from_oracle( | ||
| item, | ||
| let call_context = self.generate_call_context( | ||
| contract_address, | ||
| function_selector, | ||
| args_hash, | ||
| is_static_call, | ||
| is_delegate_call | ||
| ); | ||
|
|
||
| self.side_effect_counter = self.side_effect_counter + 1; | ||
| self.public_teardown_function_hash = item.get_compressed().hash(); | ||
| let item = PublicCallStackItemCompressed { | ||
| contract_address, | ||
| call_context, | ||
| args_hash, | ||
| returns_hash: 0, | ||
| revert_code: 0, | ||
| start_gas_left: Gas::empty(), | ||
| end_gas_left: Gas::empty() | ||
| }; | ||
|
|
||
| self.public_teardown_call_request = PublicCallRequest { | ||
| item, | ||
| counter, | ||
| }; | ||
| } | ||
|
|
||
| fn validate_call_stack_item_from_oracle( | ||
| fn generate_call_context( | ||
| self, | ||
| item: PublicCallStackItem, | ||
| contract_address: AztecAddress, | ||
| function_selector: FunctionSelector, | ||
| args_hash: Field, | ||
| is_static_call: bool, | ||
| is_delegate_call: bool | ||
| ) { | ||
| assert(contract_address.eq(item.contract_address)); | ||
| assert(function_selector.eq(item.function_data.selector)); | ||
|
|
||
| assert_eq(item.public_inputs.start_side_effect_counter, self.side_effect_counter); | ||
|
|
||
| assert(args_hash == item.public_inputs.args_hash); | ||
|
|
||
| // Assert that the call context of the enqueued call generated by the oracle matches our request. | ||
| assert(item.public_inputs.call_context.is_delegate_call == is_delegate_call); | ||
| assert(item.public_inputs.call_context.is_static_call == is_static_call); | ||
|
|
||
| if (is_delegate_call) { | ||
| // For delegate calls, we also constrain the execution context address for the nested call to be equal to our address. | ||
| assert( | ||
| item.public_inputs.call_context.storage_contract_address.eq(self.inputs.call_context.storage_contract_address) | ||
| ); | ||
| assert(item.public_inputs.call_context.msg_sender.eq(self.inputs.call_context.msg_sender)); | ||
| ) -> CallContext { | ||
| let msg_sender = if is_delegate_call { | ||
| self.msg_sender() | ||
| } else { | ||
| // For non-delegate calls, we also constrain the execution context address for the nested call to be equal to the address we called. | ||
| assert(item.public_inputs.call_context.storage_contract_address.eq(contract_address)); | ||
| assert( | ||
| item.public_inputs.call_context.msg_sender.eq(self.inputs.call_context.storage_contract_address) | ||
| ); | ||
| } | ||
| self.this_address() | ||
| }; | ||
| let storage_contract_address = if is_delegate_call { | ||
| self.this_address() | ||
| } else { | ||
| contract_address | ||
| }; | ||
| CallContext { msg_sender, storage_contract_address, function_selector, is_static_call, is_delegate_call } | ||
| } | ||
|
|
||
| fn next_counter(&mut self) -> u32 { | ||
|
|
@@ -614,8 +591,8 @@ impl Empty for PrivateContext { | |
| note_hashes: BoundedVec::new(), | ||
| nullifiers: BoundedVec::new(), | ||
| private_call_requests: BoundedVec::new(), | ||
| public_call_stack_hashes: BoundedVec::new(), | ||
| public_teardown_function_hash: 0, | ||
| public_call_requests: BoundedVec::new(), | ||
| public_teardown_call_request: PublicCallRequest::empty(), | ||
| l2_to_l1_msgs: BoundedVec::new(), | ||
| historical_header: Header::empty(), | ||
| note_encrypted_logs_hashes: BoundedVec::new(), | ||
|
|
||
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.
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.
The same checks for (non_)delegate call is done in the kernel and must be done there. It's not necessary to check them here in the app circuit. If we want to abort early before executing kernel circuits, can check and throw from the simulator.