-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutxo.go
155 lines (131 loc) · 3.2 KB
/
utxo.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
package blc
import "sync"
type UTXOSet struct {
utxoSet map[[32]byte]map[int]TxOutput // TxHash => Index => TxOut
Size int // UTXO 个数
locker sync.RWMutex
}
func NewUTXOSet() *UTXOSet {
return &UTXOSet{
utxoSet: map[[32]byte]map[int]TxOutput{},
Size: 0,
locker: sync.RWMutex{},
}
}
func (s *UTXOSet) Traverse(fn func(txHash [32]byte, index int, output TxOutput)) {
s.locker.RLock()
defer s.locker.RUnlock()
for txHash, outputs := range s.utxoSet {
for index, output := range outputs {
fn(txHash, index, output)
}
}
}
func (s *UTXOSet) Put(txHash [32]byte, index int, output TxOutput) {
s.locker.Lock()
defer s.locker.Unlock()
if _, ok := s.utxoSet[txHash]; !ok {
s.utxoSet[txHash] = make(map[int]TxOutput)
}
s.utxoSet[txHash][index] = output
s.Size++
}
func (s *UTXOSet) Remove(txHash [32]byte, index int) {
if !s.Exist(txHash, index) {
return
}
s.locker.Lock()
defer s.locker.Unlock()
delete(s.utxoSet[txHash], index)
if len(s.utxoSet[txHash]) == 0 {
delete(s.utxoSet, txHash)
}
s.Size--
}
func (s *UTXOSet) Get(txHash [32]byte, index int) *TxOutput {
if !s.Exist(txHash, index) {
return nil
}
s.locker.RLock()
out := s.utxoSet[txHash][index]
s.locker.RUnlock()
return &out
}
func (s *UTXOSet) Has(txHash [32]byte) bool {
s.locker.RLock()
_, ok := s.utxoSet[txHash]
s.locker.RUnlock()
return ok
}
func (s *UTXOSet) Exist(txHash [32]byte, index int) bool {
s.locker.RLock()
defer s.locker.RUnlock()
if _, ok := s.utxoSet[txHash]; !ok {
return false
}
if _, ok := s.utxoSet[txHash][index]; !ok {
return false
}
return true
}
func (chain *BlockChain) resetUTXOSet() error {
chain.utxoSet = NewUTXOSet()
return chain.updateUTXOSet()
}
// updateUTXOSet 更新 utxo 集合
func (chain *BlockChain) updateUTXOSet() error {
if chain.utxoSet == nil {
chain.utxoSet = NewUTXOSet()
}
spent := make(map[[32]byte]map[uint32]struct{})
// log.Debug("[blockchain utxo set]")
err := chain.Traverse(func(block *Block) bool {
for _, tx := range block.Transactions {
// 交易输入
for _, vin := range tx.Vins {
if vin.IsCoinbase() {
continue
}
if _, ok := spent[vin.TxId]; !ok {
spent[vin.TxId] = make(map[uint32]struct{})
}
spent[vin.TxId][vin.Vout] = struct{}{}
}
// 交易输出
for i, out := range tx.Vouts {
if v, ok := spent[tx.Hash]; ok {
if _, ok = v[uint32(i)]; ok {
// 被花费
delete(spent[tx.Hash], uint32(i))
continue
}
}
// log.Debugf("txid:%x\tindex:%d\taddress:%s\tvalue:%d", tx.Hash, i, out.Address, out.Value)
chain.utxoSet.Put(tx.Hash, i, out)
}
}
return true
})
if err != nil {
return err
}
return nil
}
func (chain *BlockChain) GetBalance(address string) uint64 {
utxoSet := chain.GetUTXOSet(address)
var amount uint64 = 0
utxoSet.Traverse(func(txHash [32]byte, index int, output TxOutput) {
amount += output.Value
})
return amount
}
// GetUTXOSet 获取UTXO集合
func (chain *BlockChain) GetUTXOSet(address string) *UTXOSet {
utxoSet := NewUTXOSet()
chain.utxoSet.Traverse(func(txHash [32]byte, index int, output TxOutput) {
if output.Address == address {
utxoSet.Put(txHash, index, output)
}
})
return utxoSet
}