neo-devpack-solidity 0.22.0

Production-focused Solidity-to-NeoVM compilation system
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
fn emit_runtime_throw_with_message(instructions: &mut Vec<Instruction>, message: &str) {
    instructions.push(Instruction::PushLiteral(LiteralValue::String(
        message.as_bytes().to_vec(),
    )));
    instructions.push(Instruction::Throw);
}

fn try_lower_runtime_member_builtin(
    base: &Identifier,
    member: &Identifier,
    args: &[Expression],
    ctx: &mut LoweringContext,
    instructions: &mut Vec<Instruction>,
) -> Option<bool> {
    if base.name != "Runtime" {
        return None;
    }

    match member.name.as_str() {
        "initializeServices" => {
            if !args.is_empty() {
                ctx.record_error(format!(
                    "Runtime.initializeServices requires 0 argument(s), got {}",
                    args.len()
                ));
                return Some(false);
            }

            // The devpack exposes Runtime.initializeServices() as a convenience initializer,
            // but builtin libraries are compiler intrinsics and their Solidity bodies are
            // not compiled. Treat this helper as a no-op so devpack-based sources remain
            // portable while keeping the generated bytecode minimal.
            instructions.push(Instruction::PushLiteral(LiteralValue::Boolean(true)));
            Some(true)
        }
        "notifyIndexed" => {
            if args.len() != 3 {
                ctx.record_error(format!(
                    "Runtime.notifyIndexed requires 3 argument(s), got {}",
                    args.len()
                ));
                return Some(false);
            }

            // notifyIndexed(eventName, topics, data) => notify(eventName, abi.encode(topics, data))
            if !lower_expression(&args[0], ctx, instructions) {
                return Some(false);
            }
            let payload_args = [args[1].clone(), args[2].clone()];
            if !lower_neo_serialized_arg_array(&payload_args, ctx, instructions) {
                return Some(false);
            }
            validate_runtime_notify_call(
                &[
                    args[0].clone(),
                    Expression::FunctionCall(
                        Default::default(),
                        Box::new(Expression::MemberAccess(
                            Default::default(),
                            Box::new(Expression::Variable(Identifier {
                                loc: Default::default(),
                                name: "abi".to_string(),
                            })),
                            Identifier {
                                loc: Default::default(),
                                name: "encode".to_string(),
                            },
                        )),
                        vec![args[1].clone(), args[2].clone()],
                    ),
                ],
                ctx,
            );
            instructions.push(Instruction::CallBuiltin {
                builtin: BuiltinCall::RuntimeNotify,
                arg_count: 2,
            });
            Some(true)
        }
        "notify" => {
            if args.len() != 2 {
                ctx.record_error(format!(
                    "Runtime.notify requires 2 argument(s), got {}",
                    args.len()
                ));
                return Some(false);
            }

            if !lower_expression(&args[0], ctx, instructions) {
                return Some(false);
            }

            if let Some(encoded_args) = extract_abi_encode_args(&args[1]) {
                if !lower_neo_serialized_arg_array(encoded_args, ctx, instructions) {
                    return Some(false);
                }
                validate_runtime_notify_call(args, ctx);
            } else if !lower_expression(&args[1], ctx, instructions) {
                return Some(false);
            }

            instructions.push(Instruction::CallBuiltin {
                builtin: BuiltinCall::RuntimeNotify,
                arg_count: 2,
            });
            Some(true)
        }
        "requireWitness" => {
            if args.len() != 1 {
                ctx.record_error(format!(
                    "Runtime.requireWitness requires 1 argument(s), got {}",
                    args.len()
                ));
                return Some(false);
            }

            if !lower_expression(&args[0], ctx, instructions) {
                return Some(false);
            }
            instructions.push(Instruction::CallBuiltin {
                builtin: BuiltinCall::RuntimeCheckWitness,
                arg_count: 1,
            });

            let fail_label = ctx.next_label();
            let end_label = ctx.next_label();
            // JumpIf branches when the condition is false.
            instructions.push(Instruction::JumpIf { target: fail_label });
            instructions.push(Instruction::Jump { target: end_label });
            instructions.push(Instruction::Label(fail_label));
            emit_runtime_throw_with_message(instructions, "Runtime: invalid witness");
            instructions.push(Instruction::Label(end_label));
            // Runtime.requireWitness() is a void helper.
            Some(false)
        }
        "checkAnyWitness" => {
            if args.len() != 1 {
                ctx.record_error(format!(
                    "Runtime.checkAnyWitness requires 1 argument(s), got {}",
                    args.len()
                ));
                return Some(false);
            }

            let tmp_id = ctx.next_label();
            let accounts_slot = ctx.allocate_local(
                format!("__runtime_check_any_accounts_{tmp_id}"),
                Some(ValueType::Any),
            );
            let index_slot = ctx.allocate_local(
                format!("__runtime_check_any_index_{tmp_id}"),
                Some(ValueType::Integer {
                    signed: false,
                    bits: 256,
                }),
            );

            if !lower_expression(&args[0], ctx, instructions) {
                return Some(false);
            }
            instructions.push(Instruction::StoreLocal(accounts_slot));
            instructions.push(Instruction::PushLiteral(LiteralValue::Integer(
                BigInt::zero(),
            )));
            instructions.push(Instruction::StoreLocal(index_slot));

            let loop_label = ctx.next_label();
            let advance_label = ctx.next_label();
            let done_label = ctx.next_label();
            let end_label = ctx.next_label();

            instructions.push(Instruction::Label(loop_label));
            instructions.push(Instruction::LoadLocal(index_slot));
            instructions.push(Instruction::LoadLocal(accounts_slot));
            instructions.push(Instruction::GetSize);
            instructions.push(Instruction::BinaryOp(BinaryOperator::Lt));
            // JumpIf branches when the condition is false.
            instructions.push(Instruction::JumpIf { target: done_label });

            instructions.push(Instruction::LoadLocal(accounts_slot));
            instructions.push(Instruction::LoadLocal(index_slot));
            instructions.push(Instruction::ArrayGet);
            instructions.push(Instruction::CallBuiltin {
                builtin: BuiltinCall::RuntimeCheckWitness,
                arg_count: 1,
            });
            instructions.push(Instruction::JumpIf {
                target: advance_label,
            });
            instructions.push(Instruction::PushLiteral(LiteralValue::Boolean(true)));
            instructions.push(Instruction::Jump { target: end_label });

            instructions.push(Instruction::Label(advance_label));
            instructions.push(Instruction::LoadLocal(index_slot));
            instructions.push(Instruction::PushLiteral(LiteralValue::Integer(
                BigInt::one(),
            )));
            instructions.push(Instruction::BinaryOp(BinaryOperator::Add));
            instructions.push(Instruction::StoreLocal(index_slot));
            instructions.push(Instruction::Jump { target: loop_label });

            instructions.push(Instruction::Label(done_label));
            instructions.push(Instruction::PushLiteral(LiteralValue::Boolean(false)));
            instructions.push(Instruction::Label(end_label));
            Some(true)
        }
        "checkAllWitnesses" => {
            if args.len() != 1 {
                ctx.record_error(format!(
                    "Runtime.checkAllWitnesses requires 1 argument(s), got {}",
                    args.len()
                ));
                return Some(false);
            }

            let tmp_id = ctx.next_label();
            let accounts_slot = ctx.allocate_local(
                format!("__runtime_check_all_accounts_{tmp_id}"),
                Some(ValueType::Any),
            );
            let index_slot = ctx.allocate_local(
                format!("__runtime_check_all_index_{tmp_id}"),
                Some(ValueType::Integer {
                    signed: false,
                    bits: 256,
                }),
            );

            if !lower_expression(&args[0], ctx, instructions) {
                return Some(false);
            }
            instructions.push(Instruction::StoreLocal(accounts_slot));
            instructions.push(Instruction::PushLiteral(LiteralValue::Integer(
                BigInt::zero(),
            )));
            instructions.push(Instruction::StoreLocal(index_slot));

            let loop_label = ctx.next_label();
            let fail_label = ctx.next_label();
            let done_label = ctx.next_label();
            let end_label = ctx.next_label();

            instructions.push(Instruction::Label(loop_label));
            instructions.push(Instruction::LoadLocal(index_slot));
            instructions.push(Instruction::LoadLocal(accounts_slot));
            instructions.push(Instruction::GetSize);
            instructions.push(Instruction::BinaryOp(BinaryOperator::Lt));
            instructions.push(Instruction::JumpIf { target: done_label });

            instructions.push(Instruction::LoadLocal(accounts_slot));
            instructions.push(Instruction::LoadLocal(index_slot));
            instructions.push(Instruction::ArrayGet);
            instructions.push(Instruction::CallBuiltin {
                builtin: BuiltinCall::RuntimeCheckWitness,
                arg_count: 1,
            });
            instructions.push(Instruction::JumpIf { target: fail_label });

            instructions.push(Instruction::LoadLocal(index_slot));
            instructions.push(Instruction::PushLiteral(LiteralValue::Integer(
                BigInt::one(),
            )));
            instructions.push(Instruction::BinaryOp(BinaryOperator::Add));
            instructions.push(Instruction::StoreLocal(index_slot));
            instructions.push(Instruction::Jump { target: loop_label });

            instructions.push(Instruction::Label(done_label));
            instructions.push(Instruction::PushLiteral(LiteralValue::Boolean(true)));
            instructions.push(Instruction::Jump { target: end_label });

            instructions.push(Instruction::Label(fail_label));
            instructions.push(Instruction::PushLiteral(LiteralValue::Boolean(false)));
            instructions.push(Instruction::Label(end_label));
            Some(true)
        }
        "checkMultiSigWitness" => {
            if args.len() != 2 {
                ctx.record_error(format!(
                    "Runtime.checkMultiSigWitness requires 2 argument(s), got {}",
                    args.len()
                ));
                return Some(false);
            }

            let tmp_id = ctx.next_label();
            let signers_slot = ctx.allocate_local(
                format!("__runtime_check_multisig_signers_{tmp_id}"),
                Some(ValueType::Any),
            );
            let threshold_slot = ctx.allocate_local(
                format!("__runtime_check_multisig_threshold_{tmp_id}"),
                Some(ValueType::Integer {
                    signed: false,
                    bits: 256,
                }),
            );
            let valid_slot = ctx.allocate_local(
                format!("__runtime_check_multisig_valid_{tmp_id}"),
                Some(ValueType::Integer {
                    signed: false,
                    bits: 256,
                }),
            );
            let index_slot = ctx.allocate_local(
                format!("__runtime_check_multisig_index_{tmp_id}"),
                Some(ValueType::Integer {
                    signed: false,
                    bits: 256,
                }),
            );
            let signer_slot = ctx.allocate_local(
                format!("__runtime_check_multisig_signer_{tmp_id}"),
                Some(ValueType::Any),
            );
            let inner_index_slot = ctx.allocate_local(
                format!("__runtime_check_multisig_inner_index_{tmp_id}"),
                Some(ValueType::Integer {
                    signed: false,
                    bits: 256,
                }),
            );

            if !lower_expression(&args[0], ctx, instructions) {
                return Some(false);
            }
            instructions.push(Instruction::StoreLocal(signers_slot));

            if !lower_expression(&args[1], ctx, instructions) {
                return Some(false);
            }
            instructions.push(Instruction::StoreLocal(threshold_slot));

            let threshold_positive_fail_label = ctx.next_label();
            let threshold_positive_ok_label = ctx.next_label();
            instructions.push(Instruction::LoadLocal(threshold_slot));
            instructions.push(Instruction::PushLiteral(LiteralValue::Integer(
                BigInt::zero(),
            )));
            instructions.push(Instruction::BinaryOp(BinaryOperator::Gt));
            instructions.push(Instruction::JumpIf {
                target: threshold_positive_fail_label,
            });
            instructions.push(Instruction::Jump {
                target: threshold_positive_ok_label,
            });
            instructions.push(Instruction::Label(threshold_positive_fail_label));
            emit_runtime_throw_with_message(instructions, "Runtime: threshold must be positive");
            instructions.push(Instruction::Label(threshold_positive_ok_label));

            let threshold_within_len_fail_label = ctx.next_label();
            let threshold_within_len_ok_label = ctx.next_label();
            instructions.push(Instruction::LoadLocal(threshold_slot));
            instructions.push(Instruction::LoadLocal(signers_slot));
            instructions.push(Instruction::GetSize);
            instructions.push(Instruction::BinaryOp(BinaryOperator::Le));
            instructions.push(Instruction::JumpIf {
                target: threshold_within_len_fail_label,
            });
            instructions.push(Instruction::Jump {
                target: threshold_within_len_ok_label,
            });
            instructions.push(Instruction::Label(threshold_within_len_fail_label));
            emit_runtime_throw_with_message(instructions, "Runtime: threshold exceeds signers");
            instructions.push(Instruction::Label(threshold_within_len_ok_label));

            instructions.push(Instruction::PushLiteral(LiteralValue::Integer(
                BigInt::zero(),
            )));
            instructions.push(Instruction::StoreLocal(valid_slot));
            instructions.push(Instruction::PushLiteral(LiteralValue::Integer(
                BigInt::zero(),
            )));
            instructions.push(Instruction::StoreLocal(index_slot));

            let loop_label = ctx.next_label();
            let advance_label = ctx.next_label();
            let done_label = ctx.next_label();
            let end_label = ctx.next_label();

            instructions.push(Instruction::Label(loop_label));
            instructions.push(Instruction::LoadLocal(index_slot));
            instructions.push(Instruction::LoadLocal(signers_slot));
            instructions.push(Instruction::GetSize);
            instructions.push(Instruction::BinaryOp(BinaryOperator::Lt));
            instructions.push(Instruction::JumpIf { target: done_label });

            instructions.push(Instruction::LoadLocal(signers_slot));
            instructions.push(Instruction::LoadLocal(index_slot));
            instructions.push(Instruction::ArrayGet);
            instructions.push(Instruction::StoreLocal(signer_slot));

            // Reject duplicate signer entries so quorum cannot be satisfied by repetition.
            instructions.push(Instruction::PushLiteral(LiteralValue::Integer(
                BigInt::zero(),
            )));
            instructions.push(Instruction::StoreLocal(inner_index_slot));
            let duplicate_scan_loop_label = ctx.next_label();
            let duplicate_scan_done_label = ctx.next_label();
            let duplicate_scan_continue_label = ctx.next_label();
            instructions.push(Instruction::Label(duplicate_scan_loop_label));
            instructions.push(Instruction::LoadLocal(inner_index_slot));
            instructions.push(Instruction::LoadLocal(index_slot));
            instructions.push(Instruction::BinaryOp(BinaryOperator::Lt));
            instructions.push(Instruction::JumpIf {
                target: duplicate_scan_done_label,
            });

            instructions.push(Instruction::LoadLocal(signers_slot));
            instructions.push(Instruction::LoadLocal(inner_index_slot));
            instructions.push(Instruction::ArrayGet);
            instructions.push(Instruction::LoadLocal(signer_slot));
            instructions.push(Instruction::BinaryOp(BinaryOperator::Eq));
            instructions.push(Instruction::JumpIf {
                target: duplicate_scan_continue_label,
            });
            emit_runtime_throw_with_message(instructions, "Runtime: duplicate signer");
            instructions.push(Instruction::Label(duplicate_scan_continue_label));

            instructions.push(Instruction::LoadLocal(inner_index_slot));
            instructions.push(Instruction::PushLiteral(LiteralValue::Integer(
                BigInt::one(),
            )));
            instructions.push(Instruction::BinaryOp(BinaryOperator::Add));
            instructions.push(Instruction::StoreLocal(inner_index_slot));
            instructions.push(Instruction::Jump {
                target: duplicate_scan_loop_label,
            });
            instructions.push(Instruction::Label(duplicate_scan_done_label));

            instructions.push(Instruction::LoadLocal(signer_slot));
            instructions.push(Instruction::CallBuiltin {
                builtin: BuiltinCall::RuntimeCheckWitness,
                arg_count: 1,
            });
            instructions.push(Instruction::JumpIf {
                target: advance_label,
            });

            instructions.push(Instruction::LoadLocal(valid_slot));
            instructions.push(Instruction::PushLiteral(LiteralValue::Integer(
                BigInt::one(),
            )));
            instructions.push(Instruction::BinaryOp(BinaryOperator::Add));
            instructions.push(Instruction::StoreLocal(valid_slot));

            instructions.push(Instruction::LoadLocal(valid_slot));
            instructions.push(Instruction::LoadLocal(threshold_slot));
            instructions.push(Instruction::BinaryOp(BinaryOperator::Ge));
            instructions.push(Instruction::JumpIf {
                target: advance_label,
            });
            instructions.push(Instruction::PushLiteral(LiteralValue::Boolean(true)));
            instructions.push(Instruction::Jump { target: end_label });

            instructions.push(Instruction::Label(advance_label));
            instructions.push(Instruction::LoadLocal(index_slot));
            instructions.push(Instruction::PushLiteral(LiteralValue::Integer(
                BigInt::one(),
            )));
            instructions.push(Instruction::BinaryOp(BinaryOperator::Add));
            instructions.push(Instruction::StoreLocal(index_slot));
            instructions.push(Instruction::Jump { target: loop_label });

            instructions.push(Instruction::Label(done_label));
            instructions.push(Instruction::PushLiteral(LiteralValue::Boolean(false)));
            instructions.push(Instruction::Label(end_label));
            Some(true)
        }
        _ => None,
    }
}