From 5f3a1940e578ce61cce783591fed8ce0ba7cfe44 Mon Sep 17 00:00:00 2001 From: garrettmills Date: Sat, 10 Apr 2021 07:36:14 -0500 Subject: [PATCH] Add /api/v1/chain endpoint to read blockchain --- .../controllers/api/Blockchain.controller.ts | 19 +++++++++++++++++-- src/app/http/routes/app.routes.ts | 2 ++ src/app/units/Blockchain.ts | 7 +++++++ 3 files changed, 26 insertions(+), 2 deletions(-) diff --git a/src/app/http/controllers/api/Blockchain.controller.ts b/src/app/http/controllers/api/Blockchain.controller.ts index f3918b6..4f85729 100644 --- a/src/app/http/controllers/api/Blockchain.controller.ts +++ b/src/app/http/controllers/api/Blockchain.controller.ts @@ -1,7 +1,8 @@ import {Controller} from "@extollo/lib" -import {Injectable} from "@extollo/di" +import {Injectable, Inject} from "@extollo/di" import {TransactionResource, TransactionResourceItem} from "../../../rtdb/TransactionResource" -import {one} from "@extollo/util" +import {many, one} from "@extollo/util" +import {Blockchain as BlockchainService} from "../../../units/Blockchain" /** * Blockchain Controller @@ -10,6 +11,20 @@ import {one} from "@extollo/util" */ @Injectable() export class Blockchain extends Controller { + @Inject() + protected readonly blockchain!: BlockchainService + + /** + * Read the version of the blockchain held by this host, as it currently exists. + */ + public async readBlockchain() { + return many((await this.blockchain.read()).map(x => { + // @ts-ignore + delete x.firebaseID + return x + })) + } + /** * Post a new transaction to the blockchain. This is only intended for testing. */ diff --git a/src/app/http/routes/app.routes.ts b/src/app/http/routes/app.routes.ts index 963a3c6..57313bb 100644 --- a/src/app/http/routes/app.routes.ts +++ b/src/app/http/routes/app.routes.ts @@ -4,4 +4,6 @@ Route.group('/api/v1', () => { Route.post('/encounter', 'api:Blockchain.postTransaction') .pre('DebugOnly') .pre('api:ValidateEncounterTransaction') + + Route.get('/chain', 'api:Blockchain.readBlockchain') }) diff --git a/src/app/units/Blockchain.ts b/src/app/units/Blockchain.ts index 6eb8665..b2ecfbd 100644 --- a/src/app/units/Blockchain.ts +++ b/src/app/units/Blockchain.ts @@ -213,6 +213,13 @@ export class Blockchain extends Unit { return new Block(genesis) } + /** + * Get a list of all blocks in the chain, in order. + */ + public async read(): Promise { + return BlockResource.collect().all() + } + /** * Given a client-submitted transaction, generate a block encounter transaction record. * @param item