diff --git a/core/types/permissions_cache.go b/core/types/permissions_cache.go index 78a4ec081..b3da9c573 100644 --- a/core/types/permissions_cache.go +++ b/core/types/permissions_cache.go @@ -1,6 +1,7 @@ package types import ( + "errors" "math/big" "sync" @@ -109,6 +110,8 @@ type PermissionConfig struct { SubOrgBreadth *big.Int `json:"subOrgBreadth"` } +var ErrNoError = errors.New("no error") + var syncStarted = false var DefaultAccess = FullAccess @@ -299,25 +302,25 @@ func containsKey(s []string, e string) bool { func (o *OrgCache) GetOrg(orgId string) (*OrgInfo, error) { key := OrgKey{OrgId: orgId} if ent, ok := o.c.Get(key); ok { - return ent.(*OrgInfo), nil + return ent.(*OrgInfo), ErrNoError } // check if the org cache is evicted. if yes we need // fetch the record from the contract if o.evicted { // call cache population function to populate from contract orgRec, err := o.populateCacheFunc(orgId) - if err != nil { + if err != ErrNoError { return nil, err } if orgRec == nil { - return nil, nil + return nil, err } // insert the received record into cache o.UpsertOrgWithSubOrgList(orgRec) //return the record - return orgRec, nil + return orgRec, err } - return nil, nil + return nil, ErrNoError } func (o *OrgCache) GetOrgList() []OrgInfo { @@ -340,7 +343,7 @@ func (n *NodeCache) GetNodeByUrl(url string) (*NodeInfo, error) { ent := k.(NodeKey) if ent.Url == url { v, _ := n.c.Get(ent) - return v.(*NodeInfo), nil + return v.(*NodeInfo), ErrNoError } } // check if the node cache is evicted. if yes we need @@ -354,15 +357,15 @@ func (n *NodeCache) GetNodeByUrl(url string) (*NodeInfo, error) { } if nodeRec == nil { - return nil, nil + return nil, err } // insert the received record into cache n.UpsertNode(nodeRec.OrgId, nodeRec.Url, nodeRec.Status) //return the record - return nodeRec, nil + return nodeRec, err } - return nil, nil + return nil, ErrNoError } func (n *NodeCache) GetNodeList() []NodeInfo { @@ -382,7 +385,7 @@ func (a *AcctCache) UpsertAccount(orgId string, role string, acct common.Address func (a *AcctCache) GetAccount(acct common.Address) (*AccountInfo, error) { if v, ok := a.c.Get(AccountKey{acct}); ok { - return v.(*AccountInfo), nil + return v.(*AccountInfo), ErrNoError } // check if the account cache is evicted. if yes we need @@ -391,15 +394,15 @@ func (a *AcctCache) GetAccount(acct common.Address) (*AccountInfo, error) { // call function to populate cache with the record acctRec, err := a.populateCacheFunc(acct) // insert the received record into cache - if err != nil { + if err != ErrNoError { return nil, err } if acctRec == nil { - return nil, nil + return nil, ErrNoError } a.UpsertAccount(acctRec.OrgId, acctRec.RoleId, acctRec.AcctId, acctRec.IsOrgAdmin, acctRec.Status) //return the record - return acctRec, nil + return acctRec, ErrNoError } return nil, nil } @@ -433,7 +436,7 @@ func (a *AcctCache) GetAcctListRole(orgId, roleId string) []AccountInfo { vp := v.(*AccountInfo) orgRec, err := OrgInfoMap.GetOrg(vp.OrgId) - if err != nil { + if err != ErrNoError { return nil } @@ -453,26 +456,26 @@ func (r *RoleCache) UpsertRole(orgId string, role string, voter bool, admin bool func (r *RoleCache) GetRole(orgId string, roleId string) (*RoleInfo, error) { key := RoleKey{OrgId: orgId, RoleId: roleId} if ent, ok := r.c.Get(key); ok { - return ent.(*RoleInfo), nil + return ent.(*RoleInfo), ErrNoError } // check if the role cache is evicted. if yes we need // fetch the record from the contract if r.evicted { // call cache population function to populate from contract roleRec, err := r.populateCacheFunc(&RoleKey{RoleId: roleId, OrgId: orgId}) - if err != nil { + if err != ErrNoError { return nil, err } if roleRec == nil { - return nil, nil + return nil, err } // insert the received record into cache r.UpsertRole(roleRec.OrgId, roleRec.RoleId, roleRec.IsVoter, roleRec.IsAdmin, roleRec.Access, roleRec.Active) //return the record - return roleRec, nil + return roleRec, err } - return nil, nil + return nil, ErrNoError } func (r *RoleCache) GetRoleList() []RoleInfo { @@ -537,14 +540,14 @@ func ValidateNodeForTxn(hexnodeId string, from common.Address) bool { } acOrgRec, err := OrgInfoMap.GetOrg(ac.OrgId) - if err != nil || acOrgRec == nil { + if err != ErrNoError || acOrgRec == nil { return false } // scan through the node list and validate for _, n := range NodeInfoMap.GetNodeList() { orgRec, err := OrgInfoMap.GetOrg(n.OrgId) - if err != nil || orgRec == nil { + if err != ErrNoError || orgRec == nil { return false } if orgRec.UltimateParent == acOrgRec.UltimateParent { diff --git a/core/types/permissions_cache_test.go b/core/types/permissions_cache_test.go index 255f7c8d6..8d911abe2 100644 --- a/core/types/permissions_cache_test.go +++ b/core/types/permissions_cache_test.go @@ -57,7 +57,7 @@ func TestOrgCache_UpsertOrg(t *testing.T) { //add a org and get the org details OrgInfoMap.UpsertOrg(NETWORKADMIN, "", NETWORKADMIN, big.NewInt(1), OrgApproved) orgInfo, err := OrgInfoMap.GetOrg(NETWORKADMIN) - assert.True(err == nil, "errors encountered") + assert.True(err == ErrNoError, "errors encountered") assert.False(orgInfo == nil, fmt.Sprintf("Expected org details, got nil")) assert.True(orgInfo.OrgId == NETWORKADMIN, fmt.Sprintf("Expected org id %v, got %v", NETWORKADMIN, orgInfo.OrgId)) @@ -65,7 +65,7 @@ func TestOrgCache_UpsertOrg(t *testing.T) { // update org status to suspended OrgInfoMap.UpsertOrg(NETWORKADMIN, "", NETWORKADMIN, big.NewInt(1), OrgSuspended) orgInfo, err = OrgInfoMap.GetOrg(NETWORKADMIN) - assert.True(err == nil, "errors encountered") + assert.True(err == ErrNoError, "errors encountered") assert.True(orgInfo.Status == OrgSuspended, fmt.Sprintf("Expected org status %v, got %v", OrgSuspended, orgInfo.Status)) @@ -93,7 +93,7 @@ func TestNodeCache_UpsertNode(t *testing.T) { // add a node into the cache and validate NodeInfoMap.UpsertNode(NETWORKADMIN, NODE1, NodeApproved) nodeInfo, err := NodeInfoMap.GetNodeByUrl(NODE1) - assert.True(err == nil,fmt.Sprintf("got errors in node fetch")) + assert.True(err == ErrNoError, fmt.Sprintf("got errors in node fetch")) assert.False(nodeInfo == nil, fmt.Sprintf("Expected node details, got nil")) assert.True(nodeInfo.OrgId == NETWORKADMIN, fmt.Sprintf("Expected org id for node %v, got %v", NETWORKADMIN, nodeInfo.OrgId)) @@ -107,7 +107,7 @@ func TestNodeCache_UpsertNode(t *testing.T) { // check node details update by updating node status NodeInfoMap.UpsertNode(ORGADMIN, NODE2, NodeDeactivated) nodeInfo, err = NodeInfoMap.GetNodeByUrl(NODE2) - assert.True(err == nil,fmt.Sprintf("got errors in node fetch")) + assert.True(err == ErrNoError, fmt.Sprintf("got errors in node fetch")) assert.True(nodeInfo.Status == NodeDeactivated, fmt.Sprintf("Expected node status %v, got %v", NodeDeactivated, nodeInfo.Status)) } @@ -120,7 +120,7 @@ func TestRoleCache_UpsertRole(t *testing.T) { // add a role into the cache and validate RoleInfoMap.UpsertRole(NETWORKADMIN, NETWORKADMIN, true, true, FullAccess, true) roleInfo, err := RoleInfoMap.GetRole(NETWORKADMIN, NETWORKADMIN) - assert.True(err == nil, "errors encountered") + assert.True(err == ErrNoError, "errors encountered") assert.False(roleInfo == nil, fmt.Sprintf("Expected role details, got nil")) assert.True(roleInfo.OrgId == NETWORKADMIN, fmt.Sprintf("Expected org id for node %v, got %v", NETWORKADMIN, roleInfo.OrgId)) assert.True(roleInfo.RoleId == NETWORKADMIN, fmt.Sprintf("Expected node id %v, got %v", NETWORKADMIN, roleInfo.RoleId)) @@ -133,7 +133,7 @@ func TestRoleCache_UpsertRole(t *testing.T) { // update role status and validate RoleInfoMap.UpsertRole(ORGADMIN, ORGADMIN, true, true, FullAccess, false) roleInfo, err = RoleInfoMap.GetRole(ORGADMIN, ORGADMIN) - assert.True(err == nil, "errors encountered") + assert.True(err == ErrNoError, "errors encountered") assert.True(roleInfo.Active == false, fmt.Sprintf("Expected role active status to be %v, got %v", true, roleInfo.Active)) } @@ -146,7 +146,7 @@ func TestAcctCache_UpsertAccount(t *testing.T) { // add an account into the cache and validate AcctInfoMap.UpsertAccount(NETWORKADMIN, NETWORKADMIN, Acct1, true, AcctActive) acctInfo, err := AcctInfoMap.GetAccount(Acct1) - assert.True(err == nil) + assert.True(err == ErrNoError) assert.False(acctInfo == nil, fmt.Sprintf("Expected account details, got nil")) assert.True(acctInfo.OrgId == NETWORKADMIN, fmt.Sprintf("Expected org id for the account to be %v, got %v", NETWORKADMIN, acctInfo.OrgId)) @@ -160,7 +160,7 @@ func TestAcctCache_UpsertAccount(t *testing.T) { // update account status and validate AcctInfoMap.UpsertAccount(ORGADMIN, ORGADMIN, Acct2, true, AcctBlacklisted) acctInfo, err = AcctInfoMap.GetAccount(Acct2) - assert.True(err == nil) + assert.True(err == ErrNoError) assert.True(acctInfo.Status == AcctBlacklisted, fmt.Sprintf("Expected account status to be %v, got %v", AcctBlacklisted, acctInfo.Status)) @@ -262,6 +262,6 @@ func TestLRUCacheLimit(t *testing.T) { } o, err := OrgInfoMap.GetOrg("ORG1") - testifyassert.True(t, err == nil) + testifyassert.True(t, err == ErrNoError) testifyassert.True(t, o != nil) } diff --git a/params/quorum.go b/params/quorum.go index 827404cb6..48a240517 100644 --- a/params/quorum.go +++ b/params/quorum.go @@ -9,3 +9,4 @@ const ( DEFAULT_NODECACHE_SIZE = 1000 DEFAULT_ACCOUNTCACHE_SIZE = 6000 ) + diff --git a/permission/api.go b/permission/api.go index 76fec4dd4..da43a0347 100644 --- a/permission/api.go +++ b/permission/api.go @@ -172,7 +172,7 @@ func (q *QuorumControlsAPI) AcctList() []types.AccountInfo { func (q *QuorumControlsAPI) GetOrgDetails(orgId string) (types.OrgDetailInfo, error) { o, err := types.OrgInfoMap.GetOrg(orgId) - if err != nil { + if err != types.ErrNoError { return types.OrgDetailInfo{}, err } @@ -198,7 +198,7 @@ func (q *QuorumControlsAPI) GetOrgDetails(orgId string) (types.OrgDetailInfo, er } } orgRec, err := types.OrgInfoMap.GetOrg(orgId) - if err != nil { + if err != types.ErrNoError { return types.OrgDetailInfo{}, err } @@ -557,7 +557,7 @@ func (q *QuorumControlsAPI) isNetworkAdmin(account common.Address) bool { func (q *QuorumControlsAPI) isOrgAdmin(account common.Address, orgId string) (ExecStatus, error) { org, err := types.OrgInfoMap.GetOrg(orgId) - if err != nil { + if err != types.ErrNoError { return ErrOrgDoesNotExists, err } if org == nil { @@ -628,7 +628,7 @@ func (q *QuorumControlsAPI) valNodeStatusChange(orgId, url string, op NodeUpdate } node, err := types.NodeInfoMap.GetNodeByUrl(url) - if err != nil { + if err != types.ErrNoError { return ErrInvalidNode, err } @@ -665,12 +665,12 @@ func (q *QuorumControlsAPI) valNodeStatusChange(orgId, url string, op NodeUpdate func (q *QuorumControlsAPI) validateRole(orgId, roleId string) bool { var r *types.RoleInfo r, err := types.RoleInfoMap.GetRole(orgId, roleId) - if err != nil { + if err != types.ErrNoError { return false } if r == nil { orgRec, err := types.OrgInfoMap.GetOrg(orgId) - if err != nil || orgRec == nil { + if err != types.ErrNoError || orgRec == nil { return false } r, err = types.RoleInfoMap.GetRole(orgRec.UltimateParent, roleId) @@ -685,7 +685,7 @@ func (q *QuorumControlsAPI) validateRole(orgId, roleId string) bool { func (q *QuorumControlsAPI) valAccountStatusChange(orgId string, account common.Address, permAction PermAction, op AccountUpdateAction) (ExecStatus, error) { // validates if the enode is linked the passed organization ac, err := types.AcctInfoMap.GetAccount(account) - if err != nil { + if err != types.ErrNoError { return ErrAccountNotThere, err } @@ -734,7 +734,7 @@ func (q *QuorumControlsAPI) checkOrgAdminExists(orgId, roleId string, account co func (q *QuorumControlsAPI) valSubOrgBreadthDepth(porgId string) (ExecStatus, error) { org, err := types.OrgInfoMap.GetOrg(porgId) - if err != nil || org == nil { + if err != types.ErrNoError || org == nil { return ErrOpNotAllowed, err } diff --git a/permission/permission.go b/permission/permission.go index 29f815241..ba03320ab 100644 --- a/permission/permission.go +++ b/permission/permission.go @@ -571,7 +571,7 @@ func (p *PermissionCtrl) manageAccountPermissions() error { types.AcctInfoMap.UpsertAccount(evtAccessRevoked.OrgId, evtAccessRevoked.RoleId, evtAccessRevoked.Account, evtAccessRevoked.OrgAdmin, types.AcctActive) case evtStatusChanged := <-chStatusChanged: - if ac, err := types.AcctInfoMap.GetAccount(evtStatusChanged.Account); ac != nil { + if ac, err := types.AcctInfoMap.GetAccount(evtStatusChanged.Account); err != types.ErrNoError && ac != nil { types.AcctInfoMap.UpsertAccount(evtStatusChanged.OrgId, ac.RoleId, evtStatusChanged.Account, ac.IsOrgAdmin, types.AcctStatus(int(evtStatusChanged.Status.Uint64()))) } else { log.Info("error fetching account information", "err", err) @@ -889,9 +889,9 @@ func (p *PermissionCtrl) populateAccountToCache(acctId common.Address) (*types.A } if status.Int64() == 0 { - return nil, nil + return nil, types.ErrNoError } - return &types.AccountInfo{AcctId: account, OrgId: orgId, RoleId: roleId, Status: types.AcctStatus(status.Int64()), IsOrgAdmin: isAdmin}, nil + return &types.AccountInfo{AcctId: account, OrgId: orgId, RoleId: roleId, Status: types.AcctStatus(status.Int64()), IsOrgAdmin: isAdmin}, types.ErrNoError } // getter to get a org record from the contract @@ -907,7 +907,7 @@ func (p *PermissionCtrl) populateOrgToCache(orgId string) (*types.OrgInfo, error return nil, err } if orgStatus.Int64() == 0 { - return nil, nil + return nil, types.ErrNoError } orgInfo := types.OrgInfo{OrgId: org, ParentOrgId: parentOrgId, UltimateParent: ultimateParentId, Status: types.OrgStatus(orgStatus.Int64()), Level: orgLevel} // now need to build the list of sub orgs for this org @@ -917,7 +917,7 @@ func (p *PermissionCtrl) populateOrgToCache(orgId string) (*types.OrgInfo, error } if len(subOrgIndexes) == 0 { - return &orgInfo, nil + return &orgInfo, types.ErrNoError } // range through the sub org indexes and get the org ids to populate the suborg list @@ -930,7 +930,7 @@ func (p *PermissionCtrl) populateOrgToCache(orgId string) (*types.OrgInfo, error orgInfo.SubOrgList = append(orgInfo.SubOrgList, orgId+"."+subOrgId) } - return &orgInfo, nil + return &orgInfo, types.ErrNoError } // getter to get a role record from the contract @@ -948,9 +948,9 @@ func (p *PermissionCtrl) populateRoleToCache(roleKey *types.RoleKey) (*types.Rol } if roleDetails.OrgId == "" { - return nil, nil + return nil, types.ErrNoError } - return &types.RoleInfo{OrgId: roleDetails.OrgId, RoleId: roleDetails.RoleId, IsVoter: roleDetails.Voter, IsAdmin: roleDetails.Admin, Access: types.AccessType(roleDetails.AccessType.Int64()), Active: roleDetails.Active}, nil + return &types.RoleInfo{OrgId: roleDetails.OrgId, RoleId: roleDetails.RoleId, IsVoter: roleDetails.Voter, IsAdmin: roleDetails.Admin, Access: types.AccessType(roleDetails.AccessType.Int64()), Active: roleDetails.Active}, types.ErrNoError } // getter to get a role record from the contract @@ -967,9 +967,9 @@ func (p *PermissionCtrl) populateNodeCache(url string) (*types.NodeInfo, error) } if nodeDetails.NodeStatus.Int64() == 0 { - return nil, nil + return nil, types.ErrNoError } - return &types.NodeInfo{OrgId: nodeDetails.OrgId, Url: nodeDetails.EnodeId, Status: types.NodeStatus(nodeDetails.NodeStatus.Int64())}, nil + return &types.NodeInfo{OrgId: nodeDetails.OrgId, Url: nodeDetails.EnodeId, Status: types.NodeStatus(nodeDetails.NodeStatus.Int64())}, types.ErrNoError } // getter to get a node record from the contract @@ -986,7 +986,7 @@ func (p *PermissionCtrl) populateNodeCacheAndValidate(hexNodeId, ultimateParentI numNodes := numberOfNodes.Uint64() for k := uint64(0); k < numNodes; k++ { if nodeStruct, err := permNodeInterface.GetNodeDetailsFromIndex(big.NewInt(int64(k))); err == nil { - if orgRec, err := types.OrgInfoMap.GetOrg(nodeStruct.OrgId); err == nil && orgRec != nil { + if orgRec, err := types.OrgInfoMap.GetOrg(nodeStruct.OrgId); err == types.ErrNoError && orgRec != nil { if orgRec.UltimateParent == ultimateParentId { recEnode, _ := enode.ParseV4(nodeStruct.EnodeId) if recEnode.ID() == passedEnode.ID() { diff --git a/permission/permission_test.go b/permission/permission_test.go index a50317928..d6cd2997e 100644 --- a/permission/permission_test.go +++ b/permission/permission_test.go @@ -397,7 +397,7 @@ func TestQuorumControlsAPI_NodeAPIs(t *testing.T) { assert.Equal(t, nodeCacheSize, len(types.NodeInfoMap.GetNodeList())) nodeInfo, err := types.NodeInfoMap.GetNodeByUrl(arbitraryNode4) - assert.True(t, err == nil, "node fetch returned error") + assert.True(t, err == types.ErrNoError, "node fetch returned error") assert.Equal(t, types.NodeApproved, nodeInfo.Status) } @@ -496,7 +496,7 @@ func TestQuorumControlsAPI_RoleAndAccountsAPIs(t *testing.T) { assert.Equal(t, roleCacheSize, len(types.RoleInfoMap.GetRoleList())) roleInfo, err := types.RoleInfoMap.GetRole(arbitraryNetworkAdminOrg, arbitrartNewRole1) - assert.True(t, err == nil, "error encountered" ) + assert.True(t, err == types.ErrNoError, "error encountered") assert.Equal(t, roleInfo.RoleId, arbitrartNewRole1) @@ -515,7 +515,7 @@ func TestQuorumControlsAPI_RoleAndAccountsAPIs(t *testing.T) { assert.Equal(t, accountCacheSize, len(types.AcctInfoMap.GetAcctList())) acctInfo, err := types.AcctInfoMap.GetAccount(acct) - assert.True(t, err == nil, "error encountered") + assert.True(t, err == types.ErrNoError, "error encountered") assert.True(t, acctInfo != nil, "account details nil") }