Skip to content

Commit 0c69dfc

Browse files
committed
feat: rework import linker with macros
1 parent f476ae0 commit 0c69dfc

File tree

2 files changed

+22
-282
lines changed

2 files changed

+22
-282
lines changed

crates/runtime/src/runtime/strategy_runtime.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ impl StrategyRuntime {
3131

3232
pub fn execute(&mut self) -> Result<(), TrapCode> {
3333
self.strategy
34-
.execute(&mut self.store, self.entrypoint, &[], &mut [], FuelConfig::default())
34+
.execute(&mut self.store, self.entrypoint, &[], &mut [])
3535
}
3636

3737
pub fn resume(&mut self, exit_code: i32) -> Result<(), TrapCode> {

crates/types/src/block_fuel.rs

Lines changed: 21 additions & 281 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use crate::{SysFuncIdx, FUEL_DENOM_RATE};
2-
use rwasm::{instruction_set, InstructionSet, SyscallFuelParams, TrapCode};
2+
use rwasm::SyscallFuelParams;
33

44
/// The maximum allowed value for the `x` parameter used in linear gas cost calculation
55
/// of builtins.
@@ -22,51 +22,33 @@ const FUEL_MAX_LINEAR_X: u32 = 262_143; // 2^18 - 1
2222
/// visit_local_get).
2323
///
2424
/// Word is defined as 32 bytes, the same as in the EVM.
25-
macro_rules! linear_fuel {
26-
($local_depth:expr, $base_cost:expr, $word_cost:expr) => {{
27-
// compile-time overflow check
28-
const _: () = {
29-
assert!(
30-
($base_cost as u128) + ($word_cost as u128) * (FUEL_MAX_LINEAR_X as u128) <= (u32::MAX as u128),
31-
"base_cost + word_cost * FUEL_MAX_LINEAR_X must fit into u32"
32-
);
33-
};
34-
35-
instruction_set! {
36-
LocalGet($local_depth) // Compare x with FUEL_MAX_LINEAR_X
37-
I32Const(FUEL_MAX_LINEAR_X)
38-
I32GtU
39-
BrIfEqz(2)
40-
Trap(TrapCode::IntegerOverflow)
41-
42-
LocalGet($local_depth) // Calculate cost
43-
I32Const(31)
44-
I32Add
45-
I32Const(32)
46-
I32DivU
47-
I32Const($word_cost)
48-
I32Mul
49-
I32Const($base_cost)
50-
I32Add
51-
I32Const(0) // Push two 32-bit values, which are interpreted as a single 64-bit value inside the builtin.
52-
Call(SysFuncIdx::CHARGE_FUEL)
25+
macro_rules! no_fuel {
26+
() => {
27+
SyscallFuelParams {
28+
base_fuel: 0,
29+
param_index: 0,
30+
linear_fuel: 0,
5331
}
54-
}};
32+
};
5533
}
5634

5735
macro_rules! const_fuel {
58-
($cost:expr) => {
59-
instruction_set! {
60-
I32Const($cost)
61-
I32Const(0) // Push two 32-bit values, which are interpreted as a single 64-bit value inside the builtin.
62-
Call(SysFuncIdx::CHARGE_FUEL)
36+
($base:expr) => {
37+
SyscallFuelParams {
38+
base_fuel: $base as u64,
39+
param_index: 0,
40+
linear_fuel: 0,
6341
}
6442
};
6543
}
6644

67-
macro_rules! no_fuel {
68-
() => {
69-
instruction_set! {}
45+
macro_rules! linear_fuel {
46+
($param_index:expr, $base:expr, $linear:expr) => {
47+
SyscallFuelParams {
48+
base_fuel: $base as u64,
49+
param_index: $param_index,
50+
linear_fuel: $linear as u64,
51+
}
7052
};
7153
}
7254

@@ -124,7 +106,7 @@ pub const BLS_MAP_G2_COST: u32 = 80_000 * FUEL_DENOM_RATE as u32;
124106
pub const UINT256_MUL_MOD_COST: u32 = 8 * FUEL_DENOM_RATE as u32;
125107
pub const UINT256_X2048_MUL_COST: u32 = 5_000 * FUEL_DENOM_RATE as u32;
126108

127-
pub(crate) fn emit_fuel_procedure(sys_func_idx: SysFuncIdx) -> InstructionSet {
109+
pub(crate) fn calculate_syscall_fuel(sys_func_idx: SysFuncIdx) -> SyscallFuelParams {
128110
use SysFuncIdx::*;
129111
match sys_func_idx {
130112
// input/output & state control (0x00)
@@ -189,245 +171,3 @@ pub(crate) fn emit_fuel_procedure(sys_func_idx: SysFuncIdx) -> InstructionSet {
189171
_ => no_fuel!(),
190172
}
191173
}
192-
193-
pub(crate) fn calculate_syscall_fuel(sys_func_idx: SysFuncIdx) -> SyscallFuelParams {
194-
use SysFuncIdx::*;
195-
match sys_func_idx {
196-
// input/output & state control (0x00)
197-
EXIT => SyscallFuelParams {
198-
base_fuel: 0,
199-
param_index: 0,
200-
linear_fuel: 0,
201-
},
202-
STATE => SyscallFuelParams {
203-
base_fuel: LOW_FUEL_COST as u64,
204-
param_index: 0,
205-
linear_fuel: 0,
206-
},
207-
READ_INPUT => SyscallFuelParams {
208-
base_fuel: COPY_BASE_FUEL_COST as u64,
209-
param_index: 1,
210-
linear_fuel: COPY_WORD_FUEL_COST as u64,
211-
},
212-
INPUT_SIZE => SyscallFuelParams {
213-
base_fuel: LOW_FUEL_COST as u64,
214-
param_index: 0,
215-
linear_fuel: 0,
216-
},
217-
WRITE_OUTPUT => SyscallFuelParams {
218-
base_fuel: COPY_BASE_FUEL_COST as u64,
219-
param_index: 1,
220-
linear_fuel: COPY_WORD_FUEL_COST as u64,
221-
},
222-
OUTPUT_SIZE => SyscallFuelParams {
223-
base_fuel: LOW_FUEL_COST as u64,
224-
param_index: 0,
225-
linear_fuel: 0,
226-
},
227-
READ_OUTPUT => SyscallFuelParams {
228-
base_fuel: COPY_BASE_FUEL_COST as u64,
229-
param_index: 1,
230-
linear_fuel: COPY_WORD_FUEL_COST as u64,
231-
},
232-
EXEC => SyscallFuelParams {
233-
base_fuel: 0,
234-
param_index: 0,
235-
linear_fuel: 0,
236-
},
237-
RESUME => SyscallFuelParams {
238-
base_fuel: 0,
239-
param_index: 0,
240-
linear_fuel: 0,
241-
},
242-
FORWARD_OUTPUT => SyscallFuelParams {
243-
base_fuel: COPY_BASE_FUEL_COST as u64,
244-
param_index: 1,
245-
linear_fuel: COPY_WORD_FUEL_COST as u64,
246-
},
247-
CHARGE_FUEL_MANUALLY => SyscallFuelParams {
248-
base_fuel: 0,
249-
param_index: 0,
250-
linear_fuel: 0,
251-
},
252-
FUEL => SyscallFuelParams {
253-
base_fuel: LOW_FUEL_COST as u64,
254-
param_index: 0,
255-
linear_fuel: 0,
256-
},
257-
DEBUG_LOG => SyscallFuelParams {
258-
base_fuel: DEBUG_LOG_BASE_FUEL_COST as u64,
259-
param_index: 1,
260-
linear_fuel: DEBUG_LOG_WORD_FUEL_COST as u64,
261-
},
262-
CHARGE_FUEL => SyscallFuelParams {
263-
base_fuel: CHARGE_FUEL_BASE_COST as u64,
264-
param_index: 0,
265-
linear_fuel: 0,
266-
},
267-
268-
// hashing functions (0x01)
269-
KECCAK256 => SyscallFuelParams {
270-
base_fuel: KECCAK_BASE_FUEL_COST as u64,
271-
param_index: 2,
272-
linear_fuel: KECCAK_WORD_FUEL_COST as u64,
273-
},
274-
KECCAK256_PERMUTE => SyscallFuelParams {
275-
base_fuel: KECCAK_BASE_FUEL_COST as u64,
276-
param_index: 0,
277-
linear_fuel: 0,
278-
},
279-
POSEIDON => SyscallFuelParams {
280-
base_fuel: 100,
281-
param_index: 2,
282-
linear_fuel: 20,
283-
},
284-
SHA256_EXTEND => SyscallFuelParams {
285-
base_fuel: SHA256_BASE_FUEL_COST as u64,
286-
param_index: 0,
287-
linear_fuel: 0,
288-
},
289-
SHA256_COMPRESS => SyscallFuelParams {
290-
base_fuel: SHA256_BASE_FUEL_COST as u64,
291-
param_index: 0,
292-
linear_fuel: 0,
293-
},
294-
SHA256 => SyscallFuelParams {
295-
base_fuel: SHA256_BASE_FUEL_COST as u64,
296-
param_index: 2,
297-
linear_fuel: SHA256_WORD_FUEL_COST as u64,
298-
},
299-
BLAKE3 => SyscallFuelParams {
300-
base_fuel: BLAKE3_BASE_FUEL_COST as u64,
301-
param_index: 2,
302-
linear_fuel: BLAKE3_WORD_FUEL_COST as u64,
303-
},
304-
305-
// ed25519 (0x02)
306-
ED25519_DECOMPRESS => SyscallFuelParams {
307-
base_fuel: ED25519_DECOMPRESS_COST as u64,
308-
param_index: 0,
309-
linear_fuel: 0,
310-
},
311-
ED25519_ADD => SyscallFuelParams {
312-
base_fuel: ED25519_ADD_COST as u64,
313-
param_index: 0,
314-
linear_fuel: 0,
315-
},
316-
317-
// fp1/fp2 tower field (0x03)
318-
TOWER_FP1_BN254_ADD | TOWER_FP1_BN254_SUB => SyscallFuelParams {
319-
base_fuel: FP1_ADD_COST as u64,
320-
param_index: 0,
321-
linear_fuel: 0,
322-
},
323-
TOWER_FP1_BN254_MUL => SyscallFuelParams {
324-
base_fuel: FP1_MUL_COST as u64,
325-
param_index: 0,
326-
linear_fuel: 0,
327-
},
328-
TOWER_FP1_BLS12381_ADD | TOWER_FP1_BLS12381_SUB => SyscallFuelParams {
329-
base_fuel: FP1_BLS_ADD_COST as u64,
330-
param_index: 0,
331-
linear_fuel: 0,
332-
},
333-
TOWER_FP1_BLS12381_MUL => SyscallFuelParams {
334-
base_fuel: FP1_BLS_MUL_COST as u64,
335-
param_index: 0,
336-
linear_fuel: 0,
337-
},
338-
TOWER_FP2_BN254_ADD | TOWER_FP2_BN254_SUB => SyscallFuelParams {
339-
base_fuel: FP2_ADD_COST as u64,
340-
param_index: 0,
341-
linear_fuel: 0,
342-
},
343-
TOWER_FP2_BN254_MUL => SyscallFuelParams {
344-
base_fuel: FP2_MUL_COST as u64,
345-
param_index: 0,
346-
linear_fuel: 0,
347-
},
348-
TOWER_FP2_BLS12381_ADD | TOWER_FP2_BLS12381_SUB => SyscallFuelParams {
349-
base_fuel: FP2_BLS_ADD_COST as u64,
350-
param_index: 0,
351-
linear_fuel: 0,
352-
},
353-
TOWER_FP2_BLS12381_MUL => SyscallFuelParams {
354-
base_fuel: FP2_BLS_MUL_COST as u64,
355-
param_index: 0,
356-
linear_fuel: 0,
357-
},
358-
359-
// secp256k1 (0x04)
360-
SECP256K1_ADD => SyscallFuelParams {
361-
base_fuel: SECP256K1_ADD_COST as u64,
362-
param_index: 0,
363-
linear_fuel: 0,
364-
},
365-
SECP256K1_DECOMPRESS => SyscallFuelParams {
366-
base_fuel: SECP256K1_DECOMPRESS_COST as u64,
367-
param_index: 0,
368-
linear_fuel: 0,
369-
},
370-
SECP256K1_DOUBLE => SyscallFuelParams {
371-
base_fuel: SECP256K1_DOUBLE_COST as u64,
372-
param_index: 0,
373-
linear_fuel: 0,
374-
},
375-
376-
// bls12381 (0x06)
377-
BLS12381_ADD => SyscallFuelParams {
378-
base_fuel: BLS_G1_ADD_COST as u64,
379-
param_index: 0,
380-
linear_fuel: 0,
381-
},
382-
// BLS12381_G2_ADD => SyscallFuelParams {
383-
// base_fuel: BLS_G2_ADD_COST as u64,
384-
// param_index: 0,
385-
// linear_fuel: 0,
386-
// },
387-
// BLS12381_PAIRING => SyscallFuelParams {
388-
// base_fuel: BLS_PAIRING_COST as u64,
389-
// param_index: 0,
390-
// linear_fuel: 0,
391-
// },
392-
// BLS12381_MAP_G1 => SyscallFuelParams {
393-
// base_fuel: BLS_MAP_G1_COST as u64,
394-
// param_index: 0,
395-
// linear_fuel: 0,
396-
// },
397-
// BLS12381_MAP_G2 => SyscallFuelParams {
398-
// base_fuel: BLS_MAP_G2_COST as u64,
399-
// param_index: 0,
400-
// linear_fuel: 0,
401-
// },
402-
403-
// bn254 (0x07)
404-
BN254_ADD => SyscallFuelParams {
405-
base_fuel: BN254_ADD_COST as u64,
406-
param_index: 0,
407-
linear_fuel: 0,
408-
},
409-
BN254_DOUBLE => SyscallFuelParams {
410-
base_fuel: BN254_DOUBLE_COST as u64,
411-
param_index: 0,
412-
linear_fuel: 0,
413-
},
414-
415-
// uint256 (0x08)
416-
UINT256_MUL_MOD => SyscallFuelParams {
417-
base_fuel: UINT256_MUL_MOD_COST as u64,
418-
param_index: 0,
419-
linear_fuel: 0,
420-
},
421-
UINT256_X2048_MUL => SyscallFuelParams {
422-
base_fuel: UINT256_X2048_MUL_COST as u64,
423-
param_index: 0,
424-
linear_fuel: 0,
425-
},
426-
427-
_ => SyscallFuelParams {
428-
base_fuel: 0,
429-
param_index: 0,
430-
linear_fuel: 0,
431-
},
432-
}
433-
}

0 commit comments

Comments
 (0)