blockchain_go icon indicating copy to clipboard operation
blockchain_go copied to clipboard

Sending transaction with mineNow = false.

Open LeeDark opened this issue 7 years ago • 5 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[:]
}

LeeDark avatar Apr 19 '18 21:04 LeeDark

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.

LeeDark avatar Apr 19 '18 21:04 LeeDark

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

fe1t avatar Apr 20 '18 09:04 fe1t

@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.

LeeDark avatar Apr 23 '18 10:04 LeeDark

@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.

LeeDark avatar Apr 23 '18 11:04 LeeDark

@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.

LeeDark avatar Apr 23 '18 12:04 LeeDark