-
Notifications
You must be signed in to change notification settings - Fork 657
/
state.go
156 lines (127 loc) · 3.74 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
145
146
147
148
149
150
151
152
153
154
155
156
// Copyright (C) 2019-2022, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package state
import (
"go.uber.org/zap"
"github.com/ava-labs/avalanchego/cache"
"github.com/ava-labs/avalanchego/database"
"github.com/ava-labs/avalanchego/ids"
"github.com/ava-labs/avalanchego/snow/choices"
"github.com/ava-labs/avalanchego/snow/engine/avalanche/vertex"
"github.com/ava-labs/avalanchego/utils/hashing"
"github.com/ava-labs/avalanchego/utils/logging"
"github.com/ava-labs/avalanchego/utils/wrappers"
)
type state struct {
serializer *Serializer
log logging.Logger
dbCache cache.Cacher
db database.Database
}
// Vertex retrieves the vertex with the given id from cache/disk.
// Returns nil if it's not found.
// TODO this should return an error
func (s *state) Vertex(id ids.ID) vertex.StatelessVertex {
var (
vtx vertex.StatelessVertex
bytes []byte
err error
)
if vtxIntf, found := s.dbCache.Get(id); found {
vtx, _ = vtxIntf.(vertex.StatelessVertex)
return vtx
}
if bytes, err = s.db.Get(id[:]); err != nil {
s.log.Verbo("failed to get vertex from database",
zap.Binary("key", id[:]),
zap.Error(err),
)
s.dbCache.Put(id, nil)
return nil
}
if vtx, err = s.serializer.parseVertex(bytes); err != nil {
s.log.Error("failed parsing saved vertex",
zap.Binary("key", id[:]),
zap.Binary("vertex", bytes),
zap.Error(err),
)
s.dbCache.Put(id, nil)
return nil
}
s.dbCache.Put(id, vtx)
return vtx
}
// SetVertex persists the vertex to the database and returns an error if it
// fails to write to the db
func (s *state) SetVertex(id ids.ID, vtx vertex.StatelessVertex) error {
s.dbCache.Put(id, vtx)
if vtx == nil {
return s.db.Delete(id[:])
}
return s.db.Put(id[:], 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 val, err := database.GetUInt32(s.db, id[:]); err == nil {
// The key was in the database
status := choices.Status(val)
s.dbCache.Put(id, status)
return status
}
s.dbCache.Put(id, choices.Unknown)
return choices.Unknown
}
// SetStatus sets the status of the vertex and returns an error if it fails to write to the db
func (s *state) SetStatus(id ids.ID, status choices.Status) error {
s.dbCache.Put(id, status)
if status == choices.Unknown {
return s.db.Delete(id[:])
}
return database.PutUInt32(s.db, id[:], uint32(status))
}
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[:]); err == nil {
p := wrappers.Packer{Bytes: b}
frontierSize := p.UnpackInt()
frontier := make([]ids.ID, frontierSize)
for i := 0; i < int(frontierSize) && !p.Errored(); i++ {
id, err := ids.ToID(p.UnpackFixedBytes(hashing.HashLen))
p.Add(err)
frontier[i] = id
}
if p.Offset == len(b) && !p.Errored() {
s.dbCache.Put(id, frontier)
return frontier
}
s.log.Error("failed parsing saved edge",
zap.Binary("key", id[:]),
zap.Binary("edge", b),
zap.Error(err),
)
}
s.dbCache.Put(id, nil) // Cache the miss
return nil
}
// SetEdge sets the frontier and returns an error if it fails to write to the db
func (s *state) SetEdge(id ids.ID, frontier []ids.ID) error {
s.dbCache.Put(id, frontier)
if len(frontier) == 0 {
return s.db.Delete(id[:])
}
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[:])
}
s.log.AssertNoError(p.Err)
s.log.AssertTrue(p.Offset == len(p.Bytes), "Wrong offset after packing")
return s.db.Put(id[:], p.Bytes)
}