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.io.UnsupportedEncodingException;
|
||||||
import java.security.MessageDigest;
|
import java.security.MessageDigest;
|
||||||
import java.security.NoSuchAlgorithmException;
|
import java.security.NoSuchAlgorithmException;
|
||||||
import java.util.Date;
|
|
||||||
import java.util.logging.Level;
|
import java.util.logging.Level;
|
||||||
import java.util.logging.Logger;
|
import java.util.logging.Logger;
|
||||||
|
|
||||||
@ -17,10 +16,10 @@ public class Block {
|
|||||||
private long timeStamp;
|
private long timeStamp;
|
||||||
private int nonce;
|
private int nonce;
|
||||||
|
|
||||||
public Block(String data, String previousHash) {
|
public Block(String data, String previousHash, long timeStamp) {
|
||||||
this.data = data;
|
this.data = data;
|
||||||
this.previousHash = previousHash;
|
this.previousHash = previousHash;
|
||||||
this.timeStamp = new Date().getTime();
|
this.timeStamp = timeStamp;
|
||||||
this.hash = calculateBlockHash();
|
this.hash = calculateBlockHash();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -3,6 +3,7 @@ package com.baeldung.blockchain;
|
|||||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
import java.util.Date;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
import org.junit.AfterClass;
|
import org.junit.AfterClass;
|
||||||
@ -17,10 +18,10 @@ public class BlockchainUnitTest {
|
|||||||
|
|
||||||
@BeforeClass
|
@BeforeClass
|
||||||
public static void setUp() {
|
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);
|
genesisBlock.mineBlock(prefix);
|
||||||
blockchain.add(genesisBlock);
|
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);
|
firstBlock.mineBlock(prefix);
|
||||||
blockchain.add(firstBlock);
|
blockchain.add(firstBlock);
|
||||||
}
|
}
|
||||||
@ -28,7 +29,7 @@ public class BlockchainUnitTest {
|
|||||||
@Test
|
@Test
|
||||||
public void givenBlockchain_whenNewBlockAdded_thenSuccess() {
|
public void givenBlockchain_whenNewBlockAdded_thenSuccess() {
|
||||||
Block newBlock = new Block("The is a New Block.", blockchain.get(blockchain.size() - 1)
|
Block newBlock = new Block("The is a New Block.", blockchain.get(blockchain.size() - 1)
|
||||||
.getHash());
|
.getHash(), new Date().getTime());
|
||||||
newBlock.mineBlock(prefix);
|
newBlock.mineBlock(prefix);
|
||||||
assertTrue(newBlock.getHash()
|
assertTrue(newBlock.getHash()
|
||||||
.substring(0, prefix)
|
.substring(0, prefix)
|
||||||
|
2
pom.xml
2
pom.xml
@ -579,7 +579,7 @@
|
|||||||
|
|
||||||
<module>spring-boot-nashorn</module>
|
<module>spring-boot-nashorn</module>
|
||||||
<module>java-blockchain</module>
|
<module>java-blockchain</module>
|
||||||
<!-- <module>Twitter4J</module> --> <!-- Builds locally, but fails in Jenkins, Failed to parse POMs -->
|
|
||||||
</modules>
|
</modules>
|
||||||
|
|
||||||
</profile>
|
</profile>
|
||||||
|
Loading…
x
Reference in New Issue
Block a user