-
Notifications
You must be signed in to change notification settings - Fork 293
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
feat: Add deploy contract helper to aztec-nr #4775
Merged
+340
−64
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
f648f2e
Add ContractInstance to noir protocol types
spalladino a77fea6
Format noir-protocol-circuits
spalladino 8da115b
Move CONTRACT_INSTANCE_LENGTH to global constants
spalladino caa1a8c
Add getContractInstance oracle call
spalladino 737fe98
Set deployer address as a protocol constant
spalladino 62953af
Add deploy helper method to aztec-nr
spalladino e8d97e7
Yarn
spalladino 4dfb6c7
Patch slither_output with output from CI
spalladino 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
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,38 @@ | ||
use crate::{ | ||
context::PrivateContext, | ||
oracle::get_contract_instance::get_contract_instance, | ||
}; | ||
|
||
use dep::protocol_types::{ | ||
address::AztecAddress, | ||
abis::function_selector::FunctionSelector, | ||
constants::DEPLOYER_CONTRACT_ADDRESS, | ||
}; | ||
|
||
// Calls `deploy` on the deployer contract to deploy a new instance. | ||
pub fn deploy_contract(context: &mut PrivateContext, target: AztecAddress) { | ||
let instance = get_contract_instance(target); | ||
|
||
let mut universal_deploy = false; | ||
if ! instance.deployer.is_zero() { | ||
assert(instance.deployer == context.this_address(), "Deployer address does not match current address"); | ||
universal_deploy = true; | ||
} | ||
|
||
// Adapted from noir-contracts/contracts/contract_instance_deployer_contract/src/interface/ContractInstanceDeployer.nr | ||
// That file was autogenerated running the following command from noir-projects/noir-contracts: | ||
// ../../yarn-project/node_modules/.bin/aztec-cli codegen target/contract_instance_deployer_contract-ContractInstanceDeployer.json --nr -o ./contracts/contract_instance_deployer_contract/src/interface | ||
let mut serialized_args = [0; 6]; | ||
serialized_args[0] = instance.salt; | ||
serialized_args[1] = instance.contract_class_id.to_field(); | ||
serialized_args[2] = instance.initialization_hash; | ||
serialized_args[3] = instance.portal_contract_address.to_field(); | ||
serialized_args[4] = instance.public_keys_hash.to_field(); | ||
serialized_args[5] = universal_deploy as Field; | ||
|
||
let _call_result = context.call_private_function( | ||
AztecAddress::from_field(DEPLOYER_CONTRACT_ADDRESS), | ||
FunctionSelector::from_field(0x883355ab), | ||
serialized_args | ||
); | ||
} |
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,5 +1,6 @@ | ||
mod avm; | ||
mod context; | ||
mod deploy; | ||
mod hash; | ||
mod hasher; | ||
mod history; | ||
|
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
14 changes: 14 additions & 0 deletions
14
noir-projects/aztec-nr/aztec/src/oracle/get_contract_instance.nr
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,14 @@ | ||
use dep::protocol_types::{address::AztecAddress, contract_instance::ContractInstance, constants::CONTRACT_INSTANCE_LENGTH}; | ||
|
||
#[oracle(getContractInstance)] | ||
fn get_contract_instance_oracle(_address: AztecAddress) -> [Field; CONTRACT_INSTANCE_LENGTH] {} | ||
|
||
unconstrained fn get_contract_instance_internal(address: AztecAddress) -> [Field; CONTRACT_INSTANCE_LENGTH] { | ||
get_contract_instance_oracle(address) | ||
} | ||
|
||
pub fn get_contract_instance(address: AztecAddress) -> ContractInstance { | ||
let instance = ContractInstance::deserialize(get_contract_instance_internal(address)); | ||
assert(instance.to_address().eq(address)); | ||
instance | ||
} |
65 changes: 65 additions & 0 deletions
65
...s/contracts/contract_instance_deployer_contract/src/interface/ContractInstanceDeployer.nr
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,65 @@ | ||
/* Autogenerated file, do not edit! */ | ||
|
||
use dep::std; | ||
use dep::aztec::context::{PrivateContext, PublicContext}; | ||
use dep::aztec::protocol_types::{address::AztecAddress, abis::function_selector::FunctionSelector, constants::RETURN_VALUES_LENGTH}; | ||
|
||
struct ContractClassIdDeployStruct { | ||
inner: Field, | ||
} | ||
|
||
struct PortalContractAddressDeployStruct { | ||
inner: Field, | ||
} | ||
|
||
struct PublicKeysHashDeployStruct { | ||
inner: Field, | ||
} | ||
|
||
// Interface for calling ContractInstanceDeployer functions from a private context | ||
struct ContractInstanceDeployerPrivateContextInterface { | ||
address: AztecAddress, | ||
} | ||
|
||
impl ContractInstanceDeployerPrivateContextInterface { | ||
pub fn at(address: AztecAddress) -> Self { | ||
Self { address } | ||
} | ||
|
||
pub fn deploy( | ||
self, | ||
context: &mut PrivateContext, | ||
salt: Field, | ||
contract_class_id: ContractClassIdDeployStruct, | ||
initialization_hash: Field, | ||
portal_contract_address: PortalContractAddressDeployStruct, | ||
public_keys_hash: PublicKeysHashDeployStruct, | ||
universal_deploy: bool | ||
) -> [Field; RETURN_VALUES_LENGTH] { | ||
let mut serialized_args = [0; 6]; | ||
serialized_args[0] = salt; | ||
serialized_args[1] = contract_class_id.inner; | ||
serialized_args[2] = initialization_hash; | ||
serialized_args[3] = portal_contract_address.inner; | ||
serialized_args[4] = public_keys_hash.inner; | ||
serialized_args[5] = universal_deploy as Field; | ||
|
||
context.call_private_function( | ||
self.address, | ||
FunctionSelector::from_field(0x883355ab), | ||
serialized_args | ||
) | ||
} | ||
} | ||
|
||
// Interface for calling ContractInstanceDeployer functions from a public context | ||
struct ContractInstanceDeployerPublicContextInterface { | ||
address: AztecAddress, | ||
} | ||
|
||
impl ContractInstanceDeployerPublicContextInterface { | ||
pub fn at(address: AztecAddress) -> Self { | ||
Self { address } | ||
} | ||
} | ||
|
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
74 changes: 74 additions & 0 deletions
74
noir-projects/noir-protocol-circuits/src/crates/types/src/contract_instance.nr
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,74 @@ | ||
use crate::{ | ||
address::{ | ||
aztec_address::AztecAddress, eth_address::EthAddress, partial_address::PartialAddress, | ||
public_keys_hash::PublicKeysHash | ||
}, | ||
contract_class_id::ContractClassId, | ||
constants::{GENERATOR_INDEX__CONTRACT_DEPLOYMENT_DATA, CONTRACT_INSTANCE_LENGTH}, | ||
traits::{Deserialize, Hash, Serialize} | ||
}; | ||
|
||
struct ContractInstance { | ||
salt : Field, | ||
deployer: AztecAddress, | ||
contract_class_id : ContractClassId, | ||
initialization_hash : Field, | ||
portal_contract_address : EthAddress, | ||
public_keys_hash : PublicKeysHash, | ||
} | ||
|
||
impl Eq for ContractInstance { | ||
fn eq(self, other: Self) -> bool { | ||
self.public_keys_hash.eq(other.public_keys_hash) & | ||
self.initialization_hash.eq(other.initialization_hash) & | ||
self.contract_class_id.eq(other.contract_class_id) & | ||
self.salt.eq(other.salt) & | ||
self.portal_contract_address.eq(other.portal_contract_address) | ||
} | ||
} | ||
|
||
impl Serialize<CONTRACT_INSTANCE_LENGTH> for ContractInstance { | ||
fn serialize(self) -> [Field; CONTRACT_INSTANCE_LENGTH] { | ||
[ | ||
self.salt, | ||
self.deployer.to_field(), | ||
self.contract_class_id.to_field(), | ||
self.initialization_hash, | ||
self.portal_contract_address.to_field(), | ||
self.public_keys_hash.to_field() | ||
] | ||
} | ||
} | ||
|
||
impl Deserialize<CONTRACT_INSTANCE_LENGTH> for ContractInstance { | ||
fn deserialize(serialized: [Field; CONTRACT_INSTANCE_LENGTH]) -> Self { | ||
Self { | ||
salt: serialized[0], | ||
deployer: AztecAddress::from_field(serialized[1]), | ||
contract_class_id: ContractClassId::from_field(serialized[2]), | ||
initialization_hash: serialized[3], | ||
portal_contract_address: EthAddress::from_field(serialized[4]), | ||
public_keys_hash: PublicKeysHash::from_field(serialized[5]), | ||
} | ||
} | ||
} | ||
|
||
impl Hash for ContractInstance { | ||
fn hash(self) -> Field { | ||
self.to_address().to_field() | ||
} | ||
} | ||
|
||
impl ContractInstance { | ||
fn to_address(self) -> AztecAddress { | ||
AztecAddress::compute( | ||
self.public_keys_hash, | ||
PartialAddress::compute( | ||
self.contract_class_id, | ||
self.salt, | ||
self.initialization_hash, | ||
self.portal_contract_address | ||
) | ||
) | ||
} | ||
} |
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
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.
@spalladino where does this fn selector point to/where does it come from?
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.
@rahul-kothari this is autogenerated code by the noir codegen. It calls the function with the same name as the method, in this case
deploy
.