mirror of
https://github.com/hackku21/loc-chain-backend.git
synced 2024-10-27 20:34:03 +00:00
Add timestamps to block interfaces and clean up genesis block generation
This commit is contained in:
parent
5dc6db0b8e
commit
9e4821c296
@ -53,6 +53,7 @@ export interface BlockResourceItem extends FirebaseResourceItem {
|
|||||||
lastBlockHash: string;
|
lastBlockHash: string;
|
||||||
lastBlockUUID: string;
|
lastBlockUUID: string;
|
||||||
proof: string;
|
proof: string;
|
||||||
|
timestamp: number;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Injectable()
|
@Injectable()
|
||||||
|
@ -15,6 +15,7 @@ export class Block implements BlockResourceItem {
|
|||||||
seqID: number;
|
seqID: number;
|
||||||
uuid: string;
|
uuid: string;
|
||||||
transactions: BlockTransaction[];
|
transactions: BlockTransaction[];
|
||||||
|
timestamp: number;
|
||||||
lastBlockHash: string;
|
lastBlockHash: string;
|
||||||
lastBlockUUID: string;
|
lastBlockUUID: string;
|
||||||
proof: string;
|
proof: string;
|
||||||
@ -27,6 +28,7 @@ export class Block implements BlockResourceItem {
|
|||||||
this.lastBlockHash = rec.lastBlockHash
|
this.lastBlockHash = rec.lastBlockHash
|
||||||
this.lastBlockUUID = rec.lastBlockUUID
|
this.lastBlockUUID = rec.lastBlockUUID
|
||||||
this.proof = rec.proof;
|
this.proof = rec.proof;
|
||||||
|
this.timestamp = rec.timestamp;
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Returns true if this is the genesis block. */
|
/** Returns true if this is the genesis block. */
|
||||||
@ -52,6 +54,7 @@ export class Block implements BlockResourceItem {
|
|||||||
lastBlockHash: this.lastBlockHash,
|
lastBlockHash: this.lastBlockHash,
|
||||||
lastBlockUUID: this.lastBlockUUID,
|
lastBlockUUID: this.lastBlockUUID,
|
||||||
proof: this.proof,
|
proof: this.proof,
|
||||||
|
timestamp: this.timestamp,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -139,10 +142,10 @@ export class Blockchain extends Unit {
|
|||||||
* @param group
|
* @param group
|
||||||
*/
|
*/
|
||||||
public async submitTransactions(group: [TransactionResourceItem, TransactionResourceItem]) {
|
public async submitTransactions(group: [TransactionResourceItem, TransactionResourceItem]) {
|
||||||
let lastBlock = await this.getLastBlock()
|
const lastBlock = await this.getLastBlock()
|
||||||
if ( !lastBlock ) await this.getGenesisBlock()
|
|
||||||
|
|
||||||
const block: BlockResourceItem = {
|
const block: BlockResourceItem = {
|
||||||
|
timestamp: (new Date).getTime(),
|
||||||
uuid: uuid_v4(),
|
uuid: uuid_v4(),
|
||||||
transactions: group.map(item => this.getEncounterTransaction(item)),
|
transactions: group.map(item => this.getEncounterTransaction(item)),
|
||||||
lastBlockHash: lastBlock!.hash(),
|
lastBlockHash: lastBlock!.hash(),
|
||||||
@ -162,6 +165,7 @@ export class Blockchain extends Unit {
|
|||||||
*/
|
*/
|
||||||
public getGenesisBlock(): Block {
|
public getGenesisBlock(): Block {
|
||||||
return new Block({
|
return new Block({
|
||||||
|
timestamp: (new Date).getTime(),
|
||||||
uuid: '0000',
|
uuid: '0000',
|
||||||
transactions: [],
|
transactions: [],
|
||||||
lastBlockHash: '',
|
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()
|
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()
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
Loading…
Reference in New Issue
Block a user