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

Touch the oldest completed slice when marking a burrow for liquidation #240

Draft
wants to merge 25 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
e3d75a2
Restructure entrypoint_mark_for_liquidation to save on gas costs
gkaracha Aug 17, 2021
64d5453
Touch the oldest completed slice when marking a burrow for liquidation
gkaracha Aug 17, 2021
4aeab18
Comment out the touching temporarily to evaluate the gas costs from r…
gkaracha Aug 17, 2021
7f5ea12
Add inline annotation to liquidation_auction_oldest_completed_liquida…
gkaracha Aug 19, 2021
2b810bd
Revert it all; let's start over
gkaracha Aug 19, 2021
b60fe81
1. Move mark_for_liquidation where it belongs
gkaracha Aug 19, 2021
06e1cbd
TEMP: don't waste build time
gkaracha Aug 19, 2021
cb4102f
wibbles
gkaracha Aug 19, 2021
ad9cdf9
Touch the oldest completed slice when marking a burrow for liquidatio…
gkaracha Aug 19, 2021
50bcfa1
Revert the extension, once more
gkaracha Aug 19, 2021
ef513ae
Restore original location
gkaracha Aug 19, 2021
753c996
Move it elsewhere (I'm going crazy)
gkaracha Aug 19, 2021
5981f89
Move it elsewhere (I'm going crazy)
gkaracha Aug 19, 2021
bc62efe
Add inline pragma to touch_liquidation_slice (shall increase sizes)
gkaracha Aug 19, 2021
04af76a
another attempt
gkaracha Aug 19, 2021
b73cb92
Revert "TEMP: don't waste build time"
gkaracha Aug 20, 2021
2dcc15a
Reapply cb4102f569b629d5051147b1e5d56c4855a92a9d
gkaracha Aug 20, 2021
4f34fc8
Reapply ad9cdf9bea678ce34a373a88ec301f75fd4ba241
gkaracha Aug 20, 2021
f2ebf68
Revert "Reapply ad9cdf9bea678ce34a373a88ec301f75fd4ba241"
gkaracha Aug 20, 2021
2d30241
Reapply e3d75a25be508b4f5f49d18f507ffc1a85efb49a
gkaracha Aug 20, 2021
a44099f
Reapply 64d5453fb0e1ee796db24b53a51c51c899fb45d9
gkaracha Aug 20, 2021
ae6925d
e2e/main.py: reduce batch size for mark_for_liquidation
gkaracha Aug 20, 2021
49807ec
Avoid the uglification; keep the batch_size = 30
gkaracha Aug 20, 2021
3e79c58
Revert "Avoid the uglification; keep the batch_size = 30"
gkaracha Aug 20, 2021
4849a4b
try batch size = 32
gkaracha Aug 20, 2021
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
2 changes: 1 addition & 1 deletion e2e/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -541,7 +541,7 @@ def call_bulk(bulks, *, batch_size, profiler=general_gas_profiler):
)
for burrow_no in burrows
],
batch_size=40,
batch_size=32,
profiler=mark_for_liquidation_profiler,
)

