github.com/kisexp/xdchain@v0.0.0-20211206025815-490d6b732aa7/permission/v1/contract.go (about) 1 package v1 2 3 import ( 4 "fmt" 5 "math/big" 6 7 "github.com/kisexp/xdchain/accounts/abi/bind" 8 "github.com/kisexp/xdchain/common" 9 "github.com/kisexp/xdchain/core/types" 10 "github.com/kisexp/xdchain/log" 11 "github.com/kisexp/xdchain/p2p/enode" 12 "github.com/kisexp/xdchain/permission/core" 13 ptype "github.com/kisexp/xdchain/permission/core/types" 14 pb "github.com/kisexp/xdchain/permission/v1/bind" 15 ) 16 17 type PermissionModelV1 struct { 18 ContractBackend ptype.ContractBackend 19 PermInterf *pb.PermInterface 20 PermInterfSession *pb.PermInterfaceSession 21 } 22 23 type Audit struct { 24 Backend *PermissionModelV1 25 } 26 27 type Role struct { 28 Backend *PermissionModelV1 29 } 30 31 type Control struct { 32 } 33 34 type Org struct { 35 Backend *PermissionModelV1 36 } 37 38 type Node struct { 39 Backend *PermissionModelV1 40 } 41 42 type Account struct { 43 Backend *PermissionModelV1 44 } 45 46 type Init struct { 47 Backend ptype.ContractBackend 48 49 //pb contracts 50 PermUpgr *pb.PermUpgr 51 PermImpl *pb.PermImpl 52 PermInterf *pb.PermInterface 53 PermNode *pb.NodeManager 54 PermAcct *pb.AcctManager 55 PermRole *pb.RoleManager 56 PermOrg *pb.OrgManager 57 58 //sessions 59 PermInterfSession *pb.PermInterfaceSession 60 permOrgSession *pb.OrgManagerSession 61 permNodeSession *pb.NodeManagerSession 62 permRoleSession *pb.RoleManagerSession 63 permAcctSession *pb.AcctManagerSession 64 } 65 66 func (i *Init) GetAccountDetailsFromIndex(_aIndex *big.Int) (common.Address, string, string, *big.Int, bool, error) { 67 return i.permAcctSession.GetAccountDetailsFromIndex(_aIndex) 68 } 69 70 func (i *Init) GetNumberOfAccounts() (*big.Int, error) { 71 return i.permAcctSession.GetNumberOfAccounts() 72 } 73 74 func (i *Init) GetRoleDetailsFromIndex(_rIndex *big.Int) (struct { 75 RoleId string 76 OrgId string 77 AccessType *big.Int 78 Voter bool 79 Admin bool 80 Active bool 81 }, error) { 82 return i.permRoleSession.GetRoleDetailsFromIndex(_rIndex) 83 } 84 85 func (i *Init) GetNumberOfRoles() (*big.Int, error) { 86 return i.permRoleSession.GetNumberOfRoles() 87 } 88 89 func (i *Init) GetNumberOfOrgs() (*big.Int, error) { 90 return i.permOrgSession.GetNumberOfOrgs() 91 } 92 93 func (i *Init) UpdateNetworkBootStatus() (*types.Transaction, error) { 94 return i.PermInterfSession.UpdateNetworkBootStatus() 95 } 96 97 func (i *Init) AddAdminAccount(_acct common.Address) (*types.Transaction, error) { 98 return i.PermInterfSession.AddAdminAccount(_acct) 99 } 100 101 func (i *Init) AddAdminNode(url string) (*types.Transaction, error) { 102 return i.PermInterfSession.AddAdminNode(url) 103 } 104 105 func (i *Init) SetPolicy(_nwAdminOrg string, _nwAdminRole string, _oAdminRole string) (*types.Transaction, error) { 106 return i.PermInterfSession.SetPolicy(_nwAdminOrg, _nwAdminRole, _oAdminRole) 107 } 108 109 func (i *Init) Init(_breadth *big.Int, _depth *big.Int) (*types.Transaction, error) { 110 return i.PermInterfSession.Init(_breadth, _depth) 111 } 112 113 func (i *Init) GetAccountDetails(_account common.Address) (common.Address, string, string, *big.Int, bool, error) { 114 return i.permAcctSession.GetAccountDetails(_account) 115 } 116 117 func (i *Init) GetNodeDetailsFromIndex(_nodeIndex *big.Int) (string, string, *big.Int, error) { 118 r, err := i.permNodeSession.GetNodeDetailsFromIndex(_nodeIndex) 119 return r.OrgId, r.EnodeId, r.NodeStatus, err 120 } 121 122 func (i *Init) GetNumberOfNodes() (*big.Int, error) { 123 return i.permNodeSession.GetNumberOfNodes() 124 } 125 126 func (i *Init) GetNodeDetails(enodeId string) (string, string, *big.Int, error) { 127 r, err := i.permNodeSession.GetNodeDetails(enodeId) 128 return r.OrgId, r.EnodeId, r.NodeStatus, err 129 } 130 131 func (i *Init) GetRoleDetails(_roleId string, _orgId string) (struct { 132 RoleId string 133 OrgId string 134 AccessType *big.Int 135 Voter bool 136 Admin bool 137 Active bool 138 }, error) { 139 return i.permRoleSession.GetRoleDetails(_roleId, _orgId) 140 } 141 142 func (i *Init) GetSubOrgIndexes(_orgId string) ([]*big.Int, error) { 143 return i.permOrgSession.GetSubOrgIndexes(_orgId) 144 } 145 146 func (i *Init) GetOrgInfo(_orgIndex *big.Int) (string, string, string, *big.Int, *big.Int, error) { 147 return i.permOrgSession.GetOrgInfo(_orgIndex) 148 } 149 150 func (i *Init) GetNetworkBootStatus() (bool, error) { 151 return i.PermInterfSession.GetNetworkBootStatus() 152 } 153 154 func (i *Init) GetOrgDetails(_orgId string) (string, string, string, *big.Int, *big.Int, error) { 155 return i.permOrgSession.GetOrgDetails(_orgId) 156 } 157 158 func (a *Audit) ValidatePendingOp(_authOrg, _orgId, _url string, _account common.Address, _pendingOp int64) bool { 159 pOrg, pUrl, pAcct, op, err := a.Backend.PermInterfSession.GetPendingOp(_authOrg) 160 return err == nil && (op.Int64() == _pendingOp && pOrg == _orgId && pUrl == _url && pAcct == _account) 161 } 162 163 func (a *Audit) CheckPendingOp(_orgId string) bool { 164 _, _, _, op, err := a.Backend.PermInterfSession.GetPendingOp(_orgId) 165 return err == nil && op.Int64() != 0 166 } 167 168 func (c *Control) ConnectionAllowed(_enodeId, _ip string, _port, _raftPort uint16) (bool, error) { 169 passedEnodeId, err := enode.ParseV4(_enodeId) 170 if err != nil { 171 return false, nil 172 } 173 nodeList := core.NodeInfoMap.GetNodeList() 174 for _, n := range nodeList { 175 recEnodeId, _ := enode.ParseV4(n.Url) 176 if recEnodeId.ID() == passedEnodeId.ID() && n.Status == core.NodeApproved { 177 return true, nil 178 } 179 } 180 return false, nil 181 } 182 183 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 { 184 accessType := core.GetAcctAccess(_sender) 185 switch accessType { 186 case core.ReadOnly: 187 return ptype.ErrNoPermissionForTxn 188 189 case core.Transact: 190 if transactionType == core.ContractDeployTxn { 191 return ptype.ErrNoPermissionForTxn 192 } 193 return nil 194 195 case core.FullAccess, core.ContractDeploy: 196 return nil 197 198 default: 199 return ptype.ErrNoPermissionForTxn 200 201 } 202 } 203 204 func (r *Role) RemoveRole(_args ptype.TxArgs) (*types.Transaction, error) { 205 return r.Backend.PermInterfSession.RemoveRole(_args.RoleId, _args.OrgId) 206 } 207 208 func (r *Role) AddNewRole(_args ptype.TxArgs) (*types.Transaction, error) { 209 if _args.AccessType > 3 { 210 return nil, fmt.Errorf("invalid access type given") 211 } 212 return r.Backend.PermInterfSession.AddNewRole(_args.RoleId, _args.OrgId, big.NewInt(int64(_args.AccessType)), _args.IsVoter, _args.IsAdmin) 213 } 214 215 func (o *Org) ApproveOrgStatus(_args ptype.TxArgs) (*types.Transaction, error) { 216 return o.Backend.PermInterfSession.ApproveOrgStatus(_args.OrgId, big.NewInt(int64(_args.Action))) 217 } 218 219 func (o *Org) UpdateOrgStatus(_args ptype.TxArgs) (*types.Transaction, error) { 220 return o.Backend.PermInterfSession.UpdateOrgStatus(_args.OrgId, big.NewInt(int64(_args.Action))) 221 } 222 223 func (o *Org) ApproveOrg(_args ptype.TxArgs) (*types.Transaction, error) { 224 return o.Backend.PermInterfSession.ApproveOrg(_args.OrgId, _args.Url, _args.AcctId) 225 } 226 227 func (o *Org) AddSubOrg(_args ptype.TxArgs) (*types.Transaction, error) { 228 return o.Backend.PermInterfSession.AddSubOrg(_args.POrgId, _args.OrgId, _args.Url) 229 } 230 231 func (o *Org) AddOrg(_args ptype.TxArgs) (*types.Transaction, error) { 232 return o.Backend.PermInterfSession.AddOrg(_args.OrgId, _args.Url, _args.AcctId) 233 } 234 235 func (n *Node) ApproveBlacklistedNodeRecovery(_args ptype.TxArgs) (*types.Transaction, error) { 236 return n.Backend.PermInterfSession.ApproveBlacklistedNodeRecovery(_args.OrgId, _args.Url) 237 } 238 239 func (n *Node) StartBlacklistedNodeRecovery(_args ptype.TxArgs) (*types.Transaction, error) { 240 return n.Backend.PermInterfSession.StartBlacklistedNodeRecovery(_args.OrgId, _args.Url) 241 } 242 243 func (n *Node) AddNode(_args ptype.TxArgs) (*types.Transaction, error) { 244 return n.Backend.PermInterfSession.AddNode(_args.OrgId, _args.Url) 245 } 246 247 func (n *Node) UpdateNodeStatus(_args ptype.TxArgs) (*types.Transaction, error) { 248 return n.Backend.PermInterfSession.UpdateNodeStatus(_args.OrgId, _args.Url, big.NewInt(int64(_args.Action))) 249 } 250 251 func (a *Account) AssignAccountRole(_args ptype.TxArgs) (*types.Transaction, error) { 252 return a.Backend.PermInterfSession.AssignAccountRole(_args.AcctId, _args.OrgId, _args.RoleId) 253 } 254 255 func (a *Account) UpdateAccountStatus(_args ptype.TxArgs) (*types.Transaction, error) { 256 return a.Backend.PermInterfSession.UpdateAccountStatus(_args.OrgId, _args.AcctId, big.NewInt(int64(_args.Action))) 257 } 258 259 func (a *Account) StartBlacklistedAccountRecovery(_args ptype.TxArgs) (*types.Transaction, error) { 260 return a.Backend.PermInterfSession.StartBlacklistedAccountRecovery(_args.OrgId, _args.AcctId) 261 } 262 263 func (a *Account) ApproveBlacklistedAccountRecovery(_args ptype.TxArgs) (*types.Transaction, error) { 264 return a.Backend.PermInterfSession.ApproveBlacklistedAccountRecovery(_args.OrgId, _args.AcctId) 265 } 266 267 func (a *Account) ApproveAdminRole(_args ptype.TxArgs) (*types.Transaction, error) { 268 return a.Backend.PermInterfSession.ApproveAdminRole(_args.OrgId, _args.AcctId) 269 } 270 271 func (a *Account) AssignAdminRole(_args ptype.TxArgs) (*types.Transaction, error) { 272 return a.Backend.PermInterfSession.AssignAdminRole(_args.OrgId, _args.AcctId, _args.RoleId) 273 } 274 275 // This is to make sure all Contr instances are ready and initialized 276 // 277 // Required to be call after standard service start lifecycle 278 func (i *Init) BindContracts() error { 279 log.Debug("permission service: binding contracts") 280 err := i.bindContract() 281 if err != nil { 282 return err 283 } 284 i.initSession() 285 return nil 286 } 287 288 func (i *Init) bindContract() error { 289 if err := ptype.BindContract(&i.PermUpgr, func() (interface{}, error) { 290 return pb.NewPermUpgr(i.Backend.PermConfig.UpgrdAddress, i.Backend.EthClnt) 291 }); err != nil { 292 return err 293 } 294 if err := ptype.BindContract(&i.PermImpl, func() (interface{}, error) { 295 return pb.NewPermImpl(i.Backend.PermConfig.ImplAddress, i.Backend.EthClnt) 296 }); err != nil { 297 return err 298 } 299 if err := ptype.BindContract(&i.PermInterf, func() (interface{}, error) { 300 return pb.NewPermInterface(i.Backend.PermConfig.InterfAddress, i.Backend.EthClnt) 301 }); err != nil { 302 return err 303 } 304 if err := ptype.BindContract(&i.PermAcct, func() (interface{}, error) { 305 return pb.NewAcctManager(i.Backend.PermConfig.AccountAddress, i.Backend.EthClnt) 306 }); err != nil { 307 return err 308 } 309 if err := ptype.BindContract(&i.PermNode, func() (interface{}, error) { 310 return pb.NewNodeManager(i.Backend.PermConfig.NodeAddress, i.Backend.EthClnt) 311 }); err != nil { 312 return err 313 } 314 if err := ptype.BindContract(&i.PermRole, func() (interface{}, error) { 315 return pb.NewRoleManager(i.Backend.PermConfig.RoleAddress, i.Backend.EthClnt) 316 }); err != nil { 317 return err 318 } 319 if err := ptype.BindContract(&i.PermOrg, func() (interface{}, error) { 320 return pb.NewOrgManager(i.Backend.PermConfig.OrgAddress, i.Backend.EthClnt) 321 }); err != nil { 322 return err 323 } 324 return nil 325 } 326 327 func (i *Init) initSession() { 328 auth := bind.NewKeyedTransactor(i.Backend.Key) 329 log.Debug("NodeAccount V1", "nodeAcc", auth.From) 330 i.PermInterfSession = &pb.PermInterfaceSession{ 331 Contract: i.PermInterf, 332 CallOpts: bind.CallOpts{ 333 Pending: true, 334 }, 335 TransactOpts: bind.TransactOpts{ 336 From: auth.From, 337 Signer: auth.Signer, 338 GasLimit: 47000000, 339 GasPrice: big.NewInt(0), 340 }, 341 } 342 343 i.permOrgSession = &pb.OrgManagerSession{ 344 Contract: i.PermOrg, 345 CallOpts: bind.CallOpts{ 346 Pending: true, 347 }, 348 } 349 350 i.permNodeSession = &pb.NodeManagerSession{ 351 Contract: i.PermNode, 352 CallOpts: bind.CallOpts{ 353 Pending: true, 354 }, 355 } 356 357 //populate roles 358 i.permRoleSession = &pb.RoleManagerSession{ 359 Contract: i.PermRole, 360 CallOpts: bind.CallOpts{ 361 Pending: true, 362 }, 363 } 364 365 //populate accounts 366 i.permAcctSession = &pb.AcctManagerSession{ 367 Contract: i.PermAcct, 368 CallOpts: bind.CallOpts{ 369 Pending: true, 370 }, 371 } 372 }