Skip to content

Commit cd20af0

Browse files
committed
Support v1 format for ledger hash generation
Problem: set_verification_key permission format was changed to include tx_version which changed the algorithm for ledger hash verification. This obstructs attempts to generate old-network-format hashes. Solution: provide an option to omit tx version of set_verification_key permission when computing the ledger hash.
1 parent 7914260 commit cd20af0

File tree

20 files changed

+95
-38
lines changed

20 files changed

+95
-38
lines changed

src/app/runtime_genesis_ledger/runtime_genesis_ledger.ml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ let load_ledger (accounts : Runtime_config.Accounts.t) =
6161
let packed_patched =
6262
Genesis_ledger_helper.Ledger.packed_genesis_ledger_of_accounts
6363
~depth:migrating_from_ledger_depth
64+
~omit_set_verification_key_tx_version:true
6465
(lazy accounts_altered)
6566
in
6667
( Lazy.force (Genesis_ledger.Packed.t packed)

src/lib/genesis_ledger/genesis_ledger.ml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,8 @@ module Make (Inputs : Intf.Ledger_input_intf) : Intf.S = struct
132132
|> List.mapi ~f:(fun i (_, acct) ->
133133
(Ledger.Addr.of_int_exn ~ledger_depth i, acct) )
134134
in
135-
Ledger.set_batch_accounts ledger addrs_and_accounts ) ;
135+
Ledger.set_batch_accounts ~omit_set_verification_key_tx_version ledger
136+
addrs_and_accounts ) ;
136137
ledger
137138

138139
include Utils
@@ -274,6 +275,8 @@ module Unit_test_ledger = Make (struct
274275
let directory = `Ephemeral
275276

276277
let depth = Genesis_constants.Constraint_constants.for_unit_tests.ledger_depth
278+
279+
let omit_set_verification_key_tx_version = false
277280
end)
278281

279282
let for_unit_tests : Packed.t = (module Unit_test_ledger)

src/lib/genesis_ledger/intf.ml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,8 @@ module type Ledger_input_intf = sig
7979
val directory : [ `Ephemeral | `New | `Path of string ]
8080

8181
val depth : int
82+
83+
val omit_set_verification_key_tx_version : bool
8284
end
8385

8486
module type S = sig

src/lib/genesis_ledger_helper/genesis_ledger_helper.ml

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,8 @@ module Ledger = struct
269269
let directory = `Path dirname
270270

271271
let depth = constraint_constants.ledger_depth
272+
273+
let omit_set_verification_key_tx_version = false
272274
end) )
273275
| None ->
274276
( module Genesis_ledger.Of_ledger (struct
@@ -421,14 +423,18 @@ module Ledger = struct
421423
(Lazy.map
422424
~f:(Accounts.pad_to (Option.value ~default:0 config.num_accounts)) )
423425

424-
let packed_genesis_ledger_of_accounts ~depth accounts :
426+
let packed_genesis_ledger_of_accounts
427+
?(omit_set_verification_key_tx_version = false) ~depth accounts :
425428
Genesis_ledger.Packed.t =
426429
( module Genesis_ledger.Make (struct
427430
let accounts = accounts
428431

429432
let directory = `New
430433

431434
let depth = depth
435+
436+
let omit_set_verification_key_tx_version =
437+
omit_set_verification_key_tx_version
432438
end) )
433439

434440
let load ~proof_level ~genesis_dir ~logger ~constraint_constants

src/lib/merkle_ledger/any_ledger.ml

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,8 @@ module Make_base (Inputs : Inputs_intf) :
134134

135135
let get_at_index_exn (T ((module Base), t)) = Base.get_at_index_exn t
136136

137-
let set_batch (T ((module Base), t)) = Base.set_batch t
137+
let set_batch ?omit_set_verification_key_tx_version (T ((module Base), t)) =
138+
Base.set_batch ?omit_set_verification_key_tx_version t
138139

139140
let set (T ((module Base), t)) = Base.set t
140141

@@ -188,7 +189,9 @@ module Make_base (Inputs : Inputs_intf) :
188189
let set_all_accounts_rooted_at_exn (T ((module Base), t)) =
189190
Base.set_all_accounts_rooted_at_exn t
190191

191-
let set_batch_accounts (T ((module Base), t)) = Base.set_batch_accounts t
192+
let set_batch_accounts ?omit_set_verification_key_tx_version
193+
(T ((module Base), t)) =
194+
Base.set_batch_accounts ?omit_set_verification_key_tx_version t
192195

193196
let set_inner_hash_at_addr_exn (T ((module Base), t)) =
194197
Base.set_inner_hash_at_addr_exn t

