-
Notifications
You must be signed in to change notification settings - Fork 3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Feat/logs and signature #57
base: main
Are you sure you want to change the base?
Changes from all commits
059f680
56e5b84
38bea38
77506a5
1d49252
202ff46
9157f6c
20402a5
63f1e55
b0ff59a
183747e
60236a5
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
TREE_NAME_PREFIX=eleicao_ | ||
BU_TREE_NAME_TEMPLATE=bus_eleicao_${ELECTION_ID} | ||
TREE_DEFAULT_COMMITMENT_SIZE=8 | ||
TL_MANAGER_URL=http://127.0.0.1:8000 | ||
MONGO_URL=mongodb://localhost:27017 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import logging | ||
import os | ||
|
||
from dotenv import load_dotenv | ||
|
||
load_dotenv() | ||
|
||
BU_TREE_NAME_TEMPLATE = os.getenv('BU_TREE_NAME_TEMPLATE') | ||
TREE_DEFAULT_COMMITMENT_SIZE = os.getenv('TREE_DEFAULT_COMMITMENT_SIZE') | ||
TL_MANAGER_URL = os.getenv('TL_MANAGER_URL') | ||
MONGO_URL = os.getenv('MONGO_URL') | ||
|
||
if '${ELECTION_ID}' not in BU_TREE_NAME_TEMPLATE: | ||
raise Exception("BU_TREE_NAME_TEMPLATE must contain ${ELECTION_ID}") | ||
|
||
|
||
logging.debug("BU_TREE_NAME_TEMPLATE: %s", BU_TREE_NAME_TEMPLATE) | ||
logging.debug("TL_MANAGER_URL: %s", TL_MANAGER_URL) | ||
logging.debug("MONGO_URL: %s", MONGO_URL) | ||
logging.debug("TREE_DEFAULT_COMMITMENT_SIZE: %s", TREE_DEFAULT_COMMITMENT_SIZE) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import logging | ||
|
||
from fastapi import APIRouter, UploadFile, File, Form | ||
|
||
from app.services import election_data_service | ||
|
||
router = APIRouter() | ||
|
||
|
||
@router.post("/create") | ||
async def create( | ||
tree_name: str = Form(...), | ||
file: UploadFile = File(...) | ||
): | ||
file_name = file.filename | ||
file_content = await file.read() | ||
|
||
logging.info(f"Received file {file_name} to insert in tree {tree_name}") | ||
|
||
try: | ||
election_data = election_data_service.insert(file_content, file_name, tree_name) | ||
except Exception as e: | ||
logging.error(e) | ||
return {"status": "error", "message": str(e)} | ||
|
||
return {"status": "ok", "bu": election_data.dict(exclude={"data"})} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
from pydantic import BaseModel | ||
|
||
from app.database.models.bu_model import MerkleTreeInfo | ||
|
||
|
||
class ElectionData(BaseModel): | ||
file_name: str | ||
data: bytes | ||
merkletree_info: MerkleTreeInfo |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
from pydantic import BaseModel | ||
|
||
|
||
class MerkleTreeInfo(BaseModel): | ||
tree_name: str | ||
index: int | ||
hash: str |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,5 +4,16 @@ | |
collection = get_db()["bus"] | ||
|
||
|
||
def find_one(election_id: int, zone: int, section: int): | ||
data = collection.find_one({"eleicoes": election_id, "zona": zone, "secao": section}) | ||
if data is None: | ||
return None | ||
return BuModel(**data) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Eu não entendi essa função. É para buscar um BU por eleição+zona+seção? Já não existia essa função? |
||
|
||
|
||
def save(bu: BuModel): | ||
collection.insert_one(bu.dict()) | ||
collection.update_one( | ||
{"eleicoes": bu.eleicoes, "zona": bu.zona, "secao": bu.secao}, | ||
{"$set": bu.model_dump()}, | ||
upsert=True | ||
) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Por que a gente precisa atualizar o BU? A gente não está trabalhando append-only? |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
from app.database.connection import get_db | ||
from app.database.models.election_data_model import ElectionData | ||
|
||
db = get_db() | ||
|
||
|
||
def save(data: ElectionData): | ||
db[data.merkletree_info.tree_name].insert_one(data.model_dump()) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import base64 | ||
import logging | ||
|
||
from app.database.models.election_data_model import ElectionData | ||
from app.database.models.merkle_tree_info_model import MerkleTreeInfo | ||
from app.database.repositories.election_data_repository import save | ||
from app.services.tlmanager_service import insert_creating_tree_if_not_exists | ||
|
||
|
||
def insert(file_content: bytes, filename: str, tree_name: str): | ||
response = insert_creating_tree_if_not_exists(tree_name, base64.b64encode(file_content).decode('utf-8')) | ||
|
||
election_data = ElectionData( | ||
file_name=filename, | ||
data=file_content, | ||
merkletree_info=MerkleTreeInfo( | ||
tree_name=tree_name, | ||
index=response.json()['index'], | ||
hash=response.json()['value']) | ||
) | ||
|
||
logging.debug("Saving election data to database") | ||
save(election_data) | ||
|
||
return election_data |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import logging | ||
|
||
from app.adapter.tlmanager_adapter import * | ||
|
||
TREE_DEFAULT_COMMITMENT_SIZE = os.getenv('TREE_DEFAULT_COMMITMENT_SIZE') | ||
|
||
|
||
def insert_creating_tree_if_not_exists(tree_name, data, commitment_size=TREE_DEFAULT_COMMITMENT_SIZE): | ||
insert_leaf_response = insert_leaf(tree_name, data) | ||
logging.info(insert_leaf_response.json()) | ||
if insert_leaf_response.status_code != 200 and insert_leaf_response.json().get('message') == "Tree does not exist": | ||
logging.info(f"Creating tree with name {tree_name}") | ||
create_tree(tree_name, commitment_size) | ||
|
||
logging.info(f"Trying to insert leaf for tree {tree_name} again") | ||
insert_leaf_response = insert_leaf(tree_name, data) | ||
logging.info(insert_leaf_response.json()) | ||
|
||
if insert_leaf_response.status_code != 200: | ||
raise Exception("Failed to insert leaf") | ||
|
||
return insert_leaf_response |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
const router = require('express').Router(); | ||
const election_data_repository = require('../database/repository/election-data.repository'); | ||
|
||
router.get("/find_by_merkletree_index_range", async (req, res) => { | ||
const tree_name = req.query.tree_name | ||
const initial_index = req.query.initial_index | ||
const final_index = req.query.final_index | ||
|
||
console.info(`[election-data.controller] GET /find_by_merkletree_index_range ${tree_name} ${initial_index} ${final_index}`) | ||
|
||
const data = await election_data_repository.findByMerkletreeIndexRange(tree_name, initial_index, final_index) | ||
|
||
res.json(data) | ||
}) | ||
|
||
router.get("/find_by_id", async (req, res) => { | ||
const id = req.query.id | ||
const tree_name = req.query.tree_name | ||
|
||
console.info(`[election-data.controller] GET /find_by_id ${tree_name} ${id}`) | ||
|
||
const data = await election_data_repository.findById(tree_name, id) | ||
|
||
res.json(data) | ||
}) | ||
|
||
router.get("/find_by_filename", async (req, res) => { | ||
const filename = req.query.filename | ||
const tree_name = req.query.tree_name | ||
|
||
console.info(`[election-data.controller] GET /find_by_filename ${tree_name} ${filename}`) | ||
|
||
const data = await election_data_repository.findByFilename(tree_name, filename) | ||
|
||
res.json(data) | ||
}) | ||
|
||
router.get("/download", async (req, res) => { | ||
const id = req.query.id | ||
const tree_name = req.query.tree_name | ||
|
||
console.info(`[election-data.controller] GET /download ${tree_name} ${id}`) | ||
|
||
const data = await election_data_repository.findById(tree_name, id) | ||
|
||
if (!data) { | ||
res.status(404).send('Not Found') | ||
return | ||
} | ||
|
||
const buffer = Buffer.from(data.data, 'base64') | ||
res.setHeader('Content-Disposition', `attachment; filename=${data.filename}`) | ||
|
||
res.send(buffer) | ||
}) | ||
|
||
module.exports = router; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
const mongoose = require("mongoose"); | ||
const {merkletree_info} = require("./merkletree-info.model"); | ||
|
||
const schema_election_data = new mongoose.Schema({ | ||
file_name: String, | ||
data: Buffer, | ||
merkletree_info: { type: Object, of: merkletree_info }, | ||
}); | ||
|
||
schema_election_data.index({ "file_name": 1 }); | ||
schema_election_data.index({ "merkletree_info.$**": 1 }); | ||
|
||
module.exports = { schema_election_data } |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
const mongoose = require("mongoose"); | ||
|
||
const merkletree_info = new mongoose.Schema({ | ||
tree_name: String, | ||
index: Number, | ||
hash: String, | ||
}); | ||
|
||
module.exports = { merkletree_info } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Essa lib está sendo utilizada? Se não, é bom tirar