Skip to content

Commit

Permalink
fix: set side effect hwm in child private calls
Browse files Browse the repository at this point in the history
  • Loading branch information
alexghr committed Apr 10, 2024
1 parent 57a1d69 commit 5b7af0e
Show file tree
Hide file tree
Showing 6 changed files with 81 additions and 17 deletions.
1 change: 1 addition & 0 deletions noir-projects/aztec-nr/aztec/src/context.nr
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ mod public_context;
mod avm_context;
mod interface;
mod gas;
mod utils;

use interface::ContextInterface;
use private_context::PrivateContext;
Expand Down
15 changes: 1 addition & 14 deletions noir-projects/aztec-nr/aztec/src/context/private_context.nr
Original file line number Diff line number Diff line change
Expand Up @@ -347,18 +347,7 @@ impl PrivateContext {
assert_eq(item.public_inputs.start_side_effect_counter, self.side_effect_counter);
self.side_effect_counter = item.public_inputs.end_side_effect_counter + 1;

// TODO (fees) figure out why this crashes the prover and enable it
// we need this in order to pay fees inside child call contexts
// assert(
// (item.public_inputs.min_revertible_side_effect_counter == 0 as u32)
// | (item.public_inputs.min_revertible_side_effect_counter
// > self.min_revertible_side_effect_counter)
// );

// if item.public_inputs.min_revertible_side_effect_counter
// > self.min_revertible_side_effect_counter {
// self.min_revertible_side_effect_counter = item.public_inputs.min_revertible_side_effect_counter;
// }
self.min_revertible_side_effect_counter = crate::context::utils::max(item.public_inputs.min_revertible_side_effect_counter, self.min_revertible_side_effect_counter);

assert(contract_address.eq(item.contract_address));
assert(function_selector.eq(item.function_data.selector));
Expand All @@ -383,8 +372,6 @@ impl PrivateContext {
);
}

// crate::oracle::debug_log::debug_log_array_with_prefix("Private call stack item", item.serialize());

self.private_call_stack_hashes.push(item.hash());

item.public_inputs.return_values
Expand Down
3 changes: 3 additions & 0 deletions noir-projects/aztec-nr/aztec/src/context/utils.nr
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
pub fn max(a: u32, b: u32) -> u32 {
if a > b { a } else { b }
}
6 changes: 5 additions & 1 deletion yarn-project/aztec.js/src/account_manager/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,10 @@ import { DeployAccountSentTx } from './deploy_account_sent_tx.js';
/**
* Options to deploy an account contract.
*/
export type DeployAccountOptions = Pick<DeployOptions, 'fee' | 'skipClassRegistration' | 'skipPublicDeployment'>;
export type DeployAccountOptions = Pick<
DeployOptions,
'fee' | 'skipClassRegistration' | 'skipPublicDeployment' | 'skipPublicSimulation'
>;

/**
* Manages a user account. Provides methods for calculating the account's address, deploying the account contract,
Expand Down Expand Up @@ -167,6 +170,7 @@ export class AccountManager {
.then(deployMethod =>
deployMethod.send({
contractAddressSalt: this.salt,
skipPublicSimulation: opts?.skipPublicSimulation ?? false,
skipClassRegistration: opts?.skipClassRegistration ?? true,
skipPublicDeployment: opts?.skipPublicDeployment ?? true,
skipInitialization: false,
Expand Down
71 changes: 69 additions & 2 deletions yarn-project/end-to-end/src/e2e_account_init_fees.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
type DebugLogger,
ExtendedNote,
Fr,
type FunctionCall,
NativeFeePaymentMethod,
Note,
PrivateFeePaymentMethod,
Expand All @@ -12,10 +13,18 @@ import {
type TxHash,
TxStatus,
type Wallet,
computeAuthWitMessageHash,
computeMessageSecretHash,
generatePublicKey,
} from '@aztec/aztec.js';
import { type AztecAddress, CompleteAddress, Fq, getContractClassFromArtifact } from '@aztec/circuits.js';
import {
type AztecAddress,
CompleteAddress,
Fq,
FunctionData,
FunctionSelector,
getContractClassFromArtifact,
} from '@aztec/circuits.js';
import {
TokenContract as BananaCoin,
FPCContract,
Expand Down Expand Up @@ -232,7 +241,7 @@ describe('e2e_fees_account_init', () => {
});
});

describe('public through an FPC', () => {
describe('publicly through an FPC', () => {
let mintedPublicBananas: bigint;

beforeEach(async () => {
Expand Down Expand Up @@ -270,9 +279,34 @@ describe('e2e_fees_account_init', () => {
sequencersInitialGas + actualFee,
]);
});

it('failing to pay the fee drops the transaction', async () => {
await expect(
bobsAccountManager
.deploy({
skipPublicSimulation: true,
skipPublicDeployment: false,
fee: {
maxFee,
paymentMethod: new BuggedSetupFeePaymentMethod(
bananaCoin.address,
bananaFPC.address,
await bobsAccountManager.getWallet(),
),
},
})
.wait(),
).rejects.toThrow('Tx dropped by P2P node');

await expect(
ctx.aztecNode.getContract(bobsAccountManager.getCompleteAddress().address),
).resolves.toBeUndefined();
});
});
});

// TODO (alexg) add test to validate bugged account contracts can't grief the sequencer. Requires #5649

describe('another account pays the fee', () => {
describe('in the gas token', () => {
beforeEach(async () => {
Expand Down Expand Up @@ -335,3 +369,36 @@ describe('e2e_fees_account_init', () => {
await ctx.pxe.addNote(extendedNote);
}
});

class BuggedSetupFeePaymentMethod extends PublicFeePaymentMethod {
getFunctionCalls(maxFee: Fr): Promise<FunctionCall[]> {
const nonce = Fr.random();
const messageHash = computeAuthWitMessageHash(
this.paymentContract,
this.wallet.getChainId(),
this.wallet.getVersion(),
{
args: [this.wallet.getAddress(), this.paymentContract, maxFee, nonce],
functionData: new FunctionData(
FunctionSelector.fromSignature('transfer_public((Field),(Field),Field,Field)'),
false,
),
to: this.asset,
},
);

const tooMuchFee = new Fr(maxFee.toBigInt() * 2n);

return Promise.resolve([
this.wallet.setPublicAuthWit(messageHash, true).request(),
{
to: this.getPaymentContract(),
functionData: new FunctionData(
FunctionSelector.fromSignature('fee_entrypoint_public(Field,(Field),Field)'),
true,
),
args: [tooMuchFee, this.asset, nonce],
},
]);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,12 @@ export class SetupPhaseManager extends AbstractPhaseManager {
previousPublicKernelProof: Proof,
) {
this.log.verbose(`Processing tx ${tx.getTxHash()}`);
await this.publicContractsDB.addNewContracts(tx);
const [publicKernelOutput, publicKernelProof, newUnencryptedFunctionLogs, revertReason] =
await this.processEnqueuedPublicCalls(tx, previousPublicKernelOutput, previousPublicKernelProof).catch(
// the abstract phase manager throws if simulation gives error in a non-revertible phase
async err => {
await this.publicContractsDB.removeNewContracts(tx);
await this.publicStateDB.rollbackToCommit();
throw err;
},
Expand Down

0 comments on commit 5b7af0e

Please sign in to comment.