/
store_epoch.go
163 lines (132 loc) · 3.55 KB
/
store_epoch.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
package gossip
/*
In LRU cache data stored like pointer
*/
import (
"errors"
"github.com/Fantom-foundation/lachesis-ex/hash"
"github.com/Fantom-foundation/lachesis-ex/inter"
"github.com/Fantom-foundation/lachesis-ex/inter/idx"
"github.com/Fantom-foundation/lachesis-ex/kvdb"
"github.com/Fantom-foundation/lachesis-ex/kvdb/skiperrors"
"github.com/Fantom-foundation/lachesis-ex/kvdb/table"
)
type (
epochStore struct {
Headers kvdb.KeyValueStore `table:"h"`
Tips kvdb.KeyValueStore `table:"t"`
Heads kvdb.KeyValueStore `table:"H"`
}
)
func newEpochStore(db kvdb.KeyValueStore) *epochStore {
es := &epochStore{}
table.MigrateTables(es, db)
err := errors.New("database closed")
es.Headers = skiperrors.Wrap(es.Headers, err)
es.Tips = skiperrors.Wrap(es.Tips, err)
es.Heads = skiperrors.Wrap(es.Heads, err)
return es
}
// getEpochStore is not safe for concurrent use.
func (s *Store) getEpochStore(epoch idx.Epoch) *epochStore {
tables := s.EpochDbs.Get(uint64(epoch))
if tables == nil {
return nil
}
return tables.(*epochStore)
}
// delEpochStore is not safe for concurrent use.
func (s *Store) delEpochStore(epoch idx.Epoch) {
s.EpochDbs.Del(uint64(epoch))
// Clear full LRU cache.
if s.cache.EventsHeaders != nil {
s.cache.EventsHeaders.Purge()
}
}
// SetLastEvent stores last unconfirmed event from a validator (off-chain)
func (s *Store) SetLastEvent(epoch idx.Epoch, from idx.StakerID, id hash.Event) {
es := s.getEpochStore(epoch)
if es == nil {
return
}
key := from.Bytes()
if err := es.Tips.Put(key, id.Bytes()); err != nil {
s.Log.Crit("Failed to put key-value", "err", err)
}
}
// GetLastEvent returns stored last unconfirmed event from a validator (off-chain)
func (s *Store) GetLastEvent(epoch idx.Epoch, from idx.StakerID) *hash.Event {
es := s.getEpochStore(epoch)
if es == nil {
return nil
}
key := from.Bytes()
idBytes, err := es.Tips.Get(key)
if err != nil {
s.Log.Crit("Failed to get key-value", "err", err)
}
if idBytes == nil {
return nil
}
id := hash.BytesToEvent(idBytes)
return &id
}
// SetEventHeader returns stored event header.
func (s *Store) SetEventHeader(epoch idx.Epoch, h hash.Event, e *inter.EventHeaderData) {
es := s.getEpochStore(epoch)
if es == nil {
return
}
key := h.Bytes()
s.set(es.Headers, key, e)
// Save to LRU cache.
if e != nil && s.cache.EventsHeaders != nil {
s.cache.EventsHeaders.Add(h, e)
}
}
// GetEventHeader returns stored event header.
func (s *Store) GetEventHeader(epoch idx.Epoch, h hash.Event) *inter.EventHeaderData {
key := h.Bytes()
// Check LRU cache first.
if s.cache.EventsHeaders != nil {
if v, ok := s.cache.EventsHeaders.Get(h); ok {
if w, ok := v.(*inter.EventHeaderData); ok {
return w
}
}
}
es := s.getEpochStore(epoch)
if es == nil {
return nil
}
w, _ := s.get(es.Headers, key, &inter.EventHeaderData{}).(*inter.EventHeaderData)
// Save to LRU cache.
if w != nil && s.cache.EventsHeaders != nil {
s.cache.EventsHeaders.Add(h, w)
}
return w
}
// HasEvent returns true if event exists.
func (s *Store) HasEventHeader(h hash.Event) bool {
es := s.getEpochStore(h.Epoch())
if es == nil {
return false
}
return s.has(es.Headers, h.Bytes())
}
// DelEventHeader removes stored event header.
func (s *Store) DelEventHeader(epoch idx.Epoch, h hash.Event) {
es := s.getEpochStore(epoch)
if es == nil {
return
}
key := h.Bytes()
err := es.Headers.Delete(key)
if err != nil {
s.Log.Crit("Failed to delete key", "err", err)
}
// Remove from LRU cache.
if s.cache.EventsHeaders != nil {
s.cache.EventsHeaders.Remove(h)
}
}