Skip to content

Commit

Permalink
Merging the develop branch into the main branch, v6.0.1
Browse files Browse the repository at this point in the history
  • Loading branch information
EvgenKor authored Nov 8, 2024
2 parents b472775 + 2defb09 commit a45d4e3
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 18 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "zkbob-client-js",
"version": "6.0.0",
"version": "6.0.1",
"description": "zkBob integration library",
"repository": "git@github.com:zkBob/libzkbob-client-js.git",
"author": "Dmitry Vdovin <voidxnull@gmail.com>",
Expand Down
47 changes: 34 additions & 13 deletions src/history.ts
Original file line number Diff line number Diff line change
Expand Up @@ -471,21 +471,16 @@ export class HistoryStorage {
sentHistoryRecords.push(aRec);
}

if (oldTxHash != txHash) {
// Here is a case when txHash has been changed for existing job:
// we should remove records from sentTxs with old txHash
this.removePendingTxByTxHash(oldTxHash);
}

// set history records in the sentTx mapping
// (the records with the old txHashes are still alive)
this.sentTxs.set(txHash, sentHistoryRecords);
}
}

// Mark job as completed: remove it from 'queuedTxs' and 'sentTxs' mappings
public async setQueuedTransactionsCompleted(job: SequencerJob, txHash: string) : Promise<boolean> {
return this.removePendingTxByJob(job) ||
this.removePendingTxByTxHash(txHash);
// Mark job as completed: do not remove associated records, just update txHash if needed
// An associated records from queuedTxs and sentTxs will be removed automatically on history sync
public async setQueuedTransactionsCompleted(job: SequencerJob, txHash: string) {
return this.setTxHashForQueuedTransactions(job, txHash);
}

// mark pending transaction as failed on the relayer level (we shouldn't have txHash here)
Expand Down Expand Up @@ -593,6 +588,32 @@ export class HistoryStorage {
return res;
}

private removePendingTxByCommit(commitment: bigint): boolean {
let res = false;

// remove records from the sentTxs by given commitment
this.sentTxs.forEach((records, txHash) => {
for (const aRec of records) {
if (aRec.commitment == commitment) {
this.sentTxs.delete(txHash);
res = true;
}
}
});

// remove queued txs with the same commitments
this.queuedTxs.forEach((records, jobHash) => {
for (const aRec of records) {
if (aRec.commitment == commitment) {
this.queuedTxs.delete(jobHash);
res = true;
}
}
});

return res;
}

// remove pending transactions with the txHash
private removeHistoryPendingRecordsByTxHash(txHash: string): boolean {
let deleted = false;
Expand Down Expand Up @@ -1052,9 +1073,9 @@ export class HistoryStorage {
throw new InternalError(`[HistoryStorage] Unknown transaction type ${details.txType}`)
}

if (!pending || (pending && details.isMined)) {
// if tx is in pending state - remove it only on success
this.removePendingTxByTxHash(details.txHash);
if (!pending) {
// remove non-pending tx from the aux queues
this.removePendingTxByCommit(BigInt(details.commitment));
}
} else if (txDetails.poolTxType == PoolTxType.DirectDepositBatch && txDetails.details instanceof DDBatchTxDetails) {
// transaction is DD batch on the pool
Expand Down
19 changes: 15 additions & 4 deletions src/services/proxy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,13 @@ export function compareProxyFee(fee1: ProxyFee, fee2: ProxyFee): boolean {
return r1 < r2;
}

function isProxyFeeEqual(fee1: ProxyFee, fee2: ProxyFee): boolean {
const r1 = evaluateRelayerFeeValue(fee1) + fee1.proverFee;
const r2 = evaluateRelayerFeeValue(fee2) + fee2.proverFee;

return r1 == r2;
}

export class ZkBobProxy extends ZkBobRelayer {
protected findOptimalProxyTs: number;

Expand Down Expand Up @@ -145,15 +152,19 @@ export class ZkBobProxy extends ZkBobRelayer {
compareProxyFee(proxy.fee, minFeeProxy.fee) ? proxy : minFeeProxy
);

if (this.curIdx != minFeeSeq.index) {
console.log(`ZkBobProxy: switching sequencer to ${minFeeSeq.index} (${this.url(minFeeSeq.index)}) due to best fee`);
this.curIdx = minFeeSeq.index
// Getting a random sequencer among the cheapest
const cheapSequencers = fees.filter(aFee => isProxyFeeEqual(aFee.fee, minFeeSeq.fee))
const selectedSequencer = cheapSequencers[Math.floor(Math.random() * cheapSequencers.length)];

if (this.curIdx != selectedSequencer.index) {
console.log(`ZkBobProxy: switching sequencer to ${selectedSequencer.index} (${this.url(selectedSequencer.index)}) due to the best fee`);
this.curIdx = selectedSequencer.index
this.findOptimalProxyTs = Date.now();
} else {
console.log(`ZkBobProxy: the current sequencer with index ${this.curIdx} is still the best choice`);
}

return minFeeSeq.fee;
return selectedSequencer.fee;
}

throw new InternalError('ZkBobProxy: cannot find live sequencer')
Expand Down

0 comments on commit a45d4e3

Please sign in to comment.