Commit 594f919 1 parent dc6f228 commit 594f919 Copy full SHA for 594f919
File tree 8 files changed +44
-11
lines changed
integration-tests/file-data-sources/src
8 files changed +44
-11
lines changed Original file line number Diff line number Diff line change @@ -129,6 +129,11 @@ pub struct EntityKey {
129
129
/// ID of the individual entity.
130
130
pub entity_id : Word ,
131
131
132
+ /// This is the causality region of the data source that created the entity.
133
+ ///
134
+ /// In the case of an entity lookup, this is the causality region of the data source that is
135
+ /// doing the lookup. So if the entity exists but was created on a different causality region,
136
+ /// the lookup will return empty.
132
137
pub causality_region : CausalityRegion ,
133
138
}
134
139
Original file line number Diff line number Diff line change @@ -51,12 +51,11 @@ impl CausalityRegion {
51
51
}
52
52
53
53
pub fn from_entity ( entity : & Entity ) -> Self {
54
- CausalityRegion (
55
- entity
56
- . get ( "causality_region" )
57
- . and_then ( |v| v. as_int ( ) )
58
- . unwrap_or ( 0 ) ,
59
- )
54
+ entity
55
+ . get ( "causality_region" )
56
+ . and_then ( |v| v. as_int ( ) )
57
+ . map ( CausalityRegion )
58
+ . unwrap_or ( CausalityRegion :: ONCHAIN )
60
59
}
61
60
}
62
61
Original file line number Diff line number Diff line change 1
1
use cid:: Cid ;
2
2
3
- use crate :: { components:: subgraph:: Entity , ipfs_client:: CidFile , prelude:: Link } ;
3
+ use crate :: {
4
+ blockchain:: mock:: { MockBlockchain , MockDataSource } ,
5
+ components:: subgraph:: Entity ,
6
+ ipfs_client:: CidFile ,
7
+ prelude:: Link ,
8
+ } ;
4
9
5
10
use super :: {
6
11
offchain:: { Mapping , Source } ,
@@ -45,6 +50,21 @@ fn offchain_mark_processed_error() {
45
50
x. mark_processed_at ( -1 )
46
51
}
47
52
53
+ #[ test]
54
+ fn data_source_helpers ( ) {
55
+ let offchain = new_datasource ( ) ;
56
+ let offchain_ds = DataSource :: < MockBlockchain > :: Offchain ( offchain. clone ( ) ) ;
57
+ assert ! ( offchain_ds. causality_region( ) == offchain. causality_region) ;
58
+ assert ! ( offchain_ds
59
+ . as_offchain( )
60
+ . unwrap( )
61
+ . is_duplicate_of( & offchain) ) ;
62
+
63
+ let onchain = DataSource :: < MockBlockchain > :: Onchain ( MockDataSource ) ;
64
+ assert ! ( onchain. causality_region( ) == CausalityRegion :: ONCHAIN ) ;
65
+ assert ! ( onchain. as_offchain( ) . is_none( ) ) ;
66
+ }
67
+
48
68
fn new_datasource ( ) -> offchain:: DataSource {
49
69
offchain:: DataSource :: new (
50
70
"theKind" . into ( ) ,
Original file line number Diff line number Diff line change @@ -1053,8 +1053,8 @@ impl DeploymentStore {
1053
1053
layout. find ( & conn, & key, block)
1054
1054
}
1055
1055
1056
- /// Retrieve all the entities matching `ids_for_type` from the
1057
- /// deployment `site`. Only consider entities as of the given `block`
1056
+ /// Retrieve all the entities matching `ids_for_type`, both the type and causality region, from
1057
+ /// the deployment `site`. Only consider entities as of the given `block`
1058
1058
pub ( crate ) fn get_many (
1059
1059
& self ,
1060
1060
site : Arc < Site > ,
Original file line number Diff line number Diff line change @@ -502,6 +502,7 @@ impl Layout {
502
502
. transpose ( )
503
503
}
504
504
505
+ // An optimization when looking up multiple entities, it will generate a single sql query using `UNION ALL`.
505
506
pub fn find_many (
506
507
& self ,
507
508
conn : & PgConnection ,
@@ -1229,7 +1230,7 @@ pub struct Table {
1229
1230
pub ( crate ) immutable : bool ,
1230
1231
1231
1232
/// Whether this table has an explicit `causality_region` column. If `false`, then the column is
1232
- /// not present and the causality region for all rows is implicitly `0`.
1233
+ /// not present and the causality region for all rows is implicitly `0` (equivalent to CasualityRegion::ONCHAIN) .
1233
1234
pub ( crate ) has_causality_region : bool ,
1234
1235
}
1235
1236
Original file line number Diff line number Diff line change @@ -253,6 +253,7 @@ fn find() {
253
253
. expect ( "Failed to read Thing[deadbeef]" )
254
254
. unwrap ( ) ;
255
255
assert_entity_eq ! ( scrub( & * BEEF_ENTITY ) , entity) ;
256
+ assert ! ( CausalityRegion :: from_entity( & entity) == CausalityRegion :: ONCHAIN ) ;
256
257
257
258
// Find non-existing entity
258
259
let entity = layout
Original file line number Diff line number Diff line change @@ -17,6 +17,9 @@ export function handleBlock(block: ethereum.Block): void {
17
17
}
18
18
19
19
if ( block . number == BigInt . fromI32 ( 1 ) ) {
20
+ let entity = IpfsFile . load ( "onchain" ) !
21
+ assert ( entity . content == "onchain" )
22
+
20
23
// The test assumes file data sources are processed in the block in which they are created.
21
24
// So the ds created at block 0 will have been processed.
22
25
//
@@ -71,6 +74,10 @@ export function handleFile(data: Bytes): void {
71
74
let entity = new IpfsFile ( dataSource . stringParam ( ) )
72
75
entity . content = data . toString ( )
73
76
entity . save ( )
77
+
78
+ // Test that an offchain data source can load its own entities
79
+ let loaded_entity = IpfsFile . load ( dataSource . stringParam ( ) ) !
80
+ assert ( loaded_entity . content == entity . content )
74
81
}
75
82
76
83
export function handleFile1 ( data : Bytes ) : void {
Original file line number Diff line number Diff line change @@ -213,7 +213,7 @@ async fn file_data_sources() {
213
213
let stop_block = test_ptr ( 5 ) ;
214
214
let err = ctx. start_and_sync_to_error ( stop_block. clone ( ) ) . await ;
215
215
let message = "entity type `IpfsFile1` is not on the 'entities' list for data source `File2`. \
216
- Hint: Add `IpfsFile1` to the 'entities' list, which currently is: `IpfsFile`.\t wasm backtrace:\t 0: 0x35a8 - <unknown>!src/mapping/handleFile1\t in handler `handleFile1` at block #5 ()". to_string ( ) ;
216
+ Hint: Add `IpfsFile1` to the 'entities' list, which currently is: `IpfsFile`.\t wasm backtrace:\t 0: 0x365d - <unknown>!src/mapping/handleFile1\t in handler `handleFile1` at block #5 ()". to_string ( ) ;
217
217
let expected_err = SubgraphError {
218
218
subgraph_id : ctx. deployment . hash . clone ( ) ,
219
219
message,
You can’t perform that action at this time.
0 commit comments