/
auth_manager_service.go
353 lines (285 loc) · 12.7 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
package auth
import (
"fmt"
"math/big"
"reflect"
"github.com/ethereum/go-ethereum/common"
"github.com/realcoooool/fiscov3-sdk/abi/bind"
"github.com/realcoooool/fiscov3-sdk/client"
"github.com/realcoooool/fiscov3-sdk/core/types"
"github.com/realcoooool/fiscov3-sdk/precompiled"
)
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
contractAuthPrecompiled *ContractAuthPrecompiled
committeeManager *CommitteeManager
committee *Committee
proposalManager *ProposalManager
}
//var committeeAddress = common.HexToAddress("0000000000000000000000000000000000010001")
//var proposalManagerAddress = common.HexToAddress("0000000000000000000000000000000000010001")
var committeeManagerAddress = common.HexToAddress("0000000000000000000000000000000000010001")
var contractAuthPrecompiledAddress = 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()
mgr, err := NewCommitteeManager(committeeManagerAddress, client)
if err != nil {
return nil, fmt.Errorf("NewCommitteeManager construct Service failed, err: %+v", err)
}
pre, err := NewContractAuthPrecompiled(contractAuthPrecompiledAddress, client)
if err != nil {
return nil, fmt.Errorf("NewContractAuthPrecompiled construct Service failed, err: %+v", err)
}
//opts := &bind.CallOpts{From: authManagerAuth.From}
//fmt.Println("xxxxxxxxxxxxxxxxxxxxxxxxxxxxx")
//
//fmt.Printf("services.committeeManager : %s", services.committeeManager)
//
//committeeCon, err := services.committeeManager.CommitteeManagerCaller.Committee(opts)
//
//fmt.Printf("committeeCon : %s", committeeCon)
//fmt.Printf("committeeCon err : %w", err)
//
//committee, err := NewCommittee(committeeCon, client)
//if err != nil {
// return nil, fmt.Errorf("NewCommittee construct Service failed, err: %+v", err)
//}
//
//proposalMgrCon, err := services.committeeManager.ProposalMgr(opts)
//proposalManager, err := NewProposalManager(proposalMgrCon, client)
//if err != nil {
// return nil, fmt.Errorf("NewProposalManager construct Service failed, err: %+v", err)
//}
s := &AuthManagerService{client: client,
authManagerAuth: authManagerAuth,
committeeManager: mgr,
contractAuthPrecompiled: pre,
//committee: committee,
//proposalManager: proposalManager,
}
return s, nil
}
// 6.1 无需权限的查询接口
// get Committee info
func (service *AuthManagerService) GetCommitteeInfo() (c *CommitteeInfo, err error) {
opts := &bind.CallOpts{From: service.authManagerAuth.From}
result, err := service.committee.CommitteeCaller.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
}
// get proposal info
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
}
// get global deploy auth type
func (service *AuthManagerService) GetDeployAuthType() (*big.Int, error) {
opts := &bind.CallOpts{From: service.authManagerAuth.From}
result, err := service.contractAuthPrecompiled.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.contractAuthPrecompiled.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.contractAuthPrecompiled.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.contractAuthPrecompiled.GetAdmin(opts, contractAddr)
if err != nil {
return nil, fmt.Errorf("AuthManagerService GetAdmin failed, err: %v", err)
}
return &result, nil
}
// 6.2 治理委员账号专用接口
// 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, participatesRate, 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
}
// 6.3 合约管理员账号专用接口
// 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.contractAuthPrecompiled.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.contractAuthPrecompiled.OpenMethodAuth(service.client.GetTransactOpts(), contractAddr, funcSelector, account)
if err != nil {
return nil, fmt.Errorf("AuthManagerService OpenMethodAuth failed, err: %v", err)
}
} else {
_, receipt, err = service.contractAuthPrecompiled.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")
}
}
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
}