Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Selecting random sequencer in case of equal fees #194

Merged
merged 1 commit into from
Oct 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading