當前位置: 首頁>>代碼示例>>TypeScript>>正文


TypeScript querablep.default函數代碼示例

本文整理匯總了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 = "";

      /*****************
//.........這裏部分代碼省略.........
開發者ID:duniter,項目名稱:duniter,代碼行數:101,代碼來源:proof.ts

示例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")
//.........這裏部分代碼省略.........
開發者ID:Kalmac,項目名稱:duniter,代碼行數:101,代碼來源:proof.ts

示例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')
  }
});
開發者ID:Kalmac,項目名稱:duniter,代碼行數:31,代碼來源:proof.ts

示例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)
    }
  })
開發者ID:duniter,項目名稱:duniter,代碼行數:26,代碼來源:server-shutdown.ts

示例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++;
              }
//.........這裏部分代碼省略.........
開發者ID:duniter,項目名稱:duniter,代碼行數:101,代碼來源:proof.ts

示例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
 }
開發者ID:duniter,項目名稱:duniter,代碼行數:5,代碼來源:PowWorker.ts


注:本文中的querablep.default函數示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。