-
Notifications
You must be signed in to change notification settings - Fork 260
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
18 changed files
with
420 additions
and
40 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
24 changes: 23 additions & 1 deletion
24
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 |
---|---|---|
@@ -1,14 +1,36 @@ | ||
use dep::protocol_types::{address::AztecAddress, contract_instance::ContractInstance, constants::CONTRACT_INSTANCE_LENGTH}; | ||
use dep::protocol_types::{ | ||
address::AztecAddress, contract_instance::ContractInstance, utils::arr_copy_slice, | ||
constants::CONTRACT_INSTANCE_LENGTH | ||
}; | ||
|
||
#[oracle(getContractInstance)] | ||
fn get_contract_instance_oracle(_address: AztecAddress) -> [Field; CONTRACT_INSTANCE_LENGTH] {} | ||
|
||
// Returns a ContractInstance plus a boolean indicating whether the instance was found. | ||
#[oracle(avmOpcodeGetContractInstance)] | ||
fn get_contract_instance_oracle_avm(_address: AztecAddress) -> [Field; CONTRACT_INSTANCE_LENGTH + 1] {} | ||
|
||
unconstrained fn get_contract_instance_internal(address: AztecAddress) -> [Field; CONTRACT_INSTANCE_LENGTH] { | ||
get_contract_instance_oracle(address) | ||
} | ||
|
||
unconstrained fn get_contract_instance_internal_avm(address: AztecAddress) -> [Field; CONTRACT_INSTANCE_LENGTH + 1] { | ||
get_contract_instance_oracle_avm(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 | ||
} | ||
|
||
pub fn get_contract_instance_avm(address: AztecAddress) -> Option<ContractInstance> { | ||
let fields = get_contract_instance_internal_avm(address); | ||
let found = fields[0]; | ||
if found == 0 { | ||
Option::none() | ||
} else { | ||
let ci_fields = arr_copy_slice(fields, [0; CONTRACT_INSTANCE_LENGTH], 1); | ||
Option::some(ContractInstance::deserialize(ci_fields)) | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
import { AztecAddress, EthAddress, Fr } from '@aztec/circuits.js'; | ||
|
||
import { type DeepMockProxy, mockDeep } from 'jest-mock-extended'; | ||
|
||
import { type AvmContext } from '../avm_context.js'; | ||
import { Field } from '../avm_memory_types.js'; | ||
import { initContext } from '../fixtures/index.js'; | ||
import { type AvmPersistableStateManager } from '../journal/journal.js'; | ||
import { GetContractInstance } from './contract.js'; | ||
|
||
describe('Contract opcodes', () => { | ||
let context: AvmContext; | ||
let journal: DeepMockProxy<AvmPersistableStateManager>; | ||
const address = AztecAddress.random(); | ||
|
||
beforeEach(async () => { | ||
journal = mockDeep<AvmPersistableStateManager>(); | ||
context = initContext({ | ||
persistableState: journal, | ||
}); | ||
}); | ||
|
||
describe('GETCONTRACTINSTANCE', () => { | ||
it('Should (de)serialize correctly', () => { | ||
const buf = Buffer.from([ | ||
GetContractInstance.opcode, // opcode | ||
0x01, // indirect | ||
...Buffer.from('12345678', 'hex'), // addressOffset | ||
...Buffer.from('a2345678', 'hex'), // dstOffset | ||
]); | ||
const inst = new GetContractInstance( | ||
/*indirect=*/ 0x01, | ||
/*addressOffset=*/ 0x12345678, | ||
/*dstOffset=*/ 0xa2345678, | ||
); | ||
|
||
expect(GetContractInstance.deserialize(buf)).toEqual(inst); | ||
expect(inst.serialize()).toEqual(buf); | ||
}); | ||
|
||
it('should copy contract instance to memory if found', async () => { | ||
context.machineState.memory.set(0, new Field(address.toField())); | ||
|
||
const contractInstance = { | ||
address: address, | ||
version: 1 as const, | ||
salt: new Fr(20), | ||
contractClassId: new Fr(30), | ||
initializationHash: new Fr(40), | ||
portalContractAddress: EthAddress.random(), | ||
publicKeysHash: new Fr(50), | ||
deployer: AztecAddress.random(), | ||
}; | ||
|
||
journal.hostStorage.contractsDb.getContractInstance.mockReturnValue(Promise.resolve(contractInstance)); | ||
|
||
await new GetContractInstance(/*indirect=*/ 0, /*addressOffset=*/ 0, /*dstOffset=*/ 1).execute(context); | ||
|
||
const actual = context.machineState.memory.getSlice(1, 7); | ||
expect(actual).toEqual([ | ||
new Field(1), // found | ||
new Field(contractInstance.salt), | ||
new Field(contractInstance.deployer), | ||
new Field(contractInstance.contractClassId), | ||
new Field(contractInstance.initializationHash), | ||
new Field(contractInstance.portalContractAddress.toField()), | ||
new Field(contractInstance.publicKeysHash), | ||
]); | ||
}); | ||
|
||
it('should return zeroes if not found', async () => { | ||
context.machineState.memory.set(0, new Field(address.toField())); | ||
journal.hostStorage.contractsDb.getContractInstance.mockReturnValue(Promise.resolve(undefined)); | ||
|
||
await new GetContractInstance(/*indirect=*/ 0, /*addressOffset=*/ 0, /*dstOffset=*/ 1).execute(context); | ||
|
||
const actual = context.machineState.memory.getSlice(1, 7); | ||
expect(actual).toEqual([ | ||
new Field(0), // found | ||
new Field(0), | ||
new Field(0), | ||
new Field(0), | ||
new Field(0), | ||
new Field(0), | ||
new Field(0), | ||
]); | ||
}); | ||
}); | ||
}); |
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,58 @@ | ||
import { AztecAddress, Fr } from '@aztec/circuits.js'; | ||
|
||
import type { AvmContext } from '../avm_context.js'; | ||
import { Field } from '../avm_memory_types.js'; | ||
import { Opcode, OperandType } from '../serialization/instruction_serialization.js'; | ||
import { Addressing } from './addressing_mode.js'; | ||
import { Instruction } from './instruction.js'; | ||
|
||
export class GetContractInstance extends Instruction { | ||
static readonly type: string = 'GETCONTRACTINSTANCE'; | ||
static readonly opcode: Opcode = Opcode.GETCONTRACTINSTANCE; | ||
// Informs (de)serialization. See Instruction.deserialize. | ||
static readonly wireFormat: OperandType[] = [ | ||
OperandType.UINT8, | ||
OperandType.UINT8, | ||
OperandType.UINT32, | ||
OperandType.UINT32, | ||
]; | ||
|
||
constructor(private indirect: number, private addressOffset: number, private dstOffset: number) { | ||
super(); | ||
} | ||
|
||
async execute(context: AvmContext): Promise<void> { | ||
const [addressOffset, dstOffset] = Addressing.fromWire(this.indirect).resolve( | ||
[this.addressOffset, this.dstOffset], | ||
context.machineState.memory, | ||
); | ||
|
||
const address = AztecAddress.fromField(context.machineState.memory.get(addressOffset).toFr()); | ||
const instance = await context.persistableState.hostStorage.contractsDb.getContractInstance(address); | ||
|
||
const data = | ||
instance === undefined | ||
? [ | ||
new Field(0), // not found | ||
new Field(0), | ||
new Field(0), | ||
new Field(0), | ||
new Field(0), | ||
new Field(0), | ||
new Field(0), | ||
] | ||
: [ | ||
new Fr(1), // found | ||
instance.salt, | ||
instance.deployer.toField(), | ||
instance.contractClassId, | ||
instance.initializationHash, | ||
instance.portalContractAddress.toField(), | ||
instance.publicKeysHash, | ||
].map(f => new Field(f)); | ||
|
||
context.machineState.memory.setSlice(dstOffset, data); | ||
|
||
context.machineState.incrementPc(); | ||
} | ||
} |
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.