Skip to content
This repository was archived by the owner on Jun 29, 2023. It is now read-only.

Commit ea02b74

Browse files
authored
Merge pull request #652 from thehubbleproject/bug/token-pool-zero-fee
Fix getHighestValueToken to work with zero fee txs
2 parents 8f556f1 + eef6bd5 commit ea02b74

File tree

3 files changed

+53
-4
lines changed

3 files changed

+53
-4
lines changed

test/fast/txPool.test.ts

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ import {
1313
PoolEmptyError,
1414
PoolFullError,
1515
TokenNotConfiguredError,
16-
TokenPoolEmpty
16+
TokenPoolEmpty,
17+
TokenPoolHighestFeeError
1718
} from "../../ts/exceptions";
1819
import { State } from "../../ts/state";
1920
import * as mcl from "../../ts/mcl";
@@ -130,6 +131,36 @@ describe("MultiTokenPool", () => {
130131
PoolEmptyError
131132
);
132133
});
134+
135+
it("fails if unable to determine highest value token", async function() {
136+
// This simulates a path which this func will fail
137+
// @ts-ignore
138+
pool.txCount = 1;
139+
140+
await assert.isRejected(
141+
pool.getHighestValueToken(),
142+
TokenPoolHighestFeeError
143+
);
144+
});
145+
146+
it("suceeds when summed tx fees are 0", async function() {
147+
const user1Token1 = 0;
148+
const s = State.new(-1, tokenID1BN, -1, -1);
149+
await state.create(user1Token1, s);
150+
await state.commit();
151+
152+
const token1Tx1 = txFactory(user1Token1, 0);
153+
await pool.push(token1Tx1);
154+
155+
const highToken1 = highTokenToNum(
156+
await pool.getHighestValueToken()
157+
);
158+
assert.deepEqual(highToken1, {
159+
tokenID: tokenID1,
160+
feeReceiverID: feeReceiver1,
161+
sumFees: 0
162+
});
163+
});
133164
});
134165

135166
describe("lifecycle", () => {

ts/client/txPool.ts

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@ import {
33
PoolEmptyError,
44
PoolFullError,
55
TokenNotConfiguredError,
6-
TokenPoolEmpty
6+
TokenPoolEmpty,
7+
TokenPoolHighestFeeError
78
} from "../exceptions";
89
import { sum } from "../utils";
910
import { FeeReceivers } from "./config";
@@ -133,7 +134,7 @@ export class MultiTokenPool<Item extends OffchainTx> {
133134
throw new PoolEmptyError();
134135
}
135136

136-
let highValue = BigNumber.from(0);
137+
let highValue = BigNumber.from(-1);
137138
let tokenID = BigNumber.from(-1);
138139
for (const tokenIDStr of Object.keys(this.tokenIDStrToQueue)) {
139140
const value = this.getQueueValue(tokenIDStr);
@@ -143,6 +144,11 @@ export class MultiTokenPool<Item extends OffchainTx> {
143144
}
144145
}
145146

147+
// This case should never be hit, but best to be explicit if it does
148+
if (highValue.lt(0) || tokenID.lt(0)) {
149+
throw new TokenPoolHighestFeeError();
150+
}
151+
146152
const feeReceiverID = this.tokenIDStrToFeeRecieverID[
147153
tokenID.toString()
148154
];
@@ -154,6 +160,11 @@ export class MultiTokenPool<Item extends OffchainTx> {
154160
}
155161

156162
private getQueueValue(tokenIDStr: string): BigNumber {
157-
return sum(this.tokenIDStrToQueue[tokenIDStr].map(tx => tx.fee));
163+
const tokenQueue = this.tokenIDStrToQueue[tokenIDStr];
164+
if (!tokenQueue.length) {
165+
return BigNumber.from(-1);
166+
}
167+
168+
return sum(tokenQueue.map(tx => tx.fee));
158169
}
159170
}

ts/exceptions.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -255,3 +255,10 @@ export class TokenNotConfiguredError extends Error {
255255
this.name = "TokenNotConfiguredError";
256256
}
257257
}
258+
259+
export class TokenPoolHighestFeeError extends Error {
260+
constructor() {
261+
super("unable to determine highest fee token for pool");
262+
this.name = "TokenPoolHighestFeeError";
263+
}
264+
}

0 commit comments

Comments
 (0)