[英]Hash does not change even when data set is changed to another value
I have created simple blockchain program with java.我用 java 创建了简单的区块链程序。 Currently I'm generating a hash for the transaction dataset.目前我正在为交易数据集生成一个 hash。 When i am changing the dataset for transaction the hash is not getting changed当我更改交易数据集时,hash 没有改变
Firstly I have created a transaction class with transactionName, transactionID and transaction TimeStamp values.首先,我创建了一个事务 class,其中包含事务名称、事务 ID 和事务时间戳值。 Currently transactionDate and transactionID are generated Automatically.目前 transactionDate 和 transactionID 是自动生成的。
Here is the package MainNode.pkg;这是 package MainNode.pkg;
import NodeData.pkg.TransactionClass;
import sun.util.logging.PlatformLogger;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
* @author melanrashitha
* @date 11/12/19
* @project BlockChainArchController
*/
public class MainNode {
private int hash;
private int prevHash;
private TransactionClass[] transactionData;
Logger logger = Logger.getLogger("MainNode");
public MainNode(int prevHash, TransactionClass[] transactionData) {
//Logger MainNode Transaction class init
logger.log(Level.INFO,"Node HB Received");
this.prevHash = prevHash;
logger.log(Level.INFO,"Node prevHash loaded : "+prevHash);
this.transactionData = transactionData;
logger.log(Level.INFO,"Node HB Received");
logger.log(Level.INFO,"Node transactionData loaded : "+transactionData);
//Current Calculated blockhash calculated on current transaction data, prev hashcode
logger.log(Level.INFO,"Node Hash Generating ");
Object[] object = new Object[] {transactionData,prevHash};
this.hash = object.hashCode();
logger.log(Level.INFO,"Node Hash Generated : "+ this.hash);
logger.log(Level.INFO,"Node HB Detached");
}
public int getHash() {
return hash;
}
public int getPrevHash() {
return prevHash;
}
public TransactionClass[] getTransactionData() {
return transactionData;
}
}
When the transaction data is changed it still displays same hash value更改交易数据时,它仍然显示相同的 hash 值
Here are the log messages这是日志消息
/Library/Java/JavaVirtualMachines/jdk1.8.0_212.jdk/Contents/Home/bin/java "-javaagent:/Applications/IntelliJ IDEA.app/Contents/lib/idea_rt.jar=55843:/Applications/IntelliJ IDEA.app/Contents/bin" -Dfile.encoding=UTF-8 -classpath /Library/Java/JavaVirtualMachines/jdk1.8.0_212.jdk/Contents/Home/jre/lib/charsets.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_212.jdk/Contents/Home/jre/lib/deploy.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_212.jdk/Contents/Home/jre/lib/ext/cldrdata.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_212.jdk/Contents/Home/jre/lib/ext/dnsns.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_212.jdk/Contents/Home/jre/lib/ext/jaccess.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_212.jdk/Contents/Home/jre/lib/ext/jfxrt.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_212.jdk/Contents/Home/jre/lib/ext/localedata.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_212.jdk/Contents/Home/jre/lib/ext/nashorn.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_212.jdk/Contents/Home/jre/lib/ext/sunec.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_212.jdk/Contents/Home/jre/lib/ext/sunjce_provider.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_212.jdk/Contents/Home/jre/lib/ext/sunpkcs11.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_212.jdk/Contents/Home/jre/lib/ext/zipfs.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_212.jdk/Contents/Home/jre/lib/javaws.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_212.jdk/Contents/Home/jre/lib/jce.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_212.jdk/Contents/Home/jre/lib/jfr.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_212.jdk/Contents/Home/jre/lib/jfxswt.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_212.jdk/Contents/Home/jre/lib/jsse.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_212.jdk/Contents/Home/jre/lib/management-agent.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_212.jdk/Contents/Home/jre/lib/plugin.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_212.jdk/Contents/Home/jre/lib/resources.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_212.jdk/Contents/Home/jre/lib/rt.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_212.jdk/Contents/Home/lib/ant-javafx.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_212.jdk/Contents/Home/lib/dt.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_212.jdk/Contents/Home/lib/javafx-mx.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_212.jdk/Contents/Home/lib/jconsole.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_212.jdk/Contents/Home/lib/packager.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_212.jdk/Contents/Home/lib/sa-jdi.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_212.jdk/Contents/Home/lib/tools.jar:/Users/melanrashitha/Desktop/Desktop/stripe-payment-gateway-module/BlockChainArchController/out/production/BlockChainArchController MainNode.pkg.BlockChainMain
Nov 12, 2019 11:57:02 AM NodeData.pkg.TransactionClass <init>
INFO: Transaction HB Recieved
Nov 12, 2019 11:57:02 AM NodeData.pkg.TransactionClass setTransactionDate
INFO: Transaction Date Created : 15068497599682
Nov 12, 2019 11:57:02 AM NodeData.pkg.TransactionClass setTransactionID
INFO: TransactionID Created : 64c27bf9-d3e6-48bc-b2b5-d6cc10f62df7
Nov 12, 2019 11:57:02 AM NodeData.pkg.TransactionClass <init>
INFO: Transaction HB Detached
Nov 12, 2019 11:57:02 AM NodeData.pkg.TransactionClass <init>
INFO: Transaction HB Recieved
Nov 12, 2019 11:57:02 AM NodeData.pkg.TransactionClass setTransactionDate
INFO: Transaction Date Created : 15068523666838
Nov 12, 2019 11:57:02 AM NodeData.pkg.TransactionClass setTransactionID
INFO: TransactionID Created : 9796a6c7-9d74-404a-8434-9844c18e481a
Nov 12, 2019 11:57:02 AM NodeData.pkg.TransactionClass <init>
INFO: Transaction HB Detached
Nov 12, 2019 11:57:02 AM NodeData.pkg.TransactionClass <init>
INFO: Transaction HB Recieved
Nov 12, 2019 11:57:02 AM NodeData.pkg.TransactionClass setTransactionDate
INFO: Transaction Date Created : 15068528416745
Nov 12, 2019 11:57:02 AM NodeData.pkg.TransactionClass setTransactionID
INFO: TransactionID Created : 9469aa4e-70fb-4088-9261-a5274f4214fc
Nov 12, 2019 11:57:02 AM NodeData.pkg.TransactionClass <init>
INFO: Transaction HB Detached
Nov 12, 2019 11:57:02 AM NodeData.pkg.TransactionClass <init>
INFO: Transaction HB Recieved
Nov 12, 2019 11:57:02 AM NodeData.pkg.TransactionClass setTransactionDate
INFO: Transaction Date Created : 15068533175911
Nov 12, 2019 11:57:02 AM NodeData.pkg.TransactionClass setTransactionID
INFO: TransactionID Created : 1d179fc7-eb39-4c81-bfed-4afdb8579a8f
Nov 12, 2019 11:57:02 AM NodeData.pkg.TransactionClass <init>
INFO: Transaction HB Detached
Nov 12, 2019 11:57:02 AM MainNode.pkg.MainNode <init>
INFO: Node HB Received
Nov 12, 2019 11:57:02 AM MainNode.pkg.MainNode <init>
INFO: Node prevHash loaded : 1
Nov 12, 2019 11:57:02 AM MainNode.pkg.MainNode <init>
INFO: Node HB Received
Nov 12, 2019 11:57:02 AM MainNode.pkg.MainNode <init>
INFO: Node transactionData loaded : [LNodeData.pkg.TransactionClass;@5caf905d
Nov 12, 2019 11:57:02 AM MainNode.pkg.MainNode <init>
INFO: Node Hash Generating `enter code here`
Nov 12, 2019 11:57:02 AM MainNode.pkg.MainNode <init>
INFO: Node Hash Generated : 41359092
Nov 12, 2019 11:57:02 AM MainNode.pkg.MainNode <init>
INFO: Node HB Detached
The problem is that you are calling hashCode()
on an array, in your case Object[] object
.问题是您在数组上调用hashCode()
,在您的情况下Object[] object
。 And since arrays don't override the hashCode()
method from the Object class you will get the result from the Object.hashCode()
method. And since arrays don't override the hashCode()
method from the Object class you will get the result from the Object.hashCode()
method. This method will not take into account the content of your array.此方法不会考虑您的数组的内容。 So you will get the same hashcode of an array each time in your current JRE.因此,您每次在当前的 JRE 中都会获得相同的数组哈希码。
String[] data = new String[2];
System.out.println(Arrays.toString(data)+" - "+data.hashCode());
data[0] = "abc";
System.out.println(Arrays.toString(data)+" - "+data.hashCode());
data[1] = "def";
System.out.println(Arrays.toString(data)+" - "+data.hashCode());
This will generate an output similar to this:这将生成与此类似的 output:
[null, null] - 705927765
[abc, null] - 705927765
[abc, def] - 705927765
As you see there is no change in the hashcode even thou we change its content.如您所见,即使您更改其内容,哈希码也没有变化。 To fix this problem you use the Arrays.deepHashCode()
method.要解决此问题,请使用Arrays.deepHashCode()
方法。 It will return the hashcode based on the content of the array.它将根据数组的内容返回哈希码。 Try it by running the following code:通过运行以下代码进行尝试:
String[] data = new String[2];
System.out.println(Arrays.toString(data)+" - "+Arrays.deepHashCode(data));
data[0] = "abc";
System.out.println(Arrays.toString(data)+" - "+Arrays.deepHashCode(data));
data[1] = "def";
System.out.println(Arrays.toString(data)+" - "+Arrays.deepHashCode(data));
You will get the following output:您将获得以下 output:
[null, null] - 961
[abc, null] - 2987935
[abc, def] - 3087268
Keep in mind that your TransactionClass
class must override the hashCode()
method to make it work.请记住,您的TransactionClass
class 必须覆盖hashCode()
方法才能使其工作。 Also keep in mind, when you override the hashCode()
method you might need to override the equals()
method as well.另请记住,当您覆盖hashCode()
方法时,您可能还需要覆盖equals()
方法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.