-
Notifications
You must be signed in to change notification settings - Fork 0
/
emptyBlockCreatorsContainer.go
52 lines (41 loc) · 1.42 KB
/
emptyBlockCreatorsContainer.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
package block
import (
"sync"
"github.com/ThotaGopichandThota/gn-core3/core"
"github.com/ThotaGopichandThota/gn-core3/core/check"
"github.com/ThotaGopichandThota/gn-core3/data"
)
type emptyBlockCreatorsContainer struct {
mut sync.RWMutex
blockCreators map[core.HeaderType]EmptyBlockCreator
}
// NewEmptyBlockCreatorsContainer creates a new block creators container
func NewEmptyBlockCreatorsContainer() *emptyBlockCreatorsContainer {
return &emptyBlockCreatorsContainer{
blockCreators: make(map[core.HeaderType]EmptyBlockCreator),
}
}
// Add will add a new empty block creator
func (container *emptyBlockCreatorsContainer) Add(headerType core.HeaderType, creator EmptyBlockCreator) error {
if check.IfNil(creator) {
return data.ErrNilEmptyBlockCreator
}
container.mut.Lock()
container.blockCreators[headerType] = creator
container.mut.Unlock()
return nil
}
// Get will try to get a new empty block creator. Errors if the empty block creator type is not found
func (container *emptyBlockCreatorsContainer) Get(headerType core.HeaderType) (EmptyBlockCreator, error) {
container.mut.RLock()
creator, ok := container.blockCreators[headerType]
container.mut.RUnlock()
if !ok {
return nil, data.ErrInvalidHeaderType
}
return creator, nil
}
// IsInterfaceNil returns true if there is no value under the interface
func (container *emptyBlockCreatorsContainer) IsInterfaceNil() bool {
return container == nil
}