Add AsyncTxHooks helper class for defining async tx callbacks when using send_async_transaction and the atxm library.

Allow AsyncTxHooks to be specified when executing an async transaction.
pull/3476/head
derekpierre 2024-04-11 12:21:36 -04:00
parent 26637f87e6
commit 14c9d49d8e
No known key found for this signature in database
2 changed files with 35 additions and 3 deletions

View File

@ -44,7 +44,10 @@ from nucypher.blockchain.eth.constants import (
TACO_CHILD_APPLICATION_CONTRACT_NAME,
)
from nucypher.blockchain.eth.decorators import contract_api
from nucypher.blockchain.eth.interfaces import BlockchainInterfaceFactory
from nucypher.blockchain.eth.interfaces import (
BlockchainInterface,
BlockchainInterfaceFactory,
)
from nucypher.blockchain.eth.models import PHASE1, PHASE2, Coordinator, Ferveo
from nucypher.blockchain.eth.registry import (
ContractRegistry,
@ -729,6 +732,7 @@ class CoordinatorAgent(EthereumContractAgent):
ritual_id: int,
transcript: Transcript,
transacting_power: TransactingPower,
async_tx_hooks: BlockchainInterface.AsyncTxHooks,
) -> AsyncTx:
contract_function: ContractFunction = self.contract.functions.postTranscript(
ritualId=ritual_id, transcript=bytes(transcript)
@ -736,6 +740,7 @@ class CoordinatorAgent(EthereumContractAgent):
async_tx = self.blockchain.send_async_transaction(
contract_function=contract_function,
transacting_power=transacting_power,
async_tx_hooks=async_tx_hooks,
info={"ritual_id": ritual_id, "phase": PHASE1},
)
return async_tx
@ -748,6 +753,7 @@ class CoordinatorAgent(EthereumContractAgent):
public_key: DkgPublicKey,
participant_public_key: SessionStaticKey,
transacting_power: TransactingPower,
async_tx_hooks: BlockchainInterface.AsyncTxHooks,
) -> AsyncTx:
contract_function: ContractFunction = self.contract.functions.postAggregation(
ritualId=ritual_id,
@ -759,6 +765,7 @@ class CoordinatorAgent(EthereumContractAgent):
contract_function=contract_function,
gas_estimation_multiplier=1.4,
transacting_power=transacting_power,
async_tx_hooks=async_tx_hooks,
info={"ritual_id": ritual_id, "phase": PHASE2},
)
return async_tx

View File

@ -6,7 +6,9 @@ from urllib.parse import urlparse
import requests
from atxm import AutomaticTxMachine
from atxm.tx import AsyncTx
from atxm.exceptions import InsufficientFunds
from atxm.strategies import ExponentialSpeedupStrategy
from atxm.tx import AsyncTx, FaultedTx, FinalizedTx, FutureTx, PendingTx
from constant_sorrow.constants import (
INSUFFICIENT_FUNDS, # noqa
NO_BLOCKCHAIN_CONNECTION, # noqa
@ -132,6 +134,23 @@ class BlockchainInterface:
)
return message
class AsyncTxHooks:
def __init__(
self,
on_broadcast_failure: Callable[[FutureTx, Exception], None],
on_fault: Callable[[FaultedTx], None],
on_finalized: Callable[[FinalizedTx], None],
on_insufficient_funds: Callable[
[Union[FutureTx, PendingTx], InsufficientFunds], None
],
on_broadcast: Optional[Callable[["PendingTx"], None]] = None,
):
self.on_broadcast_failure = on_broadcast_failure
self.on_fault = on_fault
self.on_finalized = on_finalized
self.on_insufficient_funds = on_insufficient_funds
self.on_broadcast = on_broadcast
def __init__(
self,
emitter=None, # TODO # 1754
@ -658,9 +677,10 @@ class BlockchainInterface:
self,
contract_function: ContractFunction,
transacting_power: TransactingPower,
async_tx_hooks: AsyncTxHooks,
transaction_gas_limit: Optional[int] = None,
gas_estimation_multiplier: float = 1.15,
info: Optional[Dict] = None,
info: Optional[Dict[str, str]] = None,
payload: dict = None,
) -> AsyncTx:
transaction = self.build_contract_transaction(
@ -686,6 +706,11 @@ class BlockchainInterface:
info=info,
params=transaction,
signer=signer,
on_broadcast=async_tx_hooks.on_broadcast,
on_broadcast_failure=async_tx_hooks.on_broadcast_failure,
on_fault=async_tx_hooks.on_fault,
on_finalized=async_tx_hooks.on_finalized,
on_insufficient_funds=async_tx_hooks.on_insufficient_funds,
)
return async_tx