Skip to content

Commit

Permalink
Merge pull request #29 from AztecProtocol/stage
Browse files Browse the repository at this point in the history
Release merge
  • Loading branch information
PhilWindle authored Mar 13, 2023
2 parents 79cfff5 + 2581b33 commit eb9a0fe
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 57 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ export class ProofData {
public readonly backwardLink: Buffer;
public readonly allowChain: Buffer;

constructor(public rawProofData: Buffer) {
constructor(public rawProofData: Buffer, txId?: Buffer) {
this.proofId = rawProofData.readUInt32BE(ProofDataOffsets.PROOF_ID);
this.noteCommitment1 = rawProofData.slice(
ProofDataOffsets.NOTE_COMMITMENT_1,
Expand Down Expand Up @@ -109,6 +109,8 @@ export class ProofData {
this.defiRoot = rawProofData.slice(ProofDataOffsets.DEFI_ROOT, ProofDataOffsets.DEFI_ROOT + 32);
this.backwardLink = rawProofData.slice(ProofDataOffsets.BACKWARD_LINK, ProofDataOffsets.BACKWARD_LINK + 32);
this.allowChain = rawProofData.slice(ProofDataOffsets.ALLOW_CHAIN, ProofDataOffsets.ALLOW_CHAIN + 32);

this.txId_ = txId;
}

get allowChainFromNote1() {
Expand Down
2 changes: 1 addition & 1 deletion yarn-project/falafel/src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -348,7 +348,7 @@ export function appFactory(server: Server, prefix: string, metrics: Metrics, ser

router.get('/get-pending-txs', recordMetric, async (ctx: Koa.Context) => {
const txs = await server.getUnsettledTxs();
ctx.body = txs.map(tx => new ProofData(tx.proofData)).map(toPendingTxJson);
ctx.body = txs.map(tx => new ProofData(tx.proofData, tx.id)).map(toPendingTxJson);
ctx.status = 200;
});

Expand Down
99 changes: 52 additions & 47 deletions yarn-project/falafel/src/world_state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,7 @@ type TxPoolProfile = {
export class WorldState {
private serialQueue = new SerialQueue();
private pipeline?: RollupPipeline;
private txPoolProfile!: TxPoolProfile;
private txPoolProfileValidUntil!: Date;
private txPoolProfile?: Promise<TxPoolProfile>;
private initialSubtreeRootsCache: Buffer[] = [];
private bridgeStatsQueryHandler: BridgeStatsQueryHandler;

Expand All @@ -88,13 +87,6 @@ export class WorldState {
private expireTxPoolAfter = 60 * 1000,
private log = createLogger('WorldState'),
) {
this.txPoolProfile = {
numTxs: 0,
numTxsInNextRollup: 0,
pendingTxCount: 0,
pendingSecondClassTxCount: 0,
pendingBridgeStats: [],
};
this.bridgeStatsQueryHandler = new BridgeStatsQueryHandler(rollupDb, txFeeResolver);
}

Expand Down Expand Up @@ -136,45 +128,10 @@ export class WorldState {
}

public async getTxPoolProfile() {
// getPendingTxs from rollup db
// remove the tranasctions that we know are in the next rollup currently being built
if (!this.txPoolProfileValidUntil || new Date().getTime() > this.txPoolProfileValidUntil.getTime()) {
const pendingTxs = await this.rollupDb.getPendingTxs();
const processedTransactions = this.pipeline?.getProcessedTxs() || [];
const pendingTransactionsNotInRollup = pendingTxs.filter(elem =>
processedTransactions.every(tx => !tx.id.equals(elem.id)),
);

const pendingBridgeStats: Map<bigint, BridgeStat> = new Map();
for (const tx of pendingTransactionsNotInRollup) {
const proof = new ProofData(tx.proofData);
if (proof.proofId !== ProofId.DEFI_DEPOSIT) {
continue;
}

const defiProof = new DefiDepositProofData(proof);
const rollupTx = createDefiRollupTx(tx, defiProof);
const bridgeCallData = rollupTx.bridgeCallData!;
const bridgeProfile = pendingBridgeStats.get(bridgeCallData) || {
bridgeCallData,
gasAccrued: 0,
};
bridgeProfile.gasAccrued += this.txFeeResolver.getSingleBridgeTxGas(bridgeCallData) + rollupTx.excessGas;

pendingBridgeStats.set(bridgeCallData, bridgeProfile);
}

this.txPoolProfile = {
numTxs: await this.rollupDb.getUnsettledTxCount(),
numTxsInNextRollup: processedTransactions.length,
pendingBridgeStats: [...pendingBridgeStats.values()],
pendingTxCount: pendingTransactionsNotInRollup.length,
pendingSecondClassTxCount: await this.rollupDb.getPendingSecondClassTxCount(),
};
this.txPoolProfileValidUntil = new Date(Date.now() + this.expireTxPoolAfter);
if (!this.txPoolProfile) {
this.txPoolProfile = this.computeTxPoolProfile();
}

return this.txPoolProfile;
return await this.txPoolProfile;
}

public async queryBridgeStats(query: BridgePublishQuery) {
Expand Down Expand Up @@ -821,4 +778,52 @@ export class WorldState {

await this.worldStateDb.commit();
}

/**
* getPendingTxs from rollup db
* remove the tranasctions that we know are in the next rollup currently being built
*/
private async computeTxPoolProfile() {
const pendingTxs = await this.rollupDb.getPendingTxs();
const processedTransactions = this.pipeline?.getProcessedTxs() || [];

const txsBeingProcessed = new Set(processedTransactions.map(tx => tx.id.toString('hex')));

const pendingTransactionsNotBeingProcessed = pendingTxs.filter(
elem => !txsBeingProcessed.has(elem.id.toString('hex')),
);

const pendingBridgeStats: Map<bigint, BridgeStat> = new Map();
for (const tx of pendingTransactionsNotBeingProcessed) {
const proof = new ProofData(tx.proofData);
if (proof.proofId !== ProofId.DEFI_DEPOSIT) {
continue;
}

const defiProof = new DefiDepositProofData(proof);
const rollupTx = createDefiRollupTx(tx, defiProof);
const bridgeCallData = rollupTx.bridgeCallData!;
const bridgeProfile = pendingBridgeStats.get(bridgeCallData) || {
bridgeCallData,
gasAccrued: 0,
};
bridgeProfile.gasAccrued += this.txFeeResolver.getSingleBridgeTxGas(bridgeCallData) + rollupTx.excessGas;

pendingBridgeStats.set(bridgeCallData, bridgeProfile);
}

setTimeout(() => {
this.txPoolProfile = undefined;
}, this.expireTxPoolAfter);

const result: TxPoolProfile = {
numTxs: await this.rollupDb.getUnsettledTxCount(),
numTxsInNextRollup: processedTransactions.length,
pendingBridgeStats: [...pendingBridgeStats.values()],
pendingTxCount: pendingTransactionsNotBeingProcessed.length,
pendingSecondClassTxCount: await this.rollupDb.getPendingSecondClassTxCount(),
};

return result;
}
}
22 changes: 14 additions & 8 deletions yarn-project/sdk/src/core_sdk/block_downloader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,17 @@ export class BlockDownloader {
private interruptableSleep = new InterruptableSleep();
private semaphore: Semaphore;
private queue = new MemoryFifo<Block[]>();
private genesisTake;
private debug = createDebugLogger('bb:block_downloader');

constructor(private rollupProvider: RollupProvider, maxQueueSize: number, private initialTreeSize: number) {
constructor(private rollupProvider: RollupProvider, maxQueueSize: number, initialTreeSize: number) {
this.semaphore = new Semaphore(maxQueueSize);
// Choosing 55 as an initial chunk to insert if starting from 0, is an aztec-connect optimisation.
// The aztec-connect genesis data consists of 73 rollups.
// Initially inserting 55 brings us to 128, after which we work with chunks of 128 rollups.
// If not synching from zero, the chunk size is whatever takes us up to the next 128 alignment.
// This allows for optimal subtree insertions in the client side merkle tree for better sync performance.
this.genesisTake = 128 - (initialTreeSize % 128);
}

public start(from = 0) {
Expand All @@ -31,13 +38,12 @@ export class BlockDownloader {
const fn = async () => {
while (this.running) {
try {
// Choosing 55 as an initial chunk to insert if starting from 0, is an aztec-connect optimisation.
// The aztec-connect genesis data consists of 73 rollups.
// Initially inserting 55 brings us to 128, after which we work with chunks of 128 rollups.
// If not synching from zero, the chunk size is whatever takes us up to the next 128 alignment.
// This allows for optimal subtree insertions in the client side merkle tree for better sync performance.
const initialTake = 128 - ((this.from === 0 ? this.initialTreeSize : this.from) % 128);
const blocks = await this.rollupProvider.getBlocks(this.from, this.from === from ? initialTake : 128);
// If requesting from block 0, then take the fixed number of blocks to take us to 128 (genesisTake)
// Otherwise, take blocks as required to get us to a 128 aligned boundary starting from block (128 - initialTreeSize).
// e.g. we are trying to get to blocks 183, 311, 439 etc....
const takeValue =
this.from < this.genesisTake ? this.genesisTake - this.from : 128 - ((this.from - this.genesisTake) % 128);
const blocks = await this.rollupProvider.getBlocks(this.from, takeValue);

if (!blocks.length) {
await this.interruptableSleep.sleep(10000);
Expand Down

0 comments on commit eb9a0fe

Please sign in to comment.