Skip to content

Commit

Permalink
avoid slashings due to pow params updates
Browse files Browse the repository at this point in the history
add pow params to requests & ignore requests with invalid pow params
  • Loading branch information
pk910 committed Sep 16, 2024
1 parent 9b3aa52 commit 13dd469
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 6 deletions.
8 changes: 7 additions & 1 deletion faucet-client/src/pow/PoWMiner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,12 @@ export interface IPoWMinerVerification {
data: string;
}

export interface IPoWMinerVerificationResult {
shareId: string;
params: string;
isValid: boolean;
}

interface PoWMinerEvents {
'stats': (stats: IPoWMinerStats) => void;
}
Expand Down Expand Up @@ -480,7 +486,7 @@ export class PoWMiner extends TypedEmitter<PoWMinerEvents> {
});
}

private onWorkerVerifyResult(worker: IPoWMinerWorker, result: any) {
private onWorkerVerifyResult(worker: IPoWMinerWorker, result: IPoWMinerVerificationResult) {
this.options.session.submitVerifyResult(result);
}

Expand Down
16 changes: 12 additions & 4 deletions faucet-client/src/pow/PoWSession.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import { TypedEmitter } from 'tiny-typed-emitter';
import { PoWClient } from "./PoWClient";
import { IPoWMinerShare, IPoWMinerVerification, PoWMiner } from "./PoWMiner";
import { IPoWMinerShare, IPoWMinerVerification, IPoWMinerVerificationResult, PoWMiner } from "./PoWMiner";
import { FaucetTime } from '../common/FaucetTime';
import { FaucetSession } from '../common/FaucetSession';
import { getPoWParamsStr } from '../utils/PoWParamsHelper';
import { IFaucetConfig, IPoWModuleConfig } from '../common/FaucetConfig';

export interface IPoWSessionOptions {
session: FaucetSession;
Expand Down Expand Up @@ -32,7 +34,7 @@ export class PoWSession extends TypedEmitter<PoWSessionEvents> {
private balance: bigint;
private shareQueue: IPoWMinerShare[];
private shareQueueProcessing: boolean;
private verifyResultQueue: any[];
private verifyResultQueue: IPoWMinerVerificationResult[];

public constructor(options: IPoWSessionOptions) {
super();
Expand Down Expand Up @@ -106,6 +108,9 @@ export class PoWSession extends TypedEmitter<PoWSessionEvents> {

private _submitShare(share: IPoWMinerShare) {
this.options.client.sendRequest("foundShare", share).catch((err) => {
if(err.code === "INVALID_SHARE" && err.data && err.data.message === "Invalid share params")
return; // Ignore invalid params

this.options.showNotification("error", "Submission error: [" + err.code + "] " + err.message, true, 20 * 1000);
});
}
Expand All @@ -114,7 +119,7 @@ export class PoWSession extends TypedEmitter<PoWSessionEvents> {
this.miner.processVerification(verification);
}

public submitVerifyResult(result) {
public submitVerifyResult(result: IPoWMinerVerificationResult) {
if(this.options.client.isReady() && this.verifyResultQueue.length === 0)
this._submitVerifyResult(result);
else
Expand All @@ -125,8 +130,11 @@ export class PoWSession extends TypedEmitter<PoWSessionEvents> {
this.verifyResultQueue.forEach((result) => this._submitVerifyResult(result));
}

private _submitVerifyResult(result) {
private _submitVerifyResult(result: IPoWMinerVerificationResult) {
this.options.client.sendRequest("verifyResult", result).catch((err) => {
if(err.code === "INVALID_VERIFYRESULT" && err.data && err.data.message === "Invalid share params")
return; // Ignore invalid params

this.options.showNotification("error", "Verification error: [" + err.code + "] " + err.message, true, 20 * 1000);
});
}
Expand Down
2 changes: 1 addition & 1 deletion faucet-client/src/worker/PoWWorker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ export class PoWWorker {
private options: IPoWWorkerOptions;
private workerId: number;
private powParams: IPoWWorkerParams;
private powDifficulty: number;
private powPreImage: string;
private working = false;
private workNonce: number;
Expand Down Expand Up @@ -122,6 +121,7 @@ export class PoWWorker {
action: "verifyResult",
data: {
shareId: share.shareId,
params: this.powParams.pstr,
isValid: isValid
}
});
Expand Down
4 changes: 4 additions & 0 deletions src/modules/pow/PoWClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -222,9 +222,13 @@ export class PoWClient {

let verifyRes: {
shareId: string;
params?: string;
isValid: boolean;
} = message.data;

if(verifyRes.params && verifyRes.params !== this.module.getPoWParamsStr())
return this.sendErrorResponse("INVALID_VERIFYRESULT", "Invalid share params", message);

let verifyValid = PoWShareVerification.processVerificationResult(verifyRes.shareId, this.getFaucetSession().getSessionId(), verifyRes.isValid);
let verifyReward = BigInt(this.module.getModuleConfig().powShareReward) * BigInt(this.module.getModuleConfig().verifyMinerRewardPerc * 100) / 10000n;
if(verifyValid && verifyReward > 0n) {
Expand Down

0 comments on commit 13dd469

Please sign in to comment.