Construct the datafeed gas strategy based on the speed name

pull/2445/head
David Núñez 2020-11-18 23:05:18 +01:00
parent 71ad8c2c31
commit a56ce5151d
4 changed files with 23 additions and 19 deletions

View File

@ -62,7 +62,7 @@ from nucypher.blockchain.eth.sol.compile.types import SourceBundle
from nucypher.blockchain.eth.utils import get_transaction_name, prettify_eth_amount
from nucypher.characters.control.emitters import JSONRPCStdoutEmitter, StdoutEmitter
from nucypher.utilities.ethereum import encode_constructor_arguments
from nucypher.utilities.gas_strategies import datafeed_fallback_gas_price_strategy, WEB3_GAS_STRATEGIES
from nucypher.utilities.gas_strategies import construct_datafeed_fallback_strategy, WEB3_GAS_STRATEGIES
from nucypher.utilities.logging import GlobalLoggerSettings, Logger
Web3Providers = Union[IPCProvider, WebsocketProvider, HTTPProvider, EthereumTester] # TODO: Move to types.py
@ -284,9 +284,8 @@ class BlockchainInterface:
if gas_strategy:
reported_gas_strategy = f"fixed/{gas_strategy.name}"
elif isinstance(self.client, InfuraClient):
gas_strategy = datafeed_fallback_gas_price_strategy
self.gas_strategy = 'fast'
reported_gas_strategy = "datafeed/fast"
gas_strategy = construct_datafeed_fallback_strategy(speed=self.gas_strategy)
reported_gas_strategy = f"datafeed/{self.gas_strategy}"
else:
reported_gas_strategy = f"web3/{self.gas_strategy}"
gas_strategy = self.get_gas_strategy(self.gas_strategy)

View File

@ -74,10 +74,10 @@ class EthereumGasPriceDatafeed(Datafeed):
return gas_price_wei
@classmethod
def construct_gas_strategy(cls):
def construct_gas_strategy(cls, speed: Optional[str] = None):
def gas_price_strategy(web3: Web3, transaction_params: TxParams = None) -> Wei:
feed = cls()
gas_price = feed.get_gas_price()
gas_price = feed.get_gas_price(speed=speed)
return gas_price
return gas_price_strategy

View File

@ -36,20 +36,22 @@ class GasStrategyError(RuntimeError):
#
def datafeed_fallback_gas_price_strategy(web3: Web3, transaction_params: TxParams = None) -> Wei:
feeds = (EtherchainGasPriceDatafeed, UpvestGasPriceDatafeed)
def construct_datafeed_fallback_strategy(speed: Optional[str] = None) -> Callable:
def datafeed_fallback_gas_price_strategy(web3: Web3, transaction_params: TxParams = None) -> Wei:
feeds = (EtherchainGasPriceDatafeed, UpvestGasPriceDatafeed)
for gas_price_feed_class in feeds:
try:
gas_strategy = gas_price_feed_class.construct_gas_strategy()
gas_price = gas_strategy(web3, transaction_params)
except Datafeed.DatafeedError:
continue
for gas_price_feed_class in feeds:
try:
gas_strategy = gas_price_feed_class.construct_gas_strategy(speed=speed)
gas_price = gas_strategy(web3, transaction_params)
except Datafeed.DatafeedError:
continue
else:
return gas_price
else:
return gas_price
else:
# Worst-case scenario, we get the price from the ETH node itself
return rpc_gas_price_strategy(web3, transaction_params)
# Worst-case scenario, we get the price from the ETH node itself
return rpc_gas_price_strategy(web3, transaction_params)
return datafeed_fallback_gas_price_strategy
#

View File

@ -27,7 +27,7 @@ from nucypher.utilities.datafeeds import (
Datafeed,
UpvestGasPriceDatafeed,
EthereumGasPriceDatafeed)
from nucypher.utilities.gas_strategies import datafeed_fallback_gas_price_strategy
from nucypher.utilities.gas_strategies import construct_datafeed_fallback_strategy
etherchain_json = {
"safeLow": "99.0",
@ -166,6 +166,7 @@ def test_datafeed_fallback_gas_price_strategy():
# In normal circumstances, the first datafeed (Etherchain) will return the gas price
with patch('nucypher.utilities.datafeeds.EtherchainGasPriceDatafeed.construct_gas_strategy',
return_value=mock_gas_strategy):
datafeed_fallback_gas_price_strategy = construct_datafeed_fallback_strategy()
assert datafeed_fallback_gas_price_strategy("web3", "tx") == mocked_gas_price
# If the first datafeed in the chain fails, we resort to the second one
@ -173,6 +174,7 @@ def test_datafeed_fallback_gas_price_strategy():
side_effect=Datafeed.DatafeedError):
with patch('nucypher.utilities.datafeeds.UpvestGasPriceDatafeed.construct_gas_strategy',
return_value=mock_gas_strategy):
datafeed_fallback_gas_price_strategy = construct_datafeed_fallback_strategy()
assert datafeed_fallback_gas_price_strategy("web3", "tx") == mocked_gas_price
# If both datafeeds fail, we fallback to the rpc_gas_price_strategy
@ -181,4 +183,5 @@ def test_datafeed_fallback_gas_price_strategy():
with patch('nucypher.utilities.datafeeds.UpvestGasPriceDatafeed._probe_feed',
side_effect=Datafeed.DatafeedError):
with patch('nucypher.utilities.gas_strategies.rpc_gas_price_strategy', side_effect=mock_gas_strategy):
datafeed_fallback_gas_price_strategy = construct_datafeed_fallback_strategy()
assert datafeed_fallback_gas_price_strategy("web3", "tx") == mocked_gas_price