-
Notifications
You must be signed in to change notification settings - Fork 57
/
auth_manager_service.go
455 lines (366 loc) · 15.8 KB
/
auth_manager_service.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
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
package auth
import (
"fmt"
"math/big"
"reflect"
"github.com/FISCO-BCOS/go-sdk/v3/abi/bind"
"github.com/FISCO-BCOS/go-sdk/v3/client"
"github.com/FISCO-BCOS/go-sdk/v3/precompiled"
"github.com/FISCO-BCOS/go-sdk/v3/types"
"github.com/ethereum/go-ethereum/common"
)
type ProposalInfo struct {
ResourceId common.Address
Proposer common.Address
ProposalType uint8
BlockNumberInterval *big.Int
Status uint8
AgreeVoters []common.Address
AgainstVoters []common.Address
}
type CommitteeInfo struct {
ParticipatesRate uint8
WinRate uint8
Governors []common.Address
Weights []uint32
}
type AuthManagerService struct {
client *client.Client
authManagerAuth *bind.TransactOpts
contractAuth *ContractAuth
accountManager *AccountManager
committee *Committee
committeeManager *CommitteeManager
proposalManager *ProposalManager
}
const (
ACCOUNT_STATUS_NORMAL = iota
ACCOUNT_STATUS_FREEZE
ACCOUNT_STATUS_ABOLISH
)
var committeeManagerAddress = common.HexToAddress("0000000000000000000000000000000000010001")
var accountManagerAddress = common.HexToAddress("0000000000000000000000000000000000010003")
var contractAuthAddress = common.HexToAddress("0000000000000000000000000000000000001005")
var DEFAULT_BLOCK_NUMBER_INTERVAL = big.NewInt(3600 * 24 * 7)
func NewAuthManagerService(client *client.Client) (services *AuthManagerService, err error) {
authManagerAuth := client.GetTransactOpts()
accountManagerInstance, err := NewAccountManager(accountManagerAddress, client)
if err != nil {
return nil, fmt.Errorf("NewAccountManager construct Service failed: %+v", err)
}
committeeManagerInstance, err := NewCommitteeManager(committeeManagerAddress, client)
if err != nil {
return nil, fmt.Errorf("NewCommitteeManager construct Service failed, err: %+v", err)
}
contractAuthInstance, err := NewContractAuth(contractAuthAddress, client)
if err != nil {
return nil, fmt.Errorf("NewContractAuth construct Service failed, err: %+v", err)
}
opts := &bind.CallOpts{From: authManagerAuth.From}
committeeCon, err := committeeManagerInstance.Committee(opts)
committeeInstance, err := NewCommittee(committeeCon, client)
if err != nil {
return nil, fmt.Errorf("NewCommittee construct Service failed, err: %+v", err)
}
proposalMgrCon, err := committeeManagerInstance.ProposalMgr(opts)
proposalManagerInstance, err := NewProposalManager(proposalMgrCon, client)
if err != nil {
return nil, fmt.Errorf("NewProposalManager construct Service failed, err: %+v", err)
}
s := &AuthManagerService{client: client,
authManagerAuth: authManagerAuth,
accountManager: accountManagerInstance,
committeeManager: committeeManagerInstance,
committee: committeeInstance,
contractAuth: contractAuthInstance,
proposalManager: proposalManagerInstance,
}
return s, nil
}
/**
* **************************************************************************************************************
* AccountManager
* **************************************************************************************************************
**/
/**
* *************************************************
* 无需权限的查询接口
* *************************************************
**/
func (service *AuthManagerService) GetAccountStatus(addr common.Address) (uint8, error) {
opts := &bind.CallOpts{From: service.authManagerAuth.From}
ret0, err := service.accountManager.GetAccountStatus(opts, addr)
return ret0, err
}
/**
* *************************************************
* 治理委员账号专用接口
* *************************************************
**/
func (service *AuthManagerService) SetAccountStatus(addr common.Address, status uint8) (int64, error) {
ret0, _, _, err := service.accountManager.SetAccountStatus(service.authManagerAuth, addr, status)
if err != nil {
return precompiled.DefaultErrorCode, fmt.Errorf("AccountManagerService SetAccountStatus failed: %v", err)
}
return int64(ret0), err
}
/**
* **************************************************************************************************************
* Committee
* **************************************************************************************************************
**/
/**
* *************************************************
* 无需权限的查询接口
* *************************************************
**/
func (service *AuthManagerService) GetCommitteeInfo() (c *CommitteeInfo, err error) {
opts := &bind.CallOpts{From: service.authManagerAuth.From}
result, err := service.committee.GetCommitteeInfo(opts)
if err != nil {
return nil, fmt.Errorf("AuthManagerService GetCommitteeInfo failed, err: %v", err)
}
if reflect.DeepEqual(result, CommitteeInfo{}) {
return nil, fmt.Errorf("AuthManagerService GetCommitteeInfo is empty, err: %v", err)
}
var info CommitteeInfo
info.Governors = result.Governors
info.WinRate = result.WinRate
info.ParticipatesRate = result.ParticipatesRate
info.Weights = result.Weights
return &info, nil
}
/**
* **************************************************************************************************************
* ProposalManager
* **************************************************************************************************************
**/
/**
* *************************************************
* 无需权限的查询接口
* *************************************************
**/
func (service *AuthManagerService) GetProposalInfo(proposalId *big.Int) (proposalInfo *ProposalInfo, err error) {
opts := &bind.CallOpts{From: service.authManagerAuth.From}
result, err := service.proposalManager.GetProposalInfo(opts, proposalId)
if err != nil {
return nil, fmt.Errorf("AuthManagerService GetProposalInfo failed, err: %v", err)
}
if reflect.DeepEqual(result, ProposalInfo{}) {
return nil, fmt.Errorf("AuthManagerService GetProposalInfo is empty, err: %v", err)
}
var info ProposalInfo
info.ResourceId = result.ResourceId
info.Proposer = result.Proposer
info.ProposalType = result.ProposalType
info.BlockNumberInterval = result.BlockNumberInterval
info.Status = result.Status
info.AgreeVoters = result.AgreeVoters
info.AgainstVoters = result.AgainstVoters
return &info, nil
}
func (service *AuthManagerService) ProposalCount() (*big.Int, error) {
opts := &bind.CallOpts{From: service.authManagerAuth.From}
result, err := service.proposalManager.ProposalCount(opts)
if err != nil {
return nil, fmt.Errorf("AuthManagerService ProposalCount failed, err: %v", err)
}
return result, err
}
func (service *AuthManagerService) GetProposalStatus(proposalId *big.Int) (uint8, error) {
opts := &bind.CallOpts{From: service.authManagerAuth.From}
result, err := service.proposalManager.GetProposalStatus(opts, proposalId)
return result, err
}
/**
* **************************************************************************************************************
* ContractAuth
* **************************************************************************************************************
**/
/**
* *************************************************
* 无需权限的查询接口
* *************************************************
**/
// get global deploy auth type
func (service *AuthManagerService) GetDeployAuthType() (*big.Int, error) {
opts := &bind.CallOpts{From: service.authManagerAuth.From}
result, err := service.contractAuth.DeployType(opts)
if err != nil {
return nil, fmt.Errorf("AuthManagerService GetDeployAuthType failed, err: %v", err)
}
return result, nil
}
// check the account whether this account can deploy contract
func (service *AuthManagerService) CheckDeployAuth(account common.Address) (*bool, error) {
opts := &bind.CallOpts{From: service.authManagerAuth.From}
result, err := service.contractAuth.HasDeployAuth(opts, account)
if err != nil {
return nil, fmt.Errorf("AuthManagerService CheckDeployAuth failed, err: %v", err)
}
return &result, nil
}
// check the contract interface func whether this account can call
func (service *AuthManagerService) CheckMethodAuth(contractAddr common.Address, funcSelector [4]byte, account common.Address) (*bool, error) {
opts := &bind.CallOpts{From: service.authManagerAuth.From}
result, err := service.contractAuth.CheckMethodAuth(opts, contractAddr, funcSelector, account)
if err != nil {
return nil, fmt.Errorf("AuthManagerService CheckMethodAuth failed, err: %v", err)
}
return &result, nil
}
// get a specific contract admin
func (service *AuthManagerService) GetAdmin(contractAddr common.Address) (account *common.Address, err error) {
opts := &bind.CallOpts{From: service.authManagerAuth.From}
result, err := service.contractAuth.GetAdmin(opts, contractAddr)
if err != nil {
return nil, fmt.Errorf("AuthManagerService GetAdmin failed, err: %v", err)
}
return &result, nil
}
/**
* *************************************************
* 合约管理员账号专用接口
* *************************************************
**/
// set a specific contract's method auth type, only contract admin can call it
// authType white_list or black_list
func (service *AuthManagerService) SetMethodAuthType(contractAddr common.Address, funcSelector [4]byte, authType uint8) (rtCode *big.Int, err error) {
_, _, receipt, err := service.contractAuth.SetMethodAuthType(service.client.GetTransactOpts(), contractAddr, funcSelector, authType)
if err != nil {
return nil, fmt.Errorf("AuthManagerService SetMethodAuthType failed, err: %v", err)
}
if receipt.Status != 0 {
return nil, fmt.Errorf("AuthManagerService SetMethodAuthType failed, ErrorMessage: %v", receipt.GetErrorMessage())
}
return parseReturnValue(receipt, "setMethodAuthType")
}
// set a specific contract's method ACL, only contract admin can call it
// isOpen if open, then white_list type is true, black_list is false; if close, then white_list type is false, black_list is true
func (service *AuthManagerService) SetMethodAuth(contractAddr common.Address, funcSelector [4]byte, account common.Address, isOpen bool) (rtCode *big.Int, err error) {
var receipt *types.Receipt
if isOpen {
_, _, receipt, err = service.contractAuth.OpenMethodAuth(service.client.GetTransactOpts(), contractAddr, funcSelector, account)
if err != nil {
return nil, fmt.Errorf("AuthManagerService OpenMethodAuth failed, err: %v", err)
}
} else {
_, _, receipt, err = service.contractAuth.CloseMethodAuth(service.client.GetTransactOpts(), contractAddr, funcSelector, account)
if err != nil {
return nil, fmt.Errorf("AuthManagerService CloseMethodAuth failed, err: %v", err)
}
}
if receipt.Status != 0 {
return nil, fmt.Errorf("AuthManagerService SetMethodAuth failed, ErrorMessage: %v", receipt.GetErrorMessage())
}
if isOpen {
return parseReturnValue(receipt, "openMethodAuth")
} else {
return parseReturnValue(receipt, "closeMethodAuth")
}
}
/**
* **************************************************************************************************************
* CommitteeManager
* **************************************************************************************************************
**/
/**
* *************************************************
* 治理委员账号专用接口
* *************************************************
**/
// apply for update governor, only governor can call it
// account new governor address
// weight 0 == delete, bigger than 0 == update or insert
func (service *AuthManagerService) UpdateGovernor(account common.Address, weight uint32) (proposalId *big.Int, err error) {
_, _, receipt, err := service.committeeManager.
CreateUpdateGovernorProposal(service.client.GetTransactOpts(), account, weight, DEFAULT_BLOCK_NUMBER_INTERVAL)
if err != nil {
return nil, fmt.Errorf("AuthManagerService UpdateGovernor failed, err: %v", err)
}
if receipt.Status != 0 {
return nil, fmt.Errorf("AuthManagerService UpdateGovernor failed, ErrorMessage: %v", receipt.GetErrorMessage())
}
return parseReturnValue(receipt, "createUpdateGovernorProposal")
}
// apply set participate rate and win rate. only governor can call it
// participatesRate [0,100]. if 0, always succeed.
// winRate [0,100].
func (service *AuthManagerService) SetRate(participatesRate uint8, winRate uint8) (proposalId *big.Int, err error) {
_, _, receipt, err := service.committeeManager.
CreateSetRateProposal(service.client.GetTransactOpts(), participatesRate, winRate, DEFAULT_BLOCK_NUMBER_INTERVAL)
if err != nil {
return nil, fmt.Errorf("AuthManagerService SetRate failed, err: %v", err)
}
if receipt.Status != 0 {
return nil, fmt.Errorf("AuthManagerService SetRate failed, ErrorMessage: %v", receipt.GetErrorMessage())
}
return parseReturnValue(receipt, "createSetRateProposal")
}
// submit a proposal of setting deploy contract auth type, only governor can call it
// deployAuthType 1-whitelist; 2-blacklist
func (service *AuthManagerService) SetDeployAuthType(deployAuthType uint8) (proposalId *big.Int, err error) {
_, _, receipt, err := service.committeeManager.
CreateSetDeployAuthTypeProposal(service.client.GetTransactOpts(), deployAuthType, DEFAULT_BLOCK_NUMBER_INTERVAL)
if err != nil {
return nil, fmt.Errorf("AuthManagerService SetDeployAuthType failed, err: %v", err)
}
if receipt.Status != 0 {
return nil, fmt.Errorf("AuthManagerService SetDeployAuthType failed, ErrorMessage: %v", receipt.GetErrorMessage())
}
return parseReturnValue(receipt, "createSetDeployAuthTypeProposal")
}
// submit a proposal of adding deploy contract auth for account, only governor can call it
// openFlag true-open; false-close
func (service *AuthManagerService) ModifyDeployAuth(account common.Address, openFlag bool) (proposalId *big.Int, err error) {
_, _, receipt, err := service.committeeManager.
CreateModifyDeployAuthProposal(service.client.GetTransactOpts(), account, openFlag, DEFAULT_BLOCK_NUMBER_INTERVAL)
if err != nil {
return nil, fmt.Errorf("AuthManagerService ModifyDeployAuth failed, err: %v", err)
}
if receipt.Status != 0 {
return nil, fmt.Errorf("AuthManagerService ModifyDeployAuth failed, ErrorMessage: %v", receipt.GetErrorMessage())
}
return parseReturnValue(receipt, "createModifyDeployAuthProposal")
}
// submit a proposal of resetting contract admin, only governor can call it
func (service *AuthManagerService) ResetAdmin(newAdmin common.Address, contractAddr common.Address) (proposalId *big.Int, err error) {
_, _, receipt, err := service.committeeManager.
CreateResetAdminProposal(service.client.GetTransactOpts(), newAdmin, contractAddr, DEFAULT_BLOCK_NUMBER_INTERVAL)
if err != nil {
return nil, fmt.Errorf("AuthManagerService ResetAdmin failed, err: %v", err)
}
if receipt.Status != 0 {
return nil, fmt.Errorf("AuthManagerService ResetAdmin failed, ErrorMessage: %v", receipt.GetErrorMessage())
}
return parseReturnValue(receipt, "createResetAdminProposal")
}
// revoke proposal, only governor can call it
func (service *AuthManagerService) RevokeProposal(proposalId big.Int) (receipt *types.Receipt, err error) {
_, receipt, err = service.committeeManager.RevokeProposal(service.client.GetTransactOpts(), &proposalId)
if err != nil {
return nil, fmt.Errorf("AuthManagerService RevokeProposal failed, err: %v", err)
}
return receipt, nil
}
// unified vote, only governor can call it
func (service *AuthManagerService) VoteProposal(proposalId big.Int, agree bool) (receipt *types.Receipt, err error) {
_, receipt, err = service.committeeManager.VoteProposal(service.client.GetTransactOpts(), &proposalId, agree)
if err != nil {
return nil, fmt.Errorf("AuthManagerService VoteProposal failed, err: %v", err)
}
return receipt, nil
}
func parseReturnValue(receipt *types.Receipt, name string) (*big.Int, error) {
// todo
// fmt.Println(receipt)
errorMessage := receipt.GetErrorMessage()
if errorMessage != "" {
return nil, fmt.Errorf("receipt.Status err: %v", errorMessage)
}
bigNum, err := precompiled.ParseBigIntFromOutput(receipt)
if err != nil {
return nil, fmt.Errorf("parseReturnValue failed, err: %v", err)
}
return bigNum, nil
}