-
Notifications
You must be signed in to change notification settings - Fork 662
/
block_state.go
124 lines (101 loc) · 2.84 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
// Copyright (C) 2019-2022, 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/vms/proposervm/block"
)
const blockCacheSize = 8192
var (
errBlockWrongVersion = errors.New("wrong version")
_ BlockState = &blockState{}
)
type BlockState interface {
GetBlock(blkID ids.ID) (block.Block, choices.Status, error)
PutBlock(blk block.Block, status choices.Status) error
}
type blockState struct {
// Caches BlockID -> Block. If the Block is nil, that means the block is not
// in storage.
blkCache cache.Cacher
db database.Database
}
type blockWrapper struct {
Block []byte `serialize:"true"`
Status choices.Status `serialize:"true"`
block block.Block
}
func NewBlockState(db database.Database) BlockState {
return &blockState{
blkCache: &cache.LRU{Size: blockCacheSize},
db: db,
}
}
func NewMeteredBlockState(db database.Database, namespace string, metrics prometheus.Registerer) (BlockState, error) {
blkCache, err := metercacher.New(
fmt.Sprintf("%s_block_cache", namespace),
metrics,
&cache.LRU{Size: blockCacheSize},
)
return &blockState{
blkCache: blkCache,
db: db,
}, err
}
func (s *blockState) GetBlock(blkID ids.ID) (block.Block, choices.Status, error) {
if blkIntf, found := s.blkCache.Get(blkID); found {
if blkIntf == nil {
return nil, choices.Unknown, database.ErrNotFound
}
blk, ok := blkIntf.(*blockWrapper)
if !ok {
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)
}