-
Notifications
You must be signed in to change notification settings - Fork 12
/
blocks.go
101 lines (78 loc) · 2.86 KB
/
blocks.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
package blocks
import (
"github.com/iotaledger/hive.go/core/memstorage"
"github.com/iotaledger/hive.go/runtime/event"
"github.com/iotaledger/hive.go/runtime/syncutils"
"github.com/iotaledger/iota-core/pkg/model"
"github.com/iotaledger/iota-core/pkg/protocol/engine/eviction"
iotago "github.com/iotaledger/iota.go/v4"
)
type Blocks struct {
Evict *event.Event1[iotago.SlotIndex]
blocks *memstorage.IndexedStorage[iotago.SlotIndex, iotago.BlockID, *Block]
evictionState *eviction.State
apiProvider iotago.APIProvider
evictionMutex syncutils.RWMutex
}
func New(evictionState *eviction.State, apiProvider iotago.APIProvider) *Blocks {
return &Blocks{
Evict: event.New1[iotago.SlotIndex](),
blocks: memstorage.NewIndexedStorage[iotago.SlotIndex, iotago.BlockID, *Block](),
evictionState: evictionState,
apiProvider: apiProvider,
}
}
func (b *Blocks) EvictUntil(slot iotago.SlotIndex) {
b.Evict.Trigger(slot)
b.evictionMutex.Lock()
defer b.evictionMutex.Unlock()
b.blocks.Evict(slot)
}
func (b *Blocks) Block(id iotago.BlockID) (block *Block, exists bool) {
b.evictionMutex.RLock()
defer b.evictionMutex.RUnlock()
if storage := b.blocks.Get(id.Slot()); storage != nil {
if block, exists = storage.Get(id); exists {
return block, true
}
}
if commitmentID, isRootBlock := b.evictionState.RootBlockCommitmentID(id); isRootBlock {
return NewRootBlock(id, commitmentID, b.apiProvider.APIForSlot(id.Slot()).TimeProvider().SlotEndTime(id.Slot())), true
}
return nil, false
}
func (b *Blocks) StoreOrUpdate(data *model.Block) (storedBlock *Block, evicted bool, updated bool) {
b.evictionMutex.RLock()
defer b.evictionMutex.RUnlock()
if evictedIndex := b.evictionState.LastEvictedSlot(); evictedIndex >= data.ID().Slot() {
return nil, true, false
}
storage := b.blocks.Get(data.ID().Slot(), true)
createdBlock, created := storage.GetOrCreate(data.ID(), func() *Block { return NewBlock(data) })
if !created {
return createdBlock, false, createdBlock.Update(data)
}
return createdBlock, false, false
}
func (b *Blocks) GetOrCreate(blockID iotago.BlockID, createFunc func() *Block) (block *Block, created bool) {
b.evictionMutex.RLock()
defer b.evictionMutex.RUnlock()
if evictedIndex := b.evictionState.LastEvictedSlot(); evictedIndex >= blockID.Slot() {
return nil, false
}
storage := b.blocks.Get(blockID.Slot(), true)
return storage.GetOrCreate(blockID, createFunc)
}
func (b *Blocks) StoreBlock(block *Block) (stored bool) {
b.evictionMutex.RLock()
defer b.evictionMutex.RUnlock()
if evictedIndex := b.evictionState.LastEvictedSlot(); evictedIndex >= block.ID().Slot() {
return false
}
storage := b.blocks.Get(block.ID().Slot(), true)
return storage.Set(block.ID(), block)
}
// Reset resets the component to a clean state as if it was created at the last commitment.
func (b *Blocks) Reset() {
b.blocks.Clear()
}