Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to make transaction id unique? #84

Open
guihao-liang opened this issue Nov 21, 2022 · 1 comment
Open

How to make transaction id unique? #84

guihao-liang opened this issue Nov 21, 2022 · 1 comment

Comments

@guihao-liang
Copy link

guihao-liang commented Nov 21, 2022

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?

@Anmilover
Copy link

Anmilover commented Nov 21, 2022 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants