/
currency_pool.go
137 lines (107 loc) · 2.35 KB
/
currency_pool.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
133
134
135
136
137
package currency
import (
"sync"
"github.com/spikeekips/mitum/base/state"
)
type CurrencyPool struct {
sync.RWMutex
demap map[CurrencyID]CurrencyDesign
stsmap map[CurrencyID]state.State
cids []CurrencyID
}
func NewCurrencyPool() *CurrencyPool {
return &CurrencyPool{
demap: map[CurrencyID]CurrencyDesign{},
stsmap: map[CurrencyID]state.State{},
}
}
func (cp *CurrencyPool) Clear() {
cp.Lock()
defer cp.Unlock()
cp.demap = nil
cp.stsmap = nil
cp.cids = nil
}
func (cp *CurrencyPool) Set(st state.State) error {
cp.Lock()
defer cp.Unlock()
de, err := StateCurrencyDesignValue(st)
if err != nil {
return err
}
cp.demap[de.Currency()] = de
cp.stsmap[de.Currency()] = st
cp.cids = append(cp.cids, de.Currency())
return nil
}
func (cp *CurrencyPool) CIDs() []CurrencyID {
cp.RLock()
defer cp.RUnlock()
return cp.cids
}
func (cp *CurrencyPool) Designs() map[CurrencyID]CurrencyDesign {
cp.RLock()
defer cp.RUnlock()
m := map[CurrencyID]CurrencyDesign{}
for k := range cp.demap {
m[k] = cp.demap[k]
}
return m
}
func (cp *CurrencyPool) States() map[CurrencyID]state.State {
cp.RLock()
defer cp.RUnlock()
m := map[CurrencyID]state.State{}
for k := range cp.stsmap {
m[k] = cp.stsmap[k]
}
return m
}
func (cp *CurrencyPool) Policy(cid CurrencyID) (CurrencyPolicy, bool) {
i, found := cp.Get(cid)
if !found {
return CurrencyPolicy{}, false
}
return i.Policy(), true
}
func (cp *CurrencyPool) Feeer(cid CurrencyID) (Feeer, bool) {
i, found := cp.Get(cid)
if !found {
return nil, false
}
return i.Policy().Feeer(), true
}
func (cp *CurrencyPool) State(cid CurrencyID) (state.State, bool) {
i, found := cp.stsmap[cid]
return i, found
}
func (cp *CurrencyPool) TraverseDesign(callback func(cid CurrencyID, de CurrencyDesign) bool) {
cp.RLock()
defer cp.RUnlock()
for k := range cp.demap {
if !callback(k, cp.demap[k]) {
break
}
}
}
func (cp *CurrencyPool) TraverseState(callback func(cid CurrencyID, de state.State) bool) {
cp.RLock()
defer cp.RUnlock()
for k := range cp.stsmap {
if !callback(k, cp.stsmap[k]) {
break
}
}
}
func (cp *CurrencyPool) Exists(cid CurrencyID) bool {
cp.RLock()
defer cp.RUnlock()
_, found := cp.demap[cid]
return found
}
func (cp *CurrencyPool) Get(cid CurrencyID) (CurrencyDesign, bool) {
cp.RLock()
defer cp.RUnlock()
i, found := cp.demap[cid]
return i, found
}