forked from bazo-blockchain/bazo-miner
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathwrite.go
137 lines (110 loc) · 3.45 KB
/
write.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
package storage
import (
"github.com/bazo-blockchain/bazo-miner/protocol"
"github.com/boltdb/bolt"
)
func WriteOpenBlock(block *protocol.Block) (err error) {
err = db.Update(func(tx *bolt.Tx) error {
b := tx.Bucket([]byte("openblocks"))
err := b.Put(block.Hash[:], block.Encode())
return err
})
return err
}
func WriteClosedBlock(block *protocol.Block) (err error) {
err = db.Update(func(tx *bolt.Tx) error {
b := tx.Bucket([]byte("closedblocks"))
err := b.Put(block.Hash[:], block.Encode())
return err
})
return err
}
func WriteClosedBlockWithoutTx(block *protocol.Block) (err error) {
err = db.Update(func(tx *bolt.Tx) error {
b := tx.Bucket([]byte("closedblockswithouttx"))
err := b.Put(block.HashWithoutTx[:], block.Encode())
return err
})
return err
}
func WriteLastClosedBlock(block *protocol.Block) (err error) {
err = db.Update(func(tx *bolt.Tx) error {
b := tx.Bucket([]byte("lastclosedblock"))
err := b.Put(block.Hash[:], block.Encode())
return err
})
return err
}
//Changing the "tx" shortcut here and using "transaction" to distinguish between bolt's transactions
func WriteOpenTx(transaction protocol.Transaction) {
openTxMutex.Lock()
txMemPool[transaction.Hash()] = transaction
switch transaction.(type) {
case *protocol.FundsTx:
WriteTxcntToTx(transaction.(*protocol.FundsTx))
}
openTxMutex.Unlock()
}
func WriteTxcntToTx(transaction *protocol.FundsTx) {
txcntToTxMapMutex.Lock()
TxcntToTxMap[transaction.TxCnt] = append(TxcntToTxMap[transaction.TxCnt], transaction.Hash())
txcntToTxMapMutex.Unlock()
}
func WriteFundsTxBeforeAggregation(transaction *protocol.FundsTx) {
openFundsTxBeforeAggregationMutex.Lock()
FundsTxBeforeAggregation = append(FundsTxBeforeAggregation, transaction)
openFundsTxBeforeAggregationMutex.Unlock()
}
func WriteBootstrapTxReceived(transaction protocol.Transaction) {
bootstrapReceivedMemPool[transaction.Hash()] = transaction
}
func WriteINVALIDOpenTx(transaction protocol.Transaction) {
openINVALIDTxMutex.Lock()
txINVALIDMemPool[transaction.Hash()] = transaction
openINVALIDTxMutex.Unlock()
}
func WriteToReceivedStash(block *protocol.Block) {
ReceivedBlockStashMutex.Lock()
//Only write it to stash if it is not in there already.
if !BlockAlreadyInStash(ReceivedBlockStash, block.Hash) {
ReceivedBlockStash = append(ReceivedBlockStash, block)
//When length of stash is > 100 --> Remove first added Block
if len(ReceivedBlockStash) > 100 {
ReceivedBlockStash = append(ReceivedBlockStash[:0], ReceivedBlockStash[1:]...)
}
}
ReceivedBlockStashMutex.Unlock()
}
func BlockAlreadyInStash(slice []*protocol.Block, newBlockHash [32]byte) bool {
for _, blockInStash := range slice {
if blockInStash.Hash == newBlockHash {
return true
}
}
return false
}
func WriteClosedTx(transaction protocol.Transaction) (err error) {
var bucket string
switch transaction.(type) {
case *protocol.FundsTx:
bucket = "closedfunds"
case *protocol.AccTx:
bucket = "closedaccs"
case *protocol.ConfigTx:
bucket = "closedconfigs"
case *protocol.StakeTx:
bucket = "closedstakes"
case *protocol.AggTx:
bucket = "closedaggregations"
}
hash := transaction.Hash()
err = db.Update(func(tx *bolt.Tx) error {
b := tx.Bucket([]byte(bucket))
err := b.Put(hash[:], transaction.Encode())
return err
})
nrClosedTransactions = nrClosedTransactions + 1
totalTransactionSize = totalTransactionSize + float32(transaction.Size())
averageTxSize = totalTransactionSize/nrClosedTransactions
return err
}