Skip to content

Commit

Permalink
example-working again with latest truebit-os version
Browse files Browse the repository at this point in the history
  • Loading branch information
hswick committed Jan 29, 2019
1 parent a891741 commit 7525b88
Show file tree
Hide file tree
Showing 13 changed files with 791 additions and 695 deletions.
1 change: 1 addition & 0 deletions build/DepositsManager.abi
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[{"constant":false,"inputs":[{"name":"_deposit","type":"uint256"}],"name":"makeDeposit","outputs":[{"name":"","type":"uint256"}],"payable":true,"stateMutability":"payable","type":"function"}]
Empty file added build/DepositsManager.bin
Empty file.
2 changes: 1 addition & 1 deletion build/Filesystem.abi
Original file line number Diff line number Diff line change
@@ -1 +1 @@
[{"constant":false,"inputs":[{"name":"id","type":"bytes32"},{"name":"a","type":"address"}],"name":"forwardData","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[{"name":"id","type":"bytes32"}],"name":"getSize","outputs":[{"name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[{"name":"id","type":"bytes32"},{"name":"file","type":"string"},{"name":"init","type":"bytes32"}],"name":"finalizeBundleIPFS","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[{"name":"id","type":"bytes32"}],"name":"getData","outputs":[{"name":"","type":"bytes32[]"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[{"name":"bid","type":"bytes32"}],"name":"getInitHash","outputs":[{"name":"","type":"bytes32"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[{"name":"id","type":"bytes32"}],"name":"getRoot","outputs":[{"name":"","type":"bytes32"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[{"name":"id","type":"bytes32"},{"name":"file","type":"string"},{"name":"init","type":"bytes32"}],"name":"debug_finalizeBundleIPFS","outputs":[{"name":"","type":"bytes32"},{"name":"","type":"bytes32"},{"name":"","type":"bytes32"},{"name":"","type":"bytes32"},{"name":"","type":"bytes32"}],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[{"name":"num","type":"uint256"}],"name":"makeBundle","outputs":[{"name":"","type":"bytes32"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[{"name":"name","type":"string"},{"name":"nonce","type":"uint256"},{"name":"arr","type":"bytes32[]"},{"name":"sz","type":"uint256"}],"name":"createFileWithContents","outputs":[{"name":"","type":"bytes32"}],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":false,"inputs":[{"name":"id","type":"bytes32"},{"name":"file_id","type":"bytes32"}],"name":"addToBundle","outputs":[{"name":"","type":"bytes32"}],"payable":false,"stateMutability":"nonpayable","type":"function"}]
[{"constant":false,"inputs":[{"name":"id","type":"bytes32"},{"name":"a","type":"address"}],"name":"forwardData","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[{"name":"id","type":"bytes32"}],"name":"getSize","outputs":[{"name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[{"name":"id","type":"bytes32"}],"name":"getData","outputs":[{"name":"","type":"bytes32[]"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[{"name":"name","type":"string"},{"name":"size","type":"uint256"},{"name":"hash","type":"string"},{"name":"root","type":"bytes32"},{"name":"nonce","type":"uint256"}],"name":"addIPFSFile","outputs":[{"name":"","type":"bytes32"}],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[{"name":"bid","type":"bytes32"}],"name":"getInitHash","outputs":[{"name":"","type":"bytes32"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[{"name":"id","type":"bytes32"}],"name":"getRoot","outputs":[{"name":"","type":"bytes32"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[{"name":"name","type":"string"}],"name":"hashName","outputs":[{"name":"","type":"bytes32"}],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":false,"inputs":[{"name":"bundleID","type":"bytes32"},{"name":"codeFileID","type":"bytes32"}],"name":"finalizeBundle","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[{"name":"num","type":"uint256"}],"name":"makeBundle","outputs":[{"name":"","type":"bytes32"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[{"name":"name","type":"string"},{"name":"nonce","type":"uint256"},{"name":"arr","type":"bytes32[]"},{"name":"sz","type":"uint256"}],"name":"createFileWithContents","outputs":[{"name":"","type":"bytes32"}],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":false,"inputs":[{"name":"id","type":"bytes32"},{"name":"file_id","type":"bytes32"}],"name":"addToBundle","outputs":[{"name":"","type":"bytes32"}],"payable":false,"stateMutability":"nonpayable","type":"function"}]
2 changes: 1 addition & 1 deletion build/Scrypt.abi
Original file line number Diff line number Diff line change
@@ -1 +1 @@
[{"constant":true,"inputs":[{"name":"data","type":"bytes"}],"name":"scrypt","outputs":[{"name":"","type":"bytes32"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[{"name":"id","type":"bytes32"},{"name":"files","type":"bytes32[]"}],"name":"solved","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[{"name":"name","type":"string"}],"name":"hashName","outputs":[{"name":"","type":"bytes32"}],"payable":false,"stateMutability":"pure","type":"function"},{"constant":true,"inputs":[{"name":"id","type":"bytes32"}],"name":"idToString","outputs":[{"name":"","type":"string"}],"payable":false,"stateMutability":"pure","type":"function"},{"constant":false,"inputs":[{"name":"data","type":"bytes"}],"name":"submitData","outputs":[{"name":"","type":"bytes32"}],"payable":false,"stateMutability":"nonpayable","type":"function"},{"inputs":[{"name":"tb","type":"address"},{"name":"tru_","type":"address"},{"name":"fs","type":"address"},{"name":"code_address","type":"string"},{"name":"init_hash","type":"bytes32"}],"payable":false,"stateMutability":"nonpayable","type":"constructor"},{"anonymous":false,"inputs":[{"indexed":false,"name":"files","type":"bytes32[]"}],"name":"GotFiles","type":"event"},{"anonymous":false,"inputs":[{"indexed":false,"name":"arr","type":"bytes32[]"}],"name":"Consuming","type":"event"},{"anonymous":false,"inputs":[{"indexed":false,"name":"data","type":"bytes32[]"}],"name":"InputData","type":"event"}]
[{"constant":true,"inputs":[{"name":"data","type":"bytes"}],"name":"formatData","outputs":[{"name":"output","type":"bytes32[]"}],"payable":false,"stateMutability":"pure","type":"function"},{"constant":true,"inputs":[{"name":"data","type":"bytes"}],"name":"scrypt","outputs":[{"name":"","type":"bytes32"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[{"name":"id","type":"bytes32"},{"name":"files","type":"bytes32[]"}],"name":"solved","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":false,"inputs":[{"name":"data","type":"bytes"}],"name":"submitData","outputs":[{"name":"","type":"bytes32"}],"payable":false,"stateMutability":"nonpayable","type":"function"},{"inputs":[{"name":"tb","type":"address"},{"name":"tru_","type":"address"},{"name":"fs","type":"address"},{"name":"_depositsManager","type":"address"},{"name":"_bundleID","type":"bytes32"},{"name":"_codeFileID","type":"bytes32"},{"name":"_initHash","type":"bytes32"}],"payable":false,"stateMutability":"nonpayable","type":"constructor"},{"anonymous":false,"inputs":[{"indexed":false,"name":"files","type":"bytes32[]"}],"name":"GotFiles","type":"event"},{"anonymous":false,"inputs":[{"indexed":false,"name":"arr","type":"bytes32[]"}],"name":"Consuming","type":"event"},{"anonymous":false,"inputs":[{"indexed":false,"name":"data","type":"bytes32[]"}],"name":"InputData","type":"event"}]
2 changes: 1 addition & 1 deletion build/Scrypt.bin

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion build/TrueBit.abi
Original file line number Diff line number Diff line change
@@ -1 +1 @@
[{"constant":false,"inputs":[{"name":"_deposit","type":"uint256"}],"name":"makeDeposit","outputs":[{"name":"","type":"uint256"}],"payable":true,"stateMutability":"payable","type":"function"},{"constant":false,"inputs":[{"name":"id","type":"bytes32"},{"name":"hash","type":"bytes32"},{"name":"st","type":"uint8"}],"name":"requireFile","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":false,"inputs":[{"name":"id","type":"bytes32"}],"name":"commitRequiredFiles","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":false,"inputs":[{"name":"initTaskHash","type":"bytes32"},{"name":"codeType","type":"uint8"},{"name":"storageType","type":"uint8"},{"name":"storageAddress","type":"string"},{"name":"maxDifficulty","type":"uint256"},{"name":"reward","type":"uint256"},{"name":"stack","type":"uint8"},{"name":"mem","type":"uint8"},{"name":"globals","type":"uint8"},{"name":"table","type":"uint8"},{"name":"call","type":"uint8"}],"name":"createTaskWithParams","outputs":[{"name":"","type":"bytes32"}],"payable":false,"stateMutability":"nonpayable","type":"function"}]
[{"constant":false,"inputs":[{"name":"id","type":"bytes32"}],"name":"commitRequiredFiles","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":false,"inputs":[{"name":"initTaskHash","type":"bytes32"},{"name":"codeType","type":"uint8"},{"name":"bundleID","type":"bytes32"},{"name":"maxDifficulty","type":"uint256"},{"name":"reward","type":"uint256"},{"name":"stack","type":"uint8"},{"name":"mem","type":"uint8"},{"name":"globals","type":"uint8"},{"name":"table","type":"uint8"},{"name":"call","type":"uint8"},{"name":"limit","type":"uint32"}],"name":"createTaskWithParams","outputs":[{"name":"","type":"bytes32"}],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":false,"inputs":[{"name":"id","type":"bytes32"},{"name":"hash","type":"bytes32"},{"name":"st","type":"uint256"}],"name":"requireFile","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"}]
125 changes: 57 additions & 68 deletions contract.sol
Original file line number Diff line number Diff line change
@@ -1,31 +1,32 @@

pragma solidity ^0.4.16;
pragma solidity ^0.5.0;

interface Filesystem {

function createFileWithContents(string name, uint nonce, bytes32[] arr, uint sz) external returns (bytes32);
function createFileWithContents(string calldata name, uint nonce, bytes32[] calldata arr, uint sz) external returns (bytes32);
function getSize(bytes32 id) external view returns (uint);
function getRoot(bytes32 id) external view returns (bytes32);
function getData(bytes32 id) external view returns (bytes32[]);
function forwardData(bytes32 id, address a) external;

// function makeSimpleBundle(uint num, address code, bytes32 code_init, bytes32 file_id) public returns (bytes32);
function getData(bytes32 id) external view returns (bytes32[] memory);
function forwardData(bytes32 id, address a) external;

function makeBundle(uint num) external view returns (bytes32);
function addToBundle(bytes32 id, bytes32 file_id) external returns (bytes32);
function finalizeBundleIPFS(bytes32 id, string file, bytes32 init) external;
function getInitHash(bytes32 bid) external view returns (bytes32);

function debug_finalizeBundleIPFS(bytes32 id, string file, bytes32 init) external returns (bytes32, bytes32, bytes32, bytes32, bytes32);
function finalizeBundle(bytes32 bundleID, bytes32 codeFileID) external;
function getInitHash(bytes32 bid) external view returns (bytes32);
function addIPFSFile(string calldata name, uint size, string calldata hash, bytes32 root, uint nonce) external returns (bytes32);
function hashName(string calldata name) external returns (bytes32);

}

interface TrueBit {
function createTaskWithParams(bytes32 initTaskHash, uint8 codeType, uint8 storageType, string storageAddress, uint maxDifficulty, uint reward,
uint8 stack, uint8 mem, uint8 globals, uint8 table, uint8 call) external returns (bytes32);
function requireFile(bytes32 id, bytes32 hash, /* Storage */ uint8 st) external;
function createTaskWithParams(bytes32 initTaskHash, uint8 codeType, bytes32 bundleID, uint maxDifficulty, uint reward,
uint8 stack, uint8 mem, uint8 globals, uint8 table, uint8 call, uint32 limit) external returns (bytes32);
function requireFile(bytes32 id, bytes32 hash, /* Storage */ uint st) external;
function commitRequiredFiles(bytes32 id) external;
function makeDeposit(uint _deposit) external payable returns (uint);
}

interface DepositsManager {
function makeDeposit(uint _deposit) external payable returns (uint);
}

interface TRU {
Expand All @@ -43,60 +44,71 @@ contract Scrypt {
TrueBit truebit;
Filesystem filesystem;
TRU tru;
DepositsManager depositsManager;

string code;
bytes32 init;
bytes32 bundleID;
bytes32 codeFileID;
bytes32 initHash;

mapping (bytes => bytes32) string_to_file;
mapping (bytes32 => bytes) task_to_string;
mapping (bytes => bytes32) result;

constructor(address tb, address tru_, address fs, string code_address, bytes32 init_hash) public {
truebit = TrueBit(tb);
tru = TRU(tru_);
filesystem = Filesystem(fs);
code = code_address; // address for wasm file in IPFS
init = init_hash; // the canonical hash
constructor(address tb, address tru_, address fs, address _depositsManager, bytes32 _bundleID, bytes32 _codeFileID, bytes32 _initHash) public {
truebit = TrueBit(tb);
tru = TRU(tru_);
filesystem = Filesystem(fs);
bundleID = _bundleID;
codeFileID = _codeFileID;
initHash = _initHash;
depositsManager = DepositsManager(_depositsManager);
}

function submitData(bytes data) public returns (bytes32) {
uint num = nonce;
nonce++;
bytes32[] memory input = new bytes32[](data.length/32+1);

function formatData(bytes memory data) public pure returns (bytes32[] memory output) {
//Format data
output = new bytes32[](data.length/32+1);
for (uint i = 0; i <= data.length/32; i++) {
uint a;
for (uint j = 0; j < 32; j++) {
a = a*256;
if (i*32+j < data.length) a += uint(data[i*32+j]);
if (i*32+j < data.length) a += uint8(data[i*32+j]);
}
input[i] = bytes32(a);
output[i] = bytes32(a);
}


return output;
}

function submitData(bytes memory data) public returns (bytes32) {
uint num = nonce;
nonce++;

bytes32[] memory input = formatData(data);
emit InputData(input);
bytes32 input_file = filesystem.createFileWithContents("input.data", num, input, data.length);
string_to_file[data] = input_file;
bytes32 bundle = filesystem.makeBundle(num);
filesystem.addToBundle(bundle, input_file);

bytes32 inputFileID = filesystem.createFileWithContents("input.data", num, input, data.length);
string_to_file[data] = inputFileID;
filesystem.addToBundle(bundleID, inputFileID);

bytes32[] memory empty = new bytes32[](0);
filesystem.addToBundle(bundle, filesystem.createFileWithContents("output.data", num+1000000000, empty, 0));
filesystem.finalizeBundleIPFS(bundle, code, init);
filesystem.addToBundle(bundleID, filesystem.createFileWithContents("output.data", num+1000000000, empty, 0));

filesystem.finalizeBundle(bundleID, codeFileID);

tru.approve(truebit, 1000);
truebit.makeDeposit(1000);
tru.approve(address(depositsManager), 1000);
depositsManager.makeDeposit(1000);
// string memory bstr = ;
bytes32 task = truebit.createTaskWithParams(filesystem.getInitHash(bundle), 1, 1, idToString(bundle), 1, 1, 20, 20, 8, 20, 10);
truebit.requireFile(task, hashName("output.data"), 0);
bytes32 task = truebit.createTaskWithParams(filesystem.getInitHash(bundleID), 1, bundleID, 1, 1, 20, 20, 8, 20, 10, 5000);
truebit.requireFile(task, filesystem.hashName("output.data"), 0);
truebit.commitRequiredFiles(task);
task_to_string[task] = data;

return filesystem.getInitHash(bundle);
return filesystem.getInitHash(bundleID);
}

bytes32 remember_task;

// this is the callback name
function solved(bytes32 id, bytes32[] files) public {
function solved(bytes32 id, bytes32[] memory files) public {
// could check the task id
require(TrueBit(msg.sender) == truebit);
remember_task = id;
Expand All @@ -107,31 +119,8 @@ contract Scrypt {
}

// need some way to get next state, perhaps shoud give all files as args
function scrypt(bytes data) public view returns (bytes32) {
function scrypt(bytes memory data) public view returns (bytes32) {
return result[data];
}

///// Utils

function idToString(bytes32 id) public pure returns (string) {
bytes memory res = new bytes(64);
for (uint i = 0; i < 64; i++) res[i] = bytes1(((uint(id) / (2**(4*i))) & 0xf) + 65);
return string(res);
}

function makeMerkle(bytes arr, uint idx, uint level) internal pure returns (bytes32) {
if (level == 0) return idx < arr.length ? bytes32(uint(arr[idx])) : bytes32(0);
else return keccak256(abi.encodePacked(makeMerkle(arr, idx, level-1), makeMerkle(arr, idx+(2**(level-1)), level-1)));
}

function calcMerkle(bytes32[] arr, uint idx, uint level) internal returns (bytes32) {
if (level == 0) return idx < arr.length ? arr[idx] : bytes32(0);
else return keccak256(abi.encodePacked(calcMerkle(arr, idx, level-1), calcMerkle(arr, idx+(2**(level-1)), level-1)));
}

// assume 256 bytes?
function hashName(string name) public pure returns (bytes32) {
return makeMerkle(bytes(name), 0, 8);
}

}
38 changes: 35 additions & 3 deletions deploy.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,19 @@ const host = "http://localhost:8545"
const Web3 = require('web3')
const web3 = new Web3(new Web3.providers.HttpProvider(host))

const merkleRoot = require('./merkleRoot')

async function deploy() {

//Upload file to IPFS
let codeBuf = fs.readFileSync("./scrypt-task/task.wasm")

let ipfsHash = (await ipfs.files.add([{content: codeBuf, path: "task.wasm"}]))[0].hash
let ipfsFile = (await ipfs.files.add([{content: codeBuf, path: "task.wasm"}]))[0]

let ipfsHash = ipfsFile.hash
let size = ipfsFile.size
let name = ipfsFile.path


if (ipfsHash == info.ipfsHash) {
throw "Wrong IPFS Hashes"
Expand All @@ -30,15 +37,38 @@ async function deploy() {
let artifacts = JSON.parse(fs.readFileSync('./truebit-os/wasm-client/' + networkName + '.json'))

let accounts = await web3.eth.getAccounts()
let account = accounts[0]

let options = {from: accounts[0].toLowerCase(), gas: 2000000}

let bundleID, codeFileID
let initHash = info.codehash

let tbFileSystem = new web3.eth.Contract(artifacts.fileSystem.abi, artifacts.fileSystem.address)

//setup bundle
let bundleNonce = Math.floor(Math.random()*Math.pow(2, 30))
bundleID = await tbFileSystem.methods.calcId(bundleNonce).call({from: account})
await tbFileSystem.methods.makeBundle(bundleNonce).send({from: account, gas: 300000})

//setup file
let fileNonce = Math.floor(Math.random()*Math.pow(2, 30))
let mr = merkleRoot(web3, codeBuf)

codeFileID = await tbFileSystem.methods.calcId(fileNonce).call({from: account})

await tbFileSystem.methods.addIPFSCodeFile(name, size, ipfsHash, mr, initHash, fileNonce).send({from: account, gas: 300000})

console.log("Registered IPFS file with Truebit filesystem")

let args = [
artifacts.incentiveLayer.address,
artifacts.tru.address,
artifacts.fileSystem.address,
info.ipfshash,
info.codehash
artifacts.depositsManager.address,
bundleID,
codeFileID,
initHash
]

let contract = new web3.eth.Contract(abi)
Expand All @@ -53,6 +83,8 @@ async function deploy() {
address: c._address,
abi: c._jsonInterface
}))

console.log("Contract has been deployed")
}

deploy()
2 changes: 1 addition & 1 deletion install_truebit_os.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
git clone git@github.com:TrueBitFoundation/truebit-os.git
git clone git@github.com:TrueBitFoundation/truebit-os.git

cd truebit-os
npm i
Expand Down
Loading

0 comments on commit 7525b88

Please sign in to comment.