-
Notifications
You must be signed in to change notification settings - Fork 659
/
serializer.go
191 lines (166 loc) · 4.17 KB
/
serializer.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
// Copyright (C) 2019-2022, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.
// Package state manages the meta-data required by consensus for an avalanche
// dag.
package state
import (
"context"
"errors"
"time"
"github.com/ava-labs/avalanchego/cache"
"github.com/ava-labs/avalanchego/database"
"github.com/ava-labs/avalanchego/database/versiondb"
"github.com/ava-labs/avalanchego/ids"
"github.com/ava-labs/avalanchego/snow/choices"
"github.com/ava-labs/avalanchego/snow/consensus/avalanche"
"github.com/ava-labs/avalanchego/snow/consensus/snowstorm"
"github.com/ava-labs/avalanchego/snow/engine/avalanche/vertex"
"github.com/ava-labs/avalanchego/utils/logging"
"github.com/ava-labs/avalanchego/utils/math"
"github.com/ava-labs/avalanchego/utils/set"
)
const (
dbCacheSize = 10000
idCacheSize = 1000
)
var (
errUnknownVertex = errors.New("unknown vertex")
errWrongChainID = errors.New("wrong ChainID in vertex")
)
var _ vertex.Manager = (*Serializer)(nil)
// Serializer manages the state of multiple vertices
type Serializer struct {
SerializerConfig
versionDB *versiondb.Database
state *prefixedState
edge set.Set[ids.ID]
}
type SerializerConfig struct {
ChainID ids.ID
VM vertex.DAGVM
DB database.Database
Log logging.Logger
XChainMigrationTime time.Time
}
func NewSerializer(config SerializerConfig) vertex.Manager {
versionDB := versiondb.New(config.DB)
dbCache := &cache.LRU{Size: dbCacheSize}
s := Serializer{
SerializerConfig: config,
versionDB: versionDB,
}
rawState := &state{
serializer: &s,
log: config.Log,
dbCache: dbCache,
db: versionDB,
}
s.state = newPrefixedState(rawState, idCacheSize)
s.edge.Add(s.state.Edge()...)
return &s
}
func (s *Serializer) ParseVtx(ctx context.Context, b []byte) (avalanche.Vertex, error) {
return newUniqueVertex(ctx, s, b)
}
func (s *Serializer) BuildVtx(
ctx context.Context,
parentIDs []ids.ID,
txs []snowstorm.Tx,
) (avalanche.Vertex, error) {
return s.buildVtx(ctx, parentIDs, txs, false)
}
func (s *Serializer) BuildStopVtx(
ctx context.Context,
parentIDs []ids.ID,
) (avalanche.Vertex, error) {
return s.buildVtx(ctx, parentIDs, nil, true)
}
func (s *Serializer) buildVtx(
ctx context.Context,
parentIDs []ids.ID,
txs []snowstorm.Tx,
stopVtx bool,
) (avalanche.Vertex, error) {
height := uint64(0)
for _, parentID := range parentIDs {
parent, err := s.getUniqueVertex(parentID)
if err != nil {
return nil, err
}
parentHeight := parent.v.vtx.Height()
childHeight, err := math.Add64(parentHeight, 1)
if err != nil {
return nil, err
}
height = math.Max(height, childHeight)
}
var (
vtx vertex.StatelessVertex
err error
)
if !stopVtx {
txBytes := make([][]byte, len(txs))
for i, tx := range txs {
txBytes[i] = tx.Bytes()
}
vtx, err = vertex.Build(
s.ChainID,
height,
parentIDs,
txBytes,
)
} else {
vtx, err = vertex.BuildStopVertex(
s.ChainID,
height,
parentIDs,
)
}
if err != nil {
return nil, err
}
uVtx := &uniqueVertex{
serializer: s,
id: vtx.ID(),
}
// setVertex handles the case where this vertex already exists even
// though we just made it
return uVtx, uVtx.setVertex(ctx, vtx)
}
func (s *Serializer) GetVtx(_ context.Context, vtxID ids.ID) (avalanche.Vertex, error) {
return s.getUniqueVertex(vtxID)
}
func (s *Serializer) Edge(context.Context) []ids.ID {
return s.edge.List()
}
func (s *Serializer) parseVertex(b []byte) (vertex.StatelessVertex, error) {
vtx, err := vertex.Parse(b)
if err != nil {
return nil, err
}
if vtx.ChainID() != s.ChainID {
return nil, errWrongChainID
}
return vtx, nil
}
func (s *Serializer) getUniqueVertex(vtxID ids.ID) (*uniqueVertex, error) {
vtx := &uniqueVertex{
serializer: s,
id: vtxID,
}
if vtx.Status() == choices.Unknown {
return nil, errUnknownVertex
}
return vtx, nil
}
func (s *Serializer) StopVertexAccepted(ctx context.Context) (bool, error) {
edge := s.Edge(ctx)
if len(edge) != 1 {
return false, nil
}
vtx, err := s.getUniqueVertex(edge[0])
if err != nil {
return false, err
}
return vtx.v.vtx.StopVertex(), nil
}