All Products
Search
Document Center

Blockchain as a Service:Use Filter

Last Updated:Mar 18, 2022

When you configure a cloud service integration trigger, you can configure a filter for the trigger, which can filter and process the blockchain data. You can refer to Fabric Sample Message for various data structures of blockchain events. For filter expression syntax, please refer to jq tool basic syntax.

The following example describes how to use a filter to sort out and preprocess blockchain events. A Function Compute trigger is used in this example. You must perform the following steps to prepare the environment:

  1. Download the cloud service integration sample code.

  2. Install the Function Compute tool Serverless Devs. For more information, see Serverless Devs introduction and installation method.

  3. Go to the event-with-filter directory and execute s deploy to deploy a function to Function Compute.

  4. Log on to the Function Compute console and enable log queries for the newly created function LoggerFunc.Enable log queries

  5. Deploy sample chaincode taskmgr in the channel by referring to Deploy a chaincode.

  6. Configure a cloud service integration trigger.

    1. Fill in information about the Function Compute instance by referring to the help documentation.

    2. In the advanced options, fill in filter expressions based on sample scenarios.

  7. Go to the blockchain2sms directory, modify the parameters in main.js based on comments, and fill in the REST API address, refresh token, channel name, and smart contract name. For more information, see Use REST APIs.

  8. Execute npm install to install dependent packages and node main.js to initiate a sample transaction.

  9. View Function Compute logs and check the event content.

Sample scenarios

1. Push only events that meet specific conditions

Push only events whose smart contract event name is event-create-task.

  • Event type: Contract

  • Filter configuration: select(.name=="event-create-task")

  • Example of pushed data:

{
  "content": "eyJuYW1lIjoidGFzay0xNTgwOTcxODQyOTM3IiwiY3JlYXRvciI6ImUyZWhtZnFhc3RoTVNQLm9jdG9wdXNfMjY4NDJfMTIzNDU2Nzg5MDEyMzQiLCJyZXF1aXJlcyI6WyJlMmVobWZxYXN0aE1TUC5vY3RvcHVzXzI2ODQyXzEyMzQ1Njc4OTAxMjM0Il0sImFwcHJvdmVkIjpudWxsLCJkZXNjcmlwdGlvbiI6IuekuuS+i+S7u+WKoe+8jHJlcXVpcmVzIOmFjee9ruWuoeaJueS7u+WKoeWujOaIkOmcgOimgemCo+S6m+eUqOaIt+WQjOaEj+OAgueUqOaIt+aPj+i/sOS4uiAn57uE57uHTVNQLueUqOaIt+WQjeensCcifQ==",
  "id": "contract-131-ca144c3385f56a429b9e8874173f7c97fbb49de69f931aec583ec50222a3a2ed",
  "instance_id": "csi-e2ehmfqasth-bcw7tzao2dzeo",
  "name": "event-create-task",
  "network": "channel3",
  "platform": "Fabric",
  "type": 8
}

2. Push only specific valid transaction events

Push only valid transactions sent to smart contract taskmgr.

  • Event type: Tx

  • Filter configuration: select(.content.to=="taskmgr" and .content.state=="VALID")

  • Example of pushed data:

    {
    "content": {
      "events": [
        "event-create-task"
      ],
      "from": "e2ehmfqasthMSP.octopus_26842_12345678901234",
      "id": "3a2e0e375c6ee0d9c6c20d756e787db44d50bfeba51f22d8f262dd1556dedba0",
      "input": "[\"create\",\"task-1580971842937\",\"{\\n \\\"requires\\\": [\\\"e2ehmfqasthMSP.octopus_26842_12345678901234\\\"],\\n \\\"description\\\": \\\"Sample task. Task completion requires the approval of certain users. The user is described as 'Organization MSP. User name'\\\"\\n }\"]",
      "state": "VALID",
      "to": "taskmgr"
    },
    "id": "tx-128-3a2e0e375c6ee0d9c6c20d756e787db44d50bfeba51f22d8f262dd1556dedba0",
    "instance_id": "csi-e2ehmfqasth-bcw7tzao2dzeo",
    "name": "3a2e0e375c6ee0d9c6c20d756e787db44d50bfeba51f22d8f262dd1556dedba0",
    "network": "channel3",
    "platform": "Fabric",
    "type": 2
    }

3. Push only part of the event content

Push only the content of smart contract events that have the event name event-create-task.

  • Event type: Contract

  • Filter configuration: select(.name=="event-create-task") | .content

  • Example of pushed data:

    "eyJuYW1lIjoidGFzay0xNTgwOTcxODQyOTM3IiwiY3JlYXRvciI6ImUyZWhtZnFhc3RoTVNQLm9jdG9wdXNfMjY4NDJfMTIzNDU2Nzg5MDEyMzQiLCJyZXF1aXJlcyI6WyJlMmVobWZxYXN0aE1TUC5vY3RvcHVzXzI2ODQyXzEyMzQ1Njc4OTAxMjM0Il0sImFwcHJvdmVkIjpudWxsLCJkZXNjcmlwdGlvbiI6IuekuuS+i+S7u+WKoe+8jHJlcXVpcmVzIOmFjee9ruWuoeaJueS7u+WKoeWujOaIkOmcgOimgemCo+S6m+eUqOaIt+WQjOaEj+OAgueUqOaIt+aPj+i/sOS4uiAn57uE57uHTVNQLueUqOaIt+WQjeensCcifQ=="

