Skip to content

Commit

Permalink
Revoke app delegate EM action C-2559 (#5201)
Browse files Browse the repository at this point in the history
Co-authored-by: Nikki Kang <kangaroo233@gmail.com>
  • Loading branch information
nicoback2 and nicoback committed May 5, 2023
1 parent c7fec30 commit 6565398
Show file tree
Hide file tree
Showing 6 changed files with 284 additions and 39 deletions.
14 changes: 14 additions & 0 deletions discovery-provider/alembic/trigger_sql/ddl.sql
Expand Up @@ -165,4 +165,18 @@ BEGIN;
txhash varchar not null,
primary key (shared_address, is_current, txhash)
);
COMMIT;

-- 5/4/23: fix app_delegates table to support revokes
BEGIN;
alter table app_delegates
add column if not exists is_current boolean not null;

alter table app_delegates
add column if not exists updated_at timestamp not null;

alter table app_delegates rename column is_revoked to is_delete;

alter table app_delegates drop constraint app_delegates_pkey;
alter table public.app_delegates add constraint app_delegates_pkey primary key (address, is_current, txhash);
COMMIT;
Expand Up @@ -32,7 +32,7 @@


def test_index_delegate(app, mocker):
"Tests delegate create action"
"Tests delegate action"

# setup db and mocked txs
with app.app_context():
Expand Down Expand Up @@ -222,6 +222,78 @@ def get_events_side_effect(_, tx_receipt):
for tx_receipt in tx_receipts
]

with db.scoped_session() as session:
# index transactions
timestamp = 1000000001
entity_manager_update(
None,
update_task,
session,
entity_manager_txs,
block_number=1,
block_timestamp=timestamp,
block_hash=0,
metadata={},
)
# validate db records
all_delegates: List[AppDelegate] = session.query(AppDelegate).all()
# make sure no new rows were added
assert len(all_delegates) == 4

# # Test invalid delete delegate txs
tx_receipts = {
"DeleteDelegateInvalidTx1": [
{
# Incorrect signer
"args": AttributeDict(
{
"_entityId": 0,
"_entityType": EntityType.APP_DELEGATE,
"_action": Action.DELETE,
"_userId": new_delegates_data[0]["user_id"],
"_metadata": f"""{{"address": "{new_delegates_data[0]["address"]}"}}""",
"_signer": "incorrectwallet",
}
)
},
],
"DeleteDelegateInvalidTx2": [
{
# Delegate doesn't exist
"args": AttributeDict(
{
"_entityId": 0,
"_entityType": EntityType.APP_DELEGATE,
"_userId": 2,
"_action": Action.DELETE,
"_metadata": '{"address": "0x3a388671bb4D6E1bbbbD79Ee191b40FB45A8F4C4"}',
"_signer": "user2wallet",
}
)
},
],
"DeleteDelegateInvalidTx3": [
{
# User id doesn't match delegate
"args": AttributeDict(
{
"_entityId": 0,
"_entityType": EntityType.APP_DELEGATE,
"_userId": 1,
"_action": Action.DELETE,
"_metadata": f"""{{"address": "{new_delegates_data[2]["address"]}"}}""",
"_signer": "user1wallet",
}
)
},
],
}

entity_manager_txs = [
AttributeDict({"transactionHash": update_task.web3.toBytes(text=tx_receipt)})
for tx_receipt in tx_receipts
]

with db.scoped_session() as session:
# index transactions
timestamp = 1000000001
Expand All @@ -239,3 +311,100 @@ def get_events_side_effect(_, tx_receipt):
all_delegates: List[AppDelegate] = session.query(AppDelegate).all()
# make sure no new rows were added
assert len(all_delegates) == 4

# Test valid delete delegate txs
tx_receipts = {
"DeleteDelegateTx1": [
{
"args": AttributeDict(
{
"_entityId": 0,
"_entityType": EntityType.APP_DELEGATE,
"_action": Action.DELETE,
"_userId": new_delegates_data[0]["user_id"],
"_metadata": f"""{{"address": "{new_delegates_data[0]["address"]}"}}""",
"_signer": f"user{new_delegates_data[0]['user_id']}wallet",
}
)
},
],
"DeleteDelegateTx2": [
{
"args": AttributeDict(
{
"_entityId": 0,
"_entityType": EntityType.APP_DELEGATE,
"_action": Action.DELETE,
"_userId": new_delegates_data[1]["user_id"],
"_metadata": f"""{{"address": "{new_delegates_data[1]["address"]}"}}""",
"_signer": f"user{new_delegates_data[1]['user_id']}wallet",
}
)
},
],
"DeleteDelegateTx3": [
{
"args": AttributeDict(
{
"_entityId": 0,
"_entityType": EntityType.APP_DELEGATE,
"_action": Action.DELETE,
"_userId": new_delegates_data[2]["user_id"],
"_metadata": f"""{{"address": "{new_delegates_data[2]["address"]}"}}""",
"_signer": f"user{new_delegates_data[2]['user_id']}wallet",
}
)
},
],
}

entity_manager_txs = [
AttributeDict({"transactionHash": update_task.web3.toBytes(text=tx_receipt)})
for tx_receipt in tx_receipts
]

with db.scoped_session() as session:
# index transactions
timestamp = 1000000001
entity_manager_update(
None,
update_task,
session,
entity_manager_txs,
block_number=1,
block_timestamp=timestamp,
block_hash=0,
metadata={},
)
# validate db records
all_delegates: List[AppDelegate] = session.query(AppDelegate).all()
assert len(all_delegates) == 7

for expected_delegate in new_delegates_data:
found_matches = [
item
for item in all_delegates
if item.address == expected_delegate["address"].lower()
]
assert len(found_matches) == 2
old = [item for item in found_matches if item.is_current == False]
assert len(old) == 1
updated = [item for item in found_matches if item.is_current == True]
assert len(updated) == 1
old = old[0]
updated = updated[0]
assert (
old.user_id == expected_delegate["user_id"]
and updated.user_id == expected_delegate["user_id"]
)
assert (
old.name == expected_delegate["name"]
and updated.name == expected_delegate["name"]
)
assert (
old.is_personal_access == expected_delegate["is_personal_access"]
and updated.is_personal_access
== expected_delegate["is_personal_access"]
)
assert old.is_delete == False and updated.is_delete == True
assert old.blocknumber == 0 and updated.blocknumber == 1
2 changes: 2 additions & 0 deletions discovery-provider/integration_tests/utils.py
Expand Up @@ -287,7 +287,9 @@ def populate_mock_db(db, entities, block_offset=None):
is_personal_access=delegate_meta.get("is_personal_access", False),
blockhash=hex(i + block_offset),
blocknumber=(i + block_offset),
is_current=True,
txhash=delegate_meta.get("txhash", str(i + block_offset)),
updated_at=delegate_meta.get("updated_at", datetime.now()),
created_at=delegate_meta.get("created_at", datetime.now()),
)
session.add(delegate)
Expand Down
7 changes: 6 additions & 1 deletion discovery-provider/src/models/delegates/app_delegate.py
Expand Up @@ -12,11 +12,16 @@ class AppDelegate(Base, RepresentableMixin):
user_id = Column(Integer, nullable=True, index=True)
name = Column(String, nullable=False, index=False)
is_personal_access = Column(Boolean, nullable=False, server_default=text("false"))
is_revoked = Column(Boolean, nullable=False, server_default=text("false"))
is_delete = Column(Boolean, nullable=False, server_default=text("false"))
is_current = Column(Boolean, nullable=False, primary_key=True)
created_at = Column(DateTime, nullable=False)
updated_at = Column(DateTime, nullable=False)
txhash = Column(
String,
primary_key=True,
nullable=False,
server_default=text("''::character varying"),
)

def get_attributes_dict(self):
return {col.name: getattr(self, col.name) for col in self.__table__.columns}

0 comments on commit 6565398

Please sign in to comment.