Skip to content

Commit 5910d01

Browse files
author
Zoran Cvetkov
committed
simplify
1 parent 270356d commit 5910d01

File tree

7 files changed

+40
-38
lines changed

7 files changed

+40
-38
lines changed

chain/substreams/src/trigger.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ use graph::{
88
subgraph::{MappingError, ProofOfIndexingEvent, SharedProofOfIndexing},
99
trigger_processor::HostedTrigger,
1010
},
11-
data::store::EntityV,
1211
prelude::{
1312
anyhow, async_trait, BlockHash, BlockNumber, BlockState, CheapClone, RuntimeHostBuilder,
1413
},
@@ -238,8 +237,7 @@ where
238237
logger,
239238
);
240239

241-
let vid = state.next_vid(block.number);
242-
state.entity_cache.set(key, EntityV::new(entity, vid))?;
240+
state.entity_cache.set(key, entity, block.number)?;
243241
}
244242
ParsedChanges::Delete(entity_key) => {
245243
let entity_type = entity_key.entity_type.cheap_clone();

core/src/subgraph/runner.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ use graph::components::{
1818
subgraph::{MappingError, PoICausalityRegion, ProofOfIndexing, SharedProofOfIndexing},
1919
};
2020
use graph::data::store::scalar::Bytes;
21-
use graph::data::store::EntityV;
2221
use graph::data::subgraph::{
2322
schema::{SubgraphError, SubgraphHealth},
2423
SubgraphFeature,
@@ -1604,6 +1603,7 @@ async fn update_proof_of_indexing(
16041603
key: EntityKey,
16051604
digest: Bytes,
16061605
block_time: BlockTime,
1606+
block: BlockNumber,
16071607
) -> Result<(), Error> {
16081608
let digest_name = entity_cache.schema.poi_digest();
16091609
let mut data = vec![
@@ -1618,12 +1618,12 @@ async fn update_proof_of_indexing(
16181618
data.push((entity_cache.schema.poi_block_time(), block_time));
16191619
}
16201620
let poi = entity_cache.make_entity(data)?;
1621-
// VID is autogenerated for POI table and our input is ignored
1622-
entity_cache.set(key, EntityV::new(poi, 0))
1621+
entity_cache.set(key, poi, block)
16231622
}
16241623

16251624
let _section_guard = stopwatch.start_section("update_proof_of_indexing");
16261625

1626+
let block_number = proof_of_indexing.get_block();
16271627
let mut proof_of_indexing = proof_of_indexing.take();
16281628

16291629
for (causality_region, stream) in proof_of_indexing.drain() {
@@ -1659,6 +1659,7 @@ async fn update_proof_of_indexing(
16591659
entity_key,
16601660
updated_proof_of_indexing,
16611661
block_time,
1662+
block_number,
16621663
)?;
16631664
}
16641665

graph/src/components/store/entity_cache.rs

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,10 @@ pub struct EntityCache {
105105
/// generated IDs, the `EntityCache` needs to be newly instantiated for
106106
/// each block
107107
seq: u32,
108+
109+
// Sequence number of the next VID value for this block. The value written
110+
// in the database consist of a block number and this SEQ number.
111+
pub vid_seq: i32,
108112
}
109113

110114
impl Debug for EntityCache {
@@ -132,6 +136,7 @@ impl EntityCache {
132136
schema: store.input_schema(),
133137
store,
134138
seq: 0,
139+
vid_seq: 0,
135140
}
136141
}
137142

@@ -152,6 +157,7 @@ impl EntityCache {
152157
schema: store.input_schema(),
153158
store,
154159
seq: 0,
160+
vid_seq: 0,
155161
}
156162
}
157163

@@ -349,9 +355,19 @@ impl EntityCache {
349355
/// with existing data. The entity will be validated against the
350356
/// subgraph schema, and any errors will result in an `Err` being
351357
/// returned.
352-
pub fn set(&mut self, key: EntityKey, entity: EntityV) -> Result<(), anyhow::Error> {
358+
pub fn set(
359+
&mut self,
360+
key: EntityKey,
361+
entity: Entity,
362+
block: BlockNumber,
363+
) -> Result<(), anyhow::Error> {
353364
// check the validate for derived fields
354-
let is_valid = entity.e.validate(&key).is_ok();
365+
let is_valid = entity.validate(&key).is_ok();
366+
367+
//The next VID is based on a block number and a sequence withing the block
368+
let vid = ((block as i64) << 32) + self.vid_seq as i64;
369+
self.vid_seq += 1;
370+
let entity = EntityV::new(entity, vid);
355371

356372
self.entity_op(key.clone(), EntityOp::Update(entity));
357373

graph/src/components/subgraph/instance.rs

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,6 @@ pub struct BlockState {
7878
// data source that have been processed.
7979
pub processed_data_sources: Vec<StoredDynamicDataSource>,
8080

81-
pub vid_seq: i32,
82-
8381
// Marks whether a handler is currently executing.
8482
in_handler: bool,
8583

@@ -95,7 +93,6 @@ impl BlockState {
9593
persisted_data_sources: Vec::new(),
9694
handler_created_data_sources: Vec::new(),
9795
processed_data_sources: Vec::new(),
98-
vid_seq: 0,
9996
in_handler: false,
10097
metrics: BlockStateMetrics::new(),
10198
}
@@ -113,7 +110,6 @@ impl BlockState {
113110
persisted_data_sources,
114111
handler_created_data_sources,
115112
processed_data_sources,
116-
vid_seq: _,
117113
in_handler,
118114
metrics,
119115
} = self;
@@ -183,10 +179,4 @@ impl BlockState {
183179
pub fn persist_data_source(&mut self, ds: StoredDynamicDataSource) {
184180
self.persisted_data_sources.push(ds)
185181
}
186-
187-
pub fn next_vid(&mut self, block_number: BlockNumber) -> i64 {
188-
let vid = ((block_number as i64) << 32) + self.vid_seq as i64;
189-
self.vid_seq += 1;
190-
vid
191-
}
192182
}

graph/src/components/subgraph/proof_of_indexing/online.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,10 @@ impl ProofOfIndexing {
242242
pub fn take(self) -> HashMap<Id, BlockEventStream> {
243243
self.per_causality_region
244244
}
245+
246+
pub fn get_block(&self) -> BlockNumber {
247+
self.block_number
248+
}
245249
}
246250

247251
pub struct ProofOfIndexingFinisher {

runtime/wasm/src/host_exports.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ use graph::components::store::{EnsLookup, GetScope, LoadRelatedRequest};
1818
use graph::components::subgraph::{
1919
InstanceDSTemplate, PoICausalityRegion, ProofOfIndexingEvent, SharedProofOfIndexing,
2020
};
21-
use graph::data::store::{self, EntityV};
21+
use graph::data::store::{self};
2222
use graph::data_source::{CausalityRegion, DataSource, EntityTypeAccess};
2323
use graph::ensure;
2424
use graph::prelude::ethabi::param_type::Reader;
@@ -248,7 +248,6 @@ impl HostExports {
248248
gas: &GasCounter,
249249
) -> Result<(), HostExportError> {
250250
let entity_type = state.entity_cache.schema.entity_type(&entity_type)?;
251-
let vid = state.next_vid(block);
252251

253252
Self::expect_object_type(&entity_type, "set")?;
254253

@@ -351,7 +350,7 @@ impl HostExports {
351350

352351
state.metrics.track_entity_write(&entity_type, &entity);
353352

354-
state.entity_cache.set(key, EntityV::new(entity, vid))?;
353+
state.entity_cache.set(key, entity, block)?;
355354

356355
Ok(())
357356
}

store/test-store/tests/graph/entity_cache.rs

Lines changed: 11 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -210,21 +210,21 @@ fn insert_modifications() {
210210
let mogwai_data = entity! { SCHEMA => id: "mogwai", name: "Mogwai" };
211211
let mogwai_key = make_band_key("mogwai");
212212
cache
213-
.set(mogwai_key.clone(), EntityV::new(mogwai_data.clone(), 0))
213+
.set(mogwai_key.clone(), mogwai_data.clone(), 0)
214214
.unwrap();
215215

216216
let sigurros_data = entity! { SCHEMA => id: "sigurros", name: "Sigur Ros" };
217217
let sigurros_key = make_band_key("sigurros");
218218
cache
219-
.set(sigurros_key.clone(), EntityV::new(sigurros_data.clone(), 0))
219+
.set(sigurros_key.clone(), sigurros_data.clone(), 0)
220220
.unwrap();
221221

222222
let result = cache.as_modifications(0);
223223
assert_eq!(
224224
sort_by_entity_key(result.unwrap().modifications),
225225
sort_by_entity_key(vec![
226226
EntityModification::insert(mogwai_key, mogwai_data, 0, 0),
227-
EntityModification::insert(sigurros_key, sigurros_data, 0, 0)
227+
EntityModification::insert(sigurros_key, sigurros_data, 0, 1)
228228
])
229229
);
230230
}
@@ -256,21 +256,21 @@ fn overwrite_modifications() {
256256
let mogwai_data = entity! { SCHEMA => id: "mogwai", name: "Mogwai", founded: 1995 };
257257
let mogwai_key = make_band_key("mogwai");
258258
cache
259-
.set(mogwai_key.clone(), EntityV::new(mogwai_data.clone(), 0))
259+
.set(mogwai_key.clone(), mogwai_data.clone(), 0)
260260
.unwrap();
261261

262262
let sigurros_data = entity! { SCHEMA => id: "sigurros", name: "Sigur Ros", founded: 1994 };
263263
let sigurros_key = make_band_key("sigurros");
264264
cache
265-
.set(sigurros_key.clone(), EntityV::new(sigurros_data.clone(), 0))
265+
.set(sigurros_key.clone(), sigurros_data.clone(), 0)
266266
.unwrap();
267267

268268
let result = cache.as_modifications(0);
269269
assert_eq!(
270270
sort_by_entity_key(result.unwrap().modifications),
271271
sort_by_entity_key(vec![
272272
EntityModification::overwrite(mogwai_key, mogwai_data, 0, 0),
273-
EntityModification::overwrite(sigurros_key, sigurros_data, 0, 0)
273+
EntityModification::overwrite(sigurros_key, sigurros_data, 0, 1)
274274
])
275275
);
276276
}
@@ -293,14 +293,12 @@ fn consecutive_modifications() {
293293
let update_data =
294294
entity! { SCHEMA => id: "mogwai", founded: 1995, label: "Rock Action Records" };
295295
let update_key = make_band_key("mogwai");
296-
cache.set(update_key, EntityV::new(update_data, 0)).unwrap();
296+
cache.set(update_key, update_data, 0).unwrap();
297297

298298
// Then, just reset the "label".
299299
let update_data = entity! { SCHEMA => id: "mogwai", label: Value::Null };
300300
let update_key = make_band_key("mogwai");
301-
cache
302-
.set(update_key.clone(), EntityV::new(update_data, 0))
303-
.unwrap();
301+
cache.set(update_key.clone(), update_data, 0).unwrap();
304302

305303
// We expect a single overwrite modification for the above that leaves "id"
306304
// and "name" untouched, sets "founded" and removes the "label" field.
@@ -721,9 +719,7 @@ fn scoped_get() {
721719
let account5 = ACCOUNT_TYPE.parse_id("5").unwrap();
722720
let wallet5 = create_wallet_entity("5", &account5, 100);
723721
let key5 = WALLET_TYPE.parse_key("5").unwrap();
724-
cache
725-
.set(key5.clone(), EntityV::new(wallet5.clone(), 5))
726-
.unwrap();
722+
cache.set(key5.clone(), wallet5.clone(), 0).unwrap();
727723

728724
// For the new entity, we can retrieve it with either scope
729725
let act5 = cache.get(&key5, GetScope::InBlock).unwrap();
@@ -744,9 +740,7 @@ fn scoped_get() {
744740
// But if it gets updated, it becomes visible with either scope
745741
let mut wallet1 = wallet1;
746742
wallet1.set("balance", 70).unwrap();
747-
cache
748-
.set(key1.clone(), EntityV::new(wallet1.clone(), 1))
749-
.unwrap();
743+
cache.set(key1.clone(), wallet1.clone(), 0).unwrap();
750744
let act1 = cache.get(&key1, GetScope::InBlock).unwrap();
751745
assert_eq!(Some(&wallet1), act1.as_ref().map(|e| e.as_ref()));
752746
let act1 = cache.get(&key1, GetScope::Store).unwrap();
@@ -793,6 +787,6 @@ fn no_interface_mods() {
793787

794788
let entity = entity! { LOAD_RELATED_SUBGRAPH => id: "1", balance: 100 };
795789

796-
cache.set(key, EntityV::new(entity, 0)).unwrap_err();
790+
cache.set(key, entity, 0).unwrap_err();
797791
})
798792
}

0 commit comments

Comments
 (0)