The introduction of smart contracts in blockchain has created that business logic tier that was missing in the first evolution of blockchain. Smart contracts offer the possibility to apply conditional logic to transactions before they are executed. Still, smart contracts can operate on data that is stored on the blockchain digital ledger only.
Business processes, however, rarely run in isolation. They often need data integration with external systems and devices. Requirements of these processes may consist of transactions initiated on a distributed ledger that include data from an external system, service, or device.
There may be situations when events generated within a blockchain, like a payment or a change of state of a smart contract, require some activities to happen in the outside world, off-chain. External systems may also react to events raised by smart contracts in response to validation logic.
Addressing these integration needs is key to businesses to adopt blockchain solutions that connect with external systems. This form of messaging integration focuses on two types of messages: messages requesting transactions to execute on a distributed ledger, and events exposed by that ledger when transactions have taken place.
In this article, I’ll present two integration patterns for blockchain networks that leverage the REST API exposed by Azure Blockchain Workbench.
Azure Blockchain Workbench
Azure Blockchain Workbench is a collection of Azure services and capabilities designed to help you create and deploy blockchain applications to share business processes and data with other organizations. Azure Blockchain Workbench provides the infrastructure scaffolding for building blockchain applications enabling developers to focus on creating business logic and smart contracts. It also makes it easier to create blockchain applications by integrating several Azure services and capabilities to help automate common development tasks.
Azure Blockchain Workbench provides a web application and REST API for managing blockchain applications and users. You can use the Blockchain Workbench REST API and message-based API to integrate with existing systems. Workbench automatically delivers events to Service Bus and Event Grid to send messages to downstream consumers. Developers can integrate with either of these messaging systems to drive transactions and to look at results.
Blockchain Workbench also includes a REST-based gateway service API. When writing to a blockchain, the API generates and delivers messages to an event broker. When data is requested by the API, queries are sent to the off-chain SQL database. The SQL database contains a replica of on-chain data and metadata that provides context and configuration information for supported smart contracts. Queries return the required data from the off-chain replica in a format informed by the metadata for the contract.
Blockchain Integration Patterns
Smart contracts often represent a business workflow that needs to integrate with external systems and devices. The REST API and messaging integration sends transactions from external systems to smart contracts included in an Azure Blockchain Workbench application. It also sends event notifications to external systems based on changes that take place within an application.
The following data integration patterns describe the best practices for connecting external systems to and from Azure Blockchain Workbench.
Sending Data to a Blockchain Ledger
The Azure Blockchain Workbench REST API provides the ability to send authenticated requests to execute transactions on a distributed ledger.
This occurs using the process depicted above, where:
- The external application authenticates to the Azure Active Directory provisioned as part of the Azure Blockchain Workbench deployment.
- Authorized users receive a bearer token that can be sent with requests to the API.
- External applications make calls to the REST API using the bearer token.
- The REST API packages the request as a message and sends it to the Service Bus. From here it will be retrieved, signed, and sent to the appropriate distributed ledger.
- The REST API makes a request to the Azure Blockchain Workbench SQL DB to record the request and establish the current provisioning status.
- The SQL DB returns the provisioning status and the API call returns the ID to the external application that called it.
Handling Events in a Smart Contract
When an event occurs within a smart contract, for example, a state change or the execution of a specific type of transaction, it is broadcast via an Azure Event Grid to downstream consumers, and those consumers then take appropriate actions.
This scenario occurs using the process depicted in the previous diagram, where:
- The smart contract transitions to a new state and sends an event to the ledger.
- The ledger receives and delivers the event to Azure Blockchain Workbench.
- Azure Blockchain Workbench is subscribed to events from the ledger and receives the event.
- Azure Blockchain Workbench publishes the event to subscribers on the Event Grid.
- External systems are subscribed to the Event Grid, consume the message, and take the appropriate actions.
In conclusion, consuming a specific API exposed by Azure Blockchain Workbench for managing smart contracts, users and data stored on a blockchain ledger, allows for integration with external systems in response to needs for data transfer into the digital ledger, or to events raised by a smart contract.