Skip to content

Commit 29083cb

Browse files
technicallytyAlex | Interchain Labs
andauthored
docs: unordered txs (#24419)
Co-authored-by: Alex | Interchain Labs <alex@interchainlabs.io>
1 parent 2f2fff2 commit 29083cb

File tree

2 files changed

+59
-10
lines changed

2 files changed

+59
-10
lines changed

docs/docs/learn/advanced/01-transactions.md

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -25,19 +25,14 @@ When users want to interact with an application and make state changes (e.g. sen
2525
Transaction objects are Cosmos SDK types that implement the `Tx` interface
2626

2727
```go reference
28-
https://github.com/cosmos/cosmos-sdk/blob/v0.50.0-alpha.0/types/tx_msg.go#L51-L56
28+
https://github.com/cosmos/cosmos-sdk/blob/v0.53.0-rc.2/types/tx_msg.go#L53-L58
2929
```
3030

3131
It contains the following methods:
3232

3333
* **GetMsgs:** unwraps the transaction and returns a list of contained `sdk.Msg`s - one transaction may have one or multiple messages, which are defined by module developers.
34-
* **ValidateBasic:** lightweight, [_stateless_](../beginner/01-tx-lifecycle.md#types-of-checks) checks used by ABCI messages [`CheckTx`](./00-baseapp.md#checktx) and [`DeliverTx`](./00-baseapp.md#delivertx) to make sure transactions are not invalid. For example, the [`auth`](https://github.com/cosmos/cosmos-sdk/tree/main/x/auth) module's `ValidateBasic` function checks that its transactions are signed by the correct number of signers and that the fees do not exceed what the user's maximum. When [`runTx`](./00-baseapp.md#runtx) is checking a transaction created from the [`auth`](https://github.com/cosmos/cosmos-sdk/tree/main/x/auth/spec) module, it first runs `ValidateBasic` on each message, then runs the `auth` module AnteHandler which calls `ValidateBasic` for the transaction itself.
3534

36-
:::note
37-
This function is different from the deprecated `sdk.Msg` [`ValidateBasic`](../beginner/01-tx-lifecycle.md#ValidateBasic) methods, which was performing basic validity checks on messages only.
38-
:::
39-
40-
As a developer, you should rarely manipulate `Tx` directly, as `Tx` is really an intermediate type used for transaction generation. Instead, developers should prefer the `TxBuilder` interface, which you can learn more about [below](#transaction-generation).
35+
As a developer, you should rarely manipulate `Tx` directly, as `Tx` is an intermediate type used for transaction generation. Instead, developers should prefer the `TxBuilder` interface, which you can learn more about [below](#transaction-generation).
4136

4237
### Signing Transactions
4338

@@ -133,17 +128,19 @@ While messages contain the information for state transition logic, a transaction
133128

134129
### Transaction Generation
135130

136-
The `TxBuilder` interface contains data closely related with the generation of transactions, which an end-user can freely set to generate the desired transaction:
131+
The `TxBuilder` interface contains data closely related with the generation of transactions, which an end-user can set to generate the desired transaction:
137132

138133
```go reference
139-
https://github.com/cosmos/cosmos-sdk/blob/v0.50.0-alpha.0/client/tx_config.go#L40-L53
134+
https://github.com/cosmos/cosmos-sdk/blob/v0.53.0-rc.2/client/tx_config.go#L39-L57
140135
```
141136

142137
* `Msg`s, the array of [messages](#messages) included in the transaction.
143138
* `GasLimit`, option chosen by the users for how to calculate how much gas they will need to pay.
144139
* `Memo`, a note or comment to send with the transaction.
145140
* `FeeAmount`, the maximum amount the user is willing to pay in fees.
146141
* `TimeoutHeight`, block height until which the transaction is valid.
142+
* `Unordered`, an option indicating this transaction may be executed in any order (requires TimeoutTimestamp to be set)
143+
* `TimeoutTimestamp`, the timeout timestamp (unordered nonce) of the transaction (required to be used with Unordered).
147144
* `Signatures`, the array of signatures from all signers of the transaction.
148145

149146
As there are currently two sign modes for signing transactions, there are also two implementations of `TxBuilder`:
@@ -154,7 +151,7 @@ As there are currently two sign modes for signing transactions, there are also t
154151
However, the two implementations of `TxBuilder` should be hidden away from end-users, as they should prefer using the overarching `TxConfig` interface:
155152

156153
```go reference
157-
https://github.com/cosmos/cosmos-sdk/blob/v0.50.0-alpha.0/client/tx_config.go#L24-L34
154+
https://github.com/cosmos/cosmos-sdk/blob/v0.53.0-rc.2/client/tx_config.go#L27-L37
158155
```
159156

160157
`TxConfig` is an app-wide configuration for managing transactions. Most importantly, it holds the information about whether to sign each transaction with `SIGN_MODE_DIRECT` or `SIGN_MODE_LEGACY_AMINO_JSON`. By calling `txBuilder := txConfig.NewTxBuilder()`, a new `TxBuilder` will be created with the appropriate sign mode.
@@ -204,3 +201,21 @@ An example can be seen [here](../../user/run-node/03-txs.md#using-rest)
204201
#### CometBFT RPC
205202

206203
The three methods presented above are actually higher abstractions over the CometBFT RPC `/broadcast_tx_{async,sync,commit}` endpoints, documented [here](https://docs.cometbft.com/v0.37/core/rpc). This means that you can use the CometBFT RPC endpoints directly to broadcast the transaction, if you wish so.
204+
205+
### Unordered Transactions
206+
207+
:::tip
208+
209+
Looking to enable unordered transactions on your chain?
210+
Check out the [v0.53.0 Upgrade Guide](https://docs.cosmos.network/v0.53/build/migrations/upgrade-guide#enable-unordered-transactions-optional)
211+
212+
:::
213+
214+
Beginning with Cosmos SDK v0.53.0, chains may enable unordered transaction support.
215+
Unordered transactions work by using a timestamp as the transaction's nonce value.
216+
The timestamp must be greater than the current block time and not exceed the chain's configured max unordered timeout timestamp duration.
217+
Senders must use a unique timestamp for each distinct transaction. The difference may be as small as a nanosecond, however.
218+
219+
These unique timestamps serve as a one-shot nonce, and their lifespan in state is short-lived.
220+
Upon transaction inclusion, an entry consisting of timeout timestamp and account address will be recorded to state.
221+
Once the block time is passed the timeout timestamp value, the entry will be removed. This ensures that unordered nonces do not indefinitely fill up the chain's storage.

docs/docs/user/run-node/03-txs.md

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,40 @@ func sendTx() error {
179179

180180
At this point, `TxBuilder`'s underlying transaction is ready to be signed.
181181

182+
#### Generating an Unordered Transaction
183+
184+
Starting with Cosmos SDK v0.53.0, users may send unordered transactions to chains that have the feature enabled.
185+
186+
Using the example above, we can set the required fields to mark a transaction as unordered.
187+
By default, unordered transactions charge an extra 2240 units of gas to offset the additional storage overhead that supports their functionality.
188+
The extra units of gas are customizable and therefore vary by chain, so be sure to check the chain's ante handler for the gas value set, if any.
189+
190+
```go
191+
func sendTx() error {
192+
// --snip--
193+
expiration := 5 * time.Minute
194+
txBuilder.SetUnordered(true)
195+
txBuilder.SetTimeoutTimestamp(time.Now().Add(expiration + (1 * time.Nanosecond)))
196+
}
197+
```
198+
199+
Unordered transactions from the same account must use a unique timeout timestamp value. The difference between each timeout timestamp value may be as small as a nanosecond, however.
200+
201+
```go
202+
import (
203+
"github.com/cosmos/cosmos-sdk/client"
204+
)
205+
206+
func sendMessages(txBuilders []client.TxBuilder) error {
207+
// --snip--
208+
expiration := 5 * time.Minute
209+
for _, txb := range txBuilders {
210+
txb.SetUnordered(true)
211+
txb.SetTimeoutTimestamp(time.Now().Add(expiration + (1 * time.Nanosecond)))
212+
}
213+
}
214+
```
215+
182216
### Signing a Transaction
183217

184218
We set encoding config to use Protobuf, which will use `SIGN_MODE_DIRECT` by default. As per [ADR-020](https://github.com/cosmos/cosmos-sdk/blob/main/docs/architecture/adr-020-protobuf-transaction-encoding.md), each signer needs to sign the `SignerInfo`s of all other signers. This means that we need to perform two steps sequentially:

0 commit comments

Comments
 (0)