forked from ava-labs/avalanchego
-
Notifications
You must be signed in to change notification settings - Fork 0
/
unique_vertex.go
178 lines (141 loc) · 4.18 KB
/
unique_vertex.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
// (c) 2019-2020, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package state
import (
"fmt"
"strings"
"github.com/ava-labs/gecko/ids"
"github.com/ava-labs/gecko/snow/choices"
"github.com/ava-labs/gecko/snow/consensus/avalanche"
"github.com/ava-labs/gecko/snow/consensus/snowstorm"
"github.com/ava-labs/gecko/utils/formatting"
)
// uniqueVertex acts as a cache for vertices in the database.
//
// If a vertex is loaded, it will have one canonical uniqueVertex. The vertex
// will eventually be evicted from memory, when the uniqueVertex is evicted from
// the cache. If the uniqueVertex has a function called again afther this
// eviction, the vertex will be re-loaded from the database.
type uniqueVertex struct {
serializer *Serializer
vtxID ids.ID
v *vertexState
}
func (vtx *uniqueVertex) refresh() {
if vtx.v == nil {
vtx.v = &vertexState{}
}
if !vtx.v.unique {
unique := vtx.serializer.state.UniqueVertex(vtx)
prevVtx := vtx.v.vtx
if unique == vtx {
vtx.v.status = vtx.serializer.state.Status(vtx.ID())
vtx.v.unique = true
} else {
// If someone is in the cache, they must be up to date
*vtx = *unique
}
switch {
case vtx.v.vtx == nil && prevVtx == nil:
vtx.v.vtx = vtx.serializer.state.Vertex(vtx.ID())
case vtx.v.vtx == nil:
vtx.v.vtx = prevVtx
}
}
}
func (vtx *uniqueVertex) Evict() {
if vtx.v != nil {
vtx.v.unique = false
}
}
func (vtx *uniqueVertex) setVertex(innerVtx *vertex) {
vtx.refresh()
if vtx.v.vtx == nil {
vtx.v.vtx = innerVtx
vtx.serializer.state.SetVertex(innerVtx)
vtx.setStatus(choices.Processing)
}
}
func (vtx *uniqueVertex) setStatus(status choices.Status) {
vtx.refresh()
if vtx.v.status != status {
vtx.serializer.state.SetStatus(vtx.ID(), status)
vtx.v.status = status
}
}
func (vtx *uniqueVertex) ID() ids.ID { return vtx.vtxID }
func (vtx *uniqueVertex) Accept() {
vtx.setStatus(choices.Accepted)
vtx.serializer.edge.Add(vtx.vtxID)
for _, parent := range vtx.Parents() {
vtx.serializer.edge.Remove(parent.ID())
}
vtx.serializer.state.SetEdge(vtx.serializer.edge.List())
// Should never traverse into parents of a decided vertex. Allows for the
// parents to be garbage collected
vtx.v.parents = nil
vtx.serializer.db.Commit()
}
func (vtx *uniqueVertex) Reject() {
vtx.setStatus(choices.Rejected)
// Should never traverse into parents of a decided vertex. Allows for the
// parents to be garbage collected
vtx.v.parents = nil
vtx.serializer.db.Commit()
}
func (vtx *uniqueVertex) Status() choices.Status { vtx.refresh(); return vtx.v.status }
func (vtx *uniqueVertex) Parents() []avalanche.Vertex {
vtx.refresh()
if len(vtx.v.parents) != len(vtx.v.vtx.parentIDs) {
vtx.v.parents = make([]avalanche.Vertex, len(vtx.v.vtx.parentIDs))
for i, parentID := range vtx.v.vtx.parentIDs {
vtx.v.parents[i] = &uniqueVertex{
serializer: vtx.serializer,
vtxID: parentID,
}
}
}
return vtx.v.parents
}
func (vtx *uniqueVertex) Txs() []snowstorm.Tx {
vtx.refresh()
if len(vtx.v.vtx.txs) != len(vtx.v.txs) {
vtx.v.txs = make([]snowstorm.Tx, len(vtx.v.vtx.txs))
for i, tx := range vtx.v.vtx.txs {
vtx.v.txs[i] = tx
}
}
return vtx.v.txs
}
func (vtx *uniqueVertex) Bytes() []byte { return vtx.v.vtx.Bytes() }
func (vtx *uniqueVertex) Verify() error { return vtx.v.vtx.Verify() }
func (vtx *uniqueVertex) String() string {
sb := strings.Builder{}
parents := vtx.Parents()
txs := vtx.Txs()
sb.WriteString(fmt.Sprintf(
"Vertex(ID = %s, Status = %s, Number of Dependencies = %d, Number of Transactions = %d)",
vtx.ID(),
vtx.Status(),
len(parents),
len(txs),
))
parentFormat := fmt.Sprintf("\n Parent[%s]: ID = %%s, Status = %%s",
formatting.IntFormat(len(parents)-1))
for i, parent := range parents {
sb.WriteString(fmt.Sprintf(parentFormat, i, parent.ID(), parent.Status()))
}
txFormat := fmt.Sprintf("\n Transaction[%s]: ID = %%s, Status = %%s",
formatting.IntFormat(len(txs)-1))
for i, tx := range txs {
sb.WriteString(fmt.Sprintf(txFormat, i, tx.ID(), tx.Status()))
}
return sb.String()
}
type vertexState struct {
unique bool
vtx *vertex
status choices.Status
parents []avalanche.Vertex
txs []snowstorm.Tx
}