-
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
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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! | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,130 +4,148 @@ 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"; | ||
|
||
export function handleBlock(block: ethereum.Block): void { | ||
let entity = new IpfsFile("onchain"); | ||
entity.content = "onchain"; | ||
entity.save(); | ||
|
||
// 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]); | ||
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(), | ||
); | ||
} | ||
|
||
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 == 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); | ||
|
||
// Test that using an invalid CID will be ignored | ||
dataSource.create("File", ["hi, I'm not valid"]); | ||
log.info( | ||
"Creating file data source from handleFile, command : {} ,hash1: {}, hash2: {}", | ||
[SPAWN_FDS_FROM_OFFCHAIN_HANDLER, hash1, hash2], | ||
); | ||
dataSource.createWithContext("File", [hash1], 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); | ||
|
||
dataSource.create("File1", [KNOWN_CID]); | ||
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); | ||
} | ||
} | ||
|
||
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"; | ||
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`. | ||
if (event.params.testCommand == CREATE_UNDEFINED_ENTITY) { | ||
log.info("Creating undefined entity", []); | ||
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_UNDEFINED_ENTITY); | ||
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 == CREATE_CONFLICTING_ENTITY) { | ||
log.info("Creating conflicting entity", []); | ||
let entity = new FileEntity(event.params.data); | ||
entity.content = "content"; | ||
entity.save(); | ||
} | ||
|
||
if ( | ||
dataSource.stringParam() != "QmVkvoPGi9jvvuxsHDVJDgzPEzagBaWSZRYoRDzU244HjZ" | ||
event.params.testCommand == | ||
ACCESS_AND_UPDATE_OFFCHAIN_ENTITY_IN_ONCHAIN_HANDLER | ||
) { | ||
// Test that an offchain data source cannot read from another offchain data source. | ||
assert( | ||
IpfsFile.load("QmVkvoPGi9jvvuxsHDVJDgzPEzagBaWSZRYoRDzU244HjZ") == null | ||
); | ||
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(); | ||
} | ||
} | ||
|
||
let entity = new IpfsFile(dataSource.stringParam()); | ||
entity.content = data.toString(); | ||
entity.save(); | ||
|
||
// 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 (event.params.testCommand == CREATE_FOO) { | ||
let entity = new Foo(event.params.data); | ||
entity.save(); | ||
let context = new DataSourceContext(); | ||
context.setString("command", CREATE_FOO); | ||
dataSource.createWithContext("File", [event.params.data], context); | ||
} | ||
|
||
export function handleFile1(data: Bytes): void { | ||
let entity = new IpfsFile1(dataSource.stringParam()); | ||
entity.content = data.toString(); | ||
entity.save(); | ||
} | ||
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(); | ||
|
||
// 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); | ||
assert(fileEntity.length == 0, "Expected exactly one file entity"); | ||
} | ||
} | ||
} | ||
|
||
// 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(); | ||
export function handleFile(data: Bytes): void { | ||
log.info("handleFile {}", [dataSource.stringParam()]); | ||
let context = dataSource.context(); | ||
|
||
if (context.isSet("command")) { | ||
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()); | ||
} | ||
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. Not sure what's idiomatic for TypeScript bu wouldn't 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. Agree, it should have been an else if. We cannot use switch cases though since switch-case in assemblyscript does not work with string |
||
|
||
if (contextCommand == ONCHAIN_FROM_OFFCHAIN) { | ||
log.info("Creating onchain data source from offchain handler", []); | ||
let address = context.getString("address"); | ||
dataSource.create("OnChainDataSource", [address]); | ||
} | ||
|
||
if (contextCommand == CREATE_UNDEFINED_ENTITY) { | ||
log.info("Creating undefined entity", []); | ||
let entity = new Foo(dataSource.stringParam()); | ||
entity.save(); | ||
} | ||
|
||
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(); | ||
} | ||
} else { | ||
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. Can remove a nesting level by doing this block if not command and returning. Then you know all the rest are commands. as zoran said it's prolly more of a switch case, mostly for making it a bit easier to maintain and read if you think it's worth. But also none of the ifs return when match so that could create some weird test situations 😆 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. Agreed, making the change. |
||
log.info("Creating FileEntity from handleFile: {} , content : {}", [ | ||
dataSource.stringParam(), | ||
data.toString(), | ||
]); | ||
|
||
let entity = new FileEntity(dataSource.stringParam()); | ||
entity.content = data.toString(); | ||
entity.save(); | ||
} | ||
} |
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!