Conversation
src/blockchain/Blockchain.java
Outdated
| } | ||
|
|
||
| public synchronized Block tail() { | ||
| return blocks.get(blocks.size() - 1); |
There was a problem hiding this comment.
If you want, you may rewrite this code using ReentrantReadWritelock to separate lock for write and read operations.
src/blockchain/Blockchain.java
Outdated
|
|
||
| private boolean isValid(Block newBlock) { | ||
| Block tailBlock = blocks.get(blocks.size() - 1); | ||
| if (!newBlock.getHash().startsWith(getPrefix()) || !newBlock.getHashPreviousBlock() |
There was a problem hiding this comment.
Objects.equals(...,...) is better to avoid potential NPE
src/blockchain/Blockchain.java
Outdated
| for (int i = 1; i < blocks.size(); i++) { | ||
| Block prev = blocks.get(i - 1); | ||
| Block cur = blocks.get(i); | ||
| if (!cur.getHashPreviousBlock().equals(prev.getHash())) { |
There was a problem hiding this comment.
I think Objects.equals(...) is better here
| } | ||
|
|
||
| private void adjustComplexity(Block newBlock) { | ||
| if (!withinAcceptable(newBlock)) { |
There was a problem hiding this comment.
It is good idea to encapsulate the condition's logic in a separated method
src/blockchain/data/Message.java
Outdated
|
|
||
| public class Message { | ||
|
|
||
| private String author; |
| private String data; | ||
|
|
||
|
|
||
| public Block(Integer id, Long minerId, String hashPreviousBlock, String hash, Long timestamp, |
There was a problem hiding this comment.
I think the constructor takes a lot of parameters. You may try to use the Builder pattern or combine some parameters together in a single entity
src/blockchain/Main.java
Outdated
| Blockchain<Message> blockchain = new Blockchain<>(persister, | ||
| new PlanMessageFormat()); | ||
|
|
||
| Thread[] miners = new Thread[NUMBER_OF_MINERS]; |
There was a problem hiding this comment.
Java 8+ way:
List<Thread> miners = Stream
.generate(() -> new Thread(() -> System.out.println("hello")))
.limit(100)
.collect(Collectors.toList());
miners.forEach(Thread::start);
miners.forEach(Thread::interrupt);
| @@ -0,0 +1,57 @@ | |||
| package blockchain; | |||
There was a problem hiding this comment.
The code looks good and readable. Convenient separation into classes and methods.
No description provided.