-
Notifications
You must be signed in to change notification settings - Fork 236
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
16 changed files
with
307 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
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,70 @@ | ||
import { AztecAddress, EthAddress, Fr } from '@aztec/circuits.js'; | ||
|
||
import { DeepMockProxy, mockDeep } from 'jest-mock-extended'; | ||
|
||
import { AvmContext } from '../avm_context.js'; | ||
import { Field } from '../avm_memory_types.js'; | ||
import { initContext } from '../fixtures/index.js'; | ||
import { AvmPersistableStateManager } from '../journal/journal.js'; | ||
import { ContractInstanceCopy } 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('CONTRACTINSTANCECOPY', () => { | ||
it('Should (de)serialize correctly', () => { | ||
const buf = Buffer.from([ | ||
ContractInstanceCopy.opcode, // opcode | ||
0x01, // indirect | ||
...Buffer.from('12345678', 'hex'), // addressOffset | ||
...Buffer.from('a2345678', 'hex'), // dstOffset | ||
]); | ||
const inst = new ContractInstanceCopy( | ||
/*indirect=*/ 0x01, | ||
/*addressOffset=*/ 0x12345678, | ||
/*dstOffset=*/ 0xa2345678, | ||
); | ||
|
||
expect(ContractInstanceCopy.deserialize(buf)).toEqual(inst); | ||
expect(inst.serialize()).toEqual(buf); | ||
}); | ||
|
||
it('should copy contract instance to memory', 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 ContractInstanceCopy(/*indirect=*/ 0, /*addressOffset=*/ 0, /*dstOffset=*/ 1).execute(context); | ||
|
||
const actual = context.machineState.memory.getSlice(1, 6); | ||
expect(actual).toEqual([ | ||
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), | ||
]); | ||
}); | ||
}); | ||
}); |
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,50 @@ | ||
import { AztecAddress } from '@aztec/circuits.js'; | ||
|
||
import type { AvmContext } from '../avm_context.js'; | ||
import { Field } from '../avm_memory_types.js'; | ||
import { InstructionExecutionError } from '../errors.js'; | ||
import { Opcode, OperandType } from '../serialization/instruction_serialization.js'; | ||
import { Addressing } from './addressing_mode.js'; | ||
import { Instruction } from './instruction.js'; | ||
|
||
export class ContractInstanceCopy extends Instruction { | ||
static readonly type: string = 'CONTRACTINSTANCECOPY'; | ||
static readonly opcode: Opcode = Opcode.CONTRACTINSTANCECOPY; | ||
// 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); | ||
|
||
if (!instance) { | ||
throw new InstructionExecutionError(`Contract instance not found: ${address}`); | ||
} | ||
const data = [ | ||
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
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.