Skip to content

Commit

Permalink
Remove canExecute return value from _updateExchangeRatesBeforeExecuti…
Browse files Browse the repository at this point in the history
…ngHubChainLeaves
  • Loading branch information
nicholaspai committed Dec 6, 2024
1 parent 7f8b359 commit 9fb8403
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 27 deletions.
15 changes: 3 additions & 12 deletions src/dataworker/Dataworker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1472,12 +1472,11 @@ export class Dataworker {
const mainnetLeaves = unexecutedLeaves.filter((leaf) => leaf.chainId === hubPoolChainId);
if (mainnetLeaves.length > 0) {
assert(mainnetLeaves.length === 1, "There should only be one Ethereum PoolRebalanceLeaf");
const updateHubChainExchangeRatesResult = await this._updateExchangeRatesBeforeExecutingHubChainLeaves(
updatedL1Tokens = await this._updateExchangeRatesBeforeExecutingHubChainLeaves(
balanceAllocator,
mainnetLeaves[0],
submitExecution
);
updatedL1Tokens = updateHubChainExchangeRatesResult.syncedL1Tokens;
leafCount += await this._executePoolRebalanceLeaves(
spokePoolClients,
mainnetLeaves,
Expand Down Expand Up @@ -1725,11 +1724,7 @@ export class Dataworker {
balanceAllocator: BalanceAllocator,
poolRebalanceLeaf: Pick<PoolRebalanceLeaf, "netSendAmounts" | "l1Tokens">,
submitExecution: boolean
): Promise<{
canExecute: boolean;
syncedL1Tokens: Set<string>;
}> {
let canExecute = true;
): Promise<Set<string>> {
const hubPool = this.clients.hubPoolClient.hubPool;
const chainId = this.clients.hubPoolClient.chainId;

Expand Down Expand Up @@ -1763,7 +1758,6 @@ export class Dataworker {
// If updated liquid reserves are not enough to cover the payment, then send an error log that
// we're short on funds. Otherwise, enqueue a sync() call and then update the availableLiquidReserves.
if (updatedLiquidReserves.lt(netSendAmounts[idx])) {
canExecute = false;
this.logger.error({
at: "Dataworker#_updateExchangeRatesBeforeExecutingHubChainLeaves",
message: `Not enough funds to execute Ethereum pool rebalance leaf on HubPool for token: ${tokenSymbol}`,
Expand Down Expand Up @@ -1796,10 +1790,7 @@ export class Dataworker {
}
}
});
return {
canExecute,
syncedL1Tokens,
};
return syncedL1Tokens;
}

async _updateExchangeRatesBeforeExecutingNonHubChainLeaves(
Expand Down
26 changes: 11 additions & 15 deletions test/Dataworker.executePoolRebalances.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,10 +101,9 @@ describe("Dataworker: Execute pool rebalances", async function () {
await fillV3(spokePool_2, depositor, deposit, destinationChainId);
await updateAllClients();

const balanceAllocator = getNewBalanceAllocator();

// Executing leaves before there is a bundle should do nothing:
let leafCount = await dataworkerInstance.executePoolRebalanceLeaves(spokePoolClients, balanceAllocator);
let leafCount = await dataworkerInstance.executePoolRebalanceLeaves(spokePoolClients, getNewBalanceAllocator());
expect(leafCount).to.equal(0);
expect(lastSpyLogIncludes(spy, "No pending proposal")).to.be.true;

Expand All @@ -116,14 +115,15 @@ describe("Dataworker: Execute pool rebalances", async function () {

// Executing leaves before bundle challenge period has passed should do nothing:
await updateAllClients();
leafCount = await dataworkerInstance.executePoolRebalanceLeaves(spokePoolClients, balanceAllocator);
leafCount = await dataworkerInstance.executePoolRebalanceLeaves(spokePoolClients, getNewBalanceAllocator());
// console.log(spy.getCall(-1))
expect(leafCount).to.equal(0);
expect(lastSpyLogIncludes(spy, "Challenge period not passed")).to.be.true;

// Advance time and execute leaves:
await hubPool.setCurrentTime(Number(await hubPool.getCurrentTime()) + Number(await hubPool.liveness()) + 1);
await updateAllClients();
leafCount = await dataworkerInstance.executePoolRebalanceLeaves(spokePoolClients, balanceAllocator);
leafCount = await dataworkerInstance.executePoolRebalanceLeaves(spokePoolClients, getNewBalanceAllocator());
expect(leafCount).to.equal(2);

// Should be 4 transactions: 1 for the to chain, 1 for the from chain, 1 for the extra ETH sent to cover
Expand All @@ -134,7 +134,7 @@ describe("Dataworker: Execute pool rebalances", async function () {

// If we attempt execution again, the hub pool client should show them as already executed.
await updateAllClients();
leafCount = await dataworkerInstance.executePoolRebalanceLeaves(spokePoolClients, balanceAllocator);
leafCount = await dataworkerInstance.executePoolRebalanceLeaves(spokePoolClients, getNewBalanceAllocator());
expect(leafCount).to.equal(0);

// TEST 3:
Expand All @@ -148,7 +148,7 @@ describe("Dataworker: Execute pool rebalances", async function () {
// Advance time and execute leaves:
await hubPool.setCurrentTime(Number(await hubPool.getCurrentTime()) + Number(await hubPool.liveness()) + 1);
await updateAllClients();
leafCount = await dataworkerInstance.executePoolRebalanceLeaves(spokePoolClients, balanceAllocator);
leafCount = await dataworkerInstance.executePoolRebalanceLeaves(spokePoolClients, getNewBalanceAllocator());
expect(leafCount).to.equal(0);
expect(multiCallerClient.transactionCount()).to.equal(0);
});
Expand Down Expand Up @@ -226,13 +226,12 @@ describe("Dataworker: Execute pool rebalances", async function () {
it("ignores negative net send amounts", async function () {
const liquidReserves = toBNWei("1");
mockHubPoolClient.setLpTokenInfo(l1Token_1.address, 0, liquidReserves);
const { syncedL1Tokens, canExecute } =
const syncedL1Tokens =
await dataworkerInstance._updateExchangeRatesBeforeExecutingHubChainLeaves(
balanceAllocator,
{ netSendAmounts: [toBNWei(-1)], l1Tokens: [l1Token_1.address] },
true
);
expect(canExecute).to.be.true;
expect(syncedL1Tokens.size).to.equal(0);
expect(multiCallerClient.transactionCount()).to.equal(0);
});
Expand All @@ -241,13 +240,12 @@ describe("Dataworker: Execute pool rebalances", async function () {
const netSendAmount = toBNWei("1");
mockHubPoolClient.setLpTokenInfo(l1Token_1.address, 0, currentReserves);

const { syncedL1Tokens, canExecute } =
const syncedL1Tokens =
await dataworkerInstance._updateExchangeRatesBeforeExecutingHubChainLeaves(
balanceAllocator,
{ netSendAmounts: [netSendAmount], l1Tokens: [l1Token_1.address] },
true
);
expect(canExecute).to.be.true;
expect(syncedL1Tokens.size).to.equal(0);
expect(multiCallerClient.transactionCount()).to.equal(0);
expect(lastSpyLogIncludes(spy, "current liquid reserves > netSendAmount")).to.be.true;
Expand All @@ -259,28 +257,26 @@ describe("Dataworker: Execute pool rebalances", async function () {
mockHubPoolClient.setLpTokenInfo(l1Token_1.address, 0, liquidReserves);
balanceAllocator.testSetBalance(1, l1Token_1.address, hubPool.address, postUpdateLiquidReserves);

const result = await dataworkerInstance._updateExchangeRatesBeforeExecutingHubChainLeaves(
const syncedL1Tokens = await dataworkerInstance._updateExchangeRatesBeforeExecutingHubChainLeaves(
balanceAllocator,
{ netSendAmounts: [netSendAmount], l1Tokens: [l1Token_1.address] },
true
);
expect(result.canExecute).to.equal(false);
expect(lastSpyLogLevel(spy)).to.equal("error");
expect(lastSpyLogIncludes(spy, "Not enough funds to execute Ethereum pool rebalance leaf")).to.be.true;
expect(result.syncedL1Tokens.size).to.equal(0);
expect(syncedL1Tokens.size).to.equal(0);
});
it("submits update if updated liquid reserves cover execution of pool leaf", async function () {
const netSendAmount = toBNWei("1");
const updatedLiquidReserves = netSendAmount.add(1);
balanceAllocator.testSetBalance(1, l1Token_1.address, hubPool.address, updatedLiquidReserves);

const { syncedL1Tokens, canExecute } =
const syncedL1Tokens =
await dataworkerInstance._updateExchangeRatesBeforeExecutingHubChainLeaves(
balanceAllocator,
{ netSendAmounts: [netSendAmount], l1Tokens: [l1Token_1.address] },
true
);
expect(canExecute).to.be.true;
expect(syncedL1Tokens.size).to.equal(1);
expect(syncedL1Tokens.has(l1Token_1.address)).to.be.true;
expect(multiCallerClient.transactionCount()).to.equal(1);
Expand Down

0 comments on commit 9fb8403

Please sign in to comment.