forked from ava-labs/avalanchego
-
Notifications
You must be signed in to change notification settings - Fork 3
/
serializer.go
156 lines (133 loc) · 3.5 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
// Copyright (C) 2019-2024, 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/MetalBlockchain/metalgo/cache"
"github.com/MetalBlockchain/metalgo/database"
"github.com/MetalBlockchain/metalgo/database/versiondb"
"github.com/MetalBlockchain/metalgo/ids"
"github.com/MetalBlockchain/metalgo/snow/choices"
"github.com/MetalBlockchain/metalgo/snow/consensus/avalanche"
"github.com/MetalBlockchain/metalgo/snow/engine/avalanche/vertex"
"github.com/MetalBlockchain/metalgo/utils/logging"
"github.com/MetalBlockchain/metalgo/utils/math"
"github.com/MetalBlockchain/metalgo/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
CortinaTime time.Time
}
func NewSerializer(config SerializerConfig) vertex.Manager {
versionDB := versiondb.New(config.DB)
dbCache := &cache.LRU[ids.ID, any]{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) BuildStopVtx(
ctx context.Context,
parentIDs []ids.ID,
) (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 = max(height, childHeight)
}
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
}