/
stack.go
80 lines (72 loc) · 2.2 KB
/
stack.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
package blockwatch
import (
"sync"
"github.com/0xProject/0x-mesh/meshdb"
)
// Stack allows performing basic stack operations on a stack of meshdb.MiniHeaders.
type Stack struct {
// TODO(albrow): Use Transactions when db supports them instead of a mutex
// here. There are cases where we need to make sure no modifications are made
// to the database in between a read/write or read/delete.
mut sync.Mutex
meshDB *meshdb.MeshDB
limit int
}
// NewStack instantiates a new stack with the specified size limit. Once the size limit
// is reached, adding additional blocks will evict the deepest block.
func NewStack(meshDB *meshdb.MeshDB, limit int) *Stack {
return &Stack{
meshDB: meshDB,
limit: limit,
}
}
// Pop removes and returns the latest block header on the block stack. It
// returns nil if the stack is empty.
func (s *Stack) Pop() (*meshdb.MiniHeader, error) {
s.mut.Lock()
defer s.mut.Unlock()
latestMiniHeader, err := s.meshDB.FindLatestMiniHeader()
if err != nil {
return nil, err
}
if latestMiniHeader == nil {
return nil, nil
}
if err := s.meshDB.MiniHeaders.Delete(latestMiniHeader.ID()); err != nil {
return nil, err
}
return latestMiniHeader, nil
}
// Push pushes a block header onto the block stack. If the stack limit is
// reached, it will remove the oldest block header.
func (s *Stack) Push(block *meshdb.MiniHeader) error {
s.mut.Lock()
defer s.mut.Unlock()
miniHeaders, err := s.meshDB.FindAllMiniHeadersSortedByNumber()
if err != nil {
return err
}
if len(miniHeaders) == s.limit {
oldestMiniHeader := miniHeaders[0]
if err := s.meshDB.MiniHeaders.Delete(oldestMiniHeader.ID()); err != nil {
return err
}
}
if err := s.meshDB.MiniHeaders.Insert(block); err != nil {
return err
}
return nil
}
// Peek returns the latest block header from the block stack without removing
// it. It returns nil if the stack is empty.
func (s *Stack) Peek() (*meshdb.MiniHeader, error) {
return s.meshDB.FindLatestMiniHeader()
}
// Inspect returns all the block headers currently on the stack
func (s *Stack) Inspect() ([]*meshdb.MiniHeader, error) {
miniHeaders, err := s.meshDB.FindAllMiniHeadersSortedByNumber()
if err != nil {
return nil, err
}
return miniHeaders, nil
}