Script Group Execution
CKB streamlines Lock Script
The process consists of 3 steps:
-
Script Grouping: CKB groups inputs by their Lock Scripts. In this example, two Lock Scripts are used in inputs. Although they point to the same code, they have different args. Let's focus on g1 with inputs indexed 0 and 2. This Script and the input indices will be used in step 3 later.
-
Code Locating: CKB identifies the code from
cell_deps
. It resolves to the Cell with data hashHs
and uses its data as the code. -
Execution: CKB loads the Script's code and execute it, starting from the entry function.
CKB syscalls help Scripts read transaction data. These syscalls usually have an argument to specify where to read the data from. For example, to read the Script itself:
ckb_load_script(addr, len, offset)
To load the first witness:
ckb_load_witness(addr, len, offset, 0, CKB_SOURCE_INPUT);
The first 3 arguments addr
, len
, and offset
control where to store the read data and how many bytes to read. For simplicity, we’ll focus on the other args in the following paragraphs.
0
is the index into the inputs array.
CKB_SOURCE_INPUT
specifies the data source and reads from transaction inputs. It is also used to read witnesses
.
The input indices previously saved during Script Grouping is used to create the virtual witnesses and inputs array for the group. The code can read input or witness using the index in the virtual array via the special source CKB_SOURCE_GROUP_INPUT
. Reading a witness using CKB_SOURCE_GROUP_INPUT
only reads the witnesses that has the same position with the specified input.
All syscalls that read inputs data can use CKB_SOURCE_GROUP_INPUT
and the index in the virtual inputs array, such as ckb_load_cell_*
syscall family.
Running Type Script is similar to running Lock Script, except the following:
- Cells without a Type Script are ignored.
- Type Scripts in both inputs and outputs are used to form Script groups.
Similar to CKB_SOURCE_GROUP_INPUT
, there is a special data source CKB_SOURCE_GROUP_OUTPUT
to use the index into the virtual outputs array in the Script group.