Java blockchain (#7694)
* Adding source code for the article tracked under BAEL-3232. * Incorporated the review comments on the article.
This commit is contained in:
parent
73743acb30
commit
8c11eeb20e
@ -3,7 +3,6 @@ package com.baeldung.blockchain;
|
||||
import java.io.UnsupportedEncodingException;
|
||||
import java.security.MessageDigest;
|
||||
import java.security.NoSuchAlgorithmException;
|
||||
import java.util.Date;
|
||||
import java.util.logging.Level;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
@ -17,10 +16,10 @@ public class Block {
|
||||
private long timeStamp;
|
||||
private int nonce;
|
||||
|
||||
public Block(String data, String previousHash) {
|
||||
public Block(String data, String previousHash, long timeStamp) {
|
||||
this.data = data;
|
||||
this.previousHash = previousHash;
|
||||
this.timeStamp = new Date().getTime();
|
||||
this.timeStamp = timeStamp;
|
||||
this.hash = calculateBlockHash();
|
||||
}
|
||||
|
||||
|
@ -3,6 +3,7 @@ package com.baeldung.blockchain;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
|
||||
import org.junit.AfterClass;
|
||||
@ -17,10 +18,10 @@ public class BlockchainUnitTest {
|
||||
|
||||
@BeforeClass
|
||||
public static void setUp() {
|
||||
Block genesisBlock = new Block("The is the Genesis Block.", "0");
|
||||
Block genesisBlock = new Block("The is the Genesis Block.", "0", new Date().getTime());
|
||||
genesisBlock.mineBlock(prefix);
|
||||
blockchain.add(genesisBlock);
|
||||
Block firstBlock = new Block("The is the First Block.", genesisBlock.getHash());
|
||||
Block firstBlock = new Block("The is the First Block.", genesisBlock.getHash(), new Date().getTime());
|
||||
firstBlock.mineBlock(prefix);
|
||||
blockchain.add(firstBlock);
|
||||
}
|
||||
@ -28,7 +29,7 @@ public class BlockchainUnitTest {
|
||||
@Test
|
||||
public void givenBlockchain_whenNewBlockAdded_thenSuccess() {
|
||||
Block newBlock = new Block("The is a New Block.", blockchain.get(blockchain.size() - 1)
|
||||
.getHash());
|
||||
.getHash(), new Date().getTime());
|
||||
newBlock.mineBlock(prefix);
|
||||
assertTrue(newBlock.getHash()
|
||||
.substring(0, prefix)
|
||||
|
Loading…
x
Reference in New Issue
Block a user