github.com/kisexp/xdchain@v0.0.0-20211206025815-490d6b732aa7/permission/v2/contract.go (about)

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