-
Notifications
You must be signed in to change notification settings - Fork 0
/
stakeext.go
executable file
·244 lines (208 loc) · 7.08 KB
/
stakeext.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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
// Copyright (c) 2013-2014 The btcsuite developers
// Copyright (c) 2015-2018 The Decred developers
// Use of this source code is governed by an ISC
// license that can be found in the LICENSE file.
package blockchain
import (
"fmt"
"github.com/hdfchain/hdfd/chaincfg/chainhash"
"github.com/hdfchain/hdfd/database"
"github.com/hdfchain/hdfd/dcrutil"
"github.com/hdfchain/hdfd/txscript"
)
// NextLotteryData returns the next tickets eligible for spending as SSGen
// on the top block. It also returns the ticket pool size and the PRNG
// state checksum.
//
// This function is safe for concurrent access.
func (b *BlockChain) NextLotteryData() ([]chainhash.Hash, int, [6]byte, error) {
b.chainLock.RLock()
defer b.chainLock.RUnlock()
tipStakeNode := b.bestChain.Tip().stakeNode
return tipStakeNode.Winners(), tipStakeNode.PoolSize(),
tipStakeNode.FinalState(), nil
}
// lotteryDataForNode is a helper function that returns winning tickets
// along with the ticket pool size and PRNG checksum for a given node.
//
// This function is NOT safe for concurrent access and MUST be called
// with the chainLock held for writes.
func (b *BlockChain) lotteryDataForNode(node *blockNode) ([]chainhash.Hash, int, [6]byte, error) {
if node.height < b.chainParams.StakeEnabledHeight {
return nil, 0, [6]byte{}, nil
}
stakeNode, err := b.fetchStakeNode(node)
if err != nil {
return nil, 0, [6]byte{}, err
}
return stakeNode.Winners(), stakeNode.PoolSize(), stakeNode.FinalState(), nil
}
// lotteryDataForBlock takes a node block hash and returns the next tickets
// eligible for voting, the number of tickets in the ticket pool, and the
// final state of the PRNG.
//
// This function is NOT safe for concurrent access and must have the chainLock
// held for write access.
func (b *BlockChain) lotteryDataForBlock(hash *chainhash.Hash) ([]chainhash.Hash, int, [6]byte, error) {
node := b.index.LookupNode(hash)
if node == nil {
return nil, 0, [6]byte{}, fmt.Errorf("block %s is not known", hash)
}
winningTickets, poolSize, finalState, err := b.lotteryDataForNode(node)
if err != nil {
return nil, 0, [6]byte{}, err
}
return winningTickets, poolSize, finalState, nil
}
// LotteryDataForBlock returns lottery data for a given block in the block
// chain, including side chain blocks.
//
// It is safe for concurrent access.
func (b *BlockChain) LotteryDataForBlock(hash *chainhash.Hash) ([]chainhash.Hash, int, [6]byte, error) {
// TODO: An optimization can be added that only calls the read lock if the
// block is not minMemoryStakeNodes blocks before the current best node.
// This is because all the data for these nodes can be assumed to be
// in memory.
b.chainLock.Lock()
winningTickets, poolSize, finalState, err := b.lotteryDataForBlock(hash)
b.chainLock.Unlock()
return winningTickets, poolSize, finalState, err
}
// LiveTickets returns all currently live tickets from the stake database.
//
// This function is NOT safe for concurrent access.
func (b *BlockChain) LiveTickets() ([]chainhash.Hash, error) {
b.chainLock.RLock()
sn := b.bestChain.Tip().stakeNode
b.chainLock.RUnlock()
return sn.LiveTickets(), nil
}
// MissedTickets returns all currently missed tickets from the stake database.
//
// This function is NOT safe for concurrent access.
func (b *BlockChain) MissedTickets() ([]chainhash.Hash, error) {
b.chainLock.RLock()
sn := b.bestChain.Tip().stakeNode
b.chainLock.RUnlock()
return sn.MissedTickets(), nil
}
// TicketsWithAddress returns a slice of ticket hashes that are currently live
// corresponding to the given address.
//
// This function is safe for concurrent access.
func (b *BlockChain) TicketsWithAddress(address dcrutil.Address) ([]chainhash.Hash, error) {
b.chainLock.RLock()
sn := b.bestChain.Tip().stakeNode
b.chainLock.RUnlock()
tickets := sn.LiveTickets()
var ticketsWithAddr []chainhash.Hash
err := b.db.View(func(dbTx database.Tx) error {
for _, hash := range tickets {
utxo, err := dbFetchUtxoEntry(dbTx, &hash)
if err != nil {
return err
}
_, addrs, _, err :=
txscript.ExtractPkScriptAddrs(txscript.DefaultScriptVersion,
utxo.PkScriptByIndex(0), b.chainParams)
if err != nil {
return err
}
if addrs[0].EncodeAddress() == address.EncodeAddress() {
ticketsWithAddr = append(ticketsWithAddr, hash)
}
}
return nil
})
if err != nil {
return nil, err
}
return ticketsWithAddr, nil
}
// CheckLiveTicket returns whether or not a ticket exists in the live ticket
// treap of the best node.
//
// This function is safe for concurrent access.
func (b *BlockChain) CheckLiveTicket(hash chainhash.Hash) bool {
b.chainLock.RLock()
sn := b.bestChain.Tip().stakeNode
b.chainLock.RUnlock()
return sn.ExistsLiveTicket(hash)
}
// CheckLiveTickets returns whether or not a slice of tickets exist in the live
// ticket treap of the best node.
//
// This function is safe for concurrent access.
func (b *BlockChain) CheckLiveTickets(hashes []chainhash.Hash) []bool {
b.chainLock.RLock()
sn := b.bestChain.Tip().stakeNode
b.chainLock.RUnlock()
existsSlice := make([]bool, len(hashes))
for i := range hashes {
existsSlice[i] = sn.ExistsLiveTicket(hashes[i])
}
return existsSlice
}
// CheckMissedTickets returns a slice of bools representing whether each ticket
// hash has been missed in the live ticket treap of the best node.
//
// This function is safe for concurrent access.
func (b *BlockChain) CheckMissedTickets(hashes []chainhash.Hash) []bool {
b.chainLock.RLock()
sn := b.bestChain.Tip().stakeNode
b.chainLock.RUnlock()
existsSlice := make([]bool, len(hashes))
for i := range hashes {
existsSlice[i] = sn.ExistsMissedTicket(hashes[i])
}
return existsSlice
}
// CheckExpiredTicket returns whether or not a ticket was ever expired.
//
// This function is safe for concurrent access.
func (b *BlockChain) CheckExpiredTicket(hash chainhash.Hash) bool {
b.chainLock.RLock()
sn := b.bestChain.Tip().stakeNode
b.chainLock.RUnlock()
return sn.ExistsExpiredTicket(hash)
}
// CheckExpiredTickets returns whether or not a ticket in a slice of
// tickets was ever expired.
//
// This function is safe for concurrent access.
func (b *BlockChain) CheckExpiredTickets(hashes []chainhash.Hash) []bool {
b.chainLock.RLock()
sn := b.bestChain.Tip().stakeNode
b.chainLock.RUnlock()
existsSlice := make([]bool, len(hashes))
for i := range hashes {
existsSlice[i] = sn.ExistsExpiredTicket(hashes[i])
}
return existsSlice
}
// TicketPoolValue returns the current value of all the locked funds in the
// ticket pool.
//
// This function is safe for concurrent access. All live tickets are at least
// 256 blocks deep on mainnet, so the UTXO set should generally always have
// the asked for transactions.
func (b *BlockChain) TicketPoolValue() (dcrutil.Amount, error) {
b.chainLock.RLock()
sn := b.bestChain.Tip().stakeNode
b.chainLock.RUnlock()
var amt int64
err := b.db.View(func(dbTx database.Tx) error {
for _, hash := range sn.LiveTickets() {
utxo, err := dbFetchUtxoEntry(dbTx, &hash)
if err != nil {
return err
}
amt += utxo.sparseOutputs[0].amount
}
return nil
})
if err != nil {
return 0, err
}
return dcrutil.Amount(amt), nil
}