Skip to content

Commit 3a9f048

Browse files
authored
Merge pull request #28 from KryStar11/master
Update the docs of CKB Bitcoin SPV Type Script
2 parents e298cd0 + 724d043 commit 3a9f048

File tree

1 file changed

+62
-96
lines changed

1 file changed

+62
-96
lines changed
+62-96
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,21 @@
11

2-
# CKB Bitcoin SPV Type Lock
2+
# CKB Bitcoin SPV Type Script
33

4-
A type script for Bitcoin SPV clients which synchronize [Bitcoin] state into [CKB].
4+
A type script designed for Bitcoin SPV clients ensures the valid synchronization of the [Bitcoin] blockchain state into the Nervos [CKB] network.
55

66
## Brief Introduction
77

8-
A [Bitcoin] SPV in [CKB] contains a set of cells, this type script is used
9-
to manage them.
10-
11-
The set is identified by the script [`args`]. The number of live cells with this
12-
specific `args` script is fixed once created, and all cells in the set are
13-
destroyed together.
8+
A Bitcoin  SPV on CKB consists of cells that are managed by the CKB Bitcoin SPV Type Script and identified by the script args. The number of live cells with the script args remains fixed once created, and these cells will be destroyed collectively as a group.
149

1510
### Cells
1611

17-
There are 2 kinds of cells in a Bitcoin SPV instance:
12+
A Bitcoin SPV instance contains two types of cells: **SPV info cell** and **SPV client cell**.
1813

19-
- Client Cell
14+
- **SPV Client Cell**
2015

21-
This cell is used to store the Bitcoin state.
16+
A cell is identified as an SPV client cell if its type script matches the SPV type script.
2217

23-
Each Bitcoin SPV instance should contain at least 3 client cells.
18+
SPV client cells store the Bitcoin state. Each Bitcoin SPV instance includes a minimum of three SPV client cells.
2419

2520
```yaml
2621
Client Cell:
@@ -35,12 +30,10 @@ There are 2 kinds of cells in a Bitcoin SPV instance:
3530
- target adjust info
3631
```
3732
38-
- Info Cell
39-
40-
This cell is used to store the basic information of current Bitcoin SPV
41-
instance. Such as the ID of the tip client cell.
33+
- **SPV Info Cell**
4234
43-
Each Bitcoin SPV instance should contain only 1 info cell.
35+
The SPV info cell stores the basic information of the current Bitcoin SPV instance, such as `tip_client_id`. Each Bitcoin SPV
36+
instance contains only one SPV info cell.
4437

4538
```yaml
4639
Info Cell:
@@ -54,26 +47,16 @@ There are 2 kinds of cells in a Bitcoin SPV instance:
5447

5548
### Operations
5649

57-
There are 4 kinds of operations:
58-
59-
- Create
50+
There are 4 kinds of operations in the Bitcoin SPV type script:
6051

61-
Create all cells for a Bitcoin SPV instance in one transaction.
52+
- **Create**
6253

63-
The outputs of this transaction should contain 1 info cell and at least 1 client cell.
54+
This operation initiates all necessary cells for a Bitcoin SPV instance in a single transaction.
6455

65-
In the follow part of this document, we denoted the number of client cells
66-
as `n`.
56+
The `outputs` include one SPV info cell and at least three SPV client cell. Cells should be consecutive, with the SPV info cell first,
57+
followed by N SPV client cells ordered by their ID from smallest to largest.
6758

68-
In current implementation, it requires that cells must be continuous and
69-
in specified order:
70-
71-
- The client info cell should be at the first.
72-
73-
- Immediately followed by `n` client cells, and these cells should be
74-
ordered by their ID from smallest to largest.
75-
76-
The structure of this kind of transaction is as follows:
59+
Let's denote the number of SPV client cells as `n`. The structure of this transaction is as follows:
7760

7861
```yaml
7962
Cell Deps:
@@ -95,10 +78,9 @@ There are 4 kinds of operations:
9578
- ... ...
9679
```
9780

98-
- Destroy
81+
- **Destroy**
9982

100-
All cells that use the same instance of this type lock should be destroyed
101-
together in one transaction.
83+
Cells within a single Bitcoin SPV instance should be destroyed in one transaction..
10284

10385
The structure of this kind of transaction is as follows:
10486

@@ -123,22 +105,17 @@ There are 4 kinds of operations:
123105
- ... ...
124106
```
125107

