Use block cache when searching or opening a recent block. (#749)

* Use block cache when searching or opening a recent block.

fixes #715

* Fixed linting errors.
This commit is contained in:
softsimon 2021-08-29 04:55:46 +03:00 committed by GitHub
parent bdfcfc96a8
commit 8fdbfdc04c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 29 additions and 18 deletions

View File

@ -65,8 +65,20 @@ export class BlockComponent implements OnInit, OnDestroy {
map((indicators) => indicators['blocktxs-' + this.blockHash] !== undefined ? indicators['blocktxs-' + this.blockHash] : 0) map((indicators) => indicators['blocktxs-' + this.blockHash] !== undefined ? indicators['blocktxs-' + this.blockHash] : 0)
); );
this.subscription = this.route.paramMap this.blocksSubscription = this.stateService.blocks$
.pipe( .subscribe(([block]) => {
this.latestBlock = block;
this.latestBlocks.unshift(block);
this.latestBlocks = this.latestBlocks.slice(0, this.stateService.env.KEEP_BLOCKS_AMOUNT);
this.setNextAndPreviousBlockLink();
if (block.id === this.blockHash) {
this.block = block;
this.fees = block.reward / 100000000 - this.blockSubsidy;
}
});
this.subscription = this.route.paramMap.pipe(
switchMap((params: ParamMap) => { switchMap((params: ParamMap) => {
const blockHash: string = params.get('id') || ''; const blockHash: string = params.get('id') || '';
this.block = undefined; this.block = undefined;
@ -94,7 +106,12 @@ export class BlockComponent implements OnInit, OnDestroy {
} else { } else {
this.isLoadingBlock = true; this.isLoadingBlock = true;
let blockInCache: Block;
if (isBlockHeight) { if (isBlockHeight) {
blockInCache = this.latestBlocks.find((block) => block.height === parseInt(blockHash, 10));
if (blockInCache) {
return of(blockInCache);
}
return this.electrsApiService.getBlockHashFromHeight$(parseInt(blockHash, 10)) return this.electrsApiService.getBlockHashFromHeight$(parseInt(blockHash, 10))
.pipe( .pipe(
switchMap((hash) => { switchMap((hash) => {
@ -107,12 +124,11 @@ export class BlockComponent implements OnInit, OnDestroy {
); );
} }
this.stateService.blocks$.subscribe(([block]) => { blockInCache = this.latestBlocks.find((block) => block.id === this.blockHash);
if (block.id === blockHash) { if (blockInCache) {
this.block = block; return of(blockInCache);
} }
});
return this.electrsApiService.getBlock$(blockHash); return this.electrsApiService.getBlock$(blockHash);
} }
}), }),
@ -159,14 +175,6 @@ export class BlockComponent implements OnInit, OnDestroy {
this.isLoadingBlock = false; this.isLoadingBlock = false;
}); });
this.blocksSubscription = this.stateService.blocks$
.subscribe(([block]) => {
this.latestBlock = block;
this.latestBlocks.unshift(block);
this.latestBlocks = this.latestBlocks.slice(0, this.stateService.env.KEEP_BLOCKS_AMOUNT);
this.setNextAndPreviousBlockLink();
});
this.networkChangedSubscription = this.stateService.networkChanged$ this.networkChangedSubscription = this.stateService.networkChanged$
.subscribe((network) => this.network = network); .subscribe((network) => this.network = network);
@ -269,12 +277,14 @@ export class BlockComponent implements OnInit, OnDestroy {
return; return;
} }
const block = this.latestBlocks.find((b) => b.height === this.nextBlockHeight - 2); const block = this.latestBlocks.find((b) => b.height === this.nextBlockHeight - 2);
this.router.navigate([(this.network && this.stateService.env.BASE_MODULE === 'mempool' ? '/' + this.network : '') + '/block/', block ? block.id : this.block.previousblockhash], { state: { data: { block, blockHeight: this.nextBlockHeight - 2 } } }); this.router.navigate([(this.network && this.stateService.env.BASE_MODULE === 'mempool' ? '/' + this.network : '') + '/block/',
block ? block.id : this.block.previousblockhash], { state: { data: { block, blockHeight: this.nextBlockHeight - 2 } } });
} }
navigateToNextBlock() { navigateToNextBlock() {
const block = this.latestBlocks.find((b) => b.height === this.nextBlockHeight); const block = this.latestBlocks.find((b) => b.height === this.nextBlockHeight);
this.router.navigate([(this.network && this.stateService.env.BASE_MODULE === 'mempool' ? '/' + this.network : '') + '/block/', block ? block.id : this.nextBlockHeight], { state: { data: { block, blockHeight: this.nextBlockHeight } } }); this.router.navigate([(this.network && this.stateService.env.BASE_MODULE === 'mempool' ? '/' + this.network : '') + '/block/',
block ? block.id : this.nextBlockHeight], { state: { data: { block, blockHeight: this.nextBlockHeight } } });
} }
setNextAndPreviousBlockLink(){ setNextAndPreviousBlockLink(){

View File

@ -12,6 +12,7 @@
"arrow-parens": false, "arrow-parens": false,
"arrow-return-shorthand": true, "arrow-return-shorthand": true,
"curly": true, "curly": true,
"no-bitwise": false,
"deprecation": { "deprecation": {
"severity": "warning" "severity": "warning"
}, },