Browse Source

feat: draft of microblock docs

feat: add api, states, and nonce details
fix/microblocks-feedback
Alexander Graebe 4 years ago
committed by Patrick Gray
parent
commit
3890c5323d
  1. BIN
      public/images/microblock-states.png
  2. 12
      src/pages/understand-stacks/mining.md
  3. 4
      src/pages/understand-stacks/network.md
  4. 101
      src/pages/understand-stacks/stacks-blockchain-api.md
  5. 1
      src/pages/understand-stacks/technical-specs.md
  6. 83
      src/pages/understand-stacks/transactions.md

BIN
public/images/microblock-states.png

Binary file not shown.

After

Width:  |  Height:  |  Size: 271 KiB

12
src/pages/understand-stacks/mining.md

@ -62,3 +62,15 @@ To calculate the amount of BTC to send miners should:
- Guess the price BTC/STX for the next day (100 blocks later) - Guess the price BTC/STX for the next day (100 blocks later)
- Guess the total amount of BTCs committed by all miners - Guess the total amount of BTCs committed by all miners
## Microblocks
Unlike most existing blockchains, Stacks blocks are not produced atomically. Instead, leaders may dynamically package transactions into a sequence of microblocks as they are received from users.
A leader can "stream" a block over the course of its tenure by selecting transactions from the mempool as they arrive and packaging them into microblocks. These microblocks contain small batches of transactions, which are organized into a hash chain to encode the order in which they were processed. If a leader produces microblocks, then the new chain tip the next leader builds off of will be the last microblock the new leader has seen.
The advantage of the streaming approach is that a leader's transaction can be included in a block during the current epoch, reducing latency.
Whether a transactions should be included in a microbloc can be [defined in the transaction encoding](/understand-stacks/transactions#anchor-mode).
> More details can be found in [SIP001](https://github.com/stacksgov/sips/blob/main/sips/sip-001/sip-001-burn-election.md).

4
src/pages/understand-stacks/network.md

@ -48,12 +48,16 @@ Nonces are added to all transactions and help identify them in order to ensure t
When a new [token transfer transaction](/understand-stacks/transactions#stacks-token-transfer) is constructed, the most recent nonce of the account needs to fetched and set. When a new [token transfer transaction](/understand-stacks/transactions#stacks-token-transfer) is constructed, the most recent nonce of the account needs to fetched and set.
> The API provides an endpoint to [simplify nonce handling](/understand-stacks/stacks-blockchain-api#nonce-handling).
## Confirmations ## Confirmations
The Stacks 2.0 network is anchored onto the bitcoin network. This allows transactions on Stacks to inherit the same finality and security of the Bitcoin blockchain. The Stacks 2.0 network is anchored onto the bitcoin network. This allows transactions on Stacks to inherit the same finality and security of the Bitcoin blockchain.
The time to mine a block, to confirm transactions, will eventually match the expected "block time" of the bitcoin network: 10 minutes. The time to mine a block, to confirm transactions, will eventually match the expected "block time" of the bitcoin network: 10 minutes.
> Transactions can also be mined in microblocks, reducing the latency significantly. Learn more [here](/understand-stacks/transactions#anchor-mode).
The block time is hardcoded and will change throughout the implementation phases of the [testnet](/understand-stacks/testnet). The current block time can be obtained through the [`GET /extended/v1/info/network_block_times`](https://blockstack.github.io/stacks-blockchain-api/#operation/get_network_block_times) endpoint: The block time is hardcoded and will change throughout the implementation phases of the [testnet](/understand-stacks/testnet). The current block time can be obtained through the [`GET /extended/v1/info/network_block_times`](https://blockstack.github.io/stacks-blockchain-api/#operation/get_network_block_times) endpoint:
```bash ```bash

101
src/pages/understand-stacks/stacks-blockchain-api.md

@ -169,23 +169,31 @@ Here is a sample response:
"limit": 10, "limit": 10,
"offset": 0, "offset": 0,
"total": 101922, "total": 101922,
"results": [ "results": [{
{ "tx_id": "0x924e0a688664851f5f96b437fabaec19b7542cfcaaf92a97eae43384cacd83d0",
"tx_id": "0x5e9f3933e358df6a73fec0d47ce3e1062c20812c129f5294e6f37a8d27c051d9", "nonce": 308,
"tx_status": "success", "fee_rate": "0",
"tx_type": "coinbase", "sender_address": "ST39F7SA0AKH7RB363W3NE2DTHD3P32ZHNX2KE7J9",
"fee_rate": "0", "sponsored": false,
"sender_address": "ST3WCQ6S0DFT7YHF53M8JPKGDS1N1GSSR91677XF1", "post_condition_mode": "deny",
"sponsored": false, "post_conditions": [],
"post_condition_mode": "deny", "anchor_mode": "on_chain_only",
"block_hash": "0x58412b50266debd0c35b1a20348ad9c0f17e5525fb155a97033256c83c9e2491", "block_hash": "0x17ceb3da5f36aab351d6b14f5aa77f85bb6b800b954b2f24c564579f80116d99",
"block_height": 3231, "parent_block_hash": "0xe0d1e8d216a77526ae2ce40294fc77038798a179a6532bb8980d3c2183f58de6",
"burn_block_time": 1594230455, "block_height": 14461,
"canonical": true, "burn_block_time": 1622875042,
"tx_index": 0, "burn_block_time_iso": "2021-06-05T06:37:22.000Z",
"coinbase_payload": { "canonical": true,
"data": "0x0000000000000000000000000000000000000000000000000000000000000000" "tx_index": 0,
} "tx_status": "success",
"tx_result": { ... },
"microblock_hash": "",
"microblock_sequence": 2147483647,
"microblock_canonical": true,
"event_count": 0,
"events": [],
"tx_type": "coinbase",
"coinbase_payload": { ... }
}, },
{ ... } { ... }
] ]
@ -290,6 +298,65 @@ This API supports [v1.4.6 of the Rosetta specification](https://www.rosetta-api.
-> Find all Data and Construction Rosetta endpoints [here](https://blockstack.github.io/stacks-blockchain-api/#tag/Rosetta) -> Find all Data and Construction Rosetta endpoints [here](https://blockstack.github.io/stacks-blockchain-api/#tag/Rosetta)
## Microblocks support
!> API support for [microblocks](/understand-stacks/mining#microblocks) is current being worked on. New API endpoints are documented [here](https://stacks-blockchain-api-git-feat-microblocks-blockstack.vercel.app/#tag/Microblocks). The details are subject to change for now.
The [transaction anchor mode](/understand-stacks/transactions#anchor-mode) defines if a transaction should be included in a microblock. If so, the latency for a transaction to be confirmed can be reduced.
The API allows querying the most recently streamed microblocks:
```bash
# for mainnet, remove `.testnet`
curl 'https://stacks-node-api-microblocks.testnet.stacks.co/extended/v1/microblock'
```
```json
{
"limit": 20,
"offset": 0,
"total": 8766,
"results": [
{
"canonical": true,
"microblock_canonical": true,
"microblock_hash": "0xe6897aab881208185e3fb6ba58d9d9e35c43c68f13fbb892b20cebd39ac69567",
"microblock_sequence": 0,
"microblock_parent_hash": "0xe0d1e8d216a77526ae2ce40294fc77038798a179a6532bb8980d3c2183f58de6",
"parent_index_block_hash": "0x178cd9a37bf38f6b85d9f18e65588e60782753b1463ae080fb9865938b0898ea",
"block_height": 14461,
"parent_block_height": 14460,
"parent_block_hash": "0xe0d1e8d216a77526ae2ce40294fc77038798a179a6532bb8980d3c2183f58de6",
"block_hash": "0x17ceb3da5f36aab351d6b14f5aa77f85bb6b800b954b2f24c564579f80116d99",
"txs": ["0x0622e096dec7e2f6e8f7d95f732e04d238b7381aea8d0aecffae026c53e73e05"]
}
]
}
```
Microblocks are a trdeoff between finality and latency. The initial confirmation of a transaction inside a microblock does not imply finality. Microblocks are subject to reorganization. Thus, the order of processing for confirmed transactions can change. Only once the microblock was added to an anchor block, finality can be assumed.
## Nonce handling
Developer have to keep track of the last nonce used in a transaction. The API provides an endpoint to make nonce handling simpler:
```bash
# for mainnet, remove `.testnet`
# replace <principal> with your STX address
curl 'https://stacks-node-api-microblocks.testnet.stacks.co/extended/v1/address/<principal>/nonces'
```
```json
{
"last_executed_tx_nonce": 5893,
"last_mempool_tx_nonce": null,
"possible_next_nonce": 5894,
"detected_missing_nonces": []
}
```
You can use the `possible_next_nonce` property as the nonce for your next transaction.
## Running an API server ## Running an API server
While Hiro provides a hosted API server of the Stacks Blockchain API, anyone can spin up their own version. Please follow the instructions in this guide to start a Docker container with the API service running: While Hiro provides a hosted API server of the Stacks Blockchain API, anyone can spin up their own version. Please follow the instructions in this guide to start a Docker container with the API service running:

1
src/pages/understand-stacks/technical-specs.md

@ -61,6 +61,7 @@ description: Summary of technical specifications of Stacks 2.0
1. _originating account_ is the account that creates, _authorizes_ and sends the transaction 1. _originating account_ is the account that creates, _authorizes_ and sends the transaction
2. _paying account_ is the account that is billed by the leader for the cost of validating and executing the transaction 2. _paying account_ is the account that is billed by the leader for the cost of validating and executing the transaction
3. _sending account_ is the account that identifies who is currently executing the transaction: this can change as a transaction executes via the `as-contract` Clarity function 3. _sending account_ is the account that identifies who is currently executing the transaction: this can change as a transaction executes via the `as-contract` Clarity function
- Transactions can be batched or streamed into blocks. The behavior can be controlled by the anchor mode of a transaction. With streaming (microblocks), a faster confirmation time is possible.
- Two types of authorizations: standard authorization is where originating account is the same as paying account. _Sponsored_ authorization is where originating account and paying account are distinct. For instance, developers or service providers could pay for users to call their smart-contracts. - Two types of authorizations: standard authorization is where originating account is the same as paying account. _Sponsored_ authorization is where originating account and paying account are distinct. For instance, developers or service providers could pay for users to call their smart-contracts.
- For sponsored authorization, first a user signs with the originating account and then a sponsor signs with the paying account. - For sponsored authorization, first a user signs with the originating account and then a sponsor signs with the paying account.
- Mempool limit for concurrent pending transactions is 25 per account - Mempool limit for concurrent pending transactions is 25 per account

83
src/pages/understand-stacks/transactions.md

@ -51,6 +51,39 @@ A sample of each transaction type can be found in the [Stacks Blockchain API res
~> Read-only contract call calls do **not** require transactions. Read more about it in the [network guide](/understand-stacks/network#read-only-function-calls). ~> Read-only contract call calls do **not** require transactions. Read more about it in the [network guide](/understand-stacks/network#read-only-function-calls).
## Anchor mode
Transactions can be mined either in an anchor block or in a microblock. If microblock-anchoring is selected, the transaction can be confirmed much quicker that the target block time. However, microblocks can be subject to re-organization, thus changing the order of transaction processing:
![microblock-states](/images/microblock-states.png)
If your transaction relies on a state that could be altered by a previous transactions with serious implications, you should carefully evalute if microblock-anchoring should be set.
The anchor mode has three options:
- `OnChainOnly` The transaction MUST be included in an anchored block
- `OffChainOnly`: The transaction MUST be included in a microblock
- `Any`: The leader can choose where to include the transaction
Here is an example where the transaction must be included in a microblock:
```js
import { AnchorMode, makeSTXTokenTransfer } from '@stacks/transactions';
import { StacksTestnet, StacksMainnet } from '@stacks/network';
const BigNum = require('bn.js');
const txOptions = {
recipient: 'SP3FGQ8Z7JY9BWYZ5WM53E0M9NK7WHJF0691NZ159',
amount: new BigNum(12345),
senderKey: 'b244296d5907de9864c0b0d51f98a13c52890be0404e83f273144cd5b9960eed01',
network: new StacksTestnet(), // for mainnet, use `StacksMainnet()`
anchorMode: AnchorMode.OffChainOnly, // must be included in a microblock
};
const transaction = await makeSTXTokenTransfer(txOptions);
```
## Post-conditions ## Post-conditions
Transaction post-conditions are a feature meant to limit the damage malicious smart contract developers and smart contract bugs can do in terms of destroying a user's assets. Post-conditions are executed whenever a contract is instantiated or a public method of an existing contract is executed. Whenever a post-condition fails, a transaction will be forced to abort. Transaction post-conditions are a feature meant to limit the damage malicious smart contract developers and smart contract bugs can do in terms of destroying a user's assets. Post-conditions are executed whenever a contract is instantiated or a public method of an existing contract is executed. Whenever a post-condition fails, a transaction will be forced to abort.
@ -304,35 +337,37 @@ When called the Stacks Blockchain API or Node RPC API, transactions returned wil
```js ```js
{ {
"tx_id": "0x19e25515652dad41ef675bd0670964e3d537b80ec19cf6ca6f1dd65d5bc642c6", "tx_id": "0x77cb1bf0804f09ad24b4c494a6c00d5b10bb0afbb94a0d646fa9640eff338e37",
"tx_status": "success", "nonce": 5893,
"tx_type": "token_transfer",
"fee_rate": "180", "fee_rate": "180",
"sender_address": "STJTXEJPJPPVDNA9B052NSRRBGQCFNKVS178VGH1", "sender_address": "STB44HYPYAT2BB2QE513NSP81HTMYWBJP02HPGK6",
"sponsored": false, "sponsored": false,
"post_condition_mode": "deny", "post_condition_mode": "deny",
"block_hash": "0x9080f6df3e0be0d6de67569330e547346a44c8ecd30d9d76b5edd1b49e2c22f6", "post_conditions": [],
"block_height": 3190, "anchor_mode": "any",
"burn_block_time": 1594227992, "block_hash": "0xf1e54a3acd04232f1362c09d5096b095363158348303396ea5fc5092e1d8788f",
"parent_block_hash": "0x3de356eb5afa5d7b781f6a925d31d69d218b772ec995930b4e15d92bd15443f9",
"block_height": 13984,
"burn_block_time": 1622678407,
"burn_block_time_iso": "2021-06-03T00:00:07.000Z",
"canonical": true, "canonical": true,
"tx_index": 1, "tx_index": 2,
"token_transfer": { "tx_status": "success",
"recipient_address": "ST1RZG804V6Y0N4XHQD3ZE2GE3XSCV3VHRKMA3GB0", "tx_result": {
"amount": "10000", "hex": "0x0703",
"memo": "0x00000000000000000000000000000000000000000000000000000000000000000000" "repr": "(ok true)"
}, },
"events": [ "microblock_hash": "",
{ "microblock_sequence": 2147483647,
"event_index": 0, "microblock_canonical": true,
"event_type": "stx_asset", "event_count": 1,
"asset": { "events": [],
"asset_event_type": "transfer", "tx_type": "token_transfer",
"sender": "STJTXEJPJPPVDNA9B052NSRRBGQCFNKVS178VGH1", "token_transfer": {
"recipient": "ST1RZG804V6Y0N4XHQD3ZE2GE3XSCV3VHRKMA3GB0", "recipient_address": "STZ4C5RT4WH4JGRQA5E0ZF5PPSQCVY1WRB6E2CGW",
"amount": "10000" "amount": "500000000",
} "memo": "0x46617563657400000000000000000000000000000000000000000000000000000000"
} }
]
} }
``` ```

Loading…
Cancel
Save