-
Notifications
You must be signed in to change notification settings - Fork 124
/
eventstate.go
244 lines (212 loc) · 5.65 KB
/
eventstate.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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
/*
* This file is part of The AnnChain.
*
* The AnnChain is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* The AnnChain is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with The www.annchain.io. If not, see <http://www.gnu.org/licenses/>.
*/
package node
import (
"bytes"
"encoding/gob"
"sync"
"github.com/tendermint/merkleeyes/iavl"
"github.com/tendermint/tmlibs/db"
)
type CodeHash = []byte
// EventState manages event subscription relationships between subchains
type EventState struct {
mtx sync.Mutex
database db.DB
rootHash []byte
trie *iavl.IAVLTree
dirty map[string]struct{}
order []string
accountCache map[string]*EventAccount
}
// EventAccount abstracts the model used in the EventState, mainly focusing on who am I listening and who is listening me
type EventAccount struct {
master *EventState
ChainID string
MySubers map[string]CodeHash
MyPubers map[string]CodeHash
}
func NewEventState(database db.DB) *EventState {
return &EventState{
database: database,
trie: iavl.NewIAVLTree(1024, database),
accountCache: make(map[string]*EventAccount),
dirty: make(map[string]struct{}),
order: make([]string, 0),
}
}
func (es *EventState) Lock() {
es.mtx.Lock()
}
func (es *EventState) Unlock() {
es.mtx.Unlock()
}
func (es *EventState) ModifyAccount(a *EventAccount) {
es.accountCache[a.ChainID] = a
if _, ok := es.dirty[a.ChainID]; !ok {
es.dirty[a.ChainID] = struct{}{}
es.order = append(es.order, a.ChainID)
}
}
func (es *EventState) CreateAccount(chainID string) (accnt *EventAccount, err error) {
es.mtx.Lock()
_, ok := es.accountCache[chainID]
if ok || es.trie.Has([]byte(chainID)) {
es.mtx.Unlock()
return nil, ErrAccountExisted
}
accnt = NewEventAccount(es, chainID)
es.accountCache[chainID] = accnt
es.dirty[chainID] = struct{}{}
es.order = append(es.order, chainID)
es.mtx.Unlock()
return
}
func (es *EventState) GetAccount(chainID string) (accnt *EventAccount, err error) {
es.mtx.Lock()
defer es.mtx.Unlock()
accnt, ok := es.accountCache[chainID]
if ok {
return
}
_, bytes, exist := es.trie.Get([]byte(chainID))
if !exist {
err = ErrAccountNotExist
return
}
accnt = &EventAccount{}
if err := accnt.FromBytes(bytes, es); err != nil {
return nil, err
}
es.accountCache[chainID] = accnt
return
}
func (es *EventState) ExistAccount(chainID string) bool {
es.mtx.Lock()
if _, ok := es.accountCache[chainID]; ok {
es.mtx.Unlock()
return ok
}
es.mtx.Unlock()
return es.trie.Has([]byte(chainID))
}
func (es *EventState) Commit() ([]byte, error) {
es.mtx.Lock()
for _, id := range es.order {
es.trie.Set([]byte(es.accountCache[id].ChainID), es.accountCache[id].ToBytes())
}
es.rootHash = es.trie.Save()
es.mtx.Unlock()
return es.rootHash, nil
}
func (es *EventState) Load(root []byte) {
es.mtx.Lock()
es.accountCache = make(map[string]*EventAccount)
es.dirty = make(map[string]struct{})
es.order = make([]string, 0)
es.trie.Load(root)
es.mtx.Unlock()
}
func (es *EventState) Reload(root []byte) {
es.mtx.Lock()
es.accountCache = make(map[string]*EventAccount)
es.dirty = make(map[string]struct{})
es.order = make([]string, 0)
es.trie.Load(root)
es.mtx.Unlock()
}
func (es *EventState) Copy() *EventState {
cp := &EventState{
database: es.database,
rootHash: es.rootHash,
trie: es.trie,
accountCache: make(map[string]*EventAccount),
dirty: make(map[string]struct{}),
order: make([]string, len(es.order), cap(es.order)),
}
for k := range es.accountCache {
cp.accountCache[k] = es.accountCache[k].Copy()
}
for k := range es.dirty {
cp.dirty[k] = struct{}{}
}
for i, id := range es.order {
cp.order[i] = id
}
return cp
}
func NewEventAccount(es *EventState, chainID string) *EventAccount {
return &EventAccount{
master: es,
ChainID: chainID,
MyPubers: make(map[string]CodeHash),
MySubers: make(map[string]CodeHash),
}
}
func (ea *EventAccount) GetSubscribers() map[string]CodeHash {
return ea.MySubers
}
func (ea *EventAccount) GetPublishers() map[string]CodeHash {
return ea.MyPubers
}
func (ea *EventAccount) AddSubscriber(sub string, hash CodeHash) {
ea.MySubers[sub] = hash
ea.master.ModifyAccount(ea)
}
func (ea *EventAccount) AddPublisher(pub string, hash CodeHash) {
ea.MyPubers[pub] = hash
ea.master.ModifyAccount(ea)
}
func (ea *EventAccount) RemoveSubscriber(sub string) {
delete(ea.MySubers, sub)
ea.master.ModifyAccount(ea)
}
func (ea *EventAccount) RemovePublisher(pub string) {
delete(ea.MyPubers, pub)
ea.master.ModifyAccount(ea)
}
func (ea *EventAccount) FromBytes(bs []byte, es *EventState) error {
gdc := gob.NewDecoder(bytes.NewBuffer(bs))
if err := gdc.Decode(ea); err != nil {
return err
}
ea.master = es
return nil
}
func (ea *EventAccount) ToBytes() []byte {
buf := &bytes.Buffer{}
gec := gob.NewEncoder(buf)
if err := gec.Encode(ea); err != nil {
return nil
}
return buf.Bytes()
}
func (ea *EventAccount) Copy() *EventAccount {
cp := &EventAccount{
master: ea.master,
ChainID: ea.ChainID,
MySubers: make(map[string]CodeHash),
MyPubers: make(map[string]CodeHash),
}
for k, v := range ea.MySubers {
cp.MySubers[k] = v
}
for k, v := range ea.MyPubers {
cp.MyPubers[k] = v
}
return cp
}