src/lib/merkle_ledger/base_ledger_intf.ml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,11 @@ module type S = sig
117117

118118
val set : t -> Location.t -> account -> unit
119119

120-
val set_batch : t -> (Location.t * account) list -> unit
120+
val set_batch :
121+
?omit_set_verification_key_tx_version:bool
122+
-> t
123+
-> (Location.t * account) list
124+
-> unit
121125

122126
val get_at_index_exn : t -> int -> account
123127

src/lib/merkle_ledger/intf.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ module type Hash = sig
100100

101101
val merge : height:int -> t -> t -> t
102102

103-
val hash_account : account -> t
103+
val hash_account : ?omit_set_verification_key_tx_version:bool -> account -> t
104104

105105
val empty_account : t
106106
end

src/lib/merkle_ledger/null_ledger.ml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,8 @@ end = struct
103103

104104
let get_at_index_exn _t = failwith "get_at_index_exn: null ledgers are empty"
105105

106-
let set_batch _t = failwith "set_batch: null ledgers cannot be mutated"
106+
let set_batch ?omit_set_verification_key_tx_version:_ _t =
107+
failwith "set_batch: null ledgers cannot be mutated"
107108

108109
let set _t = failwith "set: null ledgers cannot be mutated"
109110

@@ -165,7 +166,7 @@ end = struct
165166
let set_all_accounts_rooted_at_exn _t =
166167
failwith "set_all_accounts_rooted_at_exn: null ledgers cannot be mutated"
167168

168-
let set_batch_accounts _t =
169+
let set_batch_accounts ?omit_set_verification_key_tx_version:_ _t =
169170
failwith "set_batch_accounts: null ledgers cannot be mutated"
170171

171172
let set_inner_hash_at_addr_exn _t =

src/lib/merkle_ledger/syncable_intf.ml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,11 @@ module type S = sig
2323

2424
val set_all_accounts_rooted_at_exn : t -> addr -> account list -> unit
2525

26-
val set_batch_accounts : t -> (addr * account) list -> unit
26+
val set_batch_accounts :
27+
?omit_set_verification_key_tx_version:bool
28+
-> t
29+
-> (addr * account) list
30+
-> unit
2731

2832
(** Get all of the accounts that are in a subtree of the underlying Merkle
2933
tree rooted at `address`. The accounts are ordered by their addresses. *)

src/lib/merkle_ledger/util.ml

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -59,10 +59,16 @@ module Make (Inputs : Inputs_intf) : sig
5959
Inputs.Base.t -> (Inputs.Location.t * Inputs.Hash.t) list -> unit
6060

6161
val set_batch :
62-
Inputs.Base.t -> (Inputs.Location.t * Inputs.Account.t) list -> unit
62+
?omit_set_verification_key_tx_version:bool
63+
-> Inputs.Base.t
64+
-> (Inputs.Location.t * Inputs.Account.t) list
65+
-> unit
6366

6467
val set_batch_accounts :
65-
Inputs.Base.t -> (Inputs.Location.Addr.t * Inputs.Account.t) list -> unit
68+
?omit_set_verification_key_tx_version:bool
69+
-> Inputs.Base.t
70+
-> (Inputs.Location.Addr.t * Inputs.Account.t) list
71+
-> unit
6672

6773
val set_all_accounts_rooted_at_exn :
6874
Inputs.Base.t -> Inputs.Location.Addr.t -> Inputs.Account.t list -> unit
@@ -188,16 +194,18 @@ end = struct
188194

189195
(* TODO: When we do batch on a database, we should add accounts, locations and hashes
190196
simulatenously for full atomicity. *)
191-
let set_batch t locations_and_accounts =
197+
let set_batch ?omit_set_verification_key_tx_version t locations_and_accounts =
192198
set_raw_addresses t locations_and_accounts ;
193199
Inputs.set_raw_account_batch t locations_and_accounts ;
194200
set_hash_batch t
195201
@@ List.map locations_and_accounts ~f:(fun (location, account) ->
196202
( Inputs.location_of_hash_addr (Inputs.Location.to_path_exn location)
197-
, Inputs.Hash.hash_account account ) )
203+
, Inputs.Hash.hash_account ?omit_set_verification_key_tx_version
204+
account ) )
198205

199-
let set_batch_accounts t addresses_and_accounts =
200-
set_batch t
206+
let set_batch_accounts ?omit_set_verification_key_tx_version t
207+
addresses_and_accounts =
208+
set_batch ?omit_set_verification_key_tx_version t
201209
@@ List.map addresses_and_accounts ~f:(fun (addr, account) ->
202210
(Inputs.location_of_account_addr addr, account) )
203211

0 commit comments

Comments
 (0)