-
Notifications
You must be signed in to change notification settings - Fork 9
/
appchain_mgr.go
executable file
·348 lines (288 loc) · 10.4 KB
/
appchain_mgr.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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
package appchain_mgr
import (
"encoding/json"
"fmt"
"strconv"
"strings"
"github.com/looplab/fsm"
"github.com/meshplus/bitxhub-core/boltvm"
g "github.com/meshplus/bitxhub-core/governance"
"github.com/sirupsen/logrus"
)
const (
Prefix = "appchain"
ChainNumPrefix = "chain-num"
ChainOccupyIdPrefix = "chain-occupy-id"
ChainOccupyNamePrefix = "chain-occupy-name"
NameChainPrefix = "name-chain"
AdminChainPrefix = "admin-chain"
ChainAdminsPrefix = "chain-admins"
RelaychainType = "relaychain"
AppchainType = "appchain"
FabricType = "fabric"
ChainTypeFabric1_4_3 = "Fabric V1.4.3"
ChainTypeFabric1_4_4 = "Fabric V1.4.4"
ChainTypeHyperchain1_8_3 = "Hyperchain V1.8.3"
ChainTypeHyperchain1_8_6 = "Hyperchain V1.8.6"
ChainTypeFlato1_0_0 = "Flato V1.0.0"
ChainTypeFlato1_0_3 = "Flato V1.0.3"
ChainTypeFlato1_0_6 = "Flato V1.0.6"
ChainTypeBCOS2_6_0 = "BCOS V2.6.0"
ChainTypeCITA20_2_2 = "CITA V20.2.2"
ChainTypeETH = "ETH"
)
type AppchainManager struct {
g.Persister
}
type Appchain struct {
ID string `json:"id"`
ChainName string `json:"chain_name"`
ChainType string `json:"chain_type"`
TrustRoot []byte `json:"trust_root"`
Broker []byte `json:"broker"`
Desc string `json:"desc"`
Version uint64 `json:"version"`
DID string `json:"did"`
PubKey []byte `json:"pub_key"`
Status g.GovernanceStatus `json:"status"`
FSM *fsm.FSM `json:"fsm"`
}
type FabricBroker struct {
ChannelID string `json:"channel_id"`
ChaincodeID string `json:"chaincode_id"`
BrokerVersion string `json:"broker_version"`
}
type auditRecord struct {
Appchain *Appchain `json:"appchain"`
IsApproved bool `json:"is_approved"`
Desc string `json:"desc"`
}
var appchainStateMap = map[g.EventType][]g.GovernanceStatus{
g.EventUpdate: {g.GovernanceAvailable, g.GovernanceFrozen},
g.EventFreeze: {g.GovernanceAvailable},
g.EventActivate: {g.GovernanceFrozen},
g.EventLogout: {g.GovernanceAvailable, g.GovernanceUpdating, g.GovernanceFreezing, g.GovernanceActivating, g.GovernanceFrozen},
g.EventPause: {g.GovernanceAvailable, g.GovernanceFrozen},
g.EventUnpause: {g.GovernanceFrozen},
}
var appchainAvailableMap = map[g.GovernanceStatus]struct{}{
g.GovernanceAvailable: {},
g.GovernanceFreezing: {},
}
func New(persister g.Persister) AppchainMgr {
return &AppchainManager{persister}
}
func (a *Appchain) IsAvailable() bool {
if _, ok := appchainAvailableMap[a.Status]; ok {
return true
} else {
return false
}
}
func (a *Appchain) IsBitXHub() bool {
return strings.EqualFold(a.ChainType, RelaychainType)
}
func (chain *Appchain) setFSM(lastStatus g.GovernanceStatus) {
chain.FSM = fsm.NewFSM(
string(chain.Status),
fsm.Events{
// update 1
{Name: string(g.EventUpdate), Src: []string{string(g.GovernanceAvailable), string(g.GovernanceFrozen), string(g.GovernanceLogouting)}, Dst: string(g.GovernanceUpdating)},
{Name: string(g.EventApprove), Src: []string{string(g.GovernanceUpdating)}, Dst: string(g.GovernanceAvailable)},
{Name: string(g.EventReject), Src: []string{string(g.GovernanceUpdating)}, Dst: string(g.GovernanceFrozen)},
// freeze 2
{Name: string(g.EventFreeze), Src: []string{string(g.GovernanceAvailable), string(g.GovernanceLogouting)}, Dst: string(g.GovernanceFreezing)},
{Name: string(g.EventApprove), Src: []string{string(g.GovernanceFreezing)}, Dst: string(g.GovernanceFrozen)},
{Name: string(g.EventReject), Src: []string{string(g.GovernanceFreezing)}, Dst: string(lastStatus)},
// activate 1
{Name: string(g.EventActivate), Src: []string{string(g.GovernanceFrozen), string(g.GovernanceLogouting)}, Dst: string(g.GovernanceActivating)},
{Name: string(g.EventApprove), Src: []string{string(g.GovernanceActivating)}, Dst: string(g.GovernanceAvailable)},
{Name: string(g.EventReject), Src: []string{string(g.GovernanceActivating)}, Dst: string(lastStatus)},
// logout 3
{Name: string(g.EventLogout), Src: []string{string(g.GovernanceAvailable), string(g.GovernanceUpdating), string(g.GovernanceFreezing), string(g.GovernanceFrozen), string(g.GovernanceActivating)}, Dst: string(g.GovernanceLogouting)},
{Name: string(g.EventApprove), Src: []string{string(g.GovernanceLogouting)}, Dst: string(g.GovernanceForbidden)},
{Name: string(g.EventReject), Src: []string{string(g.GovernanceLogouting)}, Dst: string(lastStatus)},
// pause
{Name: string(g.EventPause), Src: []string{string(g.GovernanceAvailable)}, Dst: string(g.GovernanceFrozen)},
// unpause
{Name: string(g.EventUnpause), Src: []string{string(g.GovernanceFrozen)}, Dst: string(g.GovernanceAvailable)},
},
fsm.Callbacks{
"enter_state": func(e *fsm.Event) { chain.Status = g.GovernanceStatus(chain.FSM.Current()) },
},
)
}
// GovernancePre checks if the appchain can do the event. (only check, not modify infomation)
// return *appchain, extra info, error
func (am *AppchainManager) GovernancePre(chainId string, event g.EventType, _ []byte) (interface{}, *boltvm.BxhError) {
chain := &Appchain{}
if ok := am.GetObject(AppchainKey(chainId), chain); !ok {
return nil, boltvm.BError(boltvm.AppchainNonexistentChainCode, fmt.Sprintf(string(boltvm.AppchainNonexistentChainMsg), chainId, ""))
}
for _, s := range appchainStateMap[event] {
if chain.Status == s {
return chain, nil
}
}
return nil, boltvm.BError(boltvm.AppchainStatusErrorCode, fmt.Sprintf(string(boltvm.AppchainStatusErrorMsg), chainId, chain.Status, string(event)))
}
// Register registers appchain info, return chain id
func (am *AppchainManager) Register(chainInfo *Appchain) {
am.SetObject(AppchainKey(chainInfo.ID), *chainInfo)
am.SetObject(AppchainNameKey(chainInfo.ChainName), chainInfo.ID)
am.Logger().WithFields(logrus.Fields{
"id": chainInfo.ID,
"name": chainInfo.ChainName,
}).Info("Appchain is registering")
}
func (am *AppchainManager) Update(updateInfo *Appchain) (bool, []byte) {
chain := &Appchain{}
ok := am.GetObject(AppchainKey(updateInfo.ID), chain)
if !ok {
return false, []byte(fmt.Sprintf("this appchain(%s) does not exist", updateInfo.ID))
}
oldName := chain.ChainName
chain.ChainName = updateInfo.ChainName
chain.Desc = updateInfo.Desc
chain.TrustRoot = updateInfo.TrustRoot
chain.Version++
am.SetObject(AppchainKey(updateInfo.ID), chain)
if oldName != chain.ChainName {
am.Delete(AppchainNameKey(oldName))
am.SetObject(AppchainNameKey(chain.ChainName), chain.ID)
}
am.Logger().WithFields(logrus.Fields{
"id": chain.ID,
"name": chain.ChainName,
}).Info("Appchain is updating")
return true, nil
}
func (am *AppchainManager) ChangeStatus(id, trigger, lastStatus string, _ []byte) (bool, []byte) {
ok, data := am.Get(AppchainKey(id))
if !ok {
return false, []byte("this appchain does not exist")
}
chain := &Appchain{}
if err := json.Unmarshal(data, chain); err != nil {
return false, []byte(fmt.Sprintf("unmarshal json error: %v", err))
}
chain.setFSM(g.GovernanceStatus(lastStatus))
err := chain.FSM.Event(trigger)
if err != nil {
return false, []byte(fmt.Sprintf("change status error: %v", err))
}
am.SetObject(AppchainKey(id), *chain)
return true, nil
}
func (am *AppchainManager) DeleteAppchain(id string) (bool, []byte) {
am.Delete(AppchainKey(id))
am.Logger().Infof("delete appchain:%s", id)
return true, nil
}
// Audit bitxhub manager audit appchain register info
func (am *AppchainManager) Audit(proposer string, isApproved int32, desc string) (bool, []byte) {
chain := &Appchain{}
ok := am.GetObject(AppchainKey(proposer), chain)
if !ok {
return false, []byte("this appchain does not exist")
}
chain.Status = g.GovernanceAvailable
record := &auditRecord{
Appchain: chain,
IsApproved: isApproved == g.APPROVED,
Desc: desc,
}
var records []*auditRecord
am.GetObject(am.auditRecordKey(proposer), &records)
records = append(records, record)
am.SetObject(am.auditRecordKey(proposer), records)
am.SetObject(AppchainKey(proposer), chain)
return true, []byte(fmt.Sprintf("audit %s successfully", proposer))
}
func (am *AppchainManager) FetchAuditRecords(id string) (bool, []byte) {
var records []*auditRecord
am.GetObject(am.auditRecordKey(id), &records)
body, err := json.Marshal(records)
if err != nil {
return false, []byte(err.Error())
}
return true, body
}
// CountAvailable counts all available appchains
func (am *AppchainManager) CountAvailable(_ []byte) (bool, []byte) {
ok, value := am.Query(Prefix)
if !ok {
return true, []byte("0")
}
count := 0
for _, v := range value {
a := &Appchain{}
if err := json.Unmarshal(v, a); err != nil {
return false, []byte(fmt.Sprintf("unmarshal json error: %v", err))
}
if a.IsAvailable() {
count++
}
}
return true, []byte(strconv.Itoa(count))
}
// CountAll counts all appchains including approved, rejected or registered
func (am *AppchainManager) CountAll(_ []byte) (bool, []byte) {
ok, value := am.Query(Prefix)
if !ok {
return true, []byte("0")
}
return true, []byte(strconv.Itoa(len(value)))
}
// All returns all appchains
func (am *AppchainManager) All(_ []byte) (interface{}, error) {
ret := make([]*Appchain, 0)
ok, value := am.Query(Prefix)
if ok {
for _, data := range value {
chain := &Appchain{}
if err := json.Unmarshal(data, chain); err != nil {
return nil, err
}
ret = append(ret, chain)
}
}
return ret, nil
}
func (am *AppchainManager) QueryById(id string, _ []byte) (interface{}, error) {
var appchain Appchain
ok := am.GetObject(AppchainKey(id), &appchain)
if !ok {
return nil, fmt.Errorf("this appchain(%s) does not exist", id)
}
return &appchain, nil
}
func (am *AppchainManager) GetChainIdByName(name string) (string, error) {
id := ""
ok := am.GetObject(AppchainNameKey(name), &id)
if !ok {
return "", fmt.Errorf("not found chain %s", name)
}
return id, nil
}
func AppchainKey(id string) string {
return fmt.Sprintf("%s-%s", Prefix, id)
}
func AppchainOccupyNameKey(name string) string {
return fmt.Sprintf("%s-%s", ChainOccupyNamePrefix, name)
}
func AppchainNameKey(name string) string {
return fmt.Sprintf("%s-%s", NameChainPrefix, name)
}
func AppchainAdminKey(addr string) string {
return fmt.Sprintf("%s-%s", AdminChainPrefix, addr)
}
func AppAdminsChainKey(id string) string {
return fmt.Sprintf("%s-%s", ChainAdminsPrefix, id)
}
func (am *AppchainManager) auditRecordKey(id string) string {
return "audit-record-" + id
}
func (am *AppchainManager) indexMapKey(id string) string {
return fmt.Sprintf("index-tx-%s", id)
}