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

Limit zkApp Commands Hardcap #14813

Merged
merged 23 commits into from
Jan 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
31c52c4
Add in varibale to runtime config
ejMina226 Jan 9, 2024
23fcc70
Add in code to Staged ledger and move limit to constraint constant
ejMina226 Jan 9, 2024
6f5c004
Add hardcap check into apply_diff
ejMina226 Jan 10, 2024
e3977b8
Move variable from Constraint_constants to Protocol constants
ejMina226 Jan 10, 2024
74367fc
Access Protocol Constant for Staged ledger
ejMina226 Jan 10, 2024
3a4a284
Add in new variable to function so it can be passed down to staged le…
ejMina226 Jan 11, 2024
565f5fb
Merge branch 'rampup' into limit-zkapp-commands-to-one
ejMina226 Jan 11, 2024
8346fc7
Add in validation change
ejMina226 Jan 11, 2024
9773337
Moved variable to daemon
ejMina226 Jan 11, 2024
7d49938
Remove change to mlh
ejMina226 Jan 11, 2024
02d2748
Fix automation
ejMina226 Jan 11, 2024
3af8e09
Remove change
ejMina226 Jan 11, 2024
a9228f5
Change valid size
ejMina226 Jan 11, 2024
8121345
Change value in mina_compile_config
ejMina226 Jan 11, 2024
72fe824
Changed variable name
ejMina226 Jan 11, 2024
0ad8860
Code review changes
ejMina226 Jan 22, 2024
07507eb
Merge branch 'rampup' into limit-zkapp-commands-to-one
ejMina226 Jan 22, 2024
2dca09d
Build error
ejMina226 Jan 22, 2024
2fbe92c
Merge pull request #14954 from MinaProtocol/fix/compatible-rampup-merge
mrmr1993 Jan 24, 2024
85e9b7b
Merge branch 'rampup' into limit-zkapp-commands-to-one
georgeee Jan 24, 2024
e8f3041
Merge branch 'berkeley' into limit-zkapp-commands-to-one
georgeee Jan 24, 2024
4aa83dd
Merge branch 'berkeley' into limit-zkapp-commands-to-one
ejMina226 Jan 24, 2024
0a85ae5
Fix broken test
ejMina226 Jan 24, 2024
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
10 changes: 6 additions & 4 deletions src/lib/block_producer/block_producer.ml
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,8 @@ let report_transaction_inclusion_failures ~logger failed_txns =
let generate_next_state ~zkapp_cmd_limit ~constraint_constants
~previous_protocol_state ~time_controller ~staged_ledger ~transactions
~get_completed_work ~logger ~(block_data : Consensus.Data.Block_data.t)
~winner_pk ~scheduled_time ~log_block_creation ~block_reward_threshold =
~winner_pk ~scheduled_time ~log_block_creation ~block_reward_threshold
~zkapp_cmd_limit_hardcap =
let open Interruptible.Let_syntax in
let previous_protocol_state_body_hash =
Protocol_state.body previous_protocol_state |> Protocol_state.Body.hash
Expand Down Expand Up @@ -243,7 +244,7 @@ let generate_next_state ~zkapp_cmd_limit ~constraint_constants
~global_slot diff ~logger ~current_state_view:previous_state_view
~state_and_body_hash:
(previous_protocol_state_hash, previous_protocol_state_body_hash)
~coinbase_receiver ~supercharge_coinbase
~coinbase_receiver ~supercharge_coinbase ~zkapp_cmd_limit_hardcap
with
| Ok
( `Hash_after_applying next_staged_ledger_hash
Expand Down Expand Up @@ -613,7 +614,8 @@ let run ~context:(module Context : CONTEXT) ~vrf_evaluator ~prover ~verifier
~trust_system ~get_completed_work ~transaction_resource_pool
~time_controller ~consensus_local_state ~coinbase_receiver ~frontier_reader
~transition_writer ~set_next_producer_timing ~log_block_creation
~block_reward_threshold ~block_produced_bvar ~vrf_evaluation_state ~net =
~block_reward_threshold ~block_produced_bvar ~vrf_evaluation_state ~net
~zkapp_cmd_limit_hardcap =
let open Context in
O1trace.sync_thread "produce_blocks" (fun () ->
let genesis_breadcrumb =
Expand Down Expand Up @@ -754,7 +756,7 @@ let run ~context:(module Context : CONTEXT) ~vrf_evaluator ~prover ~verifier
~staged_ledger:(Breadcrumb.staged_ledger crumb)
~transactions ~get_completed_work ~logger ~log_block_creation
~winner_pk:winner_pubkey ~block_reward_threshold
~zkapp_cmd_limit:!zkapp_cmd_limit
~zkapp_cmd_limit:!zkapp_cmd_limit ~zkapp_cmd_limit_hardcap
in
[%log internal] "Generate_next_state_done" ;
match next_state_opt with
Expand Down
2 changes: 2 additions & 0 deletions src/lib/genesis_constants/genesis_constants.ml
Original file line number Diff line number Diff line change
Expand Up @@ -340,6 +340,7 @@ module T = struct
; zkapp_transaction_cost_limit : float
; max_event_elements : int
; max_action_elements : int
; zkapp_cmd_limit_hardcap : int
}
[@@deriving to_yojson, sexp_of, bin_io_unversioned]

Expand Down Expand Up @@ -400,6 +401,7 @@ let compiled : t =
Mina_compile_config.zkapp_transaction_cost_limit
; max_event_elements = Mina_compile_config.max_event_elements
; max_action_elements = Mina_compile_config.max_action_elements
; zkapp_cmd_limit_hardcap = Mina_compile_config.zkapp_cmd_limit_hardcap
}

let for_unit_tests = compiled
Original file line number Diff line number Diff line change
Expand Up @@ -586,6 +586,9 @@ let make_genesis_constants ~logger ~(default : Genesis_constants.t)
Option.value_map ~default:default.num_accounts
(config.ledger >>= fun cfg -> cfg.num_accounts)
~f:(fun num_accounts -> Some num_accounts)
; zkapp_cmd_limit_hardcap =
Option.value ~default:default.zkapp_cmd_limit_hardcap
(config.daemon >>= fun cfg -> cfg.zkapp_cmd_limit_hardcap)
}

let runtime_config_of_genesis_constants (genesis_constants : Genesis_constants.t)
Expand Down Expand Up @@ -627,6 +630,8 @@ let runtime_config_of_precomputed_values (precomputed_values : Genesis_proof.t)
Some precomputed_values.genesis_constants.max_event_elements
; max_action_elements =
Some precomputed_values.genesis_constants.max_action_elements
; zkapp_cmd_limit_hardcap =
Some precomputed_values.genesis_constants.zkapp_cmd_limit_hardcap
}
; genesis =
Some
Expand Down
1 change: 1 addition & 0 deletions src/lib/integration_test_cloud_engine/mina_automation.ml
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,7 @@ module Network_config = struct
; zkapp_transaction_cost_limit = None
; max_event_elements = None
; max_action_elements = None
; zkapp_cmd_limit_hardcap = None
}
; genesis =
Some
Expand Down
1 change: 1 addition & 0 deletions src/lib/mina_base/test/valid_size.ml
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ let genesis_constant_error limit events actions : Genesis_constants.t =
; zkapp_transaction_cost_limit = limit
; max_event_elements = events
; max_action_elements = actions
; zkapp_cmd_limit_hardcap = 128
}

(* Note that in the following tests the generated zkapp_type will have an account_updates (i.e. a call_forest)
Expand Down
3 changes: 3 additions & 0 deletions src/lib/mina_block/validation.ml
Original file line number Diff line number Diff line change
Expand Up @@ -499,6 +499,9 @@ let validate_staged_ledger_diff ?skip_staged_ledger_verification ~logger
~coinbase_receiver:(Consensus_state.coinbase_receiver consensus_state)
~supercharge_coinbase:
(Consensus_state.supercharge_coinbase consensus_state)
~zkapp_cmd_limit_hardcap:
precomputed_values.Precomputed_values.genesis_constants
.zkapp_cmd_limit_hardcap
|> Deferred.Result.map_error ~f:(fun e ->
`Staged_ledger_application_failed e )
in
Expand Down
2 changes: 2 additions & 0 deletions src/lib/mina_compile_config/mina_compile_config.ml
Original file line number Diff line number Diff line change
Expand Up @@ -80,3 +80,5 @@ let zkapp_cmd_limit = None
let zkapp_cmd_limit = Some zkapp_cmd_limit

