Skip to content

Commit 019fc4f

Browse files
committed
substreams: Make sure Bytes id get prefixed with 0x
Fixes #4661
1 parent aa9db72 commit 019fc4f

File tree

1 file changed

+28
-5
lines changed

1 file changed

+28
-5
lines changed

chain/substreams/src/trigger.rs

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,10 @@ use graph::{
99
store::{DeploymentLocator, EntityKey, EntityType, SubgraphFork},
1010
subgraph::{MappingError, ProofOfIndexingEvent, SharedProofOfIndexing},
1111
},
12-
data::{store::scalar::Bytes, value::Word},
12+
data::{
13+
store::{scalar::Bytes, IdType},
14+
value::Word,
15+
},
1316
data_source::{self, CausalityRegion},
1417
prelude::{
1518
anyhow, async_trait, BigDecimal, BigInt, BlockHash, BlockNumber, BlockState,
@@ -194,10 +197,30 @@ where
194197
return Err(MappingError::Unknown(anyhow!("Detected UNSET entity operation, either a server error or there's a new type of operation and we're running an outdated protobuf")));
195198
}
196199
Operation::Create | Operation::Update => {
197-
let entity_type: &str = &entity_change.entity;
198-
let entity_id: String = entity_change.id.clone();
200+
let schema = state.entity_cache.schema.as_ref();
201+
let entity_type = EntityType::new(entity_change.entity.to_string());
202+
// Make sure that the `entity_id` gets set to a value
203+
// that is safe for roundtrips through the database. In
204+
// particular, if the type of the id is `Bytes`, we have
205+
// to make sure that the `entity_id` starts with `0x` as
206+
// that will be what the key for such an entity have
207+
// when it is read from the database.
208+
//
209+
// Needless to say, this is a very ugly hack, and the
210+
// real fix is what's described in [this
211+
// issue](https://github.com/graphprotocol/graph-node/issues/4663)
212+
let entity_id: String = match schema.id_type(&entity_type)? {
213+
IdType::String => entity_change.id.clone(),
214+
IdType::Bytes => {
215+
if entity_change.id.starts_with("0x") {
216+
entity_change.id.clone()
217+
} else {
218+
format!("0x{}", entity_change.id)
219+
}
220+
}
221+
};
199222
let key = EntityKey {
200-
entity_type: EntityType::new(entity_type.to_string()),
223+
entity_type: entity_type.clone(),
201224
entity_id: entity_id.clone().into(),
202225
causality_region: CausalityRegion::ONCHAIN, // Substreams don't currently support offchain data
203226
};
@@ -220,7 +243,7 @@ where
220243
write_poi_event(
221244
proof_of_indexing,
222245
&ProofOfIndexingEvent::SetEntity {
223-
entity_type,
246+
entity_type: entity_type.as_str(),
224247
id: &entity_id,
225248
// TODO: This should be an entity so we do not have to build the intermediate HashMap
226249
data: &data,

0 commit comments

Comments
 (0)