Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rename XudtWitnessInput #81

Merged
merged 4 commits into from
Nov 19, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions c/rce.h
Original file line number Diff line number Diff line change
Expand Up @@ -158,8 +158,8 @@ static int rce_get_proofs(uint32_t index, SmtProofEntryVecType* res) {
CHECK2(!input.t->is_none(&input), ERROR_INVALID_MOL_FORMAT);

mol2_cursor_t bytes = input.t->unwrap(&input);
// convert Bytes to XudtWitnessInputType
XudtWitnessInputType witness_input = make_XudtWitnessInput(&bytes);
// convert Bytes to XudtWitnessType
XudtWitnessType witness_input = make_XudtWitness(&bytes);
BytesVecType extension_data_vec =
witness_input.t->extension_data(&witness_input);

Expand Down
34 changes: 17 additions & 17 deletions c/xudt_rce.c
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ int ckb_exit(signed char);
typedef unsigned __int128 uint128_t;

uint8_t g_script[SCRIPT_SIZE] = {0};
uint8_t g_raw_extension_data[RAW_EXTENSION_SIZE] = {0};
uint8_t g_extension_scripts[RAW_EXTENSION_SIZE] = {0};
WitnessArgsType g_witness_args;

uint8_t g_code_buff[MAX_CODE_SIZE] __attribute__((aligned(RISCV_PGSIZE)));
Expand Down Expand Up @@ -209,8 +209,8 @@ int get_extension_data(uint32_t index, uint8_t *buff, uint32_t buff_len,
CHECK2(!input.t->is_none(&input), ERROR_INVALID_MOL_FORMAT);

mol2_cursor_t bytes = input.t->unwrap(&input);
// convert Bytes to XudtWitnessInputType
XudtWitnessInputType witness_input = make_XudtWitnessInput(&bytes);
// convert Bytes to XudtWitnessType
XudtWitnessType witness_input = make_XudtWitness(&bytes);
BytesVecType extension_data_vec =
witness_input.t->extension_data(&witness_input);

Expand Down Expand Up @@ -239,8 +239,8 @@ int get_owner_script(uint8_t *buff, uint32_t buff_len, uint32_t *out_len) {
CHECK2(!input.t->is_none(&input), ERROR_INVALID_MOL_FORMAT);

mol2_cursor_t bytes = input.t->unwrap(&input);
// convert Bytes to XudtWitnessInputType
XudtWitnessInputType witness_input = make_XudtWitnessInput(&bytes);
// convert Bytes to XudtWitnessType
XudtWitnessType witness_input = make_XudtWitness(&bytes);
ScriptOptType owner_script = witness_input.t->owner_script(&witness_input);
CHECK2(!owner_script.t->is_none(&owner_script), ERROR_INVALID_MOL_FORMAT);
ScriptType owner_script2 = owner_script.t->unwrap(&owner_script);
Expand All @@ -253,7 +253,7 @@ int get_owner_script(uint8_t *buff, uint32_t buff_len, uint32_t *out_len) {
}

// the *var_len may be bigger than real length of raw extension data
int load_raw_extension_data(uint8_t **var_data, uint32_t *var_len) {
int load_extension_scripts(uint8_t **var_data, uint32_t *var_len) {
int err = 0;
bool use_input_type = true;
err = make_cursor_from_witness(&g_witness_args, &use_input_type);
Expand All @@ -269,16 +269,16 @@ int load_raw_extension_data(uint8_t **var_data, uint32_t *var_len) {
CHECK2(!input.t->is_none(&input), ERROR_INVALID_MOL_FORMAT);

struct mol2_cursor_t bytes = input.t->unwrap(&input);
// convert Bytes to XudtWitnessInputType
XudtWitnessInputType witness_input = make_XudtWitnessInput(&bytes);
// convert Bytes to XudtWitnessType
XudtWitnessType witness_input = make_XudtWitness(&bytes);
ScriptVecOptType script_vec =
witness_input.t->raw_extension_data(&witness_input);
witness_input.t->extension_scripts(&witness_input);

uint32_t read_len =
mol2_read_at(&script_vec.cur, g_raw_extension_data, RAW_EXTENSION_SIZE);
mol2_read_at(&script_vec.cur, g_extension_scripts, RAW_EXTENSION_SIZE);
CHECK2(read_len == script_vec.cur.size, ERROR_INVALID_MOL_FORMAT);

*var_data = g_raw_extension_data;
*var_data = g_extension_scripts;
*var_len = read_len;

err = 0;
Expand Down Expand Up @@ -379,7 +379,7 @@ int check_enhanced_owner_mode(int *owner_mode) {
}

// *var_data will point to "Raw Extension Data", which can be in args or witness
// *var_data will refer to a memory location of g_script or g_raw_extension_data
// *var_data will refer to a memory location of g_script or g_extension_scripts
int parse_args(int *owner_mode, XUDTFlags *flags, uint8_t **var_data,
uint32_t *var_len, uint8_t *hashes, uint32_t *hashes_count) {
int err = 0;
Expand Down Expand Up @@ -484,7 +484,7 @@ int parse_args(int *owner_mode, XUDTFlags *flags, uint8_t **var_data,
args_bytes_seg.size - BLAKE2B_BLOCK_SIZE - FLAGS_SIZE;
CHECK2(hash_size == BLAKE160_SIZE, ERROR_INVALID_FLAG);

err = load_raw_extension_data(var_data, var_len);
err = load_extension_scripts(var_data, var_len);
CHECK(err);
CHECK2(var_len > 0, ERROR_INVALID_MOL_FORMAT);
// verify the hash
Expand Down Expand Up @@ -633,13 +633,13 @@ int main() {
#endif
int err = 0;
int owner_mode = 0;
uint8_t *raw_extension_data = NULL;
uint8_t *extension_scripts = NULL;
uint32_t raw_extension_len = 0;
XuJiandong marked this conversation as resolved.
Show resolved Hide resolved
XUDTFlags flags = XUDTFlagsPlain;
uint8_t
input_lock_script_hashes[MAX_LOCK_SCRIPT_HASH_COUNT * BLAKE2B_BLOCK_SIZE];
uint32_t input_lock_script_hash_count = 0;
err = parse_args(&owner_mode, &flags, &raw_extension_data, &raw_extension_len,
err = parse_args(&owner_mode, &flags, &extension_scripts, &raw_extension_len,
input_lock_script_hashes, &input_lock_script_hash_count);
CHECK(err);
CHECK2(owner_mode == 1 || owner_mode == 0, ERROR_INVALID_ARGS_FORMAT);
Expand All @@ -651,7 +651,7 @@ int main() {
}

if (flags != XUDTFlagsPlain) {
CHECK2(raw_extension_data != NULL, ERROR_INVALID_ARGS_FORMAT);
CHECK2(extension_scripts != NULL, ERROR_INVALID_ARGS_FORMAT);
CHECK2(raw_extension_len > 0, ERROR_INVALID_ARGS_FORMAT);
}
err = simple_udt(owner_mode);
Expand All @@ -665,7 +665,7 @@ int main() {
}

mol_seg_t raw_extension_seg = {0};
raw_extension_seg.ptr = raw_extension_data;
raw_extension_seg.ptr = extension_scripts;
raw_extension_seg.size = raw_extension_len;
CHECK2(MolReader_ScriptVec_verify(&raw_extension_seg, true) == MOL_OK,
ERROR_INVALID_ARGS_FORMAT);
Expand Down
6 changes: 3 additions & 3 deletions c/xudt_rce.mol
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ import blockchain;
vector ScriptVec <Script>;
option ScriptVecOpt (ScriptVec);

table XudtWitnessInput {
table XudtWitness {
owner_script: ScriptOpt,
owner_signature: BytesOpt,
raw_extension_data: ScriptVecOpt,
extension_scripts: ScriptVecOpt,
extension_data: BytesVec,
}

Expand All @@ -25,7 +25,7 @@ union RCData {
/* To support multiple RCRules, need to store multiple proofs in every item
in "Bytes structure" in witness.

Which means, one item in "structure" XudtWitnessInput might be SmtProofVec
Which means, one item in "structure" XudtWitness might be SmtProofVec
*/
vector SmtProof <byte>;

Expand Down
34 changes: 17 additions & 17 deletions c/xudt_rce_mol.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,13 @@ MOLECULE_API_DECORATOR mol_errno MolReader_ScriptVec_verify
#define MolReader_ScriptVec_get(s, i) mol_dynvec_slice_by_index(s, i)
MOLECULE_API_DECORATOR mol_errno MolReader_ScriptVecOpt_verify (const mol_seg_t*, bool);
#define MolReader_ScriptVecOpt_is_none(s) mol_option_is_none(s)
MOLECULE_API_DECORATOR mol_errno MolReader_XudtWitnessInput_verify (const mol_seg_t*, bool);
#define MolReader_XudtWitnessInput_actual_field_count(s) mol_table_actual_field_count(s)
#define MolReader_XudtWitnessInput_has_extra_fields(s) mol_table_has_extra_fields(s, 4)
#define MolReader_XudtWitnessInput_get_owner_script(s) mol_table_slice_by_index(s, 0)
#define MolReader_XudtWitnessInput_get_owner_signature(s) mol_table_slice_by_index(s, 1)
#define MolReader_XudtWitnessInput_get_raw_extension_data(s) mol_table_slice_by_index(s, 2)
#define MolReader_XudtWitnessInput_get_extension_data(s) mol_table_slice_by_index(s, 3)
MOLECULE_API_DECORATOR mol_errno MolReader_XudtWitness_verify (const mol_seg_t*, bool);
#define MolReader_XudtWitness_actual_field_count(s) mol_table_actual_field_count(s)
#define MolReader_XudtWitness_has_extra_fields(s) mol_table_has_extra_fields(s, 4)
#define MolReader_XudtWitness_get_owner_script(s) mol_table_slice_by_index(s, 0)
#define MolReader_XudtWitness_get_owner_signature(s) mol_table_slice_by_index(s, 1)
#define MolReader_XudtWitness_get_extension_scripts(s) mol_table_slice_by_index(s, 2)
#define MolReader_XudtWitness_get_extension_data(s) mol_table_slice_by_index(s, 3)
#define MolReader_RCRule_verify(s, c) mol_verify_fixed_size(s, 33)
#define MolReader_RCRule_get_smt_root(s) mol_slice_by_offset(s, 0, 32)
#define MolReader_RCRule_get_flags(s) mol_slice_by_offset(s, 32, 1)
Expand Down Expand Up @@ -85,13 +85,13 @@ MOLECULE_API_DECORATOR mol_errno MolReader_XudtData_verify
#define MolBuilder_ScriptVecOpt_set(b, p, l) mol_option_builder_set(b, p, l)
#define MolBuilder_ScriptVecOpt_build(b) mol_builder_finalize_simple(b)
#define MolBuilder_ScriptVecOpt_clear(b) mol_builder_discard(b)
#define MolBuilder_XudtWitnessInput_init(b) mol_table_builder_initialize(b, 128, 4)
#define MolBuilder_XudtWitnessInput_set_owner_script(b, p, l) mol_table_builder_add(b, 0, p, l)
#define MolBuilder_XudtWitnessInput_set_owner_signature(b, p, l) mol_table_builder_add(b, 1, p, l)
#define MolBuilder_XudtWitnessInput_set_raw_extension_data(b, p, l) mol_table_builder_add(b, 2, p, l)
#define MolBuilder_XudtWitnessInput_set_extension_data(b, p, l) mol_table_builder_add(b, 3, p, l)
MOLECULE_API_DECORATOR mol_seg_res_t MolBuilder_XudtWitnessInput_build (mol_builder_t);
#define MolBuilder_XudtWitnessInput_clear(b) mol_builder_discard(b)
#define MolBuilder_XudtWitness_init(b) mol_table_builder_initialize(b, 128, 4)
#define MolBuilder_XudtWitness_set_owner_script(b, p, l) mol_table_builder_add(b, 0, p, l)
#define MolBuilder_XudtWitness_set_owner_signature(b, p, l) mol_table_builder_add(b, 1, p, l)
#define MolBuilder_XudtWitness_set_extension_scripts(b, p, l) mol_table_builder_add(b, 2, p, l)
#define MolBuilder_XudtWitness_set_extension_data(b, p, l) mol_table_builder_add(b, 3, p, l)
MOLECULE_API_DECORATOR mol_seg_res_t MolBuilder_XudtWitness_build (mol_builder_t);
#define MolBuilder_XudtWitness_clear(b) mol_builder_discard(b)
#define MolBuilder_RCRule_init(b) mol_builder_initialize_fixed_size(b, 33)
#define MolBuilder_RCRule_set_smt_root(b, p) mol_builder_set_by_offset(b, 0, p, 32)
#define MolBuilder_RCRule_set_flags(b, p) mol_builder_set_byte_by_offset(b, 32, p)
Expand Down Expand Up @@ -147,7 +147,7 @@ MOLECULE_API_DECORATOR mol_seg_res_t MolBuilder_XudtData_build

MOLECULE_API_DECORATOR const uint8_t MolDefault_ScriptVec[4] = {0x04, ____, ____, ____};
MOLECULE_API_DECORATOR const uint8_t MolDefault_ScriptVecOpt[0] = {};
MOLECULE_API_DECORATOR const uint8_t MolDefault_XudtWitnessInput[24] = {
MOLECULE_API_DECORATOR const uint8_t MolDefault_XudtWitness[24] = {
0x18, ____, ____, ____, 0x14, ____, ____, ____, 0x14, ____, ____, ____,
0x14, ____, ____, ____, 0x14, ____, ____, ____, 0x04, ____, ____, ____,
};
Expand Down Expand Up @@ -245,7 +245,7 @@ MOLECULE_API_DECORATOR mol_errno MolReader_ScriptVecOpt_verify (const mol_seg_t
return MOL_OK;
}
}
MOLECULE_API_DECORATOR mol_errno MolReader_XudtWitnessInput_verify (const mol_seg_t *input, bool compatible) {
MOLECULE_API_DECORATOR mol_errno MolReader_XudtWitness_verify (const mol_seg_t *input, bool compatible) {
if (input->size < MOL_NUM_T_SIZE) {
return MOL_ERR_HEADER;
}
Expand Down Expand Up @@ -544,7 +544,7 @@ MOLECULE_API_DECORATOR mol_errno MolReader_XudtData_verify (const mol_seg_t *inp
* Builder Functions
*/

MOLECULE_API_DECORATOR mol_seg_res_t MolBuilder_XudtWitnessInput_build (mol_builder_t builder) {
MOLECULE_API_DECORATOR mol_seg_res_t MolBuilder_XudtWitness_build (mol_builder_t builder) {
mol_seg_res_t res;
res.errno = MOL_OK;
mol_num_t offset = 20;
Expand Down
76 changes: 38 additions & 38 deletions c/xudt_rce_mol2.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,18 +24,18 @@ struct ScriptVecOptType make_ScriptVecOpt(mol2_cursor_t *cur);
bool ScriptVecOpt_is_none_impl(struct ScriptVecOptType *);
bool ScriptVecOpt_is_some_impl(struct ScriptVecOptType *);
struct ScriptVecType ScriptVecOpt_unwrap_impl(struct ScriptVecOptType *);
struct XudtWitnessInputType;
struct XudtWitnessInputVTable;
struct XudtWitnessInputVTable *GetXudtWitnessInputVTable(void);
struct XudtWitnessInputType make_XudtWitnessInput(mol2_cursor_t *cur);
struct ScriptOptType XudtWitnessInput_get_owner_script_impl(
struct XudtWitnessInputType *);
struct BytesOptType XudtWitnessInput_get_owner_signature_impl(
struct XudtWitnessInputType *);
struct ScriptVecOptType XudtWitnessInput_get_raw_extension_data_impl(
struct XudtWitnessInputType *);
struct BytesVecType XudtWitnessInput_get_extension_data_impl(
struct XudtWitnessInputType *);
struct XudtWitnessType;
struct XudtWitnessVTable;
struct XudtWitnessVTable *GetXudtWitnessVTable(void);
struct XudtWitnessType make_XudtWitness(mol2_cursor_t *cur);
struct ScriptOptType XudtWitness_get_owner_script_impl(
struct XudtWitnessType *);
struct BytesOptType XudtWitness_get_owner_signature_impl(
struct XudtWitnessType *);
struct ScriptVecOptType XudtWitness_get_extension_scripts_impl(
struct XudtWitnessType *);
struct BytesVecType XudtWitness_get_extension_data_impl(
struct XudtWitnessType *);
struct RCRuleType;
struct RCRuleVTable;
struct RCRuleVTable *GetRCRuleVTable(void);
Expand Down Expand Up @@ -121,16 +121,16 @@ typedef struct ScriptVecOptType {
ScriptVecOptVTable *t;
} ScriptVecOptType;

typedef struct XudtWitnessInputVTable {
struct ScriptOptType (*owner_script)(struct XudtWitnessInputType *);
struct BytesOptType (*owner_signature)(struct XudtWitnessInputType *);
struct ScriptVecOptType (*raw_extension_data)(struct XudtWitnessInputType *);
struct BytesVecType (*extension_data)(struct XudtWitnessInputType *);
} XudtWitnessInputVTable;
typedef struct XudtWitnessInputType {
typedef struct XudtWitnessVTable {
struct ScriptOptType (*owner_script)(struct XudtWitnessType *);
struct BytesOptType (*owner_signature)(struct XudtWitnessType *);
struct ScriptVecOptType (*extension_scripts)(struct XudtWitnessType *);
struct BytesVecType (*extension_data)(struct XudtWitnessType *);
} XudtWitnessVTable;
typedef struct XudtWitnessType {
mol2_cursor_t cur;
XudtWitnessInputVTable *t;
} XudtWitnessInputType;
XudtWitnessVTable *t;
} XudtWitnessType;

typedef struct RCRuleVTable {
mol2_cursor_t (*smt_root)(struct RCRuleType *);
Expand Down Expand Up @@ -287,48 +287,48 @@ ScriptVecType ScriptVecOpt_unwrap_impl(ScriptVecOptType *this) {
ret.t = GetScriptVecVTable();
return ret;
}
struct XudtWitnessInputType make_XudtWitnessInput(mol2_cursor_t *cur) {
XudtWitnessInputType ret;
struct XudtWitnessType make_XudtWitness(mol2_cursor_t *cur) {
XudtWitnessType ret;
ret.cur = *cur;
ret.t = GetXudtWitnessInputVTable();
ret.t = GetXudtWitnessVTable();
return ret;
}
struct XudtWitnessInputVTable *GetXudtWitnessInputVTable(void) {
static XudtWitnessInputVTable s_vtable;
struct XudtWitnessVTable *GetXudtWitnessVTable(void) {
static XudtWitnessVTable s_vtable;
static int inited = 0;
if (inited) return &s_vtable;
s_vtable.owner_script = XudtWitnessInput_get_owner_script_impl;
s_vtable.owner_signature = XudtWitnessInput_get_owner_signature_impl;
s_vtable.raw_extension_data = XudtWitnessInput_get_raw_extension_data_impl;
s_vtable.extension_data = XudtWitnessInput_get_extension_data_impl;
s_vtable.owner_script = XudtWitness_get_owner_script_impl;
s_vtable.owner_signature = XudtWitness_get_owner_signature_impl;
s_vtable.extension_scripts = XudtWitness_get_extension_scripts_impl;
s_vtable.extension_data = XudtWitness_get_extension_data_impl;
return &s_vtable;
}
ScriptOptType XudtWitnessInput_get_owner_script_impl(
XudtWitnessInputType *this) {
ScriptOptType XudtWitness_get_owner_script_impl(
XudtWitnessType *this) {
ScriptOptType ret;
mol2_cursor_t cur = mol2_table_slice_by_index(&this->cur, 0);
ret.cur = cur;
ret.t = GetScriptOptVTable();
return ret;
}
BytesOptType XudtWitnessInput_get_owner_signature_impl(
XudtWitnessInputType *this) {
BytesOptType XudtWitness_get_owner_signature_impl(
XudtWitnessType *this) {
BytesOptType ret;
mol2_cursor_t cur = mol2_table_slice_by_index(&this->cur, 1);
ret.cur = cur;
ret.t = GetBytesOptVTable();
return ret;
}
ScriptVecOptType XudtWitnessInput_get_raw_extension_data_impl(
XudtWitnessInputType *this) {
ScriptVecOptType XudtWitness_get_extension_scripts_impl(
XudtWitnessType *this) {
ScriptVecOptType ret;
mol2_cursor_t cur = mol2_table_slice_by_index(&this->cur, 2);
ret.cur = cur;
ret.t = GetScriptVecOptVTable();
return ret;
}
BytesVecType XudtWitnessInput_get_extension_data_impl(
XudtWitnessInputType *this) {
BytesVecType XudtWitness_get_extension_data_impl(
XudtWitnessType *this) {
BytesVecType ret;
mol2_cursor_t cur = mol2_table_slice_by_index(&this->cur, 3);
ret.cur = cur;
Expand Down
8 changes: 4 additions & 4 deletions tests/xudt_rce/ckb_syscall_xudt_sim.h
Original file line number Diff line number Diff line change
Expand Up @@ -230,16 +230,16 @@ int ckb_load_witness(void* addr, uint64_t* len, size_t offset, size_t index,
MolBuilder_WitnessArgs_init(&w);

mol_builder_t xwi_builder;
MolBuilder_XudtWitnessInput_init(&xwi_builder);
MolBuilder_XudtWitness_init(&xwi_builder);
if (g_flags == 2) {
MolBuilder_XudtWitnessInput_set_raw_extension_data(
MolBuilder_XudtWitness_set_extension_scripts(
&xwi_builder, g_extension_script_hash.ptr,
g_extension_script_hash.size);
}
MolBuilder_XudtWitnessInput_set_extension_data(&xwi_builder, g_structure.ptr,
MolBuilder_XudtWitness_set_extension_data(&xwi_builder, g_structure.ptr,
g_structure.size);

mol_seg_res_t xwi_res = MolBuilder_XudtWitnessInput_build(xwi_builder);
mol_seg_res_t xwi_res = MolBuilder_XudtWitness_build(xwi_builder);
ASSERT(xwi_res.errno == MOL_OK);

// here we fill a "big" lock to generate a big "witness"
Expand Down
6 changes: 3 additions & 3 deletions tests/xudt_rce/owner_script.c
Original file line number Diff line number Diff line change
Expand Up @@ -76,13 +76,13 @@ int get_owner_signature(uint8_t signature[SIGNATURE_SIZE]) {
mol_seg_t witness_input_seg =
MolReader_Bytes_raw_bytes(&witness_input_type_seg);

if (MolReader_XudtWitnessInput_verify(&witness_input_seg, false) != MOL_OK) {
printf("Error while verifying XudtWitnessInput\n");
if (MolReader_XudtWitness_verify(&witness_input_seg, false) != MOL_OK) {
printf("Error while verifying XudtWitness\n");
return ERROR_ENCODING;
}

mol_seg_t signature_bytes_seg =
MolReader_XudtWitnessInput_get_owner_signature(&witness_input_seg);
MolReader_XudtWitness_get_owner_signature(&witness_input_seg);

if (MolReader_BytesOpt_is_none(&signature_bytes_seg)) {
printf("Error owner_signature in witness is empty\n");
Expand Down
Loading