forked from Consensys/quorum
/
contract.go
372 lines (309 loc) · 11.2 KB
/
contract.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
package v1
import (
"fmt"
"math/big"
"github.com/ethereum/go-ethereum/accounts/abi/bind"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/log"
"github.com/ethereum/go-ethereum/p2p/enode"
"github.com/ethereum/go-ethereum/permission/core"
ptype "github.com/ethereum/go-ethereum/permission/core/types"
pb "github.com/ethereum/go-ethereum/permission/v1/bind"
)
type PermissionModelV1 struct {
ContractBackend ptype.ContractBackend
PermInterf *pb.PermInterface
PermInterfSession *pb.PermInterfaceSession
}
type Audit struct {
Backend *PermissionModelV1
}
type Role struct {
Backend *PermissionModelV1
}
type Control struct {
}
type Org struct {
Backend *PermissionModelV1
}
type Node struct {
Backend *PermissionModelV1
}
type Account struct {
Backend *PermissionModelV1
}
type Init struct {
Backend ptype.ContractBackend
//pb contracts
PermUpgr *pb.PermUpgr
PermImpl *pb.PermImpl
PermInterf *pb.PermInterface
PermNode *pb.NodeManager
PermAcct *pb.AcctManager
PermRole *pb.RoleManager
PermOrg *pb.OrgManager
//sessions
PermInterfSession *pb.PermInterfaceSession
permOrgSession *pb.OrgManagerSession
permNodeSession *pb.NodeManagerSession
permRoleSession *pb.RoleManagerSession
permAcctSession *pb.AcctManagerSession
}
func (i *Init) GetAccountDetailsFromIndex(_aIndex *big.Int) (common.Address, string, string, *big.Int, bool, error) {
return i.permAcctSession.GetAccountDetailsFromIndex(_aIndex)
}
func (i *Init) GetNumberOfAccounts() (*big.Int, error) {
return i.permAcctSession.GetNumberOfAccounts()
}
func (i *Init) GetRoleDetailsFromIndex(_rIndex *big.Int) (struct {
RoleId string
OrgId string
AccessType *big.Int
Voter bool
Admin bool
Active bool
}, error) {
return i.permRoleSession.GetRoleDetailsFromIndex(_rIndex)
}
func (i *Init) GetNumberOfRoles() (*big.Int, error) {
return i.permRoleSession.GetNumberOfRoles()
}
func (i *Init) GetNumberOfOrgs() (*big.Int, error) {
return i.permOrgSession.GetNumberOfOrgs()
}
func (i *Init) UpdateNetworkBootStatus() (*types.Transaction, error) {
return i.PermInterfSession.UpdateNetworkBootStatus()
}
func (i *Init) AddAdminAccount(_acct common.Address) (*types.Transaction, error) {
return i.PermInterfSession.AddAdminAccount(_acct)
}
func (i *Init) AddAdminNode(url string) (*types.Transaction, error) {
return i.PermInterfSession.AddAdminNode(url)
}
func (i *Init) SetPolicy(_nwAdminOrg string, _nwAdminRole string, _oAdminRole string) (*types.Transaction, error) {
return i.PermInterfSession.SetPolicy(_nwAdminOrg, _nwAdminRole, _oAdminRole)
}
func (i *Init) Init(_breadth *big.Int, _depth *big.Int) (*types.Transaction, error) {
return i.PermInterfSession.Init(_breadth, _depth)
}
func (i *Init) GetAccountDetails(_account common.Address) (common.Address, string, string, *big.Int, bool, error) {
return i.permAcctSession.GetAccountDetails(_account)
}
func (i *Init) GetNodeDetailsFromIndex(_nodeIndex *big.Int) (string, string, *big.Int, error) {
r, err := i.permNodeSession.GetNodeDetailsFromIndex(_nodeIndex)
return r.OrgId, r.EnodeId, r.NodeStatus, err
}
func (i *Init) GetNumberOfNodes() (*big.Int, error) {
return i.permNodeSession.GetNumberOfNodes()
}
func (i *Init) GetNodeDetails(enodeId string) (string, string, *big.Int, error) {
r, err := i.permNodeSession.GetNodeDetails(enodeId)
return r.OrgId, r.EnodeId, r.NodeStatus, err
}
func (i *Init) GetRoleDetails(_roleId string, _orgId string) (struct {
RoleId string
OrgId string
AccessType *big.Int
Voter bool
Admin bool
Active bool
}, error) {
return i.permRoleSession.GetRoleDetails(_roleId, _orgId)
}
func (i *Init) GetSubOrgIndexes(_orgId string) ([]*big.Int, error) {
return i.permOrgSession.GetSubOrgIndexes(_orgId)
}
func (i *Init) GetOrgInfo(_orgIndex *big.Int) (string, string, string, *big.Int, *big.Int, error) {
return i.permOrgSession.GetOrgInfo(_orgIndex)
}
func (i *Init) GetNetworkBootStatus() (bool, error) {
return i.PermInterfSession.GetNetworkBootStatus()
}
func (i *Init) GetOrgDetails(_orgId string) (string, string, string, *big.Int, *big.Int, error) {
return i.permOrgSession.GetOrgDetails(_orgId)
}
func (a *Audit) ValidatePendingOp(_authOrg, _orgId, _url string, _account common.Address, _pendingOp int64) bool {
pOrg, pUrl, pAcct, op, err := a.Backend.PermInterfSession.GetPendingOp(_authOrg)
return err == nil && (op.Int64() == _pendingOp && pOrg == _orgId && pUrl == _url && pAcct == _account)
}
func (a *Audit) CheckPendingOp(_orgId string) bool {
_, _, _, op, err := a.Backend.PermInterfSession.GetPendingOp(_orgId)
return err == nil && op.Int64() != 0
}
func (c *Control) ConnectionAllowed(_enodeId, _ip string, _port, _raftPort uint16) (bool, error) {
passedEnodeId, err := enode.ParseV4(_enodeId)
if err != nil {
return false, nil
}
nodeList := core.NodeInfoMap.GetNodeList()
for _, n := range nodeList {
recEnodeId, _ := enode.ParseV4(n.Url)
if recEnodeId.ID() == passedEnodeId.ID() && n.Status == core.NodeApproved {
return true, nil
}
}
return false, nil
}
func (c *Control) TransactionAllowed(_sender common.Address, _target common.Address, _value *big.Int, _gasPrice *big.Int, _gasLimit *big.Int, _payload []byte, transactionType core.TransactionType) error {
accessType := core.GetAcctAccess(_sender)
switch accessType {
case core.ReadOnly:
return ptype.ErrNoPermissionForTxn
case core.Transact:
if transactionType == core.ContractDeployTxn {
return ptype.ErrNoPermissionForTxn
}
return nil
case core.FullAccess, core.ContractDeploy:
return nil
default:
return ptype.ErrNoPermissionForTxn
}
}
func (r *Role) RemoveRole(_args ptype.TxArgs) (*types.Transaction, error) {
return r.Backend.PermInterfSession.RemoveRole(_args.RoleId, _args.OrgId)
}
func (r *Role) AddNewRole(_args ptype.TxArgs) (*types.Transaction, error) {
if _args.AccessType > 3 {
return nil, fmt.Errorf("invalid access type given")
}
return r.Backend.PermInterfSession.AddNewRole(_args.RoleId, _args.OrgId, big.NewInt(int64(_args.AccessType)), _args.IsVoter, _args.IsAdmin)
}
func (o *Org) ApproveOrgStatus(_args ptype.TxArgs) (*types.Transaction, error) {
return o.Backend.PermInterfSession.ApproveOrgStatus(_args.OrgId, big.NewInt(int64(_args.Action)))
}
func (o *Org) UpdateOrgStatus(_args ptype.TxArgs) (*types.Transaction, error) {
return o.Backend.PermInterfSession.UpdateOrgStatus(_args.OrgId, big.NewInt(int64(_args.Action)))
}
func (o *Org) ApproveOrg(_args ptype.TxArgs) (*types.Transaction, error) {
return o.Backend.PermInterfSession.ApproveOrg(_args.OrgId, _args.Url, _args.AcctId)
}
func (o *Org) AddSubOrg(_args ptype.TxArgs) (*types.Transaction, error) {
return o.Backend.PermInterfSession.AddSubOrg(_args.POrgId, _args.OrgId, _args.Url)
}
func (o *Org) AddOrg(_args ptype.TxArgs) (*types.Transaction, error) {
return o.Backend.PermInterfSession.AddOrg(_args.OrgId, _args.Url, _args.AcctId)
}
func (n *Node) ApproveBlacklistedNodeRecovery(_args ptype.TxArgs) (*types.Transaction, error) {
return n.Backend.PermInterfSession.ApproveBlacklistedNodeRecovery(_args.OrgId, _args.Url)
}
func (n *Node) StartBlacklistedNodeRecovery(_args ptype.TxArgs) (*types.Transaction, error) {
return n.Backend.PermInterfSession.StartBlacklistedNodeRecovery(_args.OrgId, _args.Url)
}
func (n *Node) AddNode(_args ptype.TxArgs) (*types.Transaction, error) {
return n.Backend.PermInterfSession.AddNode(_args.OrgId, _args.Url)
}
func (n *Node) UpdateNodeStatus(_args ptype.TxArgs) (*types.Transaction, error) {
return n.Backend.PermInterfSession.UpdateNodeStatus(_args.OrgId, _args.Url, big.NewInt(int64(_args.Action)))
}
func (a *Account) AssignAccountRole(_args ptype.TxArgs) (*types.Transaction, error) {
return a.Backend.PermInterfSession.AssignAccountRole(_args.AcctId, _args.OrgId, _args.RoleId)
}
func (a *Account) UpdateAccountStatus(_args ptype.TxArgs) (*types.Transaction, error) {
return a.Backend.PermInterfSession.UpdateAccountStatus(_args.OrgId, _args.AcctId, big.NewInt(int64(_args.Action)))
}
func (a *Account) StartBlacklistedAccountRecovery(_args ptype.TxArgs) (*types.Transaction, error) {
return a.Backend.PermInterfSession.StartBlacklistedAccountRecovery(_args.OrgId, _args.AcctId)
}
func (a *Account) ApproveBlacklistedAccountRecovery(_args ptype.TxArgs) (*types.Transaction, error) {
return a.Backend.PermInterfSession.ApproveBlacklistedAccountRecovery(_args.OrgId, _args.AcctId)
}
func (a *Account) ApproveAdminRole(_args ptype.TxArgs) (*types.Transaction, error) {
return a.Backend.PermInterfSession.ApproveAdminRole(_args.OrgId, _args.AcctId)
}
func (a *Account) AssignAdminRole(_args ptype.TxArgs) (*types.Transaction, error) {
return a.Backend.PermInterfSession.AssignAdminRole(_args.OrgId, _args.AcctId, _args.RoleId)
}
// This is to make sure all Contr instances are ready and initialized
//
// Required to be call after standard service start lifecycle
func (i *Init) BindContracts() error {
log.Debug("permission service: binding contracts")
err := i.bindContract()
if err != nil {
return err
}
i.initSession()
return nil
}
func (i *Init) bindContract() error {
if err := ptype.BindContract(&i.PermUpgr, func() (interface{}, error) {
return pb.NewPermUpgr(i.Backend.PermConfig.UpgrdAddress, i.Backend.EthClnt)
}); err != nil {
return err
}
if err := ptype.BindContract(&i.PermImpl, func() (interface{}, error) {
return pb.NewPermImpl(i.Backend.PermConfig.ImplAddress, i.Backend.EthClnt)
}); err != nil {
return err
}
if err := ptype.BindContract(&i.PermInterf, func() (interface{}, error) {
return pb.NewPermInterface(i.Backend.PermConfig.InterfAddress, i.Backend.EthClnt)
}); err != nil {
return err
}
if err := ptype.BindContract(&i.PermAcct, func() (interface{}, error) {
return pb.NewAcctManager(i.Backend.PermConfig.AccountAddress, i.Backend.EthClnt)
}); err != nil {
return err
}
if err := ptype.BindContract(&i.PermNode, func() (interface{}, error) {
return pb.NewNodeManager(i.Backend.PermConfig.NodeAddress, i.Backend.EthClnt)
}); err != nil {
return err
}
if err := ptype.BindContract(&i.PermRole, func() (interface{}, error) {
return pb.NewRoleManager(i.Backend.PermConfig.RoleAddress, i.Backend.EthClnt)
}); err != nil {
return err
}
if err := ptype.BindContract(&i.PermOrg, func() (interface{}, error) {
return pb.NewOrgManager(i.Backend.PermConfig.OrgAddress, i.Backend.EthClnt)
}); err != nil {
return err
}
return nil
}
func (i *Init) initSession() {
auth := bind.NewKeyedTransactor(i.Backend.Key)
log.Debug("NodeAccount V1", "nodeAcc", auth.From)
i.PermInterfSession = &pb.PermInterfaceSession{
Contract: i.PermInterf,
CallOpts: bind.CallOpts{
Pending: true,
},
TransactOpts: bind.TransactOpts{
From: auth.From,
Signer: auth.Signer,
GasLimit: 47000000,
GasPrice: big.NewInt(0),
},
}
i.permOrgSession = &pb.OrgManagerSession{
Contract: i.PermOrg,
CallOpts: bind.CallOpts{
Pending: true,
},
}
i.permNodeSession = &pb.NodeManagerSession{
Contract: i.PermNode,
CallOpts: bind.CallOpts{
Pending: true,
},
}
//populate roles
i.permRoleSession = &pb.RoleManagerSession{
Contract: i.PermRole,
CallOpts: bind.CallOpts{
Pending: true,
},
}
//populate accounts
i.permAcctSession = &pb.AcctManagerSession{
Contract: i.PermAcct,
CallOpts: bind.CallOpts{
Pending: true,
},
}
}