Skip to content

Commit

Permalink
Merge pull request #8 from thoschu/release
Browse files Browse the repository at this point in the history
Release
  • Loading branch information
thoschu authored Sep 24, 2020
2 parents a960e2f + 37ae1bb commit a05d0e2
Show file tree
Hide file tree
Showing 15 changed files with 2,246 additions and 106 deletions.
1 change: 1 addition & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# ToDo
130 changes: 51 additions & 79 deletions dev/blockchain.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,57 @@ function Blockchain(currentNodeUrl, nodeIdentifier) {
console.info(`${this.nodeIdentifier} # Genesis-Block created: ${JSON.stringify(this.createNewBlock(undefined, null, '0'))} on ${this.currentNodeUrl}`);
}

Blockchain.prototype.hashBlock = function (previousBlockHash, currentBlockData, nonce) {
const currentBlockDataAsString = JSON.stringify(currentBlockData);
const nonceAsString = nonce.toString();
const dataAsString = `${previousBlockHash}${nonceAsString}${currentBlockDataAsString}`;
return this.hash(dataAsString);
}

Blockchain.prototype.createNewBlock = function(nonce, previousBlockHash, hash) {
const newBlock = {
index: R.inc(this.chain.length),
timeStamp: Date.now(),
transactions: this.pendingTransactions,
nonce: nonce,
previousBlockHash: previousBlockHash,
hash: hash
};

this.pendingTransactions = [];
this.chain.push(newBlock);

return newBlock;
}

Blockchain.prototype.hash = function (dataAsString) {
return sha256(dataAsString);
}

Blockchain.prototype.proofOfWork = function (previousBlockHash, currentBlockData) {
let nonce = -1,
hash;

do {
nonce = R.inc(nonce);
hash = this.hashBlock(previousBlockHash, currentBlockData, nonce);
} while (R.not(hash.substring(0, 4).startsWith('0000')));

return nonce;
}

Blockchain.prototype.getLastBlock = function() {
return R.last(this.chain);
}

Blockchain.prototype.currentBlockData = function () {
return {
index: R.inc(this.getLastBlock().index),
transactions: this.pendingTransactions,
};
}


