Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/lk86/update-docker-entrypoint-ma…
Browse files Browse the repository at this point in the history
…ster' into lk86/update-docker-entrypoint-rosetta
  • Loading branch information
lk86 committed May 3, 2022
2 parents 26e12bd + 4f257c3 commit 529bf17
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 46 deletions.
90 changes: 54 additions & 36 deletions dockerfiles/scripts/daemon-entrypoint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,68 +7,86 @@ shopt -s nullglob

INPUT_ARGS="$@"

# These arrays can be overwritten or extended in scripts to adjust verbosity
# Example:
declare -a LOG_FILES=('mina.log')
# stderr is mostly used to print "reading password from environment varible ..."
# prover and verifier logs are also sparse, mostly memory stats and debug info
# mina-best-tip.log is useful for organizing a hard fork and is one way to monitor new blocks as they are added, but not critical
declare -a VERBOSE_LOG_FILES=('mina-stderr.log' '.mina-config/mina-prover.log' '.mina-config/mina-verifier.log' '.mina-config/mina-best-tip.log')

EXTRA_FLAGS=""
declare -a VERBOSE_LOG_FILES=('mina-stderr.log' '.mina-config/mina-prover.log' '.mina-config/mina-verifier.log')

# Attempt to execute or source custom entrypoint scripts accordingly
# Example: mount a mina-env file with variable evironment variables to source and pass to the daemon
for script in /entrypoint.d/*; do
if [ -x "$script" ]; then
"$script" $INPUT_ARGS
for script in /entrypoint.d/* /entrypoint.d/.*; do
if [[ "$( basename "${script}")" == *mina-env ]]; then
source "${script}"
elif [[ -f "${script}" ]] && [[ ! -x "${script}" ]]; then
source "${script}"
elif [[ -f "${script}" ]]; then
"${script}" $INPUT_ARGS
else
source "$script"
echo "[ERROR] Entrypoint script ${script} is not a regular file, ignoring"
fi
done

set +u # allow these variables to be unset
APPENDED_FLAGS=""

set +u # allow these variables to be unset, including EXTRA_FLAGS
# Support flags from .mina-env on debian
if [[ ${PEER_LIST_URL} ]]; then
EXTRA_FLAGS+=" --peer-list-url ${PEER_LIST_URL}"
APPENDED_FLAGS+=" --peer-list-url ${PEER_LIST_URL}"
fi
if [[ ${PEER_LIST_FILE} ]]; then
APPENDED_FLAGS+=" --peer-list-file ${PEER_LIST_FILE}"
fi
if [[ ${LOG_LEVEL} ]]; then
EXTRA_FLAGS+=" --log-level ${LOG_LEVEL}"
APPENDED_FLAGS+=" --log-level ${LOG_LEVEL}"
fi
if [[ ${FILE_LOG_LEVEL} ]]; then
EXTRA_FLAGS+=" --file-log-level ${FILE_LOG_LEVEL}"
APPENDED_FLAGS+=" --file-log-level ${FILE_LOG_LEVEL}"
fi

# If VERBOSE=true then also append other log files
# If VERBOSE=true then print daemon flags
if [[ ${VERBOSE} ]]; then
LOG_FILES+=("${VERBOSE_LOG_FILES[@]}")
# Print the flags to the daemon for debugging use
echo "[Debug] Input Args: ${INPUT_ARGS}"
echo "[Debug] Input Arguments: ${INPUT_ARGS}"
echo "[Debug] Extra Flags: ${EXTRA_FLAGS}"
echo "[Debug] Dynamically Appended Flags: ${APPENDED_FLAGS}"
fi

set -u

# Mina daemon initialization
mkdir -p .mina-config
# Create all of the log files that we will tail later
touch "${LOG_FILES[@]}"

set +e # Allow wait and kill commands to fail in this loop
while true; do
rm -f .mina-config/.mina-lock
mina $INPUT_ARGS $EXTRA_FLAGS 2>mina-stderr.log 1>mina.log &
mina_pid=$!
set +e # Allow remaining commands to fail without exiting early
rm -f .mina-config/.mina-lock

# Export variables that the daemon would read directly
export MINA_PRIVKEY_PASS MINA_LIBP2P_PASS UPTIME_PRIVKEY_PASS

tail -q -f "${LOG_FILES[@]}" &
tail_pid=$!
# Run the daemon in the foreground
mina ${INPUT_ARGS} ${EXTRA_FLAGS} ${APPENDED_FLAGS} 2>mina-stderr.log
export MINA_EXIT_CODE="$?"
echo "Mina process exited with status code ${MINA_EXIT_CODE}"

wait "$mina_pid"
echo "Mina process exited with status code $?"
sleep 15
kill "$tail_pid"
# Don't export variables to exitpoint scripts
export -n MINA_PRIVKEY_PASS MINA_LIBP2P_PASS UPTIME_PRIVKEY_PASS

if [ ! -f stay_alive ]; then
exit 0
# Attempt to execute or source custom EXITpoint scripts
# Example: `mina client export-local-logs > ~/.mina-config/log-exports/blah`
# to export logs every time the daemon shuts down
for script in /exitpoint.d/*; do
if [[ -f "${script}" ]] && [[ -x "${script}" ]]; then
"${script}"
else
echo "[ERROR] Exitpoint script ${script} is not an executable regular file, ignoring"
fi
done

# TODO: have a better way to intersperse log files like we used to, without infinite disk use
# For now, tail the last 20 lines of the verbose log files when the node shuts down
if [[ ${VERBOSE} ]]; then
# Tail verbose log files
tail -n 20 "${VERBOSE_LOG_FILES[@]}"
# Proccess .mina-config/mina-best-tip.log with jq for a short display of the last 5 blocks that the node considered "best tip"
CONSENSUS_STATE='.protocol_state.body.consensus_state'
tail -n 5 ".mina-config/mina-best-tip.log" | jq -rc '.metadata.added_transitions[0] | {state_hash: .state_hash, height: '${CONSENSUS_STATE}'.blockchain_length, slot: '${CONSENSUS_STATE}'.global_slot_since_genesis}'
fi

sleep 15 # to allow all mina proccesses to quit, cleanup, and finish logging

exit ${MINA_EXIT_CODE}
19 changes: 15 additions & 4 deletions src/lib/cache_lib/impl.ml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ module Make (Inputs : Inputs_intf) : Intf.Main.S = struct
{ name : string
; on_add : 'a -> unit
; on_remove : [ `Consumed | `Unconsumed | `Failure ] -> 'a -> unit
; element_to_string : 'a -> string
; set : ('a, 'a Intf.final_state) Hashtbl.t
; logger : Logger.t
}
Expand All @@ -28,11 +29,11 @@ module Make (Inputs : Inputs_intf) : Intf.Main.S = struct

let logger { logger; _ } = logger

let create (type elt) ~name ~logger ~on_add ~on_remove
let create (type elt) ~name ~logger ~on_add ~on_remove ~element_to_string
(module Elt : Hashtbl.Key_plain with type t = elt) : elt t =
let set = Hashtbl.create ~growth_allowed:true ?size:None (module Elt) in
let logger = Logger.extend logger [ ("cache", `String name) ] in
{ name; on_add; on_remove; set; logger }
{ name; on_add; on_remove; element_to_string; set; logger }

let final_state t x = Hashtbl.find t.set x

Expand All @@ -42,6 +43,8 @@ module Make (Inputs : Inputs_intf) : Intf.Main.S = struct
t.on_add x ;
Cached.create t x final_state

let element_to_string t = t.element_to_string

let mem t x = Hashtbl.mem t.set x

let remove t reason x = Hashtbl.remove t.set x ; t.on_remove reason x
Expand Down Expand Up @@ -169,11 +172,17 @@ module Make (Inputs : Inputs_intf) : Intf.Main.S = struct

let assert_not_consumed t msg =
let open Error in
if was_consumed t then raise (of_string msg)
if was_consumed t then
raise
(createf "%s: %s" msg
(Cache.element_to_string (cache t) (original t)))

let assert_not_finalized t msg =
let open Error in
if was_finalized t then raise (of_string msg)
if was_finalized t then
raise
(createf "%s: %s" msg
(Cache.element_to_string (cache t) (original t)))

let peek (type a b) (t : (a, b) t) : a =
assert_not_finalized t "cannot peek at finalized Cached.t" ;
Expand Down Expand Up @@ -251,6 +260,7 @@ module Make (Inputs : Inputs_intf) : Intf.Main.S = struct
let create ~logger =
Cache.create ~logger ~name:Name.t ~on_add:Registry.element_added
~on_remove:Registry.element_removed
~element_to_string:Transmuter.Target.to_string
(module Transmuter.Target)

let register_exn t x =
Expand Down Expand Up @@ -283,6 +293,7 @@ let%test_module "cache_lib test instance" =
let with_cache ~logger ~f =
Cache.create ~name:"test" ~logger ~on_add:ignore
~on_remove:(fun _ _ -> ())
~element_to_string:Fn.id
(module String)
|> f

Expand Down
9 changes: 8 additions & 1 deletion src/lib/cache_lib/intf.ml
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ module Cache = struct
-> logger:Logger.t
-> on_add:('elt -> unit)
-> on_remove:([ `Consumed | `Unconsumed | `Failure ] -> 'elt -> unit)
-> element_to_string:('elt -> string)
-> (module Hashtbl.Key_plain with type t = 'elt)
-> 'elt t

Expand All @@ -126,6 +127,8 @@ module Cache = struct

val final_state : 'elt t -> 'elt -> 'elt final_state Option.t

val element_to_string : 'elt t -> 'elt -> string

val to_list : 'elt t -> 'elt list
end
end
Expand All @@ -139,7 +142,11 @@ module Transmuter = struct
type t
end

module Target : Hash_set.Elt_plain
module Target : sig
include Hash_set.Elt_plain

val to_string : t -> string
end

val transmute : Source.t -> Target.t
end
Expand Down
7 changes: 3 additions & 4 deletions src/lib/transition_frontier/full_catchup_tree.ml
Original file line number Diff line number Diff line change
Expand Up @@ -286,10 +286,9 @@ let remove_node' t (node : Node.t) =
match node.state with
| Root _ | Failed | Finished ->
()
| Wait_for_parent c ->
ignore
( Cached.invalidate_with_failure c
: External_transition.Almost_validated.t Envelope.Incoming.t )
| Wait_for_parent _ ->
(* cache invalidation for this case is handled explicitly in the super catchup fstm *)
()
| To_download _job ->
(* TODO: Cancel job somehow *)
()
Expand Down
6 changes: 5 additions & 1 deletion src/lib/transition_handler/unprocessed_transition_cache.ml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,11 @@ module Transmuter = struct
type t = External_transition.Initial_validated.t Envelope.Incoming.t
end

module Target = State_hash
module Target = struct
include State_hash

let to_string = to_base58_check
end

let transmute enveloped_transition =
let transition, _ = Envelope.Incoming.data enveloped_transition in
Expand Down

0 comments on commit 529bf17

Please sign in to comment.