moonproof 0.2.1-aarch64-linux-gnu
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.
- checksums.yaml +7 -0
- data/lib/moonproof/libmoonproof_ffi.so +4 -0
- data/lib/moonproof/native.rb +48419 -0
- data/lib/moonproof/sdk.rb +696 -0
- data/lib/moonproof.rb +2 -0
- metadata +61 -0
|
@@ -0,0 +1,696 @@
|
|
|
1
|
+
# Idiomatic Ruby facade layered over the generated UniFFI bindings (`native.rb`).
|
|
2
|
+
#
|
|
3
|
+
# Reopens Moonproof, Moonproof::Artifact, Moonproof::Chain, and Moonproof::Contract
|
|
4
|
+
# so Ruby users get native Ruby value coercion, block-scoped `batch` / `with_snapshot`
|
|
5
|
+
# / `with_prank` / `with_witness`, stateful invariant fuzzing DSL, and clean keyword
|
|
6
|
+
# arguments while keeping all generated symbols available.
|
|
7
|
+
|
|
8
|
+
module Moonproof
|
|
9
|
+
NIGHT = night_token
|
|
10
|
+
|
|
11
|
+
class << self
|
|
12
|
+
# Load a compiled contract artifact from `.compact`, `managed/<name>/`, or `runtime-ir.cbor`.
|
|
13
|
+
def load(path)
|
|
14
|
+
load_artifact(path)
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
# Connect to a configured network (defaults to MOONPROOF_NETWORK / moonproof.toml).
|
|
18
|
+
def connect(network = nil, wallet = nil, project_root: nil)
|
|
19
|
+
if project_root
|
|
20
|
+
connect_at_sync(project_root, network, wallet)
|
|
21
|
+
else
|
|
22
|
+
connect_sync(network, wallet)
|
|
23
|
+
end
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
# Open the chain selected by MOONPROOF_CHAIN (simulator by default).
|
|
27
|
+
def chain_from_env
|
|
28
|
+
chain_from_env_sync
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
# Connect directly to a live Midnight network without requiring moonproof.toml.
|
|
32
|
+
def connect_endpoint(
|
|
33
|
+
indexer_uri:,
|
|
34
|
+
node_ws_url:,
|
|
35
|
+
network_id: "undeployed",
|
|
36
|
+
proof_server_url: nil,
|
|
37
|
+
wallet_mnemonic: nil,
|
|
38
|
+
wallet_seed_hex: nil
|
|
39
|
+
)
|
|
40
|
+
cfg = NetworkEndpointConfig.new(
|
|
41
|
+
indexer_uri: indexer_uri,
|
|
42
|
+
node_ws_url: node_ws_url,
|
|
43
|
+
network_id: network_id,
|
|
44
|
+
proof_server_url: proof_server_url,
|
|
45
|
+
wallet_mnemonic: wallet_mnemonic,
|
|
46
|
+
wallet_seed_hex: wallet_seed_hex
|
|
47
|
+
)
|
|
48
|
+
connect_endpoint_sync(cfg)
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
# Build an off-chain Compact MerkleTreePath<depth, Bytes<32>> proof.
|
|
52
|
+
def compact_merkle_path(depth, leaves, leaf_index)
|
|
53
|
+
compact_merkle_tree_path_bytes32(depth, leaves, leaf_index)
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
# Decode and structurally inspect a hex- or byte-encoded ProvenTx.
|
|
57
|
+
def inspect_proven_tx(tx)
|
|
58
|
+
if tx.encoding == Encoding::ASCII_8BIT
|
|
59
|
+
inspect_proven_tx_bytes(tx)
|
|
60
|
+
else
|
|
61
|
+
inspect_proven_tx_hex(tx)
|
|
62
|
+
end
|
|
63
|
+
end
|
|
64
|
+
|
|
65
|
+
# Convert a native Ruby value into a Moonproof::RuntimeValue.
|
|
66
|
+
def to_runtime_value(value)
|
|
67
|
+
return value if value.is_a?(RuntimeValue)
|
|
68
|
+
|
|
69
|
+
case value
|
|
70
|
+
when nil
|
|
71
|
+
RuntimeValue::NULL.new
|
|
72
|
+
when TrueClass, FalseClass
|
|
73
|
+
RuntimeValue::BOOL.new(value: value)
|
|
74
|
+
when Integer
|
|
75
|
+
RuntimeValue::INT.new(value: value.to_s)
|
|
76
|
+
when String
|
|
77
|
+
if value.encoding == Encoding::ASCII_8BIT
|
|
78
|
+
RuntimeValue::BYTES.new(value: value)
|
|
79
|
+
else
|
|
80
|
+
RuntimeValue::STR.new(value: value)
|
|
81
|
+
end
|
|
82
|
+
when Array
|
|
83
|
+
RuntimeValue::ARRAY.new(items: value.map { |v| to_runtime_value(v) })
|
|
84
|
+
when Hash
|
|
85
|
+
RuntimeValue::OBJECT.new(
|
|
86
|
+
entries: value.map { |k, v| RuntimeValueEntry.new(key: k.to_s, value: to_runtime_value(v)) }
|
|
87
|
+
)
|
|
88
|
+
else
|
|
89
|
+
raise TypeError, "cannot convert #{value.class} to Moonproof::RuntimeValue"
|
|
90
|
+
end
|
|
91
|
+
end
|
|
92
|
+
|
|
93
|
+
# Convert a Moonproof::RuntimeValue into a native Ruby value.
|
|
94
|
+
def from_runtime_value(rv)
|
|
95
|
+
case rv
|
|
96
|
+
when RuntimeValue::NULL then nil
|
|
97
|
+
when RuntimeValue::BOOL then rv.value
|
|
98
|
+
when RuntimeValue::INT then rv.value.to_i
|
|
99
|
+
when RuntimeValue::BYTES then rv.value
|
|
100
|
+
when RuntimeValue::STR then rv.value
|
|
101
|
+
when RuntimeValue::ARRAY then rv.items.map { |i| from_runtime_value(i) }
|
|
102
|
+
when RuntimeValue::OBJECT
|
|
103
|
+
rv.entries.each_with_object({}) { |e, h| h[e.key] = from_runtime_value(e.value) }
|
|
104
|
+
else
|
|
105
|
+
rv
|
|
106
|
+
end
|
|
107
|
+
end
|
|
108
|
+
end
|
|
109
|
+
|
|
110
|
+
class Artifact
|
|
111
|
+
alias audit static_audit
|
|
112
|
+
|
|
113
|
+
# Run the static `MP001`–`MP005` privacy audit and raise on any findings.
|
|
114
|
+
# Returns the `StaticAuditReportRecord` when clean.
|
|
115
|
+
def assert_privacy_clean!
|
|
116
|
+
assert_privacy_clean
|
|
117
|
+
end
|
|
118
|
+
end
|
|
119
|
+
|
|
120
|
+
class StaticAuditReportRecord
|
|
121
|
+
def clean? = @clean
|
|
122
|
+
end
|
|
123
|
+
|
|
124
|
+
class ContractStateDiffRecord
|
|
125
|
+
def empty? = @is_empty
|
|
126
|
+
end
|
|
127
|
+
|
|
128
|
+
class InvariantReportDto
|
|
129
|
+
def passed? = @passed
|
|
130
|
+
def ok? = @passed
|
|
131
|
+
end
|
|
132
|
+
|
|
133
|
+
class ActionRecordDto
|
|
134
|
+
def reverted? = @reverted
|
|
135
|
+
end
|
|
136
|
+
|
|
137
|
+
class WitnessCallRecord
|
|
138
|
+
def args_value = Moonproof.from_runtime_value(@args)
|
|
139
|
+
def observed_value = Moonproof.from_runtime_value(@observed)
|
|
140
|
+
def next_private_state_value = Moonproof.from_runtime_value(@next_private_state)
|
|
141
|
+
end
|
|
142
|
+
|
|
143
|
+
# Fluent / block-DSL builder for stateful invariant fuzzing + shrinking.
|
|
144
|
+
class InvariantSuite
|
|
145
|
+
DEFAULT_RUNS = 32
|
|
146
|
+
DEFAULT_DEPTH = 16
|
|
147
|
+
DEFAULT_SEED = 0x5EED_0001
|
|
148
|
+
DEFAULT_MAX_SHRINK_STEPS = 64
|
|
149
|
+
|
|
150
|
+
def initialize(chain, runs: nil, depth: nil, seed: nil, max_shrink_steps: nil)
|
|
151
|
+
@chain = chain
|
|
152
|
+
@runs = runs
|
|
153
|
+
@depth = depth
|
|
154
|
+
@seed = seed
|
|
155
|
+
@max_shrink_steps = max_shrink_steps
|
|
156
|
+
@actions = []
|
|
157
|
+
@invariants = []
|
|
158
|
+
end
|
|
159
|
+
|
|
160
|
+
# Register a weighted stateful action. Raising `StandardError` counts as an
|
|
161
|
+
# expected revert (ignored during fuzzing and stripped during shrinking).
|
|
162
|
+
def action(name, weight: 1, &block)
|
|
163
|
+
raise ArgumentError, "action `#{name}` requires a block" unless block
|
|
164
|
+
|
|
165
|
+
@actions << { name: name.to_s, weight: weight.to_i, block: block }
|
|
166
|
+
self
|
|
167
|
+
end
|
|
168
|
+
|
|
169
|
+
# Register an invariant check. Raising an exception or returning `false`
|
|
170
|
+
# reports a violation and triggers delta-debugging shrinking.
|
|
171
|
+
def invariant(name, &block)
|
|
172
|
+
raise ArgumentError, "invariant `#{name}` requires a block" unless block
|
|
173
|
+
|
|
174
|
+
@invariants << { name: name.to_s, block: block }
|
|
175
|
+
self
|
|
176
|
+
end
|
|
177
|
+
|
|
178
|
+
# Execute the invariant fuzzing campaign and return an `InvariantReportDto`.
|
|
179
|
+
def run(runs: nil, depth: nil, seed: nil, max_shrink_steps: nil)
|
|
180
|
+
cfg = InvariantConfigRecord.new(
|
|
181
|
+
runs: (runs.nil? ? (@runs || DEFAULT_RUNS) : runs).to_i,
|
|
182
|
+
depth: (depth.nil? ? (@depth || DEFAULT_DEPTH) : depth).to_i,
|
|
183
|
+
seed: (seed.nil? ? (@seed || DEFAULT_SEED) : seed).to_i,
|
|
184
|
+
max_shrink_steps: (max_shrink_steps.nil? ? (@max_shrink_steps || DEFAULT_MAX_SHRINK_STEPS) : max_shrink_steps).to_i
|
|
185
|
+
)
|
|
186
|
+
specs = @actions.map { |a| InvariantActionSpec.new(name: a[:name], weight: a[:weight]) }
|
|
187
|
+
inv_names = @invariants.map { |i| i[:name] }
|
|
188
|
+
session = @chain.begin_invariants(specs, inv_names, cfg)
|
|
189
|
+
|
|
190
|
+
inv_errors = [StandardError]
|
|
191
|
+
inv_errors << Minitest::Assertion if defined?(Minitest::Assertion)
|
|
192
|
+
|
|
193
|
+
loop do
|
|
194
|
+
step = session.next_step_sync
|
|
195
|
+
if step.run_action?
|
|
196
|
+
action = @actions.fetch(step.action_index)
|
|
197
|
+
begin
|
|
198
|
+
res = invoke_action(action[:block], step)
|
|
199
|
+
session.submit_action_sync(true, format_action_detail(res))
|
|
200
|
+
rescue StandardError => e
|
|
201
|
+
session.submit_action_sync(false, e.message.to_s)
|
|
202
|
+
end
|
|
203
|
+
elsif step.check_invariant?
|
|
204
|
+
inv = @invariants.fetch(step.invariant_index)
|
|
205
|
+
begin
|
|
206
|
+
res = inv[:block].call
|
|
207
|
+
if res == false
|
|
208
|
+
session.submit_invariant_sync("invariant `#{step.invariant_name}` returned false")
|
|
209
|
+
else
|
|
210
|
+
session.submit_invariant_sync(nil)
|
|
211
|
+
end
|
|
212
|
+
rescue *inv_errors => e
|
|
213
|
+
msg = e.message.to_s
|
|
214
|
+
msg = "invariant `#{step.invariant_name}` raised #{e.class}" if msg.empty?
|
|
215
|
+
session.submit_invariant_sync(msg)
|
|
216
|
+
end
|
|
217
|
+
elsif step.finished?
|
|
218
|
+
return step.report
|
|
219
|
+
else
|
|
220
|
+
raise RuntimeError, "unexpected InvariantStep: #{step.inspect}"
|
|
221
|
+
end
|
|
222
|
+
end
|
|
223
|
+
end
|
|
224
|
+
|
|
225
|
+
private
|
|
226
|
+
|
|
227
|
+
def invoke_action(block, step)
|
|
228
|
+
case block.arity
|
|
229
|
+
when 0 then block.call
|
|
230
|
+
when 1, -1 then block.call(step.step_seed)
|
|
231
|
+
else block.call(step.step_seed, step.step_index)
|
|
232
|
+
end
|
|
233
|
+
end
|
|
234
|
+
|
|
235
|
+
def format_action_detail(res)
|
|
236
|
+
case res
|
|
237
|
+
when String then res
|
|
238
|
+
when Symbol, Numeric then res.to_s
|
|
239
|
+
else ""
|
|
240
|
+
end
|
|
241
|
+
end
|
|
242
|
+
end
|
|
243
|
+
|
|
244
|
+
class Chain
|
|
245
|
+
def deploy(contract, as: nil, args: nil, private_state: nil, witnesses: nil)
|
|
246
|
+
account_name = as.is_a?(Account) ? as.name : as
|
|
247
|
+
has_opts = !account_name.nil? || !args.nil? || !private_state.nil? || !witnesses.nil?
|
|
248
|
+
opts = DeployOptions.new(
|
|
249
|
+
account: account_name,
|
|
250
|
+
args: args.nil? ? nil : Moonproof.to_runtime_value(args),
|
|
251
|
+
private_state: private_state.nil? ? nil : Moonproof.to_runtime_value(private_state)
|
|
252
|
+
)
|
|
253
|
+
if contract.is_a?(Artifact)
|
|
254
|
+
has_opts ? deploy_artifact_with_sync(contract, opts, witnesses) : deploy_artifact_sync(contract)
|
|
255
|
+
else
|
|
256
|
+
has_opts ? deploy_with_sync(contract.to_s, opts, witnesses) : deploy_sync(contract.to_s)
|
|
257
|
+
end
|
|
258
|
+
end
|
|
259
|
+
|
|
260
|
+
def join(contract, address, private_state: nil, witnesses: nil)
|
|
261
|
+
ps = private_state.nil? ? nil : Moonproof.to_runtime_value(private_state)
|
|
262
|
+
has_opts = !ps.nil? || !witnesses.nil?
|
|
263
|
+
if contract.is_a?(Artifact)
|
|
264
|
+
has_opts ? join_artifact_with_sync(contract, address, ps, witnesses) : join_artifact_sync(contract, address)
|
|
265
|
+
else
|
|
266
|
+
has_opts ? join_with_sync(contract.to_s, address, ps, witnesses) : join_sync(contract.to_s, address)
|
|
267
|
+
end
|
|
268
|
+
end
|
|
269
|
+
|
|
270
|
+
def fund(holder, amount, token: nil)
|
|
271
|
+
fund_sync(resolve_holder(holder), amount.to_s, token)
|
|
272
|
+
end
|
|
273
|
+
|
|
274
|
+
def deal(holder, amount, token: nil)
|
|
275
|
+
deal_sync(resolve_holder(holder), amount.to_s, token)
|
|
276
|
+
end
|
|
277
|
+
|
|
278
|
+
def balance(holder, token: nil)
|
|
279
|
+
balance_sync(resolve_holder(holder), token).to_i
|
|
280
|
+
end
|
|
281
|
+
|
|
282
|
+
def now
|
|
283
|
+
now_sync
|
|
284
|
+
end
|
|
285
|
+
|
|
286
|
+
def advance(seconds)
|
|
287
|
+
advance_sync(seconds)
|
|
288
|
+
end
|
|
289
|
+
|
|
290
|
+
def warp(timestamp_secs)
|
|
291
|
+
warp_sync(timestamp_secs.to_i)
|
|
292
|
+
end
|
|
293
|
+
|
|
294
|
+
def zkir_check=(enabled)
|
|
295
|
+
set_zkir_check(enabled)
|
|
296
|
+
end
|
|
297
|
+
|
|
298
|
+
def zkir_check?
|
|
299
|
+
zkir_check_enabled
|
|
300
|
+
end
|
|
301
|
+
|
|
302
|
+
def prank(account)
|
|
303
|
+
prank_sync(resolve_account(account))
|
|
304
|
+
end
|
|
305
|
+
|
|
306
|
+
def start_prank(account)
|
|
307
|
+
start_prank_sync(resolve_account(account))
|
|
308
|
+
end
|
|
309
|
+
|
|
310
|
+
def stop_prank
|
|
311
|
+
stop_prank_sync
|
|
312
|
+
end
|
|
313
|
+
|
|
314
|
+
# Impersonate `account` for the duration of the block, always calling `stop_prank` in `ensure`.
|
|
315
|
+
def with_prank(account)
|
|
316
|
+
acc = resolve_account(account)
|
|
317
|
+
start_prank_sync(acc)
|
|
318
|
+
begin
|
|
319
|
+
yield acc
|
|
320
|
+
ensure
|
|
321
|
+
stop_prank_sync
|
|
322
|
+
end
|
|
323
|
+
end
|
|
324
|
+
|
|
325
|
+
def snapshot
|
|
326
|
+
snapshot_sync
|
|
327
|
+
end
|
|
328
|
+
|
|
329
|
+
def revert(snapshot_id)
|
|
330
|
+
revert_sync(snapshot_id)
|
|
331
|
+
end
|
|
332
|
+
|
|
333
|
+
def diff_since(snapshot_id)
|
|
334
|
+
diff_since_sync(snapshot_id)
|
|
335
|
+
end
|
|
336
|
+
|
|
337
|
+
def profiler_summary
|
|
338
|
+
profiler_summary_sync
|
|
339
|
+
end
|
|
340
|
+
|
|
341
|
+
def profiler_table
|
|
342
|
+
profiler_table_sync
|
|
343
|
+
end
|
|
344
|
+
|
|
345
|
+
def profiler_json
|
|
346
|
+
profiler_json_sync
|
|
347
|
+
end
|
|
348
|
+
|
|
349
|
+
def reset_profiler
|
|
350
|
+
reset_profiler_sync
|
|
351
|
+
end
|
|
352
|
+
|
|
353
|
+
def inspect_all_ledgers
|
|
354
|
+
inspect_all_ledgers_sync
|
|
355
|
+
end
|
|
356
|
+
|
|
357
|
+
# Build and optionally execute a stateful invariant fuzzing suite.
|
|
358
|
+
#
|
|
359
|
+
# With a block: evaluates the DSL (`block.arity == 1 ? yield(suite) : suite.instance_eval(&block)`)
|
|
360
|
+
# and runs immediately, returning an `InvariantReportDto`.
|
|
361
|
+
# Without a block: returns an `InvariantSuite` for fluent `.action(...).invariant(...).run(...)`.
|
|
362
|
+
def invariants(runs: nil, depth: nil, seed: nil, max_shrink_steps: nil, &block)
|
|
363
|
+
suite = InvariantSuite.new(self, runs: runs, depth: depth, seed: seed, max_shrink_steps: max_shrink_steps)
|
|
364
|
+
return suite unless block
|
|
365
|
+
|
|
366
|
+
if block.arity == 1
|
|
367
|
+
yield suite
|
|
368
|
+
else
|
|
369
|
+
suite.instance_eval(&block)
|
|
370
|
+
end
|
|
371
|
+
suite.run
|
|
372
|
+
end
|
|
373
|
+
|
|
374
|
+
# Execute a block inside an atomic batch; commits on return, aborts on exception.
|
|
375
|
+
def batch
|
|
376
|
+
batch_id = begin_batch_sync
|
|
377
|
+
begin
|
|
378
|
+
yield batch_id
|
|
379
|
+
rescue StandardError
|
|
380
|
+
abort_batch_sync(batch_id)
|
|
381
|
+
raise
|
|
382
|
+
end
|
|
383
|
+
commit_batch_sync(batch_id)
|
|
384
|
+
end
|
|
385
|
+
|
|
386
|
+
# Save a snapshot, yield, and always revert in ensure.
|
|
387
|
+
def with_snapshot
|
|
388
|
+
snap = snapshot_sync
|
|
389
|
+
begin
|
|
390
|
+
yield snap
|
|
391
|
+
ensure
|
|
392
|
+
revert_sync(snap)
|
|
393
|
+
end
|
|
394
|
+
end
|
|
395
|
+
|
|
396
|
+
private
|
|
397
|
+
|
|
398
|
+
def resolve_holder(target)
|
|
399
|
+
case target
|
|
400
|
+
when HolderRef then target
|
|
401
|
+
when Contract then target.holder
|
|
402
|
+
when ContractHandle then target.contract.holder
|
|
403
|
+
when Account then HolderRef::USER.new(address: target.user_address_hex)
|
|
404
|
+
when String then holder_for_account(target)
|
|
405
|
+
when Symbol then holder_for_account(target.to_s)
|
|
406
|
+
else raise TypeError, "unsupported holder: #{target.class}"
|
|
407
|
+
end
|
|
408
|
+
end
|
|
409
|
+
|
|
410
|
+
def resolve_account(target)
|
|
411
|
+
case target
|
|
412
|
+
when Account then target
|
|
413
|
+
when String, Symbol then account(target.to_s)
|
|
414
|
+
else raise TypeError, "unsupported account: #{target.class}"
|
|
415
|
+
end
|
|
416
|
+
end
|
|
417
|
+
end
|
|
418
|
+
|
|
419
|
+
class Contract
|
|
420
|
+
def call(circuit, *args)
|
|
421
|
+
rv_args = args.map { |a| Moonproof.to_runtime_value(a) }
|
|
422
|
+
Moonproof.from_runtime_value(call_sync(circuit.to_s, rv_args))
|
|
423
|
+
end
|
|
424
|
+
|
|
425
|
+
def tx(circuit, *args)
|
|
426
|
+
rv_args = args.map { |a| Moonproof.to_runtime_value(a) }
|
|
427
|
+
tx_sync(circuit.to_s, rv_args)
|
|
428
|
+
end
|
|
429
|
+
|
|
430
|
+
def tx_with_diff(circuit, *args)
|
|
431
|
+
rv_args = args.map { |a| Moonproof.to_runtime_value(a) }
|
|
432
|
+
call_with_diff_sync(circuit.to_s, rv_args)
|
|
433
|
+
end
|
|
434
|
+
|
|
435
|
+
def call_with_diff(circuit, args = [])
|
|
436
|
+
arg_list = args.is_a?(Array) ? args : [args]
|
|
437
|
+
rv_args = arg_list.map { |a| Moonproof.to_runtime_value(a) }
|
|
438
|
+
call_with_diff_sync(circuit.to_s, rv_args)
|
|
439
|
+
end
|
|
440
|
+
|
|
441
|
+
def set_witness(name, value)
|
|
442
|
+
set_witness_sync(name.to_s, Moonproof.to_runtime_value(value))
|
|
443
|
+
end
|
|
444
|
+
|
|
445
|
+
def set_witness_with_state(name, value, next_state)
|
|
446
|
+
set_witness_with_state_sync(
|
|
447
|
+
name.to_s,
|
|
448
|
+
Moonproof.to_runtime_value(value),
|
|
449
|
+
Moonproof.to_runtime_value(next_state)
|
|
450
|
+
)
|
|
451
|
+
end
|
|
452
|
+
|
|
453
|
+
def set_witness_sequence(name, values)
|
|
454
|
+
rv_values = values.map { |v| Moonproof.to_runtime_value(v) }
|
|
455
|
+
set_witness_sequence_sync(name.to_s, rv_values)
|
|
456
|
+
end
|
|
457
|
+
|
|
458
|
+
def remove_witness(name)
|
|
459
|
+
remove_witness_sync(name.to_s)
|
|
460
|
+
end
|
|
461
|
+
|
|
462
|
+
def clear_witnesses
|
|
463
|
+
clear_witnesses_sync
|
|
464
|
+
end
|
|
465
|
+
|
|
466
|
+
def witness_calls
|
|
467
|
+
witness_calls_sync
|
|
468
|
+
end
|
|
469
|
+
|
|
470
|
+
def clear_witness_log
|
|
471
|
+
clear_witness_log_sync
|
|
472
|
+
end
|
|
473
|
+
|
|
474
|
+
def snapshot_witnesses
|
|
475
|
+
snapshot_witnesses_sync
|
|
476
|
+
end
|
|
477
|
+
|
|
478
|
+
def revert_witnesses(snap)
|
|
479
|
+
revert_witnesses_sync(snap)
|
|
480
|
+
end
|
|
481
|
+
|
|
482
|
+
# Snapshot witnesses, apply temporary overrides (hash and/or keyword args),
|
|
483
|
+
# yield, and always revert witnesses in `ensure`.
|
|
484
|
+
def with_witness(overrides = nil, **kw_overrides)
|
|
485
|
+
merged = {}
|
|
486
|
+
if overrides.is_a?(Hash)
|
|
487
|
+
overrides.each { |k, v| merged[k.to_s] = v }
|
|
488
|
+
elsif !overrides.nil?
|
|
489
|
+
raise ArgumentError, "with_witness expects a Hash or keyword arguments, got #{overrides.class}"
|
|
490
|
+
end
|
|
491
|
+
kw_overrides.each { |k, v| merged[k.to_s] = v }
|
|
492
|
+
|
|
493
|
+
snap = snapshot_witnesses_sync
|
|
494
|
+
begin
|
|
495
|
+
merged.each { |name, val| set_witness(name, val) }
|
|
496
|
+
yield self
|
|
497
|
+
ensure
|
|
498
|
+
revert_witnesses_sync(snap)
|
|
499
|
+
end
|
|
500
|
+
end
|
|
501
|
+
|
|
502
|
+
def static_audit
|
|
503
|
+
static_audit_sync
|
|
504
|
+
end
|
|
505
|
+
|
|
506
|
+
alias audit static_audit
|
|
507
|
+
|
|
508
|
+
def assert_privacy_clean
|
|
509
|
+
assert_privacy_clean_sync
|
|
510
|
+
end
|
|
511
|
+
|
|
512
|
+
def assert_privacy_clean!
|
|
513
|
+
assert_privacy_clean_sync
|
|
514
|
+
end
|
|
515
|
+
|
|
516
|
+
def prove(circuit, *args)
|
|
517
|
+
rv_args = args.map { |a| Moonproof.to_runtime_value(a) }
|
|
518
|
+
prove_sync(circuit.to_s, rv_args)
|
|
519
|
+
end
|
|
520
|
+
|
|
521
|
+
def merkle_path(field, leaf, index: nil)
|
|
522
|
+
rv = merkle_path_sync(field.to_s, Moonproof.to_runtime_value(leaf), index)
|
|
523
|
+
rv.nil? ? nil : Moonproof.from_runtime_value(rv)
|
|
524
|
+
end
|
|
525
|
+
|
|
526
|
+
def call_in_batch(batch_id, circuit, *args)
|
|
527
|
+
rv_args = args.map { |a| Moonproof.to_runtime_value(a) }
|
|
528
|
+
call_in_batch_sync(batch_id, circuit.to_s, rv_args)
|
|
529
|
+
end
|
|
530
|
+
|
|
531
|
+
def read(field)
|
|
532
|
+
Moonproof.from_runtime_value(read_sync(field.to_s))
|
|
533
|
+
end
|
|
534
|
+
|
|
535
|
+
def state
|
|
536
|
+
Moonproof.from_runtime_value(state_sync)
|
|
537
|
+
end
|
|
538
|
+
|
|
539
|
+
def private_state
|
|
540
|
+
Moonproof.from_runtime_value(private_state_sync)
|
|
541
|
+
end
|
|
542
|
+
|
|
543
|
+
def public_state
|
|
544
|
+
public_state_sync
|
|
545
|
+
end
|
|
546
|
+
|
|
547
|
+
def exposes?(secret)
|
|
548
|
+
case secret
|
|
549
|
+
when Integer
|
|
550
|
+
exposes_int_sync(secret.to_s)
|
|
551
|
+
when String
|
|
552
|
+
if secret.encoding == Encoding::ASCII_8BIT
|
|
553
|
+
exposes_bytes_sync(secret)
|
|
554
|
+
else
|
|
555
|
+
exposes_string_sync(secret)
|
|
556
|
+
end
|
|
557
|
+
else
|
|
558
|
+
raise TypeError, "secret must be Integer or String"
|
|
559
|
+
end
|
|
560
|
+
end
|
|
561
|
+
end
|
|
562
|
+
|
|
563
|
+
# Declarative hash/block-backed Witnesses implementation with native Ruby coercion.
|
|
564
|
+
class WitnessMap < Witnesses
|
|
565
|
+
def initialize(handlers = {})
|
|
566
|
+
@handlers = handlers.transform_keys(&:to_s)
|
|
567
|
+
end
|
|
568
|
+
|
|
569
|
+
def on(name, &block)
|
|
570
|
+
@handlers[name.to_s] = block
|
|
571
|
+
self
|
|
572
|
+
end
|
|
573
|
+
|
|
574
|
+
def invoke(witness_name, args, private_state)
|
|
575
|
+
handler = @handlers[witness_name]
|
|
576
|
+
raise ContractRuntimeError::Runtime.new(details: "unexpected witness `#{witness_name}`") unless handler
|
|
577
|
+
|
|
578
|
+
res = if handler.respond_to?(:call)
|
|
579
|
+
case handler.arity
|
|
580
|
+
when 0 then handler.call
|
|
581
|
+
when 1 then handler.call(Moonproof.from_runtime_value(args))
|
|
582
|
+
else handler.call(Moonproof.from_runtime_value(args), Moonproof.from_runtime_value(private_state))
|
|
583
|
+
end
|
|
584
|
+
else
|
|
585
|
+
handler
|
|
586
|
+
end
|
|
587
|
+
|
|
588
|
+
return res if res.is_a?(WitnessInvocation)
|
|
589
|
+
return WitnessInvocation.new(observed: res.runtime_value, next_private_state: private_state) if res.is_a?(CompactMerklePathProof)
|
|
590
|
+
|
|
591
|
+
WitnessInvocation.new(observed: Moonproof.to_runtime_value(res), next_private_state: private_state)
|
|
592
|
+
end
|
|
593
|
+
end
|
|
594
|
+
|
|
595
|
+
# Compact off-chain cryptography and encoding helpers matching Compact builtins.
|
|
596
|
+
module CompactCrypto
|
|
597
|
+
module_function
|
|
598
|
+
|
|
599
|
+
def pad32(label) = Moonproof.compact_pad32(label)
|
|
600
|
+
def field_bytes32(value) = Moonproof.compact_field_bytes32(value)
|
|
601
|
+
def persistent_hash(chunks) = Moonproof.compact_persistent_hash(chunks)
|
|
602
|
+
def degrade_to_transient(bytes32) = Moonproof.compact_degrade_to_transient(bytes32)
|
|
603
|
+
def transient_hash(fields) = Moonproof.compact_transient_hash(fields)
|
|
604
|
+
def leaf_hash(leaf) = Moonproof.compact_leaf_hash_bytes32(leaf)
|
|
605
|
+
def merkle_path(depth, leaves, leaf_index) = Moonproof.compact_merkle_tree_path_bytes32(depth, leaves, leaf_index)
|
|
606
|
+
def owner_either_coin_pk(coin_public_key) = Moonproof.compact_owner_either_coin_pk(coin_public_key)
|
|
607
|
+
def owner_either_contract(contract_address) = Moonproof.compact_owner_either_contract(contract_address)
|
|
608
|
+
end
|
|
609
|
+
|
|
610
|
+
# Shared execution context records and base handle for codegen-emitted contracts.
|
|
611
|
+
CircuitContext = Struct.new(
|
|
612
|
+
:contract_address_bytes, :contract_state_bytes,
|
|
613
|
+
:private_state, :coin_public_key_bytes, :current_time_secs,
|
|
614
|
+
:ledger_parameters_bytes, keyword_init: true
|
|
615
|
+
)
|
|
616
|
+
CircuitResults = Struct.new(:result, :context, :input_bytes, :output_bytes, keyword_init: true)
|
|
617
|
+
ConstructorContext = Struct.new(:initial_private_state, :coin_public_key_bytes, :signing_key_bytes, keyword_init: true)
|
|
618
|
+
ConstructorResult = Struct.new(:initial_contract_state_bytes, :initial_private_state, keyword_init: true)
|
|
619
|
+
|
|
620
|
+
def self.run_initialize(executable, ctx, args)
|
|
621
|
+
record = ConstructorContextRecord.new(
|
|
622
|
+
initial_private_state: to_runtime_value(ctx.initial_private_state),
|
|
623
|
+
coin_public_key_bytes: ctx.coin_public_key_bytes,
|
|
624
|
+
signing_key_bytes: ctx.signing_key_bytes
|
|
625
|
+
)
|
|
626
|
+
res = executable.initialize_blocking(record, args)
|
|
627
|
+
ConstructorResult.new(
|
|
628
|
+
initial_contract_state_bytes: res.initial_contract_state_bytes,
|
|
629
|
+
initial_private_state: from_runtime_value(res.initial_private_state)
|
|
630
|
+
)
|
|
631
|
+
end
|
|
632
|
+
|
|
633
|
+
def self.run_circuit(executable, circuit_name, ctx, args)
|
|
634
|
+
record = CircuitCallContextRecord.new(
|
|
635
|
+
contract_address_bytes: ctx.contract_address_bytes,
|
|
636
|
+
private_state: to_runtime_value(ctx.private_state),
|
|
637
|
+
coin_public_key_bytes: ctx.coin_public_key_bytes,
|
|
638
|
+
current_time_secs: ctx.current_time_secs.to_i,
|
|
639
|
+
ledger_parameters_bytes: ctx.ledger_parameters_bytes
|
|
640
|
+
)
|
|
641
|
+
res = executable.execute_circuit_blocking(circuit_name, record, ctx.contract_state_bytes, args)
|
|
642
|
+
next_ctx = CircuitContext.new(
|
|
643
|
+
contract_address_bytes: ctx.contract_address_bytes,
|
|
644
|
+
contract_state_bytes: res.next_contract_state_bytes,
|
|
645
|
+
private_state: from_runtime_value(res.next_private_state),
|
|
646
|
+
coin_public_key_bytes: ctx.coin_public_key_bytes,
|
|
647
|
+
current_time_secs: ctx.current_time_secs,
|
|
648
|
+
ledger_parameters_bytes: ctx.ledger_parameters_bytes
|
|
649
|
+
)
|
|
650
|
+
CircuitResults.new(
|
|
651
|
+
result: res.result,
|
|
652
|
+
context: next_ctx,
|
|
653
|
+
input_bytes: res.input_bytes,
|
|
654
|
+
output_bytes: res.output_bytes
|
|
655
|
+
)
|
|
656
|
+
end
|
|
657
|
+
|
|
658
|
+
# Shared base class for codegen-emitted typed contract handles.
|
|
659
|
+
class ContractHandle
|
|
660
|
+
attr_reader :contract
|
|
661
|
+
|
|
662
|
+
def initialize(contract)
|
|
663
|
+
@contract = contract
|
|
664
|
+
end
|
|
665
|
+
|
|
666
|
+
def address = @contract.address
|
|
667
|
+
def name = @contract.name
|
|
668
|
+
def holder = @contract.holder
|
|
669
|
+
def state = @contract.state
|
|
670
|
+
def private_state = @contract.private_state
|
|
671
|
+
def public_state = @contract.public_state
|
|
672
|
+
|
|
673
|
+
def set_witness(name, value) = @contract.set_witness(name, value)
|
|
674
|
+
def set_witness_with_state(name, value, next_state) = @contract.set_witness_with_state(name, value, next_state)
|
|
675
|
+
def set_witness_sequence(name, values) = @contract.set_witness_sequence(name, values)
|
|
676
|
+
def remove_witness(name) = @contract.remove_witness(name)
|
|
677
|
+
def clear_witnesses = @contract.clear_witnesses
|
|
678
|
+
def witness_calls = @contract.witness_calls
|
|
679
|
+
def clear_witness_log = @contract.clear_witness_log
|
|
680
|
+
def snapshot_witnesses = @contract.snapshot_witnesses
|
|
681
|
+
def revert_witnesses(snap) = @contract.revert_witnesses(snap)
|
|
682
|
+
|
|
683
|
+
def with_witness(overrides = nil, **kw_overrides, &block)
|
|
684
|
+
@contract.with_witness(overrides, **kw_overrides) do
|
|
685
|
+
yield self if block
|
|
686
|
+
end
|
|
687
|
+
end
|
|
688
|
+
|
|
689
|
+
def tx_with_diff(circuit, *args) = @contract.tx_with_diff(circuit, *args)
|
|
690
|
+
def call_with_diff(circuit, args = []) = @contract.call_with_diff(circuit, args)
|
|
691
|
+
def static_audit = @contract.static_audit
|
|
692
|
+
def audit = @contract.audit
|
|
693
|
+
def assert_privacy_clean = @contract.assert_privacy_clean
|
|
694
|
+
def assert_privacy_clean! = @contract.assert_privacy_clean!
|
|
695
|
+
end
|
|
696
|
+
end
|
data/lib/moonproof.rb
ADDED