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

fix(rpc): better error handling for transaction submission #2525

Merged
merged 4 commits into from
Apr 28, 2020

Conversation

bowenwang1996
Copy link
Collaborator

@bowenwang1996 bowenwang1996 commented Apr 25, 2020

Add two experimental rpcs:

  • EXPERIMENTAL_broadcast_tx_sync that sends the transaction and waits for its validity to be checked before returning, but doesn't wait for the transaction to be processed.
  • EXPERIMENTAL_check_tx that checks whether a transaction is still valid. For the rpc to work, the node that the request is sent to must track the shard of the sender, otherwise it will return Cannot determine whether the transaction is valid.

Fixes #2039.

Test plan

  • test_check_invalid_tx that does a sanity check on EXPERIMENTAL_check_tx.
  • pytest rpc_tx_submission that checks transactions can be submitted in all three ways and work well. Also check that invalid transactions can be caught by EXPERIMENTAL_check_tx.

@gitpod-io
Copy link

gitpod-io bot commented Apr 25, 2020

Copy link
Collaborator

@SkidanovAlex SkidanovAlex left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

EXPERIMENTAL_broadcast_tx_sync is not a correct name. sync implies that you won't return until the transaction is processed. In other words broadcast_tx_sync and broadcast_tx_commit must be synonyms.

We should either repurpose the existing boardcast_tx_async to do what your new method does, or call it differently (e.g. validate_and_broadcast_tx_async?)

chain/client/src/client.rs Outdated Show resolved Hide resolved
chain/client/src/test_utils.rs Show resolved Hide resolved
chain/jsonrpc/src/lib.rs Outdated Show resolved Hide resolved
Comment on lines 322 to 335
NetworkClientResponses::ValidTx => {
if check_only {
Ok(Value::Null)
} else {
Ok(Value::String(tx_hash))
}
}
NetworkClientResponses::RequestRouted => {
if check_only {
Ok(Value::String("Node doesn't track this shard. Cannot determine whether the transaction is valid".to_string()))
} else {
Ok(Value::String("Transaction is routed".to_string()))
}
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. The return value should be an object (JSON key-value). Consider the RPC client perspective, you will need to guess if the returned string an error or a transaction hash; that is going to feel unreliable. We should always return an object (a structure / JSON key-value) instead of a plain string, this way it is also going to help us to extend the API response without breaking changes in the future (if needed).
  2. This whole check_only looks a bit ugly to me, but I don't have better suggestions at the moment.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you will need to guess if the returned string an error or a transaction hash; that is going to feel unreliable

You don't need to guess. They are two different rpcs

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should always return an object (a structure / JSON key-value) instead of a plain string

What do you suggest here?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

They are two different rpcs

I was referring to the fact that the RPC can either return "some-tx-hash" or "Node does't track this...", so from the type perspective they are both strings. To distinguish them you need to either hardcode the error text messages or try to parse that string as hash (check base58 validity).

Currently, the response is:

{
  "jsonrpc": "2.0",
  "result": "some-tx-hash"
}

or

{
  "jsonrpc": "2.0",
  "result": "Node doesn't track ..."
}

This is really hard to work with, and also it is not extendible without a breaking change.

I suggest to have:

{
  "jsonrpc": "2.0",
  "result": {
    "transaction_hash": "some-tx-hash",
  }
}

and

{
  "jsonrpc": "2.0",
  "error": {
    "code": ...
    "data": "Node doesn't track...",
    "message": "Server error"
  }
}

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, I just noticed that the OK response is null value. Can we provide anything helpful in the response? (e.g. block height, block hash, chunk hash the transaction landed, if that is trivial to get)

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That is for checking the transaction. If you just want to check whether the transaction is valid I don't know what else you want to include in the response.

chain/jsonrpc/src/lib.rs Show resolved Hide resolved
@bowenwang1996
Copy link
Collaborator Author

@SkidanovAlex the naming follows tendermint API https://docs.tendermint.com/master/rpc/#/Tx/broadcast_tx_sync. It waits until the transaction gets into mempool before returning.

Copy link
Collaborator

@SkidanovAlex SkidanovAlex left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please wait for @frol's approval before merging

@frol
Copy link
Collaborator

frol commented Apr 28, 2020

We seem to miss a structured error handling here (as per similar discussion #2518 (comment)) /cc @fckt

@frol frol requested a review from lexfrl April 28, 2020 00:27
@bowenwang1996
Copy link
Collaborator Author

@frol I created a separate issue #2536 for this.

@bowenwang1996 bowenwang1996 merged commit a5c92ee into master Apr 28, 2020
@bowenwang1996 bowenwang1996 deleted the rpc-tx-error branch April 28, 2020 00:55
}
NetworkClientResponses::RequestRouted => {
if check_only {
Err(RpcError::server_error(Some("Node doesn't track this shard. Cannot determine whether the transaction is valid".to_string())))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bowenwang1996
What does this error mean for user?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@fckt Good catch.

We should help users to understand that they are expected to reach another node that tracks the shard.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@frol how to better phrase it? Should we include something like "try another node"?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was thinking the node must be found automatically? (maybe the request should contain a sender_id?) How to use this method?

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.

Invalid transactions dropped from the pool don't have the error
4 participants