Skip to content

Commit aa65cf6

Browse files
leoyvensFilippo Costa
authored andcommitted
Limit big int (#4594)
* runtime, graph: Limit bigint size * fix(runtime): make gas complexity of big int and decimal quadratic
1 parent 3861a0c commit aa65cf6

File tree

17 files changed

+213
-131
lines changed

17 files changed

+213
-131
lines changed

chain/ethereum/src/runtime/abi.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -489,7 +489,7 @@ impl ToAscObj<AscEthereumTransaction_0_0_1> for EthereumTransactionData {
489489
) -> Result<AscEthereumTransaction_0_0_1, HostExportError> {
490490
Ok(AscEthereumTransaction_0_0_1 {
491491
hash: asc_new(heap, &self.hash, gas)?,
492-
index: asc_new(heap, &BigInt::from(self.index), gas)?,
492+
index: asc_new(heap, &BigInt::from_unsigned_u128(self.index), gas)?,
493493
from: asc_new(heap, &self.from, gas)?,
494494
to: self
495495
.to
@@ -510,7 +510,7 @@ impl ToAscObj<AscEthereumTransaction_0_0_2> for EthereumTransactionData {
510510
) -> Result<AscEthereumTransaction_0_0_2, HostExportError> {
511511
Ok(AscEthereumTransaction_0_0_2 {
512512
hash: asc_new(heap, &self.hash, gas)?,
513-
index: asc_new(heap, &BigInt::from(self.index), gas)?,
513+
index: asc_new(heap, &BigInt::from_unsigned_u128(self.index), gas)?,
514514
from: asc_new(heap, &self.from, gas)?,
515515
to: self
516516
.to
@@ -532,7 +532,7 @@ impl ToAscObj<AscEthereumTransaction_0_0_6> for EthereumTransactionData {
532532
) -> Result<AscEthereumTransaction_0_0_6, HostExportError> {
533533
Ok(AscEthereumTransaction_0_0_6 {
534534
hash: asc_new(heap, &self.hash, gas)?,
535-
index: asc_new(heap, &BigInt::from(self.index), gas)?,
535+
index: asc_new(heap, &BigInt::from_unsigned_u128(self.index), gas)?,
536536
from: asc_new(heap, &self.from, gas)?,
537537
to: self
538538
.to

graph/examples/stress.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -249,7 +249,7 @@ impl Template for BigInt {
249249
}
250250
None => 1,
251251
};
252-
BigInt::from(3u64).pow(size as u8) * BigInt::from(f)
252+
BigInt::from(3u64).pow(size as u8).unwrap() * BigInt::from(f)
253253
}
254254

255255
fn sample(&self, size: usize, rng: Option<&mut SmallRng>) -> Box<Self> {
@@ -274,7 +274,7 @@ impl Template for BigDecimal {
274274
Some(rng) => rng.gen_range(-100..=100),
275275
None => 1,
276276
};
277-
let bi = BigInt::from(3u64).pow(size as u8) * BigInt::from(f);
277+
let bi = BigInt::from(3u64).pow(size as u8).unwrap() * BigInt::from(f);
278278
BigDecimal::new(bi, exp)
279279
}
280280

graph/src/data/query/error.rs

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
use graphql_parser::Pos;
22
use hex::FromHexError;
3-
use num_bigint;
43
use serde::ser::*;
54
use std::collections::HashMap;
65
use std::error::Error;
@@ -292,12 +291,6 @@ impl From<FromHexError> for QueryExecutionError {
292291
}
293292
}
294293

295-
impl From<num_bigint::ParseBigIntError> for QueryExecutionError {
296-
fn from(e: num_bigint::ParseBigIntError) -> Self {
297-
QueryExecutionError::ValueParseError("BigInt".to_string(), format!("{}", e))
298-
}
299-
}
300-
301294
impl From<bigdecimal::ParseBigDecimalError> for QueryExecutionError {
302295
fn from(e: bigdecimal::ParseBigDecimalError) -> Self {
303296
QueryExecutionError::ValueParseError("BigDecimal".to_string(), format!("{}", e))

graph/src/data/store/ethereum.rs

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,6 @@
11
use super::scalar;
22
use crate::prelude::*;
3-
use web3::types::{Address, Bytes, H2048, H256, H64, U128, U256, U64};
4-
5-
impl From<U128> for Value {
6-
fn from(n: U128) -> Value {
7-
Value::BigInt(scalar::BigInt::from_signed_u256(&n.into()))
8-
}
9-
}
3+
use web3::types::{Address, Bytes, H2048, H256, H64, U64};
104

115
impl From<Address> for Value {
126
fn from(address: Address) -> Value {
@@ -43,9 +37,3 @@ impl From<U64> for Value {
4337
Value::BigInt(BigInt::from(n))
4438
}
4539
}
46-
47-
impl From<U256> for Value {
48-
fn from(n: U256) -> Value {
49-
Value::BigInt(BigInt::from_unsigned_u256(&n))
50-
}
51-
}

graph/src/data/store/mod.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -313,7 +313,9 @@ impl Value {
313313
// just a string.
314314
match n.as_str() {
315315
BYTES_SCALAR => Value::Bytes(scalar::Bytes::from_str(s)?),
316-
BIG_INT_SCALAR => Value::BigInt(scalar::BigInt::from_str(s)?),
316+
BIG_INT_SCALAR => Value::BigInt(scalar::BigInt::from_str(s).map_err(|e| {
317+
QueryExecutionError::ValueParseError("BigInt".to_string(), format!("{}", e))
318+
})?),
317319
BIG_DECIMAL_SCALAR => Value::BigDecimal(scalar::BigDecimal::from_str(s)?),
318320
_ => Value::String(s.clone()),
319321
}

0 commit comments

Comments
 (0)