Skip to content

Commit

Permalink
Add tests for constructor parameters and legacy tx speed up functiona…
Browse files Browse the repository at this point in the history
…lity.
  • Loading branch information
derekpierre committed Mar 4, 2024
1 parent 55fe942 commit 3ca26b9
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 2 deletions.
2 changes: 2 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ def legacy_transaction(account, w3):
params = {
"chainId": 1337,
"nonce": 0,
"from": account.address,
"to": account.address,
"value": 0,
"gas": 21000,
Expand All @@ -44,6 +45,7 @@ def eip1559_transaction(account, w3):
params = {
"chainId": 1337,
"nonce": 0,
"from": account.address,
"to": account.address,
"value": 0,
"gas": 21000,
Expand Down
83 changes: 81 additions & 2 deletions tests/test_strategy.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import math
from datetime import datetime, timedelta

import pytest
Expand Down Expand Up @@ -74,7 +75,7 @@ def warning_trapper(event):
e.message = "Transaction has timed out"


def test_speedup_strategy(w3, eip1559_transaction, mocker):
def test_speedup_strategy_constructor(w3):
# invalid increase percentage
for speedup_perc in [-1, -0.24, 0, 1.01, 1.1]:
with pytest.raises(ValueError):
Expand All @@ -85,5 +86,83 @@ def test_speedup_strategy(w3, eip1559_transaction, mocker):
with pytest.raises(ValueError):
_ = FixedRateSpeedUp(w3=w3, max_tip_factor=max_tip)

speedup_strategy = FixedRateSpeedUp(w3)
# defaults
speedup_strategy = FixedRateSpeedUp(w3=w3)
assert speedup_strategy.speedup_factor == (
1 + FixedRateSpeedUp._SPEEDUP_INCREASE_PERCENTAGE
)
assert speedup_strategy.max_tip_factor == FixedRateSpeedUp._MAX_TIP_FACTOR

# other values
speedup_increase = 0.223
max_tip_factor = 4
speedup_strategy = FixedRateSpeedUp(
w3=w3,
speedup_increase_percentage=speedup_increase,
max_tip_factor=max_tip_factor,
)
assert speedup_strategy.speedup_factor == (1 + speedup_increase)
assert speedup_strategy.max_tip_factor == max_tip_factor


def test_speedup_strategy_legacy_tx(w3, legacy_transaction, mocker):
speedup_percentage = 0.112 # 11.2%
speedup_strategy = FixedRateSpeedUp(
w3, speedup_increase_percentage=speedup_percentage
)
assert speedup_strategy.name == "speedup"

transaction_count = legacy_transaction["nonce"]
mocker.patch.object(w3.eth, "get_transaction_count", return_value=transaction_count)
pending_tx = mocker.Mock(spec=PendingTx)
pending_tx.id = 1

# legacy transaction - keep speeding up

# generated gas price < existing tx gas price
generated_gas_price = legacy_transaction["gasPrice"] - 1 # < what is in tx
mocker.patch.object(w3.eth, "generate_gas_price", return_value=generated_gas_price)

assert legacy_transaction["gasPrice"]
tx_params = dict(legacy_transaction)
for i in range(3):
pending_tx.params = tx_params
old_gas_price = tx_params["gasPrice"]
old_nonce = tx_params["nonce"]
tx_params = speedup_strategy.execute(pending_tx)

current_gas_price = tx_params["gasPrice"]
assert current_gas_price != old_gas_price
assert current_gas_price == math.ceil(old_gas_price * (1 + speedup_percentage))
assert tx_params["nonce"] == old_nonce

# generated gas price is None - same results as before
mocker.patch.object(w3.eth, "generate_gas_price", return_value=None) # set to None
for i in range(3):
tx_params = dict(legacy_transaction)
pending_tx.params = tx_params
old_gas_price = tx_params["gasPrice"]
old_nonce = tx_params["nonce"]
tx_params = speedup_strategy.execute(pending_tx)

current_gas_price = tx_params["gasPrice"]
assert current_gas_price != old_gas_price
assert current_gas_price == math.ceil(old_gas_price * (1 + speedup_percentage))
assert tx_params["nonce"] == old_nonce

# increase generated gas price more than existing gas price in legacy tx
tx_params = dict(legacy_transaction)
pending_tx.params = tx_params
generated_gas_price = tx_params["gasPrice"] * 2 # > what is in tx
mocker.patch.object(w3.eth, "generate_gas_price", return_value=generated_gas_price)

old_gas_price = tx_params["gasPrice"]
old_nonce = tx_params["nonce"]
updated_tx_params = speedup_strategy.execute(pending_tx)

current_gas_price = updated_tx_params["gasPrice"]
assert current_gas_price != old_gas_price
assert current_gas_price == math.ceil(
generated_gas_price * (1 + speedup_percentage)
)
assert updated_tx_params["nonce"] == old_nonce

0 comments on commit 3ca26b9

Please sign in to comment.