forked from ava-labs/avalanchego
-
Notifications
You must be signed in to change notification settings - Fork 0
/
state.go
144 lines (114 loc) · 3.4 KB
/
state.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
// (c) 2019-2020, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package state
import (
"github.com/ava-labs/gecko/cache"
"github.com/ava-labs/gecko/database"
"github.com/ava-labs/gecko/ids"
"github.com/ava-labs/gecko/snow/choices"
"github.com/ava-labs/gecko/utils/formatting"
"github.com/ava-labs/gecko/utils/hashing"
"github.com/ava-labs/gecko/utils/wrappers"
)
type state struct {
serializer *Serializer
dbCache cache.Cacher
db database.Database
}
func (s *state) Vertex(id ids.ID) *vertex {
if vtxIntf, found := s.dbCache.Get(id); found {
vtx, _ := vtxIntf.(*vertex)
return vtx
}
if b, err := s.db.Get(id.Bytes()); err == nil {
// The key was in the database
if vtx, err := s.serializer.parseVertex(b); err == nil {
s.dbCache.Put(id, vtx) // Cache the element
return vtx
}
s.serializer.ctx.Log.Error("Parsing failed on saved vertex.\nPrefixed key = %s\nBytes = %s",
id,
formatting.DumpBytes{Bytes: b})
}
s.dbCache.Put(id, nil) // Cache the miss
return nil
}
func (s *state) SetVertex(id ids.ID, vtx *vertex) {
s.dbCache.Put(id, vtx)
if vtx == nil {
s.db.Delete(id.Bytes())
return
}
s.db.Put(id.Bytes(), vtx.bytes)
}
func (s *state) Status(id ids.ID) choices.Status {
if statusIntf, found := s.dbCache.Get(id); found {
status, _ := statusIntf.(choices.Status)
return status
}
if b, err := s.db.Get(id.Bytes()); err == nil {
// The key was in the database
p := wrappers.Packer{Bytes: b}
status := choices.Status(p.UnpackInt())
if p.Offset == len(b) && !p.Errored() {
s.dbCache.Put(id, status)
return status
}
s.serializer.ctx.Log.Error("Parsing failed on saved status.\nPrefixed key = %s\nBytes = \n%s",
id,
formatting.DumpBytes{Bytes: b})
}
s.dbCache.Put(id, choices.Unknown)
return choices.Unknown
}
func (s *state) SetStatus(id ids.ID, status choices.Status) {
s.dbCache.Put(id, status)
if status == choices.Unknown {
s.db.Delete(id.Bytes())
return
}
p := wrappers.Packer{Bytes: make([]byte, 4)}
p.PackInt(uint32(status))
s.serializer.ctx.Log.AssertNoError(p.Err)
s.serializer.ctx.Log.AssertTrue(p.Offset == len(p.Bytes), "Wrong offset after packing")
s.db.Put(id.Bytes(), p.Bytes)
}
func (s *state) Edge(id ids.ID) []ids.ID {
if frontierIntf, found := s.dbCache.Get(id); found {
frontier, _ := frontierIntf.([]ids.ID)
return frontier
}
if b, err := s.db.Get(id.Bytes()); err == nil {
p := wrappers.Packer{Bytes: b}
frontier := []ids.ID{}
for i := p.UnpackInt(); i > 0 && !p.Errored(); i-- {
id, _ := ids.ToID(p.UnpackFixedBytes(hashing.HashLen))
frontier = append(frontier, id)
}
if p.Offset == len(b) && !p.Errored() {
s.dbCache.Put(id, frontier)
return frontier
}
s.serializer.ctx.Log.Error("Parsing failed on saved ids.\nPrefixed key = %s\nBytes = %s",
id,
formatting.DumpBytes{Bytes: b})
}
s.dbCache.Put(id, nil) // Cache the miss
return nil
}
func (s *state) SetEdge(id ids.ID, frontier []ids.ID) {
s.dbCache.Put(id, frontier)
if len(frontier) == 0 {
s.db.Delete(id.Bytes())
return
}
size := wrappers.IntLen + hashing.HashLen*len(frontier)
p := wrappers.Packer{Bytes: make([]byte, size)}
p.PackInt(uint32(len(frontier)))
for _, id := range frontier {
p.PackFixedBytes(id.Bytes())
}
s.serializer.ctx.Log.AssertNoError(p.Err)
s.serializer.ctx.Log.AssertTrue(p.Offset == len(p.Bytes), "Wrong offset after packing")
s.db.Put(id.Bytes(), p.Bytes)
}