本文整理汇总了TypeScript中querablep.default函数的典型用法代码示例。如果您正苦于以下问题:TypeScript default函数的具体用法?TypeScript default怎么用?TypeScript default使用的例子?那么, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了default函数的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的TypeScript代码示例。
示例1: createPowWorker
export function createPowWorker() {
let powDAL:PowDAL|null = null
let computing = querablep(Promise.resolve(null));
let askedStop = false;
// By default, we do not prefix the PoW by any number
let prefix = 0;
let signatureFunc:any, lastSecret:any, currentCPU = 1;
process.on('uncaughtException', (err:any) => {
console.error(err.stack || Error(err))
if (process.send) {
process.send({error: err});
} else {
throw Error('process.send() is not defined')
}
});
process.on('unhandledRejection', () => {
process.exit()
})
process.on('message', async (message) => {
switch (message.command) {
case 'newPoW':
(async () => {
askedStop = true
// Very important: do not await if the computation is already done, to keep the lock on JS engine
if (!computing.isFulfilled()) {
await computing;
}
if (message.value.rootPath) {
const params = await directory.getHomeFS(false, message.value.rootPath, false)
powDAL = new PowDAL(message.value.rootPath, params.fs)
}
const res = await beginNewProofOfWork(message.value);
answer(message, res);
})()
break;
case 'cancel':
if (!computing.isFulfilled()) {
askedStop = true;
}
break;
case 'conf':
if (message.value.cpu !== undefined) {
currentCPU = message.value.cpu
}
if (message.value.prefix !== undefined) {
prefix = message.value.prefix
}
answer(message, { currentCPU, prefix });
break;
}
})
function beginNewProofOfWork(stuff:any) {
askedStop = false;
computing = querablep((async () => {
/*****************
* PREPARE POW STUFF
****************/
let nonce = 0;
const maxDuration = stuff.maxDuration || 1000
const conf = stuff.conf;
const block = stuff.block;
const nonceBeginning = stuff.nonceBeginning;
const nbZeros = stuff.zeros;
const pair = stuff.pair;
const forcedTime = stuff.forcedTime;
currentCPU = conf.cpu || ProverConstants.DEFAULT_CPU;
prefix = parseInt(conf.prefix || prefix)
if (prefix && prefix < ProverConstants.NONCE_RANGE) {
prefix *= 100 * ProverConstants.NONCE_RANGE
}
const highMark = stuff.highMark;
let sigFunc = null;
if (signatureFunc && lastSecret === pair.sec) {
sigFunc = signatureFunc;
}
else {
lastSecret = pair.sec;
sigFunc = (msg:string) => KeyGen(pair.pub, pair.sec).signSync(msg)
}
signatureFunc = sigFunc;
let pow = "", sig = "", raw = "";
/*****************
//.........这里部分代码省略.........
示例2: beginNewProofOfWork
function beginNewProofOfWork(stuff:any) {
askedStop = false;
computing = querablep((async () => {
/*****************
* PREPARE POW STUFF
****************/
let nonce = 0;
const conf = stuff.conf;
const block = stuff.block;
const nonceBeginning = stuff.nonceBeginning;
const nbZeros = stuff.zeros;
const pair = stuff.pair;
const forcedTime = stuff.forcedTime;
currentCPU = conf.cpu || Constants.DEFAULT_CPU;
prefix = parseInt(conf.prefix || prefix)
if (prefix && prefix < Constants.NONCE_RANGE) {
prefix *= 10 * Constants.NONCE_RANGE
}
const highMark = stuff.highMark;
const turnDuration = stuff.turnDuration || TURN_DURATION_IN_MILLISEC
let sigFunc = null;
if (signatureFunc && lastSecret === pair.sec) {
sigFunc = signatureFunc;
}
else {
lastSecret = pair.sec;
sigFunc = (msg:string) => KeyGen(pair.pub, pair.sec).signSync(msg)
}
signatureFunc = sigFunc;
let pow = "", sig = "", raw = "";
/*****************
* GO!
****************/
let testsCount = 0;
let found = false;
let score = 0;
let turn = 0;
while (!found && !askedStop) {
/*****************
* A TURN
****************/
await Promise.race([
// I. Stop the turn if it exceeds `turnDuration` ms
countDown(turnDuration),
// II. Process the turn's PoW
(async () => {
/*****************
* A TURN OF POW ~= 100ms by default
* --------------------
*
* The concept of "turn" is required to limit the CPU usage.
* We need a time reference to have the speed = nb tests / period of time.
* Here we have:
*
* - speed = testsCount / turn
*
* We have taken 1 turn = 100ms to control the CPU usage after 100ms of PoW. This means that during the
* very first 100ms of the PoW, CPU usage = 100%. Then it becomes controlled to the %CPU set.
****************/
// Prove
let i = 0;
const thisTurn = turn;
const pausePeriod = score ? score / PAUSES_PER_TURN : 10; // number of pauses per turn
// We limit the number of tests according to CPU usage
const testsPerRound = score ? Math.floor(score * currentCPU) : 1000 * 1000 * 1000
// Time is updated regularly during the proof
block.time = getBlockTime(block, conf, forcedTime)
if (block.number === 0) {
block.medianTime = block.time
}
block.inner_hash = getBlockInnerHash(block);
/*****************
* Iterations of a turn
****************/
while(!found && i < testsPerRound && thisTurn === turn && !askedStop) {
// Nonce change (what makes the PoW change if the time field remains the same)
nonce++
/*****************
* A PROOF OF WORK
****************/
// The final nonce is composed of 3 parts
block.nonce = prefix + nonceBeginning + nonce
raw = dos2unix("InnerHash: " + block.inner_hash + "\nNonce: " + block.nonce + "\n")
//.........这里部分代码省略.........
示例3: require
import {DBBlock} from "../../../lib/db/DBBlock"
import {ConfDTO} from "../../../lib/dto/ConfDTO"
import {Constants} from "./constants"
import {KeyGen} from "../../../lib/common-libs/crypto/keyring"
import {dos2unix} from "../../../lib/common-libs/dos2unix"
import {rawer} from "../../../lib/common-libs/index";
const moment = require('moment');
const querablep = require('querablep');
const PAUSES_PER_TURN = 5;
// This value can be changed
let TURN_DURATION_IN_MILLISEC = 100;
let computing = querablep(Promise.resolve(null));
let askedStop = false;
// By default, we do not prefix the PoW by any number
let prefix = 0;
let signatureFunc:any, lastSecret:any, currentCPU = 1;
process.on('uncaughtException', (err:any) => {
console.error(err.stack || Error(err))
if (process.send) {
process.send({error: err});
} else {
throw Error('process.send() is not defined')
}
});
示例4: Promise
it('should not interrupt a task in the documents FIFO', async () => {
const s1 = NewTestingServer({})
const fifo = s1._server.getDocumentsFIFO()
const ops:any[] = []
for (let i = 0; i < 10; i++) {
ops.push(querablep(fifo.pushFIFOPromise('op_' + i, async () => {
// Wait 100ms
await new Promise(res => setTimeout(res, 15))
})))
}
fifo.remainingTasksCount().should.equal(10)
while(fifo.remainingTasksCount() >= 9) {
// Wait 1ms until two tasks have been taken
await new Promise(res => setTimeout(res, 5))
}
await fifo.closeFIFO()
await ops[0]
await ops[1]
fifo.remainingTasksCount().should.equal(8)
ops[0].isFulfilled().should.equal(true)
ops[1].isFulfilled().should.equal(true)
for (let i = 2; i < 10; i++) {
ops[i].isFulfilled().should.equal(false)
}
})
示例5: beginNewProofOfWork
function beginNewProofOfWork(stuff:any) {
askedStop = false;
computing = querablep((async () => {
/*****************
* PREPARE POW STUFF
****************/
let nonce = 0;
const maxDuration = stuff.maxDuration || 1000
const conf = stuff.conf;
const block = stuff.block;
const nonceBeginning = stuff.nonceBeginning;
const nbZeros = stuff.zeros;
const pair = stuff.pair;
const forcedTime = stuff.forcedTime;
currentCPU = conf.cpu || ProverConstants.DEFAULT_CPU;
prefix = parseInt(conf.prefix || prefix)
if (prefix && prefix < ProverConstants.NONCE_RANGE) {
prefix *= 100 * ProverConstants.NONCE_RANGE
}
const highMark = stuff.highMark;
let sigFunc = null;
if (signatureFunc && lastSecret === pair.sec) {
sigFunc = signatureFunc;
}
else {
lastSecret = pair.sec;
sigFunc = (msg:string) => KeyGen(pair.pub, pair.sec).signSync(msg)
}
signatureFunc = sigFunc;
let pow = "", sig = "", raw = "";
/*****************
* GO!
****************/
let pausePeriod = 1;
let testsCount = 0;
let found = false;
let turn = 0;
const profiler = new ProcessCpuProfiler(100)
let cpuUsage = profiler.cpuUsageOverLastMilliseconds(1)
// We limit the number of tests according to CPU usage
let testsPerRound = stuff.initialTestsPerRound || 1
let turnDuration = 20 // We initially goes quickly to the max speed = 50 reevaluations per second (1000 / 20)
while (!found && !askedStop) {
/*****************
* A TURN ~ 100ms
****************/
await Promise.race([
// I. Stop the turn if it exceeds `turnDuration` ms
countDown(turnDuration),
// II. Process the turn's PoW
(async () => {
// Prove
let i = 0;
const thisTurn = turn;
// Time is updated regularly during the proof
block.time = getBlockTime(block, conf, forcedTime)
if (block.number === 0) {
block.medianTime = block.time
}
block.inner_hash = getBlockInnerHash(block);
/*****************
* Iterations of a turn
****************/
while(!found && i < testsPerRound && thisTurn === turn && !askedStop) {
// Nonce change (what makes the PoW change if the time field remains the same)
nonce++
/*****************
* A PROOF OF WORK
****************/
// The final nonce is composed of 3 parts
block.nonce = prefix + nonceBeginning + nonce
raw = dos2unix("InnerHash: " + block.inner_hash + "\nNonce: " + block.nonce + "\n")
sig = dos2unix(sigFunc(raw))
pow = hashf("InnerHash: " + block.inner_hash + "\nNonce: " + block.nonce + "\n" + sig + "\n").toUpperCase()
/*****************
* Check the POW result
****************/
let j = 0, charOK = true;
while (j < nbZeros && charOK) {
charOK = pow[j] === '0';
j++;
}
//.........这里部分代码省略.........
示例6: askProof
askProof(commandMessage:{ uuid:string, command:string, value:any }) {
this.proofPromise = querablep(new Promise<{ message: { answer:any }}|null>(res => this.proofResolver = res))
this.nodejsWorker.send(commandMessage)
return this.proofPromise
}