Skip to content

Commit

Permalink
docs: expand anchor and checkpoints page
Browse files Browse the repository at this point in the history
  • Loading branch information
thunderbiscuit committed Jan 26, 2024
1 parent 7f4bfad commit f0bf4de
Show file tree
Hide file tree
Showing 3 changed files with 104 additions and 9 deletions.
18 changes: 16 additions & 2 deletions docs/architecture/anchors.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,21 @@

There are a few versions of anchors, but they all have in common that they implement the trait `bdk_chain::Anchor`. For users of `bdk::Wallet`, the most important of these anchors is `bdk_chain::ConfirmationTimeHeightAnchor`.

A `TxGraph` is generic in its Anchor type.
You can think of an anchor as the glue that binds a transaction to a block. If a transaction is anchored in a block that is in the best chain, this transaction must therefore also be in the best chain. If that "anchor block" ever gets reorged and the transaction does not have any other anchors, the transaction becomes unconfirmed (since it's not tied to any particular block anymore!). A Transaction can have multiple anchors.

You can think of an anchor as the glue that binds a transaction to a block. If a transaction is anchored in a block that is in the best chain, this transaction is therefore also in the best chain. If that block ever gets reorged, the transaction becomes unconfirmed. A Transaction can have multiple anchors.
Every Anchor implementation contains a `BlockId` parameter, and must implement the `Ord` trait, meaning that any two anchors can always be compared and ordered relative to each other, facilitating sorting based on their `BlockId` values.

## ConfirmationTimeHeightAnchor

```rust
pub struct ConfirmationTimeHeightAnchor {
/// The anchor block.
pub anchor_block: BlockId,
/// The confirmation height of the chain data being anchored.
pub confirmation_height: u32,
/// The confirmation time of the chain data being anchored.
pub confirmation_time: u64,
}
```

A `TxGraph` is generic in its `Anchor` type.
87 changes: 84 additions & 3 deletions docs/architecture/checkpoint.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,87 @@
# CheckPoints

A `CheckPoint` is a type that contains a block height and hash, and potentially a pointer to a previous `CheckPoint`. For example, printing the CheckPoint for the genesis block of testnet, you get this:
```shell
CheckPoint(CPInner { block: BlockId { height: 0, hash: 0x000000000933ea01ad0ee984209779baaec3ced90fa3f408719526f8d77f4943 }, prev: None })
A `CheckPoint` is a type that contains a `BlockId` (height and hash), and potentially a pointer to a previous `CheckPoint`. For example, printing the CheckPoint for the genesis block of testnet, you get this:

```rust
CheckPoint(
CPInner {
block: BlockId {
height: 0,
hash: 0x000000000933ea01ad0ee984209779baaec3ced90fa3f408719526f8d77f4943
},
prev: None
}
)
```

You can see an example of how checkpoints chain into each other by building a `LocalChain` and printing the `tip` field, which contains its recentmost checkpoint (which in turns points to a previous checkpoint, all the way down to genesis) like so:

```rust
let local_chain = LocalChain::from_blocks(
[
(0, Hash::hash("zero".as_bytes())),
(1, Hash::hash("first".as_bytes())),
(2, Hash::hash("second".as_bytes())),
(3, Hash::hash("third".as_bytes())),
(12, Hash::hash("twelve".as_bytes())),
(17, Hash::hash("seventeen".as_bytes())),
]
.into_iter()
.collect::<BTreeMap<u32, BlockHash>>(),
).unwrap();

println!("Local chain checkpoints: \n{:#?}\n", local_chain.tip());
```

Which will print:
```rust
CheckPoint(
CPInner {
block: BlockId {
height: 17,
hash: 0x8a0ca06e16959dd0d1e814fe3b1b2df6e1e01b7f8a8254d6501f765d7abca794,
},
prev: Some(
CPInner {
block: BlockId {
height: 12,
hash: 0x91a825c5c1eea6886cda4e98dac99d915697c362e19a2920d5a242e9b4fc5922,
},
prev: Some(
CPInner {
block: BlockId {
height: 3,
hash: 0xb3803c0a544bad22bd52594014848a1dbf1a6308b69a4dbbb00306f9d9f3cb96,
},
prev: Some(
CPInner {
block: BlockId {
height: 2,
hash: 0x928411406d12ade8e2d0dfeb43f2d165923595cb68d89561c2ae7fc6b935840b,
},
prev: Some(
CPInner {
block: BlockId {
height: 1,
hash: 0xcf0b7afa0779ec616649ecada0e3711b2acee4e5631289ef615b167cb0ac9f4b,
},
prev: Some(
CPInner {
block: BlockId {
height: 0,
hash: 0x2cf2de24e85d6179e06f842e74accef3bfa8a3fe0e194fafa30045c9a9187c92,
},
prev: None,
},
),
},
),
},
),
},
),
},
),
},
)
```
8 changes: 4 additions & 4 deletions docs/getting-started/getting-started.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,16 @@ cd my_bdk_app
2. Add `bdk` to your `Cargo.toml` file. Find the latest `bdk@1` release on [`crates.io`](https://crates.io/crates/bdk/versions), for example:

```shell
cargo add bdk@1.0.0-alpha.3
cargo add bdk@1.0.0-alpha.4
```

3. Add other required dependencies:

```shell
cargo add bdk_esplora@0.7.0
cargo add bdk_file_store@0.3.0
cargo add bdk_esplora@0.6.0
cargo add bdk_file_store@0.4.0
```

See the [Wallet with Electrum](./book/electrum-wallet.md) page for how to create and sync a wallet.
See the [Wallet with Electrum Example](../book/electrum-wallet.md) page for how to create and sync a wallet.

[Getting Started]: https://www.rust-lang.org/learn/get-started

0 comments on commit f0bf4de

Please sign in to comment.