4. Obtain the write-in information of a blockchain

Push only the write-in information for all keys. The information must be contained in the valid transactions generated by smart contract taskmgr.

  • Event type: Tx

  • Filter configuration: select(.content.state=="VALID" and .content.to=="taskmgr") | .content.data.data.actions.[0].payload.action.proposal_response_payload.extension.results.ns_rwset[] | select(.namespace=="taskmgr") | .rwset.writes

  • Example of pushed data:

    [{"is_delete":false,"key":"\u0000e2ehmfqasthMSP.octopus_26842_12345678901234\u0000task-1581479306267\u0000","value":"eyJuYW1lIjoidGFzay0xNTgwOTcxODQyOTM3IiwiY3JlYXRvciI6ImUyZWhtZnFhc3RoTVNQLm9jdG9wdXNfMjY4NDJfMTIzNDU2Nzg5MDEyMzQiLCJyZXF1aXJlcyI6WyJlMmVobWZxYXN0aE1TUC5vY3RvcHVzXzI2ODQyXzEyMzQ1Njc4OTAxMjM0Il0sImFwcHJvdmVkIjpudWxsLCJkZXNjcmlwdGlvbiI6IuekuuS+i+S7u+WKoe+8jHJlcXVpcmVzIOmFjee9ruWuoeaJueS7u+WKoeWujOaIkOmcgOimgemCo+S6m+eUqOaIt+WQjOaEj+OAgueUqOaIt+aPj+i/sOS4uiAn57uE57uHTVNQLueUqOaIt+WQjeensCcifQ=="}]

5. Monitor specific keys

Monitor valid transactions generated by smart contract taskmgr to check the value change of a specific key.

  • Event type: Tx

  • Filter configuration: select(.content.state=="VALID" and .content.to=="taskmgr") | .content.data.data.actions.[0].payload.action.proposal_response_payload.extension.results.ns_rwset[] | select(.namespace=="taskmgr") | .rwset.writes[] | select(.key=="special_key")

  • Example of pushed data:

    {"is_delete":false,"key":"special_key","value":"MTA="}

5. Export Data to Database Table

We can export specific blockchain data to database table via filter. The data is processed and transformed to an Object Array by filter, each Object represents a record to be inserted. The Object Keys are table column names, while the Object Values are table data:

[
  {
    "columnName1": "value1",
    "columnName2": "value2",
    "columnName3": "value3",
  },
  {
    "columnName1": "value3",
    "columnName2": "value4",
    "columnName3": "value5",
  },
  ...
]

Export the valid transactions content written by smart contract taskmgr to below table:

  • Table structure:

    • primary key: (event_id,namespace,key)

    • event_id: Event ID

    • namespace: name of smart contract

    • key: content of key

    • value: content of value in Base64 encoding

    • create_time: time of written

    • tx_id: transaction ID

    • is_delete: if this is a delete operation

    • creator: initiator of this operation

  • filter configuration: select(.content.state=="VALID" and .content.to=="taskmgr") | .id as $eventID | .content.id as $txID | .content.from as $creator | content.data.header.channel_header.timestamp as $createTime | .content.data.data.actions.[0].payload.action.proposal_response_payload.extension.results.ns_rwset | ap(.namespace as $namespace | .rwset.writes | map( .event_id = $eventID | .namespace = $namespace | .tx_id = $txID | .create_time = $createTime | .creator = $creator )) | dd

  • Event type: Tx

  • Sample of exported data:

    [
    {
      "event_id": "tx-128-3a2e0e375c6ee0d9c6c20d756e787db44d50bfeba51f22d8f262dd1556dedba0",
      "namespace": "taskmgr",
      "tx_id": "3a2e0e375c6ee0d9c6c20d756e787db44d50bfeba51f22d8f262dd1556dedba0",
      "is_delete": false,
      "key": "\u0000e2ehmfqasthMSP.octopus_26842_12345678901234\u0000task-1581479306267\u0000",
      "value": "eyJuYW1lIjoidGFzay0xNTgwOTcxODQyOTM3IiwiY3JlYXRvciI6ImUyZWhtZnFhc3RoTVNQLm9jdG9wdXNfMjY4NDJfMTIzNDU2Nzg5MDEyMzQiLCJyZXF1aXJlcyI6WyJlMmVobWZxYXN0aE1TUC5vY3RvcHVzzI2ODQyXzEyMzQ1Njc4OTAxMjM0Il0sImFwcHJvdmVkIjpudWxsLCJkZXNjcmlwdGlvbiI6IuekuuS+i+S7u+WKoe+8jHlcXVpcmVzIOmFjee9ruWuoeaJueS7u+WKoeWujOaIkOmcgOimgemCo+S6m+eUqOaIt+WQjOaEj+OAgueUqOaIt+aPj+i/sOS4uiAn57uE57uHTVNQLueUqOaIt+WQjeensCcifQ==",
      "create_time": "2020-02-06T06:50:42.267092Z",
      "creator": "e2ehmfqasthMSP.octopus_26842_12345678901234"
    }
    ]