-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.js
More file actions
136 lines (105 loc) · 4.31 KB
/
index.js
File metadata and controls
136 lines (105 loc) · 4.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
const path = require("path")
const Web3Wrapper = require("./lib/web3Wrapper")
const ControllerWrapper = require("./lib/controllerWrapper")
const LivepeerVerifierWrapper = require("./lib/livepeerVerifierWrapper")
const ComputationArchive = require("./lib/computationArchive")
const Web3 = require("web3")
const ethUtil = require("ethereumjs-util")
const ethAbi = require("ethereumjs-abi")
const prompt = require("prompt-sync")()
const yargsOpts = {
alias: {
"controller": ["c"],
"account": ["a"],
"password": ["p"]
},
configuration: {
"parse-numbers": false
}
}
const argv = require("yargs-parser")(process.argv.slice(2), yargsOpts)
const provider = new Web3.providers.HttpProvider("http://localhost:8545")
const ARCHIVE_NAME = path.resolve(__dirname, "archive.zip")
const ARCHIVE_DIR = path.resolve(__dirname, "archive")
const LOGS_DIR = path.resolve(__dirname, "logs")
const DOCKER_IMAGE_NAME = "verifier"
const run = async () => {
if (argv.controller === undefined) {
abort("Must pass in the Controller contract address")
}
if (argv.account === undefined) {
abort("Must pass in a valid Ethereum account address")
}
const web3Wrapper = new Web3Wrapper(provider)
const nodeType = await web3Wrapper.getNodeType()
if (!nodeType.match(/TestRPC/i)) {
// Not connected to TestRPC
// User must unlock account
try {
await unlock(argv.account, argv.password, web3Wrapper)
} catch (err) {
abort("Failed to unlock account")
}
}
console.log(`Account ${argv.account} unlocked`)
const controller = new ControllerWrapper(web3Wrapper, argv.controller)
const verifierAddress = await controller.getVerifierAddress()
const verifier = new LivepeerVerifierWrapper(web3Wrapper, verifierAddress, argv.account)
const archive = new ComputationArchive(ARCHIVE_NAME, ARCHIVE_DIR, LOGS_DIR, DOCKER_IMAGE_NAME)
const archiveHash = await verifier.getVerificationCodeHash()
console.log(`Using verification code at IPFS hash ${archiveHash}`)
await archive.setup(archiveHash)
console.log("Finished setting up computation archive")
const eventSub = await watchForVerifyRequests(verifier, archive)
process.on("SIGINT", async () => {
try {
await eventSub.stopWatching()
await archive.cleanup()
} catch (error) {
console.error(error)
}
console.log("Stop watching for events and exiting...")
process.exit(0)
})
}
const abort = msg => {
console.log(msg || "Error occured")
process.exit(1)
}
const unlock = async (account, password, web3Wrapper) => {
try {
await web3Wrapper.unlockAccount(account, password)
} catch (err) {
// Prompt for password if default password fails
password = prompt("Password: ", {echo: ""})
await web3Wrapper.unlockAccount(account, password)
}
}
const watchForVerifyRequests = async (verifier, archive) => {
const eventSub = await verifier.subscribeToVerifyRequest()
console.log("Watching for verification request events...")
eventSub.watch(async (err, event) => {
console.log(`Received verify request #${event.args.requestId}`)
console.log(`Job ID: ${event.args.jobId} Claim ID: ${event.args.claimId} Seg #: ${event.args.segmentNumber}`)
let result
try {
result = await processVerifyRequest(event.args.requestId, archive, event.args.transcodingOptions, event.args.dataStorageHash)
} catch (error) {
console.error(error)
return
}
const transcoderResultHash = ethUtil.bufferToHex(
ethAbi.soliditySHA3(["bytes", "bytes"], [ethUtil.toBuffer(event.args.dataHash), ethUtil.toBuffer(event.args.transcodedDataHash)])
)
console.log(`Results for verify request #${event.args.requestId}`)
console.log(`Transcoder result hash: ${transcoderResultHash}`)
console.log(`Verifier result hash: 0x${result}`)
})
return eventSub
}
const processVerifyRequest = async (requestNum, archive, transcodingOptions, dataStorageHash) => {
await archive.runDockerApp(requestNum, dataStorageHash, transcodingOptions)
const result = await archive.getDockerAppResult(requestNum)
return result
}
run().catch(console.log)