Skip to content

Commit

Permalink
Merge pull request #19 from leapdao/unit_tests
Browse files Browse the repository at this point in the history
Simple interactions tests
  • Loading branch information
TheReturnOfJan authored Jul 10, 2020
2 parents b17934b + d06d8c5 commit a1eb076
Show file tree
Hide file tree
Showing 4 changed files with 586 additions and 18 deletions.
22 changes: 15 additions & 7 deletions deploy.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const Machine = require('./build/Machine.json');
const Merkle = require('./build/Merkle.json');
const Oracle = require('./build/Oracle.json');
const Court = require('./build/Court.json');
const Client = require('./build/EMOClient.json');

const encodeFunctionType = (...args) => {
const isBytes24 =
Expand Down Expand Up @@ -35,33 +36,33 @@ const encodeFunctionType = (...args) => {
if (!contract.functions.hasOwnProperty(functionName)) {
throw new Error("The given contract does not have the function that was given!");
}

const funcFragment = contract.interface.fragments.reduce((acc, cv) => {
if (cv.type === "function" && cv.name === functionName)
return cv;
else
return acc;
});
const funcSelector = contract.interface.getSighash(funcFragment);

return (contract.address + funcSelector.replace("0x", ""));
} else {
throw new Error("Wrong argument types!");
}
}

const pimpOracle = (oracle) => {

oracle.interface._abiCoder._getCoder = function (param) {
if (param.type === "function") {
return this.__proto__._getCoder({...param, type: 'bytes24'});
}
return this.__proto__._getCoder(param);
}

}

const deploy = (wallet, machineFilePath) => async () => {
const deploy = (wallet, machineFilePath, defaultTimeout) => async () => {
const machine = await deployContract(
wallet,
Machine,
Expand All @@ -76,29 +77,36 @@ const deploy = (wallet, machineFilePath) => async () => {
const machineString = machineFilePath + ":Machine";
link(Oracle, machineString, machine.address);
link(Court, machineString, machine.address);
link(Client, machineString, machine.address);
} catch {
throw new Error("Linking the Machine failed. Are you deploying with the same Machine you compiled?");
}
link(Court, 'temp/Merkle.sol:Merkle', merkle.address);
const oracle = await deployContract(
wallet,
Oracle,
[]
["100000000000000000", 1000, wallet.address]
);
const court = await deployContract(
wallet,
Court,
[]
);

const client = await deployContract(
wallet,
Client,
[oracle.address, defaultTimeout]
);

pimpOracle(oracle);
machine.gen = getStructGeneratorsForCode(
fs.readFileSync(
machineFilePath.replace('temp', 'src'), 'utf8'
)
);

return [machine, merkle, oracle, court];
return [machine, merkle, oracle, court, client];
}

module.exports = {
Expand Down
14 changes: 11 additions & 3 deletions src/ExampleClient.sol
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import "./Machine.template.sol";
contract EMOClient {
IOracle oracle;

uint defaultTimeout;
uint public defaultTimeout;

mapping(bytes32 => Machine.Seed) seeds; // initialStateHash => seed

Expand Down Expand Up @@ -38,7 +38,7 @@ contract EMOClient {
}

function failCallback(bytes32 _questionKey) external {
if (timesRetried[_questionKey] > 3) {
if (timesRetried[_questionKey] >= 2) {
failed[_questionKey] = true;
} else {
_retry(_questionKey);
Expand All @@ -58,8 +58,16 @@ contract EMOClient {
return images[imageHash];
}

function showSeedByInitialStateHash(bytes32 _initialStateHash) external view returns(Machine.Seed memory) {
return seeds[_initialStateHash];
}

function imageHashForExampleMachine(Machine.Image memory _image) public pure returns(bytes32) {
return keccak256(abi.encodePacked(_image.sum));
}

// Maybe set visibility to public, so the user can also compute and use initialStateHash instead of seed
function _seedToInitialStateHash(Machine.Seed memory _seed) internal pure returns(bytes32) {
function _seedToInitialStateHash(Machine.Seed memory _seed) public pure returns(bytes32) {
return Machine.stateHash(Machine.create(_seed));
}

Expand Down
13 changes: 13 additions & 0 deletions src/Oracle.sol
Original file line number Diff line number Diff line change
Expand Up @@ -93,13 +93,26 @@ contract Oracle is IOracle {
uint public STAKE_SIZE;
uint public MAX_ANSWER_NUMBER;

constructor(uint _stake_size, uint _max_answer_number, address _court) public {
court = _court;
STAKE_SIZE = _stake_size;
MAX_ANSWER_NUMBER = _max_answer_number;
}

function getQuestion (
bytes32 questionKey
) override external view returns (Question memory)
{
return questions[questionKey];
}

function getQuestionTime(bytes32 questionKey) external view returns(uint, uint) {
Question memory question = questions[questionKey];
uint askTime = question.askTime;
uint timeout = question.timeout;
return (askTime, timeout);
}

function getAnswer (
bytes32 answerKey
) override external view returns (Answer memory)
Expand Down
Loading

0 comments on commit a1eb076

Please sign in to comment.