From 0aac7161df7e10914d9a4779ef5edcc7ee88c99c Mon Sep 17 00:00:00 2001 From: garrettmills Date: Sat, 10 Apr 2021 15:36:06 -0500 Subject: [PATCH] Prevent circular refresh from peer-triggered refresh --- .../controllers/api/Blockchain.controller.ts | 3 ++- src/app/units/Blockchain.ts | 21 +++++++++++-------- 2 files changed, 14 insertions(+), 10 deletions(-) diff --git a/src/app/http/controllers/api/Blockchain.controller.ts b/src/app/http/controllers/api/Blockchain.controller.ts index 3d3a8a6..f5a8f64 100644 --- a/src/app/http/controllers/api/Blockchain.controller.ts +++ b/src/app/http/controllers/api/Blockchain.controller.ts @@ -38,7 +38,8 @@ export class Blockchain extends Controller { * most recent submission, that has NOT been accepted yet. */ public async readBlockchainSubmission() { - return many((await this.blockchain.getSubmitChain()).map(x => { + const resultOfPeerRefresh = !!this.request.query.resultOfPeerRefresh + return many((await this.blockchain.getSubmitChain(resultOfPeerRefresh)).map(x => { // @ts-ignore delete x.firebaseID return x diff --git a/src/app/units/Blockchain.ts b/src/app/units/Blockchain.ts index d2b6aad..2b34835 100644 --- a/src/app/units/Blockchain.ts +++ b/src/app/units/Blockchain.ts @@ -172,10 +172,11 @@ export class Blockchain extends Unit { /** * From a peer, fetch the submission blockchain, if it is valid. * @param peer + * @param resultOfPeerRefresh */ - public async getPeerSubmit(peer: Peer): Promise { + public async getPeerSubmit(peer: Peer, resultOfPeerRefresh: boolean): Promise { try { - const result = await axios.get(`${peer.host}api/v1/chain/submit`) + const result = await axios.get(`${peer.host}api/v1/chain/submit${resultOfPeerRefresh ? '?resultOfPeerRefresh=true' : ''}`) const blocks: unknown = result.data?.data?.records if ( Array.isArray(blocks) && blocks.every(block => { const match = isBlockResourceItem(block) @@ -222,7 +223,7 @@ export class Blockchain extends Unit { } }) - this.refresh() + this.refresh(false) } catch (e) { this.logging.error(e) } @@ -273,7 +274,7 @@ export class Blockchain extends Unit { ).every(Boolean) } - public async refresh() { + public async refresh(resultOfPeerRefresh: boolean) { if ( this.isSubmitting ) { return } else { @@ -290,7 +291,7 @@ export class Blockchain extends Unit { for ( const peer of peers ) { console.log('[PEERS]', peers) - const blocks: Block[] | undefined = await this.getPeerSubmit(peer) + const blocks: Block[] | undefined = await this.getPeerSubmit(peer, resultOfPeerRefresh) console.log('[PEER BLOCKS]', blocks) if ( blocks && await this.validate(blocks) ) { const block = blocks.reverse()[0] @@ -351,7 +352,7 @@ export class Blockchain extends Unit { this.isSubmitting = false } - public async getSubmitChain(): Promise { + public async getSubmitChain(resultOfPeerRefresh: boolean): Promise { await this.firebase.trylock('block', 'Blockchain_getSubmitChain') const blocks = await this.read() const submit = await this.attemptSubmit() @@ -361,7 +362,9 @@ export class Blockchain extends Unit { } await this.firebase.unlock('block') - // this.refresh() + if ( !resultOfPeerRefresh ) { + this.refresh(resultOfPeerRefresh) + } return blocks } @@ -403,7 +406,7 @@ export class Blockchain extends Unit { } this.pendingTransactions.push(...txes) - this.refresh() + this.refresh(false) /*const lastBlock = await this.getLastBlock() @@ -436,7 +439,7 @@ export class Blockchain extends Unit { } this.pendingTransactions.push(...exposures) - this.refresh() + this.refresh(false) /*const lastBlock = await this.getLastBlock()