Added check api to retrieve blocks for certain timeframe

This commit is contained in:
QiTao Weng 2021-04-10 16:17:01 +00:00
parent 71b4fefef7
commit fbc3711560
4 changed files with 784 additions and 825 deletions

File diff suppressed because it is too large Load Diff

View File

@ -3,6 +3,7 @@ import * as fs from "fs"
export default {
name: env('APP_NAME', 'Extollo'),
defaultTime: env('DEFAULT_TIME', 1.21e+9),
gpg: {
key: {

View File

@ -1,9 +1,11 @@
import {Controller} from "@extollo/lib"
import {Controller, Config} from "@extollo/lib"
import {Injectable, Inject} from "@extollo/di"
import {TransactionResource, TransactionResourceItem} from "../../../rtdb/TransactionResource"
import {many, one} from "@extollo/util"
import {Iterable, many, one} from "@extollo/util"
import {Block, Blockchain as BlockchainService} from "../../../units/Blockchain"
import {ExposureResource, ExposureResourceItem} from "../../../rtdb/ExposureResource";
import {FirebaseUnit} from "../../../units/FirebaseUnit"
import { BlockResource, BlockResourceItem } from "../../../rtdb/BlockResource"
/**
* Blockchain Controller
@ -15,6 +17,11 @@ export class Blockchain extends Controller {
@Inject()
protected readonly blockchain!: BlockchainService
@Inject()
protected readonly config!: Config
@Inject()
protected readonly firebase!: FirebaseUnit
/**
* Read the version of the blockchain held by this host, as it currently exists.
*/
@ -68,4 +75,18 @@ export class Blockchain extends Controller {
await (<ExposureResource> this.make(ExposureResource)).push(item)
return one(item)
}
public async check() {
let minTime = this.request.input('minTime')
if (!minTime) {
minTime = (new Date).getTime() - this.config.get('app.defaultTime')
}
const snapshot = await (<BlockResource> this.make(BlockResource)).ref()
.orderByChild('timestamp')
.startAt(minTime)
.once('value')
let blocks = (Object.values(snapshot.val()) as BlockResourceItem[]).filter((item: BlockResourceItem) => item.seqID !== 0)
return many(blocks)
}
}

View File

@ -13,4 +13,8 @@ Route.group('/api/v1', () => {
.pre('DebugOnly')
Route.get('/chain', 'api:Blockchain.readBlockchain')
Route.get('/check', 'api:Blockchain.check')
.pre('api:FirebaseUserOnly')
Route.get('/check-debug', 'api:Blockchain.check')
.pre('DebugOnly')
})