I'm reading part 4, and I think if txid is not unique for this tutorial. Txid should be unique for blockchain. Otherwise, you can't lookup each transaction by txid.
The logic to generate txid. It generates the same id for newly mined coin:
|
func NewCoinbaseTX(to, data string) *Transaction { |
|
if data == "" { |
|
data = fmt.Sprintf("Reward to '%s'", to) |
|
} |
|
|
|
txin := TXInput{[]byte{}, -1, data} |
|
txout := TXOutput{subsidy, to} |
|
tx := Transaction{nil, []TXInput{txin}, []TXOutput{txout}} |
|
tx.SetID() |
the FindUnspentTransactions would go wrong:
|
if spentTXOs[txID] != nil { |
|
for _, spentOut := range spentTXOs[txID] { |
|
if spentOut == outIdx { |
|
continue Outputs |
|
} |
for example, could you mine 2 new coins with duplicated txid by calling NewCoinbaseTX("same_miner", "")
Then one coin could be used and the other is not. The above FindUnspentTransactions logic can't distinguish those 2 coins.
Even if you add rand in part 6, it still has a chance of colliding.
|
func NewCoinbaseTX(to, data string) *Transaction { |
|
if data == "" { |
|
randData := make([]byte, 20) |
|
_, err := rand.Read(randData) |
|
if err != nil { |
|
log.Panic(err) |
|
} |
|
|
|
data = fmt.Sprintf("%x", randData) |
|
} |
|
|
|
txin := TXInput{[]byte{}, -1, nil, []byte(data)} |
|
txout := NewTXOutput(subsidy, to) |
|
tx := Transaction{nil, []TXInput{txin}, []TXOutput{*txout}} |
|
tx.ID = tx.Hash() |
Any suggestion to make sure that the txid is unique?
I'm reading part 4, and I think if
txidis not unique for this tutorial.Txidshould be unique for blockchain. Otherwise, you can't lookup each transaction bytxid.The logic to generate
txid. It generates the same id for newly minedcoin:blockchain_go/transaction.go
Lines 64 to 72 in e17722e
the FindUnspentTransactions would go wrong:
blockchain_go/blockchain.go
Lines 78 to 82 in e17722e
for example, could you mine 2 new coins with duplicated
txidby callingNewCoinbaseTX("same_miner", "")Then one coin could be used and the other is not. The above
FindUnspentTransactionslogic can't distinguish those 2 coins.Even if you add
randin part 6, it still has a chance of colliding.blockchain_go/transaction.go
Lines 174 to 188 in 0538111
Any suggestion to make sure that the
txidis unique?