igniter_lang 0.1.0.alpha.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,80 @@
1
+ # frozen_string_literal: true
2
+
3
+ module IgniterLang
4
+ module RuntimeSmoke
5
+ begin
6
+ require_relative "../../experiments/runtime_machine_memory_proof/compiled_program"
7
+ PROOF_LOAD_ERROR = nil
8
+ rescue LoadError => e
9
+ PROOF_LOAD_ERROR = e
10
+ end
11
+
12
+ DEFAULT_AS_OF = defined?(RuntimeMachineMemoryProof::PROOF_AS_OF) ? RuntimeMachineMemoryProof::PROOF_AS_OF : nil
13
+ DEFAULT_MACHINE_ID = "runtime-machine/production-compiler-cli"
14
+ DEFAULT_SESSION_ID = "session/production-compiler-cli"
15
+ DEFAULT_RULE_VERSION = "production-compiler-cli-wrapper-v0"
16
+
17
+ module_function
18
+
19
+ def run(out_path:, sample_input:, as_of: DEFAULT_AS_OF, machine_id: DEFAULT_MACHINE_ID,
20
+ session_id: DEFAULT_SESSION_ID, rule_version: DEFAULT_RULE_VERSION)
21
+ ensure_available!
22
+ program = RuntimeMachineMemoryProof::CompiledProgram.load_igapp(out_path)
23
+ program.validate!
24
+ contract_id = program.contracts.keys.fetch(0)
25
+ backend = RuntimeMachineMemoryProof::MemoryTBackend.new
26
+ machine = RuntimeMachineMemoryProof::RuntimeMachine.new(
27
+ machine_id: machine_id,
28
+ session_id: session_id,
29
+ backend: backend
30
+ )
31
+ machine.boot
32
+ load = machine.load_program(program)
33
+ evaluation = machine.evaluate_program(contract_id, eval_input_for(contract_id, sample_input), as_of: as_of)
34
+ checkpoint = machine.checkpoint(horizon: { as_of: as_of, rule_version: rule_version })
35
+ resume = machine.resume(image: checkpoint.fetch(:semantic_image), requested_as_of: as_of)
36
+
37
+ {
38
+ "load_status" => load.fetch(:status),
39
+ "contract_id" => contract_id,
40
+ "evaluate_status" => evaluation.fetch(:status),
41
+ "outputs" => evaluation.fetch(:outputs),
42
+ "compatibility_report_status" => resume.fetch(:status),
43
+ "trusted" => load.fetch(:status) == "loaded" &&
44
+ evaluation.fetch(:status) == "ok" &&
45
+ resume.fetch(:status) == "trusted"
46
+ }
47
+ rescue => e
48
+ {
49
+ "load_status" => "blocked",
50
+ "error" => "#{e.class}: #{e.message}",
51
+ "trusted" => false
52
+ }
53
+ end
54
+
55
+ def callback(**options)
56
+ lambda do |out_path:, sample_input:|
57
+ run(out_path: out_path, sample_input: sample_input, **options)
58
+ end
59
+ end
60
+
61
+ def eval_input_for(contract_id, sample_input)
62
+ return { "a" => 19, "b" => 23 } if contract_id == "Add"
63
+
64
+ sample_input
65
+ end
66
+
67
+ def available?
68
+ defined?(RuntimeMachineMemoryProof::CompiledProgram) &&
69
+ defined?(RuntimeMachineMemoryProof::RuntimeMachine)
70
+ end
71
+
72
+ def ensure_available!
73
+ return if available?
74
+
75
+ message = "IgniterLang::RuntimeSmoke is proof-backed; runtime_machine_memory_proof is unavailable in this package context"
76
+ message = "#{message}: #{PROOF_LOAD_ERROR.message}" if PROOF_LOAD_ERROR
77
+ raise LoadError, message
78
+ end
79
+ end
80
+ end