2018-07-11 00:25:45 -07:00
|
|
|
pragma solidity ^0.4.23;
|
|
|
|
|
|
|
|
contract Permissions {
|
|
|
|
|
2018-09-05 19:40:21 -07:00
|
|
|
// enum and struct declaration
|
|
|
|
enum NodeStatus { NotInList, PendingApproval, Approved, PendingDeactivation, Deactivated, PendingBlacklisting, Blacklisted }
|
|
|
|
enum AccountAccess { FullAccess, ReadOnly, Transact, ContractDeploy }
|
|
|
|
struct NodeDetails {
|
|
|
|
string enodeId; //e.g. 127.0.0.1:20005
|
2018-08-12 19:00:35 -07:00
|
|
|
string ipAddrPort;
|
|
|
|
string discPort;
|
|
|
|
string raftPort;
|
2018-07-11 00:25:45 -07:00
|
|
|
NodeStatus status;
|
|
|
|
}
|
|
|
|
|
2018-09-05 19:40:21 -07:00
|
|
|
// 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;
|
2018-08-05 22:26:29 -07:00
|
|
|
|
2018-09-05 19:40:21 -07:00
|
|
|
// use an array to store account details
|
|
|
|
// if we want to list all account one day, mapping is not capable
|
|
|
|
address[] private accountList;
|
|
|
|
|
|
|
|
// store node approval, deactivation and blacklisting vote status (prevent double vote)
|
|
|
|
mapping (uint => mapping (address => bool)) private voteStatus;
|
|
|
|
// valid vote count
|
|
|
|
mapping (uint => uint) private voteCount;
|
|
|
|
|
2018-10-24 03:21:21 -07:00
|
|
|
// checks if firts time network boot up has happened or not
|
2018-10-25 22:38:56 -07:00
|
|
|
bool private networkBoot = false;
|
2018-10-24 03:21:21 -07:00
|
|
|
|
2018-09-19 20:18:39 -07:00
|
|
|
// node permission events for new node propose
|
|
|
|
event NodeProposed(string _enodeId);
|
2018-08-12 19:00:35 -07:00
|
|
|
event NodeApproved(string _enodeId, string _ipAddrPort, string _discPort, string _raftPort);
|
2018-09-19 20:18:39 -07:00
|
|
|
event VoteNodeApproval(string _enodeId, address _accountAddress);
|
|
|
|
|
|
|
|
// node permission events for node decativation
|
2018-07-11 00:25:45 -07:00
|
|
|
event NodePendingDeactivation (string _enodeId);
|
2018-08-12 19:00:35 -07:00
|
|
|
event NodeDeactivated(string _enodeId, string _ipAddrPort, string _discPort, string _raftPort);
|
2018-09-19 20:18:39 -07:00
|
|
|
event VoteNodeDeactivation(string _enodeId, address _accountAddress);
|
|
|
|
|
|
|
|
// node permission events for node blacklist
|
|
|
|
event NodePendingBlacklist(string _enodeId);
|
2018-08-31 04:35:35 -07:00
|
|
|
event NodeBlacklisted(string _enodeId, string _ipAddrPort, string _discPort, string _raftPort);
|
2018-09-19 20:18:39 -07:00
|
|
|
event VoteNodeBlacklist(string _enodeId, address _accountAddress);
|
|
|
|
|
2018-09-05 19:40:21 -07:00
|
|
|
// account permission events
|
|
|
|
event AccountAccessModified(address _address, AccountAccess _access);
|
2018-09-19 20:18:39 -07:00
|
|
|
|
|
|
|
// events related to voting accounts for majority voting
|
2018-09-05 19:40:21 -07:00
|
|
|
event NoVotingAccount();
|
|
|
|
event VoterAdded(address _address);
|
|
|
|
event VoterRemoved(address _address);
|
2018-07-11 00:25:45 -07:00
|
|
|
|
2018-09-19 20:18:39 -07:00
|
|
|
// Checks if the given enode exists
|
2018-09-05 19:40:21 -07:00
|
|
|
modifier enodeInList(string _enodeId)
|
|
|
|
{
|
|
|
|
require(nodeIdToIndex[keccak256(abi.encodePacked(_enodeId))] != 0, "Enode is not in the list");
|
|
|
|
_;
|
|
|
|
}
|
2018-09-19 20:18:39 -07:00
|
|
|
|
|
|
|
// Checks if the given enode does not exists
|
2018-09-05 19:40:21 -07:00
|
|
|
modifier enodeNotInList(string _enodeId)
|
|
|
|
{
|
|
|
|
require(nodeIdToIndex[keccak256(abi.encodePacked(_enodeId))] == 0, "Enode is in the list");
|
|
|
|
_;
|
|
|
|
}
|
2018-09-19 20:18:39 -07:00
|
|
|
|
|
|
|
// Checks if the account can vote
|
2018-09-05 19:40:21 -07:00
|
|
|
modifier canVote()
|
|
|
|
{
|
|
|
|
bool flag = false;
|
|
|
|
for (uint i=0; i<accountList.length; i++){
|
|
|
|
if (accountList[i] == msg.sender){
|
|
|
|
flag = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
require(flag, "Account can not vote");
|
|
|
|
_;
|
2018-08-31 04:35:35 -07:00
|
|
|
}
|
|
|
|
|
2018-09-05 19:40:21 -07:00
|
|
|
/* public and external functions */
|
|
|
|
// view functions
|
|
|
|
|
2018-10-25 22:38:56 -07:00
|
|
|
// Get number of nodes
|
|
|
|
function getNetworkBootStatus() public view returns (bool)
|
|
|
|
{
|
|
|
|
return networkBoot;
|
|
|
|
}
|
|
|
|
|
2018-09-05 19:40:21 -07:00
|
|
|
// Get number of nodes
|
2018-09-17 22:52:46 -07:00
|
|
|
function getNumberOfNodes() public view returns (uint)
|
2018-09-05 19:40:21 -07:00
|
|
|
{
|
|
|
|
return numberOfNodes;
|
|
|
|
}
|
|
|
|
// Get number of accounts and voting accounts
|
2018-09-17 22:52:46 -07:00
|
|
|
function getNumberOfAccounts() public view returns (uint)
|
2018-09-05 19:40:21 -07:00
|
|
|
{
|
|
|
|
return accountList.length;
|
|
|
|
}
|
|
|
|
// Get node status by enode id
|
2018-09-17 22:52:46 -07:00
|
|
|
function getNodeStatus(string _enodeId) public view enodeInList(_enodeId) returns (NodeStatus)
|
2018-09-05 19:40:21 -07:00
|
|
|
{
|
|
|
|
return nodeList[getNodeIndex(_enodeId)].status;
|
|
|
|
}
|
|
|
|
// Get vote count by enode id
|
2018-09-17 22:52:46 -07:00
|
|
|
function getVoteCount(string _enodeId) public view enodeInList(_enodeId) returns (uint)
|
2018-09-05 19:40:21 -07:00
|
|
|
{
|
|
|
|
return voteCount[getNodeIndex(_enodeId)];
|
|
|
|
}
|
|
|
|
// Get vote status by enode id and voter address
|
2018-09-17 22:52:46 -07:00
|
|
|
function getVoteStatus(string _enodeId, address _voter) public view enodeInList(_enodeId) returns (bool)
|
2018-09-05 19:40:21 -07:00
|
|
|
{
|
|
|
|
return voteStatus[getNodeIndex(_enodeId)][_voter];
|
|
|
|
}
|
|
|
|
// for potential external use
|
|
|
|
// Get enode id by index
|
2018-09-17 22:52:46 -07:00
|
|
|
function getEnodeId(uint _index) external view returns (string)
|
2018-09-05 19:40:21 -07:00
|
|
|
{
|
|
|
|
if (_index <= numberOfNodes){
|
|
|
|
return nodeList[_index].enodeId;
|
|
|
|
} else {
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Get account address by index
|
2018-09-17 22:52:46 -07:00
|
|
|
function getAccountAddress(uint _index) external view returns (address)
|
2018-09-05 19:40:21 -07:00
|
|
|
{
|
|
|
|
if (_index <= accountList.length){
|
|
|
|
return accountList[_index];
|
|
|
|
} else {
|
|
|
|
return address(0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// state change functions
|
2018-10-24 03:21:21 -07:00
|
|
|
// update the networ boot status as true
|
2018-10-25 22:38:56 -07:00
|
|
|
function updateNetworkBootStatus() external returns (bool)
|
|
|
|
{
|
2018-10-24 03:21:21 -07:00
|
|
|
require (networkBoot == false, "Invalid call: Network boot up completed");
|
|
|
|
networkBoot = true;
|
2018-10-25 22:38:56 -07:00
|
|
|
return networkBoot;
|
2018-10-24 03:21:21 -07:00
|
|
|
}
|
2018-09-05 19:40:21 -07:00
|
|
|
|
2018-08-31 04:35:35 -07:00
|
|
|
// propose a new node to the network
|
2018-09-19 20:18:39 -07:00
|
|
|
function proposeNode(string _enodeId, string _ipAddrPort, string _discPort, string _raftPort) external enodeNotInList(_enodeId)
|
2018-09-05 19:40:21 -07:00
|
|
|
{
|
2018-10-24 03:21:21 -07:00
|
|
|
if (!(networkBoot)){
|
2018-09-05 19:40:21 -07:00
|
|
|
numberOfNodes++;
|
|
|
|
nodeIdToIndex[keccak256(abi.encodePacked(_enodeId))] = numberOfNodes;
|
2018-10-24 03:21:21 -07:00
|
|
|
nodeList.push(NodeDetails(_enodeId, _ipAddrPort,_discPort, _raftPort, NodeStatus.Approved));
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
if (checkVotingAccountExist()){
|
|
|
|
// increment node number, add node to the list
|
|
|
|
numberOfNodes++;
|
|
|
|
nodeIdToIndex[keccak256(abi.encodePacked(_enodeId))] = numberOfNodes;
|
|
|
|
nodeList.push(NodeDetails(_enodeId, _ipAddrPort,_discPort, _raftPort, NodeStatus.PendingApproval));
|
|
|
|
// add voting status, numberOfNodes is the index of current proposed node
|
|
|
|
for (uint i = 0; i < accountList.length; i++){
|
|
|
|
voteStatus[numberOfNodes][accountList[i]] = false;
|
|
|
|
}
|
|
|
|
voteCount[numberOfNodes] = 0;
|
|
|
|
// emit event
|
|
|
|
emit NodeProposed(_enodeId);
|
2018-09-05 19:40:21 -07:00
|
|
|
}
|
|
|
|
}
|
2018-07-11 00:25:45 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Adds a node to the nodeList mapping and emits node added event if successfully and node exists event of node is already present
|
2018-09-17 22:52:46 -07:00
|
|
|
function approveNode(string _enodeId) external canVote
|
2018-09-05 19:40:21 -07:00
|
|
|
{
|
|
|
|
require(getNodeStatus(_enodeId) == NodeStatus.PendingApproval, "Node need to be in PendingApproval status");
|
|
|
|
uint nodeIndex = getNodeIndex(_enodeId);
|
|
|
|
require(voteStatus[nodeIndex][msg.sender] == false, "Node can not double vote");
|
|
|
|
// vote node
|
|
|
|
voteStatus[nodeIndex][msg.sender] = true;
|
|
|
|
voteCount[nodeIndex]++;
|
|
|
|
// emit event
|
|
|
|
emit VoteNodeApproval(_enodeId, msg.sender);
|
|
|
|
// check if node vote reach majority
|
|
|
|
checkNodeApproval(_enodeId);
|
2018-07-11 00:25:45 -07:00
|
|
|
}
|
|
|
|
|
2018-08-31 04:35:35 -07:00
|
|
|
// Propose a node for deactivation from network
|
2018-09-19 20:18:39 -07:00
|
|
|
function proposeDeactivation(string _enodeId) external enodeInList(_enodeId)
|
2018-09-05 19:40:21 -07:00
|
|
|
{
|
|
|
|
if (checkVotingAccountExist()){
|
|
|
|
require(getNodeStatus(_enodeId) == NodeStatus.Approved, "Node need to be in Approved status");
|
|
|
|
uint nodeIndex = getNodeIndex(_enodeId);
|
|
|
|
nodeList[nodeIndex].status = NodeStatus.PendingDeactivation;
|
|
|
|
// add voting status, numberOfNodes is the index of current proposed node
|
|
|
|
for (uint i = 0; i < accountList.length; i++){
|
|
|
|
voteStatus[nodeIndex][accountList[i]] = false;
|
|
|
|
}
|
|
|
|
voteCount[nodeIndex] = 0;
|
|
|
|
// emit event
|
|
|
|
emit NodePendingDeactivation(_enodeId);
|
|
|
|
}
|
2018-07-11 00:25:45 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
//deactivates a given Enode and emits the decativation event
|
2018-09-19 20:18:39 -07:00
|
|
|
function deactivateNode(string _enodeId) external canVote
|
2018-09-05 19:40:21 -07:00
|
|
|
{
|
2018-07-11 00:25:45 -07:00
|
|
|
require(getNodeStatus(_enodeId) == NodeStatus.PendingDeactivation, "Node need to be in PendingDeactivation status");
|
2018-09-05 19:40:21 -07:00
|
|
|
uint nodeIndex = getNodeIndex(_enodeId);
|
|
|
|
require(voteStatus[nodeIndex][msg.sender] == false, "Node can not double vote");
|
|
|
|
// vote node
|
|
|
|
voteStatus[nodeIndex][msg.sender] = true;
|
|
|
|
voteCount[nodeIndex]++;
|
|
|
|
// emit event
|
|
|
|
emit VoteNodeDeactivation(_enodeId, msg.sender);
|
|
|
|
// check if node vote reach majority
|
|
|
|
checkNodeDeactivation(_enodeId);
|
2018-07-11 00:25:45 -07:00
|
|
|
}
|
|
|
|
|
2018-09-05 19:40:21 -07:00
|
|
|
// Propose node for blacklisting
|
2018-09-19 20:18:39 -07:00
|
|
|
function proposeNodeBlacklisting(string _enodeId, string _ipAddrPort, string _discPort, string _raftPort) external
|
2018-09-05 19:40:21 -07:00
|
|
|
{
|
|
|
|
if (checkVotingAccountExist()){
|
|
|
|
uint nodeIndex;
|
|
|
|
// check if node is in the nodeList
|
|
|
|
if (nodeIdToIndex[keccak256(abi.encodePacked(_enodeId))] != 0){
|
|
|
|
// no matter what status the node is in, vote will reset and node status change to PendingBlacklisting
|
|
|
|
nodeList[nodeIndex].status = NodeStatus.PendingBlacklisting;
|
|
|
|
nodeIndex = getNodeIndex(_enodeId);
|
|
|
|
} else {
|
|
|
|
// increment node number, add node to the list
|
|
|
|
numberOfNodes++;
|
|
|
|
nodeIdToIndex[keccak256(abi.encodePacked(_enodeId))] = numberOfNodes;
|
2018-09-19 20:18:39 -07:00
|
|
|
nodeList.push(NodeDetails(_enodeId, _ipAddrPort,_discPort, _raftPort, NodeStatus.PendingBlacklisting));
|
2018-09-05 19:40:21 -07:00
|
|
|
nodeIndex = numberOfNodes;
|
|
|
|
}
|
|
|
|
// add voting status, numberOfNodes is the index of current proposed node
|
|
|
|
for (uint i = 0; i < accountList.length; i++){
|
|
|
|
voteStatus[nodeIndex][accountList[i]] = false;
|
|
|
|
}
|
|
|
|
voteCount[nodeIndex] = 0;
|
|
|
|
// emit event
|
2018-09-19 20:18:39 -07:00
|
|
|
emit NodePendingBlacklist(_enodeId);
|
2018-08-31 04:35:35 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//Approve node blacklisting
|
2018-09-19 20:18:39 -07:00
|
|
|
function blacklistNode(string _enodeId) external canVote
|
2018-09-05 19:40:21 -07:00
|
|
|
{
|
2018-08-31 04:35:35 -07:00
|
|
|
require(getNodeStatus(_enodeId) == NodeStatus.PendingBlacklisting, "Node need to be in PendingBlacklisting status");
|
2018-09-05 19:40:21 -07:00
|
|
|
uint nodeIndex = getNodeIndex(_enodeId);
|
|
|
|
require(voteStatus[nodeIndex][msg.sender] == false, "Node can not double vote");
|
|
|
|
// vote node
|
|
|
|
voteStatus[nodeIndex][msg.sender] = true;
|
|
|
|
voteCount[nodeIndex]++;
|
|
|
|
// emit event
|
2018-09-19 20:18:39 -07:00
|
|
|
emit VoteNodeBlacklist(_enodeId, msg.sender);
|
2018-09-05 19:40:21 -07:00
|
|
|
// check if node vote reach majority
|
|
|
|
checkNodeBlacklisting(_enodeId);
|
2018-08-31 04:35:35 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Checks if the Node is already added. If yes then returns true
|
2018-09-17 22:52:46 -07:00
|
|
|
function updateAccountAccess(address _address, AccountAccess _accountAccess) external
|
2018-09-05 19:40:21 -07:00
|
|
|
{
|
|
|
|
emit AccountAccessModified(_address, _accountAccess);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add voting account
|
2018-09-17 22:52:46 -07:00
|
|
|
function addVoter(address _address) external
|
2018-09-05 19:40:21 -07:00
|
|
|
{
|
|
|
|
// Check if account already exists
|
|
|
|
for (uint i=0; i<accountList.length; i++){
|
|
|
|
if (accountList[i] == _address){
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
accountList.push(_address);
|
|
|
|
emit VoterAdded(_address);
|
|
|
|
}
|
|
|
|
// Remove voting account
|
2018-09-17 22:52:46 -07:00
|
|
|
function removeVoter(address _address) external
|
2018-09-05 19:40:21 -07:00
|
|
|
{
|
|
|
|
// Check if account already exists
|
|
|
|
for (uint i=0; i<accountList.length; i++){
|
|
|
|
if (accountList[i] == _address){
|
|
|
|
for (uint j=i+1; j<accountList.length; j++){
|
|
|
|
accountList[j-1] = accountList[j];
|
|
|
|
}
|
|
|
|
delete accountList[accountList.length];
|
|
|
|
emit VoterRemoved(_address);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* private functions */
|
|
|
|
|
2018-09-17 22:52:46 -07:00
|
|
|
function getNodeIndex(string _enodeId) internal view returns (uint)
|
2018-09-05 19:40:21 -07:00
|
|
|
{
|
|
|
|
return nodeIdToIndex[keccak256(abi.encodePacked(_enodeId))] - 1;
|
|
|
|
}
|
|
|
|
|
2018-09-17 22:52:46 -07:00
|
|
|
function checkVotingAccountExist() internal returns (bool)
|
2018-09-05 19:40:21 -07:00
|
|
|
{
|
|
|
|
if (accountList.length == 0){
|
|
|
|
emit NoVotingAccount();
|
|
|
|
return false;
|
|
|
|
} else {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-17 22:52:46 -07:00
|
|
|
function checkNodeApproval(string _enodeId) internal
|
2018-09-05 19:40:21 -07:00
|
|
|
{
|
|
|
|
uint nodeIndex = getNodeIndex(_enodeId);
|
|
|
|
if (voteCount[nodeIndex] > accountList.length / 2){
|
|
|
|
nodeList[nodeIndex].status = NodeStatus.Approved;
|
|
|
|
emit NodeApproved(nodeList[nodeIndex].enodeId, nodeList[nodeIndex].ipAddrPort, nodeList[nodeIndex].discPort, nodeList[nodeIndex].raftPort);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-17 22:52:46 -07:00
|
|
|
function checkNodeDeactivation(string _enodeId) internal
|
2018-09-05 19:40:21 -07:00
|
|
|
{
|
|
|
|
uint nodeIndex = getNodeIndex(_enodeId);
|
|
|
|
if (voteCount[nodeIndex] > accountList.length / 2){
|
|
|
|
nodeList[nodeIndex].status = NodeStatus.Deactivated;
|
|
|
|
emit NodeDeactivated(nodeList[nodeIndex].enodeId, nodeList[nodeIndex].ipAddrPort, nodeList[nodeIndex].discPort, nodeList[nodeIndex].raftPort);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-17 22:52:46 -07:00
|
|
|
function checkNodeBlacklisting(string _enodeId) internal
|
2018-09-05 19:40:21 -07:00
|
|
|
{
|
|
|
|
uint nodeIndex = getNodeIndex(_enodeId);
|
|
|
|
if (voteCount[nodeIndex] > accountList.length / 2){
|
|
|
|
nodeList[nodeIndex].status = NodeStatus.Blacklisted;
|
|
|
|
emit NodeBlacklisted(nodeList[nodeIndex].enodeId, nodeList[nodeIndex].ipAddrPort, nodeList[nodeIndex].discPort, nodeList[nodeIndex].raftPort);
|
|
|
|
}
|
2018-08-31 04:35:35 -07:00
|
|
|
}
|
|
|
|
|
2018-07-11 00:25:45 -07:00
|
|
|
}
|