This commit is contained in:
QiTao Weng 2021-04-10 09:23:54 +00:00
parent 025bc58869
commit 5ae20e945f
3 changed files with 37 additions and 37 deletions

View File

@ -11,33 +11,33 @@ import { collect, uuid_v4 } from "@extollo/util"
* Utility wrapper class for a block in the chain. * Utility wrapper class for a block in the chain.
*/ */
export class Block implements BlockResourceItem { export class Block implements BlockResourceItem {
firebaseID: string; firebaseID: string
seqID: number; seqID: number
uuid: string; uuid: string
transactions: BlockTransaction[]; transactions: BlockTransaction[]
timestamp: number; timestamp: number
lastBlockHash: string; lastBlockHash: string
lastBlockUUID: string; lastBlockUUID: string
proof: string; proof: string
get config(): Config { get config(): Config {
return Application.getApplication().make(Config) return Application.getApplication().make(Config)
} }
constructor(rec: BlockResourceItem) { constructor(rec: BlockResourceItem) {
this.firebaseID = rec.firebaseID; this.firebaseID = rec.firebaseID
this.seqID = rec.seqID this.seqID = rec.seqID
this.uuid = rec.uuid this.uuid = rec.uuid
this.transactions = rec.transactions this.transactions = rec.transactions
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; this.timestamp = rec.timestamp
} }
/** Returns true if this is the genesis block. */ /** Returns true if this is the genesis block. */
async isGenesis() { async isGenesis() {
// first block will be guaranteed uuid 0000 // first block will be guaranteed uuid 0000
if (this.uuid !== '0000') { if (this.uuid !== '0000') {
return false; return false
} }
const proof = this.proof const proof = this.proof
const publicKey = this.config.get("app.gpg.key.public") const publicKey = this.config.get("app.gpg.key.public")

View File

@ -1,5 +1,5 @@
import {Singleton, Inject} from "@extollo/di" import { Singleton, Inject } from "@extollo/di"
import {Unit, Logging, Config} from "@extollo/lib" import { Unit, Logging, Config } from "@extollo/lib"
import * as firebase from "firebase-admin" import * as firebase from "firebase-admin"
export type RTDBRef = 'peers' | 'transaction' | 'block' export type RTDBRef = 'peers' | 'transaction' | 'block'
@ -11,7 +11,7 @@ export type RTDBRef = 'peers' | 'transaction' | 'block'
*/ */
@Singleton() @Singleton()
export class FirebaseUnit extends Unit { export class FirebaseUnit extends Unit {
protected _firebase = firebase; protected _firebase = firebase
@Inject() @Inject()
protected readonly logging!: Logging protected readonly logging!: Logging

View File

@ -1,9 +1,9 @@
import {FirebaseUnit} from "../FirebaseUnit" import { FirebaseUnit } from "../FirebaseUnit"
import {TransactionResource, TransactionResourceItem} from "../../rtdb/TransactionResource" import { TransactionResource, TransactionResourceItem } from "../../rtdb/TransactionResource"
import {Singleton, Inject} from "@extollo/di" import { Singleton, Inject } from "@extollo/di"
import {Unit, Logging} from "@extollo/lib" import { Unit, Logging } from "@extollo/lib"
import * as openpgp from "openpgp" import * as openpgp from "openpgp"
import {Blockchain} from "../Blockchain" import { Blockchain } from "../Blockchain"
/** /**
* Transaction Unit * Transaction Unit
@ -18,7 +18,7 @@ export class Transaction extends Unit {
@Inject() @Inject()
protected readonly blockchain!: Blockchain protected readonly blockchain!: Blockchain
public async compareTransactions(transaction1: TransactionResourceItem, transaction2: TransactionResourceItem){ public async compareTransactions(transaction1: TransactionResourceItem, transaction2: TransactionResourceItem) {
// verify signature // verify signature
const result1 = await openpgp.verify({ const result1 = await openpgp.verify({
publicKeys: await openpgp.readKey({ publicKeys: await openpgp.readKey({
@ -38,9 +38,9 @@ export class Transaction extends Unit {
armoredSignature: transaction2.validationSignature // parse detached signature armoredSignature: transaction2.validationSignature // parse detached signature
}) })
}) })
return await (result1.signatures[0].verified) && await (result2.signatures[0].verified); return await (result1.signatures[0].verified) && await (result2.signatures[0].verified)
} }
public async up() { public async up() {
this.firebase.ref("transaction").on("value", async () => { this.firebase.ref("transaction").on("value", async () => {
// array of pairs of tranaction resource items // array of pairs of tranaction resource items
@ -51,25 +51,25 @@ export class Transaction extends Unit {
transactions.each(transaction1 => { transactions.each(transaction1 => {
// for each item that is not itself // for each item that is not itself
transactions.where("combinedHash", "!=", transaction1.combinedHash) transactions.where("combinedHash", "!=", transaction1.combinedHash)
// get a second item // get a second item
.each(transaction2 => { .each(transaction2 => {
//if the item matches //if the item matches
if (this.compareTransactions(transaction1, transaction2)) { if (this.compareTransactions(transaction1, transaction2)) {
// and remove the two matching items // and remove the two matching items
transactions = transactions.whereNotIn("combinedHash", [transaction1.combinedHash, transaction2.combinedHash]) transactions = transactions.whereNotIn("combinedHash", [transaction1.combinedHash, transaction2.combinedHash])
// insert grouped items into groupedTransactions // insert grouped items into groupedTransactions
groupedTransactions.push([transaction1, transaction2]); groupedTransactions.push([transaction1, transaction2])
} }
}) })
}) })
for (const group of groupedTransactions) { for (const group of groupedTransactions) {
await this.blockchain.submitTransactions(group); await this.blockchain.submitTransactions(group)
await this.firebase.ref("transaction").child(group[0].firebaseID).remove(); await this.firebase.ref("transaction").child(group[0].firebaseID).remove()
await this.firebase.ref("transaction").child(group[1].firebaseID).remove(); await this.firebase.ref("transaction").child(group[1].firebaseID).remove()
} }
}) })
} }
public async down() { public async down() {
} }
} }