126-
- Update
108+
- **Update**
127109

128110
After creation, the `n` client cells should have same data.
129111

130-
The client cell who has the same ID as the `tip_client_id` in the info cell,
131-
we consider that it has the latest data.
132-
133-
The client cell who has the next ID of the `tip_client_id` in the info cell,
134-
we consider that it has the oldest data. These client cells form a ring, where
135-
the next ID after `n-1` is `0`.
136-
137-
Once we update the Bitcoin SPV instance, we put the new data into the client
138-
cell which has the oldest data, and update the `tip_client_id` in the client
139-
info cell to its ID.
112+
The SPV client cell which ID matches the `tip_client_id` of the SPV info cell contains the most recent data, the SPV client cell next
113+
in the sequence after the `tip_client_id` of the info cell holds the oldest data. This sequence arrangement of cells forms a ring
114+
where after the last cell (`ID = n-1`), it wraps around back to the first cell (`ID = 0`).
140115

141-
Do the above step in repetition.
116+
When the Bitcoin SPV instance is updated, the new data will be put into the client cell that currently has the oldest data. Also,
117+
the `tip_client_id` in the SPV info cell will be replaced by the `ID` of the SPV client cell that just received the new data. This SPV
118+
info cell now becomes the new "latest data" holder.
142119

143120
The structure of this kind of transaction is as follows:
144121

@@ -160,18 +137,15 @@ There are 4 kinds of operations:
160137
- ... ...
161138
```
162139

163-
- Reorg
140+
- **Reorg**
164141

165-
When receives blocks from a new longer chain, and there has at least one
166-
client cell whose tip block is the common ancestor block of both the old
167-
chain and the new chain, then a chain reorganization will be required.
142+
When receiving blocks from a new, longer chain, if there is at least one client cell whose tip block is the
143+
common ancestor block of both the old chain and the new chain, a reorg is triggered. The reorg will be based
144+
on this common ancestor, and all client cells with a tip higher than it will be updated accordingly.
168145

169-
**If no common ancestor block was found, then the Bitcoin SPV instance
170-
will be broken, and it requires re-deployment.**
146+
**If no common ancestor block is identified, the Bitcoin SPV instance will fail and require re-deployment.**
171147

172-
let's denote the client ID of the best common ancestor to be `t`.
173-
174-
The structure of this kind of transaction is as follows:
148+
Let's denote the client ID of the best common ancestor as `t`. The structure of this transaction is as follows:
175149

176150
```yaml
177151
Cell Deps:
@@ -203,65 +177,54 @@ index of the output SPV info cell, and the proof should be set in
203177

204178
### Usages
205179

206-
When you want to verify a transaction with Bitcoin SPV Client cell:
180+
To verify a transaction using the Bitcoin SPV Client cell, follow these steps:
207181

208-
- Choose any client cell which contains the block that transaction in.
182+
- Select an SPV client cell that contains the block where the transaction is;
209183

210184
- Create a transaction proof, with the following data:
211185

212-
- The MMR proof of the header which contains this transaction.
186+
- The MMR proof of the block header which contains this transaction;
213187

214-
- The TxOut proof of that transaction.
188+
- The TxOut proof of the transaction;
215189

216-
- The index of that transaction.
190+
- The index of the transaction;
217191

218-
- The height of that header.
192+
- The height of the block header.
219193

