Skip to content

Commit

Permalink
Merge pull request #47 from LN-Zap/refactor
Browse files Browse the repository at this point in the history
Add maxHeightDelta setting to exclude stale data
  • Loading branch information
mrfelton authored Feb 19, 2024
2 parents f771823 + 1dcaf9f commit 654ef82
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 14 deletions.
21 changes: 11 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,16 +68,17 @@ Here are the available configuration options:

### Application settings

| Config Key | Description | Default Value | Environment Variable |
| ------------------------ | --------------------------------------------------------------------------------------------------------- | ----------------------- | -------------------- |
| `server.port` | The port on which the server runs | `3000` | `PORT` |
| `server.baseUrl` | The base url port on which the server is accessible | `http://localhost:3000` | `BASE_URL` |
| `settings.logLevel` | The log level to use for the application | `debug` | `LOGLEVEL` |
| `settings.timeout` | Timeout to use when fetching data (ms) | `5000` | `TIMEOUT` |
| `settings.feeMultiplier` | The multiplier to apply to the fee estimates | `1` | `FEE_MULTIPLIER` |
| `settings.feeMinimum` | The minimum fee (sat/vB) to use for fee estimates if we could not determine from a configured data source | `2` | `FEE_MINIMUM` |
| `cache.stdTTL` | The standard time to live in seconds for every generated cache element | `15` | `CACHE_STDTTL` |
| `cache.checkperiod` | The period in seconds, used for the automatic delete check interval | `20` | `CACHE_CHECKPERIOD` |
| Config Key | Description | Default Value | Environment Variable |
| ------------------------- | --------------------------------------------------------------------------------------------------------- | ----------------------- | -------------------- |
| `server.port` | The port on which the server runs | `3000` | `PORT` |
| `server.baseUrl` | The base url port on which the server is accessible | `http://localhost:3000` | `BASE_URL` |
| `settings.logLevel` | The log level to use for the application | `debug` | `LOGLEVEL` |
| `settings.timeout` | Timeout to use when fetching data (ms) | `5000` | `TIMEOUT` |
| `settings.feeMultiplier` | The multiplier to apply to the fee estimates | `1` | `FEE_MULTIPLIER` |
| `settings.feeMinimum` | The minimum fee (sat/vB) to use for fee estimates if we could not determine from a configured data source | `2` | `FEE_MINIMUM` |
| `settings.maxHeightDelta` | The maximum acceptable difference between the block heights of the most current fee estimates. | `3` | `MAX_HEIGHT_DELTA` |
| `cache.stdTTL` | The standard time to live in seconds for every generated cache element | `15` | `CACHE_STDTTL` |
| `cache.checkperiod` | The period in seconds, used for the automatic delete check interval | `20` | `CACHE_CHECKPERIOD` |

### Mempool settings

Expand Down
3 changes: 2 additions & 1 deletion config/custom-environment-variables.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
"loglevel": "LOGLEVEL",
"timeout": "TIMEOUT",
"feeMultiplier": "FEE_MULTIPLIER",
"feeMinimum": "FEE_MINIMUM"
"feeMinimum": "FEE_MINIMUM",
"maxHeightDelta": "MAX_HEIGHT_DELTA"
},
"mempool": {
"baseUrl": "MEMPOOL_BASE_URL",
Expand Down
3 changes: 2 additions & 1 deletion config/default.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
"loglevel": "debug",
"timeout": 5000,
"feeMultiplier": 1,
"feeMinimum": 2
"feeMinimum": 2,
"maxHeightDelta": 3
},
"mempool": {
"baseUrl": "https://mempool.space",
Expand Down
29 changes: 28 additions & 1 deletion src/lib/DataProviderManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,19 @@ const log = logger(LOGLEVEL);
export class DataProviderManager {
private providers: Provider[] = [];
private cache: NodeCache;
private maxHeightDelta: number;
private feeMultiplier: number;
private feeMinimum: number;
private cacheKey: string = "data";

constructor(
cacheConfig: CacheConfig,
maxHeightDelta: number = 1,
feeMultiplier: number = 1,
feeMinimum: number = 1,
) {
this.cache = new NodeCache(cacheConfig);
this.maxHeightDelta = maxHeightDelta;
this.feeMultiplier = feeMultiplier;
this.feeMinimum = feeMinimum;
}
Expand All @@ -43,7 +46,7 @@ export class DataProviderManager {
return data;
}

const dataPoints = await this.getSortedDataPoints();
const dataPoints = await this.getRelevantDataPoints();
const blockHeight = dataPoints[0].blockHeight;
const blockHash = dataPoints[0].blockHash;
const feeEstimates = this.mergeFeeEstimates(dataPoints);
Expand Down Expand Up @@ -120,6 +123,30 @@ export class DataProviderManager {
return dataPoints;
}

/**
* Gets relevant data points based on the height difference threshold.
*
* @returns A promise that resolves to an array of relevant data points.
*/
private async getRelevantDataPoints(): Promise<DataPoint[]> {
// Get sorted data points from all providers
const dataPoints = await this.getSortedDataPoints();

// Filter out providers that don't meet the relevancy threshold criteria
return dataPoints.filter((dp) => {
const isRelevant =
dataPoints[0].blockHeight - dp.blockHeight <= this.maxHeightDelta;

if (!isRelevant) {
console.warn({
msg: `Data point from block ${dp.blockHeight} was filtered out due to relevancy threshold.`,
});
}

return isRelevant;
});
}

/**
* Merges fee estimates from multiple data points.
*
Expand Down
2 changes: 2 additions & 0 deletions src/server.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ const LOGLEVEL = config.get<string>("settings.loglevel");
const TIMEOUT = config.get<number>("settings.timeout");
const FEE_MULTIPLIER = config.get<number>("settings.feeMultiplier");
const FEE_MINIMUM = config.get<number>("settings.feeMinimum");
const MAX_HEIGHT_DELTA = config.get<number>("settings.maxHeightDelta");
const CACHE_STDTTL = config.get<number>("cache.stdTTL");
const CACHE_CHECKPERIOD = config.get<number>("cache.checkperiod");

Expand All @@ -49,6 +50,7 @@ const service = new DataProviderManager(
stdTTL: CACHE_STDTTL,
checkperiod: CACHE_CHECKPERIOD,
},
MAX_HEIGHT_DELTA,
FEE_MULTIPLIER,
FEE_MINIMUM,
);
Expand Down
1 change: 0 additions & 1 deletion test/DataProviderManager.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,5 @@ test("should merge fee estimates from multiple providers correctly", async () =>
"2": 20000,
"3": 5000,
"5": 3000,
"10": 1000,
});
});

0 comments on commit 654ef82

Please sign in to comment.