Skip to content

Commit

Permalink
testing global cars in public context
Browse files Browse the repository at this point in the history
  • Loading branch information
benesjan committed Feb 7, 2024
1 parent 785ce34 commit d8d2851
Show file tree
Hide file tree
Showing 2 changed files with 116 additions and 0 deletions.
10 changes: 10 additions & 0 deletions yarn-project/noir-contracts/contracts/test_contract/src/main.nr
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,16 @@ contract Test {
storage.example_constant.initialize(&mut note, false);
}

#[aztec(public)]
fn assert_coinbase(coinbase: EthAddress) {
assert(context.coinbase().eq(coinbase), "Invalid coinbase");
}

#[aztec(public)]
fn assert_fee_recipient(fee_recipient: AztecAddress) {
assert(context.fee_recipient().eq(fee_recipient), "Invalid fee recipient");
}

unconstrained fn get_constant() -> pub Field {
let constant = storage.example_constant.view_note();
constant.value
Expand Down
106 changes: 106 additions & 0 deletions yarn-project/simulator/src/public/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -632,4 +632,110 @@ describe('ACIR public execution simulator', () => {
});
});
});

describe('Global variables in public context', () => {
let contractAddress: AztecAddress;
let callContext: CallContext;

beforeAll(() => {
contractAddress = AztecAddress.random();
callContext = CallContext.from({
msgSender: AztecAddress.random(),
storageContractAddress: AztecAddress.random(),
portalContractAddress: EthAddress.ZERO,
functionSelector: FunctionSelector.empty(),
isContractDeployment: false,
isDelegateCall: false,
isStaticCall: false,
startSideEffectCounter: 0,
});
});

const computeGlobalVariables = (coinbase: EthAddress, feeRecipient: AztecAddress) =>
new GlobalVariables(new Fr(1), new Fr(2), Fr.ZERO, Fr.ZERO, coinbase, feeRecipient);

describe('Coinbase', () => {
let expectedCoinbase: EthAddress;
let globalVariables: GlobalVariables;
let assertCoinbaseArtifact: FunctionArtifact;
let functionData: FunctionData;

beforeAll(() => {
expectedCoinbase = EthAddress.random();
globalVariables = computeGlobalVariables(expectedCoinbase, AztecAddress.random());

assertCoinbaseArtifact = TestContractArtifact.functions.find(f => f.name === 'assert_coinbase')!;
functionData = FunctionData.fromAbi(assertCoinbaseArtifact);
});

beforeEach(() => {
publicContracts.getBytecode.mockResolvedValue(Buffer.from(assertCoinbaseArtifact.bytecode, 'base64'));
});

it('Valid', () => {
// We set the coinbase function arg to one we set in global vars
const args = encodeArguments(assertCoinbaseArtifact, [expectedCoinbase]);

const execution: PublicExecution = { contractAddress, functionData, args, callContext };
executor = new PublicExecutor(publicState, publicContracts, commitmentsDb, header);

// Now we check that the global vars value was correctly set by checking if the assert func throws
expect(() => executor.simulate(execution, globalVariables)).not.toThrow();
});

it('Invalid', async () => {
// We set the coinbase function arg to a random invalid value
const invalidCoinbase = EthAddress.random();
const args = encodeArguments(assertCoinbaseArtifact, [invalidCoinbase]);

const execution: PublicExecution = { contractAddress, functionData, args, callContext };
executor = new PublicExecutor(publicState, publicContracts, commitmentsDb, header);

// Now we check that the function throws in case of invalid coinbase
await expect(executor.simulate(execution, globalVariables)).rejects.toThrowError('Invalid coinbase');
});
});

describe('Fee recipient', () => {
let expectedFeeRecipient: AztecAddress;
let globalVariables: GlobalVariables;
let assertFeeRecipientArtifact: FunctionArtifact;
let functionData: FunctionData;

beforeAll(() => {
expectedFeeRecipient = AztecAddress.random();
globalVariables = computeGlobalVariables(EthAddress.random(), expectedFeeRecipient);

assertFeeRecipientArtifact = TestContractArtifact.functions.find(f => f.name === 'assert_fee_recipient')!;
functionData = FunctionData.fromAbi(assertFeeRecipientArtifact);
});

beforeEach(() => {
publicContracts.getBytecode.mockResolvedValue(Buffer.from(assertFeeRecipientArtifact.bytecode, 'base64'));
});

it('Valid', () => {
// We set the fee recipient function arg to one we set in global vars
const args = encodeArguments(assertFeeRecipientArtifact, [expectedFeeRecipient]);

const execution: PublicExecution = { contractAddress, functionData, args, callContext };
executor = new PublicExecutor(publicState, publicContracts, commitmentsDb, header);

// Now we check that the global vars value was correctly set by checking if the assert func throws
expect(() => executor.simulate(execution, globalVariables)).not.toThrow();
});

it('Invalid', async () => {
// We set the fee recipient function arg to a random invalid value
const invalidFeeRecipient = AztecAddress.random();
const args = encodeArguments(assertFeeRecipientArtifact, [invalidFeeRecipient]);

const execution: PublicExecution = { contractAddress, functionData, args, callContext };
executor = new PublicExecutor(publicState, publicContracts, commitmentsDb, header);

// Now we check that the function throws in case of invalid fee recipient
await expect(executor.simulate(execution, globalVariables)).rejects.toThrowError('Invalid fee recipient');
});
});
});
});

0 comments on commit d8d2851

Please sign in to comment.