Skip to content
This repository has been archived by the owner on Nov 9, 2022. It is now read-only.

feat: support snapshot and revert syscalls #136

Merged
merged 3 commits into from
Nov 3, 2022
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
23 changes: 23 additions & 0 deletions c/generator_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@
#define GW_SYS_BN_ADD 3601
#define GW_SYS_BN_MUL 3602
#define GW_SYS_BN_PAIRING 3603
/* Syscall state */
#define GW_SYS_SNAPSHOT 3701
#define GW_SYS_REVERT 3702

typedef struct gw_context_t {
/* verification context */
Expand Down Expand Up @@ -70,6 +73,8 @@ typedef struct gw_context_t {
sys_get_registry_address_by_script_hash;
gw_get_script_hash_by_registry_address_fn
sys_get_script_hash_by_registry_address;
gw_snapshot_fn sys_snapshot;
gw_revert_fn sys_revert;
_gw_load_raw_fn _internal_load_raw;
_gw_store_raw_fn _internal_store_raw;
} gw_context_t;
Expand Down Expand Up @@ -405,6 +410,22 @@ int sys_pay_fee(gw_context_t *ctx, gw_reg_addr_t addr, uint32_t sudt_id,
return syscall(GW_SYS_PAY_FEE, buf, len, sudt_id, (uint8_t *)&amount, 0, 0);
}

int sys_snapshot(gw_context_t *ctx, uint32_t *snapshot) {
if (ctx == NULL) {
return GW_FATAL_INVALID_CONTEXT;
}

return syscall(GW_SYS_SNAPSHOT, snapshot, 0, 0, 0, 0, 0);
}

int sys_revert(gw_context_t *ctx, uint32_t snapshot) {
if (ctx == NULL) {
return GW_FATAL_INVALID_CONTEXT;
}

return syscall(GW_SYS_REVERT, snapshot, 0, 0, 0, 0, 0);
}

int _sys_load_rollup_config(uint8_t *addr, uint64_t *len) {
volatile uint64_t inner_len = *len;
int ret = syscall(GW_SYS_LOAD_ROLLUP_CONFIG, addr, &inner_len, 0, 0, 0, 0);
Expand Down Expand Up @@ -448,6 +469,8 @@ int gw_context_init(gw_context_t *ctx) {
_gw_get_registry_address_by_script_hash;
ctx->sys_get_script_hash_by_registry_address =
_gw_get_script_hash_by_registry_address;
ctx->sys_snapshot = sys_snapshot;
ctx->sys_revert = sys_revert;
ctx->_internal_load_raw = _internal_load_raw;
ctx->_internal_store_raw = _internal_store_raw;

Expand Down
18 changes: 18 additions & 0 deletions c/gw_def.h
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,24 @@ typedef int (*gw_get_registry_address_by_script_hash_fn)(
typedef int (*gw_get_script_hash_by_registry_address_fn)(
struct gw_context_t *ctx, gw_reg_addr_t *address, uint8_t script_hash[32]);

/**
* Create snapshot of state
*
* @param ctx The godwoken context
* @param snapshot_id The pointer of the snapshot id to save the result
* @return The status code, 0 is success
*/
typedef int (*gw_snapshot_fn)(struct gw_context_t *ctx, uint32_t *snapshot_id);

/**
* Revert state
*
* @param ctx The godwoken context
* @param snapshot_id The snapshot id
* @return The status code, 0 is success
*/
typedef int (*gw_revert_fn)(struct gw_context_t *ctx, uint32_t snapshot_id);

/**
* Load value by raw key from state tree
*
Expand Down
14 changes: 14 additions & 0 deletions c/validator_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,8 @@ typedef struct gw_context_t {
sys_get_registry_address_by_script_hash;
gw_get_script_hash_by_registry_address_fn
sys_get_script_hash_by_registry_address;
gw_snapshot_fn sys_snapshot;
gw_revert_fn sys_revert;
_gw_load_raw_fn _internal_load_raw;
_gw_store_raw_fn _internal_store_raw;

Expand Down Expand Up @@ -690,6 +692,16 @@ int sys_pay_fee(gw_context_t *ctx, gw_reg_addr_t payer_addr, uint32_t sudt_id,
return 0;
}

int sys_snapshot(gw_context_t *ctx, uint32_t *snapshot) {
/* unsuport operation */
return GW_FATAL_INVALID_CONTEXT;
}

int sys_revert(gw_context_t *ctx, uint32_t snapshot) {
/* unsuport operation */
return GW_FATAL_INVALID_CONTEXT;
}

/* Find cell by type hash */
int _find_cell_by_type_hash(uint8_t type_hash[32], uint64_t source,
uint64_t *index) {
Expand Down Expand Up @@ -1623,6 +1635,8 @@ int gw_context_init(gw_context_t *ctx) {
_gw_get_registry_address_by_script_hash;
ctx->sys_get_script_hash_by_registry_address =
_gw_get_script_hash_by_registry_address;
ctx->sys_snapshot = sys_snapshot;
ctx->sys_revert = sys_revert;
ctx->_internal_load_raw = _internal_load_raw;
ctx->_internal_store_raw = _internal_store_raw;

Expand Down