Lovely band-aids

working-state
Garrett Mills 3 years ago
parent 313fdb65e9
commit 1bba0a18ec
Signed by: garrettmills
GPG Key ID: D2BF5FBA8298F246

@ -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: {

@ -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'
)
}

@ -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 (<BlockResource>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

Loading…
Cancel
Save