From 556de4ef1432f8f9ba578cde68bc829c87f74dc3 Mon Sep 17 00:00:00 2001 From: Evgen Date: Wed, 23 Oct 2024 16:37:00 +0300 Subject: [PATCH] Selecting random sequencer in case of equal fees --- package.json | 2 +- src/services/proxy.ts | 19 +++++++++++++++---- 2 files changed, 16 insertions(+), 5 deletions(-) diff --git a/package.json b/package.json index 16e1877..7f56362 100644 --- a/package.json +++ b/package.json @@ -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 ", diff --git a/src/services/proxy.ts b/src/services/proxy.ts index 5fa5a52..b812836 100644 --- a/src/services/proxy.ts +++ b/src/services/proxy.ts @@ -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; @@ -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')