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

REFUND increment rationalization for SSTORE #525

Merged
merged 4 commits into from
Dec 5, 2024
Merged
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
107 changes: 66 additions & 41 deletions hub/constraints/instruction-handling/sto.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -133,47 +133,72 @@
(DOM-SUB-stamps---revert-with-current 4 ;; kappa
0))))) ;; c

(defun (orig-is-zero) (shift storage/VALUE_ORIG_IS_ZERO 3))
(defun (curr-is-zero) (shift storage/VALUE_CURR_IS_ZERO 3))
(defun (next-is-zero) (shift storage/VALUE_NEXT_IS_ZERO 3))
(defun (curr-is-orig) (shift storage/VALUE_CURR_IS_ORIG 3))
(defun (next-is-orig) (shift storage/VALUE_NEXT_IS_ORIG 3))
(defun (next-is-curr) (shift storage/VALUE_NEXT_IS_CURR 3))
(defun (cold-slot) (force-bin (- 1 (shift storage/WARMTH 3))))


(defconstraint storage-instruction---setting-gas-costs (:guard (storage-instruction---no-stack-exceptions))
(defun (orig-is-zero) (shift storage/VALUE_ORIG_IS_ZERO 3))
(defun (curr-is-zero) (shift storage/VALUE_CURR_IS_ZERO 3))
(defun (next-is-zero) (shift storage/VALUE_NEXT_IS_ZERO 3))
(defun (curr-is-orig) (shift storage/VALUE_CURR_IS_ORIG 3))
(defun (next-is-orig) (shift storage/VALUE_NEXT_IS_ORIG 3))
(defun (next-is-curr) (shift storage/VALUE_NEXT_IS_CURR 3))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defun (orig-not-zero) (force-bin (- 1 (orig-is-zero))))
(defun (curr-not-zero) (force-bin (- 1 (curr-is-zero))))
(defun (next-not-zero) (force-bin (- 1 (next-is-zero))))
(defun (curr-not-orig) (force-bin (- 1 (curr-is-orig))))
(defun (next-not-orig) (force-bin (- 1 (next-is-orig))))
(defun (next-not-curr) (force-bin (- 1 (next-is-curr))))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defun (context-wont-revert) (force-bin (- 1 CONTEXT_WILL_REVERT)))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defun (cold-slot) (force-bin (- 1 (shift storage/WARMTH 3))))


(defconstraint storage-instruction---setting-gas-costs---SLOAD-case
(:guard (storage-instruction---no-stack-exceptions))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(if-not-zero (oogx-or-no-exception)
(if-zero (force-bin (storage-instruction---is-SSTORE))
;; SLOAD case
;;;;;;;;;;;;;
(if-zero (force-bin (shift storage/WARMTH 3))
(eq! GAS_COST GAS_CONST_G_COLD_SLOAD)
(eq! GAS_COST GAS_CONST_G_WARM_ACCESS))
;; SSTORE case
;;;;;;;;;;;;;;
(begin
(if-not-zero (next-is-curr)
(eq! GAS_COST (+ GAS_CONST_G_WARM_ACCESS (* (cold-slot) GAS_CONST_G_COLD_SLOAD))))
(if-zero (curr-is-orig)
(eq! GAS_COST (+ GAS_CONST_G_WARM_ACCESS (* (cold-slot) GAS_CONST_G_COLD_SLOAD))))
(if-zero (next-is-curr)
(if-not-zero (curr-is-orig)
(if-not-zero (orig-is-zero)
(eq! GAS_COST (+ GAS_CONST_G_SSET (* (cold-slot) GAS_CONST_G_COLD_SLOAD)))
(eq! GAS_COST (+ GAS_CONST_G_SRESET (* (cold-slot) GAS_CONST_G_COLD_SLOAD))))))))))
(if-not-zero (storage-instruction---is-SLOAD)
(if-not-zero (cold-slot)
(eq! GAS_COST GAS_CONST_G_COLD_SLOAD)
(eq! GAS_COST GAS_CONST_G_WARM_ACCESS)))))

(defconstraint storage-instruction---setting-gas-costs---SSTORE-case
(:guard (storage-instruction---no-stack-exceptions))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(if-not-zero (oogx-or-no-exception)
(if-not-zero (storage-instruction---is-SSTORE)
(begin
(if-not-zero (next-is-curr)
(eq! GAS_COST (+ GAS_CONST_G_WARM_ACCESS (* (cold-slot) GAS_CONST_G_COLD_SLOAD))))
(if-not-zero (curr-not-orig)
(eq! GAS_COST (+ GAS_CONST_G_WARM_ACCESS (* (cold-slot) GAS_CONST_G_COLD_SLOAD))))
(if-not-zero (next-not-curr)
(if-not-zero (curr-is-orig)
(if-not-zero (orig-is-zero)
(eq! GAS_COST (+ GAS_CONST_G_SSET (* (cold-slot) GAS_CONST_G_COLD_SLOAD)))
(eq! GAS_COST (+ GAS_CONST_G_SRESET (* (cold-slot) GAS_CONST_G_COLD_SLOAD))))))))))

(defun (r-clean-clear) (* (next-not-curr)
(curr-is-orig)
(next-is-zero)
REFUND_CONST_R_SCLEAR))
(defun (r-dirty-clear) (* (next-not-curr)
(curr-not-orig)
(orig-not-zero)
(- (next-is-zero)
(curr-is-zero))
REFUND_CONST_R_SCLEAR))
(defun (r-dirty-reset) (* (next-not-curr)
(curr-not-orig)
(next-is-orig)
(+ (* (orig-is-zero) (- GAS_CONST_G_SSET GAS_CONST_G_WARM_ACCESS))
(* (orig-not-zero) (- GAS_CONST_G_WARM_ACCESS GAS_CONST_G_SRESET)))))

(defconstraint storage-instruction---setting-the-refund (:guard (storage-instruction---no-stack-exceptions))
(if-zero CONTEXT_WILL_REVERT
(if-not-zero (storage-instruction---is-SSTORE)
(if-not-zero (force-bin (next-is-curr))
(eq! REFUND_COUNTER_NEW REFUND_COUNTER)
(if-zero (force-bin (curr-is-orig))
(eq! REFUND_COUNTER_NEW
(+ REFUND_COUNTER
(* (- 1 (orig-is-zero)) (- (next-is-zero) (curr-is-zero)) REFUND_CONST_R_SCLEAR )
(* (next-is-orig) (orig-is-zero) (- GAS_CONST_G_SSET GAS_CONST_G_WARM_ACCESS))
(* (next-is-orig) (- 1 (orig-is-zero)) (- GAS_CONST_G_SRESET GAS_CONST_G_WARM_ACCESS))))
(if-zero (force-bin (next-is-zero))
(eq! REFUND_COUNTER_NEW REFUND_COUNTER)
(eq! REFUND_COUNTER_NEW (+ REFUND_COUNTER REFUND_CONST_R_SCLEAR))))))))
(eq!
REFUND_COUNTER_NEW
(+ REFUND_COUNTER
(* (context-wont-revert)
(storage-instruction---is-SSTORE)
(+ (r-clean-clear)
(r-dirty-clear)
(r-dirty-reset))))))
Loading