github.com/muhammedhassanm/blockchain@v0.0.0-20200120143007-697261defd4d/sawtooth-supply-chain-master/server/blockchain/index.js (about)

     1  /**
     2   * Copyright 2017 Intel Corporation
     3   *
     4   * Licensed under the Apache License, Version 2.0 (the "License");
     5   * you may not use this file except in compliance with the License.
     6   * You may obtain a copy of the License at
     7   *
     8   *     http://www.apache.org/licenses/LICENSE-2.0
     9   *
    10   * Unless required by applicable law or agreed to in writing, software
    11   * distributed under the License is distributed on an "AS IS" BASIS,
    12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13   * See the License for the specific language governing permissions and
    14   * limitations under the License.
    15   * ----------------------------------------------------------------------------
    16   */
    17  'use strict'
    18  
    19  const _ = require('lodash')
    20  const { Stream } = require('sawtooth-sdk/messaging/stream')
    21  const {
    22    Message,
    23    ClientBatchSubmitRequest,
    24    ClientBatchSubmitResponse,
    25    ClientBatchStatus,
    26    ClientBatchStatusRequest,
    27    ClientBatchStatusResponse
    28  } = require('sawtooth-sdk/protobuf')
    29  
    30  const batcher = require('./batcher')
    31  const config = require('../system/config')
    32  
    33  const VALIDATOR_URL = config.VALIDATOR_URL
    34  const stream = new Stream(VALIDATOR_URL)
    35  
    36  const connect = () => {
    37    return new Promise(resolve => stream.connect(resolve))
    38      .then(() => {
    39        stream.onReceive(msg => {
    40          console.warn('Received message of unknown type:', msg.messageType)
    41        })
    42      })
    43  }
    44  
    45  const submit = (txnBytes, { wait }) => {
    46    const batch = batcher.batch(txnBytes)
    47  
    48    return stream.send(
    49      Message.MessageType.CLIENT_BATCH_SUBMIT_REQUEST,
    50      ClientBatchSubmitRequest.encode({
    51        batches: [batch]
    52      }).finish()
    53    )
    54    .then(response => ClientBatchSubmitResponse.decode(response))
    55    .then((decoded) => {
    56      const submitStatus = _.findKey(ClientBatchSubmitResponse.Status,
    57                               val => val === decoded.status)
    58      if (submitStatus !== 'OK') {
    59        throw new Error(`Batch submission failed with status '${submitStatus}'`)
    60      }
    61  
    62      if (wait === null) {
    63        return { batch: batch.headerSignature }
    64      }
    65  
    66      return stream.send(
    67        Message.MessageType.CLIENT_BATCH_STATUS_REQUEST,
    68        ClientBatchStatusRequest.encode({
    69          batchIds: [batch.headerSignature],
    70          wait: true,
    71          timeout: wait
    72        }).finish()
    73      )
    74      .then(statusResponse => {
    75        const statusBody = ClientBatchStatusResponse
    76          .decode(statusResponse)
    77          .batchStatuses[0]
    78  
    79        if (statusBody.status !== ClientBatchStatus.Status.COMMITTED) {
    80          const id = statusBody.batchId
    81          const status = _.findKey(ClientBatchStatus.Status,
    82                                   val => val === statusBody.status)
    83          const message = statusBody.invalidTransactions.length > 0
    84            ? statusBody.invalidTransactions[0].message
    85            : ''
    86          throw new Error(`Batch ${id} is ${status}, with message: ${message}`)
    87        }
    88  
    89        // Wait to return until new block is in database
    90        return new Promise(resolve => setTimeout(() => {
    91          resolve({ batch: batch.headerSignature })
    92        }, 1000))
    93      })
    94    })
    95  }
    96  
    97  module.exports = {
    98    connect,
    99    submit
   100  }