Skip to content

Commit

Permalink
tests: blockchain model
Browse files Browse the repository at this point in the history
  • Loading branch information
fgladisch committed Mar 23, 2018
1 parent 2e86dc2 commit 135f728
Show file tree
Hide file tree
Showing 7 changed files with 226 additions and 35 deletions.
46 changes: 26 additions & 20 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,22 +1,28 @@
{
"name": "fxchain",
"version": "0.1.0",
"description": "The best blockchain in the world.",
"main": "src/index.ts",
"repository": "[email protected]:fgladisch/fxchain.git",
"author": "Felix Gladisch <[email protected]>",
"license": "MIT",
"private": false,
"devDependencies": {
"@types/node": "^9.6.0",
"tslint": "5.9.1",
"tslint-config-prettier": "^1.10.0",
"typescript": "2.7.2"
},
"scripts": {
"compile": "tsc"
},
"engines": {
"node": "^8 || ^9"
}
"name": "fxchain",
"version": "0.1.0",
"description": "The best blockchain in the world.",
"main": "src/index.ts",
"repository": "[email protected]:fgladisch/fxchain.git",
"author": "Felix Gladisch <[email protected]>",
"license": "MIT",
"private": false,
"devDependencies": {
"@types/chai": "4.1.2",
"@types/mocha": "5.0.0",
"@types/node": "9.6.0",
"chai": "4.1.2",
"mocha": "5.0.5",
"ts-node": "5.0.1",
"tslint": "5.9.1",
"tslint-config-prettier": "1.10.0",
"typescript": "2.7.2"
},
"scripts": {
"compile": "tsc",
"test": "mocha -r ts-node/register src/**/*.test.ts"
},
"engines": {
"node": "8 || 9"
}
}
40 changes: 40 additions & 0 deletions src/blockchain.model.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import { assert } from "chai";

import { Block } from "./block.model";
import { Blockchain } from "./blockchain.model";

const TEST_DATA: string = "TEST";

describe("Blockchain", () => {
it("should create a new blockchain", () => {
const blockchain: Blockchain = new Blockchain();
assert.isDefined(blockchain.getGenesisBlock());
});

it("should create a blockchain with existing blocks", () => {
const testBlock: Block = new Block({
data: TEST_DATA,
index: 0,
previousHash: null
});
const blockchain: Blockchain = new Blockchain([testBlock]);
assert.equal(blockchain.getChain()[0], testBlock);
});

it("should generate a new block", () => {
const blockchain: Blockchain = new Blockchain();
blockchain.generateNextBlock(TEST_DATA);
const latestBlock: Block = blockchain.getLatestBlock();
assert.equal(latestBlock.data, TEST_DATA);
});

it("should overwrite the existing chain", () => {
const blockchain: Blockchain = new Blockchain();
blockchain.generateNextBlock(TEST_DATA);
const copy: Blockchain = new Blockchain([...blockchain.getChain()]);
copy.generateNextBlock(TEST_DATA);
assert.lengthOf(blockchain.getChain(), 2);
blockchain.replaceChain(copy.getChain());
assert.lengthOf(blockchain.getChain(), 3);
});
});
7 changes: 3 additions & 4 deletions src/blockchain.model.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Block, BlockParams } from "./block.model";
import { isValidChain, isValidNewBlock } from "./util/blockchain";
import { isValidChain } from "./util/blockchain";
import { calculateBlockHash } from "./util/crypto";

const GENESIS_BLOCK_PARAMS: BlockParams = {
Expand All @@ -11,7 +11,7 @@ const GENESIS_BLOCK_PARAMS: BlockParams = {
export class Blockchain {
private chain: Block[];

constructor(chain: Block[]) {
constructor(chain?: Block[]) {
if (chain) {
this.chain = chain;
} else {
Expand Down Expand Up @@ -47,9 +47,8 @@ export class Blockchain {
}

public replaceChain(newBlocks: Block[]): void {
if (isValidChain(this.chain[0], newBlocks) && newBlocks.length > this.chain.length) {
if (isValidChain(this.getGenesisBlock(), newBlocks) && newBlocks.length > this.chain.length) {
this.chain = newBlocks;
// TODO: broadcastLatest();
}
}
}
10 changes: 6 additions & 4 deletions src/util/blockchain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,12 @@ export function isValidChain(genesis: Block, chain: Block[]): boolean {
return false;
}

return chain.reduce(
(valid, block) => isValidNewBlock(block, this.chain[block.index - 1]) || valid,
true
);
return chain.reduce((valid, block) => {
if (block.index === 0) {
return true;
}
return isValidNewBlock(block, chain[block.index - 1]) || valid;
}, true);
}

export function isValidNewBlock(newBlock: Block, previousBlock: Block): boolean {
Expand Down
5 changes: 2 additions & 3 deletions src/util/crypto.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
import crypto = require("crypto");

import { Block } from "./block.model";
import { Block } from "../block.model";

export function createHash(text: string): string {
return crypto
.createHash("sha256")
.update(text, "utf8")
.digest()
.toString();
.digest("hex");
}

export function calculateBlockHash({ index, previousHash, timestamp, data }: Block): string {
Expand Down
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@
"outDir": "dist"
},
"include": ["src/**/*.ts"],
"exclude": ["node_modules"]
"exclude": ["node_modules", "src/**/*.test.ts"]
}
Loading

0 comments on commit 135f728

Please sign in to comment.