Skip to content

Commit

Permalink
[RELEASE] 0.5.0 (#351)
Browse files Browse the repository at this point in the history
  • Loading branch information
aziolek authored Jul 22, 2024
2 parents 1adc7e2 + 6ade9f3 commit f6cba22
Show file tree
Hide file tree
Showing 12 changed files with 1,645 additions and 9 deletions.
480 changes: 480 additions & 0 deletions backend/app/constants.py

Large diffs are not rendered by default.

10 changes: 9 additions & 1 deletion backend/app/infrastructure/database/uniqueness_quotient.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
from decimal import Decimal
from typing import Optional
from typing import List, Optional

from sqlalchemy.orm import joinedload

from app import db
from app.infrastructure.database.models import UniquenessQuotient, User
Expand All @@ -15,6 +17,12 @@ def get_uq_by_user(user: User, epoch: int) -> Optional[UniquenessQuotient]:
return query.one_or_none()


def get_all_uqs_by_epoch(epoch: int) -> List[UniquenessQuotient]:
query = UniquenessQuotient.query.options(joinedload(UniquenessQuotient.user))
query = query.filter(UniquenessQuotient.epoch == epoch)
return query.all()


def get_uq_by_address(user_address: str, epoch: int) -> Optional[UniquenessQuotient]:
user: User = get_user_by_address(user_address)

Expand Down
46 changes: 46 additions & 0 deletions backend/app/infrastructure/routes/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -256,3 +256,49 @@ def get(self, user_address: str, epoch: int):
app.logger.debug(f"Uniqueness quotient: {uq_score}")

return {"uniquenessQuotient": uq_score}


user_uq_pair_model = api.model(
"UserUQPair",
{
"userAddress": fields.String(
required=True, description="User ethereum address"
),
"uniquenessQuotient": fields.String(
required=True, description="Uniqueness quotient score"
),
},
)

uq_pairs_model = api.model(
"ListUserUQPair",
{
"uqsInfo": fields.List(fields.Nested(user_uq_pair_model), required=True),
},
)


@ns.route("/uq/<int:epoch>/all")
class AllUQScores(OctantResource):
@ns.doc(
description="Returns uniqueness quotient scores for all users for given epoch",
params={
"epoch": "Epoch number",
},
)
@ns.marshal_with(uq_pairs_model)
@ns.response(200, "uniqueness quotients retrieved")
def get(self, epoch: int):
app.logger.debug(f"Getting uniqueness quotients for epoch {epoch}")
uq_scores = uq_controller.get_all_uqs(epoch)
app.logger.debug(f"Uniqueness quotient len: {len(uq_scores)}")

return {
"uqsInfo": [
{
"uniquenessQuotient": uq_score,
"userAddress": user_address,
}
for user_address, uq_score in uq_scores
]
}
11 changes: 11 additions & 0 deletions backend/app/modules/uq/controller.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
from decimal import Decimal
from typing import List, Tuple

from app.context.epoch_state import EpochState
from app.context.manager import epoch_context
from app.exceptions import (
NotImplementedForGivenEpochState,
)
from app.modules.registry import get_services
from app.modules.uq import core


def get_uq(user_address: str, epoch_num: int) -> Decimal:
Expand All @@ -19,3 +21,12 @@ def get_uq(user_address: str, epoch_num: int) -> Decimal:

service = get_services(context.epoch_state).uniqueness_quotients
return service.retrieve(context, user_address)


def get_all_uqs(epoch_num: int) -> List[Tuple[str, Decimal]]:
context = epoch_context(epoch_num)

if context.epoch_state > EpochState.PENDING:
raise NotImplementedForGivenEpochState()

return core.get_all_uqs(epoch_num)
7 changes: 7 additions & 0 deletions backend/app/modules/uq/core.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,17 @@
from decimal import Decimal
from typing import List, Tuple

from app.constants import LOW_UQ_SCORE, MAX_UQ_SCORE
from app.infrastructure.database.uniqueness_quotient import get_all_uqs_by_epoch


def calculate_uq(gp_score: float, uq_threshold: int) -> Decimal:
if gp_score >= uq_threshold:
return MAX_UQ_SCORE

return LOW_UQ_SCORE


def get_all_uqs(epoch_num: int) -> List[Tuple[str, Decimal]]:
all_uqs = get_all_uqs_by_epoch(epoch_num)
return [(uq.user.address, uq.validated_score) for uq in all_uqs]
9 changes: 5 additions & 4 deletions backend/app/modules/user/antisybil/controller.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
import datetime
from datetime import datetime
from typing import Tuple

from app.context.epoch_state import EpochState
from app.context.manager import state_context
from app.modules.registry import get_services


def get_user_antisybil_status(user_address: str) -> (int, datetime):
def get_user_antisybil_status(user_address: str) -> Tuple[int, datetime]:
context = state_context(EpochState.CURRENT)
service = get_services(context.epoch_state).user_antisybil_service
return service.get_antisybil_status(context, user_address)


def update_user_antisybil_status(user_address: str) -> (int, datetime):
def update_user_antisybil_status(user_address: str) -> Tuple[int, datetime]:
context = state_context(EpochState.CURRENT)
service = get_services(context.epoch_state).user_antisybil_service

Expand All @@ -21,4 +22,4 @@ def update_user_antisybil_status(user_address: str) -> (int, datetime):
service.update_antisybil_status(
context, user_address, score, expires_at, all_stamps
)
return score, expires_at
return service.get_antisybil_status(context, user_address)
Loading

0 comments on commit f6cba22

Please sign in to comment.