1 2
import { create as createLogger } from '../common/log'
2 2
const log = createLogger('ccp')
3 2
import RouteBroadcaster from '../services/route-broadcaster'
4
import reduct = require('reduct')
5
import { IlpPrepare } from 'ilp-packet'
6 2
import {
7
  CCP_CONTROL_DESTINATION,
8
  CCP_UPDATE_DESTINATION,
9
  deserializeCcpRouteUpdateRequest,
10
  deserializeCcpRouteControlRequest,
11
  serializeCcpResponse
12
} from 'ilp-protocol-ccp'
13

14 2
export default class CcpController {
15
  protected routeBroadcaster: RouteBroadcaster
16

17
  constructor (deps: reduct.Injector) {
18 2
    this.routeBroadcaster = deps(RouteBroadcaster)
19
  }
20

21
  async handle (
22
    data: Buffer,
23
    sourceAccount: string,
24
    { parsedPacket }: { parsedPacket: IlpPrepare }
25
  ) {
26 2
    switch (parsedPacket.destination) {
27 2
      case CCP_CONTROL_DESTINATION:
28 0
        return this.handleRouteControl(data, sourceAccount)
29
      case CCP_UPDATE_DESTINATION:
30 2
        return this.handleRouteUpdate(data, sourceAccount)
31
      default:
32 0
        throw new Error('unrecognized ccp message. destination=' + parsedPacket.destination)
33
    }
34
  }
35

36
  async handleRouteControl (data: Buffer, sourceAccount: string) {
37 0
    const routeControl = deserializeCcpRouteControlRequest(data)
38 0
    log.trace('received route control message. sender=%s, tableId=%s epoch=%s features=%s', sourceAccount, routeControl.lastKnownRoutingTableId, routeControl.lastKnownEpoch, routeControl.features.join(','))
39

40 0
    this.routeBroadcaster.handleRouteControl(sourceAccount, routeControl)
41

42 0
    return serializeCcpResponse()
43
  }
44

45
  async handleRouteUpdate (data: Buffer, sourceAccount: string) {
46 2
    const routeUpdate = deserializeCcpRouteUpdateRequest(data)
47 2
    log.trace('received routes. sender=%s speaker=%s currentEpoch=%s fromEpoch=%s toEpoch=%s newRoutes=%s withdrawnRoutes=%s', sourceAccount, routeUpdate.speaker, routeUpdate.currentEpochIndex, routeUpdate.fromEpochIndex, routeUpdate.toEpochIndex, routeUpdate.newRoutes.length, routeUpdate.withdrawnRoutes.length)
48

49 2
    this.routeBroadcaster.handleRouteUpdate(sourceAccount, routeUpdate)
50

51 2
    return serializeCcpResponse()
52
  }
53
}

Read our documentation on viewing source code .

Loading