[%%endif]

let zkapp_cmd_limit_hardcap = 128
4 changes: 3 additions & 1 deletion src/lib/mina_lib/mina_lib.ml
Original file line number Diff line number Diff line change
Expand Up @@ -1391,7 +1391,9 @@ let start t =
~log_block_creation:t.config.log_block_creation
~block_reward_threshold:t.config.block_reward_threshold
~block_produced_bvar:t.components.block_produced_bvar
~vrf_evaluation_state:t.vrf_evaluation_state ~net:t.components.net ;
~vrf_evaluation_state:t.vrf_evaluation_state ~net:t.components.net
~zkapp_cmd_limit_hardcap:
t.config.precomputed_values.genesis_constants.zkapp_cmd_limit_hardcap ;
perform_compaction t ;
let () =
match t.config.node_status_url with
Expand Down
7 changes: 7 additions & 0 deletions src/lib/runtime_config/runtime_config.ml
Original file line number Diff line number Diff line change
Expand Up @@ -416,6 +416,7 @@ module Json_layout = struct
; zkapp_transaction_cost_limit : float option [@default None]
; max_event_elements : int option [@default None]
; max_action_elements : int option [@default None]
; zkapp_cmd_limit_hardcap : int option [@default None]
}
[@@deriving yojson, fields, dhall_type]

