forked from decred/dcrdata
-
Notifications
You must be signed in to change notification settings - Fork 0
/
blockmemdb.go
162 lines (142 loc) · 3.69 KB
/
blockmemdb.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
package main
// BlockDataToMemdb satisfies BlockDataSaver interface by implementing the
// Store(data *blockdata.BlockData) method. It saves the data in memory with a map. All
// the data is destroyed on exit. Something like this is needed that uses a
// durable database (e.g. MySQL).
import (
"sync"
"github.com/decred/dcrd/dcrjson"
"github.com/decred/dcrd/wire"
apitypes "github.com/decred/dcrdata/v4/api/types"
"github.com/decred/dcrdata/v4/blockdata"
)
// BlockDataToMemdb models the block data and block data basic as maps
type BlockDataToMemdb struct {
mtx *sync.Mutex
Height int
blockDataMap map[int]*blockdata.BlockData
blockSummaryMap map[int]*apitypes.BlockDataBasic
}
// NewBlockDataToMemdb returns a new BlockDataToMemdb
func NewBlockDataToMemdb(m ...*sync.Mutex) *BlockDataToMemdb {
if len(m) > 1 {
panic("Too many inputs.")
}
saver := &BlockDataToMemdb{
Height: -1,
blockDataMap: make(map[int]*blockdata.BlockData),
blockSummaryMap: make(map[int]*apitypes.BlockDataBasic),
}
if len(m) > 0 {
saver.mtx = m[0]
}
return saver
}
// Store writes blockdata.BlockData to memdb
func (s *BlockDataToMemdb) Store(data *blockdata.BlockData, _ *wire.MsgBlock) error {
if s.mtx != nil {
s.mtx.Lock()
defer s.mtx.Unlock()
}
s.Height = int(data.Header.Height)
// save data to slice in memory
s.blockDataMap[s.Height] = data
blockSummary := data.ToBlockSummary()
s.blockSummaryMap[s.Height] = &blockSummary
return nil
}
// GetHeight returns the blockdata height
func (s *BlockDataToMemdb) GetHeight() int {
return s.Height
}
// Get returns blockdata for block idx
func (s *BlockDataToMemdb) Get(idx int) *blockdata.BlockData {
if idx < 0 {
return nil
}
if s.mtx != nil {
s.mtx.Lock()
defer s.mtx.Unlock()
}
return s.blockDataMap[idx]
}
// GetHeader returns the block header for block idx
func (s *BlockDataToMemdb) GetHeader(idx int) *dcrjson.GetBlockHeaderVerboseResult {
if idx < 0 {
return nil
}
if s.mtx != nil {
s.mtx.Lock()
defer s.mtx.Unlock()
}
blockdata, ok := s.blockDataMap[idx]
if !ok {
return nil
}
return &blockdata.Header
}
// GetFeeInfo returns the fee info for block idx
func (s *BlockDataToMemdb) GetFeeInfo(idx int) *dcrjson.FeeInfoBlock {
if idx < 0 {
return nil
}
if s.mtx != nil {
s.mtx.Lock()
defer s.mtx.Unlock()
}
blockdata, ok := s.blockDataMap[idx]
if !ok {
return nil
}
return &blockdata.FeeInfo
}
// GetStakeDiffEstimate returns the stake difficulty estimates for block idx
func (s *BlockDataToMemdb) GetStakeDiffEstimate(idx int) *dcrjson.EstimateStakeDiffResult {
if idx < 0 {
return nil
}
if s.mtx != nil {
s.mtx.Lock()
defer s.mtx.Unlock()
}
blockdata, ok := s.blockDataMap[idx]
if !ok {
return nil
}
return &blockdata.EstStakeDiff
}
// GetStakeInfoExtendedByHeight returns the stake info for block idx
func (s *BlockDataToMemdb) GetStakeInfoExtendedByHeight(idx int) *apitypes.StakeInfoExtended {
if idx < 0 {
return nil
}
if s.mtx != nil {
s.mtx.Lock()
defer s.mtx.Unlock()
}
blockdata, ok := s.blockDataMap[idx]
if !ok {
return nil
}
stakeinfo := blockdata.ToStakeInfoExtended()
return &stakeinfo
}
// GetBestBlock returns the best block
func (s *BlockDataToMemdb) GetBestBlock() *blockdata.BlockData {
return s.Get(s.Height)
}
// GetSummary returns the block data summary for block idx
func (s *BlockDataToMemdb) GetSummary(idx int) *apitypes.BlockDataBasic {
if idx < 0 {
return nil
}
if s.mtx != nil {
s.mtx.Lock()
defer s.mtx.Unlock()
}
return s.blockSummaryMap[idx]
}
// GetBestBlockSummary returns the block data summary for the best block
func (s *BlockDataToMemdb) GetBestBlockSummary() *apitypes.BlockDataBasic {
return s.GetSummary(s.Height)
}