Expand Down
121 changes: 78 additions & 43 deletions src/checker.ml
Original file line number Diff line number Diff line change
Expand Up @@ -289,48 +289,6 @@ let entrypoint_set_burrow_delegate (state, (burrow_no, delegate_opt): checker *
assert_checker_invariants state;
([op], state)

let[@inline] entrypoint_mark_for_liquidation (state, burrow_id: checker * burrow_id) : (LigoOp.operation list * checker) =
assert_checker_invariants state;
let _ = ensure_no_tez_given () in
let burrow = find_burrow state.burrows burrow_id in

let
{ liquidation_reward = liquidation_reward;
collateral_to_auction = collateral_to_auction;
burrow_state = burrow;
} = match burrow_request_liquidation state.parameters burrow with
| None ->
(* Note: disabling coverage for the unreported but accessed right-hand side;
* accessibility is sufficiently marked on the pattern itself. *)
((Ligo.failwith error_NotLiquidationCandidate [@coverage off]): liquidation_details)
| Some type_and_details -> let _, details = type_and_details in details
in

let state =
if eq_tok_tok collateral_to_auction tok_zero then
(* If the slice would be empty, don't create it. *)
{ state with burrows = Ligo.Big_map.update burrow_id (Some burrow) state.burrows; }
else
(* Otherwise do. *)
let contents =
{ burrow = burrow_id;
tok = collateral_to_auction;
min_kit_for_unwarranted = compute_min_kit_for_unwarranted state.parameters burrow collateral_to_auction;
} in
let (updated_liquidation_auctions, _leaf_ptr) =
liquidation_auction_send_to_auction state.liquidation_auctions contents in
{ state with
burrows = Ligo.Big_map.update burrow_id (Some burrow) state.burrows;
liquidation_auctions = updated_liquidation_auctions;
} in

let op = match (LigoOp.Tezos.get_entrypoint_opt "%burrowSendTezTo" (burrow_address burrow): (Ligo.tez * Ligo.address) Ligo.contract option) with
| Some c -> LigoOp.Tezos.tez_address_transaction (tez_of_tok liquidation_reward, !Ligo.Tezos.sender) (Ligo.tez_from_literal "0mutez") c
| None -> (Ligo.failwith error_GetEntrypointOptFailureBurrowSendTezTo : LigoOp.operation) in

assert_checker_invariants state;
([op], state)

(* Cancel the liquidation of a slice. *)
let entrypoint_cancel_liquidation_slice (state, leaf_ptr: checker * leaf_ptr) : (LigoOp.operation list * checker) =
assert_checker_invariants state;
Expand Down Expand Up @@ -359,7 +317,7 @@ let entrypoint_cancel_liquidation_slice (state, leaf_ptr: checker * leaf_ptr) :
(* Note that this function prepends the operation to the list of operations
* given. This means that if we entrypoint_touch a list of liquidation slices,
* the order of operations is reversed. *)
let touch_liquidation_slice
let[@inline] touch_liquidation_slice
(ops: LigoOp.operation list)
(auctions: liquidation_auctions)
(state_burrows: burrow_map)
Expand Down Expand Up @@ -453,6 +411,83 @@ let touch_liquidation_slice
| None -> (Ligo.failwith error_GetEntrypointOptFailureBurrowSendSliceToChecker : LigoOp.operation) in
((op :: ops), auctions, state_burrows, state_parameters, state_fa2_state)

let[@inline] entrypoint_mark_for_liquidation (state, burrow_id: checker * burrow_id) : (LigoOp.operation list * checker) =
assert_checker_invariants state;
let _ = ensure_no_tez_given () in

let
{ burrows = state_burrows;
cfmm = state_cfmm;
parameters = state_parameters;
liquidation_auctions = state_liquidation_auctions;
last_price = state_last_price;
fa2_state = state_fa2_state;
external_contracts = state_external_contracts;
} = state in

let burrow = find_burrow state_burrows burrow_id in

let
{ liquidation_reward = liquidation_reward;
collateral_to_auction = collateral_to_auction;
burrow_state = burrow;
} = match burrow_request_liquidation state_parameters burrow with
| None ->
(* Note: disabling coverage for the unreported but accessed right-hand side;
* accessibility is sufficiently marked on the pattern itself. *)
((Ligo.failwith error_NotLiquidationCandidate [@coverage off]): liquidation_details)
| Some type_and_details -> let _, details = type_and_details in details
in

let state_burrows = Ligo.Big_map.update burrow_id (Some burrow) state_burrows in

let state_liquidation_auctions =
if eq_tok_tok collateral_to_auction tok_zero then
(* If the slice would be empty, don't create it. *)
state_liquidation_auctions
else
(* Otherwise do. *)
let contents =
{ burrow = burrow_id;
tok = collateral_to_auction;
min_kit_for_unwarranted = compute_min_kit_for_unwarranted state_parameters burrow collateral_to_auction;
} in
let (state_liquidation_auctions, _leaf_ptr) =
liquidation_auction_send_to_auction state_liquidation_auctions contents in
state_liquidation_auctions
in

let op = match (LigoOp.Tezos.get_entrypoint_opt "%burrowSendTezTo" (burrow_address burrow): (Ligo.tez * Ligo.address) Ligo.contract option) with
| Some c -> LigoOp.Tezos.tez_address_transaction (tez_of_tok liquidation_reward, !Ligo.Tezos.sender) (Ligo.tez_from_literal "0mutez") c
| None -> (Ligo.failwith error_GetEntrypointOptFailureBurrowSendTezTo : LigoOp.operation) in
let ops = [op] in

(* Touch the oldest liquidation slice (if it exists). This should help the
* system keep the number of liquidation slices close to linear in many
* occasions. *)
let ops, state_liquidation_auctions, state_burrows, state_parameters, state_fa2_state =
match liquidation_auction_oldest_completed_liquidation_slice state_liquidation_auctions with
| None ->
ops, state_liquidation_auctions, state_burrows, state_parameters, state_fa2_state
| Some leaf ->
let ops, state_liquidation_auctions, state_burrows, state_parameters, state_fa2_state =
touch_liquidation_slice ops state_liquidation_auctions state_burrows state_parameters state_fa2_state leaf in
ops, state_liquidation_auctions, state_burrows, state_parameters, state_fa2_state
in

let state =
{ burrows = state_burrows;
cfmm = state_cfmm;
parameters = state_parameters;
liquidation_auctions = state_liquidation_auctions;
last_price = state_last_price;
fa2_state = state_fa2_state;
external_contracts = state_external_contracts;
} in

assert_checker_invariants state;
(ops, state)

(* NOTE: The list of operations returned is in reverse order (with respect to
* the order the input slices were processed in). However, since the operations
* computed are independent from each other, this needs not be a problem. *)
Expand Down