Expand Down Expand Up @@ -1135,6 +1136,7 @@ module Daemon = struct
; zkapp_transaction_cost_limit : float option [@default None]
; max_event_elements : int option [@default None]
; max_action_elements : int option [@default None]
; zkapp_cmd_limit_hardcap : int option [@default None]
}
[@@deriving bin_io_unversioned]

Expand Down Expand Up @@ -1168,6 +1170,9 @@ module Daemon = struct
opt_fallthrough ~default:t1.max_event_elements t2.max_event_elements
; max_action_elements =
opt_fallthrough ~default:t1.max_action_elements t2.max_action_elements
; zkapp_cmd_limit_hardcap =
opt_fallthrough ~default:t1.zkapp_cmd_limit_hardcap
t2.zkapp_cmd_limit_hardcap
}

let gen =
Expand All @@ -1178,6 +1183,7 @@ module Daemon = struct
let%bind zkapp_signed_pair_update_cost = Float.gen_incl 0.0 100.0 in
let%bind zkapp_transaction_cost_limit = Float.gen_incl 0.0 100.0 in
let%bind max_event_elements = Int.gen_incl 0 100 in
let%bind zkapp_cmd_limit_hardcap = Int.gen_incl 0 1000 in
let%map max_action_elements = Int.gen_incl 0 1000 in
{ txpool_max_size = Some txpool_max_size
; peer_list_url = None
Expand All @@ -1187,6 +1193,7 @@ module Daemon = struct
; zkapp_transaction_cost_limit = Some zkapp_transaction_cost_limit
; max_event_elements = Some max_event_elements
; max_action_elements = Some max_action_elements
; zkapp_cmd_limit_hardcap = Some zkapp_cmd_limit_hardcap
}
end

Expand Down
49 changes: 42 additions & 7 deletions src/lib/staged_ledger/staged_ledger.ml
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ module T = struct
| Mismatched_statuses of
Transaction.t With_status.t * Transaction_status.t
| Invalid_public_key of Public_key.Compressed.t
| ZkApps_exceed_limit of int * int
| Unexpected of Error.t
[@@deriving sexp]

Expand Down Expand Up @@ -102,6 +103,11 @@ module T = struct
!"A transaction contained an invalid public key %{sexp: \
Public_key.Compressed.t}"
pk
| ZkApps_exceed_limit (count, limit) ->
Format.asprintf
!"There are %{sexp: int} ZkApps in the block when there is a \
configured limit of %{sexp: int}"
count limit
| Unexpected e ->
Error.to_string_hum e

Expand Down Expand Up @@ -1006,7 +1012,7 @@ module T = struct

