From 1bba0a18ec688df83a773f7635a5ea039704c3b2 Mon Sep 17 00:00:00 2001 From: garrettmills Date: Sat, 10 Apr 2021 14:18:55 -0500 Subject: [PATCH] Lovely band-aids --- src/app/configs/server.config.ts | 2 +- src/app/rtdb/BlockResource.ts | 6 +++--- src/app/units/Blockchain.ts | 26 +++++++++++++++++++++++--- 3 files changed, 27 insertions(+), 7 deletions(-) diff --git a/src/app/configs/server.config.ts b/src/app/configs/server.config.ts index 7be6fc8..704fc13 100644 --- a/src/app/configs/server.config.ts +++ b/src/app/configs/server.config.ts @@ -7,7 +7,7 @@ export default { base_url: env('BASE_URL', 'http://localhost:8000/'), peers: [ - // 'http://127.0.0.1:8100/', + 'http://127.0.0.1:8100/', ], session: { diff --git a/src/app/rtdb/BlockResource.ts b/src/app/rtdb/BlockResource.ts index d599835..fd1ad3e 100644 --- a/src/app/rtdb/BlockResource.ts +++ b/src/app/rtdb/BlockResource.ts @@ -68,11 +68,11 @@ export interface BlockResourceItem extends FirebaseResourceItem { export function isBlockResourceItem(what: any): what is BlockResourceItem { return ( typeof what?.uuid === 'string' - && Array.isArray(what?.transactions) + && (!what?.transactions || Array.isArray(what?.transactions)) && typeof what?.lastBlockHash === 'string' && typeof what?.lastBlockUUID === 'string' - && typeof what?.proof === 'string' - && typeof what?.timestamp === 'number' + && typeof what?.proof + && typeof what?.timestamp && typeof what?.waitTime === 'number' ) } diff --git a/src/app/units/Blockchain.ts b/src/app/units/Blockchain.ts index 46efcb0..a9eee85 100644 --- a/src/app/units/Blockchain.ts +++ b/src/app/units/Blockchain.ts @@ -177,10 +177,15 @@ export class Blockchain extends Unit { try { const result = await axios.get(`${peer.host}api/v1/chain/submit`) const blocks: unknown = result.data?.data?.records - if ( Array.isArray(blocks) && blocks.every(isBlockResourceItem) ) { + if ( Array.isArray(blocks) && blocks.every(block => { + const match = isBlockResourceItem(block) + console.log(match, block) + return match + }) ) { return blocks.map(x => new Block(x)) } } catch (e) { + this.logging.error(e) return undefined } } @@ -214,6 +219,7 @@ export class Blockchain extends Unit { }, { headers: { [header]: await this.getPeerToken(), + 'content-type': 'application/json', } }) @@ -283,7 +289,9 @@ export class Blockchain extends Unit { const time_x_peer: {[key: string]: Peer | true} = {} for ( const peer of peers ) { + console.log('[PEERS]', peers) const blocks: Block[] | undefined = await this.getPeerSubmit(peer) + console.log('[PEER BLOCKS]', blocks) if ( blocks && await this.validate(blocks) ) { const block = blocks.reverse()[0] if ( !block || block.seqID === validSeqID || !block.seqID ) continue @@ -296,8 +304,10 @@ export class Blockchain extends Unit { block.waitTime += penalty time_x_block[block.waitTime] = block - time_x_blocks[block.waitTime] = blocks + time_x_blocks[block.waitTime] = blocks.reverse() time_x_peer[block.waitTime] = peer + } else { + console.log('VALIDATION FAIL') } } @@ -320,7 +330,17 @@ export class Blockchain extends Unit { await (this.app().make(BlockResource)).push(block) } else { await this.firebase.ref('block').transaction((_) => { - return (time_x_blocks[min] || []).map(x => x.toItem()) + return (time_x_blocks[min] || []).map(x => { + const item = x.toItem() + // @ts-ignore + delete item.firebaseID + + if ( !item.transactions ) { + item.transactions = [] + } + + return item + }) }) this.pendingSubmit = undefined