Rename Transaction.GetHash to SetID; add Transaction.ID field
This commit is contained in:
parent
326ecb828c
commit
32dd771eef
2
block.go
2
block.go
|
@ -36,7 +36,7 @@ func (b *Block) HashTransactions() []byte {
|
||||||
var txHash [32]byte
|
var txHash [32]byte
|
||||||
|
|
||||||
for _, tx := range b.Transactions {
|
for _, tx := range b.Transactions {
|
||||||
txHashes = append(txHashes, tx.GetHash())
|
txHashes = append(txHashes, tx.ID)
|
||||||
}
|
}
|
||||||
txHash = sha256.Sum256(bytes.Join(txHashes, []byte{}))
|
txHash = sha256.Sum256(bytes.Join(txHashes, []byte{}))
|
||||||
|
|
||||||
|
|
|
@ -70,7 +70,7 @@ func (bc *Blockchain) FindUnspentTransactions(address string) []*Transaction {
|
||||||
block := bci.Next()
|
block := bci.Next()
|
||||||
|
|
||||||
for _, tx := range block.Transactions {
|
for _, tx := range block.Transactions {
|
||||||
txID := hex.EncodeToString(tx.GetHash())
|
txID := hex.EncodeToString(tx.ID)
|
||||||
|
|
||||||
Outputs:
|
Outputs:
|
||||||
for outIdx, out := range tx.Vout {
|
for outIdx, out := range tx.Vout {
|
||||||
|
@ -115,7 +115,7 @@ func (bc *Blockchain) FindUTXOs(address string, amount int) (int, map[string][]i
|
||||||
|
|
||||||
Work:
|
Work:
|
||||||
for _, tx := range unspentTXs {
|
for _, tx := range unspentTXs {
|
||||||
txID := hex.EncodeToString(tx.GetHash())
|
txID := hex.EncodeToString(tx.ID)
|
||||||
|
|
||||||
for outIdx, out := range tx.Vout {
|
for outIdx, out := range tx.Vout {
|
||||||
if out.CanBeUnlockedWith(address) && accumulated < amount {
|
if out.CanBeUnlockedWith(address) && accumulated < amount {
|
||||||
|
|
|
@ -13,6 +13,7 @@ const subsidy = 10
|
||||||
|
|
||||||
// Transaction represents a Bitcoin transaction
|
// Transaction represents a Bitcoin transaction
|
||||||
type Transaction struct {
|
type Transaction struct {
|
||||||
|
ID []byte
|
||||||
Vin []TXInput
|
Vin []TXInput
|
||||||
Vout []TXOutput
|
Vout []TXOutput
|
||||||
}
|
}
|
||||||
|
@ -22,8 +23,8 @@ func (tx Transaction) IsCoinbase() bool {
|
||||||
return len(tx.Vin) == 1 && len(tx.Vin[0].Txid) == 0 && tx.Vin[0].Vout == -1
|
return len(tx.Vin) == 1 && len(tx.Vin[0].Txid) == 0 && tx.Vin[0].Vout == -1
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetHash hashes the transaction and returns the hash
|
// SetID sets ID of a transaction
|
||||||
func (tx Transaction) GetHash() []byte {
|
func (tx Transaction) SetID() {
|
||||||
var encoded bytes.Buffer
|
var encoded bytes.Buffer
|
||||||
var hash [32]byte
|
var hash [32]byte
|
||||||
|
|
||||||
|
@ -33,8 +34,7 @@ func (tx Transaction) GetHash() []byte {
|
||||||
log.Panic(err)
|
log.Panic(err)
|
||||||
}
|
}
|
||||||
hash = sha256.Sum256(encoded.Bytes())
|
hash = sha256.Sum256(encoded.Bytes())
|
||||||
|
tx.ID = hash[:]
|
||||||
return hash[:]
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// TXInput represents a transaction input
|
// TXInput represents a transaction input
|
||||||
|
@ -68,7 +68,8 @@ func NewCoinbaseTX(to, data string) *Transaction {
|
||||||
|
|
||||||
txin := TXInput{[]byte{}, -1, data}
|
txin := TXInput{[]byte{}, -1, data}
|
||||||
txout := TXOutput{subsidy, to}
|
txout := TXOutput{subsidy, to}
|
||||||
tx := Transaction{[]TXInput{txin}, []TXOutput{txout}}
|
tx := Transaction{nil, []TXInput{txin}, []TXOutput{txout}}
|
||||||
|
tx.SetID()
|
||||||
|
|
||||||
return &tx
|
return &tx
|
||||||
}
|
}
|
||||||
|
@ -103,7 +104,8 @@ func NewUTXOTransaction(from, to string, value int, bc *Blockchain) *Transaction
|
||||||
outputs = append(outputs, TXOutput{acc - value, from}) // a change
|
outputs = append(outputs, TXOutput{acc - value, from}) // a change
|
||||||
}
|
}
|
||||||
|
|
||||||
tx := Transaction{inputs, outputs}
|
tx := Transaction{nil, inputs, outputs}
|
||||||
|
tx.SetID()
|
||||||
|
|
||||||
return &tx
|
return &tx
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue