Skip to content
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

Use fast_keccak when posible #1917

Merged
merged 1 commit into from
Mar 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,5 @@ psycogreen==1.0.2
psycopg2==2.9.9
redis==5.0.2
requests==2.31.0
safe-eth-py[django]==6.0.0b18
safe-eth-py[django]==6.0.0b21
web3==6.15.1
14 changes: 7 additions & 7 deletions safe_transaction_service/history/tests/factories.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
from factory.django import DjangoModelFactory
from factory.fuzzy import FuzzyInteger
from hexbytes import HexBytes
from web3 import Web3

from gnosis.eth.constants import NULL_ADDRESS
from gnosis.eth.utils import fast_keccak_text
from gnosis.safe.safe_signature import SafeSignatureType

from ..models import (
Expand Down Expand Up @@ -53,8 +53,8 @@ class Meta:
gas_limit = factory.fuzzy.FuzzyInteger(100000000, 200000000)
gas_used = factory.fuzzy.FuzzyInteger(100000, 500000)
timestamp = factory.LazyFunction(timezone.now)
block_hash = factory.Sequence(lambda n: Web3.keccak(text=f"block-{n}").hex())
parent_hash = factory.Sequence(lambda n: Web3.keccak(text=f"block{n - 1}").hex())
block_hash = factory.Sequence(lambda n: fast_keccak_text(f"block-{n}").hex())
parent_hash = factory.Sequence(lambda n: fast_keccak_text(f"block{n - 1}").hex())


class EthereumTxFactory(DjangoModelFactory):
Expand All @@ -63,7 +63,7 @@ class Meta:

block = factory.SubFactory(EthereumBlockFactory)
tx_hash = factory.Sequence(
lambda n: Web3.keccak(text=f"ethereum_tx_hash-{n}").hex()
lambda n: fast_keccak_text(f"ethereum_tx_hash-{n}").hex()
)
_from = factory.LazyFunction(lambda: Account.create().address)
gas = factory.fuzzy.FuzzyInteger(1000, 5000)
Expand Down Expand Up @@ -226,7 +226,7 @@ class Meta:
module = factory.LazyFunction(lambda: Account.create().address)
to = factory.LazyFunction(lambda: Account.create().address)
value = FuzzyInteger(low=0, high=10)
data = factory.Sequence(lambda n: Web3.keccak(text=f"module-tx-{n}"))
data = factory.Sequence(lambda n: fast_keccak_text(f"module-tx-{n}"))
operation = FuzzyInteger(low=0, high=1)
failed = False

Expand All @@ -236,7 +236,7 @@ class Meta:
model = MultisigTransaction

safe_tx_hash = factory.Sequence(
lambda n: Web3.keccak(text=f"multisig-tx-{n}").hex()
lambda n: fast_keccak_text(f"multisig-tx-{n}").hex()
)
safe = factory.LazyFunction(lambda: Account.create().address)
ethereum_tx = factory.SubFactory(EthereumTxFactory)
Expand All @@ -263,7 +263,7 @@ class Meta:
ethereum_tx = factory.SubFactory(EthereumTxFactory)
multisig_transaction = factory.SubFactory(MultisigTransactionFactory)
multisig_transaction_hash = factory.Sequence(
lambda n: Web3.keccak(text=f"multisig-confirmation-tx-{n}").hex()
lambda n: fast_keccak_text(f"multisig-confirmation-tx-{n}").hex()
)
owner = factory.LazyFunction(lambda: Account.create().address)
signature = None
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@

from eth_account import Account
from requests.exceptions import ConnectionError as RequestsConnectionError
from web3 import Web3

from gnosis.eth import EthereumClient
from gnosis.eth.tests.ethereum_test_case import EthereumTestCaseMixin
from gnosis.eth.utils import fast_keccak_text

from ..models import (
EthereumTx,
Expand Down Expand Up @@ -88,7 +88,7 @@ def test_create_or_update_from_tx_hashes_existing(self):

# Test block hash changes
ethereum_tx = ethereum_txs[0]
ethereum_tx.block.block_hash = Web3.keccak(text="aloha")
ethereum_tx.block.block_hash = fast_keccak_text("aloha")
ethereum_tx.block.save(update_fields=["block_hash"])
tx_hash = ethereum_tx.tx_hash

Expand Down
31 changes: 16 additions & 15 deletions safe_transaction_service/history/tests/test_migrations.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@

from django_test_migrations.migrator import Migrator
from eth_account import Account
from web3 import Web3

from gnosis.eth.utils import fast_keccak, fast_keccak_text


class TestMigrations(TestCase):
Expand All @@ -25,13 +26,13 @@ def build_ethereum_tx(self, ethereum_block_class, ethereum_tx_class):
gas_limit=2,
gas_used=2,
timestamp=timezone.now(),
block_hash=Web3.keccak(b"34"),
parent_hash=Web3.keccak(b"12"),
block_hash=fast_keccak(b"34"),
parent_hash=fast_keccak(b"12"),
)

return ethereum_tx_class.objects.create(
block=ethereum_block,
tx_hash=Web3.keccak(b"tx-hash"),
tx_hash=fast_keccak(b"tx-hash"),
gas=23000,
gas_price=1,
nonce=0,
Expand All @@ -53,7 +54,7 @@ def test_migration_forward_0068(self):
]
for origin in origins:
MultisigTransactionOld.objects.create(
safe_tx_hash=Web3.keccak(text=f"multisig-tx-{origin}").hex(),
safe_tx_hash=fast_keccak_text(f"multisig-tx-{origin}").hex(),
safe=Account.create().address,
value=0,
operation=0,
Expand All @@ -72,21 +73,21 @@ def test_migration_forward_0068(self):
)

# String should keep string
hash = Web3.keccak(text=f"multisig-tx-{origins[0]}").hex()
hash = fast_keccak_text(f"multisig-tx-{origins[0]}").hex()
self.assertEqual(MultisigTransactionNew.objects.get(pk=hash).origin, origins[0])

# String json should be converted to json
hash = Web3.keccak(text=f"multisig-tx-{origins[1]}").hex()
hash = fast_keccak_text(f"multisig-tx-{origins[1]}").hex()
self.assertEqual(
MultisigTransactionNew.objects.get(pk=hash).origin, json.loads(origins[1])
)

# Empty string should be empty object
hash = Web3.keccak(text=f"multisig-tx-{origins[2]}").hex()
hash = fast_keccak_text(f"multisig-tx-{origins[2]}").hex()
self.assertEqual(MultisigTransactionNew.objects.get(pk=hash).origin, {})

# None should be empty object
hash = Web3.keccak(text=f"multisig-tx-{origins[2]}").hex()
hash = fast_keccak_text(f"multisig-tx-{origins[2]}").hex()
self.assertEqual(MultisigTransactionNew.objects.get(pk=hash).origin, {})

def test_migration_backward_0068(self):
Expand All @@ -99,7 +100,7 @@ def test_migration_backward_0068(self):
origins = ["{ TestString", {"url": "https://example.com", "name": "app"}, {}]
for origin in origins:
MultisigTransactionNew.objects.create(
safe_tx_hash=Web3.keccak(text=f"multisig-tx-{origin}").hex(),
safe_tx_hash=fast_keccak_text(f"multisig-tx-{origin}").hex(),
safe=Account.create().address,
value=0,
operation=0,
Expand All @@ -118,17 +119,17 @@ def test_migration_backward_0068(self):
)

# String should keep string
hash = Web3.keccak(text=f"multisig-tx-{origins[0]}").hex()
hash = fast_keccak_text(f"multisig-tx-{origins[0]}").hex()
self.assertEqual(MultisigTransactionOld.objects.get(pk=hash).origin, origins[0])

# Json should be converted to a string json
hash = Web3.keccak(text=f"multisig-tx-{origins[1]}").hex()
hash = fast_keccak_text(f"multisig-tx-{origins[1]}").hex()
self.assertEqual(
MultisigTransactionOld.objects.get(pk=hash).origin, json.dumps(origins[1])
)

# Empty object should be None
hash = Web3.keccak(text=f"multisig-tx-{origins[2]}").hex()
hash = fast_keccak_text(f"multisig-tx-{origins[2]}").hex()
self.assertEqual(MultisigTransactionOld.objects.get(pk=hash).origin, None)

def test_migration_forward_0069(self):
Expand Down Expand Up @@ -262,7 +263,7 @@ def test_migration_forward_0073_safe_apps_links(self):
MultisigTransaction = new_state.apps.get_model("history", "MultisigTransaction")
for origin in origins:
MultisigTransaction.objects.create(
safe_tx_hash=Web3.keccak(text=f"multisig-tx-{origin}").hex(),
safe_tx_hash=fast_keccak_text(f"multisig-tx-{origin}").hex(),
safe=Account.create().address,
value=0,
operation=0,
Expand Down Expand Up @@ -310,7 +311,7 @@ def test_migration_backward_0073_safe_apps_links(self):
MultisigTransaction = new_state.apps.get_model("history", "MultisigTransaction")
for origin in origins:
MultisigTransaction.objects.create(
safe_tx_hash=Web3.keccak(text=f"multisig-tx-{origin}").hex(),
safe_tx_hash=fast_keccak_text(f"multisig-tx-{origin}").hex(),
safe=Account.create().address,
value=0,
operation=0,
Expand Down
8 changes: 4 additions & 4 deletions safe_transaction_service/history/tests/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
from django.utils import timezone

from eth_account import Account
from web3 import Web3

from gnosis.eth.utils import fast_keccak_text
from gnosis.safe.safe_signature import SafeSignatureType

from safe_transaction_service.contracts.models import ContractQuerySet
Expand Down Expand Up @@ -60,7 +60,7 @@

class TestModelSignals(TestCase):
def test_bind_confirmations(self):
safe_tx_hash = Web3.keccak(text="prueba")
safe_tx_hash = fast_keccak_text("prueba")
ethereum_tx = EthereumTxFactory()
MultisigConfirmation.objects.create(
ethereum_tx=ethereum_tx,
Expand All @@ -87,7 +87,7 @@ def test_bind_confirmations(self):
self.assertEqual(multisig_tx.confirmations.count(), 1)

def test_bind_confirmations_reverse(self):
safe_tx_hash = Web3.keccak(text="prueba")
safe_tx_hash = fast_keccak_text("prueba")
ethereum_tx = EthereumTxFactory()
multisig_tx, _ = MultisigTransaction.objects.get_or_create(
safe_tx_hash=safe_tx_hash,
Expand Down Expand Up @@ -1257,7 +1257,7 @@ def test_get_or_create_from_block(self):

# Test block with different block-hash but same block number
mock_block_2 = dict(mock_block)
mock_block_2["hash"] = Web3.keccak(text="another-hash")
mock_block_2["hash"] = fast_keccak_text("another-hash")
self.assertNotEqual(mock_block["hash"], mock_block_2["hash"])
with self.assertRaises(IntegrityError):
EthereumBlock.objects.get_or_create_from_block(mock_block_2)
Expand Down
8 changes: 4 additions & 4 deletions safe_transaction_service/history/tests/test_tx_processor.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@

from eth_account import Account
from eth_utils import keccak
from web3 import Web3

from gnosis.eth.ethereum_client import TracingManager
from gnosis.eth.utils import fast_keccak_text
from gnosis.safe.safe_signature import SafeSignatureType
from gnosis.safe.tests.safe_test_case import SafeTestCaseMixin

Expand Down Expand Up @@ -340,7 +340,7 @@ def test_tx_processor_is_failed(self):
ethereum_tx = EthereumTxFactory(logs=logs)
self.assertTrue(tx_processor.is_failed(ethereum_tx, logs[0]["data"]))
self.assertFalse(
tx_processor.is_failed(ethereum_tx, Web3.keccak(text="hola").hex())
tx_processor.is_failed(ethereum_tx, fast_keccak_text("hola").hex())
)

# Event for Safes >= 1.1.1
Expand All @@ -359,7 +359,7 @@ def test_tx_processor_is_failed(self):
ethereum_tx = EthereumTxFactory(logs=logs)
self.assertTrue(tx_processor.is_failed(ethereum_tx, safe_tx_hash))
self.assertFalse(
tx_processor.is_failed(ethereum_tx, Web3.keccak(text="hola").hex())
tx_processor.is_failed(ethereum_tx, fast_keccak_text("hola").hex())
)

# Event for Safes >= 1.4.1
Expand All @@ -378,7 +378,7 @@ def test_tx_processor_is_failed(self):
ethereum_tx = EthereumTxFactory(logs=logs)
self.assertTrue(tx_processor.is_failed(ethereum_tx, safe_tx_hash))
self.assertFalse(
tx_processor.is_failed(ethereum_tx, Web3.keccak(text="hola").hex())
tx_processor.is_failed(ethereum_tx, fast_keccak_text("hola").hex())
)

def test_tx_is_version_breaking_signatures(self):
Expand Down
11 changes: 5 additions & 6 deletions safe_transaction_service/history/tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,10 @@
from rest_framework import status
from rest_framework.exceptions import ErrorDetail
from rest_framework.test import APIRequestFactory, APITestCase, force_authenticate
from web3 import Web3

from gnosis.eth.constants import NULL_ADDRESS
from gnosis.eth.ethereum_client import EthereumClient, TracingManager
from gnosis.eth.utils import fast_is_checksum_address
from gnosis.eth.utils import fast_is_checksum_address, fast_keccak_text
from gnosis.safe import CannotEstimateGas, Safe, SafeOperationEnum
from gnosis.safe.safe_signature import SafeSignature, SafeSignatureType
from gnosis.safe.signatures import signature_to_bytes
Expand Down Expand Up @@ -684,7 +683,7 @@ def test_get_module_transaction(self):
)

def test_get_multisig_confirmation(self):
random_safe_tx_hash = Web3.keccak(text="enxebre").hex()
random_safe_tx_hash = fast_keccak_text("enxebre").hex()
response = self.client.get(
reverse(
"v1:history:multisig-transaction-confirmations",
Expand All @@ -710,7 +709,7 @@ def test_get_multisig_confirmation(self):
self.assertEqual(response.data["count"], 2)

def test_post_multisig_confirmation(self):
random_safe_tx_hash = Web3.keccak(text="enxebre").hex()
random_safe_tx_hash = fast_keccak_text("enxebre").hex()
data = {
"signature": Account.create()
.signHash(random_safe_tx_hash)["signature"]
Expand Down Expand Up @@ -819,7 +818,7 @@ def test_post_multisig_confirmation(self):
self.assertEqual(MultisigConfirmation.objects.count(), 2)

def test_get_multisig_transaction(self):
safe_tx_hash = Web3.keccak(text="gnosis").hex()
safe_tx_hash = fast_keccak_text("gnosis").hex()
response = self.client.get(
reverse("v1:history:multisig-transaction", args=(safe_tx_hash,)),
format="json",
Expand Down Expand Up @@ -903,7 +902,7 @@ def test_get_multisig_transaction(self):

def test_delete_multisig_transaction(self):
owner_account = Account.create()
safe_tx_hash = Web3.keccak(text="random-tx").hex()
safe_tx_hash = fast_keccak_text("random-tx").hex()
url = reverse("v1:history:multisig-transaction", args=(safe_tx_hash,))
data = {"signature": "0x" + "1" * (130 * 2)} # 2 signatures of 65 bytes
response = self.client.delete(url, format="json", data=data)
Expand Down
9 changes: 5 additions & 4 deletions safe_transaction_service/notifications/tests/test_tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
from django.test import TestCase

from eth_account import Account
from web3 import Web3

from gnosis.eth.utils import fast_keccak_text

from safe_transaction_service.history.models import (
EthereumTxCallType,
Expand Down Expand Up @@ -91,7 +92,7 @@ def test_send_notification_owner_task(self):
safe_address = safe_contract.address
threshold = 2
owners = [Account.create().address for _ in range(2)]
safe_tx_hash = Web3.keccak(text="hola").hex()
safe_tx_hash = fast_keccak_text("hola").hex()
with self.assertLogs(logger=task_logger) as cm:
self.assertEqual(
send_notification_owner_task.delay(safe_address, safe_tx_hash).result,
Expand Down Expand Up @@ -183,7 +184,7 @@ def test_send_notification_owner_task(self):
self.assertIn("does not require more confirmations", cm.output[0])

def test_send_notification_owner_delegate_task(self):
safe_tx_hash = Web3.keccak(text="aloha").hex()
safe_tx_hash = fast_keccak_text("aloha").hex()
safe_contract = SafeContractFactory()
safe_address = safe_contract.address
safe_status = SafeLastStatusFactory(address=safe_address, threshold=3)
Expand Down Expand Up @@ -218,7 +219,7 @@ def test_send_notification_owner_delegate_task(self):

def test_send_notification_owner_task_called(self):
safe_address = Account.create().address
safe_tx_hash = Web3.keccak(text="hola").hex()
safe_tx_hash = fast_keccak_text("hola").hex()
payload = {
"address": safe_address,
"type": WebHookType.PENDING_MULTISIG_TRANSACTION.name,
Expand Down
Loading