Skip to content

Commit

Permalink
Selecting random sequencer in case of equal fees (#194)
Browse files Browse the repository at this point in the history
  • Loading branch information
EvgenKor authored Oct 25, 2024
1 parent eaac94d commit 7a0e5b5
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 5 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
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 7a0e5b5

Please sign in to comment.