quorum/permission/contract/NodeManager.sol

183 lines
6.8 KiB
Solidity
Raw Normal View History

pragma solidity ^0.5.3;
2019-05-08 20:07:54 -07:00
2019-03-15 02:26:57 -07:00
import "./PermissionsUpgradable.sol";
contract NodeManager {
PermissionsUpgradable private permUpgradable;
// enum and struct declaration
// changing node status to integer (0-NotInList, 1- PendingApproval, 2-Approved, 3-Deactivated, 4-Blacklisted)
// PendingDeactivation, Deactivated, PendingActivation, PendingBlacklisting, Blacklisted)
2019-05-08 20:07:54 -07:00
// enum NodeStatus {NotInList, PendingApproval, Approved, PendingDeactivation, Deactivated, PendingActivation, PendingBlacklisting, Blacklisted}
struct NodeDetails {
string enodeId; //e.g. 127.0.0.1:20005
string orgId;
uint status;
}
// use an array to store node details
// if we want to list all node one day, mapping is not capable
NodeDetails[] private nodeList;
// use a mapping of enodeid to array index to track node
mapping(bytes32 => uint) private nodeIdToIndex;
// keep track of node number
uint private numberOfNodes;
// node permission events for new node propose
event NodeProposed(string _enodeId, string _orgId);
event NodeApproved(string _enodeId, string _orgId);
2019-03-28 21:28:11 -07:00
// node permission events for node deactivation
event NodeDeactivated(string _enodeId, string _orgId);
// node permission events for node activation
event NodeActivated(string _enodeId, string _orgId);
// node permission events for node blacklist
event NodeBlacklisted(string _enodeId, string _orgId);
2019-05-10 00:17:01 -07:00
// checks if the caller is implementation contracts
2019-03-15 02:26:57 -07:00
modifier onlyImpl
{
require(msg.sender == permUpgradable.getPermImpl());
_;
}
// Checks if the given enode exists
modifier enodeInList(string memory _enodeId)
{
require(nodeIdToIndex[keccak256(abi.encodePacked(_enodeId))] != 0, "Enode is not in the list");
_;
}
// Checks if the given enode does not exists
modifier enodeNotInList(string memory _enodeId)
{
require(nodeIdToIndex[keccak256(abi.encodePacked(_enodeId))] == 0, "Enode is in the list");
_;
}
2019-05-10 00:17:01 -07:00
// constructor. sets the upgradable address
2019-03-15 02:26:57 -07:00
constructor (address _permUpgradable) public {
permUpgradable = PermissionsUpgradable(_permUpgradable);
2019-03-15 02:26:57 -07:00
}
// Get node details given enode Id
function getNodeDetails(string memory enodeId) public view returns (string memory _orgId, string memory _enodeId, uint _nodeStatus)
{
uint nodeIndex = getNodeIndex(enodeId);
return (nodeList[nodeIndex].orgId, nodeList[nodeIndex].enodeId, nodeList[nodeIndex].status);
}
2019-05-10 00:17:01 -07:00
// Get node details given index
function getNodeDetailsFromIndex(uint nodeIndex) public view returns (string memory _orgId, string memory _enodeId, uint _nodeStatus)
{
return (nodeList[nodeIndex].orgId, nodeList[nodeIndex].enodeId, nodeList[nodeIndex].status);
}
2019-05-10 00:17:01 -07:00
// Get number of nodes
function getNumberOfNodes() public view returns (uint)
{
return numberOfNodes;
}
// Get node status by enode id
function getNodeStatus(string memory _enodeId) public view returns (uint)
{
2019-05-08 20:07:54 -07:00
if (nodeIdToIndex[keccak256(abi.encodePacked(_enodeId))] == 0) {
return 0;
}
return nodeList[getNodeIndex(_enodeId)].status;
}
2019-05-10 00:17:01 -07:00
// called at the time of initialization for adding admin nodes
function addAdminNode(string calldata _enodeId, string calldata _orgId) external
onlyImpl
enodeNotInList(_enodeId)
{
numberOfNodes++;
nodeIdToIndex[keccak256(abi.encodePacked(_enodeId))] = numberOfNodes;
nodeList.push(NodeDetails(_enodeId, _orgId, 2));
}
2019-05-10 00:17:01 -07:00
// called at the time of new org creation. will need approval for the node to be
// part of the network
function addNode(string memory _enodeId, string memory _orgId) public
2019-03-15 02:26:57 -07:00
onlyImpl
enodeNotInList(_enodeId)
{
numberOfNodes++;
nodeIdToIndex[keccak256(abi.encodePacked(_enodeId))] = numberOfNodes;
2019-05-08 20:07:54 -07:00
nodeList.push(NodeDetails(_enodeId, _orgId, 1));
emit NodeProposed(_enodeId, _orgId);
}
2019-05-10 00:17:01 -07:00
// can be called by org admins to add new nodes to the org or sub orgs
2019-03-15 02:26:57 -07:00
function addOrgNode(string calldata _enodeId, string calldata _orgId) external
onlyImpl
enodeNotInList(_enodeId)
{
numberOfNodes++;
nodeIdToIndex[keccak256(abi.encodePacked(_enodeId))] = numberOfNodes;
2019-05-08 20:07:54 -07:00
nodeList.push(NodeDetails(_enodeId, _orgId, 2));
emit NodeApproved(_enodeId, _orgId);
}
2019-05-10 00:17:01 -07:00
// updates the node status to approved and emits the event
function approveNode(string memory _enodeId, string memory _orgId) public
2019-03-15 02:26:57 -07:00
onlyImpl
enodeInList(_enodeId)
{
// node should belong to the passed org
require(checkOrg(_enodeId, _orgId), "Node does not belong to the org");
require(getNodeStatus(_enodeId) == 1, "Node need to be in PendingApproval status");
uint nodeIndex = getNodeIndex(_enodeId);
// vote node
nodeList[nodeIndex].status = 2;
emit NodeApproved(nodeList[nodeIndex].enodeId, nodeList[nodeIndex].orgId);
}
2019-05-10 00:17:01 -07:00
// updates the node status. Used for deactivating or blacklisting a node and reactivating
// a deactivated node
function updateNodeStatus(string calldata _enodeId, string calldata _orgId, uint _status) external
onlyImpl
enodeInList(_enodeId)
{
// node should belong to the org
require(checkOrg(_enodeId, _orgId), "Node does not belong to the org");
// changing node status to integer (0-NotInList, 1- PendingApproval, 2-Approved, 3-Deactivated, 4-Blacklisted)
// operations that can be done 3-Deactivate Node, 4-ActivateNode, 5-Blacklist nodeList
require((_status == 3 || _status == 4 || _status == 5), "invalid operation");
2019-05-08 20:07:54 -07:00
if (_status == 3) {
require(getNodeStatus(_enodeId) == 2, "Op cannot be performed");
nodeList[getNodeIndex(_enodeId)].status = 3;
emit NodeDeactivated(_enodeId, _orgId);
}
2019-05-08 20:07:54 -07:00
else if (_status == 4) {
require(getNodeStatus(_enodeId) == 3, "Op cannot be performed");
nodeList[getNodeIndex(_enodeId)].status = 2;
emit NodeActivated(_enodeId, _orgId);
}
else {
nodeList[getNodeIndex(_enodeId)].status = 5;
emit NodeBlacklisted(_enodeId, _orgId);
}
}
/* private functions */
2019-05-10 00:17:01 -07:00
// returs the node index for given node id
function getNodeIndex(string memory _enodeId) internal view
returns (uint)
{
return nodeIdToIndex[keccak256(abi.encodePacked(_enodeId))] - 1;
}
2019-05-10 00:17:01 -07:00
// checks if the node is linked to the passed org
function checkOrg(string memory _enodeId, string memory _orgId) internal view
2019-05-08 20:07:54 -07:00
returns (bool)
{
return (keccak256(abi.encodePacked(nodeList[getNodeIndex(_enodeId)].orgId)) == keccak256(abi.encodePacked(_orgId)));
}
}