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

Thread through protocol state view #5875

Merged
merged 6 commits into from
Sep 9, 2020
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
9 changes: 4 additions & 5 deletions src/lib/block_producer/block_producer.ml
Original file line number Diff line number Diff line change
Expand Up @@ -117,10 +117,9 @@ let generate_next_state ~constraint_constants ~previous_protocol_state
Protocol_state.hash_with_body ~body_hash:previous_protocol_state_body_hash
previous_protocol_state
in
let previous_global_slot =
let previous_state_view =
Protocol_state.body previous_protocol_state
|> Coda_state.Protocol_state.Body.consensus_state
|> Consensus.Data.Consensus_state.curr_slot
|> Coda_state.Protocol_state.Body.view
in
let%bind res =
Interruptible.uninterruptible
Expand All @@ -129,13 +128,13 @@ let generate_next_state ~constraint_constants ~previous_protocol_state
measure "create_diff" (fun () ->
Staged_ledger.create_diff ~constraint_constants staged_ledger ~self
~coinbase_receiver ~logger
~current_global_slot:previous_global_slot
~current_state_view:previous_state_view
~transactions_by_fee:transactions ~get_completed_work
~log_block_creation )
in
match%map
Staged_ledger.apply_diff_unchecked staged_ledger ~constraint_constants
diff ~logger ~current_global_slot:previous_global_slot
diff ~logger ~current_state_view:previous_state_view
~state_and_body_hash:
(previous_protocol_state_hash, previous_protocol_state_body_hash)
with
Expand Down
2 changes: 1 addition & 1 deletion src/lib/coda_base/ledger.mli
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ val apply_user_command :

val apply_transaction :
constraint_constants:Genesis_constants.Constraint_constants.t
-> txn_global_slot:Coda_numbers.Global_slot.t
-> txn_state_view:Snapp_predicate.Protocol_state.View.t
-> t
-> Transaction.t
-> Undo.t Or_error.t
Expand Down
6 changes: 4 additions & 2 deletions src/lib/coda_base/transaction_logic.ml
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ module type S = sig

val apply_transaction :
constraint_constants:Genesis_constants.Constraint_constants.t
-> txn_global_slot:Global_slot.t
-> txn_state_view:Snapp_predicate.Protocol_state.View.t
-> ledger
-> Transaction.t
-> Undo.t Or_error.t
Expand Down Expand Up @@ -1774,10 +1774,12 @@ module Make (L : Ledger_intf) : S with type ledger := L.t = struct
[%test_eq: Ledger_hash.t] undo.previous_hash (merkle_root ledger) ) ;
res

let apply_transaction ~constraint_constants ~txn_global_slot ledger
let apply_transaction ~constraint_constants
~(txn_state_view : Snapp_predicate.Protocol_state.View.t) ledger
(t : Transaction.t) =
O1trace.measure "apply_transaction" (fun () ->
let previous_hash = merkle_root ledger in
let txn_global_slot = txn_state_view.curr_global_slot in
Or_error.map
( match t with
| User_command txn ->
Expand Down
4 changes: 2 additions & 2 deletions src/lib/coda_base/transaction_validator.ml
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,6 @@ let apply_user_command ~constraint_constants ~txn_global_slot l uc =
~f:(fun undo -> undo.Undo.User_command_undo.common.user_command.status)
(apply_user_command l ~constraint_constants ~txn_global_slot uc)

let apply_transaction ~constraint_constants ~txn_global_slot l txn =
let apply_transaction ~constraint_constants ~txn_state_view l txn =
Result.map ~f:Undo.user_command_status
(apply_transaction l ~constraint_constants ~txn_global_slot txn)
(apply_transaction l ~constraint_constants ~txn_state_view txn)
2 changes: 1 addition & 1 deletion src/lib/coda_base/transaction_validator.mli
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ val apply_user_command :

val apply_transaction :
constraint_constants:Genesis_constants.Constraint_constants.t
-> txn_global_slot:Coda_numbers.Global_slot.t
-> txn_state_view:Snapp_predicate.Protocol_state.View.t
-> Hashless_ledger.t
-> Transaction.t
-> User_command_status.t Or_error.t
Expand Down
30 changes: 30 additions & 0 deletions src/lib/coda_state/protocol_state.ml
Original file line number Diff line number Diff line change
Expand Up @@ -124,12 +124,42 @@ module Body = struct

let consensus_state {Poly.consensus_state; _} = consensus_state

let view_checked (t : var) : Snapp_predicate.Protocol_state.View.Checked.t =
let module C = Consensus.Proof_of_stake.Exported.Consensus_state in
let cs : Consensus.Data.Consensus_state.var = t.consensus_state in
{ snarked_ledger_hash= t.blockchain_state.snarked_ledger_hash
; snarked_next_available_token=
t.blockchain_state.snarked_next_available_token
; timestamp= t.blockchain_state.timestamp
; blockchain_length= C.blockchain_length_var cs
; min_window_density= C.min_window_density_var cs
; last_vrf_output= ()
; total_currency= C.total_currency_var cs
; curr_global_slot= C.curr_global_slot_var cs
; staking_epoch_data= C.staking_epoch_data_var cs
; next_epoch_data= C.next_epoch_data_var cs }

[%%endif]

let hash s =
Random_oracle.hash ~init:Hash_prefix.protocol_state_body
(Random_oracle.pack_input (to_input s))
|> State_body_hash.of_hash

let view (t : Value.t) : Snapp_predicate.Protocol_state.View.t =
let module C = Consensus.Proof_of_stake.Exported.Consensus_state in
let cs = t.consensus_state in
{ snarked_ledger_hash= t.blockchain_state.snarked_ledger_hash
; snarked_next_available_token=
t.blockchain_state.snarked_next_available_token
; timestamp= t.blockchain_state.timestamp
; blockchain_length= C.blockchain_length cs
; min_window_density= C.min_window_density cs
; last_vrf_output= ()
; total_currency= C.total_currency cs
; curr_global_slot= C.curr_global_slot cs
; staking_epoch_data= C.staking_epoch_data cs
; next_epoch_data= C.next_epoch_data cs }
end

module Value = struct
Expand Down
4 changes: 4 additions & 0 deletions src/lib/coda_state/protocol_state.mli
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,10 @@ module Body : sig
val hash_checked : var -> (State_body_hash.var, _) Checked.t

val consensus_state : (_, _, 'a, _) Poly.t -> 'a

val view : Value.t -> Snapp_predicate.Protocol_state.View.t

val view_checked : var -> Snapp_predicate.Protocol_state.View.Checked.t
end

module Value : sig
Expand Down
6 changes: 2 additions & 4 deletions src/lib/coda_transition/external_transition.ml
Original file line number Diff line number Diff line change
Expand Up @@ -1035,10 +1035,8 @@ module Staged_ledger_validation = struct
Staged_ledger.apply
~constraint_constants:precomputed_values.constraint_constants ~logger
~verifier parent_staged_ledger staged_ledger_diff
~current_global_slot:
( Coda_state.Protocol_state.(
Body.consensus_state @@ body parent_protocol_state)
|> Consensus.Data.Consensus_state.curr_slot )
~current_state_view:
Coda_state.Protocol_state.(Body.view @@ body parent_protocol_state)
~state_and_body_hash:
(let body_hash =
Protocol_state.(Body.hash @@ body parent_protocol_state)
Expand Down
10 changes: 10 additions & 0 deletions src/lib/consensus/intf.ml
Original file line number Diff line number Diff line change
Expand Up @@ -456,6 +456,16 @@ module type S = sig

val curr_global_slot_var : var -> Global_slot.Checked.t

val blockchain_length_var : var -> Length.Checked.t

val min_window_density_var : var -> Length.Checked.t

val total_currency_var : var -> Amount.Checked.t

val staking_epoch_data_var : var -> Coda_base.Epoch_data.var

val next_epoch_data_var : var -> Coda_base.Epoch_data.var

val graphql_type :
unit -> ('ctx, Value.t option) Graphql_async.Schema.typ

Expand Down
9 changes: 9 additions & 0 deletions src/lib/consensus/proof_of_stake.mli
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,18 @@ module Exported : sig
include
module type of Data.Consensus_state
with type Value.Stable.V1.t = Data.Consensus_state.Value.Stable.V1.t
and type var = Data.Consensus_state.var

val global_slot : Value.t -> Global_slot.t

val total_currency : Value.t -> Currency.Amount.t

val min_window_density : Value.t -> Coda_numbers.Length.t

val staking_epoch_data : Value.t -> Coda_base.Epoch_data.Value.t

val next_epoch_data : Value.t -> Coda_base.Epoch_data.Value.t

(* unsafe modules for creating dummy states when doing vrf evaluations *)
(* TODO: refactor code so that [Hooks.next_proposal] does not require a full [Consensus_state] *)
module Unsafe : sig
Expand Down
Loading