Skip to content

Commit

Permalink
chore: fix sassy-salamander chores v1 (#10218)
Browse files Browse the repository at this point in the history
Handful of chores.

fix #10074
fix #10075
fix #10077
  • Loading branch information
just-mitch authored Dec 2, 2024
1 parent cae1203 commit 7227b48
Show file tree
Hide file tree
Showing 8 changed files with 66 additions and 48 deletions.
5 changes: 5 additions & 0 deletions yarn-project/circuit-types/src/tx_execution_request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { BufferReader, serializeToBuffer } from '@aztec/foundation/serialize';
import { bufferToHex, hexToBuffer } from '@aztec/foundation/string';
import { type FieldsOf } from '@aztec/foundation/types';

import { inspect } from 'util';
import { z } from 'zod';

import { AuthWitness } from './auth_witness.js';
Expand Down Expand Up @@ -141,4 +142,8 @@ export class TxExecutionRequest {
[AuthWitness.random()],
);
}

[inspect.custom]() {
return `TxExecutionRequest(${this.origin} called ${this.functionSelector})`;
}
}
6 changes: 5 additions & 1 deletion yarn-project/end-to-end/src/spartan/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -355,7 +355,11 @@ export async function awaitL2BlockNumber(
await sleep(1000);
tips = await rollupCheatCodes.getTips();
}
logger.info(`Reached L2 Block ${tips.pending}`);
if (tips.pending < blockNumber) {
throw new Error(`Timeout waiting for L2 Block ${blockNumber}, only reached ${tips.pending}`);
} else {
logger.info(`Reached L2 Block ${tips.pending}`);
}
}

export async function restartBot(namespace: string, logger: Logger) {
Expand Down
2 changes: 1 addition & 1 deletion yarn-project/foundation/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -164,4 +164,4 @@
"engines": {
"node": ">=18"
}
}
}
2 changes: 1 addition & 1 deletion yarn-project/prover-client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -104,4 +104,4 @@
"engines": {
"node": ">=18"
}
}
}
36 changes: 28 additions & 8 deletions yarn-project/pxe/src/pxe_service/pxe_service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ import {
} from '@aztec/protocol-contracts';
import { type AcirSimulator } from '@aztec/simulator';

import { inspect } from 'util';

import { type PXEServiceConfig, getPackageInfo } from '../config/index.js';
import { ContractDataOracle } from '../contract_data_oracle/index.js';
import { IncomingNoteDao } from '../database/incoming_note_dao.js';
Expand Down Expand Up @@ -519,8 +521,7 @@ export class PXEService implements PXE {
return new TxProvingResult(privateExecutionResult, publicInputs, clientIvcProof!);
})
.catch(err => {
this.log.error(err);
throw err;
throw this.contextualizeError(err, inspect(txRequest), inspect(privateExecutionResult));
});
}

Expand Down Expand Up @@ -576,8 +577,15 @@ export class PXEService implements PXE {
);
})
.catch(err => {
this.log.error(err);
throw err;
throw this.contextualizeError(
err,
inspect(txRequest),
`simulatePublic=${simulatePublic}`,
`msgSender=${msgSender?.toString() ?? 'undefined'}`,
`skipTxValidation=${skipTxValidation}`,
`profile=${profile}`,
`scopes=${scopes?.map(s => s.toString()).join(', ') ?? 'undefined'}`,
);
});
}

Expand All @@ -588,8 +596,7 @@ export class PXEService implements PXE {
}
this.log.info(`Sending transaction ${txHash}`);
await this.node.sendTx(tx).catch(err => {
this.log.error(err);
throw err;
throw this.contextualizeError(err, inspect(tx));
});
this.log.info(`Sent transaction ${txHash}`);
return txHash;
Expand All @@ -613,8 +620,12 @@ export class PXEService implements PXE {
return executionResult;
})
.catch(err => {
this.log.error(err);
throw err;
const stringifiedArgs = args.map(arg => arg.toString()).join(', ');
throw this.contextualizeError(
err,
`simulateUnconstrained ${to}:${functionName}(${stringifiedArgs})`,
`scopes=${scopes?.map(s => s.toString()).join(', ') ?? 'undefined'}`,
);
});
}

