Skip to content

Commit

Permalink
Detect timeouts in asc_type_id (#4475)
Browse files Browse the repository at this point in the history
* refactor(runtime): Add sanity checks

* fix(runtime): Detect non-deterministic traps in asc_type_id

They would previously be unconditionally treated as deterministic.

* refactor(runtime): Change signatures from DeterministicHostError to HostExportError

This all falls out of changing the signature of `fn asc_type_id`.
  • Loading branch information
leoyvens authored Mar 22, 2023
1 parent 08f0281 commit 3be894f
Show file tree
Hide file tree
Showing 23 changed files with 252 additions and 209 deletions.
16 changes: 8 additions & 8 deletions chain/arweave/src/runtime/abi.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::codec;
use crate::trigger::TransactionWithBlockPtr;
use graph::runtime::gas::GasCounter;
use graph::runtime::{asc_new, AscHeap, AscPtr, DeterministicHostError, ToAscObj};
use graph::runtime::{asc_new, AscHeap, AscPtr, HostExportError, ToAscObj};
use graph_runtime_wasm::asc_abi::class::{Array, Uint8Array};

pub(crate) use super::generated::*;
Expand All @@ -11,7 +11,7 @@ impl ToAscObj<AscTag> for codec::Tag {
&self,
heap: &mut H,
gas: &GasCounter,
) -> Result<AscTag, DeterministicHostError> {
) -> Result<AscTag, HostExportError> {
Ok(AscTag {
name: asc_new(heap, self.name.as_slice(), gas)?,
value: asc_new(heap, self.value.as_slice(), gas)?,
Expand All @@ -24,7 +24,7 @@ impl ToAscObj<AscTransactionArray> for Vec<Vec<u8>> {
&self,
heap: &mut H,
gas: &GasCounter,
) -> Result<AscTransactionArray, DeterministicHostError> {
) -> Result<AscTransactionArray, HostExportError> {
let content = self
.iter()
.map(|x| asc_new(heap, x.as_slice(), gas))
Expand All @@ -38,7 +38,7 @@ impl ToAscObj<AscTagArray> for Vec<codec::Tag> {
&self,
heap: &mut H,
gas: &GasCounter,
) -> Result<AscTagArray, DeterministicHostError> {
) -> Result<AscTagArray, HostExportError> {
let content = self
.iter()
.map(|x| asc_new(heap, x, gas))
Expand All @@ -52,7 +52,7 @@ impl ToAscObj<AscProofOfAccess> for codec::ProofOfAccess {
&self,
heap: &mut H,
gas: &GasCounter,
) -> Result<AscProofOfAccess, DeterministicHostError> {
) -> Result<AscProofOfAccess, HostExportError> {
Ok(AscProofOfAccess {
option: asc_new(heap, &self.option, gas)?,
tx_path: asc_new(heap, self.tx_path.as_slice(), gas)?,
Expand All @@ -67,7 +67,7 @@ impl ToAscObj<AscTransaction> for codec::Transaction {
&self,
heap: &mut H,
gas: &GasCounter,
) -> Result<AscTransaction, DeterministicHostError> {
) -> Result<AscTransaction, HostExportError> {
Ok(AscTransaction {
format: self.format,
id: asc_new(heap, self.id.as_slice(), gas)?,
Expand Down Expand Up @@ -108,7 +108,7 @@ impl ToAscObj<AscBlock> for codec::Block {
&self,
heap: &mut H,
gas: &GasCounter,
) -> Result<AscBlock, DeterministicHostError> {
) -> Result<AscBlock, HostExportError> {
Ok(AscBlock {
indep_hash: asc_new(heap, self.indep_hash.as_slice(), gas)?,
nonce: asc_new(heap, self.nonce.as_slice(), gas)?,
Expand Down Expand Up @@ -182,7 +182,7 @@ impl ToAscObj<AscTransactionWithBlockPtr> for TransactionWithBlockPtr {
&self,
heap: &mut H,
gas: &GasCounter,
) -> Result<AscTransactionWithBlockPtr, DeterministicHostError> {
) -> Result<AscTransactionWithBlockPtr, HostExportError> {
Ok(AscTransactionWithBlockPtr {
tx: asc_new(heap, &self.tx.as_ref(), gas)?,
block: asc_new(heap, self.block.as_ref(), gas)?,
Expand Down
4 changes: 2 additions & 2 deletions chain/arweave/src/trigger.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use graph::runtime::asc_new;
use graph::runtime::gas::GasCounter;
use graph::runtime::AscHeap;
use graph::runtime::AscPtr;
use graph::runtime::DeterministicHostError;
use graph::runtime::HostExportError;
use graph_runtime_wasm::module::ToAscPtr;
use std::{cmp::Ordering, sync::Arc};

Expand Down Expand Up @@ -38,7 +38,7 @@ impl ToAscPtr for ArweaveTrigger {
self,
heap: &mut H,
gas: &GasCounter,
) -> Result<AscPtr<()>, DeterministicHostError> {
) -> Result<AscPtr<()>, HostExportError> {
Ok(match self {
ArweaveTrigger::Block(block) => asc_new(heap, block.as_ref(), gas)?.erase(),
ArweaveTrigger::Transaction(tx) => asc_new(heap, tx.as_ref(), gas)?.erase(),
Expand Down
7 changes: 4 additions & 3 deletions chain/cosmos/src/runtime/abi.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use crate::protobuf::*;
use graph::runtime::HostExportError;
pub use graph::semver::Version;

pub use graph::runtime::{
Expand All @@ -16,7 +17,7 @@ impl ToAscObj<AscBytesArray> for Vec<Vec<u8>> {
&self,
heap: &mut H,
gas: &GasCounter,
) -> Result<AscBytesArray, DeterministicHostError> {
) -> Result<AscBytesArray, HostExportError> {
let content: Result<Vec<_>, _> = self
.iter()
.map(|x| asc_new(heap, &graph_runtime_wasm::asc_abi::class::Bytes(x), gas))
Expand Down Expand Up @@ -52,7 +53,7 @@ impl ToAscObj<AscAny> for prost_types::Any {
&self,
heap: &mut H,
gas: &GasCounter,
) -> Result<AscAny, DeterministicHostError> {
) -> Result<AscAny, HostExportError> {
Ok(AscAny {
type_url: asc_new(heap, &self.type_url, gas)?,
value: asc_new(
Expand All @@ -71,7 +72,7 @@ impl ToAscObj<AscAnyArray> for Vec<prost_types::Any> {
&self,
heap: &mut H,
gas: &GasCounter,
) -> Result<AscAnyArray, DeterministicHostError> {
) -> Result<AscAnyArray, HostExportError> {
let content: Result<Vec<_>, _> = self.iter().map(|x| asc_new(heap, x, gas)).collect();

Ok(AscAnyArray(Array::new(&content?, heap, gas)?))
Expand Down
5 changes: 3 additions & 2 deletions chain/cosmos/src/trigger.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ use std::{cmp::Ordering, sync::Arc};
use graph::blockchain::{Block, BlockHash, TriggerData};
use graph::cheap_clone::CheapClone;
use graph::prelude::{BlockNumber, Error};
use graph::runtime::{asc_new, gas::GasCounter, AscHeap, AscPtr, DeterministicHostError};
use graph::runtime::HostExportError;
use graph::runtime::{asc_new, gas::GasCounter, AscHeap, AscPtr};
use graph_runtime_wasm::module::ToAscPtr;

use crate::codec;
Expand Down Expand Up @@ -42,7 +43,7 @@ impl ToAscPtr for CosmosTrigger {
self,
heap: &mut H,
gas: &GasCounter,
) -> Result<AscPtr<()>, DeterministicHostError> {
) -> Result<AscPtr<()>, HostExportError> {
Ok(match self {
CosmosTrigger::Block(block) => asc_new(heap, block.as_ref(), gas)?.erase(),
CosmosTrigger::Event { event_data, .. } => {
Expand Down
34 changes: 17 additions & 17 deletions chain/ethereum/src/runtime/abi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use graph::{
},
runtime::{
asc_get, asc_new, gas::GasCounter, AscHeap, AscIndexId, AscPtr, AscType,
DeterministicHostError, FromAscObj, IndexForAscTypeId, ToAscObj,
DeterministicHostError, FromAscObj, HostExportError, IndexForAscTypeId, ToAscObj,
},
};
use graph_runtime_derive::AscType;
Expand Down Expand Up @@ -42,7 +42,7 @@ impl ToAscObj<AscLogParamArray> for Vec<ethabi::LogParam> {
&self,
heap: &mut H,
gas: &GasCounter,
) -> Result<AscLogParamArray, DeterministicHostError> {
) -> Result<AscLogParamArray, HostExportError> {
let content: Result<Vec<_>, _> = self.iter().map(|x| asc_new(heap, x, gas)).collect();
let content = content?;
Ok(AscLogParamArray(Array::new(&content, heap, gas)?))
Expand Down Expand Up @@ -73,7 +73,7 @@ impl ToAscObj<AscTopicArray> for Vec<H256> {
&self,
heap: &mut H,
gas: &GasCounter,
) -> Result<AscTopicArray, DeterministicHostError> {
) -> Result<AscTopicArray, HostExportError> {
let topics = self
.iter()
.map(|topic| asc_new(heap, topic, gas))
Expand Down Expand Up @@ -106,7 +106,7 @@ impl ToAscObj<AscLogArray> for Vec<Log> {
&self,
heap: &mut H,
gas: &GasCounter,
) -> Result<AscLogArray, DeterministicHostError> {
) -> Result<AscLogArray, HostExportError> {
let logs = self
.iter()
.map(|log| asc_new(heap, &log, gas))
Expand Down Expand Up @@ -416,7 +416,7 @@ impl ToAscObj<AscEthereumBlock> for EthereumBlockData {
&self,
heap: &mut H,
gas: &GasCounter,
) -> Result<AscEthereumBlock, DeterministicHostError> {
) -> Result<AscEthereumBlock, HostExportError> {
Ok(AscEthereumBlock {
hash: asc_new(heap, &self.hash, gas)?,
parent_hash: asc_new(heap, &self.parent_hash, gas)?,
Expand Down Expand Up @@ -448,7 +448,7 @@ impl ToAscObj<AscEthereumBlock_0_0_6> for EthereumBlockData {
&self,
heap: &mut H,
gas: &GasCounter,
) -> Result<AscEthereumBlock_0_0_6, DeterministicHostError> {
) -> Result<AscEthereumBlock_0_0_6, HostExportError> {
Ok(AscEthereumBlock_0_0_6 {
hash: asc_new(heap, &self.hash, gas)?,
parent_hash: asc_new(heap, &self.parent_hash, gas)?,
Expand Down Expand Up @@ -484,7 +484,7 @@ impl ToAscObj<AscEthereumTransaction_0_0_1> for EthereumTransactionData {
&self,
heap: &mut H,
gas: &GasCounter,
) -> Result<AscEthereumTransaction_0_0_1, DeterministicHostError> {
) -> Result<AscEthereumTransaction_0_0_1, HostExportError> {
Ok(AscEthereumTransaction_0_0_1 {
hash: asc_new(heap, &self.hash, gas)?,
index: asc_new(heap, &BigInt::from(self.index), gas)?,
Expand All @@ -505,7 +505,7 @@ impl ToAscObj<AscEthereumTransaction_0_0_2> for EthereumTransactionData {
&self,
heap: &mut H,
gas: &GasCounter,
) -> Result<AscEthereumTransaction_0_0_2, DeterministicHostError> {
) -> Result<AscEthereumTransaction_0_0_2, HostExportError> {
Ok(AscEthereumTransaction_0_0_2 {
hash: asc_new(heap, &self.hash, gas)?,
index: asc_new(heap, &BigInt::from(self.index), gas)?,
Expand All @@ -527,7 +527,7 @@ impl ToAscObj<AscEthereumTransaction_0_0_6> for EthereumTransactionData {
&self,
heap: &mut H,
gas: &GasCounter,
) -> Result<AscEthereumTransaction_0_0_6, DeterministicHostError> {
) -> Result<AscEthereumTransaction_0_0_6, HostExportError> {
Ok(AscEthereumTransaction_0_0_6 {
hash: asc_new(heap, &self.hash, gas)?,
index: asc_new(heap, &BigInt::from(self.index), gas)?,
Expand Down Expand Up @@ -556,7 +556,7 @@ where
&self,
heap: &mut H,
gas: &GasCounter,
) -> Result<AscEthereumEvent<T, B>, DeterministicHostError> {
) -> Result<AscEthereumEvent<T, B>, HostExportError> {
Ok(AscEthereumEvent {
address: asc_new(heap, &self.address, gas)?,
log_index: asc_new(heap, &BigInt::from_unsigned_u256(&self.log_index), gas)?,
Expand Down Expand Up @@ -589,7 +589,7 @@ where
&self,
heap: &mut H,
gas: &GasCounter,
) -> Result<AscEthereumEvent_0_0_7<T, B>, DeterministicHostError> {
) -> Result<AscEthereumEvent_0_0_7<T, B>, HostExportError> {
let (event_data, optional_receipt) = self;
let AscEthereumEvent {
address,
Expand Down Expand Up @@ -623,7 +623,7 @@ impl ToAscObj<AscEthereumLog> for Log {
&self,
heap: &mut H,
gas: &GasCounter,
) -> Result<AscEthereumLog, DeterministicHostError> {
) -> Result<AscEthereumLog, HostExportError> {
Ok(AscEthereumLog {
address: asc_new(heap, &self.address, gas)?,
topics: asc_new(heap, &self.topics, gas)?,
Expand Down Expand Up @@ -670,7 +670,7 @@ impl ToAscObj<AscEthereumTransactionReceipt> for &TransactionReceipt {
&self,
heap: &mut H,
gas: &GasCounter,
) -> Result<AscEthereumTransactionReceipt, DeterministicHostError> {
) -> Result<AscEthereumTransactionReceipt, HostExportError> {
Ok(AscEthereumTransactionReceipt {
transaction_hash: asc_new(heap, &self.transaction_hash, gas)?,
transaction_index: asc_new(heap, &BigInt::from(self.transaction_index), gas)?,
Expand Down Expand Up @@ -714,7 +714,7 @@ impl ToAscObj<AscEthereumCall> for EthereumCallData {
&self,
heap: &mut H,
gas: &GasCounter,
) -> Result<AscEthereumCall, DeterministicHostError> {
) -> Result<AscEthereumCall, HostExportError> {
Ok(AscEthereumCall {
address: asc_new(heap, &self.to, gas)?,
block: asc_new(heap, &self.block, gas)?,
Expand All @@ -734,7 +734,7 @@ impl ToAscObj<AscEthereumCall_0_0_3<AscEthereumTransaction_0_0_2, AscEthereumBlo
gas: &GasCounter,
) -> Result<
AscEthereumCall_0_0_3<AscEthereumTransaction_0_0_2, AscEthereumBlock>,
DeterministicHostError,
HostExportError,
> {
Ok(AscEthereumCall_0_0_3 {
to: asc_new(heap, &self.to, gas)?,
Expand All @@ -756,7 +756,7 @@ impl ToAscObj<AscEthereumCall_0_0_3<AscEthereumTransaction_0_0_6, AscEthereumBlo
gas: &GasCounter,
) -> Result<
AscEthereumCall_0_0_3<AscEthereumTransaction_0_0_6, AscEthereumBlock_0_0_6>,
DeterministicHostError,
HostExportError,
> {
Ok(AscEthereumCall_0_0_3 {
to: asc_new(heap, &self.to, gas)?,
Expand All @@ -774,7 +774,7 @@ impl ToAscObj<AscLogParam> for ethabi::LogParam {
&self,
heap: &mut H,
gas: &GasCounter,
) -> Result<AscLogParam, DeterministicHostError> {
) -> Result<AscLogParam, HostExportError> {
Ok(AscLogParam {
name: asc_new(heap, self.name.as_str(), gas)?,
value: asc_new(heap, &self.value, gas)?,
Expand Down
4 changes: 2 additions & 2 deletions chain/ethereum/src/trigger.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ use graph::runtime::asc_new;
use graph::runtime::gas::GasCounter;
use graph::runtime::AscHeap;
use graph::runtime::AscPtr;
use graph::runtime::DeterministicHostError;
use graph::runtime::HostExportError;
use graph::semver::Version;
use graph_runtime_wasm::module::ToAscPtr;
use std::convert::TryFrom;
Expand Down Expand Up @@ -116,7 +116,7 @@ impl ToAscPtr for MappingTrigger {
self,
heap: &mut H,
gas: &GasCounter,
) -> Result<AscPtr<()>, DeterministicHostError> {
) -> Result<AscPtr<()>, HostExportError> {
Ok(match self {
MappingTrigger::Log {
block,
Expand Down
Loading

0 comments on commit 3be894f

Please sign in to comment.