220-
- Use [the API `SpvClient::verify_transaction(..)`](https://github.com/ckb-cell/ckb-bitcoin-spv/blob/2464c8f/verifier/src/types/extension/packed.rs#L275-L292) to verify the transaction.
221-
222-
A simple example could be found in [this test](https://github.com/ckb-cell/ckb-bitcoin-spv/blob/2464c8f/prover/src/tests/service.rs#L132-L181).
194+
- Use the [`SpvClient::verify_transaction(..)`](https://github.com/ckb-cell/ckb-bitcoin-spv/blob/2464c8f/verifier/src/types/extension/packed.rs#L275-L292) for the verification. For detailed guidance, please refer to the [API example].
223195

224196
### Limits
225197

226-
- The lower limit of SPV client cells count is 3.
227-
228-
- The upper limit of SPV client cells count is not set, but base on the data type, `u8`,
229-
any number larger than `250` is not recommended.
230-
231-
### Known Issues
232-
233-
- `VM Internal Error: MemWriteOnExecutablePage`
234-
235-
Don't set hash type[^1] to be `Data`.
198+
- The minimum count of SPV client cells is 3;
199+
200+
- The maximum count of SPV client cells is advisable not to exceed `250` given the **`u8`** data type.
236201

237-
`Data1` is introduced from [CKB RFC 0032], and `Data2` is introduced from [CKB RFC 0051].
202+
### Known Issues and Solutions
238203

239-
- Failed to reorg when there is only 1 stale SPV client.
204+
- **Issue #1**: `VM Internal Error: MemWriteOnExecutablePage`
240205

241-
When only 1 SPV client is stale, in the normal case, the reorg transaction
242-
contains 1 input SPV client cell and 1 output SPV client cell is enough.
206+
**Solution**: Don't set hash type[^1] to be `Data`.
243207

244-
But, the structure of this transaction is totally the same as an update
245-
transaction, this leads to ambiguity.
208+
`Data1` is introduced in [CKB RFC 0032], and `Data2` is introduced in [CKB RFC 0051].
246209

247-
So, we define a rule to resolve this case:
210+
- **Issue #2**: Failed to reorg when there is only 1 stale SPV client.
248211

249-
- When there is only 1 SPV client is stale, the reorg transaction has to
250-
rebuild 2 SPV client.
212+
When only one SPV client cell is stale, a typical reorg transaction has the same structure as an update transaction,
213+
consisting of one SPV client cell in the inputs and one SPV client cell in the outputs. However, this similarity can lead to ambiguity.
251214

252-
That means, the reorg transaction for 1 stale SPV client should contain 2
253-
output SPV client cells and 2 output SPV client cells.
215+
**Solution**: To address this issue, the following rule has been set:
254216

255-
Since the reorg rarely happens in Bitcoin mainnet, the cost is acceptable.
256-
257-
- Throw "arithmetic operation overflow" when update a Bitcoin SPV instance for
258-
a Bitcoin dev chain.
259-
260-
The bitcoin dev chain doesn't follow the rule of difficulty change.
261-
262-
So, calculate the next target and partial chain work may lead to an
263-
arithmetic operation overflow.
217+
- In cases where only one SPV client has failed, the reorg transaction must involve the reconstruction of one additional SPV client;
218+
Specifically, the reorg transaction for one stale SPV client should include two SPV client cell in the `inputs` and two SPV client
219+
cells in the `outputs` ;
220+
Considering that reorgs are a rare occurrence on the Bitcoin mainnet, the cost incurred by this approach is considered manageable.
264221

222+
- **Issue #3**: Throw **"Arithmetic Operation Overflow"** when updating a Bitcoin SPV instance for a Bitcoin dev chain.
223+
224+
As the Bitcoin dev chain does not adhere to Bitcoin difficulty adjustment, calculations for the next target and the
225+
partial chain work could result in an arithmetic overflow.
226+
227+
265228
[^1]: [Section "Code Locating"] in "CKB RFC 0022: CKB Transaction Structure".
266229

267230
[Bitcoin]: https://bitcoin.org/
@@ -270,6 +233,9 @@ When you want to verify a transaction with Bitcoin SPV Client cell:
270233
[`args`]: https://github.com/nervosnetwork/rfcs/blob/v2020.01.15/rfcs/0019-data-structures/0019-data-structures.md#description-1
271234
[the field `output_type` of `WitnessArgs`]: https://github.com/nervosnetwork/ckb/blob/v0.114.0/util/gen-types/schemas/blockchain.mol#L117
272235

236+
[API example]: https://github.com/ckb-cell/ckb-bitcoin-spv/blob/2464c8f/prover/src/tests/service.rs#L132-L181
237+
238+
273239
[Section "Code Locating"]: https://github.com/nervosnetwork/rfcs/blob/v2020.01.15/rfcs/0022-transaction-structure/0022-transaction-structure.md#code-locating
274240
[CKB RFC 0032]: https://github.com/nervosnetwork/rfcs/blob/dff5235616e5c7aec706326494dce1c54163c4be/rfcs/0032-ckb-vm-version-selection/0032-ckb-vm-version-selection.md#specification
275241
[CKB RFC 0051]: https://github.com/nervosnetwork/rfcs/blob/dff5235616e5c7aec706326494dce1c54163c4be/rfcs/0051-ckb2023/0051-ckb2023.md#ckb-vm-v2

0 commit comments

Comments
 (0)