Expand Down Expand Up @@ -986,4 +997,13 @@ export class PXEService implements PXE {
async resetNoteSyncData() {
return await this.db.resetNoteSyncData();
}

private contextualizeError(err: Error, ...context: string[]): Error {
this.log.error(err.name, err);
this.log.debug('Context:');
for (const c of context) {
this.log.debug(c);
}
return err;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -843,7 +843,7 @@ class TestSubject extends Sequencer {
}

public override doRealWork() {
this.setState(SequencerState.IDLE, 0, true /** force */);
this.setState(SequencerState.IDLE, 0n, true /** force */);
return super.doRealWork();
}
}
59 changes: 24 additions & 35 deletions yarn-project/sequencer-client/src/sequencer/sequencer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ export class Sequencer {
public start() {
this.runningPromise = new RunningPromise(this.work.bind(this), this.pollingIntervalMs);
this.runningPromise.start();
this.setState(SequencerState.IDLE, 0, true /** force */);
this.setState(SequencerState.IDLE, 0n, true /** force */);
this.log.info('Sequencer started');
return Promise.resolve();
}
Expand All @@ -201,7 +201,7 @@ export class Sequencer {
this.log.debug(`Stopping sequencer`);
await this.runningPromise?.stop();
this.publisher.interrupt();
this.setState(SequencerState.STOPPED, 0, true /** force */);
this.setState(SequencerState.STOPPED, 0n, true /** force */);
this.log.info('Stopped sequencer');
}

Expand All @@ -212,7 +212,7 @@ export class Sequencer {
this.log.info('Restarting sequencer');
this.publisher.restart();
this.runningPromise!.start();
this.setState(SequencerState.IDLE, 0, true /** force */);
this.setState(SequencerState.IDLE, 0n, true /** force */);
}

/**
Expand All @@ -232,7 +232,7 @@ export class Sequencer {
* - If our block for some reason is not included, revert the state
*/
protected async doRealWork() {
this.setState(SequencerState.SYNCHRONIZING, 0);
this.setState(SequencerState.SYNCHRONIZING, 0n);
// Update state when the previous block has been synced
const prevBlockSynced = await this.isBlockSynced();
// Do not go forward with new block if the previous one has not been mined and processed
Expand All @@ -243,7 +243,7 @@ export class Sequencer {

this.log.debug('Previous block has been mined and processed');

this.setState(SequencerState.PROPOSER_CHECK, 0);
this.setState(SequencerState.PROPOSER_CHECK, 0n);

const chainTip = await this.l2BlockSource.getBlock(-1);
const historicalHeader = chainTip?.header;
Expand Down Expand Up @@ -277,9 +277,8 @@ export class Sequencer {
if (!this.shouldProposeBlock(historicalHeader, {})) {
return;
}
const secondsIntoSlot = getSecondsIntoSlot(this.l1GenesisTime, this.aztecSlotDuration, Number(slot));

this.setState(SequencerState.WAITING_FOR_TXS, secondsIntoSlot);
this.setState(SequencerState.WAITING_FOR_TXS, slot);

// Get txs to build the new block.
const pendingTxs = this.p2pClient.getTxs('pending');
Expand Down Expand Up @@ -325,7 +324,7 @@ export class Sequencer {
} catch (err) {
this.log.error(`Error assembling block`, (err as any).stack);
}
this.setState(SequencerState.IDLE, 0);
this.setState(SequencerState.IDLE, 0n);
}

protected async work() {
Expand All @@ -339,7 +338,7 @@ export class Sequencer {
throw err;
}
} finally {
this.setState(SequencerState.IDLE, 0);
this.setState(SequencerState.IDLE, 0n);
}
}

Expand Down Expand Up @@ -398,13 +397,23 @@ export class Sequencer {
return true;
}

setState(proposedState: SequencerState, secondsIntoSlot: number, force: boolean = false) {
/**
* Sets the sequencer state and checks if we have enough time left in the slot to transition to the new state.
* @param proposedState - The new state to transition to.
* @param currentSlotNumber - The current slot number.
* @param force - Whether to force the transition even if the sequencer is stopped.
*
* @dev If the `currentSlotNumber` doesn't matter (e.g. transitioning to IDLE), pass in `0n`;
* it is only used to check if we have enough time left in the slot to transition to the new state.
*/
setState(proposedState: SequencerState, currentSlotNumber: bigint, force: boolean = false) {
if (this.state === SequencerState.STOPPED && force !== true) {
this.log.warn(
`Cannot set sequencer from ${this.state} to ${proposedState} as it is stopped. Set force=true to override.`,
);
return;
}
const secondsIntoSlot = getSecondsIntoSlot(this.l1GenesisTime, this.aztecSlotDuration, Number(currentSlotNumber));
if (!this.doIHaveEnoughTimeLeft(proposedState, secondsIntoSlot)) {
throw new SequencerTooSlowError(this.state, proposedState, this.timeTable[proposedState], secondsIntoSlot);
}
Expand Down Expand Up @@ -567,12 +576,7 @@ export class Sequencer {

this.metrics.recordNewBlock(newGlobalVariables.blockNumber.toNumber(), validTxs.length);
const workTimer = new Timer();
const secondsIntoSlot = getSecondsIntoSlot(
this.l1GenesisTime,
this.aztecSlotDuration,
newGlobalVariables.slotNumber.toNumber(),
);
this.setState(SequencerState.CREATING_BLOCK, secondsIntoSlot);
this.setState(SequencerState.CREATING_BLOCK, newGlobalVariables.slotNumber.toBigInt());
this.log.info(
`Building blockNumber=${newGlobalVariables.blockNumber.toNumber()} txCount=${
validTxs.length
Expand Down Expand Up @@ -688,23 +692,13 @@ export class Sequencer {
this.log.info('Creating block proposal');
const proposal = await this.validatorClient.createBlockProposal(block.header, block.archive.root, txHashes);

let secondsIntoSlot = getSecondsIntoSlot(
this.l1GenesisTime,
this.aztecSlotDuration,
block.header.globalVariables.slotNumber.toNumber(),
);
const slotNumber = block.header.globalVariables.slotNumber.toBigInt();

this.setState(SequencerState.PUBLISHING_BLOCK_TO_PEERS, secondsIntoSlot);
this.setState(SequencerState.PUBLISHING_BLOCK_TO_PEERS, slotNumber);
this.log.info('Broadcasting block proposal to validators');
this.validatorClient.broadcastBlockProposal(proposal);

secondsIntoSlot = getSecondsIntoSlot(
this.l1GenesisTime,
this.aztecSlotDuration,
block.header.globalVariables.slotNumber.toNumber(),
);

this.setState(SequencerState.WAITING_FOR_ATTESTATIONS, secondsIntoSlot);
this.setState(SequencerState.WAITING_FOR_ATTESTATIONS, slotNumber);
const attestations = await this.validatorClient.collectAttestations(proposal, numberOfRequiredAttestations);
this.log.info(`Collected attestations from validators, number of attestations: ${attestations.length}`);

Expand Down Expand Up @@ -761,13 +755,8 @@ export class Sequencer {
txHashes?: TxHash[],
proofQuote?: EpochProofQuote,
) {
const secondsIntoSlot = getSecondsIntoSlot(
this.l1GenesisTime,
this.aztecSlotDuration,
block.header.globalVariables.slotNumber.toNumber(),
);
// Publishes new block to the network and awaits the tx to be mined
this.setState(SequencerState.PUBLISHING_BLOCK, secondsIntoSlot);
this.setState(SequencerState.PUBLISHING_BLOCK, block.header.globalVariables.slotNumber.toBigInt());

const publishedL2Block = await this.publisher.proposeL2Block(block, attestations, txHashes, proofQuote);
if (!publishedL2Block) {
Expand Down
2 changes: 1 addition & 1 deletion yarn-project/sequencer-client/src/sequencer/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,5 +75,5 @@ export function orderAttestations(attestations: BlockAttestation[], orderAddress

export function getSecondsIntoSlot(l1GenesisTime: number, aztecSlotDuration: number, slotNumber: number): number {
const slotStartTimestamp = l1GenesisTime + slotNumber * aztecSlotDuration;
return Date.now() / 1000 - slotStartTimestamp;
return Number((Date.now() / 1000 - slotStartTimestamp).toFixed(3));
}

0 comments on commit 7227b48

Please sign in to comment.