Blockchain.prototype.getAddressData = function (address) {
let balance = 0;
const addressTransactions = [];
Expand Down Expand Up @@ -111,36 +162,6 @@ Blockchain.prototype.isChainValid = function (blockchain) {
return validChain && R.not(noValidGenesisBlock);
}

Blockchain.prototype.currentBlockData = function () {
return {
index: R.inc(this.getLastBlock().index),
transactions: this.pendingTransactions,
};
}

Blockchain.prototype.proofOfWork = function (previousBlockHash, currentBlockData) {
let nonce = -1,
hash;

do {
nonce = R.inc(nonce);
hash = this.hashBlock(previousBlockHash, currentBlockData, nonce);
} while (R.not(hash.substring(0, 4).startsWith('0000')));

return nonce;
}

Blockchain.prototype.hashBlock = function (previousBlockHash, currentBlockData, nonce) {
const currentBlockDataAsString = JSON.stringify(currentBlockData);
const nonceAsString = nonce.toString();
const dataAsString = `${previousBlockHash}${nonceAsString}${currentBlockDataAsString}`;
return this.hash(dataAsString);
}

Blockchain.prototype.hash = function (dataAsString) {
return sha256(dataAsString);
}

Blockchain.prototype.createNewTransaction = function(amount, sender, recipient) {
const newTransaction = {
transactionId: uuidv4()
Expand All @@ -159,53 +180,4 @@ Blockchain.prototype.addTransactionToPendingTransaction = function(newTransactio
return R.inc(this.getLastBlock()['index']);
}

Blockchain.prototype.getLastBlock = function() {
return R.last(this.chain);
}

Blockchain.prototype.createNewBlock = function(nonce, previousBlockHash, hash) {
const newBlock = {
index: R.inc(this.chain.length),
timeStamp: Date.now(),
transactions: this.pendingTransactions,
nonce: nonce,
previousBlockHash: previousBlockHash,
hash: hash
};

this.pendingTransactions = [];
this.chain.push(newBlock);

return newBlock;
}

// export class Blockchain {
// constructor() {
// this.chain = [];
// this.newTransactions = [];
// }
//
// createNewBlock(nonce, previousBlockHash, hash) {
// const newBlock = this.getABlock(nonce, previousBlockHash, hash);
//
// this.newTransactions = [];
// this.chain.push(newBlock);
//
// return newBlock;
// }
//
// getABlock(nonce, previousBlockHash, hash) {
// const tempBlock = {
// index: this.chain.length + 1,
// timeStamp: Date.now(),
// transactions: this.newTransactions,
// nonce: nonce,
// previousBlockHash: previousBlockHash,
// hash: hash
// };
//
// return tempBlock;
// }
// }

module.exports = Blockchain;
184 changes: 184 additions & 0 deletions dev/blockchain.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,184 @@
'use strict';

import * as sha256 from 'sha256';
import {dec, inc, last, not} from 'ramda';
import * as uuid from 'uuid';

export class Blockchain {
constructor(currentNodeUrl, nodeIdentifier) {
this.nodeIdentifier = nodeIdentifier;
this.currentNodeUrl = currentNodeUrl;
this.networkNodes = [];

this.chain = [];
this.pendingTransactions = [];

console.info(`${this.nodeIdentifier} # Genesis-Block created: ${JSON.stringify(this.createNewBlock(undefined, null, '0'))} on ${this.currentNodeUrl}`);
}

getAddressData(address) {
let balance = 0;
const addressTransactions = [];

this.chain.forEach(block => {
block.transactions.forEach(transaction => {
const isSenderHit = transaction.sender === address;
const isRecipientHit = transaction.recipient === address;

if (isSenderHit || isRecipientHit) {
addressTransactions.push(transaction);
}
});
});

addressTransactions.forEach(transaction => {
if (transaction.recipient === address) {
balance += transaction.amount;
} else if (transaction.sender === address) {
balance -= transaction.amount;
}
});

return {
addressTransactions,
balance
};
}

getTransactionById(transactionId) {
let correctTransaction = null;
let correctBlock = null;

this.chain.some(block => {
return block.transactions.some(transaction => {
const isHit = transaction.transactionId === transactionId;

if (isHit) {
correctTransaction = transaction;
correctBlock = block;
}

return isHit;
});
});

return {
transaction: correctTransaction,
block: correctBlock
};
}

getBlockByHash(blockHash) {
let correctBlock = null;

this.chain.some(block => {
const isHit = block.hash === blockHash;

correctBlock = isHit ? block : correctBlock;

return isHit;
});

return correctBlock;
}

isChainValid(blockchain) {
let validChain = true;
const genesisBlock = blockchain[0];
const correctNonce = genesisBlock.nonce === undefined;
const correctPreviousBlockHash = genesisBlock.previousBlockHash === null;
const correctHash = genesisBlock.hash === '0';
const correctTransactions = genesisBlock.transactions.length === 0;
const noValidGenesisBlock = !correctNonce || !correctPreviousBlockHash || !correctHash || !correctTransactions;

for (let i = 1; i < blockchain.length; i++) {
const currentBlock = blockchain[i];
const previousBlock = blockchain[dec(i)];
const tempBlockData = {
index: currentBlock.index,
transactions: currentBlock.transactions
};
const blockHash = this.hashBlock(previousBlock.hash, tempBlockData, currentBlock.nonce);

// console.log('previousBlock.hash ->', previousBlock.hash);
// console.log('currentBlock.hash ->', currentBlock.hash);

if ((!blockHash.substring(0, 4).startsWith('0000')) && currentBlock.previousBlockHash !== previousBlock.hash) {
validChain = false;
break;
}
}

return validChain && not(noValidGenesisBlock);
}

currentBlockData() {
return {
index: inc(this.getLastBlock().index),
transactions: this.pendingTransactions,
};
}

proofOfWork(previousBlockHash, currentBlockData) {
let nonce = -1,
hash;

do {
nonce = inc(nonce);
hash = this.hashBlock(previousBlockHash, currentBlockData, nonce);
} while (not(hash.substring(0, 4).startsWith('0000')));

return nonce;
}

hashBlock(previousBlockHash, currentBlockData, nonce) {
const currentBlockDataAsString = JSON.stringify(currentBlockData);
const nonceAsString = nonce.toString();
const dataAsString = `${previousBlockHash}${nonceAsString}${currentBlockDataAsString}`;

return this.hash(dataAsString);
}

hash(dataAsString) {
return sha256(dataAsString);
}


createNewTransaction(amount, sender, recipient) {
const newTransaction = {
transactionId: uuid.v4()
};

newTransaction.amount = amount;
newTransaction.sender = sender;
newTransaction.recipient = recipient;

return newTransaction;
}

addTransactionToPendingTransaction(newTransaction) {
this.pendingTransactions.push(newTransaction);

return inc(this.getLastBlock()['index']);
}

getLastBlock() {
return last(this.chain);
}

createNewBlock(nonce, previousBlockHash, hash) {
const newBlock = {
index: inc(this.chain.length),
timeStamp: Date.now(),
transactions: this.pendingTransactions,
nonce: nonce,
previousBlockHash: previousBlockHash,
hash: hash
};

this.pendingTransactions = [];
this.chain.push(newBlock);

return newBlock;
}
}
Loading

0 comments on commit a05d0e2

Please sign in to comment.