forked from bazo-blockchain/bazo-miner
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathread.go
295 lines (244 loc) · 7.06 KB
/
read.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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
package storage
import (
"github.com/bazo-blockchain/bazo-miner/protocol"
"github.com/boltdb/bolt"
"sort"
)
//Always return nil if requested hash is not in the storage. This return value is then checked against by the caller
func ReadOpenBlock(hash [32]byte) (block *protocol.Block) {
var encodedBlock []byte
db.View(func(tx *bolt.Tx) error {
b := tx.Bucket([]byte("openblocks"))
encodedBlock = b.Get(hash[:])
return nil
})
if encodedBlock == nil {
return nil
}
return block.Decode(encodedBlock)
}
func ReadClosedBlock(hash [32]byte) (block *protocol.Block) {
db.View(func(tx *bolt.Tx) error {
b := tx.Bucket([]byte("closedblocks"))
encodedBlock := b.Get(hash[:])
block = block.Decode(encodedBlock)
return nil
})
if block == nil {
return nil
}
return block
}
//This function does read all blocks without transactions inside.
func ReadClosedBlockWithoutTx(hash [32]byte) (block *protocol.Block) {
db.View(func(tx *bolt.Tx) error {
b := tx.Bucket([]byte("closedblockswithouttx"))
encodedBlock := b.Get(hash[:])
block = block.Decode(encodedBlock)
return nil
})
if block == nil {
return nil
}
return block
}
func ReadLastClosedBlock() (block *protocol.Block) {
db.View(func(tx *bolt.Tx) error {
b := tx.Bucket([]byte("lastclosedblock"))
cb := b.Cursor()
_, encodedBlock := cb.First()
block = block.Decode(encodedBlock)
return nil
})
if block == nil {
return nil
}
return block
}
func ReadAllClosedBlocksWithTransactions() (allClosedBlocks []*protocol.Block) {
//This does return all blocks which are either in closedblocks or closedblockswithouttx bucket of the Database.
//They are not ordered at teh request, but this does actually not matter. Because it will be ordered below
block := ReadLastClosedBlock()
if block != nil {
db.View(func(tx *bolt.Tx) error {
b := tx.Bucket([]byte("closedblocks"))
b.ForEach(func(k, v []byte) error {
if v != nil {
encodedBlock := v
block = block.Decode(encodedBlock)
allClosedBlocks = append(allClosedBlocks, block)
}
return nil
})
return nil
})
}
//blocks are sorted here.
sort.Sort(ByHeight(allClosedBlocks))
return allClosedBlocks
}
func ReadAllClosedFundsAndAggTransactions() (allClosedTransactions []protocol.Transaction) {
var fundsTx protocol.FundsTx
db.View(func(tx *bolt.Tx) error {
b := tx.Bucket([]byte("closedfunds"))
b.ForEach(func(k, v []byte) error {
if v != nil {
encodedFundsTx := v
allClosedTransactions = append(allClosedTransactions, fundsTx.Decode(encodedFundsTx))
}
return nil
})
return nil
})
var aggTx protocol.AggTx
db.View(func(tx *bolt.Tx) error {
b := tx.Bucket([]byte("closedaggregations"))
b.ForEach(func(k, v []byte) error {
if v != nil {
encodedAggTx := v
if encodedAggTx != nil{
allClosedTransactions = append(allClosedTransactions, aggTx.Decode(encodedAggTx))
}
}
return nil
})
return nil
})
return allClosedTransactions
}
//This method does read all blocks in closedBlocks & closedblockswithouttx.
func ReadAllClosedBlocks() (allClosedBlocks []*protocol.Block) {
//This does return all blocks which are either in closedblocks or closedblockswithouttx bucket of the Database.
//They are not ordered at teh request, but this does actually not matter. Because it will be ordered below
block := ReadLastClosedBlock()
if block != nil {
db.View(func(tx *bolt.Tx) error {
b := tx.Bucket([]byte("closedblocks"))
b.ForEach(func(k, v []byte) error {
if v != nil {
encodedBlock := v
block = block.Decode(encodedBlock)
allClosedBlocks = append(allClosedBlocks, block)
}
return nil
})
b = tx.Bucket([]byte("closedblockswithouttx"))
b.ForEach(func(k, v []byte) error {
if v != nil {
encodedBlock := v
block = block.Decode(encodedBlock)
allClosedBlocks = append(allClosedBlocks, block)
}
return nil
})
return nil
})
}
//blocks are sorted here.
sort.Sort(ByHeight(allClosedBlocks))
return allClosedBlocks
}
//The three functions and the type below are used to order the gathered closed blocks from below according to
//their block height.
type ByHeight []*protocol.Block
func (a ByHeight) Len() int { return len(a) }
func (a ByHeight) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
func (a ByHeight) Less(i, j int) bool { return a[i].Height < a[j].Height }
func ReadReceivedBlockStash() (receivedBlocks []*protocol.Block){
return ReceivedBlockStash
}
func ReadOpenTx(hash [32]byte) (transaction protocol.Transaction) {
openTxMutex.Lock()
defer openTxMutex.Unlock()
return txMemPool[hash]
}
func ReadTxcntToTx(txCnt uint32) (transactions [][32]byte) {
txcntToTxMapMutex.Lock()
defer txcntToTxMapMutex.Unlock()
return TxcntToTxMap[txCnt]
}
func ReadFundsTxBeforeAggregation() ([]*protocol.FundsTx) {
openFundsTxBeforeAggregationMutex.Lock()
defer openFundsTxBeforeAggregationMutex.Unlock()
return FundsTxBeforeAggregation
}
func ReadAllBootstrapReceivedTransactions() (allOpenTxs []protocol.Transaction) {
openTxMutex.Lock()
defer openTxMutex.Unlock()
for key := range bootstrapReceivedMemPool {
allOpenTxs = append(allOpenTxs, txMemPool[key])
}
return
}
func ReadINVALIDOpenTx(hash [32]byte) (transaction protocol.Transaction) {
openINVALIDTxMutex.Lock()
defer openINVALIDTxMutex.Unlock()
return txINVALIDMemPool[hash]
}
func ReadAllINVALIDOpenTx() (allOpenInvalidTxs []protocol.Transaction) {
openINVALIDTxMutex.Lock()
defer openINVALIDTxMutex.Unlock()
for key := range txINVALIDMemPool {
allOpenInvalidTxs = append(allOpenInvalidTxs, txINVALIDMemPool[key])
}
return allOpenInvalidTxs
}
//Needed for the miner to prepare a new block
func ReadAllOpenTxs() (allOpenTxs []protocol.Transaction) {
openTxMutex.Lock()
defer openTxMutex.Unlock()
for key := range txMemPool {
allOpenTxs = append(allOpenTxs, txMemPool[key])
}
return
}
//Personally I like it better to test (which tx type it is) here, and get returned the interface. Simplifies the code
func ReadClosedTx(hash [32]byte) (transaction protocol.Transaction) {
var encodedTx []byte
var fundstx *protocol.FundsTx
db.View(func(tx *bolt.Tx) error {
b := tx.Bucket([]byte("closedfunds"))
encodedTx = b.Get(hash[:])
return nil
})
if encodedTx != nil {
return fundstx.Decode(encodedTx)
}
var acctx *protocol.AccTx
db.View(func(tx *bolt.Tx) error {
b := tx.Bucket([]byte("closedaccs"))
encodedTx = b.Get(hash[:])
return nil
})
if encodedTx != nil {
return acctx.Decode(encodedTx)
}
var configtx *protocol.ConfigTx
db.View(func(tx *bolt.Tx) error {
b := tx.Bucket([]byte("closedconfigs"))
encodedTx = b.Get(hash[:])
return nil
})
if encodedTx != nil {
return configtx.Decode(encodedTx)
}
var staketx *protocol.StakeTx
db.View(func(tx *bolt.Tx) error {
b := tx.Bucket([]byte("closedstakes"))
encodedTx = b.Get(hash[:])
return nil
})
if encodedTx != nil {
return staketx.Decode(encodedTx)
}
var aggTx *protocol.AggTx
db.View(func(tx *bolt.Tx) error {
b := tx.Bucket([]byte("closedaggregations"))
encodedTx = b.Get(hash[:])
return nil
})
if encodedTx != nil {
return aggTx.Decode(encodedTx)
}
return nil
}