Comments (9)
@mikkelgroth Thanks! Already started on a simple implementation in Angular. Here is a sneak peek:
https://savjee.github.io/savjeecoin-frontend/
from savjeecoin.
@mikkelgroth This is an interesting idea to build a simple front-end for it! Do you mind if I adapt your code a bit and maybe include it in the repository?
from savjeecoin.
Hi!
Thanks for your suggestion. Could you open a pull request so I can merge this in?
from savjeecoin.
I have make a pull request
from savjeecoin.
Do you not have to change row of order for the genesis hash and the nonce init....since your first validate hash operation will turn out false since genesis hash is calculated with out nonce=0 ?
from savjeecoin.
@mikkelgroth Not sure I understand your concern... Can you rephrase?
from savjeecoin.
When you run your validate routine .calculateHash() on the genesis block, the hash comes out false because the validate function uses the hash function that includes nonce, but under the creation of the genesis you hash first before you in next line add nonce=0....so genesis.calculateHash is not equal to genesis.hash (I think...since I got that error before I changed the order of the two lines of code, but granted, that I have rearranged a lot of your code in making a small html/js demo)
from savjeecoin.
Just for your info...this is my derived work (and please have in mind that I am a sparetime coder morron ;-)
My blockchainHTMLdemo.html file:
`
Blockchain demo
- Open this in a new Chrome or Firefox browser
- Press Ctrl+Shift+i to open a console log window in the development tool box in Chrome or Firefox do not use IE
- Having the console log is important, to see activity, since the page does not load until the script is done running
- Sellect Difficulty, this means how many leading zeros does there have to be in a valid mined hash
- Sellect Transactions, this is how many blocks should be mined in the simulation
<select id="dif">
<option value="3">Difficulty 3</option>
<option value="4">Difficulty 4</option>
<option value="5">Difficulty 5</option>
<option value="6">Difficulty 6</option>
</select>
<select id="trans">
<option value="3">3 Transactions</option>
<option value="5">5 Transactions</option>
<option value="10">10 Transactions</option>
<option value="20">20 Transactions</option>
<option value="40">40 Transactions</option>
<option value="100">100 Transactions</option>
</select>
<input id="startDemo" type="button" value="Start demo" onclick="startDemo(document.getElementById('dif').value, document.getElementById('trans').value)"/>
<input id="clearDemo" type="button" value="Clear screen" onclick="overide()" />
<p id="chainlog"></p>
<script type="text/javascript">
function overide() {
document.getElementById('chainlog').innerHTML = "";
console.clear();
}
function startDemo(dif, trans) {
document.getElementById('chainlog').innerHTML = "<h3>Demo run log</h3>";
const SHA256 = CryptoJS.SHA256;
class Block {
constructor(index, timestamp, data, previousHash = '') {
this.index = index;
this.timestamp = timestamp;
this.data = data;
this.previousHash = previousHash;
this.nonce = 0;
this.hash = this.calculateHash();
}
calculateHash() {
return SHA256(this.index + this.timestamp + JSON.stringify(this.data) + this.previousHash + this.nonce).toString();
}
mineBlock(difficulty) {
while (this.hash.substring(0, difficulty) != Array(difficulty + 1).join("0")) {
this.nonce++;
this.hash = this.calculateHash();
}
document.getElementById('chainlog').innerHTML += "Block mined --> Hash: <b>" + this.hash + "</b>, Nonce: <b>" + this.nonce + "</b>";
console.log("Block mined! - Hash: " + this.hash + ", Nonce: " + this.nonce);
}
}
class Blockchain {
constructor(difficulty, transnr) {
this.chain = [this.createGenesisBlock()];
this.difficulty = difficulty;
document.getElementById('chainlog').innerHTML += "Created the chain with difficulty (number of leading zeros in hash): " + this.difficulty + "<br/>";
console.log("Created the chain with difficulty (number of leading zeros in hash): " + this.difficulty);
document.getElementById('chainlog').innerHTML += "Added the Genesis block (the first block that can be used to start previous hash chain), Chain length: " + this.chain.length + "<br/>Creating and adding " + transnr + " transactions<br/>";
console.log("Added the Genesis block (the first block that can be used to start previous hash chain), Chain length: " + this.chain.length);
}
createGenesisBlock() {
return new Block(0, "2018/01/01", "Genesis Block", "0");
}
getLatestBlock() {
return this.chain[this.chain.length - 1];
}
addBlock(newBlock) {
newBlock.previousHash = this.getLatestBlock().hash;
newBlock.mineBlock(this.difficulty);
this.chain.push(newBlock);
document.getElementById('chainlog').innerHTML += ", Chain length: <b>" + chain.chain.length + "</b><br/>";
}
ischainValid() {
for (let i = 1; i < this.chain.length; i++) {
let currentBlock = this.chain[i];
let previousBlock = this.chain[i - 1];
if (currentBlock.hash != currentBlock.calculateHash()) {
return false;
}
if (currentBlock.previousHash != previousBlock.calculateHash()) {
document.getElementById('chainlog').innerHTML += "Prev hash: <b>" + currentBlock.previousHash + "</b>, Calc prev hash: " + previousBlock.calculateHash() + "<br/>";
return false;
}
}
return true;
}
displayAccounts(size) {
var accountsDebet = new Array(size);
var accountsCredit = new Array(size);
var debnr, crenr, amnr;
for (let n = 0; n < size; n++) {
accountsDebet[n] = 0;
accountsCredit[n] = 0;
}
for (let i = 1; i < this.chain.length; i++) {
debnr = parseInt(this.chain[i].data.debetaccount) % 1000;
crenr = parseInt(this.chain[i].data.creditaccount) % 1000;
amnr = parseInt(this.chain[i].data.amount);
accountsDebet[debnr - 1] = amnr + parseInt(accountsDebet[parseInt(this.chain[i].data.debetaccount) % 1000 - 1]);
accountsCredit[crenr - 1] = amnr + parseInt(accountsCredit[parseInt(this.chain[i].data.creditaccount) % 1000 - 1]);
}
for (let n = 0; n < size; n++) {
document.getElementById('chainlog').innerHTML += "Account: 100" + (n + 1) + ", Debet: " + accountsDebet[n] + ", Credit: " + accountsCredit[n] + ", Ballance: " + (parseInt(accountsDebet[n]) - parseInt(accountsCredit[n])) + "<br/>";
}
}
}
// *****************************************************************************************
// THIS IS WHERE YOU SET YOUR DIFFICULTY AND HOW MANY TRANSACTIONS YOU WANT TO SIMULATE
// *****************************************************************************************
var difficulty = parseInt(dif); // SET DIFICULITY FOR NUMBER OF PRE-SEEDING ZEROS IN HASH! (1,2,3 = miliseconds | 4 = seconds | 5 = minutes | 6 = doze off | 7 = go home (this script will probably run dry by now of integers in nonce value) | 8 = not this week | 9 = months | 10 = go die in peace | 11)
var transactions = parseInt(trans); // SET HOW MANY TRANSACTIONS YOU WANT TO SIMULATED
// *****************************************************************************************
let chain = new Blockchain(difficulty, transactions);
var timestart;
var timeend;
var timestamp;
var am;
var deb;
var cre;
timestart = new Date().getTime();
document.getElementById('chainlog').innerHTML += "<h3>Transactions</h3>";
for (var i = 1; i < transactions + 1; i++) {
if(i>1) document.getElementById('chainlog').innerHTML += "------------------------------------------<br/>";
console.log("---");
am = Math.floor(Math.random() * 50) + 1; // SET HOW BIG A RANDOM AMOUNT YOU WANT TO TRANSFER -> RANDOM() * 50 + 1 = FROM 1 TO 50
deb = Math.floor(Math.random() * 5) + 1001; // SET A RANDOM DEBIT ACCOUNT TO TRANSFER AMOUNT TO -> RANDOM() * 5 + 1001 = FROM 1001 TO 1005
cre = Math.floor(Math.random() * 5) + 1001; // SET A RANDOM CREDIT ACCOUNT TO TRANSFER AMOUNT FROM -> RANDOM() * 5 + 1001 = FROM 1001 TO 1005
timestamp = new Date().getTime();
document.getElementById('chainlog').innerHTML += "Block created --> Block ID: <b>" + i + "</b>, Timestamp: <b>" + timestamp + "</b>, Amount: <b>" + am + "</b>, Debet account: <b>" + deb + "</b>, Credit account: <b>" + cre + "</b><br/>";
console.log("Block created, Block ID: " + i + ", Timestamp: " + timestamp + ", Amount: " + am + ", Debet account: " + deb + ", Credit account: " + cre);
console.log("... Mining block, please wait ... ");
chain.addBlock(new Block(i, timestamp, { amount: am, debetaccount: deb, creditaccount: cre }));
}
timeend = new Date().getTime();
var timed = timeend - timestart;
document.getElementById('chainlog').innerHTML += "<h3>Time spend mining and validating</h3>";
document.getElementById('chainlog').innerHTML += "Mining time: " + Math.floor(timed / 60000) + " min , " + Math.floor(timed % 60000 / 1000) + " sec , " + Math.floor(timed % 1000) + " mili<br/>";
console.log("Mining time: " + Math.floor(timed / 60000) + " min , " + Math.floor(timed % 60000 / 1000) + " sec , " + Math.floor(timed % 1000) + " mili");
timestart = new Date().getTime();
document.getElementById('chainlog').innerHTML += "Is chain valid: " + chain.ischainValid() + "<br/>";
console.log("Is chain valid: " + chain.ischainValid());
timeend = new Date().getTime();
timed = timeend - timestart;
document.getElementById('chainlog').innerHTML += "Validating time: " + Math.floor(timed / 60000) + " min , " + Math.floor(timed % 60000 / 1000) + " sec , " + Math.floor(timed % 1000) + " mili<br/>";
console.log("Validating time: " + Math.floor(timed / 60000) + " min , " + Math.floor(timed % 60000 / 1000) + " sec , " + Math.floor(timed % 1000) + " mili");
document.getElementById('chainlog').innerHTML += "<h3>Accounts overview after transactions</h3>";
chain.displayAccounts(5);
}
</script>
<h3>Descriptions</h3>
<ul>
<li>Each transaction is moving a random amount (1-50) from one of 5 accounts to another (1001-1005)</li>
<li>Minning: Each transaction is hashed and the content including previous hash is mathematical crushed in to a hex number</li>
<li>A number called Nonce is initiated to 1 and incremented by one until the hash have the desired leading zeros, trial and error method</li>
<li>So basically, the Nonce represents how many times the hashing mecanism had to run until the block was succesfully mined</li>
</ul>
<i>* Thanks to Savjee for basic js code from https://github.com/SavjeeTutorials/SavjeeCoin/</i>
from savjeecoin.
Ohhh please do...looking forward to see your changes....maybe have the frontend reflect what is going on in runtime in stead of having to wait for a completed run....
-Mikkel
from savjeecoin.
Related Issues (20)
- Reactivate Travis HOT 1
- Account balance can't be negative HOT 2
- Not enough balance HOT 2
- Subject of decentralization, worker nodes and wallets HOT 6
- TypeError: Block is not a constructor HOT 3
- Error: Not enough balance HOT 2
- reward transaction not signed? HOT 2
- Cpuminer with this blockchain HOT 2
- isChainValid should check that previousHash matches the hash of its previous block? HOT 3
- [DELETE ME,PLEASE] isChainValid should check that previousHash matches the hash of its previous block?
- how to connect with local host or database? HOT 4
- Genesis block doesn't follow the difficulty HOT 2
- Fix for double spending during mining HOT 6
- Is chain valid? false HOT 3
- Hard to know the valid src. HOT 1
- Reward transaction does not get signed
- TypeError: Transaction is not a constructor HOT 1
- TypeError: Cannot read properties of undefined (reading 'getPublic') HOT 1
- Prettier and strict mode HOT 2
- TypeError: block.transactions is not iterable
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from savjeecoin.