-
Notifications
You must be signed in to change notification settings - Fork 1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Derived fields should respect causality region + Improve tests #5488
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,10 @@ | ||
type IpfsFile @entity { | ||
type FileEntity @entity { | ||
id: ID! | ||
content: String! | ||
foo: Foo @relation | ||
} | ||
|
||
type IpfsFile1 @entity { | ||
type Foo @entity { | ||
id: ID! | ||
content: String! | ||
ipfs: FileEntity @derivedFrom(field: "foo") | ||
} | ||
|
||
type SpawnTestEntity @entity { | ||
id: ID! | ||
content: String! | ||
context: String! | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,130 +4,144 @@ import { | |
BigInt, | ||
Bytes, | ||
DataSourceContext, | ||
store, | ||
log, | ||
} from "@graphprotocol/graph-ts"; | ||
import { TestEvent } from "../generated/Contract/Contract"; | ||
import { IpfsFile, IpfsFile1, SpawnTestEntity } from "../generated/schema"; | ||
|
||
// CID of `file-data-sources/abis/Contract.abi` after being processed by graph-cli. | ||
const KNOWN_CID = "QmQ2REmceVtzawp7yrnxLQXgNNCtFHEnig6fL9aqE1kcWq"; | ||
import { FileEntity, Foo } from "../generated/schema"; | ||
|
||
const ONCHAIN_FROM_OFFCHAIN = "CREATE_ONCHAIN_DATASOURCE_FROM_OFFCHAIN_HANDLER"; | ||
const CREATE_FILE = "CREATE_FILE"; | ||
// const CREATE_FILE_FROM_HANDLE_FILE = "CREATE_FILE_FROM_HANDLE_FILE"; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe remove it, if its not used. |
||
const CREATE_UNDEFINED_ENTITY = "CREATE_UNDEFINED_ENTITY"; | ||
const CREATE_CONFLICTING_ENTITY = "CREATE_CONFLICTING_ENTITY"; | ||
const SPAWN_FDS_FROM_OFFCHAIN_HANDLER = "SPAWN_FDS_FROM_OFFCHAIN_HANDLER"; | ||
const ACCESS_AND_UPDATE_OFFCHAIN_ENTITY_IN_ONCHAIN_HANDLER = | ||
"ACCESS_AND_UPDATE_OFFCHAIN_ENTITY_IN_ONCHAIN_HANDLER"; | ||
const ACCESS_FILE_ENTITY_THROUGH_DERIVED_FIELD = | ||
"ACCESS_FILE_ENTITY_THROUGH_DERIVED_FIELD"; | ||
|
||
const CREATE_FOO = "CREATE_FOO"; | ||
export function handleTestEvent(event: TestEvent): void { | ||
if (event.params.testCommand == CREATE_FILE) { | ||
dataSource.createWithContext( | ||
"File", | ||
[event.params.data], | ||
new DataSourceContext(), | ||
); | ||
} | ||
|
||
export function handleBlock(block: ethereum.Block): void { | ||
let entity = new IpfsFile("onchain"); | ||
entity.content = "onchain"; | ||
entity.save(); | ||
if (event.params.testCommand == SPAWN_FDS_FROM_OFFCHAIN_HANDLER) { | ||
let comma_separated_hash = event.params.data; | ||
let hash1 = comma_separated_hash.split(",")[0]; | ||
let hash2 = comma_separated_hash.split(",")[1]; | ||
let context = new DataSourceContext(); | ||
context.setString("command", SPAWN_FDS_FROM_OFFCHAIN_HANDLER); | ||
context.setString("hash", hash2); | ||
|
||
// This will create the same data source twice, once at block 0 and another at block 2. | ||
// The creation at block 2 should be detected as a duplicate and therefore a noop. | ||
if (block.number == BigInt.fromI32(0) || block.number == BigInt.fromI32(2)) { | ||
dataSource.create("File", [KNOWN_CID]); | ||
log.info( | ||
"Creating file data source from handleFile, command : {} ,hash1: {}, hash2: {}", | ||
[SPAWN_FDS_FROM_OFFCHAIN_HANDLER, hash1, hash2], | ||
); | ||
dataSource.createWithContext("File", [hash1], context); | ||
} | ||
|
||
if (block.number == BigInt.fromI32(1)) { | ||
let entity = IpfsFile.load("onchain")!; | ||
assert(entity.content == "onchain"); | ||
|
||
// The test assumes file data sources are processed in the block in which they are created. | ||
// So the ds created at block 0 will have been processed. | ||
// | ||
// Test that onchain data sources cannot read offchain data. | ||
assert(IpfsFile.load(KNOWN_CID) == null); | ||
if (event.params.testCommand == ONCHAIN_FROM_OFFCHAIN) { | ||
let context = new DataSourceContext(); | ||
context.setString("command", ONCHAIN_FROM_OFFCHAIN); | ||
context.setString("address", "0x0000000000000000000000000000000000000000"); | ||
dataSource.createWithContext("File", [event.params.data], context); | ||
} | ||
|
||
// Test that using an invalid CID will be ignored | ||
dataSource.create("File", ["hi, I'm not valid"]); | ||
if (event.params.testCommand == CREATE_UNDEFINED_ENTITY) { | ||
log.info("Creating undefined entity", []); | ||
let context = new DataSourceContext(); | ||
context.setString("command", CREATE_UNDEFINED_ENTITY); | ||
dataSource.createWithContext("File", [event.params.data], context); | ||
} | ||
|
||
// This will invoke File1 data source with same CID, which will be used | ||
// to test whether same cid is triggered across different data source. | ||
if (block.number == BigInt.fromI32(3)) { | ||
// Test that onchain data sources cannot read offchain data (again, but this time more likely to hit the DB than the write queue). | ||
assert(IpfsFile.load(KNOWN_CID) == null); | ||
if (event.params.testCommand == CREATE_CONFLICTING_ENTITY) { | ||
log.info("Creating conflicting entity", []); | ||
let entity = new FileEntity(event.params.data); | ||
entity.content = "content"; | ||
entity.save(); | ||
} | ||
|
||
dataSource.create("File1", [KNOWN_CID]); | ||
if ( | ||
event.params.testCommand == | ||
ACCESS_AND_UPDATE_OFFCHAIN_ENTITY_IN_ONCHAIN_HANDLER | ||
) { | ||
let hash = event.params.data; | ||
log.info("Creating file data source from handleFile: {}", [hash]); | ||
let entity = FileEntity.load(event.params.data); | ||
if (entity == null) { | ||
log.info("Entity not found", []); | ||
} else { | ||
// This should never be logged if the entity was created in the offchain handler | ||
// Such entities are not accessible in onchain handlers and will return null on load | ||
log.info("Updating entity content", []); | ||
entity.content = "updated content"; | ||
entity.save(); | ||
} | ||
} | ||
} | ||
|
||
export function handleTestEvent(event: TestEvent): void { | ||
let command = event.params.testCommand; | ||
|
||
if (command == "createFile2") { | ||
// Will fail the subgraph when processed due to mismatch in the entity type and 'entities'. | ||
dataSource.create("File2", [KNOWN_CID]); | ||
} else if (command == "saveConflictingEntity") { | ||
// Will fail the subgraph because the same entity has been created in a file data source. | ||
let entity = new IpfsFile(KNOWN_CID); | ||
entity.content = "empty"; | ||
if (event.params.testCommand == CREATE_FOO) { | ||
let entity = new Foo(event.params.data); | ||
entity.save(); | ||
} else if (command == "createFile1") { | ||
// Will fail the subgraph with a conflict between two entities created by offchain data sources. | ||
let context = new DataSourceContext(); | ||
context.setBytes("hash", event.block.hash); | ||
dataSource.createWithContext("File1", [KNOWN_CID], context); | ||
} else if (command == "spawnOffChainHandlerTest") { | ||
// Used to test the spawning of a file data source from another file data source handler. | ||
// `SpawnTestHandler` will spawn a file data source that will be handled by `spawnOffChainHandlerTest`, | ||
// which creates another file data source `OffChainDataSource`, which will be handled by `handleSpawnedTest`. | ||
let context = new DataSourceContext(); | ||
context.setString("command", command); | ||
dataSource.createWithContext("SpawnTestHandler", [KNOWN_CID], context); | ||
} else if (command == "spawnOnChainHandlerTest") { | ||
// Used to test the failure of spawning of on-chain data source from a file data source handler. | ||
// `SpawnTestHandler` will spawn a file data source that will be handled by `spawnTestHandler`, | ||
// which creates an `OnChainDataSource`, which should fail since spawning onchain datasources | ||
// from offchain handlers is not allowed. | ||
let context = new DataSourceContext(); | ||
context.setString("command", command); | ||
dataSource.createWithContext("SpawnTestHandler", [KNOWN_CID], context); | ||
} else { | ||
assert(false, "Unknown command: " + command); | ||
context.setString("command", CREATE_FOO); | ||
dataSource.createWithContext("File", [event.params.data], context); | ||
} | ||
} | ||
|
||
export function handleFile(data: Bytes): void { | ||
// Test that offchain data sources cannot read onchain data. | ||
assert(IpfsFile.load("onchain") == null); | ||
if (event.params.testCommand == ACCESS_FILE_ENTITY_THROUGH_DERIVED_FIELD) { | ||
let entity = Foo.load(event.params.data); | ||
if (entity == null) { | ||
log.info("Entity not found", []); | ||
} else { | ||
log.info("Accessing file entity through derived field", []); | ||
let fileEntity = entity.ipfs.load(); | ||
|
||
if ( | ||
dataSource.stringParam() != "QmVkvoPGi9jvvuxsHDVJDgzPEzagBaWSZRYoRDzU244HjZ" | ||
) { | ||
// Test that an offchain data source cannot read from another offchain data source. | ||
assert( | ||
IpfsFile.load("QmVkvoPGi9jvvuxsHDVJDgzPEzagBaWSZRYoRDzU244HjZ") == null | ||
); | ||
assert(fileEntity.length == 0, "Expected exactly one file entity"); | ||
} | ||
} | ||
} | ||
|
||
let entity = new IpfsFile(dataSource.stringParam()); | ||
entity.content = data.toString(); | ||
entity.save(); | ||
export function handleFile(data: Bytes): void { | ||
log.info('handleFile {}', [dataSource.stringParam()]); | ||
let context = dataSource.context(); | ||
|
||
// Test that an offchain data source can load its own entities | ||
let loaded_entity = IpfsFile.load(dataSource.stringParam())!; | ||
assert(loaded_entity.content == entity.content); | ||
} | ||
if (!context.isSet('command')) { | ||
log.info('Creating FileEntity from handleFile: {} , content : {}', [ | ||
dataSource.stringParam(), | ||
data.toString(), | ||
]); | ||
|
||
export function handleFile1(data: Bytes): void { | ||
let entity = new IpfsFile1(dataSource.stringParam()); | ||
entity.content = data.toString(); | ||
entity.save(); | ||
} | ||
let entity = new FileEntity(dataSource.stringParam()); | ||
entity.content = data.toString(); | ||
entity.save(); | ||
|
||
// Used to test spawning a file data source from another file data source handler. | ||
// This function spawns a file data source that will be handled by `handleSpawnedTest`. | ||
export function spawnTestHandler(data: Bytes): void { | ||
let context = new DataSourceContext(); | ||
context.setString("file", "fromSpawnTestHandler"); | ||
let command = dataSource.context().getString("command"); | ||
if (command == "spawnOffChainHandlerTest") { | ||
dataSource.createWithContext("OffChainDataSource", [KNOWN_CID], context); | ||
} else if (command == "spawnOnChainHandlerTest") { | ||
dataSource.createWithContext("OnChainDataSource", [KNOWN_CID], context); | ||
return; | ||
} | ||
} | ||
|
||
// This is the handler for the data source spawned by `spawnOffChainHandlerTest`. | ||
export function handleSpawnedTest(data: Bytes): void { | ||
let entity = new SpawnTestEntity(dataSource.stringParam()); | ||
let context = dataSource.context().getString("file"); | ||
entity.content = data.toString(); | ||
entity.context = context; | ||
entity.save(); | ||
} | ||
let contextCommand = context.getString('command'); | ||
|
||
if (contextCommand == SPAWN_FDS_FROM_OFFCHAIN_HANDLER) { | ||
let hash = context.getString('hash'); | ||
log.info('Creating file data source from handleFile: {}', [hash]); | ||
dataSource.createWithContext('File', [hash], new DataSourceContext()); | ||
} else if (contextCommand == ONCHAIN_FROM_OFFCHAIN) { | ||
log.info('Creating onchain data source from offchain handler', []); | ||
let address = context.getString('address'); | ||
dataSource.create('OnChainDataSource', [address]); | ||
} else if (contextCommand == CREATE_UNDEFINED_ENTITY) { | ||
log.info('Creating undefined entity', []); | ||
let entity = new Foo(dataSource.stringParam()); | ||
entity.save(); | ||
} else if (contextCommand == CREATE_FOO) { | ||
log.info('Creating FileEntity with relation to Foo', []); | ||
let entity = new FileEntity(dataSource.stringParam()); | ||
entity.foo = dataSource.stringParam(); | ||
entity.content = data.toString(); | ||
entity.save(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Love short implementations!