Skip to content

Add cycle-id ranges to synthetic pox events #4414

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

Merged
merged 12 commits into from
Mar 14, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
110 changes: 97 additions & 13 deletions pox-locking/src/events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,18 +111,31 @@ fn create_event_info_data_code(
args: &[Value],
response: &ResponseData,
) -> String {
// If a given burn block height is in a prepare phase, then the stacker will be in the _next_ reward cycle, so bump the cycle by 1
// `prepare_offset` is 1 or 0, depending on whether current execution is in a prepare phase or not
let prepare_offset = r#"
(prepare-offset (if (<
(mod (- %height% (var-get first-burnchain-block-height)) (var-get pox-reward-cycle-length))
(- (var-get pox-reward-cycle-length) (var-get pox-prepare-cycle-length))
) u0 u1))
"#;

match function_name {
"stack-stx" => {
format!(
r#"
(let (
(unlock-burn-height (reward-cycle-to-burn-height (+ (current-pox-reward-cycle) u1 {lock_period})))
{prepare_offset}
)
{{
data: {{
;; amount of ustx to lock.
;; equal to args[0]
lock-amount: {lock_amount},
;; burnchain height when the unlock finishes.
;; derived from args[3]
unlock-burn-height: (reward-cycle-to-burn-height (+ (current-pox-reward-cycle) u1 {lock_period})),
unlock-burn-height: unlock-burn-height,
;; PoX address tuple.
;; equal to args[1].
pox-addr: {pox_addr},
Expand All @@ -140,8 +153,12 @@ fn create_event_info_data_code(
max-amount: {max_amount},
;; equal to args[7]
auth-id: {auth_id},
;; Get end cycle ID
end-cycle-id: (some (burn-height-to-reward-cycle unlock-burn-height)),
;; Get start cycle ID
start-cycle-id: (+ (current-pox-reward-cycle) u1 prepare-offset),
}}
}}
}})
"#,
lock_amount = &args[0],
lock_period = &args[3],
Expand All @@ -151,19 +168,24 @@ fn create_event_info_data_code(
signer_key = &args.get(5).unwrap_or(&Value::none()),
max_amount = &args.get(6).unwrap_or(&Value::none()),
auth_id = &args.get(7).unwrap_or(&Value::none()),
prepare_offset = prepare_offset.replace("%height%", "burn-block-height"),
)
}
"delegate-stack-stx" => {
format!(
r#"
(let (
(unlock-burn-height (reward-cycle-to-burn-height (+ (current-pox-reward-cycle) u1 {lock_period})))
{prepare_offset}
)
{{
data: {{
;; amount of ustx to lock.
;; equal to args[1]
lock-amount: {lock_amount},
;; burnchain height when the unlock finishes.
;; derived from args[4]
unlock-burn-height: (reward-cycle-to-burn-height (+ (current-pox-reward-cycle) u1 {lock_period})),
unlock-burn-height: unlock-burn-height,
;; PoX address tuple.
;; equal to args[2]
pox-addr: {pox_addr},
Expand All @@ -178,19 +200,28 @@ fn create_event_info_data_code(
;; stacker
;; equal to args[0]
stacker: '{stacker},
;; Get end cycle ID
end-cycle-id: (some (burn-height-to-reward-cycle unlock-burn-height)),
;; Get start cycle ID
start-cycle-id: (+ (current-pox-reward-cycle) u1 prepare-offset),
}}
}}
}})
"#,
stacker = &args[0],
lock_amount = &args[1],
pox_addr = &args[2],
start_burn_height = &args[3],
lock_period = &args[4],
prepare_offset = prepare_offset.replace("%height%", "burn-block-height"),
)
}
"stack-increase" => {
format!(
r#"
(let (
(unlock-height (get unlock-height (stx-account tx-sender)))
{prepare_offset}
)
{{
data: {{
;; amount to increase by
Expand All @@ -210,19 +241,28 @@ fn create_event_info_data_code(
max-amount: {max_amount},
;; equal to args[4]
auth-id: {auth_id},
;; Get end cycle ID
end-cycle-id: (some (burn-height-to-reward-cycle unlock-height)),
;; Get start cycle ID
start-cycle-id: (+ (current-pox-reward-cycle) u1 prepare-offset),
}}
}}
}})
"#,
increase_by = &args[0],
signer_sig = &args.get(1).unwrap_or(&Value::none()),
signer_key = &args.get(2).unwrap_or(&Value::none()),
max_amount = &args.get(3).unwrap_or(&Value::none()),
auth_id = &args.get(4).unwrap_or(&Value::none()),
prepare_offset = prepare_offset.replace("%height%", "burn-block-height"),
)
}
"delegate-stack-increase" => {
format!(
r#"
(let (
(unlock-height (get unlock-height (stx-account '{stacker})))
{prepare_offset}
)
{{
data: {{
;; pox addr
Expand All @@ -239,13 +279,18 @@ fn create_event_info_data_code(
delegator: tx-sender,
;; stacker
;; equal to args[0]
stacker: '{stacker}
stacker: '{stacker},
;; Get end cycle ID
end-cycle-id: (some (burn-height-to-reward-cycle unlock-height)),
;; Get start cycle ID
start-cycle-id: (+ (current-pox-reward-cycle) u1 prepare-offset),
}}
}}
"#,
stacker = &args[0],
pox_addr = &args[1],
increase_by = &args[2],
prepare_offset = prepare_offset.replace("%height%", "burn-block-height"),
)
}
"stack-extend" => {
Expand All @@ -262,6 +307,7 @@ fn create_event_info_data_code(
unlock-in-cycle))
(last-extend-cycle (- (+ first-extend-cycle {extend_count}) u1))
(new-unlock-ht (reward-cycle-to-burn-height (+ u1 last-extend-cycle)))
{prepare_offset}
)
{{
data: {{
Expand All @@ -281,6 +327,10 @@ fn create_event_info_data_code(
max-amount: {max_amount},
;; equal to args[5]
auth-id: {auth_id},
;; Get end cycle ID
end-cycle-id: (some (burn-height-to-reward-cycle new-unlock-ht)),
;; Get start cycle ID
start-cycle-id: (+ (current-pox-reward-cycle) u1 prepare-offset),
}}
}})
"#,
Expand All @@ -290,6 +340,7 @@ fn create_event_info_data_code(
signer_key = &args.get(3).map_or("none".to_string(), |v| v.to_string()),
max_amount = &args.get(4).unwrap_or(&Value::none()),
auth_id = &args.get(5).unwrap_or(&Value::none()),
prepare_offset = prepare_offset.replace("%height%", "burn-block-height"),
)
}
"delegate-stack-extend" => {
Expand All @@ -305,6 +356,7 @@ fn create_event_info_data_code(
unlock-in-cycle))
(last-extend-cycle (- (+ first-extend-cycle {extend_count}) u1))
(new-unlock-ht (reward-cycle-to-burn-height (+ u1 last-extend-cycle)))
{prepare_offset}
)
{{
data: {{
Expand All @@ -320,13 +372,18 @@ fn create_event_info_data_code(
delegator: tx-sender,
;; stacker
;; equal to args[0]
stacker: '{stacker}
stacker: '{stacker},
;; Get end cycle ID
end-cycle-id: (some (burn-height-to-reward-cycle new-unlock-ht)),
;; Get start cycle ID
start-cycle-id: (+ (current-pox-reward-cycle) u1 prepare-offset),
}}
}})
"#,
stacker = &args[0],
pox_addr = &args[1],
extend_count = &args[2]
extend_count = &args[2],
prepare_offset = prepare_offset.replace("%height%", "burn-block-height"),
)
}
"stack-aggregation-commit" | "stack-aggregation-commit-indexed" => {
Expand Down Expand Up @@ -354,6 +411,10 @@ fn create_event_info_data_code(
max-amount: {max_amount},
;; equal to args[5]
auth-id: {auth_id},
;; Get end cycle ID
end-cycle-id: (some {reward_cycle}),
;; Get start cycle ID
start-cycle-id: {reward_cycle},
}}
}}
"#,
Expand Down Expand Up @@ -383,7 +444,11 @@ fn create_event_info_data_code(
;; delegator (this is the caller)
delegator: tx-sender,
;; equal to args[2]
reward-cycle-index: {reward_cycle_index}
reward-cycle-index: {reward_cycle_index},
;; Get end cycle ID
end-cycle-id: (some {reward_cycle}),
;; Get start cycle ID
start-cycle-id: {reward_cycle},
}}
}}
"#,
Expand All @@ -395,6 +460,9 @@ fn create_event_info_data_code(
"delegate-stx" => {
format!(
r#"
(let (
{prepare_offset}
)
{{
data: {{
;; amount of ustx to delegate.
Expand All @@ -408,32 +476,48 @@ fn create_event_info_data_code(
unlock-burn-height: {until_burn_height},
;; optional PoX address tuple.
;; equal to args[3].
pox-addr: {pox_addr}
pox-addr: {pox_addr},
;; Get end cycle ID
end-cycle-id: (match {until_burn_height}
height (some (burn-height-to-reward-cycle height))
none
),
;; Get start cycle ID
start-cycle-id: (+ (current-pox-reward-cycle) u1 prepare-offset),
}}
}}
}})
"#,
amount_ustx = &args[0],
delegate_to = &args[1],
until_burn_height = &args[2],
pox_addr = &args[3],
prepare_offset = prepare_offset.replace("%height%", "burn-block-height"),
)
}
"revoke-delegate-stx" => {
if let Value::Optional(opt) = *response.data.clone() {
eprintln!("Response data in revoke-delegate-stx is: {:?}", opt.data);
format!(
r#"
{{
data: {{ delegate-to: '{delegate_to} }}
data: {{
delegate-to: '{delegate_to},
;; Get end cycle ID
end-cycle-id: none,
;; Get start cycle ID
start-cycle-id: (+ (current-pox-reward-cycle) u1),
}},
}}
"#,
delegate_to = opt
.data
.clone()
.map(|boxed_value| *boxed_value)
.unwrap()
.expect_tuple()
.expect("FATAL: unexpected clarity value")
.get("delegated-to")
.unwrap()
.unwrap(),
)
} else {
"{data: {unimplemented: true}}".into()
Expand Down
Loading