Adding source code for the article tracked under BAEL-3232. (#7657)
This commit is contained in:
committed by
Grzegorz Piwowarek
parent
0216c364b0
commit
9d825c429e
@@ -0,0 +1,65 @@
|
||||
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;
|
||||
|
||||
public class Block {
|
||||
|
||||
private static Logger logger = Logger.getLogger(Block.class.getName());
|
||||
|
||||
private String hash;
|
||||
private String previousHash;
|
||||
private String data;
|
||||
private long timeStamp;
|
||||
private int nonce;
|
||||
|
||||
public Block(String data, String previousHash) {
|
||||
this.data = data;
|
||||
this.previousHash = previousHash;
|
||||
this.timeStamp = new Date().getTime();
|
||||
this.hash = calculateBlockHash();
|
||||
}
|
||||
|
||||
public String mineBlock(int prefix) {
|
||||
String prefixString = new String(new char[prefix]).replace('\0', '0');
|
||||
while (!hash.substring(0, prefix)
|
||||
.equals(prefixString)) {
|
||||
nonce++;
|
||||
hash = calculateBlockHash();
|
||||
}
|
||||
return hash;
|
||||
}
|
||||
|
||||
public String calculateBlockHash() {
|
||||
String dataToHash = previousHash + Long.toString(timeStamp) + Integer.toString(nonce) + data;
|
||||
MessageDigest digest = null;
|
||||
byte[] bytes = null;
|
||||
try {
|
||||
digest = MessageDigest.getInstance("SHA-256");
|
||||
bytes = digest.digest(dataToHash.getBytes("UTF-8"));
|
||||
} catch (NoSuchAlgorithmException | UnsupportedEncodingException ex) {
|
||||
logger.log(Level.SEVERE, ex.getMessage());
|
||||
}
|
||||
StringBuffer buffer = new StringBuffer();
|
||||
for (byte b : bytes) {
|
||||
buffer.append(String.format("%02x", b));
|
||||
}
|
||||
return buffer.toString();
|
||||
}
|
||||
|
||||
public String getHash() {
|
||||
return this.hash;
|
||||
}
|
||||
|
||||
public String getPreviousHash() {
|
||||
return this.previousHash;
|
||||
}
|
||||
|
||||
public void setData(String data) {
|
||||
this.data = data;
|
||||
}
|
||||
}
|
||||
13
java-blockchain/src/main/resources/logback.xml
Normal file
13
java-blockchain/src/main/resources/logback.xml
Normal file
@@ -0,0 +1,13 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<configuration>
|
||||
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
|
||||
<encoder>
|
||||
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n
|
||||
</pattern>
|
||||
</encoder>
|
||||
</appender>
|
||||
|
||||
<root level="INFO">
|
||||
<appender-ref ref="STDOUT" />
|
||||
</root>
|
||||
</configuration>
|
||||
@@ -0,0 +1,67 @@
|
||||
package com.baeldung.blockchain;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import org.junit.AfterClass;
|
||||
import org.junit.BeforeClass;
|
||||
import org.junit.Test;
|
||||
|
||||
public class BlockchainUnitTest {
|
||||
|
||||
public static List<Block> blockchain = new ArrayList<Block>();
|
||||
public static int prefix = 4;
|
||||
public static String prefixString = new String(new char[prefix]).replace('\0', '0');
|
||||
|
||||
@BeforeClass
|
||||
public static void setUp() {
|
||||
Block genesisBlock = new Block("The is the Genesis Block.", "0");
|
||||
genesisBlock.mineBlock(prefix);
|
||||
blockchain.add(genesisBlock);
|
||||
Block firstBlock = new Block("The is the First Block.", genesisBlock.getHash());
|
||||
firstBlock.mineBlock(prefix);
|
||||
blockchain.add(firstBlock);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenBlockchain_whenNewBlockAdded_thenSuccess() {
|
||||
Block newBlock = new Block("The is a New Block.", blockchain.get(blockchain.size() - 1)
|
||||
.getHash());
|
||||
newBlock.mineBlock(prefix);
|
||||
assertTrue(newBlock.getHash()
|
||||
.substring(0, prefix)
|
||||
.equals(prefixString));
|
||||
blockchain.add(newBlock);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenBlockchain_whenValidated_thenSuccess() {
|
||||
boolean flag = true;
|
||||
for (int i = 0; i < blockchain.size(); i++) {
|
||||
String previousHash = i == 0 ? "0"
|
||||
: blockchain.get(i - 1)
|
||||
.getHash();
|
||||
flag = blockchain.get(i)
|
||||
.getHash()
|
||||
.equals(blockchain.get(i)
|
||||
.calculateBlockHash())
|
||||
&& previousHash.equals(blockchain.get(i)
|
||||
.getPreviousHash())
|
||||
&& blockchain.get(i)
|
||||
.getHash()
|
||||
.substring(0, prefix)
|
||||
.equals(prefixString);
|
||||
if (!flag)
|
||||
break;
|
||||
}
|
||||
assertTrue(flag);
|
||||
}
|
||||
|
||||
@AfterClass
|
||||
public static void tearDown() {
|
||||
blockchain.clear();
|
||||
}
|
||||
|
||||
}
|
||||
13
java-blockchain/src/test/resources/.gitignore
vendored
Normal file
13
java-blockchain/src/test/resources/.gitignore
vendored
Normal file
@@ -0,0 +1,13 @@
|
||||
*.class
|
||||
|
||||
#folders#
|
||||
/target
|
||||
/neoDb*
|
||||
/data
|
||||
/src/main/webapp/WEB-INF/classes
|
||||
*/META-INF/*
|
||||
|
||||
# Packaged files #
|
||||
*.jar
|
||||
*.war
|
||||
*.ear
|
||||
Reference in New Issue
Block a user