Skip to content

Commit

Permalink
fix: Add checks for arg-type (#638)
Browse files Browse the repository at this point in the history
* fix: Add checks for arg-type

* fix: support arg of 'number' type

* fix: correct wrapped fr in acir sim

* no need to force encode in some e2e tests!

---------

Co-authored-by: Maddiaa0 <[email protected]>
Co-authored-by: Rahul Kothari <[email protected]>
  • Loading branch information
3 people authored May 22, 2023
1 parent 9d3ea2c commit 4ffbe9c
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 33 deletions.
12 changes: 11 additions & 1 deletion yarn-project/acir-simulator/src/abi_coder/encoder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,17 @@ class ArgumentEncoder {
private encodeArgument(abiType: ABIType, arg: any) {
switch (abiType.kind) {
case 'field':
this.flattened.push(new Fr(arg));
if (typeof arg === 'number') {
this.flattened.push(new Fr(BigInt(arg)));
} else if (typeof arg === 'bigint') {
this.flattened.push(new Fr(arg));
} else if (typeof arg === 'object') {
if (typeof arg.toField === 'function') {
this.flattened.push(arg.toField());
} else {
this.flattened.push(arg);
}
}
break;
case 'boolean':
this.flattened.push(new Fr(arg ? 1n : 0n));
Expand Down
11 changes: 3 additions & 8 deletions yarn-project/acir-simulator/src/client/private_execution.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -329,7 +329,7 @@ describe('Private Execution test suite', () => {
AztecAddress.random(),
AztecAddress.ZERO,
new FunctionData(Buffer.alloc(4), true, false),
encodeArguments(parentAbi, [Fr.fromBuffer(childAddress.toBuffer()).value, Fr.fromBuffer(childSelector).value]),
encodeArguments(parentAbi, [Fr.fromBuffer(childAddress.toBuffer()), Fr.fromBuffer(childSelector)]),
Fr.random(),
txContext,
Fr.ZERO,
Expand Down Expand Up @@ -427,8 +427,7 @@ describe('Private Execution test suite', () => {
AztecAddress.random(),
contractAddress,
new FunctionData(Buffer.alloc(4), true, true),
// BUG: placing a fr in args will result in a fr wrapped in an fr: https://github.com/AztecProtocol/aztec-packages/issues/611
encodeArguments(abi, [bridgedAmount, recipient, messageKey.value, secret.value]),
encodeArguments(abi, [bridgedAmount, recipient, messageKey, secret]),
Fr.random(),
txContext,
Fr.ZERO,
Expand Down Expand Up @@ -459,11 +458,7 @@ describe('Private Execution test suite', () => {
AztecAddress.random(),
parentAddress,
new FunctionData(Buffer.alloc(4), true, false),
encodeArguments(parentAbi, [
Fr.fromBuffer(childAddress.toBuffer()).value,
Fr.fromBuffer(childSelector).value,
42n,
]),
encodeArguments(parentAbi, [Fr.fromBuffer(childAddress.toBuffer()), Fr.fromBuffer(childSelector), 42n]),
Fr.random(),
txContext,
Fr.ZERO,
Expand Down
26 changes: 7 additions & 19 deletions yarn-project/end-to-end/src/e2e_nested_contract.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,6 @@ describe('e2e_nested_contract', () => {
return contract;
};

const addressToField = (address: AztecAddress): bigint => {
return Fr.fromBuffer(address.toBuffer()).value;
};

const getChildStoredValue = (child: { address: AztecAddress }) =>
node.getStorageAt(child.address, 1n).then(x => toBigInt(x!));

Expand All @@ -69,7 +65,7 @@ describe('e2e_nested_contract', () => {
*/
it('should mine transactions that perform nested calls', async () => {
const tx = parentContract.methods
.entryPoint(addressToField(childContract.address), Fr.fromBuffer(childContract.methods.value.selector).value)
.entryPoint(childContract.address, Fr.fromBuffer(childContract.methods.value.selector))
.send({ from: accounts[0] });

await tx.isMined(0, 0.1);
Expand All @@ -80,11 +76,7 @@ describe('e2e_nested_contract', () => {

it('should mine transactions that perform public nested calls', async () => {
const tx = parentContract.methods
.pubEntryPoint(
addressToField(childContract.address),
Fr.fromBuffer(childContract.methods.pubValue.selector).value,
42n,
)
.pubEntryPoint(childContract.address, Fr.fromBuffer(childContract.methods.pubValue.selector), 42n)
.send({ from: accounts[0] });

await tx.isMined(0, 0.1);
Expand All @@ -95,11 +87,7 @@ describe('e2e_nested_contract', () => {

it('should mine transactions that enqueue public calls', async () => {
const tx = parentContract.methods
.enqueueCallToChild(
addressToField(childContract.address),
Fr.fromBuffer(childContract.methods.pubStoreValue.selector).value,
42n,
)
.enqueueCallToChild(childContract.address, Fr.fromBuffer(childContract.methods.pubStoreValue.selector), 42n)
.send({ from: accounts[0] });

await tx.isMined(0, 0.1);
Expand All @@ -112,8 +100,8 @@ describe('e2e_nested_contract', () => {
it('should mine transactions that enqueue a public call with nested public calls', async () => {
const tx = parentContract.methods
.enqueueCallToPubEntryPoint(
addressToField(childContract.address),
Fr.fromBuffer(childContract.methods.pubStoreValue.selector).value,
childContract.address,
Fr.fromBuffer(childContract.methods.pubStoreValue.selector),
42n,
)
.send({ from: accounts[0] });
Expand All @@ -128,8 +116,8 @@ describe('e2e_nested_contract', () => {
it.skip('should mine transactions that enqueue multiple public calls with nested public calls', async () => {
const tx = parentContract.methods
.enqueueCallsToPubEntryPoint(
addressToField(childContract.address),
Fr.fromBuffer(childContract.methods.pubStoreValue.selector).value,
childContract.address,
Fr.fromBuffer(childContract.methods.pubStoreValue.selector),
42n,
)
.send({ from: accounts[0] });
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -158,11 +158,7 @@ describe('e2e_rollup_native_asset_contract', () => {
const ethOutAddress = EthAddress.fromString('0x000000000000000000000000000000000000dead');

const tx = contract.methods
.withdraw(
withdrawAmount,
pointToPublicKey(await aztecRpcServer.getAccountPublicKey(owner)),
ethOutAddress.toField().value,
)
.withdraw(withdrawAmount, pointToPublicKey(await aztecRpcServer.getAccountPublicKey(owner)), ethOutAddress)
.send({ from: accounts[0] });

await tx.isMined(0, 0.1);
Expand Down

0 comments on commit 4ffbe9c

Please sign in to comment.