66import { Router } from 'express'
77const Chain = require ( "../../../modules/chain" ) ;
88import ChainModel from '../../../models/chain'
9+ import ChainCode from '../../../models/chainCode'
10+ import util from 'util'
911import config from '../../../config'
12+ import Moment from 'moment'
13+ import { extendMoment } from 'moment-range' ;
14+ const moment = extendMoment ( Moment ) ;
15+ const log4js = require ( 'log4js' ) ;
16+ const logger = log4js . getLogger ( __filename . slice ( __dirname . length + 1 ) ) ;
17+ const logLevel = process . env . DEV === "True" ? "DEBUG" : "INFO"
18+ logger . setLevel ( logLevel ) ;
1019
1120const router = new Router ( )
1221
@@ -22,13 +31,24 @@ router.get("/:apikey/list", function(req, res) {
2231 } ) ;
2332} ) ;
2433
34+ router . get ( "/search" , function ( req , res ) {
35+ const chainName = req . query . name ;
36+ ChainModel . count ( { user_id : req . apikey , name : chainName } , function ( err , result ) {
37+ res . json ( {
38+ success : true ,
39+ existed : result > 0
40+ } )
41+ } )
42+ } )
43+
2544router . get ( "/:apikey/db-list" , function ( req , res ) {
2645 ChainModel . find ( { user_id : req . apikey } , function ( err , docs ) {
2746 if ( err ) res . json ( { success : false , err} )
2847 const chains = docs . map ( ( chain , i ) => {
2948 return {
3049 id : chain . id ,
31- name : chain . name
50+ name : chain . name ,
51+ type : chain . type
3252 }
3353 } )
3454 res . json ( {
@@ -90,4 +110,260 @@ router.post("/:apikey/:id/edit", function(req, res) {
90110 } ) ;
91111} ) ;
92112
113+ router . get ( "/:id/blockHeight" , function ( req , res ) {
114+ const chainId = req . params . id ;
115+ ChainModel . findOne ( { _id : chainId } , function ( err , chain ) {
116+ const chainRootDir = util . format ( config . path . chain , req . username , chainId )
117+ const helper = require ( `${ chainRootDir } /lib/helper` )
118+ helper . initialize ( chain . template )
119+ const query = require ( `${ chainRootDir } /lib/query` )
120+ query . getChannelHeight ( 'peer1' , req . username , 'org1' )
121+ . then ( function ( message ) {
122+ res . json ( {
123+ success : true ,
124+ height : parseInt ( message )
125+ } )
126+ } ) ;
127+ } )
128+ } )
129+
130+ router . get ( "/:id/channels" , function ( req , res ) {
131+ const chainId = req . params . id ;
132+ ChainModel . findOne ( { _id : chainId } , function ( err , chain ) {
133+ const chainRootDir = util . format ( config . path . chain , req . username , chainId )
134+ const query = require ( `${ chainRootDir } /lib/query` )
135+ query . initialize ( chain . template )
136+ query . getChannels ( 'peer1' , req . username , 'org1' )
137+ . then ( function ( message ) {
138+ res . send ( {
139+ success : true ,
140+ ...message
141+ } ) ;
142+ } , ( err ) => {
143+ res . send ( {
144+ success : false ,
145+ channels : [ ] ,
146+ error : err . stack ? err . stack : err
147+ } )
148+ } ) . catch ( ( err ) => {
149+ res . send ( {
150+ success : false ,
151+ channels : [ ] ,
152+ error : err . stack ? err . stack : err
153+ } )
154+ } ) ;
155+ } )
156+ } )
157+
158+ router . get ( "/:id/recentBlock" , function ( req , res ) {
159+ const chainId = req . params . id ;
160+ const blockHeight = parseInt ( req . query . blockHeight ) ;
161+ let recentNum = parseInt ( req . query . recentNum ) ;
162+ recentNum = recentNum > blockHeight ? blockHeight : recentNum ;
163+ let blockIds = [ ]
164+ for ( let index = blockHeight - 1 ; index >= blockHeight - recentNum ; index -- ) {
165+ blockIds . push ( index )
166+ }
167+ let allBlocks = [ ]
168+ ChainModel . findOne ( { _id : chainId } , function ( err , chain ) {
169+ let promises = [ ]
170+ for ( let index in blockIds ) {
171+ const blockId = blockIds [ index ] ;
172+ let p = new Promise ( ( resolve , reject ) => {
173+ const chainRootDir = util . format ( config . path . chain , req . username , chainId )
174+ const query = require ( `${ chainRootDir } /lib/query` )
175+ query . initialize ( chain . template )
176+ query . getBlockByNumber ( 'peer1' , blockId , req . username , 'org1' )
177+ . then ( function ( message ) {
178+ const { header : { data_hash} } = message ;
179+ let txTimestamps = [ ]
180+ message . data . data . map ( ( item , index ) => {
181+ const { payload : { header : { channel_header : { tx_id, timestamp, channel_id} } } } = item ;
182+ const txTime = moment ( timestamp , "ddd MMM DD YYYY HH:mm:ss GMT+0000 (UTC)" )
183+ txTimestamps . push ( txTime . utc ( ) )
184+ } )
185+ txTimestamps = txTimestamps . sort ( function ( a , b ) { return a - b ; } ) ;
186+ allBlocks . push ( {
187+ id : blockId ,
188+ hash : data_hash ,
189+ transactions : message . data . data . length ,
190+ timestamp : txTimestamps . slice ( - 1 ) . pop ( )
191+ } )
192+ resolve ( )
193+ } )
194+ } )
195+ promises . push ( p )
196+ }
197+ Promise . all ( promises ) . then ( ( ) => {
198+ res . json ( { success : true , allBlocks} )
199+ } )
200+ } )
201+ } )
202+
203+ router . get ( "/:id/recentTransaction" , function ( req , res ) {
204+ const chainId = req . params . id ;
205+ const blockHeight = parseInt ( req . query . blockHeight ) ;
206+ let recentNum = parseInt ( req . query . recentNum ) ;
207+ recentNum = recentNum > blockHeight ? blockHeight : recentNum ;
208+ let blockIds = [ ]
209+ for ( let index = blockHeight - 1 ; index >= blockHeight - recentNum ; index -- ) {
210+ blockIds . push ( index )
211+ }
212+ let allTransactions = [ ]
213+ ChainModel . findOne ( { _id : chainId } , function ( err , chain ) {
214+ let promises = [ ]
215+ for ( let index in blockIds ) {
216+ const blockId = blockIds [ index ] ;
217+ let p = new Promise ( ( resolve , reject ) => {
218+ const chainRootDir = util . format ( config . path . chain , req . username , chainId )
219+ const query = require ( `${ chainRootDir } /lib/query` )
220+ query . initialize ( chain . template )
221+ query . getBlockByNumber ( 'peer1' , blockId , req . username , 'org1' )
222+ . then ( function ( message ) {
223+ message . data . data . map ( ( item , index ) => {
224+ const { payload : { header : { channel_header : { tx_id, timestamp, channel_id} } } } = item ;
225+ const txTime = moment ( timestamp , "ddd MMM DD YYYY HH:mm:ss GMT+0000 (UTC)" )
226+ if ( tx_id ) {
227+ allTransactions . push ( {
228+ id : tx_id ,
229+ timestamp : txTime . utc ( ) ,
230+ channelId : channel_id
231+ } )
232+ }
233+ } )
234+ resolve ( )
235+ } )
236+ } )
237+ promises . push ( p )
238+ }
239+ Promise . all ( promises ) . then ( ( ) => {
240+ res . json ( { success : true , allTransactions} )
241+ } )
242+ } )
243+ } )
244+
245+ router . get ( "/:id/queryByBlockId" , function ( req , res ) {
246+ const blockId = req . query . id ;
247+ const chainId = req . params . id ;
248+ ChainModel . findOne ( { _id : chainId } , function ( err , chain ) {
249+ const chainRootDir = util . format ( config . path . chain , req . username , chainId )
250+ const query = require ( `${ chainRootDir } /lib/query` )
251+ query . initialize ( chain . template )
252+ query . getBlockByNumber ( 'peer1' , blockId , req . username , 'org1' )
253+ . then ( function ( message ) {
254+ let txList = [ ]
255+ message . data . data . map ( ( item , index ) => {
256+ const { payload : { header : { channel_header : { tx_id, timestamp, channel_id} } } } = item ;
257+ const txTime = moment ( timestamp , "ddd MMM DD YYYY HH:mm:ss GMT+0000 (UTC)" )
258+ txList . push ( {
259+ id : tx_id ,
260+ timestamp : txTime . unix ( ) ,
261+ channelId : channel_id
262+ } )
263+ } )
264+ res . send ( {
265+ success : true ,
266+ txList
267+ } ) ;
268+ } , ( err ) => {
269+ res . json ( {
270+ success : false ,
271+ error : err . stack ? err . stack : err
272+ } )
273+ } ) . catch ( ( err ) => {
274+ res . json ( {
275+ success : false ,
276+ txList : [ ] ,
277+ error : err . stack ? err . stack : err
278+ } )
279+ } ) ;
280+ } )
281+ } )
282+
283+ router . get ( "/:id/queryByTransactionId" , function ( req , res ) {
284+ const trxnId = req . query . id ;
285+ const chainId = req . params . id ;
286+ ChainModel . findOne ( { _id : chainId } , function ( err , chain ) {
287+ const chainRootDir = util . format ( config . path . chain , req . username , chainId )
288+ const query = require ( `${ chainRootDir } /lib/query` )
289+ query . initialize ( chain . template )
290+ query . getTransactionByID ( 'peer1' , trxnId , req . username , 'org1' )
291+ . then ( function ( message ) {
292+ const { transactionEnvelope : { payload : { data : { actions} } } , validationCode} = message
293+ const action = actions . length ? actions [ 0 ] : { }
294+ const { payload : { chaincode_proposal_payload : { input : { chaincode_spec : { type, chaincode_id : { name} , input : { args} } } } } } = action
295+ res . json ( {
296+ success : true ,
297+ validationCode,
298+ type,
299+ name,
300+ args
301+ } )
302+ } , ( err ) => {
303+ res . json ( {
304+ success : false ,
305+ error : err . stack ? err . stack : err
306+ } )
307+ } ) . catch ( ( err ) => {
308+ res . json ( {
309+ success : false ,
310+ error : err . stack ? err . stack : err
311+ } )
312+ } ) ;
313+ } )
314+ } )
315+
316+ router . get ( "/:id/chainCodes" , function ( req , res ) {
317+ const chainId = req . params . id ;
318+ let allChainCodes = [ ]
319+ ChainModel . findOne ( { _id : chainId } , function ( err , chain ) {
320+ const chainRootDir = util . format ( config . path . chain , req . username , chainId )
321+ const query = require ( `${ chainRootDir } /lib/query` )
322+ query . initialize ( chain . template )
323+ query . getInstalledChaincodes ( 'peer1' , 'instantiated' , req . username , 'org1' )
324+ . then ( function ( message ) {
325+ let promises = [ ]
326+ for ( let index in message ) {
327+ const chainCodeString = message [ index ] ;
328+ let l = chainCodeString . slice ( 0 , - 1 ) . split ( ',' ) ;
329+ let chainCode = { } ;
330+ for ( let i in l ) {
331+ let a = l [ i ] . split ( ':' ) ;
332+ logger . debug ( `key ${ a [ 0 ] } ${ a [ 1 ] } ` )
333+ chainCode [ a [ 0 ] . trim ( ) ] = a [ 1 ] . trim ( ) ;
334+ }
335+ let p = new Promise ( ( resolve , reject ) => {
336+ logger . debug ( `chain code name ${ chainCode . name } ` )
337+ ChainCode . findOne ( { chainCodeName : chainCode . name } , function ( err , chainCodeDoc ) {
338+ if ( err ) {
339+ resolve ( )
340+ } else {
341+ if ( chainCodeDoc ) {
342+ allChainCodes . push ( {
343+ name : chainCodeDoc . name
344+ } )
345+ }
346+ resolve ( )
347+ }
348+ } )
349+ } )
350+ promises . push ( p )
351+ }
352+ Promise . all ( promises ) . then ( ( ) => {
353+ res . json ( { success : true , allChainCodes} )
354+ } )
355+ } , ( err ) => {
356+ res . json ( {
357+ success : false ,
358+ error : err . stack ? err . stack : err
359+ } )
360+ } ) . catch ( ( err ) => {
361+ res . json ( {
362+ success : false ,
363+ error : err . stack ? err . stack : err
364+ } )
365+ } ) ;
366+ } )
367+ } )
368+
93369export default router
0 commit comments