/
prefixed_state.go
100 lines (82 loc) · 2.12 KB
/
prefixed_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
// Copyright (C) 2019-2023, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package state
import (
"github.com/ava-labs/avalanchego/cache"
"github.com/ava-labs/avalanchego/ids"
"github.com/ava-labs/avalanchego/snow/choices"
"github.com/ava-labs/avalanchego/snow/engine/avalanche/vertex"
)
const (
vtxID uint64 = iota
vtxStatusID
edgeID
)
var uniqueEdgeID = ids.Empty.Prefix(edgeID)
type prefixedState struct {
state *state
vtx, status cache.Cacher[ids.ID, ids.ID]
uniqueVtx cache.Deduplicator[ids.ID, *uniqueVertex]
}
func newPrefixedState(state *state, idCacheSizes int) *prefixedState {
return &prefixedState{
state: state,
vtx: &cache.LRU[ids.ID, ids.ID]{Size: idCacheSizes},
status: &cache.LRU[ids.ID, ids.ID]{Size: idCacheSizes},
uniqueVtx: &cache.EvictableLRU[ids.ID, *uniqueVertex]{Size: idCacheSizes},
}
}
func (s *prefixedState) UniqueVertex(vtx *uniqueVertex) *uniqueVertex {
return s.uniqueVtx.Deduplicate(vtx)
}
func (s *prefixedState) Vertex(id ids.ID) vertex.StatelessVertex {
var (
vID ids.ID
ok bool
)
if vID, ok = s.vtx.Get(id); !ok {
vID = id.Prefix(vtxID)
s.vtx.Put(id, vID)
}
return s.state.Vertex(vID)
}
func (s *prefixedState) SetVertex(vtx vertex.StatelessVertex) error {
var (
rawVertexID = vtx.ID()
vID ids.ID
ok bool
)
if vID, ok = s.vtx.Get(rawVertexID); !ok {
vID = rawVertexID.Prefix(vtxID)
s.vtx.Put(rawVertexID, vID)
}
return s.state.SetVertex(vID, vtx)
}
func (s *prefixedState) Status(id ids.ID) choices.Status {
var (
sID ids.ID
ok bool
)
if sID, ok = s.status.Get(id); !ok {
sID = id.Prefix(vtxStatusID)
s.status.Put(id, sID)
}
return s.state.Status(sID)
}
func (s *prefixedState) SetStatus(id ids.ID, status choices.Status) error {
var (
sID ids.ID
ok bool
)
if sID, ok = s.status.Get(id); !ok {
sID = id.Prefix(vtxStatusID)
s.status.Put(id, sID)
}
return s.state.SetStatus(sID, status)
}
func (s *prefixedState) Edge() []ids.ID {
return s.state.Edge(uniqueEdgeID)
}
func (s *prefixedState) SetEdge(frontier []ids.ID) error {
return s.state.SetEdge(uniqueEdgeID, frontier)
}