feat: calculate call interval using average block time
This commit is contained in:
parent
af735816e9
commit
018c724529
|
@ -31,7 +31,7 @@ export default class Subspace {
|
||||||
|
|
||||||
this.options = {};
|
this.options = {};
|
||||||
this.options.refreshLastNBlocks = options.refreshLastNBlocks ?? 12;
|
this.options.refreshLastNBlocks = options.refreshLastNBlocks ?? 12;
|
||||||
this.options.callInterval = options.callInterval ?? 0;
|
this.options.callInterval = options.callInterval;
|
||||||
this.options.dbFilename = options.dbFilename ?? "subspace.db";
|
this.options.dbFilename = options.dbFilename ?? "subspace.db";
|
||||||
this.options.disableDatabase = options.disableDatabase;
|
this.options.disableDatabase = options.disableDatabase;
|
||||||
|
|
||||||
|
@ -69,7 +69,11 @@ export default class Subspace {
|
||||||
if (this.isWebsocketProvider) {
|
if (this.isWebsocketProvider) {
|
||||||
this._initNewBlocksSubscription();
|
this._initNewBlocksSubscription();
|
||||||
} else {
|
} else {
|
||||||
this.options.callInterval = this.options.callInterval || 1000;
|
|
||||||
|
if(!this.options.callInterval){
|
||||||
|
this.options.callInterval = Math.max(this._calcAverage(), 1000);
|
||||||
|
}
|
||||||
|
|
||||||
this._initCallInterval();
|
this._initCallInterval();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -137,6 +141,15 @@ export default class Subspace {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
_calcAverage(){
|
||||||
|
const times = [];
|
||||||
|
for (let i = 1; i < this.latest10Blocks.length; i++) {
|
||||||
|
let time = this.latest10Blocks[i].timestamp - this.latest10Blocks[i - 1].timestamp;
|
||||||
|
times.push(time);
|
||||||
|
}
|
||||||
|
return times.length ? Math.round(times.reduce((a, b) => a + b) / times.length) * 1000 : 0;
|
||||||
|
}
|
||||||
|
|
||||||
_initNewBlocksSubscription() {
|
_initNewBlocksSubscription() {
|
||||||
if(this.options.callInterval !== 0) return;
|
if(this.options.callInterval !== 0) return;
|
||||||
|
|
||||||
|
@ -268,7 +281,7 @@ export default class Subspace {
|
||||||
}
|
}
|
||||||
|
|
||||||
trackBlock() {
|
trackBlock() {
|
||||||
return this._getDistinctObservableFromPromise("gasPrice", () => this.web3.getBlock("latest"), block => {
|
return this._getDistinctObservableFromPromise("block", () => this.web3.getBlock("latest"), block => {
|
||||||
if (this.latest10Blocks[this.latest10Blocks.length - 1].number === block.number) return;
|
if (this.latest10Blocks[this.latest10Blocks.length - 1].number === block.number) return;
|
||||||
this.latest10Blocks.push(block);
|
this.latest10Blocks.push(block);
|
||||||
if (this.latest10Blocks.length > 10) {
|
if (this.latest10Blocks.length > 10) {
|
||||||
|
@ -283,17 +296,10 @@ export default class Subspace {
|
||||||
|
|
||||||
trackAverageBlocktime() {
|
trackAverageBlocktime() {
|
||||||
return this._getObservable("avgBlockTime", () => {
|
return this._getObservable("avgBlockTime", () => {
|
||||||
const calcAverage = () => {
|
|
||||||
const times = [];
|
|
||||||
for (let i = 1; i < this.latest10Blocks.length; i++) {
|
|
||||||
let time = this.latest10Blocks[i].timestamp - this.latest10Blocks[i - 1].timestamp;
|
|
||||||
times.push(time);
|
|
||||||
}
|
|
||||||
return times.length ? Math.round(times.reduce((a, b) => a + b) / times.length) * 1000 : 0;
|
|
||||||
};
|
|
||||||
|
|
||||||
return this.trackBlock().pipe(
|
return this.trackBlock().pipe(
|
||||||
map(() => calcAverage()),
|
map(() => this._calcAverage()),
|
||||||
distinctUntilChanged((a, b) => equal(a, b))
|
distinctUntilChanged((a, b) => equal(a, b))
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
|
Loading…
Reference in New Issue