-
Notifications
You must be signed in to change notification settings - Fork 177
/
Copy pathstack.go
95 lines (85 loc) · 2.69 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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
package blockwatch
import (
"math/big"
"sync"
ethcommon "github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
)
// MiniHeader is a succinct representation of an Ethereum block header
type MiniHeader struct {
Hash ethcommon.Hash
Parent ethcommon.Hash
Number *big.Int
L1BlockNumber *big.Int
Logs []types.Log
}
// MiniHeaderStore is an interface for a store that manages the state of a MiniHeader collection
type MiniHeaderStore interface {
FindLatestMiniHeader() (*MiniHeader, error)
FindAllMiniHeadersSortedByNumber() ([]*MiniHeader, error)
InsertMiniHeader(header *MiniHeader) error
DeleteMiniHeader(hash ethcommon.Hash) error
}
// Stack allows performing basic stack operations on a stack of 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
store MiniHeaderStore
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(store MiniHeaderStore, limit int) *Stack {
return &Stack{
store: store,
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() (*MiniHeader, error) {
s.mut.Lock()
defer s.mut.Unlock()
latestMiniHeader, err := s.store.FindLatestMiniHeader()
if err != nil {
return nil, err
}
if latestMiniHeader == nil {
return nil, nil
}
if err := s.store.DeleteMiniHeader(latestMiniHeader.Hash); 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(header *MiniHeader) error {
s.mut.Lock()
defer s.mut.Unlock()
miniHeaders, err := s.store.FindAllMiniHeadersSortedByNumber()
if err != nil {
return err
}
if len(miniHeaders) == s.limit {
oldestMiniHeader := miniHeaders[0]
if err := s.store.DeleteMiniHeader(oldestMiniHeader.Hash); err != nil {
return err
}
}
if err := s.store.InsertMiniHeader(header); 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() (*MiniHeader, error) {
return s.store.FindLatestMiniHeader()
}
// Inspect returns all the block headers currently on the stack
func (s *Stack) Inspect() ([]*MiniHeader, error) {
return s.store.FindAllMiniHeadersSortedByNumber()
}