mirror of
https://github.com/Qortal/qortal.git
synced 2025-07-22 20:26:50 +00:00
Hash the current state when creating a patch
This is included in the patch metadata and then validated every time it is rebuilt.
This commit is contained in:
@@ -24,7 +24,7 @@ public class ArbitraryDataCombiner {
|
||||
private Path pathAfter;
|
||||
private byte[] signatureBefore;
|
||||
private Path finalPath;
|
||||
ArbitraryDataMetadataPatch metadata;
|
||||
private ArbitraryDataMetadataPatch metadata;
|
||||
|
||||
public ArbitraryDataCombiner(Path pathBefore, Path pathAfter, byte[] signatureBefore) {
|
||||
this.pathBefore = pathBefore;
|
||||
@@ -39,6 +39,7 @@ public class ArbitraryDataCombiner {
|
||||
this.validatePreviousSignature();
|
||||
this.validatePreviousHash();
|
||||
this.process();
|
||||
this.validateCurrentHash();
|
||||
|
||||
} finally {
|
||||
this.postExecute();
|
||||
@@ -132,6 +133,22 @@ public class ArbitraryDataCombiner {
|
||||
this.finalPath = merge.getMergePath();
|
||||
}
|
||||
|
||||
private void validateCurrentHash() throws IOException {
|
||||
byte[] currentHash = this.metadata.getCurrentHash();
|
||||
if (currentHash == null) {
|
||||
throw new IllegalStateException("Unable to extract current hash from patch metadata");
|
||||
}
|
||||
|
||||
ArbitraryDataDigest digest = new ArbitraryDataDigest(this.finalPath);
|
||||
digest.compute();
|
||||
boolean valid = digest.isHashValid(currentHash);
|
||||
if (!valid) {
|
||||
String currentHash58 = Base58.encode(currentHash);
|
||||
throw new InvalidObjectException(String.format("Current state hash mismatch. " +
|
||||
"Patch curHash: %s, actual: %s", currentHash58, digest.getHash58()));
|
||||
}
|
||||
}
|
||||
|
||||
public Path getFinalPath() {
|
||||
return this.finalPath;
|
||||
}
|
||||
|
@@ -65,6 +65,7 @@ public class ArbitraryDataDiff {
|
||||
private Path pathAfter;
|
||||
private byte[] previousSignature;
|
||||
private byte[] previousHash;
|
||||
private byte[] currentHash;
|
||||
private Path diffPath;
|
||||
private String identifier;
|
||||
|
||||
@@ -92,6 +93,7 @@ public class ArbitraryDataDiff {
|
||||
this.findAddedOrModifiedFiles();
|
||||
this.findRemovedFiles();
|
||||
this.validate();
|
||||
this.hashCurrentState();
|
||||
this.writeMetadata();
|
||||
|
||||
} finally {
|
||||
@@ -272,6 +274,12 @@ public class ArbitraryDataDiff {
|
||||
}
|
||||
}
|
||||
|
||||
private void hashCurrentState() throws IOException {
|
||||
ArbitraryDataDigest digest = new ArbitraryDataDigest(this.pathAfter);
|
||||
digest.compute();
|
||||
this.currentHash = digest.getHash();
|
||||
}
|
||||
|
||||
private void writeMetadata() throws IOException {
|
||||
ArbitraryDataMetadataPatch metadata = new ArbitraryDataMetadataPatch(this.diffPath);
|
||||
metadata.setAddedPaths(this.addedPaths);
|
||||
@@ -279,6 +287,7 @@ public class ArbitraryDataDiff {
|
||||
metadata.setRemovedPaths(this.removedPaths);
|
||||
metadata.setPreviousSignature(this.previousSignature);
|
||||
metadata.setPreviousHash(this.previousHash);
|
||||
metadata.setCurrentHash(this.currentHash);
|
||||
metadata.write();
|
||||
}
|
||||
|
||||
|
@@ -23,6 +23,7 @@ public class ArbitraryDataMetadataPatch extends ArbitraryDataMetadata {
|
||||
private List<Path> removedPaths;
|
||||
private byte[] previousSignature;
|
||||
private byte[] previousHash;
|
||||
private byte[] currentHash;
|
||||
|
||||
public ArbitraryDataMetadataPatch(Path filePath) {
|
||||
super(filePath);
|
||||
@@ -56,6 +57,12 @@ public class ArbitraryDataMetadataPatch extends ArbitraryDataMetadata {
|
||||
this.previousHash = Base58.decode(prevHash);
|
||||
}
|
||||
}
|
||||
if (patch.has("curHash")) {
|
||||
String curHash = patch.getString("curHash");
|
||||
if (curHash != null) {
|
||||
this.currentHash = Base58.decode(curHash);
|
||||
}
|
||||
}
|
||||
if (patch.has("added")) {
|
||||
JSONArray added = (JSONArray) patch.get("added");
|
||||
if (added != null) {
|
||||
@@ -101,6 +108,7 @@ public class ArbitraryDataMetadataPatch extends ArbitraryDataMetadata {
|
||||
|
||||
patch.put("prevSig", Base58.encode(this.previousSignature));
|
||||
patch.put("prevHash", Base58.encode(this.previousHash));
|
||||
patch.put("curHash", Base58.encode(this.currentHash));
|
||||
patch.put("added", new JSONArray(this.addedPaths));
|
||||
patch.put("removed", new JSONArray(this.removedPaths));
|
||||
|
||||
@@ -157,4 +165,12 @@ public class ArbitraryDataMetadataPatch extends ArbitraryDataMetadata {
|
||||
return this.previousHash;
|
||||
}
|
||||
|
||||
public void setCurrentHash(byte[] currentHash) {
|
||||
this.currentHash = currentHash;
|
||||
}
|
||||
|
||||
public byte[] getCurrentHash() {
|
||||
return this.currentHash;
|
||||
}
|
||||
|
||||
}
|
||||
|
Reference in New Issue
Block a user