Created
May 14, 2019 15:37
-
-
Save cyberpirate92/9504c62249cbd574bc4abaf012c4783d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.security.NoSuchAlgorithmException; | |
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(); | |
chain.add(block); | |
} | |
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