Skip to content

Instantly share code, notes, and snippets.

@jimpick
Created February 26, 2019 02:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jimpick/74b62cb970656e35a8ccdf2a32ebd9be to your computer and use it in GitHub Desktop.
Save jimpick/74b62cb970656e35a8ccdf2a32ebd9be to your computer and use it in GitHub Desktop.
node.js program to propose small storage deal to multiple miners in parallel
const util = require('util')
const spawn = require('child_process').spawn
const execFile = util.promisify(require('child_process').execFile)
const PQueue = require('p-queue')
const subset = require('./asks-subset.json')
const asks = subset.sort((a, b) => a.Price - b.Price)
async function run () {
let probeCid = await importFile('hello.txt')
probeCid = probeCid.replace(/\n$/, '')
console.log('probeCid:', probeCid)
let count = 0
const queue = new PQueue({ concurrency: 10 })
for (let ask of asks) {
count += 1
const myCount = count
queue.add(() => probe())
async function probe () {
console.log(`Ask ${myCount} of ${Object.keys(asks).length}`)
console.log(ask)
const before = Date.now()
const duration = 30
console.log(
`go-filecoin client propose-storage-deal ` +
`--allow-duplicates ${ask.Miner} ${probeCid} ${ask.ID} ${duration}`
)
const [output, code] = await spawnAndWait('go-filecoin', [
'client',
'propose-storage-deal',
'--allow-duplicates',
ask.Miner,
probeCid,
`${ask.ID}`,
`${duration}`
], { prefix: `${myCount}: ` } )
const elapsed = Math.floor((Date.now() - before) / 1000)
console.log(`${myCount}: Done in ${elapsed}s. Exit code: ${code}`)
}
}
return queue
}
async function importFile (file) {
const { stdout } = await execFile('go-filecoin', [
'client',
'import',
file
])
return stdout
}
function spawnAndWait (cmd, args, options = {}) {
const promise = new Promise((resolve, reject) => {
let output = ''
const child = spawn(cmd, args)
child.stdout.on('data', appendOutput)
child.stderr.on('data', appendOutput)
child.on('close', code => resolve([output, code]))
function appendOutput (data) {
process.stdout.write(`${options.prefix}${data.toString()}`)
output += data
}
})
return promise
}
run()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment