Skip to content

Commit

Permalink
fmt
Browse files Browse the repository at this point in the history
  • Loading branch information
Maddiaa0 committed Feb 3, 2024
1 parent 09550a7 commit a546ff4
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 89 deletions.
22 changes: 13 additions & 9 deletions avm-transpiler/src/transpile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,12 @@ pub fn brillig_to_avm(brillig: &Brillig) -> Vec<u8> {
bytecode
}

fn handle_foreign_call(avm_instrs: &mut Vec<AvmInstruction>, function: &String, destinations: &Vec<ValueOrArray>, inputs: &Vec<ValueOrArray>) {
fn handle_foreign_call(
avm_instrs: &mut Vec<AvmInstruction>,
function: &String,
destinations: &Vec<ValueOrArray>,
inputs: &Vec<ValueOrArray>,
) {
// For the foreign calls we want to handle, we do not want inputs, as they are getters
assert!(inputs.len() == 0);
assert!(destinations.len() == 1);
Expand All @@ -298,23 +303,22 @@ fn handle_foreign_call(avm_instrs: &mut Vec<AvmInstruction>, function: &String,
"timestamp" => AvmOpcode::TIMESTAMP,
// "isStaticCall" => AvmOpcode::ISSTATICCALL,
// "isDelegateCall" => AvmOpcode::ISDELEGATECALL,
_ => panic!("Transpiler doesn't know how to process ForeignCall function {:?}", function),

_ => panic!(
"Transpiler doesn't know how to process ForeignCall function {:?}",
function
),
};

avm_instrs.push(AvmInstruction {
opcode,
indirect: Some(0),
operands: vec![
AvmOperand::U32 { value: dest_offset as u32},
],
operands: vec![AvmOperand::U32 {
value: dest_offset as u32,
}],
..Default::default()
});


}


/// Compute an array that maps each Brillig pc to an AVM pc.
/// This must be done before transpiling to properly transpile jump destinations.
/// This is necessary for two reasons:
Expand Down
155 changes: 75 additions & 80 deletions yarn-project/acir-simulator/src/avm/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
import { Fr } from '@aztec/foundation/fields';
import { AztecAddress } from '@aztec/foundation/aztec-address';
import { EthAddress } from '@aztec/foundation/eth-address';
import { Fr } from '@aztec/foundation/fields';
import { AvmTestContractArtifact } from '@aztec/noir-contracts';

import { mock, MockProxy } from 'jest-mock-extended';
import { MockProxy, mock } from 'jest-mock-extended';

import { AvmMachineState } from './avm_machine_state.js';
import { TypeTag } from './avm_memory_types.js';
import { initExecutionEnvironment } from './fixtures/index.js';
import { executeAvm } from './interpreter/interpreter.js';
import { AvmJournal } from './journal/journal.js';
import { Add, CalldataCopy, Return } from './opcodes/index.js';
import { decodeFromBytecode, encodeToBytecode } from './serialization/bytecode_serialization.js';
import { AvmMachineState } from './avm_machine_state.js';
import { EthAddress } from '@aztec/foundation/eth-address';

describe('avm', () => {
let journal: MockProxy<AvmJournal>;
let journal: MockProxy<AvmJournal>;

beforeEach(() => {
journal = mock<AvmJournal>();
})
});

it('Should execute bytecode that performs basic addition', async () => {
const calldata: Fr[] = [new Fr(1), new Fr(2)];
Expand All @@ -45,8 +45,6 @@ describe('avm', () => {
});

describe('testing transpiled Noir contracts', () => {


// TODO(https://github.com/AztecProtocol/aztec-packages/issues/4361): sync wire format w/transpiler.
it('Should execute contract function that performs addition', async () => {
const calldata: Fr[] = [new Fr(1), new Fr(2)];
Expand All @@ -69,87 +67,84 @@ describe('avm', () => {
expect(returnData).toEqual([new Fr(3)]);
});

describe("Test env getters from noir contract", () => {

describe('Test env getters from noir contract', () => {
const testEnvGetter = async (valueName: string, value: any, functionName: string) => {
const getterArtifact = AvmTestContractArtifact.functions.find(f => f.name === functionName)
const getterArtifact = AvmTestContractArtifact.functions.find(f => f.name === functionName);

// Decode
const instructions = decodeFromBytecode(Buffer.from(getterArtifact!.bytecode, 'base64'));
// Decode
const instructions = decodeFromBytecode(Buffer.from(getterArtifact!.bytecode, 'base64'));

// Execute
const overrides = {[valueName]: value};
const context = new AvmMachineState(initExecutionEnvironment(overrides));
const avmReturnData = await executeAvm(context, journal, instructions);
// Execute
const overrides = { [valueName]: value };
const context = new AvmMachineState(initExecutionEnvironment(overrides));
const avmReturnData = await executeAvm(context, journal, instructions);

expect(avmReturnData.reverted).toBe(false);
expect(avmReturnData.reverted).toBe(false);

const returnData = avmReturnData.output;
expect(returnData.length).toBe(1);
expect(returnData).toEqual([value.toField()]);

}
const returnData = avmReturnData.output;
expect(returnData.length).toBe(1);
expect(returnData).toEqual([value.toField()]);
};

it('address', async () => {
const address = AztecAddress.fromField(new Fr(1));
await testEnvGetter('address', address, 'avm_getAddress');
});

it('storageAddress', async () => {
const storageAddress = AztecAddress.fromField(new Fr(1));
await testEnvGetter('storageAddress', storageAddress, 'avm_getStorageAddress');
});

it('sender', async () => {
const sender = AztecAddress.fromField(new Fr(1));
await testEnvGetter('sender', sender, 'avm_getSender');
});

it('origin', async () => {
const origin = AztecAddress.fromField(new Fr(1));
await testEnvGetter('origin', origin, 'avm_getOrigin');
});

it('portal', async () => {
const portal = EthAddress.fromField(new Fr(1));
await testEnvGetter('portal', portal, 'avm_getPortal');
});

it('getFeePerL1Gas', async () => {
const fee = new Fr(1);
await testEnvGetter('feePerL1Gas', fee, 'avm_getFeePerL1Gas');
});

it('getFeePerL2Gas', async () => {
const fee = new Fr(1);
await testEnvGetter('feePerL2Gas', fee, 'avm_getFeePerL2Gas');
});

it('getFeePerDaGas', async () => {
const fee = new Fr(1);
await testEnvGetter('feePerDaGas', fee, 'avm_getFeePerDaGas');
});

// it('chainId', async () => {
// const chainId = new Fr(1);
// await testEnvGetter('chainId', chainId, 'avm_getChainId');
// });

// it('version', async () => {
// const version = new Fr(1);
// await testEnvGetter('version', version, 'avm_getVersion');
// });

// it('blockNumber', async () => {
// const blockNumber = new Fr(1);
// await testEnvGetter('blockNumber', blockNumber, 'avm_getBlockNumber');
// });

// it('timestamp', async () => {
// const timestamp = new Fr(1);
// await testEnvGetter('timestamp', timestamp, 'avm_getTimestamp');
// });
});

it('storageAddress', async () => {
const storageAddress = AztecAddress.fromField(new Fr(1));
await testEnvGetter('storageAddress', storageAddress, 'avm_getStorageAddress');
});

it('sender', async () => {
const sender = AztecAddress.fromField(new Fr(1));
await testEnvGetter('sender', sender, 'avm_getSender');
});

it('origin', async () => {
const origin = AztecAddress.fromField(new Fr(1));
await testEnvGetter('origin', origin, 'avm_getOrigin');
});

it('portal', async () => {
const portal = EthAddress.fromField(new Fr(1));
await testEnvGetter('portal', portal, 'avm_getPortal');
});

it('getFeePerL1Gas', async () => {
const fee = new Fr(1);
await testEnvGetter('feePerL1Gas', fee, 'avm_getFeePerL1Gas');
});

it('getFeePerL2Gas', async () => {
const fee = new Fr(1);
await testEnvGetter('feePerL2Gas', fee, 'avm_getFeePerL2Gas');
});

it('getFeePerDaGas', async () => {
const fee = new Fr(1);
await testEnvGetter('feePerDaGas', fee, 'avm_getFeePerDaGas');
});

// it('chainId', async () => {
// const chainId = new Fr(1);
// await testEnvGetter('chainId', chainId, 'avm_getChainId');
// });

// it('version', async () => {
// const version = new Fr(1);
// await testEnvGetter('version', version, 'avm_getVersion');
// });

// it('blockNumber', async () => {
// const blockNumber = new Fr(1);
// await testEnvGetter('blockNumber', blockNumber, 'avm_getBlockNumber');
// });

// it('timestamp', async () => {
// const timestamp = new Fr(1);
// await testEnvGetter('timestamp', timestamp, 'avm_getTimestamp');
// });
});

});
});

0 comments on commit a546ff4

Please sign in to comment.