let apply_diff ?(skip_verification = false) ~logger ~constraint_constants
~global_slot t pre_diff_info ~current_state_view ~state_and_body_hash
~log_prefix =
~log_prefix ~zkapp_cmd_limit_hardcap =
let open Deferred.Result.Let_syntax in
let max_throughput =
Int.pow 2 t.constraint_constants.transaction_capacity_log_2
Expand All @@ -1027,6 +1033,25 @@ module T = struct
|> Set.to_list
in
Ledger.unsafe_preload_accounts_from_parent new_ledger accounts_accessed ;
let%bind () =
(* Check number of zkApps in a block does not exceed hardcap *)
O1trace.thread "zkapp_hardcap_check" (fun () ->
let is_zkapp : Transaction.t With_status.t -> bool = function
| { With_status.data =
Transaction.Command (Mina_base.User_command.Zkapp_command _)
; status = _
} ->
true
| _ ->
false
in
let zk_app_count = List.count ~f:is_zkapp transactions in
if zk_app_count > zkapp_cmd_limit_hardcap then
Deferred.Result.fail
(Staged_ledger_error.ZkApps_exceed_limit
(zk_app_count, zkapp_cmd_limit_hardcap) )
else Deferred.Result.return () )
in
[%log internal] "Update_coinbase_stack"
~metadata:
[ ("transactions", `Int (List.length transactions))
Expand Down Expand Up @@ -1229,7 +1254,7 @@ module T = struct
let apply ?skip_verification ~constraint_constants ~global_slot t
~get_completed_work (witness : Staged_ledger_diff.t) ~logger ~verifier
~current_state_view ~state_and_body_hash ~coinbase_receiver
~supercharge_coinbase =
~supercharge_coinbase ~zkapp_cmd_limit_hardcap =
let open Deferred.Result.Let_syntax in
let work = Staged_ledger_diff.completed_works witness in
let%bind () =
Expand Down Expand Up @@ -1262,7 +1287,7 @@ module T = struct
~constraint_constants ~global_slot t
(forget_prediff_info prediff)
~logger ~current_state_view ~state_and_body_hash
~log_prefix:"apply_diff"
~log_prefix:"apply_diff" ~zkapp_cmd_limit_hardcap
in
[%log internal] "Diff_applied" ;
[%log debug]
Expand All @@ -1284,7 +1309,7 @@ module T = struct
let apply_diff_unchecked ~constraint_constants ~global_slot t
(sl_diff : Staged_ledger_diff.With_valid_signatures_and_proofs.t) ~logger
~current_state_view ~state_and_body_hash ~coinbase_receiver
~supercharge_coinbase =
~supercharge_coinbase ~zkapp_cmd_limit_hardcap =
let open Deferred.Result.Let_syntax in
let%bind prediff =
Result.map_error ~f:(fun error -> Staged_ledger_error.Pre_diff error)
Expand All @@ -1296,6 +1321,7 @@ module T = struct
(forget_prediff_info prediff)
~constraint_constants ~global_slot ~logger ~current_state_view
~state_and_body_hash ~log_prefix:"apply_diff_unchecked"
~zkapp_cmd_limit_hardcap

module Resources = struct
module Discarded = struct
Expand Down Expand Up @@ -2348,6 +2374,11 @@ let%test_module "staged ledger tests" =

let proof_level = Genesis_constants.Proof_level.for_unit_tests

let constraint_constants =
Genesis_constants.Constraint_constants.for_unit_tests

let zkapp_cmd_limit_hardcap = 200

let logger = Logger.null ()

let `VK vk, `Prover zkapp_prover =
Expand Down Expand Up @@ -2404,6 +2435,7 @@ let%test_module "staged ledger tests" =
Sl.apply ~constraint_constants ~global_slot !sl diff' ~logger
~verifier ~get_completed_work:(Fn.const None) ~current_state_view
~state_and_body_hash ~coinbase_receiver ~supercharge_coinbase
~zkapp_cmd_limit_hardcap
with
| Ok x ->
x
Expand Down Expand Up @@ -3337,6 +3369,7 @@ let%test_module "staged ledger tests" =
( state_hashes.state_hash
, state_hashes.state_body_hash |> Option.value_exn )
~coinbase_receiver ~supercharge_coinbase:true
~zkapp_cmd_limit_hardcap
in
let checked', diff' =
match apply_res with
Expand Down Expand Up @@ -4364,6 +4397,7 @@ let%test_module "staged ledger tests" =
~logger ~verifier ~get_completed_work:(Fn.const None)
~current_state_view ~state_and_body_hash
~coinbase_receiver ~supercharge_coinbase:false
~zkapp_cmd_limit_hardcap
with
| Ok _x ->
assert false
Expand Down Expand Up @@ -4572,7 +4606,7 @@ let%test_module "staged ledger tests" =
(Staged_ledger_diff.forget diff)
~logger ~verifier ~get_completed_work:(Fn.const None)
~current_state_view ~state_and_body_hash ~coinbase_receiver
~supercharge_coinbase:false
~supercharge_coinbase:false ~zkapp_cmd_limit_hardcap
with
| Ok _x -> (
let valid_command_1_with_status =
Expand Down Expand Up @@ -4619,6 +4653,7 @@ let%test_module "staged ledger tests" =
~logger ~verifier ~get_completed_work:(Fn.const None)
~current_state_view ~state_and_body_hash
~coinbase_receiver ~supercharge_coinbase:false
~zkapp_cmd_limit_hardcap
with
| Ok _x ->
assert false
Expand Down Expand Up @@ -4715,7 +4750,7 @@ let%test_module "staged ledger tests" =
apply ~logger ~constraint_constants ~global_slot
~get_completed_work:(Fn.const None) ~verifier
~current_state_view ~state_and_body_hash ~coinbase_receiver
~supercharge_coinbase:false sl diff
~supercharge_coinbase:false sl diff ~zkapp_cmd_limit_hardcap
in
match (expectation, result) with
| `Accept, Ok _ | `Reject, Error _ ->
Expand Down Expand Up @@ -5135,7 +5170,7 @@ let%test_module "staged ledger tests" =
~get_completed_work:(Fn.const None) ~logger
~verifier:verifier_full ~current_state_view
~state_and_body_hash ~coinbase_receiver
~supercharge_coinbase:false
~supercharge_coinbase:false ~zkapp_cmd_limit_hardcap
with
| Ok _ ->
failwith "invalid block should be rejected"
Expand Down
3 changes: 3 additions & 0 deletions src/lib/staged_ledger/staged_ledger.mli
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@ module Staged_ledger_error : sig
| Insufficient_work of string
| Mismatched_statuses of Transaction.t With_status.t * Transaction_status.t
| Invalid_public_key of Public_key.Compressed.t
| ZkApps_exceed_limit of int * int
| Unexpected of Error.t
[@@deriving sexp]

Expand Down Expand Up @@ -192,6 +193,7 @@ val apply :
-> state_and_body_hash:State_hash.t * State_body_hash.t
-> coinbase_receiver:Public_key.Compressed.t
-> supercharge_coinbase:bool
-> zkapp_cmd_limit_hardcap:int
-> ( [ `Hash_after_applying of Staged_ledger_hash.t ]
* [ `Ledger_proof of
( Ledger_proof.t
Expand All @@ -216,6 +218,7 @@ val apply_diff_unchecked :
-> state_and_body_hash:State_hash.t * State_body_hash.t
-> coinbase_receiver:Public_key.Compressed.t
-> supercharge_coinbase:bool
-> zkapp_cmd_limit_hardcap:int
-> ( [ `Hash_after_applying of Staged_ledger_hash.t ]
* [ `Ledger_proof of
( Ledger_proof.t
Expand Down
3 changes: 3 additions & 0 deletions src/lib/transition_frontier/frontier_base/breadcrumb.ml
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,7 @@ let build ?skip_staged_ledger_verification ~logger ~precomputed_values ~verifier
| Insufficient_work _
| Mismatched_statuses _
| Invalid_public_key _
| ZkApps_exceed_limit _
| Unexpected _ ->
make_actions Gossiped_invalid_transition
in
Expand Down Expand Up @@ -407,6 +408,8 @@ module For_tests = struct
staged_ledger_diff
~constraint_constants:precomputed_values.constraint_constants
~current_state_view ~state_and_body_hash ~supercharge_coinbase
~zkapp_cmd_limit_hardcap:
precomputed_values.genesis_constants.zkapp_cmd_limit_hardcap
with
| Ok r ->
return r
Expand Down