https://gitlab.com/tezos/tezos
Raw File
Tip revision: 92764979115e85c11e7e01985e5eee5c17a7cca8 authored by Ole Krüger on 13 February 2024, 12:09:41 UTC
RISC-V: Fix DynRegion length check
Tip revision: 9276497
format_baker_accounts.ml
(*****************************************************************************)
(*                                                                           *)
(* Open Source License                                                       *)
(* Copyright (c) 2023 Trili Tech <contact@trili.tech>                        *)
(*                                                                           *)
(* Permission is hereby granted, free of charge, to any person obtaining a   *)
(* copy of this software and associated documentation files (the "Software"),*)
(* to deal in the Software without restriction, including without limitation *)
(* the rights to use, copy, modify, merge, publish, distribute, sublicense,  *)
(* and/or sell copies of the Software, and to permit persons to whom the     *)
(* Software is furnished to do so, subject to the following conditions:      *)
(*                                                                           *)
(* The above copyright notice and this permission notice shall be included   *)
(* in all copies or substantial portions of the Software.                    *)
(*                                                                           *)
(* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR*)
(* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,  *)
(* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL   *)
(* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER*)
(* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING   *)
(* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER       *)
(* DEALINGS IN THE SOFTWARE.                                                 *)
(*                                                                           *)
(*****************************************************************************)

type t = {baker : string; key : string}

let default_entries =
  "    is_bootstrap_baker_account: true\n\
  \    bootstrap_balance: \"40000000000000\"\n"

let format_entry baker key =
  Format.sprintf "  %s:\n    key: %s\n%s" baker key default_entries

let encoding =
  let open Data_encoding in
  list
    (conv
       (fun {baker; key} -> (baker, key))
       (fun (baker, key) -> {baker; key})
       (obj2 (req "name" string) (req "value" string)))

let secret_keys_file = "secret_keys"

let output_file = "accounts_template.yaml"

(* Function that transforms the "secret_keys" file generated by the
   [generate_baker_accounts] function into a usable format for the
   "core-blockchain-replay/charts/tezos/values.yaml" file.
   Example:

   - input:
   [{ "name": "baker_1",
     "value":
       "unencrypted:edsk32nV4ERjJbQ7Kmfxuij7pb6oQvTGei1dQrZNZNJohNRWEDvHAU" }]

   - output:
   accounts:
     baker_1:
       key: edsk4C4dY96gMZLz4r3L3h1sZP1eN5Dsv1NQpLVcCiv4V5AACK4ByH
       is_bootstrap_baker_account: true
       bootstrap_balance: "40000000000000" *)
let format_baker_accounts client_dir =
  let input_file = Filename.concat client_dir secret_keys_file in
  let* json =
    if Sys.file_exists input_file then Lwt_utils_unix.Json.read_file input_file
    else Test.fail "The %s file does not exist." input_file
  in
  let json_encoding =
    match json with
    | Ok json_encoding -> json_encoding
    | _ -> Test.fail "Wrong input file."
  in
  let bakers_list = Data_encoding.Json.destruct encoding json_encoding in
  let bakers_output =
    String.concat ""
    @@ List.map
         (fun {baker; key} ->
           format_entry baker (Uri.path @@ Uri.of_string key))
         bakers_list
  in
  let output = "accounts:\n" ^ bakers_output in
  let* res =
    Lwt_utils_unix.with_atomic_open_out
      ~overwrite:true
      (Filename.concat client_dir output_file)
    @@ fun chan -> Lwt_utils_unix.write_string chan output
  in
  match res with
  | Ok () -> Lwt.return_unit
  | Error _ -> Test.fail "Failed to write to output directory."
back to top