eth-account icon indicating copy to clipboard operation
eth-account copied to clipboard

web3.eth.account signature hypothesis tests

Open carver opened this issue 7 years ago • 5 comments

What was wrong?

Want to catch/prevent more issues like ethereum/web3.py#466 earlier.

How can it be fixed?

More hypothesis tests. Piper's suggestions (on ethereum/web3.py#477):

  • A signed transaction's signature recovers to the expected sender address
  • The raw transaction can be rlp decoded into a transaction object
  • An rlp decoded transaction object's v,r,s match the v,r,s from the signature.
  • The signature bytes are 65 bytes in length

The downside is that they can be pretty time-intensive. Just a test for the length of signature bytes added 7 seconds to a 53-second test suite.

Example:

--- a/tests/core/eth-module/test_accounts.py
+++ b/tests/core/eth-module/test_accounts.py
@@ -2,10 +2,19 @@
 
 import pytest
 
+from eth_keys.constants import (
+    SECPK1_N,
+)
 from eth_utils import (
     is_checksum_address,
 )
 
+from hypothesis import (
+    example,
+    given,
+    strategies as st,
+)
+
 from web3 import (
     Account,
     Web3,
@@ -268,6 +277,16 @@ def test_eth_account_sign(acct, message, key, expected_bytes, expected_hash, v,
     assert account.sign(message_text=message) == signed
 
 
+@given(st.binary(), st.integers(
+    min_value=Web3.toInt(hexstr='0x1' + '00' * 31),
+    max_value=SECPK1_N - 1
+    )
+)
+def test_message_signature_length(acct, message, key):
+    signed = acct.sign(message, key)
+    assert len(signed.signature) == 65
+
+
 @pytest.mark.parametrize(
     'txn, private_key, expected_raw_tx, tx_hash, r, s, v',
     (

carver avatar Nov 30 '17 18:11 carver

We can use the same strategy that we used to speed up the test suite in the Py-EVM codebase for slow tests like this. We can setup a daily cron run on travis that runs and only run the slow tests during the cron run.

See implementation here: https://github.com/ethereum/py-evm/blob/master/tests/json-fixtures/test_state.py#L116-L119

pipermerriam avatar Nov 30 '17 18:11 pipermerriam

[old issue cleanup]

I believe this hypothesis testing belongs (and already exists) in eth-account. Correct me if I'm wrong.

wolovim avatar Sep 18 '20 20:09 wolovim

Hm, I don't think this particular test exists yet in eth-account. Maybe copy/move the issue over and reopen?

carver avatar Sep 18 '20 22:09 carver

Re: transfer, looks like I've got insufficient privileges in eth-account @carver. Would you mind moving it over? (The -> Transfer issue button lives in the sidebar.) Thanks!

wolovim avatar Sep 18 '20 23:09 wolovim

@marcgarreau Transferred! TIL ✨

kclowes avatar Sep 21 '20 17:09 kclowes