From 57eabb9450a96e6f5beacf05d05d8c1db222f160 Mon Sep 17 00:00:00 2001 From: Alexandre ABRIOUX Date: Mon, 1 Nov 2021 13:28:56 +0100 Subject: [PATCH] feat: add rebalance interval --- src/block.ts | 15 +++++++++++---- src/config.ts | 1 + src/pos/client.ts | 4 +++- 3 files changed, 15 insertions(+), 5 deletions(-) diff --git a/src/block.ts b/src/block.ts index afdc4c1..2defeb8 100644 --- a/src/block.ts +++ b/src/block.ts @@ -14,7 +14,11 @@ import humanizeDuration from "humanize-duration"; import pTimeout from "p-timeout"; import { formatCTSI } from "./util"; -import { CONFIRMATIONS, CONFIRMATION_TIMEOUT } from "./config"; +import { + CONFIRMATIONS, + CONFIRMATION_TIMEOUT, + REBALANCE_INTERVAL, +} from "./config"; import { ProtocolClient } from "./pos"; import * as monitoring from "./monitoring"; import { constants } from "ethers"; @@ -23,8 +27,8 @@ const explorerUrl = "https://explorer.cartesi.io/staking"; export class BlockProducer { private address: string; - private client: ProtocolClient; + private lastRebalanceCall = 0; constructor(address: string, client: ProtocolClient) { this.address = address; @@ -185,7 +189,10 @@ export class BlockProducer { } async rebalance() { - // XXX: what should we do with the boolean return value? - await this.client.rebalance(); + const currentTimestamp = new Date().getTime(); + if (currentTimestamp < this.lastRebalanceCall + REBALANCE_INTERVAL) + return; + if (await this.client.rebalance()) + this.lastRebalanceCall = currentTimestamp; } } diff --git a/src/config.ts b/src/config.ts index ad1415b..c5abb3a 100644 --- a/src/config.ts +++ b/src/config.ts @@ -14,6 +14,7 @@ import { parseEther } from "@ethersproject/units"; export const TIMEOUT = 24 * 60 * 60 * 1000; export const RETRY_INTERVAL = 10000; export const POLLING_INTERVAL = 30000; +export const REBALANCE_INTERVAL = 0; export const CONFIRMATIONS = 1; export const CONFIRMATION_TIMEOUT = 10 * 60 * 1000; // 10 minutes export const GAS_LIMIT_MULTIPLIER = 160; diff --git a/src/pos/client.ts b/src/pos/client.ts index 2a3ab59..a51d1d4 100644 --- a/src/pos/client.ts +++ b/src/pos/client.ts @@ -300,8 +300,10 @@ export class PoolProtocolImpl extends AbstractProtocolClient { // increment rebalance counter monitoring.rebalance.inc(); + + return true; } - return true; + return false; } }