|
| 1 | +open Async |
| 2 | +open Core |
| 3 | +open Mina_automation |
| 4 | +open Mina_automation_runner |
| 5 | +open Mina_automation_fixture.Archive |
| 6 | + |
| 7 | +(** |
| 8 | + * Test the basic functionality of the mina archive with mocked deamon |
| 9 | + *) |
| 10 | + |
| 11 | +(* asserts count of archive blocked (we are skipping genesis block) *) |
| 12 | +let assert_archived_blocks ~archive_uri ~expected = |
| 13 | + let connection = Psql.Conn_str archive_uri in |
| 14 | + let%bind actual_blocks_count = |
| 15 | + Psql.run_command ~connection |
| 16 | + "SELECT COUNT(*) FROM blocks WHERE global_slot_since_genesis > 1" |
| 17 | + in |
| 18 | + let actual_blocks_count = |
| 19 | + match actual_blocks_count with |
| 20 | + | Ok count -> |
| 21 | + count |> Int.of_string |
| 22 | + | Error err -> |
| 23 | + failwith ("Failed to query blocks count: " ^ Error.to_string_hum err) |
| 24 | + in |
| 25 | + if Int.( <> ) actual_blocks_count expected then |
| 26 | + failwithf "Invalid number of archive blocks. Actual (%d) vs Expected (%d)" |
| 27 | + actual_blocks_count expected () |
| 28 | + else Deferred.unit |
| 29 | + |
| 30 | +module ArchivePrecomputedBlocksFromDaemon = struct |
| 31 | + type t = Mina_automation_fixture.Archive.after_bootstrap |
| 32 | + |
| 33 | + let test_case (test_data : t) = |
| 34 | + let open Deferred.Let_syntax in |
| 35 | + let daemon = Daemon.default in |
| 36 | + let archive_uri = test_data.archive.config.postgres_uri in |
| 37 | + let output = test_data.temp_dir in |
| 38 | + let%bind precomputed_blocks = |
| 39 | + Network_data.untar_precomputed_blocks test_data.network_data output |
| 40 | + in |
| 41 | + let precomputed_blocks = |
| 42 | + List.map precomputed_blocks ~f:(fun file -> output ^ "/" ^ file) |
| 43 | + |> List.filter ~f:(fun file -> String.is_suffix file ~suffix:".json") |
| 44 | + in |
| 45 | + Archive.Process.start_logging test_data.archive ; |
| 46 | + let%bind () = |
| 47 | + Daemon.archive_blocks_from_files daemon |
| 48 | + ~archive_address:test_data.archive.config.server_port |
| 49 | + ~format:Archive_blocks.Precomputed precomputed_blocks |
| 50 | + in |
| 51 | + |
| 52 | + let%bind () = |
| 53 | + assert_archived_blocks ~archive_uri |
| 54 | + ~expected:(List.length precomputed_blocks) |
| 55 | + in |
| 56 | + let connection = Psql.Conn_str archive_uri in |
| 57 | + let%bind latest_state_hash = |
| 58 | + Psql.run_command ~connection |
| 59 | + "SELECT state_hash FROM blocks ORDER BY id DESC LIMIT 1" |
| 60 | + in |
| 61 | + let latest_state_hash = |
| 62 | + match latest_state_hash with |
| 63 | + | Ok hash -> |
| 64 | + hash |
| 65 | + | Error err -> |
| 66 | + failwith |
| 67 | + ("Failed to query latest state hash: " ^ Error.to_string_hum err) |
| 68 | + in |
| 69 | + let output_ledger = output ^ "/output_ledger.json" in |
| 70 | + let replayer = Replayer.default in |
| 71 | + let%bind replayer_output = |
| 72 | + Replayer.run replayer ~archive_uri |
| 73 | + ~input_config: |
| 74 | + (Network_data.replayer_input_file_path test_data.network_data) |
| 75 | + ~target_state_hash:latest_state_hash ~interval_checkpoint:10 |
| 76 | + ~output_ledger () |
| 77 | + in |
| 78 | + let () = print_endline replayer_output in |
| 79 | + let output_ledger = Replayer.Output.of_json_file_exn output_ledger in |
| 80 | + assert ( |
| 81 | + String.equal output_ledger.target_epoch_ledgers_state_hash |
| 82 | + latest_state_hash ) ; |
| 83 | + Deferred.Or_error.return Mina_automation_fixture.Intf.Passed |
| 84 | +end |
| 85 | + |
| 86 | +let () = |
| 87 | + Backtrace.elide := false ; |
| 88 | + Async.Scheduler.set_record_backtraces true |
| 89 | + |
| 90 | +let () = |
| 91 | + let open Alcotest in |
| 92 | + run "Test archive node." |
| 93 | + [ ( "precomputed blocks" |
| 94 | + , [ test_case "The mina daemon works in background mode" `Quick |
| 95 | + (Runner.run_blocking |
| 96 | + ( module Mina_automation_fixture.Archive.Make_FixtureWithBootstrap |
| 97 | + (ArchivePrecomputedBlocksFromDaemon) ) ) |
| 98 | + ] ) |
| 99 | + ] |
0 commit comments