forked from Qortal/qortal
More refactoring
This commit is contained in:
parent
bfc0122c1b
commit
f938d8c878
@ -295,7 +295,7 @@ public class WebsiteResource {
|
||||
byte[] chunkHashes = transactionData.getChunkHashes();
|
||||
|
||||
// Load data file(s)
|
||||
DataFile dataFile = DataFile.fromDigest(digest);
|
||||
DataFile dataFile = DataFile.fromHash(digest);
|
||||
if (!dataFile.exists()) {
|
||||
if (!dataFile.allChunksExist(chunkHashes)) {
|
||||
// TODO: fetch them?
|
||||
|
@ -1,5 +1,7 @@
|
||||
package org.qortal.network.message;
|
||||
|
||||
import org.qortal.transform.transaction.TransactionTransformer;
|
||||
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.UnsupportedEncodingException;
|
||||
@ -7,33 +9,33 @@ import java.nio.ByteBuffer;
|
||||
|
||||
public class GetDataFileMessage extends Message {
|
||||
|
||||
private static final int DIGEST_LENGTH = 32;
|
||||
private static final int HASH_LENGTH = TransactionTransformer.SHA256_LENGTH;
|
||||
|
||||
private final byte[] digest;
|
||||
private final byte[] hash;
|
||||
|
||||
public GetDataFileMessage(byte[] digest) {
|
||||
this(-1, digest);
|
||||
public GetDataFileMessage(byte[] hash) {
|
||||
this(-1, hash);
|
||||
}
|
||||
|
||||
private GetDataFileMessage(int id, byte[] digest) {
|
||||
private GetDataFileMessage(int id, byte[] hash) {
|
||||
super(id, MessageType.GET_DATA_FILE);
|
||||
|
||||
this.digest = digest;
|
||||
this.hash = hash;
|
||||
}
|
||||
|
||||
public byte[] getDigest() {
|
||||
return this.digest;
|
||||
public byte[] getHash() {
|
||||
return this.hash;
|
||||
}
|
||||
|
||||
public static Message fromByteBuffer(int id, ByteBuffer bytes) throws UnsupportedEncodingException {
|
||||
if (bytes.remaining() != DIGEST_LENGTH)
|
||||
if (bytes.remaining() != HASH_LENGTH)
|
||||
return null;
|
||||
|
||||
byte[] digest = new byte[DIGEST_LENGTH];
|
||||
byte[] hash = new byte[HASH_LENGTH];
|
||||
|
||||
bytes.get(digest);
|
||||
bytes.get(hash);
|
||||
|
||||
return new GetDataFileMessage(id, digest);
|
||||
return new GetDataFileMessage(id, hash);
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -41,7 +43,7 @@ public class GetDataFileMessage extends Message {
|
||||
try {
|
||||
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
|
||||
|
||||
bytes.write(this.digest);
|
||||
bytes.write(this.hash);
|
||||
|
||||
return bytes.toByteArray();
|
||||
} catch (IOException e) {
|
||||
|
@ -47,7 +47,7 @@ public class HSQLDBArbitraryRepository implements ArbitraryRepository {
|
||||
byte[] chunkHashes = transactionData.getChunkHashes();
|
||||
|
||||
// Load data file(s)
|
||||
DataFile dataFile = DataFile.fromDigest(digest);
|
||||
DataFile dataFile = DataFile.fromHash(digest);
|
||||
if (chunkHashes.length > 0) {
|
||||
dataFile.addChunkHashes(chunkHashes);
|
||||
}
|
||||
@ -82,7 +82,7 @@ public class HSQLDBArbitraryRepository implements ArbitraryRepository {
|
||||
byte[] chunkHashes = transactionData.getChunkHashes();
|
||||
|
||||
// Load data file(s)
|
||||
DataFile dataFile = DataFile.fromDigest(digest);
|
||||
DataFile dataFile = DataFile.fromHash(digest);
|
||||
if (chunkHashes.length > 0) {
|
||||
dataFile.addChunkHashes(chunkHashes);
|
||||
}
|
||||
@ -167,7 +167,7 @@ public class HSQLDBArbitraryRepository implements ArbitraryRepository {
|
||||
byte[] chunkHashes = arbitraryTransactionData.getChunkHashes();
|
||||
|
||||
// Load data file(s)
|
||||
DataFile dataFile = DataFile.fromDigest(digest);
|
||||
DataFile dataFile = DataFile.fromHash(digest);
|
||||
if (chunkHashes.length > 0) {
|
||||
dataFile.addChunkHashes(chunkHashes);
|
||||
}
|
||||
|
@ -90,8 +90,8 @@ public class DataFile {
|
||||
return new DataFile(hash58);
|
||||
}
|
||||
|
||||
public static DataFile fromDigest(byte[] digest) {
|
||||
return DataFile.fromHash58(Base58.encode(digest));
|
||||
public static DataFile fromHash(byte[] hash) {
|
||||
return DataFile.fromHash58(Base58.encode(hash));
|
||||
}
|
||||
|
||||
public static DataFile fromPath(String path) {
|
||||
@ -100,7 +100,7 @@ public class DataFile {
|
||||
try {
|
||||
byte[] fileContent = Files.readAllBytes(file.toPath());
|
||||
byte[] digest = Crypto.digest(fileContent);
|
||||
DataFile dataFile = DataFile.fromDigest(digest);
|
||||
DataFile dataFile = DataFile.fromHash(digest);
|
||||
|
||||
// Copy file to base directory if needed
|
||||
Path filePath = Paths.get(path);
|
||||
@ -194,6 +194,9 @@ public class DataFile {
|
||||
}
|
||||
|
||||
public void addChunkHashes(byte[] chunks) {
|
||||
if (chunks == null || chunks.length == 0) {
|
||||
return;
|
||||
}
|
||||
ByteBuffer byteBuffer = ByteBuffer.wrap(chunks);
|
||||
while (byteBuffer.remaining() >= TransactionTransformer.SHA256_LENGTH) {
|
||||
byte[] chunkDigest = new byte[TransactionTransformer.SHA256_LENGTH];
|
||||
|
Loading…
x
Reference in New Issue
Block a user