Advertisement
Guest User

Untitled

a guest
Oct 12th, 2018
1,153
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const request = require('request')
  2.  
  3. class MyEventHub {
  4.     constructor(eventHub) {
  5.         // Stores processed transaction ids
  6.         this.transactions = {}
  7.         this.currentBlock = null
  8.         this.channel = eventHub._channel
  9.         eventHub.registerBlockEvent(this.handleBlockEvent.bind(this), this.onErrorBlock)
  10.     }
  11.  
  12.     // Sets up listner for transaction ids
  13.     // Success = success callback -> passes in result to success
  14.     // err = error callback -> run if we have an error?
  15.     // transactionid = transaction Id - string(?)
  16.     registerTxEvent(transactionId, success, err) {
  17.         if (this.transactions[transactionId.toString()]) {
  18.             // We already have the transaction processed, so run success callback
  19.             return success()
  20.             // but what about the payload?
  21.         }
  22.  
  23.         this.transactions[transactionId.toString()] = {success, err, callback: true}
  24.     }
  25.  
  26.     onErrorBlock(error) {
  27.         console.log('Blockevent listener encountered an error :/')
  28.         console.error(error)
  29.     }
  30.  
  31.     // wrappre around processBlock
  32.     async handleBlockEvent(block) {
  33.         let blockNumber = block.header.number
  34.         // See if we've missed out any blocks
  35.         if (this.currentBlock && this.currentBlock < blockNumber - 1) {
  36.             // We've missed some blocks. need to go and fetch them all
  37.             // set block incase we receive another one while processing older blocks
  38.             let missingBlockNumber = this.currentBlock
  39.             this.currentBlock = blockNumber
  40.             console.log('missed blocks, going back to fetch some :)')
  41.             while (missingBlockNumber < blockNumber-1) {
  42.                 let block = await this.channel.queryBlock(missingBlockNumber)
  43.                 this.processBlock(block)
  44.             }
  45.         } else {
  46.             this.currentBlock = blockNumber
  47.         }
  48.  
  49.         this.processBlock(block)
  50.     }
  51.  
  52.     processBlock(block) {
  53.         // loop through transactions and see what we can do here
  54.         for (let transaction of block.data.data) {
  55.             let header = transaction.payload.header; // the "header" object contains metadata of the transaction
  56.             let tx_id = header.channel_header.tx_id
  57.             let actions = transaction.payload.data.actions
  58.             for (let action of actions) {
  59.                 let prp = action.payload.action.proposal_response_payload
  60.                 let {chaincode_id, events, response} = prp.extension
  61.                 let listener = this.transactions[tx_id]
  62.                 if (listener) {
  63.                     if (response.status > 300) {
  64.                         console.log('we have recieved a strange status')
  65.                         listener.success(response, 'WIERD')
  66.                     } else {
  67.                         listener.success(response, 'VALID')
  68.                     }
  69.                 } else {
  70.                     this.transactions[tx_id] = {response, callback: false}
  71.                 }
  72.             }
  73.         }
  74.  
  75.         let id = block.header.data_hash
  76.         request.put({
  77.             url: `${url}/${db}/${id}`,
  78.             body: block,
  79.             json: true
  80.         }, function(err, resp, body) {
  81.             console.log(err, resp, body)
  82.         })
  83.     }
  84. }
  85.  
  86.  
  87.  
  88.  
  89. module.exports = MyEventHub
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement