Skip to content

Commit 4d8ceb0

Browse files
author
Jannis Pohlmann
committed
runtime/wasm: Update tests to BigInt and test BigInt/i32/hex conversions
1 parent 2693e4d commit 4d8ceb0

File tree

8 files changed

+144
-20
lines changed

8 files changed

+144
-20
lines changed

runtime/wasm/src/asc_abi/test.rs

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ use wasmi::{
77
RuntimeValue, Signature,
88
};
99

10+
use graph::prelude::BigInt;
1011
use graph::web3::types::{H160, U256};
1112

1213
use super::class::*;
@@ -160,18 +161,26 @@ fn string() {
160161
}
161162

162163
#[test]
163-
fn abi_u256() {
164+
fn abi_big_int() {
164165
let module = TestModule::new("wasm_test/abi_classes.wasm");
165-
let address = U256::zero();
166166

167-
// As an `Uint64Array`
168-
let array_buffer: AscPtr<Uint8Array> = module.asc_new(&address);
169-
let new_uint_obj: AscPtr<Uint8Array> = module.takes_ptr_returns_ptr("test_uint", array_buffer);
170-
171-
// This should have 1 added to the first and last `u64`s.
172-
let new_uint: U256 = module.asc_get(new_uint_obj);
173-
174-
assert_eq!(new_uint, U256([1, 0, 0, 1]))
167+
// Test passing in 0 and increment it by 1
168+
let old_uint = U256::zero();
169+
let array_buffer: AscPtr<AscBigInt> = module.asc_new(&BigInt::from_unsigned_u256(&old_uint));
170+
let new_uint_obj: AscPtr<AscBigInt> = module.takes_ptr_returns_ptr("test_uint", array_buffer);
171+
let new_uint: BigInt = module.asc_get(new_uint_obj);
172+
assert_eq!(new_uint, BigInt::from(1 as i32));
173+
let new_uint = new_uint.to_unsigned_u256();
174+
assert_eq!(new_uint, U256([1, 0, 0, 0]));
175+
176+
// Test passing in -50 and increment it by 1
177+
let old_uint = BigInt::from(-50);
178+
let array_buffer: AscPtr<AscBigInt> = module.asc_new(&old_uint);
179+
let new_uint_obj: AscPtr<AscBigInt> = module.takes_ptr_returns_ptr("test_uint", array_buffer);
180+
let new_uint: BigInt = module.asc_get(new_uint_obj);
181+
assert_eq!(new_uint, BigInt::from(-49 as i32));
182+
let new_uint_from_u256 = BigInt::from_signed_u256(&new_uint.to_signed_u256());
183+
assert_eq!(new_uint, new_uint_from_u256);
175184
}
176185

177186
#[test]
@@ -420,8 +429,6 @@ fn abi_store_value() {
420429
module.asc_get(module.takes_ptr_returns_ptr("value_from_bigint", bytes_ptr));
421430
assert_eq!(
422431
new_value,
423-
Value::BigInt(::graph::data::store::scalar::BigInt::from_signed_bytes_le(
424-
bytes
425-
))
432+
Value::BigInt(::graph::data::store::scalar::BigInt::from_unsigned_bytes_le(bytes))
426433
);
427434
}

runtime/wasm/src/module/test.rs

Lines changed: 92 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ use graph::components::subgraph::*;
1414
use graph::data::store::scalar;
1515
use graph::data::subgraph::*;
1616
use graph::util;
17-
use graph::web3::types::Address;
17+
use graph::web3::types::{Address, H256, U256};
1818
use hex;
1919
use std::collections::HashMap;
2020
use std::io::Cursor;
@@ -365,3 +365,94 @@ fn token_numeric_conversion() {
365365
.expect("call did not return i32");
366366
assert_eq!(num, num_return);
367367
}
368+
369+
#[test]
370+
fn big_int_to_from_i32() {
371+
let mut module = test_module(mock_data_source("wasm_test/big_int_to_from_i32.wasm"));
372+
373+
// Convert i32 to BigInt
374+
let input: i32 = -157;
375+
let output_ptr: AscPtr<AscBigInt> = module
376+
.module
377+
.invoke_export(
378+
"i32_to_big_int",
379+
&[RuntimeValue::from(input)],
380+
&mut module.externals,
381+
).expect("call failed")
382+
.expect("call returned nothing")
383+
.try_into()
384+
.expect("call did not return pointer");
385+
let output: BigInt = module.heap.asc_get(output_ptr);
386+
assert_eq!(output, BigInt::from(-157 as i32));
387+
388+
// Convert BigInt to i32
389+
let input = BigInt::from(-50 as i32);
390+
let input_ptr: AscPtr<AscBigInt> = module.heap.asc_new(&input);
391+
let output: i32 = module
392+
.module
393+
.invoke_export(
394+
"big_int_to_i32",
395+
&[RuntimeValue::from(input_ptr)],
396+
&mut module.externals,
397+
).expect("call failed")
398+
.expect("call returned nothing")
399+
.try_into()
400+
.expect("call did not return pointer");
401+
assert_eq!(output, -50 as i32);
402+
}
403+
404+
#[test]
405+
fn big_int_to_hex() {
406+
let mut module = test_module(mock_data_source("wasm_test/big_int_to_hex.wasm"));
407+
408+
// Convert zero to hex
409+
let zero = BigInt::from_unsigned_u256(&U256::zero());
410+
let zero: AscPtr<AscBigInt> = module.heap.asc_new(&zero);
411+
let zero_hex_ptr: AscPtr<AscString> = module
412+
.module
413+
.invoke_export(
414+
"big_int_to_hex",
415+
&[RuntimeValue::from(zero)],
416+
&mut module.externals,
417+
).expect("call failed")
418+
.expect("call returned nothing")
419+
.try_into()
420+
.expect("call did not return pointer");
421+
let zero_hex_str: String = module.heap.asc_get(zero_hex_ptr);
422+
assert_eq!(zero_hex_str, "0x00");
423+
424+
// Convert 1 to hex
425+
let one = BigInt::from_unsigned_u256(&U256::one());
426+
let one: AscPtr<AscBigInt> = module.heap.asc_new(&one);
427+
let one_hex_ptr: AscPtr<AscString> = module
428+
.module
429+
.invoke_export(
430+
"big_int_to_hex",
431+
&[RuntimeValue::from(one)],
432+
&mut module.externals,
433+
).expect("call failed")
434+
.expect("call returned nothing")
435+
.try_into()
436+
.expect("call did not return pointer");
437+
let one_hex_str: String = module.heap.asc_get(one_hex_ptr);
438+
assert_eq!(one_hex_str, "0x01");
439+
440+
// Convert U256::max_value() to hex
441+
let u256_max = BigInt::from_unsigned_u256(&U256::max_value());
442+
let u256_max: AscPtr<AscBigInt> = module.heap.asc_new(&u256_max);
443+
let u256_max_hex_ptr: AscPtr<AscString> = module
444+
.module
445+
.invoke_export(
446+
"big_int_to_hex",
447+
&[RuntimeValue::from(u256_max)],
448+
&mut module.externals,
449+
).expect("call failed")
450+
.expect("call returned nothing")
451+
.try_into()
452+
.expect("call did not return pointer");
453+
let u256_max_hex_str: String = module.heap.asc_get(u256_max_hex_ptr);
454+
assert_eq!(
455+
u256_max_hex_str,
456+
"0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"
457+
);
458+
}

runtime/wasm/wasm_test/abi_classes.ts

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,24 +12,23 @@ const array_buffer_header_size = 8;
1212
export function test_address(address: Address): Address {
1313
let new_address = address.subarray();
1414

15-
// Add 1 to the first and last bytes.
15+
// Add 1 to the first and last byte.
1616
new_address[0] += 1;
1717
new_address[address.length - 1] += 1;
1818

1919
return new_address
2020
}
2121

22-
// Sequence of 4 `u64`s.
23-
type Uint = Uint64Array;
22+
// Sequence of 32 `u8`s.
23+
type Uint = Uint8Array;
2424

25-
// Clone the Uint to a new buffer, add 1 to the first and last `u64`s and return
25+
// Clone the Uint to a new buffer, add 1 to the first and last `u8`s and return
2626
// the new Uint.
2727
export function test_uint(address: Uint): Uint {
2828
let new_address = address.subarray();
2929

30-
// Add 1 to the first and last bytes.
30+
// Add 1 to the first byte.
3131
new_address[0] += 1;
32-
new_address[address.length - 1] += 1;
3332

3433
return new_address
3534
}
-545 Bytes
Binary file not shown.
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import "allocator/arena";
2+
3+
export { allocate_memory };
4+
5+
declare namespace typeConversion {
6+
function i32ToBigInt(i: i32): Uint8Array
7+
function bigIntToI32(n: Uint8Array): i32
8+
}
9+
10+
export function big_int_to_i32(n: Uint8Array): i32 {
11+
return typeConversion.bigIntToI32(n)
12+
}
13+
14+
export function i32_to_big_int(i: i32): Uint8Array {
15+
return typeConversion.i32ToBigInt(i)
16+
}
591 Bytes
Binary file not shown.
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import "allocator/arena";
2+
3+
export { allocate_memory };
4+
5+
declare namespace typeConversion {
6+
function bigIntToHex(n: Uint8Array): String
7+
}
8+
9+
export function big_int_to_hex(n: Uint8Array): String {
10+
return typeConversion.bigIntToHex(n)
11+
}
441 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)