-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathupdate_ballotspecs_db.py
More file actions
110 lines (91 loc) · 2.95 KB
/
update_ballotspecs_db.py
File metadata and controls
110 lines (91 loc) · 2.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
import json
import logging
import os
from binascii import hexlify
import pymongo
from mode import *
import hashlib
import requests
# Data for each document
ID = "id"
SHORT_TITLE = "short_title"
QUESTION = "question"
DESCRIPTION = "description"
START_DATE = "start_date"
CHAMBER = "chamber"
SPONSOR = "sponsor"
# --- also create
BALLOTSPEC_HASH = "ballotspec_hash"
# Connection String
_client = None
def _get_client():
global _client
if _client is None:
_client = pymongo.MongoClient(mongosettings[URL])
return _client
log = logging.getLogger(__name__)
def hash_ballotspec(ballotspec_string):
h = hashlib.sha256()
h.update(str(ballotspec_string).encode('utf-8'))
return (h.hexdigest())
def push_to_chain(method, params):
log.info(f"Pushing to BC API: {json.dumps(params)}")
print(method)
print(params)
r = requests.post("https://api.blockchain.suzuka.flux.party/members/api", data=json.dumps({"method": method, "params": params}))
print(r.text)
log.info(f"push_to_chain Response: {r}\n\n-- Response content {r.content}\n\n-- As text: {r.text}")
return json.loads(r.text)["billCreationTxid"]
def render_spec_hash(_s):
if isinstance(_s, str) or type(_s) is str:
if _s[:2] != "0x" or len(_s) != 66:
return "0x" + ("00" * (32 - len(_s)) + hexlify(_s.encode()).decode())
return _s
def update_ballotspecs(id, short_title, question, description, start_date, chamber, sponsor):
db = _get_client()[mongosettings[MONGODB]]
ballotspecs_collection = db[mongosettings[BALLOTSPECSCOLLECTION]]
bills_collection = db[mongosettings[BILLSCOLLECTION]]
input_dict = {
ID: id,
SHORT_TITLE: short_title,
QUESTION: question,
DESCRIPTION: description,
START_DATE: start_date,
CHAMBER: chamber,
SPONSOR: sponsor,
}
# create ballotspec_hash
issue_string = json.dumps(input_dict)
ballotspec_dict = {
"ballotTitle": id,
"longDesc": issue_string,
"shortDesc": short_title,
"ballotVersion": 2,
"optionsVersion": 1,
}
ballot_spec_sz = json.dumps(ballotspec_dict)
bs_h = hash_ballotspec(ballot_spec_sz)
try:
# Post to API => posts the blockchain
TxID = push_to_chain("ballot_publish", {
"specHash": bs_h, #render_spec_hash(id),
"ballotSpec": ballot_spec_sz,
"realSpecHash": bs_h
})
print("Bill")
print(bs_h)
print(TxID)
except Exception as e:
import traceback
log.error(f"Error pushing to chain: {e}\n\n{traceback.format_tb(e.__traceback__)}\n\nCONTINUING")
try:
ballotspecs_collection.insert_one(
{'_id': input_dict["id"],
'data': input_dict,
BALLOTSPEC_HASH: bs_h,
# "tx_id" : TxID,
"specHash": bs_h,
"ballotSpec": ballot_spec_sz,
"realSpecHash": bs_h})
except Exception as e:
print(e)