-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathblockchain_manager.go
188 lines (153 loc) · 4.65 KB
/
blockchain_manager.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
package kcoin
import (
"crypto/ecdsa"
"encoding/hex"
"errors"
"log"
)
type BlockchainManager struct {
blockchain *Blockchain
}
func NewBlockchainManager(blockchain *Blockchain) *BlockchainManager {
return &BlockchainManager{
blockchain: blockchain,
}
}
func (bManager *BlockchainManager) SendCoin(wallet Wallet, toAddress []byte, amount int) {
tx, err := bManager.NewUTXOTransaction(wallet, toAddress, amount)
if err != nil {
log.Println(err)
return
}
bManager.blockchain.AddBlock([]*Transaction{tx})
}
func (bManager *BlockchainManager) NewUTXOTransaction(
wallet Wallet, toAddress []byte, amount int) (*Transaction, error) {
if bManager.GetBalance(wallet.GetAddress()) < amount {
return nil, errors.New("ERROR: Not enough funds")
}
var inputs []TransactionInput
accumulated := 0
txToUnspentOutputIndexs := bManager.FindUnspentTransactions(wallet.GetAddress())
for tx, unspentIndexes := range txToUnspentOutputIndexs {
newInputs, newAccumulated := makeInputsFromUnspentTxOutputs(
wallet.PrivateKey.PublicKey, amount, *tx, unspentIndexes, accumulated)
inputs = append(inputs, newInputs...)
accumulated = newAccumulated
if accumulated >= amount {
break
}
}
outputs := makeOutputsForUTXOTransaction(
wallet.GetAddress(), toAddress, amount, accumulated-amount)
txManager := NewTransactionManager(NewTransaction(inputs, outputs))
txManager.Sign(wallet.PrivateKey)
return txManager.tx, nil
}
func (bManager *BlockchainManager) GetBalance(address []byte) int {
balance := 0
txToUnspentOutputIndexs := bManager.FindUnspentTransactions(address)
for tx, unspentIndexs := range txToUnspentOutputIndexs {
for outpuIndex, output := range tx.Outputs {
if containsUint(unspentIndexs, uint(outpuIndex)) {
balance += output.Value
}
}
}
return balance
}
func (bManager *BlockchainManager) FindUnspentTransactions(address []byte) map[*Transaction][]uint {
txToUnspentOutputIndexs := make(map[*Transaction][]uint)
txIDToSpentOutputIndexes := make(map[string][]uint)
blockchainIterator := bManager.blockchain.Iterator()
for {
block := blockchainIterator.Next()
for _, tx := range block.Transactions {
unspentTxOutputs := findTransactionUnspentOutputIndexes(
address, *tx, txIDToSpentOutputIndexes)
txToUnspentOutputIndexs[tx] = append(
txToUnspentOutputIndexs[tx],
unspentTxOutputs...)
for _, input := range tx.Inputs {
if isInputSpent(address, input) && !tx.IsCoinbase() {
inputTxID := hex.EncodeToString(input.TxID)
txIDToSpentOutputIndexes[inputTxID] = append(
txIDToSpentOutputIndexes[inputTxID],
uint(input.OutputIndex))
}
}
}
if len(block.PrevBlockHash) == 0 {
break
}
}
return txToUnspentOutputIndexs
}
func findTransactionUnspentOutputIndexes(
address []byte,
transaction Transaction,
txIDToSpentOutputIndexes map[string][]uint) []uint {
var unspentOutputIndexes []uint
txID := hex.EncodeToString(transaction.ID)
for outputIndex, output := range transaction.Outputs {
if !containsUint(txIDToSpentOutputIndexes[txID], uint(outputIndex)) {
pubKeyHash, _ := getPubKeyHashFromAddress(address)
if output.IsLockedWithKey(pubKeyHash) {
unspentOutputIndexes = append(unspentOutputIndexes, uint(outputIndex))
}
}
}
return unspentOutputIndexes
}
func isInputSpent(address []byte, input TransactionInput) bool {
pubKeyHash, _ := getPubKeyHashFromAddress(address)
if input.UsesKey(pubKeyHash) {
return true
}
return false
}
func makeInputsFromUnspentTxOutputs(
pubKey ecdsa.PublicKey,
amount int,
tx Transaction,
unspentIndexes []uint,
accumulated int) ([]TransactionInput, int) {
var resultInputs []TransactionInput
resultAccumulated := accumulated
for outputIndex, output := range tx.Outputs {
if containsUint(unspentIndexes, uint(outputIndex)) {
newInput := TransactionInput{
TxID: tx.ID,
OutputIndex: outputIndex,
XYAppendedPubKey: getXYAppendedPubKey(pubKey),
XYAppendedSignature: nil,
}
resultInputs = append(resultInputs, newInput)
resultAccumulated += output.Value
}
if resultAccumulated >= amount {
return resultInputs, resultAccumulated
}
}
return resultInputs, resultAccumulated
}
func makeOutputsForUTXOTransaction(
fromAddress, toAddress []byte,
amount, exchange int) []TransactionOutput {
toPubKey, _ := getPubKeyHashFromAddress(toAddress)
resultOutputs := []TransactionOutput{
TransactionOutput{
Value: amount,
PubKeyHash: toPubKey,
},
}
fromPubKey, _ := getPubKeyHashFromAddress(fromAddress)
if exchange != 0 {
resultOutputs = append(
resultOutputs, TransactionOutput{
Value: exchange,
PubKeyHash: fromPubKey,
})
}
return resultOutputs
}