-
Notifications
You must be signed in to change notification settings - Fork 662
/
block_state.go
142 lines (117 loc) · 3.37 KB
/
block_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
// Copyright (C) 2019-2023, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package state
import (
"errors"
"fmt"
"github.com/prometheus/client_golang/prometheus"
"github.com/ava-labs/avalanchego/cache"
"github.com/ava-labs/avalanchego/cache/metercacher"
"github.com/ava-labs/avalanchego/database"
"github.com/ava-labs/avalanchego/ids"
"github.com/ava-labs/avalanchego/snow/choices"
"github.com/ava-labs/avalanchego/utils/constants"
"github.com/ava-labs/avalanchego/utils/units"
"github.com/ava-labs/avalanchego/utils/wrappers"
"github.com/ava-labs/avalanchego/vms/proposervm/block"
)
const blockCacheSize = 64 * units.MiB
var (
errBlockWrongVersion = errors.New("wrong version")
_ BlockState = (*blockState)(nil)
)
type BlockState interface {
GetBlock(blkID ids.ID) (block.Block, choices.Status, error)
PutBlock(blk block.Block, status choices.Status) error
DeleteBlock(blkID ids.ID) error
}
type blockState struct {
// Caches BlockID -> Block. If the Block is nil, that means the block is not
// in storage.
blkCache cache.Cacher[ids.ID, *blockWrapper]
db database.Database
}
type blockWrapper struct {
Block []byte `serialize:"true"`
Status choices.Status `serialize:"true"`
block block.Block
}
func cachedBlockSize(_ ids.ID, bw *blockWrapper) int {
if bw == nil {
return ids.IDLen + constants.PointerOverhead
}
return ids.IDLen + len(bw.Block) + wrappers.IntLen + 2*constants.PointerOverhead
}
func NewBlockState(db database.Database) BlockState {
return &blockState{
blkCache: cache.NewSizedLRU[ids.ID, *blockWrapper](
blockCacheSize,
cachedBlockSize,
),
db: db,
}
}
func NewMeteredBlockState(db database.Database, namespace string, metrics prometheus.Registerer) (BlockState, error) {
blkCache, err := metercacher.New[ids.ID, *blockWrapper](
fmt.Sprintf("%s_block_cache", namespace),
metrics,
cache.NewSizedLRU[ids.ID, *blockWrapper](
blockCacheSize,
cachedBlockSize,
),
)
return &blockState{
blkCache: blkCache,
db: db,
}, err
}
func (s *blockState) GetBlock(blkID ids.ID) (block.Block, choices.Status, error) {
if blk, found := s.blkCache.Get(blkID); found {
if blk == nil {
return nil, choices.Unknown, database.ErrNotFound
}
return blk.block, blk.Status, nil
}
blkWrapperBytes, err := s.db.Get(blkID[:])
if err == database.ErrNotFound {
s.blkCache.Put(blkID, nil)
return nil, choices.Unknown, database.ErrNotFound
}
if err != nil {
return nil, choices.Unknown, err
}
blkWrapper := blockWrapper{}
parsedVersion, err := c.Unmarshal(blkWrapperBytes, &blkWrapper)
if err != nil {
return nil, choices.Unknown, err
}
if parsedVersion != version {
return nil, choices.Unknown, errBlockWrongVersion
}
// The key was in the database
blk, err := block.Parse(blkWrapper.Block)
if err != nil {
return nil, choices.Unknown, err
}
blkWrapper.block = blk
s.blkCache.Put(blkID, &blkWrapper)
return blk, blkWrapper.Status, nil
}
func (s *blockState) PutBlock(blk block.Block, status choices.Status) error {
blkWrapper := blockWrapper{
Block: blk.Bytes(),
Status: status,
block: blk,
}
bytes, err := c.Marshal(version, &blkWrapper)
if err != nil {
return err
}
blkID := blk.ID()
s.blkCache.Put(blkID, &blkWrapper)
return s.db.Put(blkID[:], bytes)
}
func (s *blockState) DeleteBlock(blkID ids.ID) error {
s.blkCache.Evict(blkID)
return s.db.Delete(blkID[:])
}