我用Java创建了简单的区块链程序。目前,我正在为交易数据集生成哈希。当我更改交易数据集时,哈希值不会更改
首先,我创建了一个带有transactionName,transactionID和transaction TimeStamp值的事务类。当前transactionDate和transactionID是自动生成的。
这是软件包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;
}
}
更改交易数据后,它仍显示相同的哈希 值
以下是日志消息
/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
答案 0 :(得分:2)
问题是您要在数组hashCode()
上调用Object[] object
。并且由于数组不会覆盖Object类中的hashCode()
方法,因此您将从Object.hashCode()
方法中获取结果。此方法将不考虑数组的内容。因此,您每次在当前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());
这将生成类似于以下内容的输出:
[null, null] - 705927765
[abc, null] - 705927765
[abc, def] - 705927765
如您所见,即使您更改其哈希码,哈希码也没有变化。要解决此问题,请使用Arrays.deepHashCode()
方法。它将基于数组的内容返回哈希码。通过运行以下代码来尝试:
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));
您将获得以下输出:
[null, null] - 961
[abc, null] - 2987935
[abc, def] - 3087268
请记住,您的TransactionClass
类必须重写hashCode()
方法以使其起作用。另外请记住,当您覆盖hashCode()
方法时,您可能还需要覆盖equals()
方法。