Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
import java.util.ArrayList;
import java.util.List;
public class BlockChain {
public List<Block> chain;
public BlockChain() {
chain = new ArrayList<>();
public void addBlock(Block block) throws NoSuchAlgorithmException {
block.previousBlockHash = chain.size() == 0 ? CryptoHelper.sha256("0") : chain.get(chain.size()-1).getHash();
public boolean isValid() throws NoSuchAlgorithmException {
for (var i=1; i < chain.size(); ++i) {
var previousBlock = chain.get(i-1);
var currentBlock = chain.get(i);
if (!currentBlock.previousBlockHash.equals(previousBlock.getHash())) {
return false;
return true;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.