Skip to content
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

Fix: cross-chain Meta Evidence #52

Merged
merged 5 commits into from
Oct 20, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
docs/_build/
dist/
node_modules/
23 changes: 12 additions & 11 deletions docs/archon-arbitrable.rst
Original file line number Diff line number Diff line change
Expand Up @@ -138,17 +138,18 @@ Parameters

The options parameter can include:

================ ====== ======================================================
Key Type Description
================ ====== ======================================================
strict bool If true, an error will throw if hash or chain ID validations fail.
strictHashes bool [DEPRECATED] If true, an error will throw if hash validations fail.
customHashFn fn Hashing function that should be used to validate the hashes.
fromBlock int The block where we start searching for event logs.
toBlock int The block where we will stop searching for event logs.
filters object Additional filters for event logs.
scriptParameters object Parameters to pass to sandboxed script.
================ ====== ======================================================
================ ======== ======================================================
Key Type Description
================ ======== ======================================================
`strict` bool If true, an error will throw if hash or chain ID validations fail.
`strictHashes` bool [DEPRECATED] If true, an error will throw if hash validations fail.
`customHashFn` fn Hashing function that should be used to validate the hashes.
`fromBlock` int The block where we start searching for event logs.
`toBlock` int The block where we will stop searching for event logs.
`filters` object Additional filters for event logs.
`scriptParameters` object Parameters to pass to sandboxed script.
`getJsonRpcUrl` function A callback `(chainID: number) => string` to get a JSON-RPC URL for the given chain ID. Required in cross-chain MetaEvidece.
================ ======== ======================================================

.. tip:: Use :ref:`getDispute <getDispute>` to get the metaEvidenceID for a dispute.

