forked from hacash/miner
-
Notifications
You must be signed in to change notification settings - Fork 0
/
store.go
170 lines (139 loc) · 4.42 KB
/
store.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
package minerrelayservice
import (
"bytes"
"encoding/binary"
"fmt"
"github.com/hacash/chain/leveldb"
"github.com/hacash/core/fields"
"github.com/hacash/core/sys"
"github.com/hacash/miner/message"
"os"
)
const KeyAutoIdxUserMiningResult = "autoidx1"
// Initialize storage
func (api *RelayService) initStore() {
if api.config.StoreEnable == false {
return
}
// initialization
absDataDir := sys.AbsDir(api.config.DataDir)
err := os.MkdirAll(absDataDir, os.ModePerm)
if err != nil {
fmt.Println("[Miner Relay Service] initStore ERROR:")
fmt.Println(err)
return
}
// create db
ldb, e2 := leveldb.OpenFile(absDataDir, nil)
if e2 != nil {
fmt.Println("[Miner Relay Service] leveldb.OpenFile ERROR:")
fmt.Println(e2)
return
}
api.ldb = ldb
// Read auto incrementing index
idx1bts, e3 := ldb.Get([]byte(KeyAutoIdxUserMiningResult), nil)
if e3 != nil {
idx1bts = []byte{0, 0, 0, 0, 0, 0, 0, 0}
}
idx1 := binary.BigEndian.Uint64(idx1bts)
// Auto increment index
api.userMiningResultStoreAutoIdx = idx1
}
// Save user mining statistics
func (api *RelayService) saveMiningResultToStore(rwdaddr fields.Address, isMintSuccessed bool, resultStuff *message.MsgPendingMiningBlockStuff) {
//fmt.Println("saveMiningResultToStore start")
// Serial save
api.userMiningResultStoreAutoIdxMutex.Lock()
defer api.userMiningResultStoreAutoIdxMutex.Unlock()
if api.ldb == nil || api.config.StoreEnable == false {
return
}
//fmt.Println("NewStoreItemUserMiningResultV0")
// Component storage unit
stoitem := NewStoreItemUserMiningResultV0()
if isMintSuccessed {
stoitem.IsMintSuccessed = 1
}
if api.config.SaveMiningHash {
stoitem.IsSaveMiningResultHash = 1
stoitem.MiningResultHash = resultStuff.BlockHeadMeta.HashFresh()
}
if api.config.SaveMiningNonce {
stoitem.IsSaveMiningResultNonce = 1
stoitem.MiningResultHeadNonce = resultStuff.BlockHeadMeta.GetNonceByte()
stoitem.MiningResultCoinbaseNonce = resultStuff.CoinbaseTx.MinerNonce
}
stobts := stoitem.Serialize()
// key
kob1 := fields.BlockHeight(resultStuff.BlockHeadMeta.GetHeight())
heikey, _ := kob1.Serialize()
keybuf := bytes.NewBuffer(heikey)
keybuf.Write(rwdaddr)
// save item
keybts := keybuf.Bytes()
k1 := []byte("mr" + string(keybts))
// check k1
_, e1 := api.ldb.Get(k1, nil)
if e1 == nil {
//fmt.Println("if e1 == nil { api.ldb.Put(k1, stobts, nil)")
// If it already exists, you only need to replace it
api.ldb.Put(k1, stobts, nil) // mr
return // Replace and return
}
//fmt.Println("api.ldb.Put(k1, stobts, nil)")
// Save new
api.ldb.Put(k1, stobts, nil) // mr
// save idx
api.userMiningResultStoreAutoIdx += 1
kob2 := fields.VarUint5(api.userMiningResultStoreAutoIdx)
idxkey, _ := kob2.Serialize()
//fmt.Println("mri"+string(idxkey), `api.ldb.Put([]byte("mri"+string(idxkey)), keybts, nil) // mri`)
api.ldb.Put([]byte("mri"+string(idxkey)), keybts, nil) // mri
// idx
kob3 := fields.VarUint8(api.userMiningResultStoreAutoIdx)
idxkeyauto, _ := kob3.Serialize()
//fmt.Println(KeyAutoIdxUserMiningResult, `api.ldb.Put([]byte(KeyAutoIdxUserMiningResult), idxkeyauto, nil)`)
api.ldb.Put([]byte(KeyAutoIdxUserMiningResult), idxkeyauto, nil)
// save ok
}
// Storage of mining stuffs
func (api *RelayService) saveMiningBlockStuffToStore(stuff *message.MsgPendingMiningBlockStuff) {
if api.ldb == nil || api.config.StoreEnable == false || api.config.SaveMiningBlockStuff == false {
return
}
// Storage
blkhei := stuff.BlockHeadMeta.GetHeight()
stodatas := stuff.Serialize()
k1 := fields.BlockHeight(blkhei)
heikey, _ := k1.Serialize()
// save
err := api.ldb.Put(heikey, stodatas, nil)
if err != nil {
fmt.Println("[Miner Relay Service] saveMiningBlockStuffToStore ERROR:")
fmt.Println(err)
}
}
// Read and store mining stuff
func (api *RelayService) readMiningBlockStuffFormStore(blkhei uint64) *message.MsgPendingMiningBlockStuff {
if api.ldb == nil || api.config.StoreEnable == false || api.config.SaveMiningBlockStuff == false {
return nil
}
// Storage
k1 := fields.BlockHeight(blkhei)
heikey, _ := k1.Serialize()
// save
stodatas, err := api.ldb.Get(heikey, nil)
if err != nil {
fmt.Println("[Miner Relay Service] readMiningBlockStuffFormStore ERROR:")
fmt.Println(err)
}
var stuff = message.MsgPendingMiningBlockStuff{}
_, e2 := stuff.Parse(stodatas, 0)
if e2 != nil {
fmt.Println("[Miner Relay Service] MsgPendingMiningBlockStuff.Parse ERROR:")
fmt.Println(e2)
}
// ok
return &stuff
}