Jeiwan / blockchain_go

A simplified blockchain implementation in Golang

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Sending transaction with mineNow = false.

LeeDark opened this issue · comments

If you try to send transaction with mineNow = false from same wallet (address) 2 times, you got 2 transactions with same transaction hash, because NewUTXOTransaction will create identical Transaction structs.
My solution works with adding some salt:

// Hash returns the hash of the Transaction
func (tx *Transaction) Hash() []byte {
	var hash [32]byte

	txCopy := *tx
	txCopy.ID = []byte{}

	salt := make([]byte, 32)
	_, err := io.ReadFull(rand.Reader, salt)
	if err != nil {
		fmt.Printf("ERROR: Creating salt failed: %s\n", err)
	}

	data := txCopy.Serialize()
	data = append(data, salt...)

	hash = sha256.Sum256(data)

	return hash[:]
}

Also this case creates block with 2 transactions (after I fixed Hash() function), but the second one didnt change Sender output Value.
For example Sender had 1000 coins, Receiver had 0 coins. After sending 100 coins two times first transaction will decrease Sender output Value to 900, and increase Receiver output Value to 100, and second transaction will decrease Sender output Value to 900, and increase Receiver output Value to 100, but must be like this: will decrease Sender output Value to 800, and increase Receiver output Value to 200.
Have you an idea how to fix it? I think it is MineBlock() function issue or handleTx() function issue.

commented

I'm stuck at this problem too.
If you manage to solve the problem, you can also fix this issue automatically #23.

@fe1t Yes, I will investigate the core of this problem. I did not fork this repo, I created my own repo based on this tutorial with my features. So perhaps, I will fork this repo and create PRs.

@fe1t I will add Timestamp field to Transaction. Then I will fix handleTx() in case if len(mempool) >= 2. I think that we should recheck transactions (txs) that were created before by some algorithm.

@fe1t Unfortunately this bug is huge, we should also fix Sign/Verify when rechecking transactions, also we cant Find or Verify transaction before adding block with them, because they are not present in the blockchain. So now I just fix len(mempool) >= 1, but it is workaround just for simple testing.