Implement basic chain validation and stub PoW

This commit is contained in:
Garrett Mills 2021-04-10 02:51:22 -05:00
parent 289f184b4d
commit 29f9fe9f1f
Signed by: garrettmills
GPG Key ID: D2BF5FBA8298F246
4 changed files with 1000 additions and 730 deletions

1
.gitignore vendored
View File

@ -104,3 +104,4 @@ dist
.tern-port
lib/*
*.gpg.key

View File

@ -13,6 +13,7 @@
"@extollo/lib": "^0.1.2",
"@extollo/orm": "^0.1.1",
"@extollo/util": "^0.3.1",
"bcrypt": "^5.0.1",
"copyfiles": "^2.4.1",
"firebase-admin": "^9.6.0",
"openpgp": "^5.0.0-1",

File diff suppressed because it is too large Load Diff

View File

@ -4,6 +4,7 @@ import {FirebaseUnit} from "./FirebaseUnit"
import {BlockResource, BlockResourceItem, BlockTransaction} from "../rtdb/BlockResource"
import {TransactionResourceItem} from "../rtdb/TransactionResource"
import * as crypto from "crypto"
import {collect} from "@extollo/util";
export class Block implements BlockResourceItem {
seqID: number;
@ -80,7 +81,12 @@ export class Blockchain extends Unit {
}
public async validate(chain: Block[]) {
const blocks = collect<Block>(chain)
return blocks.every((block: Block, idx: number) => {
if ( idx === 0 ) return true; // TODO handle genesis
return block.lastBlockHash === blocks.at(idx)!.hash()
})
}
public async refresh() {
@ -107,4 +113,12 @@ export class Blockchain extends Unit {
public async down() {
}
protected async generateProofOfWork(lastBlock: Block): Promise<string> {
const hash = lastBlock.hash()
// Create a signature from the hash using the server's private key
return hash
}
}