/
fastscan.go
194 lines (174 loc) · 6.45 KB
/
fastscan.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
189
190
191
192
193
194
package wallet
import (
"fmt"
"time"
"github.com/HyperspaceApp/Hyperspace/build"
"github.com/HyperspaceApp/Hyperspace/modules"
"github.com/HyperspaceApp/Hyperspace/persist"
siasync "github.com/HyperspaceApp/Hyperspace/sync"
"github.com/HyperspaceApp/Hyperspace/types"
)
var errMaxKeys = fmt.Errorf("refused to generate more than %v keys from seed", maxScanKeys)
// maxScanKeys is the number of maximum number of keys the seedScanner will
// generate before giving up.
var maxScanKeys = func() uint64 {
switch build.Release {
case "dev":
return 1e6
case "standard":
return 100e6
case "testing":
return 100e3
default:
panic("unrecognized build.Release")
}
}()
// A scannedOutput is an output found in the blockchain that was generated
// from a given seed.
type scannedOutput struct {
id types.OutputID
value types.Currency
seedIndex uint64
}
// A seedScanner scans the blockchain for addresses that belong to a given
// seed.
type seedScanner struct {
dustThreshold types.Currency // minimum value of outputs to be included
keys map[types.UnlockHash]uint64 // map address to seed index
keysArray [][]byte
minimumIndex uint64 // minimum index from which keys should start
maximumInternalIndex uint64 // the next internal index we can use
maximumExternalIndex uint64 // the next external address we look for
seed modules.Seed
addressGapLimit uint64
siacoinOutputs map[types.SiacoinOutputID]scannedOutput
cs modules.ConsensusSet
walletStopChan <-chan struct{}
log *persist.Logger
}
func (s seedScanner) getMaximumExternalIndex() uint64 {
return s.maximumExternalIndex
}
// func (s seedScanner) getMaximumInternalIndex() uint64 {
// return s.maximumInternalIndex
// }
func (s *seedScanner) setDustThreshold(d types.Currency) {
s.dustThreshold = d
}
func (s seedScanner) getSiacoinOutputs() map[types.SiacoinOutputID]scannedOutput {
return s.siacoinOutputs
}
func (s seedScanner) numKeys() uint64 {
return uint64(len(s.keys))
}
// generateKeys generates n additional keys from the seedScanner's seed.
func (s *seedScanner) generateKeys(n uint64) {
initialProgress := s.numKeys()
for i, k := range generateKeys(s.seed, initialProgress+s.minimumIndex, n) {
s.keys[k.UnlockConditions.UnlockHash()] = initialProgress + s.minimumIndex + uint64(i)
u := k.UnlockConditions.UnlockHash()
s.keysArray = append(s.keysArray, u[:])
// log.Printf("index: %d\n", s.keys[k.UnlockConditions.UnlockHash()])
}
s.maximumInternalIndex += n
}
// ProcessHeaderConsensusChange match consensus change headers with generated seeds
// It needs to look for two types new outputs:
//
// 1) Delayed outputs that have matured during this block. These outputs come
// attached to the HeaderConsensusChange via the output diff.
//
// 2) Fresh outputs that were created and activated during this block. If the
// current block contains these outputs, the header filter will match the wallet's
// keys.
//
// In a full node, we read the block directly from the consensus db and grab the
// outputs from the block output diff.
func (s *seedScanner) ProcessHeaderConsensusChange(hcc modules.HeaderConsensusChange) {
siacoinOutputDiffs, err := hcc.FetchSpaceCashOutputDiffs(s.keysArray)
for err != nil {
s.log.Severe("ERROR: failed to fetch space cash outputs:", err)
if err == siasync.ErrStopped {
return
}
select {
case <-s.walletStopChan:
return
case <-time.After(50 * time.Millisecond):
break // will not go out of forloop
}
siacoinOutputDiffs, err = hcc.FetchSpaceCashOutputDiffs(s.keysArray)
}
// apply the aggregated output diffs
for _, diff := range siacoinOutputDiffs {
if diff.Direction == modules.DiffApply {
if index, exists := s.keys[diff.SiacoinOutput.UnlockHash]; exists && diff.SiacoinOutput.Value.Cmp(s.dustThreshold) > 0 {
// log.Printf("fast DiffApply %d: %s %s\n", index, diff.SiacoinOutput.UnlockHash, diff.SiacoinOutput.Value.HumanString())
s.siacoinOutputs[diff.ID] = scannedOutput{
id: types.OutputID(diff.ID),
value: diff.SiacoinOutput.Value,
seedIndex: index,
}
}
} else if diff.Direction == modules.DiffRevert {
// NOTE: DiffRevert means the output was either spent or was in a
// block that was reverted.
if _, exists := s.keys[diff.SiacoinOutput.UnlockHash]; exists {
// log.Printf("fast DiffRevert %d: %s %s\n", index, diff.SiacoinOutput.UnlockHash, diff.SiacoinOutput.Value.HumanString())
delete(s.siacoinOutputs, diff.ID)
}
}
}
for _, diff := range siacoinOutputDiffs {
index, exists := s.keys[diff.SiacoinOutput.UnlockHash]
if exists {
s.log.Debugln("Seed scanner found a key used at index", index)
// log.Println("Fast Seed scanner found a key used at index", index)
if index >= s.maximumExternalIndex {
s.maximumExternalIndex = index + 1
s.adjustKeys()
}
}
}
}
func (s *seedScanner) adjustKeys() {
gap := s.maximumInternalIndex - s.maximumExternalIndex
if gap > 0 {
toGrow := s.addressGapLimit - gap
s.generateKeys(toGrow)
}
// log.Printf("Update maximumExternalIndex: %d,maximumInternalIndex: %d, totalKeys: %d", s.maximumExternalIndex, s.maximumInternalIndex, len(s.keys))
}
// scan subscribes s to cs and scans the blockchain for addresses that belong
// to s's seed. If scan returns errMaxKeys, additional keys may need to be
// generated to find all the addresses.
func (s *seedScanner) scan(cancel <-chan struct{}) error {
s.walletStopChan = cancel
numKeys := uint64(s.addressGapLimit)
s.generateKeys(numKeys)
if err := s.cs.HeaderConsensusSetSubscribe(s, modules.ConsensusChangeBeginning, cancel); err != nil {
return err
}
s.cs.HeaderUnsubscribe(s)
// log.Printf("scan s.maximumExternalIndex %d\n", s.maximumExternalIndex)
// for id, sco := range s.siacoinOutputs {
// log.Printf("scan siacoinOutputs: %d %s", sco.seedIndex, sco.value.String())
// s.siacoinOutputs[id] = sco
// }
return nil
}
// newSeedScanner returns a new seedScanner.
func newFastSeedScanner(seed modules.Seed, addressGapLimit uint64,
cs modules.ConsensusSet, log *persist.Logger) *seedScanner {
return &seedScanner{
seed: seed,
addressGapLimit: addressGapLimit,
minimumIndex: 0,
maximumInternalIndex: 0,
maximumExternalIndex: 0,
keys: make(map[types.UnlockHash]uint64, numInitialKeys),
siacoinOutputs: make(map[types.SiacoinOutputID]scannedOutput),
cs: cs,
log: log,
}
}