0

I wanted to get started with blockchain development and I know how it works but for some strange reasons to me, when I make a change in previous transactions hash value doesn't change in the previous block either in next block.

I have tried adding a few more elements in the transaction but it still doesn't change.

This is Program.cs file

class Program
{
    static void Main(string[] args)
    {
        string[] genesisTransaction = { "This is genesis transaction", "Satoshi sent 10 bitcoins to Teufik" };
        Block genesisBlock = new Block(0, genesisTransaction);

        string[] firstTransaction = { "This is first transaction","Teufik sent 5 bitcoins to Tamara" };
        Block firstBlock = new Block(genesisBlock.GetBlockHash(), firstTransaction);

        Console.Write("Genesis block: ");
        Console.WriteLine(genesisBlock.GetBlockHash());
        Console.Write("First block: ");
        Console.WriteLine(firstBlock.GetBlockHash());
    }
}

This is Block class

class Block
{
    private int blockHash;
    private int previousHash;
    private string[] transactions;

    public Block(int previousHash, string[] transactions)
    {
        this.previousHash = previousHash;
        this.transactions = transactions;
        object[] contents = { transactions.GetHashCode(), previousHash };
        blockHash = contents.GetHashCode();
    }

    public int GetBlockHash() => blockHash;
    public int GetPreviousHash() => previousHash;
    public string[] GetTransactions() => transactions;

}

The actual result should be: By adding/deleting even one character hash value will give a different value

  • Is the `GetHashCode` .NET internal one? That doesn’t actually calculate a hash based on the contents so it can’t be used here – Sami Kuhmonen Sep 01 '19 at 09:33
  • I see, thank you but how I can make it external? I mean considering a code, the code is valid. What do you suggest to change? Should I override GetHashCode() method? – Teufik Cajlakovic Sep 01 '19 at 09:45
  • You should build an actual hashing mechanism that hashes the contents, like a blockchain requires. You can’t override the method for an `object[]` nor should you since it might be confusing. Just create a method that does the hashing. – Sami Kuhmonen Sep 01 '19 at 10:00

0 Answers0