Add timestamps to block interfaces and clean up genesis block generation

This commit is contained in:
Garrett Mills 2021-04-10 03:58:38 -05:00
parent 5dc6db0b8e
commit 9e4821c296
Signed by: garrettmills
GPG Key ID: D2BF5FBA8298F246
2 changed files with 14 additions and 5 deletions

View File

@ -53,6 +53,7 @@ export interface BlockResourceItem extends FirebaseResourceItem {
lastBlockHash: string;
lastBlockUUID: string;
proof: string;
timestamp: number;
}
@Injectable()

View File

@ -15,6 +15,7 @@ export class Block implements BlockResourceItem {
seqID: number;
uuid: string;
transactions: BlockTransaction[];
timestamp: number;
lastBlockHash: string;
lastBlockUUID: string;
proof: string;
@ -27,6 +28,7 @@ export class Block implements BlockResourceItem {
this.lastBlockHash = rec.lastBlockHash
this.lastBlockUUID = rec.lastBlockUUID
this.proof = rec.proof;
this.timestamp = rec.timestamp;
}
/** Returns true if this is the genesis block. */
@ -52,6 +54,7 @@ export class Block implements BlockResourceItem {
lastBlockHash: this.lastBlockHash,
lastBlockUUID: this.lastBlockUUID,
proof: this.proof,
timestamp: this.timestamp,
}
}
@ -139,10 +142,10 @@ export class Blockchain extends Unit {
* @param group
*/
public async submitTransactions(group: [TransactionResourceItem, TransactionResourceItem]) {
let lastBlock = await this.getLastBlock()
if ( !lastBlock ) await this.getGenesisBlock()
const lastBlock = await this.getLastBlock()
const block: BlockResourceItem = {
timestamp: (new Date).getTime(),
uuid: uuid_v4(),
transactions: group.map(item => this.getEncounterTransaction(item)),
lastBlockHash: lastBlock!.hash(),
@ -162,6 +165,7 @@ export class Blockchain extends Unit {
*/
public getGenesisBlock(): Block {
return new Block({
timestamp: (new Date).getTime(),
uuid: '0000',
transactions: [],
lastBlockHash: '',
@ -173,11 +177,15 @@ export class Blockchain extends Unit {
}
/**
* Get the last block in the blockchain.
* Get the last block in the blockchain, or push the genesis if one doesn't already exist.
*/
public async getLastBlock(): Promise<Block | undefined> {
public async getLastBlock(): Promise<Block> {
const rec: BlockResourceItem | undefined = await BlockResource.collect().last()
return rec ? new Block(rec) : undefined
if ( rec ) return new Block(rec)
const genesis = this.getGenesisBlock().toItem()
await (<BlockResource> this.app().make(BlockResource)).push(genesis)
return this.getLastBlock()
}
/**