From 797d0bebb88843171c823f913da82a3625ebac49 Mon Sep 17 00:00:00 2001 From: Luke Miller Date: Thu, 6 May 2021 14:13:02 -0400 Subject: [PATCH] significant progress! a working process from beginning to end can be seen in test/org_req_test.lua --- modpol/modpol.lua | 3 ++- modpol/modules/consent.lua | 29 ++++++++++++++++++++++------- modpol/orgs/base.lua | 15 ++++++++++++++- modpol/orgs/requests.lua | 22 ++++++++++++++++++++++ modpol/tests/org_req_test.lua | 9 ++++++--- 5 files changed, 66 insertions(+), 12 deletions(-) diff --git a/modpol/modpol.lua b/modpol/modpol.lua index 21ce509..0edf03c 100644 --- a/modpol/modpol.lua +++ b/modpol/modpol.lua @@ -78,8 +78,9 @@ if (modpol.orgs.array) then if type(org) == 'table' then setmetatable(org, modpol.orgs) -- sets process metatable on load + print(org.modules) for id, process in ipairs(org.processes) do - setmetatable(process, org.modules[process.type]) + setmetatable(process, modpol.modules[process.type]) end end end diff --git a/modpol/modules/consent.lua b/modpol/modules/consent.lua index 1704050..001ae2f 100644 --- a/modpol/modules/consent.lua +++ b/modpol/modules/consent.lua @@ -12,6 +12,7 @@ function temp_consent_process() org_id = nil, request_id = nil, total_votes = 0, + majority_to_pass = 0.51, votes_yes = {}, votes_no = {} } @@ -33,6 +34,11 @@ end -- ====================================================== -- function for users to vote on a pending request function modpol.modules.consent:approve(user, decision) + if not modpol.orgs.get_org(self.org_id):has_member(user) then + modpol.ocutil.log('Error in consent:approve -> user not a member of the org') + return + end + if decision then table.insert(self.votes_yes, user) modpol.ocutil.log('User ' .. user .. ' voted yes on request #' .. self.request_id) @@ -43,15 +49,24 @@ function modpol.modules.consent:approve(user, decision) self.total_votes = self.total_votes + 1 + self:update_status() end -- =================================================== -- determines whether process has finished and resolves request if it has (unfinished) --- function modpol.modules.consent.call_vote_check(process) --- if votes_yes > to_pass then --- call_success() --- elseif votes_no > to_pass then --- call_failure() --- end --- end +function modpol.modules.consent:update_status() + local process_org = modpol.orgs.get_org(self.org_id) + local eligible_voters = process_org:get_member_count() + local votes_needed = math.ceil(self.majority_to_pass * eligible_voters) + + if #self.votes_yes >= votes_needed then + modpol.ocutil.log('Request #' .. self.request_id .. ' passes') + process_org:resolve_request(self.request_id, true) + elseif #self.votes_no >= votes_needed then + modpol.ocutil.log('Request #' .. self.request_id .. ' fails to pass') + process_org:resolve_request(self.request_id, false) + else + modpol.ocutil.log('Waiting for more votes...') + end +end diff --git a/modpol/orgs/base.lua b/modpol/orgs/base.lua index 37315ad..28cf136 100644 --- a/modpol/orgs/base.lua +++ b/modpol/orgs/base.lua @@ -271,7 +271,8 @@ end function modpol.orgs:list_member() local str for k, v in ipairs(self.members) do - if str then + -- checking to see if member name is valid + if str and str ~= '' then str = str .. '\n' .. v else str = v @@ -280,6 +281,18 @@ function modpol.orgs:list_member() return str end +-- ============================== +-- because member list uses lazy deletion, using #org.members will not show an accurate number +function modpol.orgs:get_member_count() + local count = 0 + for k, v in ipairs(self.members) do + -- the empty string represents a deleted member in the members list + if v ~= '' then + count = count + 1 + end + end + return count +end -- ==================================== -- adds a new policy to the policy table -- must define the policy type, process associated with it, and whether the request must be made by an org member diff --git a/modpol/orgs/requests.lua b/modpol/orgs/requests.lua index 1ff255a..f79820d 100644 --- a/modpol/orgs/requests.lua +++ b/modpol/orgs/requests.lua @@ -67,6 +67,28 @@ function modpol.orgs:list_request() return str end +-- =============================== +-- if the request was approved, the associated function is called, otherwise it is deleted +function modpol.orgs:resolve_request(request_id, approve) + if approve then + local request = self.requests[request_id] + local p = request.params + + if request.type == "add_org" then + self:add_org(p[1]) + elseif request.type == "delete" then + self:delete() + elseif request.type == "add_member" then + self:add_member(p[1]) + elseif request.type == "remove_member" then + self:remove_member(p[1]) + end + + end + + self.requests[request_id] = "deleted" +end + -- ================================ -- tries to make a request to the org function modpol.orgs:make_request(request) diff --git a/modpol/tests/org_req_test.lua b/modpol/tests/org_req_test.lua index 6696db6..08d1812 100644 --- a/modpol/tests/org_req_test.lua +++ b/modpol/tests/org_req_test.lua @@ -3,17 +3,20 @@ dofile('../modpol.lua'); modpol.orgs.reset() test_org = modpol.instance:add_org('test_org') +test_org:add_member('luke') +test_org:add_member('nathan') test_org:set_policy("add_member", nil, false); new_request = { - user = "lukvmil", + user = "josh", type = "add_member", - params = {"lukvmil"} + params = {"josh"} } request_id = test_org:make_request(new_request) process_id = test_org:create_process("consent", request_id) process = test_org.processes[process_id] -process:approve("luke", true) \ No newline at end of file +process:approve("luke", true) +process:approve("nathan", true) \ No newline at end of file