2021-04-10 14:01:13 +00:00
|
|
|
import { FirebaseUnit } from "../FirebaseUnit"
|
|
|
|
import { Singleton, Inject } from "@extollo/di"
|
|
|
|
import { Unit, Logging } from "@extollo/lib"
|
|
|
|
import { Blockchain } from "../Blockchain"
|
|
|
|
import { ExposureResource, ExposureResourceItem } from "../../rtdb/ExposureResource"
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Exposure Unit
|
|
|
|
* ---------------------------------------
|
|
|
|
* This unit listens for exposure notifications created on the realtime database.
|
|
|
|
* When new ones come through, it validates them, and pushes them onto this
|
|
|
|
* server's blockchain.
|
|
|
|
*/
|
|
|
|
@Singleton()
|
|
|
|
export class Exposure extends Unit {
|
|
|
|
/** True if currently processing transactions. */
|
|
|
|
private processing: boolean = false
|
|
|
|
|
|
|
|
@Inject()
|
|
|
|
protected readonly firebase!: FirebaseUnit
|
|
|
|
|
|
|
|
@Inject()
|
|
|
|
protected readonly blockchain!: Blockchain
|
|
|
|
|
|
|
|
@Inject()
|
|
|
|
protected readonly logging!: Logging
|
|
|
|
|
|
|
|
/** Claim the right to process transactions. Returns true if the right was granted. */
|
|
|
|
claim() {
|
|
|
|
if ( !this.processing ) {
|
|
|
|
this.processing = true
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Release the right to claim transactions. */
|
|
|
|
release() {
|
|
|
|
this.processing = false
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Subscribe to the transactions reference and wait for new transactions to be added.
|
|
|
|
*/
|
|
|
|
public async up() {
|
2021-04-11 01:35:29 +00:00
|
|
|
this.firebase.ref('exposure').on('child_added', (snapshot) => {
|
|
|
|
/*this.logging.debug('Received child_added event for exposures reference.')
|
2021-04-10 14:01:13 +00:00
|
|
|
if ( !this.claim() ) return
|
2021-04-10 21:35:42 +00:00
|
|
|
// await this.firebase.trylock('block', 'Exposure_child_added')
|
2021-04-10 14:01:13 +00:00
|
|
|
|
|
|
|
const exposure: ExposureResourceItem = snapshot.val()
|
|
|
|
|
|
|
|
// Push the exposure transactions onto the chain
|
|
|
|
await this.blockchain.submitExposures(exposure)
|
|
|
|
|
|
|
|
if ( snapshot.key )
|
|
|
|
await (<ExposureResource> this.make(ExposureResource)).ref().child(snapshot.key).remove()
|
|
|
|
|
|
|
|
this.release()
|
2021-04-11 01:35:29 +00:00
|
|
|
// await this.firebase.unlock('block')*/
|
|
|
|
|
|
|
|
this.blockchain.submitExposures(snapshot.val())
|
2021-04-10 14:01:13 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Release listeners and resources before shutdown.
|
|
|
|
*/
|
|
|
|
public async down() {
|
|
|
|
// Release all subscriptions before shutdown
|
2021-04-11 01:35:29 +00:00
|
|
|
this.firebase.ref('exposure').off()
|
2021-04-10 14:01:13 +00:00
|
|
|
}
|
|
|
|
}
|