Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

tx: Remove wait_for_in_block helper method #1237

Merged
merged 1 commit into from
Nov 2, 2023
Merged

Conversation

lexnv
Copy link
Collaborator

@lexnv lexnv commented Oct 31, 2023

The wait_for_in_block waits until the transaction stream produces a TxStatus::InBestBlock event.

Transitioning to the latest RPC-V2, mainly using the transaction and chainHead, there is no strong guarantee that a new block reported by the transaction method will ever be reported by the chainHead.
This edge case could happen when the transaction API encounters a best block that is immediately pruned, possibly leading to the chainHead not gaining visibility for the said pruned block.

To offer stronger guarantees, the subxt submit_transaction ensures that a block reported by TxStatus::InFinalizedBlock is accompanied by a chainHead::Finalized event.

Users who may still want the same behavior for testing purposes could then:

let mut progress: TxProgress = ..;

while let Some(status) = self.next().await {
    match status? {
        TxStatus::InBestBlock(s) | TxStatus::InFinalizedBlock(s) => return Ok(s),

        // Error scenarios; return the error.
        TxStatus::Error { message } | TxStatus::Invalid { message } | TxStatus::Dropped { message } => {
            // treat error
        }
        // Ignore anything else and wait for next status event:
        _ => continue,
    }
}

Motivated by: #1236

cc @ascjones this might be of interest for contracts

@lexnv lexnv requested a review from a team as a code owner October 31, 2023 12:37
@niklasad1
Copy link
Member

I still find this useful for instance on some scenarios such as the staking miner then one could select "best blocks" instead of "finalized block" but yeah it can be a footgun

@jsdw
Copy link
Collaborator

jsdw commented Oct 31, 2023

I think this is a good way to go; we don't remove the ability to wait for whatever event somebody wants (via the lower level stream of events one can wait for as above), but we take away the method and thus the assumption that wait_for_in_block will Just Work under normal circumstances (which it occasionally won't, especially via the new APIs).

That said, if this did prove contentious I'd also be happy with us just adding a warning to the method that it may fail some times under normal operation.

@lexnv
Copy link
Collaborator Author

lexnv commented Oct 31, 2023

I still find this useful for instance on some scenarios such as the staking miner then one could select "best blocks" instead of "finalized block" but yeah it can be a footgun

Yep, this is still possible to do, although using a lower-level approach.
It might be possible that staking miner runs into a similar issue as detected by the CI, where it waits for the "BestBlock" but that might not (ever) be propagated to chainHead and cause a failure while using the RPCs methods with that block. Also, there's no guarantee that the current "BestBlock" will be finalized, since another "BestBlock { hash: None }" could indicate that the prior reported "BestBlock" is now pruned.

This is in response to #1236 just to keep the stronger guarantees around the RPC v2, waiting for @ascjones to confirm this is ok from their side 🙏

@niklasad1
Copy link
Member

niklasad1 commented Oct 31, 2023

Yep, this is still possible to do, although using a lower-level approach.
It might be possible that staking miner runs into a similar issue as detected by the CI, where it waits for the "BestBlock" but that might not (ever) be propagated to chainHead and cause a failure while using the RPCs methods with that block. Also, there's no guarantee that the current "BestBlock" will be finalized, since another "BestBlock { hash: None }" could indicate that the prior reported "BestBlock" is now pruned.

Yepp, it's dangerous to use but "might be faster" than finalized blocks so in some applications it might be worth the risk (probably not)....

It's probably better for subxt just to support finalized blocks and for anything else one can implement custom logic for specific behavior but it's tricky to write such thing because the actual behavior when a subscription is closed or not. Not really clearly documented, see https://substrate.stackexchange.com/questions/7892/when-will-i-stop-receiving-transactionstatus-updates-for-a-transaction-submitted

@jsdw
Copy link
Collaborator

jsdw commented Nov 2, 2023

It's probably better for subxt just to support finalized blocks and for anything else one can implement custom logic for specific behavior but it's tricky to write such thing because the actual behavior when a subscription is closed or not.

In Subxt, if you use .next() on the TX progress events, rather than eg wait_for_in_block or whatever, then the stream will close itself at the right time and things anyway, so a naive impl doesn't need to worry about anything; it's just got a choice about whether it wants to report on any of the specific errors or just report a general "stream has closed without tx being in block" error :)

@jsdw jsdw merged commit 40aca5b into master Nov 2, 2023
10 checks passed
@jsdw jsdw deleted the lexnv/remove-in-block branch November 2, 2023 17:50
tadeohepperle pushed a commit that referenced this pull request Nov 9, 2023
tadeohepperle added a commit that referenced this pull request Nov 10, 2023
* skeleton commit

* signed extension decoding

* fix some minor things

* make api more similar to Extrinsics

* defer decoding of signed extensions

* fix byte slices

* add test for nonce signed extension

* adjust test and extend for tip

* clippy

* support both  ChargeTransactionPayment and ChargeAssetTxPayment

* address PR comments

* Extend lifetimes, expose pub structs, remove as_type

* add signed extensions to block subscribing example

* add Decoded type

* fix merging bug and tests

* add decoded type in CustomSignedExtension

* fix minor issues, extend test

* cargo fmt differences

* remove the `decoded` function

* new as_signed_extra fn, do not expose as_type anymore

* fix Result-Option order, simplify obtaining Nonce

* tx: Remove `wait_for_in_block` helper method (#1237)

Signed-off-by: Alexandru Vasile <[email protected]>

* Update smoldot to 0.12 (#1212)

* Update lightclient

Signed-off-by: Alexandru Vasile <[email protected]>

* testing: Fix typo

Signed-off-by: Alexandru Vasile <[email protected]>

* testing: Update cargo.toml

Signed-off-by: Alexandru Vasile <[email protected]>

* lightclient: Add tracing logs to improve debugging

Signed-off-by: Alexandru Vasile <[email protected]>

* lightclient: Add socket buffers module for `PlatformRef`

Signed-off-by: Alexandru Vasile <[email protected]>

* lightclient: Update `SubxtPlatform`

Signed-off-by: Alexandru Vasile <[email protected]>

* cargo: Add lightclient dependencies

Signed-off-by: Alexandru Vasile <[email protected]>

* Update cargo.lock of wasm tests

Signed-off-by: Alexandru Vasile <[email protected]>

* lightclient: Add constant for with-buffer module

Signed-off-by: Alexandru Vasile <[email protected]>

* lightclient: Replace rand crate with getrandom

Signed-off-by: Alexandru Vasile <[email protected]>

* example: Update cargo lock file

Signed-off-by: Alexandru Vasile <[email protected]>

* examples: Update deps

Signed-off-by: Alexandru Vasile <[email protected]>

---------

Signed-off-by: Alexandru Vasile <[email protected]>
Co-authored-by: Tadeo Hepperle <[email protected]>

* ChargeAssetTxPayment: support providing u32 or MultiLocation in default impl (#1227)

* Asset Id in Config trait

* add example configuring the config

* fmt

* fix Default trait bound

* merge examples, fix default again

* adjust config in examples

* Update subxt/src/config/mod.rs

Co-authored-by: James Wilson <[email protected]>

---------

Co-authored-by: James Wilson <[email protected]>

* generic AssetId

* fix generics

* fmt

---------

Signed-off-by: Alexandru Vasile <[email protected]>
Co-authored-by: James Wilson <[email protected]>
Co-authored-by: Alexandru Vasile <[email protected]>
@jsdw jsdw mentioned this pull request Dec 7, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants