Skip to content

Commit d901f1a

Browse files
author
Cedric
committed
simplify sudt
1 parent abc8233 commit d901f1a

File tree

5 files changed

+38
-39
lines changed

5 files changed

+38
-39
lines changed

README.md

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,30 +6,30 @@ Cell Script is a newly designed language for smart-contract programming on the U
66

77
Here is an example of a simple cell script.
88
```
9-
//package main
109
import "debug"
1110
import "tx"
1211
import "cell"
1312
1413
// main is the entry point of every cell script
1514
function main() {
1615
tx.scriptVerify()
17-
var ins := tx.inputs()
18-
var outs := tx.outputs()
16+
var ins = tx.inputs()
17+
var outs = tx.outputs()
1918
20-
var in_sum, out_sum uint128
19+
var in_sum uint64
20+
var out_sum uint64
2121
2222
for _, input := range ins {
23-
in_sum += input.data.as(uint128)
24-
if in_sum < input.data.as(uint128) {
23+
in_sum += input
24+
if in_sum < input {
2525
debug.Printf("input overflow")
2626
return 1
2727
}
2828
}
2929
3030
for _, output := range outs {
31-
out_sum += output.data.as(uint128)
32-
if out_sum < input.data.as(uint128) {
31+
out_sum += output
32+
if out_sum < output {
3333
debug.Printf("output overflow")
3434
return 1
3535
}

pkg/cell/cell.cell

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
package cell
22

3-
type Cell string
3+
type Cell uint64

tests/examples/cell-data.cell

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,6 @@ import "debug"
66

77
function main() {
88
var c cell.Cell
9-
c = "1234"
9+
c = 1234
1010
return 0
1111
}

tests/examples/sudt.cell

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,27 @@
1-
//package main
21
import "debug"
32
import "tx"
43
import "cell"
54

65
// main is the entry point of every cell script
76
function main() {
8-
var inputs := tx.inputs()
9-
var outputs := tx.outputs()
7+
tx.scriptVerify()
8+
var ins = tx.inputs()
9+
var outs = tx.outputs()
1010

11-
var in_sum, out_sum uint128
11+
var in_sum uint64
12+
var out_sum uint64
1213

13-
for _, input := range inputs {
14-
in_sum += input.data.as(uint128)
15-
if in_sum < input.data.as(uint128) {
14+
for _, input := range ins {
15+
in_sum += input
16+
if in_sum < input {
1617
debug.Printf("input overflow")
1718
return 1
1819
}
1920
}
2021

21-
for _, output := range outputs {
22-
out_sum += output.data.as(uint128)
23-
if out_sum < input.data.as(uint128) {
22+
for _, output := range outs {
23+
out_sum += output
24+
if out_sum < output {
2425
debug.Printf("output overflow")
2526
return 1
2627
}

third-party/wrapper.c

Lines changed: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,17 @@ const uint32_t MAX_DATA_SIZE = 4 * 1024 * 1024;
1515
#define ERROR_SYSCALL -3
1616
#define ERROR_SCRIPT_TOO_LONG -21
1717

18+
typedef struct {
19+
uint64_t data;
20+
}cell_meta_data;
21+
1822
typedef struct {
1923
uint32_t size;
2024
uint32_t cap;
2125
uint32_t offset;
22-
mol_seg_t* data;
26+
cell_meta_data* ptr;
2327
}cell_data_t;
28+
2429
cell_data_t EMPTY_CELL_DATA = {0};
2530

2631
bool script_verify() {
@@ -86,12 +91,13 @@ uint64_t get_output_cell_data_len(int i) {
8691

8792
cell_data_t get_utxo_inputs() {
8893
cell_data_t inputs = {0};
89-
inputs.data = malloc(MAX_CELLS * sizeof(mol_seg_t));
94+
inputs.ptr = malloc(MAX_CELLS * sizeof(mol_seg_t));
9095
int i = 0;
9196
while (1) {
9297
uint64_t len = get_input_cell_data_len(i);
93-
uint8_t* data = (uint8_t*)malloc(len * sizeof(uint8_t));
94-
int ret = ckb_load_cell_data(data, &len, 0, i,
98+
// uint8_t* data = (uint8_t*)malloc(len * sizeof(uint8_t));
99+
uint64_t cur_data = 0;
100+
int ret = ckb_load_cell_data(&cur_data, &len, 0, i,
95101
CKB_SOURCE_GROUP_INPUT);
96102
// When `CKB_INDEX_OUT_OF_BOUND` is reached, we know we have iterated
97103
// through all cells of current type.
@@ -105,14 +111,10 @@ cell_data_t get_utxo_inputs() {
105111
return EMPTY_CELL_DATA;
106112
}
107113
if (i >= (int)MAX_CELLS) {
108-
for (int j = 0; j <= i; j++) {
109-
free(inputs.data[j].ptr);
110-
}
111-
free(inputs.data);
114+
free(inputs.ptr);
112115
return EMPTY_CELL_DATA;
113116
}
114-
inputs.data[i].size = len;
115-
inputs.data[i].ptr = data;
117+
inputs.ptr[i].data = cur_data;
116118
i += 1;
117119
inputs.size = i;
118120
};
@@ -121,12 +123,12 @@ cell_data_t get_utxo_inputs() {
121123

122124
cell_data_t get_utxo_outputs() {
123125
cell_data_t outputs = {0};
124-
outputs.data = malloc(MAX_CELLS * sizeof(mol_seg_t));
126+
outputs.ptr = malloc(MAX_CELLS * sizeof(mol_seg_t));
125127
int i = 0;
126128
while (1) {
127129
uint64_t len = get_output_cell_data_len(i);
128-
uint8_t* data = (uint8_t*)malloc(len * sizeof(uint8_t));
129-
int ret = ckb_load_cell_data(&data, &len, 0, i,
130+
uint64_t cur_data = 0;
131+
int ret = ckb_load_cell_data(&cur_data, &len, 0, i,
130132
CKB_SOURCE_GROUP_OUTPUT);
131133
// When `CKB_INDEX_OUT_OF_BOUND` is reached, we know we have iterated
132134
// through all cells of current type.
@@ -140,14 +142,10 @@ cell_data_t get_utxo_outputs() {
140142
return EMPTY_CELL_DATA;
141143
}
142144
if (i >= (int)MAX_CELLS) {
143-
for (int j = 0; j <= i; j++) {
144-
free(outputs.data[j].ptr);
145-
}
146-
free(outputs.data);
145+
free(outputs.ptr);
147146
return EMPTY_CELL_DATA;
148147
}
149-
outputs.data[i].size = len;
150-
outputs.data[i].ptr = data;
148+
outputs.ptr[i].data = cur_data;
151149
i += 1;
152150
outputs.size = i;
153151
}

0 commit comments

Comments
 (0)