-
Notifications
You must be signed in to change notification settings - Fork 28
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Feature/revision tracking #41
Open
JH456
wants to merge
15
commits into
ra4king:master
Choose a base branch
from
JH456:feature/revision-tracking
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
e4d5d71
made CircuitFile maintain a save history
JH456 34aefb9
broke backwards compatibility with older versions
JH456 47effba
added function header for verifying history and made error message mo…
JH456 9aa9742
implemented simple blockchain for save history
JH456 41dbe6a
fixed verification issues with blockchain
JH456 a76fde0
added copy tracking
JH456 e0cbe37
reduced duplication in copiedBlocks
JH456 690e685
made pasting now also grabs a random block
JH456 80537c1
made file hash based only on actual file contents
JH456 6533e68
fixed build
JH456 4bb3c53
made copiedBlocks copy from parsed
JH456 eb8957e
added verification
JH456 105c24f
cleaned up a bit
JH456 c7c1328
Update src/com/ra4king/circuitsim/gui/file/FileFormat.java
ra4king a3810db
Update src/com/ra4king/circuitsim/gui/file/FileFormat.java
ra4king File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,6 +6,8 @@ | |
import java.io.FileWriter; | ||
import java.io.IOException; | ||
import java.io.Reader; | ||
import java.security.MessageDigest; | ||
import java.util.Base64; | ||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
@@ -48,20 +50,145 @@ public static void writeFile(File file, String contents) throws IOException { | |
writer.write('\n'); | ||
} | ||
} | ||
|
||
|
||
private static String sha256ify(String input) { | ||
// Shamelessly stolen from: | ||
// https://medium.com/programmers-blockchain/create-simple-blockchain-java-tutorial-from-scratch-6eeed3cb03fa | ||
try { | ||
MessageDigest digest = MessageDigest.getInstance("SHA-256"); | ||
//Applies sha256 to our input, | ||
byte[] hash = digest.digest(input.getBytes("UTF-8")); | ||
StringBuffer hexString = new StringBuffer(); // This will contain hash as hexidecimal | ||
for (int i = 0; i < hash.length; i++) { | ||
String hex = Integer.toHexString(0xff & hash[i]); | ||
if(hex.length() == 1) hexString.append('0'); | ||
hexString.append(hex); | ||
} | ||
return hexString.toString(); | ||
} | ||
catch(Exception e) { | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
|
||
private static class RevisionSignatureBlock { | ||
private String currentHash; | ||
private String previousHash; | ||
private String fileDataHash; | ||
private String timeStamp; | ||
private String copiedBlocks; | ||
|
||
private RevisionSignatureBlock(String stringifiedBlock) { | ||
String decodedBlock = new String(Base64.getDecoder().decode(stringifiedBlock.getBytes())); | ||
String[] fields = decodedBlock.split("\t"); | ||
if (fields.length < 4) { | ||
throw new NullPointerException("File is corrupted. Contact Course Staff for Assistance."); | ||
} else { | ||
this.previousHash = fields[0]; | ||
this.currentHash = fields[1]; | ||
this.timeStamp = fields[2]; | ||
this.fileDataHash = fields[3]; | ||
this.copiedBlocks = ""; | ||
for (int i = 4; i < fields.length; i++) { | ||
this.copiedBlocks += "\t" + fields[i]; | ||
} | ||
} | ||
} | ||
|
||
private RevisionSignatureBlock(String previousHash, String fileDataHash, List<String> copiedBlocks) { | ||
this.previousHash = previousHash; | ||
this.fileDataHash = fileDataHash; | ||
this.timeStamp = "" + System.currentTimeMillis(); | ||
this.copiedBlocks = ""; | ||
for (String hash : copiedBlocks) { | ||
this.copiedBlocks += "\t" + hash; | ||
} | ||
this.currentHash = this.hash(); | ||
} | ||
|
||
private String hash() { | ||
return FileFormat.sha256ify(previousHash + fileDataHash + timeStamp + this.copiedBlocks); | ||
} | ||
|
||
private String stringify() { | ||
// Lack of a tab between fileDataHash and copiedBlocks is intentional. copiedBlocks starts with a tab. | ||
String stringifiedBlock = Base64.getEncoder().encodeToString( | ||
String.format("%s\t%s\t%s\t%s%s", previousHash, currentHash, timeStamp, fileDataHash, | ||
copiedBlocks).getBytes()); | ||
return stringifiedBlock; | ||
} | ||
} | ||
|
||
private static String getLastHash(List<String> revisionSignatures) { | ||
if (revisionSignatures.size() == 0) { | ||
return ""; | ||
} else { | ||
RevisionSignatureBlock tailBlock = | ||
new RevisionSignatureBlock(revisionSignatures.get(revisionSignatures.size() - 1)); | ||
return tailBlock.currentHash; | ||
} | ||
} | ||
|
||
public static class CircuitFile { | ||
private final String version = CircuitSim.VERSION; | ||
|
||
public final int globalBitSize; | ||
public final int clockSpeed; | ||
public final List<String> libraryPaths; | ||
public final List<CircuitInfo> circuits; | ||
public final List<String> revisionSignatures; | ||
private List<String> copiedBlocks; | ||
|
||
public CircuitFile(int globalBitSize, int clockSpeed, List<String> libraryPaths, List<CircuitInfo> circuits) { | ||
public CircuitFile(int globalBitSize, int clockSpeed, List<String> libraryPaths, List<CircuitInfo> circuits, | ||
List<String> revisionSignatures, List<String> copiedBlocks) { | ||
this.globalBitSize = globalBitSize; | ||
this.clockSpeed = clockSpeed; | ||
this.libraryPaths = libraryPaths; | ||
this.circuits = circuits; | ||
this.revisionSignatures = revisionSignatures; | ||
this.copiedBlocks = copiedBlocks; | ||
} | ||
|
||
|
||
private String hash() { | ||
String fileData = GSON.toJson(libraryPaths) | ||
+ GSON.toJson(circuits); | ||
return FileFormat.sha256ify(fileData); | ||
} | ||
|
||
public void addRevisionSignatureBlock() { | ||
String currentFileDataHash = hash(); | ||
String previousHash = FileFormat.getLastHash(revisionSignatures); | ||
RevisionSignatureBlock newBlock = | ||
new RevisionSignatureBlock(previousHash, currentFileDataHash, copiedBlocks); | ||
revisionSignatures.add(newBlock.stringify()); | ||
this.copiedBlocks = null; | ||
} | ||
|
||
public boolean revisionSignaturesAreValid() { | ||
if (revisionSignatures == null || revisionSignatures.size() < 1) { | ||
return false; | ||
} | ||
String expectedFileDataHash = this.hash(); | ||
RevisionSignatureBlock lastBlock = | ||
new RevisionSignatureBlock(this.revisionSignatures.get(this.revisionSignatures.size() - 1)); | ||
String actualFileDataHash = lastBlock.fileDataHash; | ||
if (!actualFileDataHash.equals(expectedFileDataHash) || !lastBlock.currentHash.equals(lastBlock.hash())) { | ||
return false; | ||
} | ||
String[] blocks = this.revisionSignatures.toArray(new String[]{}); | ||
for (int i = blocks.length - 1; i > 0; i--) { | ||
RevisionSignatureBlock block = new RevisionSignatureBlock(blocks[i]); | ||
RevisionSignatureBlock prevBlock = new RevisionSignatureBlock(blocks[i - 1]); | ||
if (!block.currentHash.equals(block.hash()) || !block.previousHash.equals(prevBlock.currentHash)) { | ||
return false; | ||
} | ||
} | ||
return new RevisionSignatureBlock(blocks[0]).previousHash.equals(""); | ||
} | ||
|
||
public List<String> getCopiedBlocks() { | ||
return this.copiedBlocks; | ||
} | ||
} | ||
|
||
|
@@ -114,6 +241,7 @@ public WireInfo(int x, int y, int length, boolean isHorizontal) { | |
} | ||
|
||
public static void save(File file, CircuitFile circuitFile) throws IOException { | ||
circuitFile.addRevisionSignatureBlock(); | ||
writeFile(file, stringify(circuitFile)); | ||
} | ||
|
||
|
@@ -122,7 +250,11 @@ public static String stringify(CircuitFile circuitFile) { | |
} | ||
|
||
public static CircuitFile load(File file) throws IOException { | ||
return parse(readFile(file)); | ||
CircuitFile savedFile = parse(readFile(file)); | ||
if (!savedFile.revisionSignaturesAreValid()) { | ||
throw new NullPointerException("File is corrupted. Contact Course Staff for Assistance."); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ditto |
||
} | ||
return savedFile; | ||
} | ||
|
||
public static CircuitFile parse(String contents) { | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't like "Contact course staff for assistance" because I want to keep course-specific content out of this. Perhaps: "File hash mismatch: file is corrupted."