-
Notifications
You must be signed in to change notification settings - Fork 108
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(api): adding revocations, task for coinbase revocations (#649)
- Loading branch information
1 parent
830ca20
commit 60ff1ed
Showing
18 changed files
with
935 additions
and
558 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
46 changes: 46 additions & 0 deletions
46
api/ceramic_cache/management/commands/backfill_ceramic_cache_proof_value.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import time | ||
|
||
from django.core.management.base import BaseCommand | ||
from django.db import connection | ||
|
||
from ceramic_cache.models import CeramicCache | ||
|
||
|
||
class Command(BaseCommand): | ||
help = "Backfills proof_value from JSON data" | ||
|
||
def handle(self, *args, **options): | ||
batch_size = 10000 | ||
max_id = CeramicCache.objects.latest("id").id | ||
current_id = CeramicCache.objects.earliest("id").id | ||
|
||
while current_id <= max_id: | ||
self.stdout.write( | ||
self.style.NOTICE(f"Backfilling starting at id {current_id}") | ||
) | ||
with connection.cursor() as cursor: | ||
cursor.execute( | ||
""" | ||
UPDATE ceramic_cache_ceramiccache | ||
SET | ||
proof_value = COALESCE( | ||
(stamp::json->>'proof')::json->>'proofValue', | ||
(stamp::json->>'proof')::json->>'jws', | ||
'TEST' | ||
) | ||
WHERE | ||
id >= %s | ||
AND id < %s | ||
""", | ||
[current_id, current_id + batch_size], | ||
) | ||
|
||
current_id += batch_size | ||
|
||
self.stdout.write( | ||
self.style.SUCCESS(f"Backfilled up to id {current_id - 1}") | ||
) | ||
|
||
time.sleep(5) | ||
|
||
self.stdout.write(self.style.SUCCESS("Data backfill completed successfully")) |
107 changes: 107 additions & 0 deletions
107
api/ceramic_cache/management/commands/check_coinbase_revocations.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
from datetime import UTC, datetime | ||
from typing import List | ||
|
||
import requests | ||
from django.core.management.base import BaseCommand | ||
|
||
from ceramic_cache.models import CeramicCache, Revocation | ||
from passport_admin.models import LastScheduledRun | ||
|
||
QUERY_URL = "https://base.easscan.org/graphql" | ||
COINBASE_ATTESTER = "0x357458739F90461b99789350868CD7CF330Dd7EE" | ||
SCHEMA = "0xf8b05c79f090979bf4a80270aba232dff11a10d9ca55c4f88de95317970f0de9" | ||
|
||
COINBASE_STAMP_PROVIDER = "CoinbaseDualVerification" | ||
|
||
TASK_NAME = "check_coinbase_revocations" | ||
|
||
|
||
class Command(BaseCommand): | ||
help = "Copy latest stamp weights to eligible scorers and launch rescore" | ||
|
||
def handle(self, *args, **kwargs): | ||
self.stdout.write("Running ...") | ||
|
||
is_first_run = not LastScheduledRun.objects.filter(name=TASK_NAME).exists() | ||
|
||
if is_first_run: | ||
start_time = datetime.fromtimestamp(0).astimezone(UTC) | ||
else: | ||
start_time = LastScheduledRun.objects.get(name=TASK_NAME).last_run | ||
|
||
end_time = datetime.now(UTC) | ||
|
||
self.stdout.write( | ||
self.style.SUCCESS( | ||
f"Checking for revoked attestations between [{start_time}, {end_time})" | ||
) | ||
) | ||
|
||
revoked_addresses = self.get_revoked_attestation_addresses(start_time, end_time) | ||
|
||
self.stdout.write( | ||
self.style.NOTICE(f"Found {len(revoked_addresses)} revoked addresses") | ||
) | ||
|
||
for address in revoked_addresses: | ||
stamps = CeramicCache.objects.filter( | ||
address=address, | ||
provider=COINBASE_STAMP_PROVIDER, | ||
revocation__isnull=True, | ||
) | ||
|
||
for stamp in stamps: | ||
self.stdout.write( | ||
self.style.SUCCESS( | ||
f"Revoking stamp with proof_value={stamp.proof_value} for address={address}" | ||
) | ||
) | ||
|
||
Revocation.objects.create( | ||
proof_value=stamp.proof_value, ceramic_cache=stamp | ||
) | ||
|
||
LastScheduledRun.objects.update_or_create( | ||
name=TASK_NAME, defaults={"last_run": end_time} | ||
) | ||
|
||
self.stdout.write(self.style.SUCCESS("Done")) | ||
|
||
def get_revoked_attestation_addresses( | ||
self, start_time: datetime, end_time: datetime | ||
) -> List[str]: | ||
start_time_unix = int(start_time.timestamp()) | ||
end_time_unix = int(end_time.timestamp()) | ||
|
||
query = f""" | ||
query RevocationsQuery {{ | ||
attestations( | ||
where: {{ | ||
revoked: {{ equals: true }} | ||
attester: {{ equals: "{COINBASE_ATTESTER}" }} | ||
revocationTime: {{ gte: {start_time_unix}, lt: {end_time_unix} }} | ||
schemaId: {{ | ||
equals: "{SCHEMA}" | ||
}} | ||
}} | ||
) {{ | ||
recipient | ||
}} | ||
}} | ||
""" | ||
|
||
response = requests.post( | ||
QUERY_URL, | ||
json={"query": query}, | ||
headers={"Content-Type": "application/json"}, | ||
) | ||
|
||
if response.status_code != 200: | ||
raise Exception(f"Failed to query attestations: {response.text}") | ||
|
||
response_body = response.json() | ||
|
||
return [ | ||
attestation["recipient"].lower() | ||
for attestation in response_body["data"]["attestations"] | ||
] |
44 changes: 44 additions & 0 deletions
44
api/ceramic_cache/migrations/0022_ceramiccache_proof_value_revocation.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
# Generated by Django 4.2.6 on 2024-08-01 23:28 | ||
|
||
import django.db.models.deletion | ||
from django.db import migrations, models | ||
|
||
|
||
class Migration(migrations.Migration): | ||
dependencies = [ | ||
("ceramic_cache", "0021_ceramiccache_expiration_date_and_more"), | ||
] | ||
|
||
operations = [ | ||
migrations.AddField( | ||
model_name="ceramiccache", | ||
name="proof_value", | ||
field=models.CharField(db_index=True, default="", max_length=256), | ||
), | ||
migrations.CreateModel( | ||
name="Revocation", | ||
fields=[ | ||
( | ||
"id", | ||
models.BigAutoField( | ||
auto_created=True, | ||
primary_key=True, | ||
serialize=False, | ||
verbose_name="ID", | ||
), | ||
), | ||
( | ||
"proof_value", | ||
models.CharField(db_index=True, max_length=256, unique=True), | ||
), | ||
( | ||
"ceramic_cache", | ||
models.OneToOneField( | ||
on_delete=django.db.models.deletion.CASCADE, | ||
related_name="revocation", | ||
to="ceramic_cache.ceramiccache", | ||
), | ||
), | ||
], | ||
), | ||
] |
17 changes: 17 additions & 0 deletions
17
api/ceramic_cache/migrations/0023_alter_ceramiccache_proof_value.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
# Generated by Django 4.2.6 on 2024-08-01 23:28 | ||
|
||
from django.db import migrations, models | ||
|
||
|
||
class Migration(migrations.Migration): | ||
dependencies = [ | ||
("ceramic_cache", "0022_ceramiccache_proof_value_revocation"), | ||
] | ||
|
||
operations = [ | ||
migrations.AlterField( | ||
model_name="ceramiccache", | ||
name="proof_value", | ||
field=models.CharField(db_index=True, max_length=256), | ||
), | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.