-
Notifications
You must be signed in to change notification settings - Fork 40
/
prepcache.go
192 lines (170 loc) · 4.41 KB
/
prepcache.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
package icstate
import (
"math/big"
"github.com/icon-project/goloop/common"
"github.com/icon-project/goloop/common/containerdb"
"github.com/icon-project/goloop/common/errors"
"github.com/icon-project/goloop/common/log"
"github.com/icon-project/goloop/icon/iiss/icobject"
"github.com/icon-project/goloop/icon/iiss/icutils"
"github.com/icon-project/goloop/module"
"github.com/icon-project/goloop/service/scoredb"
)
var (
prepBaseDictPrefix = containerdb.ToKey(
containerdb.HashBuilder,
scoredb.DictDBPrefix,
"prep_base",
)
prepStatusDictPrefix = containerdb.ToKey(
containerdb.HashBuilder,
scoredb.DictDBPrefix,
"prep_status",
)
)
type PRepBaseCache struct {
bases map[string]*PRepBaseState
dict *containerdb.DictDB
}
func (c *PRepBaseCache) Get(owner module.Address, createIfNotExist bool) *PRepBaseState {
key := icutils.ToKey(owner)
base := c.bases[key]
if base != nil {
return base
}
o := c.dict.Get(owner)
if o == nil {
if createIfNotExist {
base = NewPRepBaseState()
c.bases[key] = base
} else {
// return nil
}
} else {
ps := ToPRepBase(o.Object())
if ps != nil {
base = new(PRepBaseState).Reset(ps)
c.bases[key] = base
}
}
return base
}
func (c *PRepBaseCache) Clear() {
c.Flush()
c.bases = make(map[string]*PRepBaseState)
}
func (c *PRepBaseCache) Reset() {
for key, base := range c.bases {
addr, err := common.NewAddress([]byte(key))
if err != nil {
panic(errors.Errorf("Address convert error"))
}
value := c.dict.Get(addr)
if value == nil {
delete(c.bases, key)
} else {
base.Reset(ToPRepBase(value.Object()))
}
}
}
func (c *PRepBaseCache) Flush() {
for k, base := range c.bases {
key, err := common.BytesToAddress([]byte(k))
if err != nil {
panic(errors.Errorf("PRepBaseCache is broken: %s", k))
}
if base.IsEmpty() {
if err = c.dict.Delete(key); err != nil {
log.Errorf("Failed to delete PRep key %x, err+%+v", key, err)
}
delete(c.bases, k)
} else {
o := icobject.New(TypePRepBase, base.GetSnapshot())
if err := c.dict.Set(key, o); err != nil {
log.Errorf("Failed to set snapshotMap for %x, err+%+v", key, err)
}
}
}
}
func newPRepBaseCache(store containerdb.ObjectStoreState) *PRepBaseCache {
return &PRepBaseCache{
bases: make(map[string]*PRepBaseState),
dict: containerdb.NewDictDB(store, 1, prepBaseDictPrefix),
}
}
// ====================================
type PRepStatusCache struct {
statuses map[string]*PRepStatusState
dict *containerdb.DictDB
illegal *containerdb.DictDB
}
func (c *PRepStatusCache) Get(owner module.Address, createIfNotExist bool) *PRepStatusState {
key := icutils.ToKey(owner)
status := c.statuses[key]
if status != nil {
return status
}
o := c.dict.Get(owner)
if o == nil {
if createIfNotExist {
status = NewPRepStatus()
c.statuses[key] = status
} else {
// return nil
}
} else {
snapshot := ToPRepStatus(o.Object())
if snapshot != nil {
status = NewPRepStatusWithSnapshot(snapshot)
if value := c.illegal.Get(owner); value != nil {
status.SetEffectiveDelegated(new(big.Int).Add(status.Delegated(), value.BigInt()))
}
c.statuses[key] = status
}
}
return status
}
func (c *PRepStatusCache) Clear() {
c.Flush()
c.statuses = make(map[string]*PRepStatusState)
}
func (c *PRepStatusCache) Reset() {
for key, status := range c.statuses {
addr, err := common.NewAddress([]byte(key))
if err != nil {
panic(errors.Errorf("Address convert error"))
}
value := c.dict.Get(addr)
if value == nil {
delete(c.statuses, key)
} else {
status.Reset(ToPRepStatus(value.Object()))
}
}
}
func (c *PRepStatusCache) Flush() {
for k, status := range c.statuses {
key, err := common.BytesToAddress([]byte(k))
if err != nil {
panic(errors.Errorf("PRepStatusCache is broken: %s", k))
}
if status.IsEmpty() {
if err = c.dict.Delete(key); err != nil {
log.Errorf("Failed to delete PRep key %x, err+%+v", key, err)
}
delete(c.statuses, k)
} else {
o := icobject.New(TypePRepStatus, status.GetSnapshot())
if err := c.dict.Set(key, o); err != nil {
log.Errorf("Failed to set snapshotMap for %x, err+%+v", key, err)
}
}
}
}
func newPRepStatusCache(store containerdb.ObjectStoreState) *PRepStatusCache {
return &PRepStatusCache{
statuses: make(map[string]*PRepStatusState),
dict: containerdb.NewDictDB(store, 1, prepStatusDictPrefix),
illegal: containerdb.NewDictDB(store, 1, pRepIllegalDelegatedKey),
}
}