Skip to content

Commit

Permalink
Don't autopush txs across RPC (#16314)
Browse files Browse the repository at this point in the history
This PR is a step in the direction of a more observer-style wallet.

Currently, most of our wallet functions just push directly to the
network as a matter of course when creating a transaction. While this is
convenient (especially in tests) it will be impossible should we get to
a wallet design that does not contain the private key as a key component
to wallet operations.

For now, the wallet still signs the transaction and has it ready to be
pushed to the network should the user specify that. But all of the
pushing logic has been migrated to the top-most layer available (usually
the RPC) so that the user always has control over whether or not they
would like to immediately push the transaction.

In addition, any transaction method in the RPC now returns a
`"transactions"` key so that it is easy for a front-end client to have
everything they need to push the transaction as if the wallet was doing
it (using the `push_transactions` endpoint).
  • Loading branch information
Quexington authored Nov 6, 2023
2 parents 188bb20 + 83e980e commit 008f3ba
Show file tree
Hide file tree
Showing 20 changed files with 819 additions and 358 deletions.
22 changes: 7 additions & 15 deletions chia/pools/pool_wallet.py
Original file line number Diff line number Diff line change
Expand Up @@ -463,7 +463,6 @@ async def create_new_pool_wallet_transaction(
name=spend_bundle.name(),
valid_times=parse_timelock_info(extra_conditions),
)
await standard_wallet.push_transaction(standard_wallet_record)
p2_singleton_puzzle_hash: bytes32 = launcher_id_to_p2_puzzle_hash(
launcher_coin_id, p2_singleton_delay_time, p2_singleton_delayed_ph
)
Expand Down Expand Up @@ -528,17 +527,6 @@ async def generate_fee_transaction(
)
return fee_tx

async def publish_transactions(self, travel_tx: TransactionRecord, fee_tx: Optional[TransactionRecord]) -> None:
# We create two transaction records, one for the pool wallet to keep track of the travel TX, and another
# for the standard wallet to keep track of the fee. However, we will only submit the first one to the
# blockchain, and this one has the fee inside it as well.
# The fee tx, if present, will be added to the DB with no spend_bundle set, which has the effect that it
# will not be sent to full nodes.

await self.wallet_state_manager.add_pending_transaction(travel_tx)
if fee_tx is not None:
await self.wallet_state_manager.add_pending_transaction(dataclasses.replace(fee_tx, spend_bundle=None))

async def generate_travel_transactions(
self, fee: uint64, tx_config: TXConfig
) -> Tuple[TransactionRecord, Optional[TransactionRecord]]:
Expand Down Expand Up @@ -622,6 +610,7 @@ async def generate_travel_transactions(
fee_tx = await self.generate_fee_transaction(fee, tx_config)
assert fee_tx.spend_bundle is not None
signed_spend_bundle = SpendBundle.aggregate([signed_spend_bundle, fee_tx.spend_bundle])
fee_tx = dataclasses.replace(fee_tx, spend_bundle=None)

tx_record = TransactionRecord(
confirmed_at_height=uint32(0),
Expand All @@ -643,7 +632,6 @@ async def generate_travel_transactions(
valid_times=ConditionValidTimes(),
)

await self.publish_transactions(tx_record, fee_tx)
return tx_record, fee_tx

@staticmethod
Expand Down Expand Up @@ -926,7 +914,6 @@ async def claim_pool_rewards(
valid_times=ConditionValidTimes(),
)

await self.publish_transactions(absorb_transaction, fee_tx)
return absorb_transaction, fee_tx

async def new_peak(self, peak_height: uint32) -> None:
Expand Down Expand Up @@ -971,7 +958,12 @@ async def new_peak(self, peak_height: uint32) -> None:
assert self.target_state.relative_lock_height >= self.MINIMUM_RELATIVE_LOCK_HEIGHT
assert self.target_state.pool_url is not None

await self.generate_travel_transactions(self.next_transaction_fee, self.next_tx_config)
travel_tx, fee_tx = await self.generate_travel_transactions(
self.next_transaction_fee, self.next_tx_config
)
await self.wallet_state_manager.add_pending_transaction(travel_tx)
if fee_tx is not None:
await self.wallet_state_manager.add_pending_transaction(fee_tx)

async def have_unconfirmed_transaction(self) -> bool:
unconfirmed: List[TransactionRecord] = await self.wallet_state_manager.tx_store.get_unconfirmed_for_wallet(
Expand Down
12 changes: 11 additions & 1 deletion chia/rpc/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,16 @@ async def rpc_endpoint(self, request: Dict[str, Any], *args, **kwargs) -> Dict[s
):
raise ValueError("Relative timelocks are not currently supported in the RPC")

return await func(self, request, *args, tx_config=tx_config, extra_conditions=extra_conditions, **kwargs)
push: Optional[bool] = request.get("push")

return await func(
self,
request,
*args,
tx_config=tx_config,
extra_conditions=extra_conditions,
**({"push": push} if push is not None else {}),
**kwargs,
)

return rpc_endpoint
Loading

0 comments on commit 008f3ba

Please sign in to comment.