Expand Down
6 changes: 4 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
"src"
],
"scripts": {
"build": "babel src -d dist",
"build": "cross-env NODE_ENV=production babel src -d dist",
"docs": "esdoc",
"ganache": "ganache-cli",
"prepublishOnly": "run-s build",
Expand All @@ -33,7 +33,8 @@
"axios": "^0.21.0",
"iframe": "^1.0.0",
"js-sha3": "^0.8.0",
"multihashes": "0.4.14"
"multihashes": "0.4.14",
"v8-sandbox": "^2.2.2"
},
"peerDependencies": {
"web3": "^1.3.6"
Expand All @@ -47,6 +48,7 @@
"assert": "^2.0.0",
"babel-eslint": "^10.1.0",
"babel-jest": "^26.6.1",
"cross-env": "^7.0.3",
"electron": "^13.1.6",
"esdoc": "^1.1.0",
"esdoc-ecmascript-proposal-plugin": "^1.0.0",
Expand Down
48 changes: 34 additions & 14 deletions src/standards/Arbitrable.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ class Arbitrable extends StandardContract {
* @param {string} contractAddress - The address of the arbitrable contract.
* @param {string} arbitratorAddress - The address of the arbitrator contract.
* @param {number} evidenceGroupID - The index of the evidence group.
* @param {object} options - Additional paramaters. Includes fromBlock, toBlock, filters, strictHashes, strict
* @param {object} options - Additional paramaters. Includes fromBlock, toBlock, filters, strict
* @returns {object[]} An array of evidence objects
*/
getEvidence = async (
Expand Down Expand Up @@ -84,7 +84,6 @@ class Arbitrable extends StandardContract {
const { file: evidenceJSON, isValid: evidenceJSONValid } = await validateFileFromURI(evidenceURI, {
preValidated,
strict,
strictHashes: options.strictHashes,
customHashFn: options.customHashFn,
});

Expand All @@ -98,7 +97,6 @@ class Arbitrable extends StandardContract {
await validateFileFromURI(evidenceURI, {
preValidated,
strict,
strictHashes: options.strictHashes,
hash: evidenceJSON.fileHash,
customHashFn: options.customHashFn,
})
Expand Down Expand Up @@ -144,14 +142,15 @@ class Arbitrable extends StandardContract {
* NOTE: If more than one MetaEvidence with the same metaEvidenceID is found it will return the 1st one.
* @param {string} contractAddress - The address of the Arbitrable contract.
* @param {number} metaEvidenceID - The identifier of the metaEvidence log.
* @param {object} options - Additional paramaters. Includes fromBlock, toBlock, strictHashes
* @param {object} options - Additional paramaters. Includes fromBlock, toBlock, strict, getJsonRpcUrl
* @returns {object} The metaEvidence object
*/
getMetaEvidence = async (
async getMetaEvidence(
contractAddress = isRequired("contractAddress"),
metaEvidenceID = isRequired("metaEvidenceID"),
options = {}
) => {
) {
const { getJsonRpcUrl = () => {} } = options;
const strict = options.strict || options.strictHashes;
const contractInstance = this._loadContractInstance(contractAddress);

Expand All @@ -178,7 +177,6 @@ class Arbitrable extends StandardContract {
const { file: _metaEvidenceJSON, isValid: metaEvidenceJSONValid } = await validateFileFromURI(metaEvidenceUri, {
preValidated,
strict,
strictHashes: options.strictHashes,
customHashFn: options.customHashFn,
});

Expand Down Expand Up @@ -209,7 +207,6 @@ class Arbitrable extends StandardContract {
const script = await validateFileFromURI(scriptURI, {
preValidated,
strict,
strictHashes: options.strictHashes,
hash: metaEvidenceJSON.dynamicScriptHash,
customHashFn: options.customHashFn,
});
Expand Down Expand Up @@ -237,7 +234,7 @@ class Arbitrable extends StandardContract {
Number(fileParameters.arbitratorChainID) !== Number(scriptParameters.arbitratorChainID)
) {
throw new Error(
`MetaEvidence requires 'arbitratorChainID' to be ${fileParameters.arbitratorChainID}, but ${scriptParameters.arbitratorChainID} was given`
`MetaEvidence requires 'arbitratorChainID' to be ${fileParameters.arbitratorChainID}, but ${scriptParameters.arbitratorChainID} was given.`
);
}

Expand All @@ -246,8 +243,33 @@ class Arbitrable extends StandardContract {
...scriptParameters,
};

if (injectedParameters.arbitrableChainID === undefined) {
injectedParameters.arbitrableChainID = injectedParameters.arbitratorChainID;
injectedParameters.arbitrableContractAddress =
injectedParameters.arbitrableContractAddress || contractAddress;
injectedParameters.arbitratorJsonRpcUrl =
injectedParameters.arbitratorJsonRpcUrl || getJsonRpcUrl(injectedParameters.arbitratorChainID);
injectedParameters.arbitrableChainID =
injectedParameters.arbitrableChainID || injectedParameters.arbitratorChainID;
injectedParameters.arbitrableJsonRpcUrl =
injectedParameters.arbitrableJsonRpcUrl || getJsonRpcUrl(injectedParameters.arbitrableChainID);

if (
injectedParameters.arbitratorChainID !== undefined &&
injectedParameters.arbitratorJsonRpcUrl === undefined
) {
console.warn(
`Could not obtain a valid 'arbitratorJsonRpcUrl' for chain ID ${injectedParameters.arbitratorChainID} on the Arbitrator side.
You should either provide it directly or provide a 'options.getJsonRpcUrl(chainID: number) => string' callback.`
);
}

if (
injectedParameters.arbitrableChainID !== undefined &&
injectedParameters.arbitrableJsonRpcUrl === undefined
) {
console.warn(
`Could not obtain a valid 'arbitrableJsonRpcUrl' for chain ID ${injectedParameters.arbitrableChainID} on the Arbitrable side.
You should either provide it directly or provide a 'options.getJsonRpcUrl(chainID: number) => string' callback.`
);
}

const metaEvidenceEdits = await fetchDataFromScript(script.file, injectedParameters);
Expand Down Expand Up @@ -277,7 +299,6 @@ class Arbitrable extends StandardContract {
await validateFileFromURI(fileURI, {
preValidated,
strict,
strictHashes: options.strictHashes,
hash: metaEvidenceJSON.fileHash,
customHashFn: options.customHashFn,
})
Expand All @@ -303,7 +324,6 @@ class Arbitrable extends StandardContract {
interfaceValid = (
await validateFileFromURI(disputeInterfaceURI, {
strict,
strictHashes: options.strictHashes,
hash: metaEvidenceJSON.evidenceDisplayInterfaceHash,
customHashFn: options.customHashFn,
})
Expand All @@ -326,7 +346,7 @@ class Arbitrable extends StandardContract {
blockNumber: metaEvidenceLog.blockNumber,
transactionHash: metaEvidenceLog.transactionHash,
};
};
}

/**
* Fetch the ruling for a dispute.
Expand Down
49 changes: 46 additions & 3 deletions src/utils/frame-loader.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import iframe from "iframe";

export default function fetchDataFromScript(scriptString, scriptParameters) {
return typeof window !== "undefined" ? fetchDataFromScriptIframe(scriptString, scriptParameters) : {};
return typeof window !== "undefined"
? fetchDataFromScriptIframe(scriptString, scriptParameters)
: fetchDataFromScriptSandbox(scriptString, scriptParameters);
}

const fetchDataFromScriptIframe = async (scriptString, scriptParameters) => {
const { default: iframe } = await import("iframe");

let resolver;
const returnPromise = new Promise((resolve) => {
resolver = resolve;
Expand Down Expand Up @@ -44,3 +46,44 @@ const fetchDataFromScriptIframe = async (scriptString, scriptParameters) => {
_.iframe.style.display = "none";
return returnPromise;
};

const fetchDataFromScriptSandbox = async (scriptString, scriptParameters) => {
const { default: Sandbox } = await import("v8-sandbox");
const sandbox = new Sandbox();

const code = `
${scriptString}

let resolveScript
let rejectScript
let scriptParameters = JSON.parse(stringifiedScriptParameters)

const returnPromise = new Promise((resolve, reject) => {
resolveScript = resolve
rejectScript = reject
})

getMetaEvidence()

returnPromise.then((metaEvidence) => {
setResult({ value: metaEvidence })
}).catch((err) => {
setResult({ error: err })
})`;

const { error, value } = await sandbox.execute({
code,
globals: {
stringifiedScriptParameters: JSON.stringify(scriptParameters || {}),
},
timeout: 5000,
});

await sandbox.shutdown();

if (error) {
throw error;
}

return value;
};
Loading