Skip to content

Commit 8c11eeb

Browse files
kcacademicpivovarit
authored andcommitted
Java blockchain (eugenp#7694)
* Adding source code for the article tracked under BAEL-3232. * Incorporated the review comments on the article.
1 parent 73743ac commit 8c11eeb

File tree

3 files changed

+7
-7
lines changed

3 files changed

+7
-7
lines changed

java-blockchain/src/main/java/com/baeldung/blockchain/Block.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
import java.io.UnsupportedEncodingException;
44
import java.security.MessageDigest;
55
import java.security.NoSuchAlgorithmException;
6-
import java.util.Date;
76
import java.util.logging.Level;
87
import java.util.logging.Logger;
98

@@ -17,10 +16,10 @@ public class Block {
1716
private long timeStamp;
1817
private int nonce;
1918

20-
public Block(String data, String previousHash) {
19+
public Block(String data, String previousHash, long timeStamp) {
2120
this.data = data;
2221
this.previousHash = previousHash;
23-
this.timeStamp = new Date().getTime();
22+
this.timeStamp = timeStamp;
2423
this.hash = calculateBlockHash();
2524
}
2625

java-blockchain/src/test/java/com/baeldung/blockchain/BlockchainUnitTest.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import static org.junit.jupiter.api.Assertions.assertTrue;
44

55
import java.util.ArrayList;
6+
import java.util.Date;
67
import java.util.List;
78

89
import org.junit.AfterClass;
@@ -17,18 +18,18 @@ public class BlockchainUnitTest {
1718

1819
@BeforeClass
1920
public static void setUp() {
20-
Block genesisBlock = new Block("The is the Genesis Block.", "0");
21+
Block genesisBlock = new Block("The is the Genesis Block.", "0", new Date().getTime());
2122
genesisBlock.mineBlock(prefix);
2223
blockchain.add(genesisBlock);
23-
Block firstBlock = new Block("The is the First Block.", genesisBlock.getHash());
24+
Block firstBlock = new Block("The is the First Block.", genesisBlock.getHash(), new Date().getTime());
2425
firstBlock.mineBlock(prefix);
2526
blockchain.add(firstBlock);
2627
}
2728

2829
@Test
2930
public void givenBlockchain_whenNewBlockAdded_thenSuccess() {
3031
Block newBlock = new Block("The is a New Block.", blockchain.get(blockchain.size() - 1)
31-
.getHash());
32+
.getHash(), new Date().getTime());
3233
newBlock.mineBlock(prefix);
3334
assertTrue(newBlock.getHash()
3435
.substring(0, prefix)

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -579,7 +579,7 @@
579579

580580
<module>spring-boot-nashorn</module>
581581
<module>java-blockchain</module>
582-
<!-- <module>Twitter4J</module> --> <!-- Builds locally, but fails in Jenkins, Failed to parse POMs -->
582+
583583
</modules>
584584

585585
</profile>

0 commit comments

Comments
 (0)