forked from Fantom-foundation/go-opera
-
Notifications
You must be signed in to change notification settings - Fork 0
/
store_block.go
132 lines (111 loc) · 3 KB
/
store_block.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
package gossip
import (
"github.com/Fantom-foundation/lachesis-base/hash"
"github.com/Fantom-foundation/lachesis-base/inter/idx"
"github.com/ethereum/go-ethereum/rlp"
"github.com/frenchie-foundation/go-opera/inter"
)
func (s *Store) GetGenesisHash() *hash.Hash {
valBytes, err := s.table.Genesis.Get([]byte("g"))
if err != nil {
s.Log.Crit("Failed to get key-value", "err", err)
}
if len(valBytes) == 0 {
return nil
}
val := hash.BytesToHash(valBytes)
return &val
}
func (s *Store) SetGenesisHash(val hash.Hash) {
err := s.table.Genesis.Put([]byte("g"), val.Bytes())
if err != nil {
s.Log.Crit("Failed to put key-value", "err", err)
}
}
// SetBlock stores chain block.
func (s *Store) SetBlock(n idx.Block, b *inter.Block) {
s.rlp.Set(s.table.Blocks, n.Bytes(), b)
// Add to LRU cache.
s.cache.Blocks.Add(n, b, uint(b.EstimateSize()))
}
// GetBlock returns stored block.
func (s *Store) GetBlock(n idx.Block) *inter.Block {
// Get block from LRU cache first.
if c, ok := s.cache.Blocks.Get(n); ok {
return c.(*inter.Block)
}
block, _ := s.rlp.Get(s.table.Blocks, n.Bytes(), &inter.Block{}).(*inter.Block)
// Add to LRU cache.
if block != nil {
s.cache.Blocks.Add(n, block, uint(block.EstimateSize()))
}
return block
}
func (s *Store) ForEachBlock(fn func(index idx.Block, block *inter.Block)) {
it := s.table.Blocks.NewIterator(nil, nil)
defer it.Release()
for it.Next() {
var block inter.Block
err := rlp.DecodeBytes(it.Value(), &block)
if err != nil {
s.Log.Crit("Failed to decode block", "err", err)
}
fn(idx.BytesToBlock(it.Key()), &block)
}
}
// SetBlockIndex stores chain block index.
func (s *Store) SetBlockIndex(id hash.Event, n idx.Block) {
if err := s.table.BlockHashes.Put(id.Bytes(), n.Bytes()); err != nil {
s.Log.Crit("Failed to put key-value", "err", err)
}
s.cache.BlockHashes.Add(id, n, nominalSize)
}
// GetBlockIndex returns stored block index.
func (s *Store) GetBlockIndex(id hash.Event) *idx.Block {
nVal, ok := s.cache.BlockHashes.Get(id)
if ok {
n, ok := nVal.(idx.Block)
if ok {
return &n
}
}
buf, err := s.table.BlockHashes.Get(id.Bytes())
if err != nil {
s.Log.Crit("Failed to get key-value", "err", err)
}
if buf == nil {
return nil
}
n := idx.BytesToBlock(buf)
s.cache.BlockHashes.Add(id, n, nominalSize)
return &n
}
// SetGenesisBlockIndex stores genesis block index.
func (s *Store) SetGenesisBlockIndex(n idx.Block) {
if err := s.table.Genesis.Put([]byte("i"), n.Bytes()); err != nil {
s.Log.Crit("Failed to put key-value", "err", err)
}
}
// GetGenesisBlockIndex returns stored genesis block index.
func (s *Store) GetGenesisBlockIndex() *idx.Block {
buf, err := s.table.Genesis.Get([]byte("i"))
if err != nil {
s.Log.Crit("Failed to get key-value", "err", err)
}
if buf == nil {
return nil
}
n := idx.BytesToBlock(buf)
return &n
}
func (s *Store) GetGenesisTime() inter.Timestamp {
n := s.GetGenesisBlockIndex()
if n == nil {
return 0
}
block := s.GetBlock(*n)
if block == nil {
return 0
}
return block.Time
}