Melan Rashitha
Melan Rashitha

Reputation: 504

Hash does not change even when data set is changed to another value

I have created simple blockchain program with java. Currently I'm generating a hash for the transaction dataset. When i am changing the dataset for transaction the hash is not getting changed

Firstly I have created a transaction class with transactionName, transactionID and transaction TimeStamp values. Currently transactionDate and transactionID are generated Automatically.

Here is the 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

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

Upvotes: 2

Views: 494

Answers (1)

Progman
Progman

Reputation: 19570

The problem is that you are calling hashCode() on an array, in your case 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. 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.

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:

[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. 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:

[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. Also keep in mind, when you override the hashCode() method you might need to override the equals() method as well.

Upvotes: 4

Related Questions