A method and device for communication between blockchain and off-chain devices

By performing specific transactions and contract operations between blockchain and off-chain devices, two-way communication and data processing are realized, solving the problem of single-direction communication in the existing technology, and ensuring the Secure data transmission and status management.

CN113691633BActive Publication Date: 2024-12-20ANT BLOCKCHAIN TECHNOLOGY (SHANGHAI) CO LTD
View PDF 0 Cites 0 Cited by

Patent Information

Application Number
CN202110998412.9
Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2021-03-30
Publication Date
2024-12-20
Estimated Expiration
2041-03-30

AI Technical Summary

Technical Problem

The existing communication solutions between blockchain and off-chain devices only realize single-direction communication, and cannot achieve two-way communication, resulting in the blockchain being unable to actively obtain off-chain data or resources.

Method used

Ensure two-way communication and data retention periods by performing a series of transactions and contract operations between blockchain nodes and off-chain devices, including pre-opening, opening, data processing, pre-closing and closing messages, and recording sessions with smart contracts Status facilitate query and communication.

Benefits of technology

It realizes secure two-way communication between blockchain and off-chain devices, ensures the retention and processing of session data, and facilitates the query and management of session status by each node.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN113691633B_ABST
    Figure CN113691633B_ABST
Patent Text Reader

Abstract

The embodiments of this specification provide a method and apparatus for communication between a blockchain and an off-chain device. The method is executed by a blockchain node, including: executing a first transaction to provide a pre-opening message of a first session to the off-chain device, wherein the first session is a session between the blockchain and the off-chain device; executing a second transaction sent by the off-chain device to provide the off-chain device with an opening message of the first session, wherein the second transaction includes confirmation information of the pre-opening message.
Need to check novelty before this filing date? Find Prior Art

Description

[0001] This application is a divisional application of the invention patent application with application number 202110340111.7 filed on March 30, 2021, and entitled “A method and device for communication between blockchain and off-chain devices”. Technical Field

[0002] The embodiments of this specification relate to the field of blockchain technology, and more specifically, to a method and apparatus for communication between a blockchain and off-chain devices. Background Art

[0003] Blockchain technology, also known as distributed ledger technology, is a decentralized distributed database technology with the characteristics of decentralization, openness, transparency, immutability and trustworthiness. Every transaction in the blockchain will be broadcast to the blockchain nodes of the entire network, and each full node has full and consistent data. Since the blockchain is a closed environment, the blockchain cannot actively initiate network calls, and the smart contracts on the chain can only passively receive data. Therefore, the blockchain cannot actively obtain data or resources outside the chain. In the prior art, in order to overcome the above problems, the off-chain trusted device can log in to the blockchain client to exchange information with the blockchain smart contract, so as to provide the blockchain with off-chain data or resources, such as off-chain information, off-chain data processing capabilities, etc. However, in the existing communication schemes between blockchain and off-chain devices, only one-way communication is realized in which the blockchain receives information from the off-chain device through the contract.

[0004] Therefore, a more efficient solution is needed for communicating between blockchain and off-chain devices. Summary of the invention

[0005] The embodiments of this specification are intended to provide a more effective communication solution between blockchain and off-chain devices to address the deficiencies in the prior art.

[0006] To achieve the above-mentioned purpose, in one aspect, this specification provides a method for communication between a blockchain and an off-chain device, the method being executed by a blockchain node, comprising:

[0007] Executing a first transaction to provide a pre-opening message of a first session to an off-chain device, wherein the first session is a session between the blockchain and the off-chain device;

[0008] A second transaction sent by the off-chain device is executed to provide the off-chain device with a start message of the first session, wherein the second transaction includes confirmation information of the pre-start message.

[0009] In one embodiment, executing the first transaction further includes executing the first transaction to store a pre-opening message of the first session in the blockchain.

[0010] In one embodiment, executing the first transaction further includes executing the first transaction to record the state of the first session as a pre-opening state in the blockchain.

[0011] In one embodiment, the first contract is called in the first transaction, wherein recording the state of the first session as a pre-opening state in the blockchain includes recording the state of the first session as a pre-opening state in the account state of the first contract.

[0012] In one embodiment, the first contract is called in the second transaction, and the execution of the second transaction sent by the off-chain device includes executing the first contract called in the second transaction to determine whether the second transaction is executed within a first predetermined time period, and in the case where it is determined that the second transaction is executed within the first predetermined time period, providing a start message of the first session to the off-chain device.

[0013] In one embodiment, executing the first contract called in the second transaction further causes, in a case where it is determined that the second transaction is executed after a first predetermined period of time, the first session to be terminated.

[0014] In one implementation, providing the off-chain device with an opening message of the first session includes modifying a state of the first session to an opening state.

[0015] In one implementation, providing the off-chain device with a start message of the first session includes storing the start message of the first session in a blockchain.

[0016] In one embodiment, the method further includes, after providing a start message of the first session to the off-chain device, executing a third transaction that calls the first contract, wherein the third transaction includes first data to be processed, so as to determine whether the state of the first session is an open state, and when it is determined that the state of the first session is an open state, storing the first data in association with the first session in the blockchain.

[0017] In one embodiment, the method further comprises:

[0018] executing a fourth transaction sent by the off-chain device, wherein the fourth transaction includes pre-closing information,

[0019] So as to determine whether all messages sent by the off-chain device are received;

[0020] In a case where it is determined that all messages sent by the off-chain device are received, a closing message of the first session is provided to the off-chain device.

[0021] In one implementation, providing the off-chain device with a closing message of the first session includes modifying a state of the first session to a closed state.

[0022] In one embodiment, executing the fourth transaction also makes it possible that, in the event that it is determined that at least part of the message sent by the off-chain device has not been received, request information is stored in the blockchain in association with the first session, and the request information is used to request the off-chain device to resend the at least part of the message.

[0023] In one embodiment, executing the fourth transaction further causes, in a case where it is determined that a message at least partially sent by the off-chain device is not received, the state of the first session is modified to a pre-closed state.

[0024] Another aspect of the present specification provides a method for communication between a blockchain and an off-chain device, the method being executed by the off-chain device, comprising:

[0025] Obtain a pre-start message of a first session from the blockchain, where the first session is a session between the blockchain and the off-chain device;

[0026] Sending a second transaction to the blockchain, wherein the second transaction includes confirmation information of the pre-opening message;

[0027] A data retention period for the first session is started.

[0028] In one implementation, sending the second transaction to the blockchain includes sending the second transaction to the blockchain within a first predetermined time period.

[0029] In one embodiment, the method further includes, after starting the data retention period, determining whether to obtain a start message of the first session from the blockchain within a second predetermined period, and ending the data retention period when the start message of the first session is not obtained from the blockchain within the second predetermined period.

[0030] In one embodiment, the method further includes, when a preset condition is met, sending a fourth transaction to the blockchain, wherein the fourth transaction includes pre-closing information of the first session.

[0031] In one embodiment, the method further includes, after sending a fourth transaction to the blockchain, obtaining a closing message of the first session from the blockchain; and ending the data retention period.

[0032] In one embodiment, the method further includes, after sending a fourth transaction to the blockchain, obtaining a request message for at least part of the messages in the first session that have been sent from the blockchain; and sending a fifth transaction to the blockchain, wherein the fifth transaction includes the at least part of the messages.

[0033] On the other hand, the present specification provides a communication device for a blockchain and an off-chain device, wherein the device is deployed in a blockchain node and includes:

[0034] A first execution unit is configured to execute a first transaction to provide a pre-opening message of a first session to an off-chain device, wherein the first session is a session between the blockchain and the off-chain device;

[0035] The second execution unit is configured to execute a second transaction sent by the off-chain device to provide the off-chain device with an opening message of the first session, wherein the second transaction includes confirmation information of the pre-opening message.

[0036] In one embodiment, the first execution unit is further configured to execute a first transaction to store a pre-opening message of the first session in the blockchain.

[0037] In one embodiment, the first execution unit is further configured to execute a first transaction to record the state of the first session as a pre-opening state in the blockchain.

[0038] In one embodiment, the first contract is called in the first transaction, wherein the first execution unit is further configured to record the state of the first session as a pre-opening state in the account state of the first contract.

[0039] In one embodiment, the first contract is called in the second transaction, and the second execution unit is further configured to execute the first contract called in the second transaction, so as to determine whether the second transaction is executed within a first predetermined time period, and in the case where it is determined that the second transaction is executed within the first predetermined time period, provide a start message of the first session to the off-chain device.

[0040] In one embodiment, the first execution unit is further configured to, in the case where it is determined that the second transaction is executed after a first predetermined period of time, end the first session.

[0041] In one implementation, the second execution unit is further configured to modify the state of the first session to an open state.

[0042] In one implementation, the second execution unit is further configured to store a start message of the first session in the blockchain.

[0043] In one embodiment, the apparatus further includes a third execution unit configured to, after providing a start message of the first session to the off-chain device, execute a third transaction that calls the first contract, wherein the third transaction includes first data to be processed, so as to determine whether the state of the first session is an open state, and when it is determined that the state of the first session is an open state, store the first data in association with the first session in the blockchain.

[0044] In one embodiment, the device further comprises:

[0045] a fourth execution unit, configured to execute a fourth transaction sent by the off-chain device, wherein the fourth transaction includes pre-closing information;

[0046] So as to determine whether all messages sent by the off-chain device are received;

[0047] In a case where it is determined that all messages sent by the off-chain device are received, a closing message of the first session is provided to the off-chain device.

[0048] In one implementation, the fourth execution unit is further configured to modify the state of the first session to a closed state.

[0049] In one embodiment, the fourth execution unit is also configured to, in the event that it is determined that at least part of the message sent by the off-chain device has not been received, store request information in association with the first session in the blockchain, wherein the request information is used to request the off-chain device to resend at least part of the message.

[0050] In one embodiment, the fourth execution unit is further configured to, in a case where it is determined that at least part of the message sent by the off-chain device is not received, modify the state of the first session to a pre-closing state.

[0051] On the other hand, the present specification provides a communication device for a blockchain and an off-chain device, wherein the device is deployed on the off-chain device and includes:

[0052] A first acquisition unit is configured to acquire a pre-opening message of a first session from a blockchain, where the first session is a session between the blockchain and the off-chain device;

[0053] A first sending unit is configured to send a second transaction to the blockchain, wherein the second transaction includes confirmation information of the pre-opening message;

[0054] The starting unit is configured to start the data retention period of the first session.

[0055] In one implementation, the first sending unit is further configured to send a second transaction to the blockchain within a first predetermined time period.

[0056] In one embodiment, the device further includes a determination unit configured to determine whether to obtain a start message of the first session from the blockchain within a second predetermined time period after the data retention period is started, and a first ending unit configured to end the data retention period when the start message of the first session is not obtained from the blockchain within the second predetermined time period.

[0057] In one embodiment, the device further includes a second sending unit configured to send a fourth transaction to the blockchain when a preset condition is met, wherein the fourth transaction includes pre-closing information of the first session.

[0058] In one embodiment, the device further includes: a second acquisition unit configured to, after sending a fourth transaction to the blockchain, acquire a closing message of the first session from the blockchain; and a second ending unit configured to end the data retention period.

[0059] In one embodiment, the device also includes a third acquisition unit configured to, after sending a fourth transaction to the blockchain, obtain a request message for at least part of the messages in the first session that have been sent from the blockchain; and a third sending unit configured to send a fifth transaction to the blockchain, wherein the fifth transaction includes at least part of the message.

[0060] On the other hand, the present specification provides a computer-readable storage medium having a computer program stored thereon, which, when executed in a computer, causes the computer to execute any one of the above methods.

[0061] Another aspect of the present specification provides a computing device, including a memory and a processor, wherein the memory stores a computer program, and the processor implements any one of the above methods when executing the computer program.

[0062] Through the communication scheme between the blockchain and the off-chain device according to the embodiment of this specification, the session can be opened after the blockchain and the off-chain device have both confirmed the channel, and the off-chain device guarantees the session data retention period during the session opening, thereby ensuring secure two-way communication between the blockchain and the off-chain device. In addition, in the embodiment of this specification, by using the smart contract in the blockchain to conduct a session with the off-chain device, and recording the status of the session in the account status of the smart contract, it is convenient for each node to query the session status, and communication with the off-chain device can be carried out based on the session status. BRIEF DESCRIPTION OF THE DRAWINGS

[0063] The embodiments of this specification are described in conjunction with the accompanying drawings to make the embodiments of this specification clearer:

[0064] Figure 1 A schematic diagram of a blockchain system according to an embodiment of this specification is shown;

[0065] Figure 2 A flow chart showing a communication method between a blockchain 11 and an off-chain device 12 according to an embodiment of this specification;

[0066] Figure 3 A communication device 300 for a blockchain and an off-chain device according to an embodiment of this specification is shown;

[0067] Figure 4 A communication device 400 for use between a blockchain and an off-chain device according to an embodiment of this specification. DETAILED DESCRIPTION

[0068] The embodiments of this specification will be described below with reference to the accompanying drawings.

[0069] Figure 1 A schematic diagram of a blockchain system according to an embodiment of this specification is shown. Figure 1 As shown, the system includes a blockchain 11 and an off-chain device 12. Among them, a contract C1 and a communication contract C2 are deployed in the blockchain 11. Among them, the communication contract C2 is used for two-way communication with the off-chain device, and the contract C1 realizes data interaction with the off-chain device by calling the communication contract C2. The off-chain device 12 is a trusted device, and the trusted device can realize its credibility in the form of hardware, software, etc. For example, the off-chain device 12 may include a trusted execution environment (TEE), and the TEE has its own hardware private key and public key, or the off-chain device 12 realizes its credibility through algorithms such as secure multi-party computing and zero-knowledge implementation, or the off-chain device 12 can be a device of a trusted authority, and so on.

[0070] The communication contract C2 can conduct a conversation with at least one off-chain device. The communication contract C2 includes, for example, a request to open a conversation function, a conversation opening function, a data sending function, a result return function, and a conversation closing function, and the conversation status between the communication contract C2 and each off-chain device is recorded in the account database of the blockchain 11, and the conversation status includes, for example: pre-opening state, opening state, pre-closing state, and closing state.

[0071] For example, node 1 in blockchain 11 wants to call contract C1 for data processing. Since communication contract C2 is called in contract C1, node 1 can first query the status of communication contract C2 in the account database of blockchain to determine whether communication contract C2 has established a session with off-chain device 12. If it is found that the session status between communication contract C2 and off-chain device 12 is closed, node 1 can send transaction 1 and call the request to open session function in communication contract C2 in transaction 1 to send a pre-open message to off-chain device 12. After the node of blockchain 11 executes transaction 1, the "pre-open message" sent by communication contract C2 to off-chain device 12 is stored in the blockchain, and the status of session 1 between communication contract C2 and off-chain device is recorded as "pre-open state". Off-chain device 12 can read the above "pre-open message" by reading the data in blockchain 11, and send transaction 2 to blockchain 11, which calls the session opening function in communication contract C2. Off-chain device 12 starts the data retention period while sending transaction 2, thereby ensuring the security of data in the session. After executing transaction 2, the node in blockchain 11 changes the status of session 1 to "open status".

[0072] After confirming that the status of session 1 has changed to "open", node 1 can send transaction 3 to call contract C1, such as Figure 1 As shown in , the data sending function in the communication contract C2 is called with the data D1 as a parameter in the contract C1. When the node in the blockchain 11 executes the transaction 3, the communication contract C2 called therein is executed, thereby storing the data D1 for sending to the off-chain device 12 in the blockchain. For example, Figure 1 As shown in , in the blockchain 11, data D1 is sent to the off-chain device 12 in the form of a receipt R1 including data D1 and a Spv certificate. The blockchain 11 can be any blockchain, such as a Bitcoin chain, an Ethereum chain, a Hyperledger Fabric blockchain (hereinafter referred to as a Fabric chain), etc. Different blockchains have different on-chain data certification methods. For example, the Fabric chain uses the endorsement node signature method, and the Ethereum chain uses the Spv certification method. The following uses the Ethereum chain as an example for explanation.

[0073] Then, the off-chain device 12 can read the receipt R1 and its Spv certificate from the blockchain 11. The off-chain device 12 includes a Spv client. After obtaining the receipt R1 and the Spv certificate, the off-chain device 12 uses the Spv client to verify the receipt R1 through the Spv verification method, and after verification, obtains the data D1 sent by the communication contract C2 from the receipt R1, and performs data processing on the data D1.

[0074] The off-chain device 12 is, for example, a TEE device, which, after processing the data D1, obtains, for example, a processing result, namely, a processing result E1. The off-chain device 12 signs the processing result E1 using its hardware private key, and sends a transaction 4 to the blockchain that calls the communication contract C2, wherein the call calls the result return function of the communication contract C2 with the processing result E1 and its signature as parameters, so as to provide the processing result E1 and its signature to the communication contract C2. Thus, when the transaction 4 is executed in the blockchain, the communication contract C2 verifies the signature of the processing result E1 using the public key of the off-chain device 12 preset therein, and provides the processing result E1 to the contract C1 by calling back the contract C1 after the verification is passed.

[0075] When the off-chain device 12 is about to end the session with the communication contract C2, it sends transaction 5 to the blockchain 11, and calls the request to close the session function of the communication contract C2 in transaction 5. When the node in the blockchain 11 executes transaction 5, it determines whether the data from the off-chain device 12 has been received. If it is determined that the data has been received, the "session closing message" is stored in the blockchain, and the state of the session 1 is changed to the closed state, thereby ending the execution of transaction 5. After reading the "session closing message" from the blockchain 11, the off-chain device 12 ends the above data retention period, that is, the data in the above session is deleted.

[0076] It is understandable that the above reference Figure 1 The description is only illustrative and is not intended to limit the scope of the embodiments of this specification. For example, in the blockchain 11, it is not limited to calling the communication contract through the contract C1. For example, the communication contract C2 can also be directly called with the data D1 as the input parameter in the transaction to process the data D1 through the off-chain device 12. The communication process between the above-mentioned blockchain and the off-chain device will be described in detail below.

[0077] Figure 2 A flow chart of a communication method between a blockchain 11 and an off-chain device 12 according to an embodiment of the present specification is shown.

[0078] like Figure 2As shown in , the method is interactively communicated by the blockchain 11 and the off-chain device 12, thereby realizing the session between the blockchain 11 and the off-chain device 12. When a node in the blockchain 11 wishes to send data to the off-chain device 12, the node in the blockchain 11 can store the data it wishes to send in the form of provable data (such as transaction receipts, account data, etc.) in the blockchain by sending a transaction, so that the off-chain device 12 can receive the data by reading the receipt in the blockchain. When the off-chain device 12 wishes to send data to the chain, the off-chain device 12 can send a transaction to the blockchain through the blockchain client therein, thereby providing data to the blockchain through the transaction. In one embodiment, the blockchain 11 can communicate with the off-chain device 12 by executing a transaction that calls the communication contract C2, which will be described as an example below. It can be understood that the communication method provided in the embodiment of this specification is not limited to being performed by executing a transaction that calls the communication contract C2, but can also be performed by executing a transaction including a data field, as long as the message to be sent and the recipient or sender of the message can be clearly specified through the data field of the transaction.

[0079] First, in step S202, the blockchain 11 sends a pre-opening message to the off-chain device 12.

[0080] As described above, the account status of the communication contract C2 records the session status between the communication contract C2 and the off-chain device. For example, initially, the account status of the communication contract C2 records that the status of the session 1 with the off-chain device 12 is closed, or the account status of the communication contract C2 does not record any session with the off-chain device 12, that is, the session status of the communication contract C2 and the off-chain device is closed. When the node 1 in the blockchain 11 wants to process data by calling the communication contract C2, it can first query the account status of the communication contract C2, so as to know that the communication contract C2 has not established a session with the off-chain device 12. Therefore, node 1 can send transaction 1, in which the request to open the session function in the communication contract C2 is called with the off-chain device 12 identifier as the input parameter in transaction 1, so as to send a pre-opening message to the off-chain device 12.

[0081] After reaching consensus on transaction 1, each node in the blockchain executes transaction 1 separately to obtain a consistent state. When any node in the blockchain executes transaction 1, it stores a receipt R1 of transaction 1 in the blockchain. The receipt R1 includes the account of the communication contract C2, the identifier of the off-chain device 12, and the pre-opening message, such as "OpenSyncMsg." In one embodiment, the receipt R1 may also include the identifier of session 1 (e.g., "session 1"). At the same time, the node in the blockchain also records the session status between the communication contract C2 and the off-chain device 12 in the account status of the communication contract C2, for example, using session 1 as the identifier of the session, and recording the status of session 1 as "pre-opening status." Table 1 schematically shows a table of session status recorded in the account status of the communication contract C2 in the blockchain node.

[0082] Table 1

[0083] Session ID Off-chain device identification Session State Session 1 Off-chain devices 12 Pre-open state Session 2 Off-chain devices16 Open state …

[0084] As shown in Table 1, multiple sessions may be recorded in the account status of the communication contract C2, such as session 1 and session 2. Different sessions are sessions with different off-chain devices and may have different session states. For example, session 1 is a session between the communication contract C2 and the off-chain device 12, and the state of session 1 is the pre-open state.

[0085] The off-chain device 12 can monitor the receipt including the account address of the communication contract C2 and the identification of the off-chain device 12 through its Spv client, or the off-chain device 12 can monitor the receipt including the "session 1 pair. Whenever it is determined that the receipt is stored in the blockchain 11, the receipt can be read from the blockchain 11, and the Spv certification data of the receipt can be read accordingly. Alternatively, the blockchain 11 can actively push information. Every time it is determined that the receipt generated by the execution of the communication contract C2 and sent to the off-chain device 12 is stored in the blockchain 11, the receipt and the corresponding Spv certification data are actively pushed to the off-chain device 12. Therefore, after the above-mentioned receipt R1 is stored in the blockchain 11, the off-chain device 12 can read the receipt R1 and its Spv certification from the blockchain 11, that is, receive the pre-opening message of the session 1 in the receipt R1.

[0086] It can be understood that although the above description describes providing the pre-opening message to the off-chain device 12 in the form of a receipt, the embodiments of this specification are not limited thereto. For example, the blockchain 11 can provide the pre-opening message to the off-chain device 12 through the status of the session 1 in the account status of the communication contract C2. The off-chain device 12 can obtain the message by reading the status of the session 1 in the blockchain 11.

[0087] Node 1 in blockchain 11 may query whether a transaction sent by off-chain device 12 for confirming the pre-opening message is included in a predetermined number of blocks after transaction 1 is formed into a block. If not, node 1 may Figure 2 As shown by the dotted line in the middle, a transaction is sent to call the session closing function in the communication contract C2. After the transaction is executed, the information of session 1 recorded in the account status of the communication contract C2 is deleted, or the status of session 1 is changed to a closed state. Optionally, after the transaction is executed, a session closing message is stored in the blockchain 11 so that the off-chain device 12 can obtain the information.

[0088] In step S204, the off-chain device 12 sends pre-opening confirmation information to the blockchain 11.

[0089] After reading receipt R1 and its Spv certificate, the off-chain device 12 verifies receipt R1 using the Spv certificate through the Spv client. After the verification is passed, the "OpenSyncMsg message" can be read from receipt R1. The off-chain device 12 can send transaction 2 to the blockchain 11 through the client within a predetermined period after reading receipt R1. In transaction 2, for example, the session start function in the communication contract C2 is called to send a pre-start confirmation message to the blockchain 11. After sending transaction 2, the off-chain device 12 opens a session with the communication contract C2 in the blockchain 11, and correspondingly opens a data retention period corresponding to the session. During the data retention period, the off-chain device 12 retains any communication data with the communication contract C2 in the period to ensure data security. Table 2 schematically shows a table of data retention periods for each session recorded in the off-chain device 12.

[0090] Table 2

[0091] Session ID Contract Address Data retention period Session 1 Communication contract C2 address Open Session 2 Contract C3 address closure …

[0092] As shown in Table 2, the off-chain device 12 can establish sessions with multiple contracts, such as Session 1 and Session 2, etc. In the off-chain device 12, the communication data in each session can be associated with "Session 1", so that when the data retention period of Session 1 is turned on according to Table 2, the communication data in Session 1 is retained in the off-chain device 12, and when it is determined that the data retention period of Session 2 is closed according to Table 2, the communication data in Session 2 can be deleted, and the record of Session 2 is deleted in Table 2.

[0093] In step S206, the blockchain 11 sends an opening message to the off-chain device 12.

[0094] When the nodes in the blockchain 11 execute transaction 2 after reaching a consensus on transaction 2, they first determine whether the state of session 1 is in the pre-open state. If the state of session 1 is in the pre-open state, the state of session 1 is modified to the open state, and a receipt R2 corresponding to transaction 2 is stored in the blockchain 11, which includes the account of the communication contract C2, the identifier of the off-chain device 12, and the opening message (such as OpenMsg). If the account state of the communication contract C2 does not include the information of session 1 (or the state of session 1 is closed), the execution of transaction 2 is terminated, that is, the above-mentioned operation of modifying the session state and storing receipt R2 is not performed.

[0095] After sending transaction 2, the off-chain device 12 determines whether the above-mentioned opening message sent by the blockchain 11 is read within the predetermined period. If the above-mentioned opening message is read within the predetermined period, it can be confirmed that the blockchain 11 has received the transaction 2 sent by it, and the above-mentioned data retention period (such as Figure 2 If the above-mentioned opening message is not read within the predetermined period, the above-mentioned data retention period is ended (as shown in the data retention period T1 in FIG. Figure 2 The predetermined period of time may be predetermined based on the transaction block time in the blockchain 11. In one embodiment, after executing transaction 2, the receipt R2 is not stored in the blockchain 11. After sending transaction 2, the off-chain device 12 may determine whether the to-be-processed data sent to it by the blockchain 11 is read within the predetermined period of time. If the to-be-processed data sent to it is read within the predetermined period of time, the data retention period is continued to be retained. If the to-be-processed data is not read within the predetermined period of time, the data retention period is ended.

[0096] In step S208, the blockchain 11 sends the data D1 to be processed to the off-chain device 12.

[0097] After node 1 executes transaction 2 to change the state of session 1 to the open state, node 1 can start data processing through the off-chain device 12. For example, node 1 sends transaction 3, in which the data sending function in the communication contract C2 is called with data D1 and the identifier of the off-chain device 12 as parameters. After reaching a consensus on transaction 3, the nodes in the blockchain execute transaction 3, and after executing transaction 3, the receipt 3 of transaction 3 is stored. The receipt 3 includes the identifier of the communication contract C2, the identifier of the off-chain device 12, and the data D1. Similar to the above-mentioned reading of receipt R1, the off-chain device 12 can read receipt 3 and its Spv certificate from the blockchain 11 through the client, verify receipt 3, and obtain data D1 and the identifier of the communication contract C2 from receipt 3.

[0098] In step S210, the off-chain device 12 sends the processing result of the data D1 to the blockchain 11.

[0099] The off-chain device 12 is, for example, a TEE device, which has a preset data processing program and has its hardware private key and public key. After obtaining the identification of the data D1 and the communication contract C2, the off-chain device 12 uses the preset data processing program to process the data D1, thereby obtaining the processing result of the data D1 (processing result E1), and uses its hardware private key to digitally sign the processing result E1. Afterwards, the off-chain device 12 sends transaction 4 to the blockchain 11, in which the result return function in the communication contract C2 is called with the processing result E1 and the TEE signature as input parameters. The result return function is preset with the hardware public key of the off-chain device 12. When the node 1 in the blockchain 11 executes transaction 4, the hardware public key is used to verify the TEE signature. After the verification is passed, the processing result E1 of the data D1 can be obtained. When Figure 1 In the case where contract C1 is called in transaction 3 and communication contract C2 is called in contract C1, the result return function in communication contract C2 is called in transaction 4, and contract C1 is called with processing result E1 as the input parameter in the function. Thus, when node 1 executes transaction 4, after obtaining processing result E1 through communication contract C2, contract C1 is executed with processing result E1 as a parameter, thereby obtaining the final execution result of contract C1.

[0100] When other nodes in the blockchain 11 (e.g., node 2) wish to call the communication contract C2, similarly to node 1, they first query the session status between the communication contract C2 and the off-chain device 12 in the account status of the communication contract C2. When it is found that the status of the session 1 between the communication contract C2 and the off-chain device 12 is open, node 2 can send data to the off-chain device 12 similarly to node 1 (e.g., Figure 2 ) and receives the processing result E2 of the data D2.

[0101] In step S212, the off-chain device 12 sends pre-closing information to the blockchain 11.

[0102] The off-chain device 12 sends a pre-closing message for session 1 to the blockchain when a predetermined condition is met. The predetermined condition is, for example, any of the following: the off-chain device 12 does not receive the data to be processed for a predetermined length of time, the blockchain 11 stores a message for requesting the off-chain device 12 to close session 1 in the blockchain 11 by executing the communication contract C2, the off-chain device 12 completes processing of a predetermined amount of data, and so on.

[0103] Specifically, the off-chain device 12 sends transaction 5 to the blockchain 11 through the client, and calls the closing session function of the communication contract C2 in transaction 5 to send a pre-closing message to the blockchain 11.

[0104] In step S214, the blockchain 11 sends a close message to the off-chain device 12.

[0105] When executing transaction 5, the node in blockchain 11 determines whether all messages or data sent by off-chain device 12 are received; if it is determined that all messages or data sent by off-chain device 12 are received, the status of session 1 in the account status in communication contract C2 is deleted (i.e., session 1 is closed), or the status of session 1 is modified to a closed state, the execution of transaction 5 is terminated, and receipt 5 is stored in blockchain 11, which includes the account of communication contract C2, the identifier of off-chain device 12 and the session closing message (for example, "CloseMsg".

[0106] Among them, when executing transaction 5, according to the pre-settings between the communication contract C2 and the off-chain device 12, different methods can be used to determine whether all messages or data sent by the off-chain device 12 are received.

[0107] In one embodiment, the communication contract C2 may number the data sent each time, for example, Figure 2 As shown in , the data to be sent are numbered as data D1 and data D2, and the off-chain device 12 also numbers the data results sent back to the blockchain accordingly, for example, the processing result of data D1 is numbered as processing result E1, and the processing result of data D2 is numbered as processing result E2. The blockchain 11 can determine which data the processing result corresponds to based on the number of the received processing result. When the off-chain device 12 sends a pre-close message, the number of the last processing result sent by it in transaction 5 (for example, "10") can be used as a parameter to call the request to close the session function in the communication contract C2, so that when transaction 5 is executed in the blockchain 11, it is determined whether the processing results E1-processing results E10 have been received, thereby determining whether all messages or data sent by the off-chain device 12 have been received. In this embodiment, after the off-chain device 12 sends the pre-closing message through transaction 5, it is possible that the blockchain 11 has sent the data D11, data D12, etc. to be processed to the off-chain device 12. When executing transaction 5, these data numbers can also be recorded in the status of session 1 of the communication contract C2 for user query or processing when the next session is opened.

[0108] In another embodiment, for example, the communication contract C2 and the off-chain device 12 are pre-set: the off-chain device 12 closes the session after processing a predetermined number (e.g., 20) of data. Thus, when executing transaction 5 in the blockchain 11, it can be determined whether 20 processing results have been received, thereby determining whether all messages or data sent by the off-chain device 12 have been received.

[0109] In one embodiment, for example, a transaction of calling contract C1 is executed in the blockchain to process data, wherein communication contract C2 is called in contract C1. Off-chain device 12 returns the processing of data by calling back the transaction of communication contract C2, wherein communication contract C2 returns the returned processing result to contract C1 by calling back contract C1, and contract C1 stores the processing result in the account status of contract C1 after obtaining the processing result. Thus, the blockchain node can determine whether all messages or data sent by off-chain device 12 are received by querying the account status of contract C1.

[0110] After reading receipt 5 and its Spv certificate from blockchain 11, off-chain device 12 verifies receipt 5 and obtains the account of communication contract C2 and "CloseMsg" from receipt 5, thereby ending the data retention period T1 corresponding to communication contract C2, that is, deleting the communication data in session 1, thereby ending session 1.

[0111] When executing transaction 5, if it is determined that part of the message sent by the off-chain device 12 has not been received, a receipt 5 is stored in the blockchain, which includes the account of the communication contract C2, the identifier of the off-chain device 12, and the identifier of the part of the message that has not been received (for example, the message number), and the state of session 1 is modified to the pre-closed state. After reading the receipt 5, the off-chain device 12 sends transaction 6, in which the close session function of the communication contract C2 is called with the part of the message as the input parameter. After receiving and executing transaction 6, the blockchain 11 deletes the information of session 1 in the account state of the communication contract C2 (i.e., closes session 1) or modifies the state of session 1 to the closed state, and stores receipt 6 in the blockchain 11, which includes the account of the communication contract C2, the identifier of the off-chain device 12, and the session closing message (for example, "CloseMsgClosed".

[0112] It can be understood that although the communication scheme provided by the embodiment of this specification is described above by taking the communication contract C2 as an example, the embodiment of this specification is not limited thereto. For example, the transaction including the data field can be sent on the blockchain 11 side. Figure 2 The communication process shown in the figure, wherein each time a message is sent, a session identifier (e.g., "session 1 example") and message content (e.g., pre-open message, pre-open confirmation message, open message, pending data, pre-close message, close message, etc.) are included in the data field of the transaction, thereby recording the session status and data in each transaction. Preferably, the blockchain node can also create a session status database locally to record the status of each session similarly to the contract account status, thereby facilitating the query of the session status.

[0113] In addition, in the above description, when the node of blockchain 11 needs to determine the session status between blockchain 11 and off-chain device 12, it is determined by querying the session status in the account status of communication contract C2. It can be understood that the embodiment of the present application is not limited to this. The node in blockchain 11 can also determine the status of session 1 by querying the receipts of each transaction related to session 1 in the ledger data of the blockchain. For example, when the receipt of transaction 2 is queried in the ledger data, but the receipt of the pre-close message sent by the off-chain device 12 is not queried, it can be determined that the status of session 1 is open. Similarly, other states of session 1 can be determined based on the queried receipts of other transactions related to session 1. That is, in the solution of the embodiment of the present application, it is not necessary to record the status of session 1 in the account status of communication contract C2.

[0114] Figure 3 According to an embodiment of the present specification, a communication device 300 for a blockchain and an off-chain device is deployed on a blockchain node, including:

[0115] A first execution unit 31 is configured to execute a first transaction to provide a pre-opening message of a first session to an off-chain device, wherein the first session is a session between the blockchain and the off-chain device;

[0116] The second execution unit 32 is configured to execute a second transaction sent by the off-chain device to provide the off-chain device with a start message of the first session, wherein the second transaction includes confirmation information of the pre-start message.

[0117] In one embodiment, the first execution unit 31 is further configured to execute a first transaction to store a pre-opening message of the first session in the blockchain.

[0118] In one embodiment, the first execution unit 31 is further configured to execute a first transaction to record the state of the first session as a pre-opening state in the blockchain.

[0119] In one embodiment, the first contract is called in the first transaction, wherein the first execution unit 31 is further configured to record the state of the first session as a pre-opening state in the account state of the first contract.

[0120] In one embodiment, the first contract is called in the second transaction, and the second execution unit 32 is further configured to execute the first contract called in the second transaction, so as to determine whether the second transaction is executed within a first predetermined time period, and in the case where it is determined that the second transaction is executed within the first predetermined time period, provide a start message of the first session to the off-chain device.

[0121] In one embodiment, the first execution unit 31 is further configured to end the first session if it is determined that the second transaction is to be executed after a first predetermined period of time.

[0122] In one implementation, the second execution unit 32 is further configured to modify the state of the first session to an open state.

[0123] In one implementation, the second execution unit 32 is further configured to store a start message of the first session in the blockchain.

[0124] In one embodiment, the apparatus 300 further includes a third execution unit 33 configured to, after providing a start message of the first session to the off-chain device, execute a third transaction that calls the first contract, wherein the third transaction includes first data to be processed, so as to determine whether the state of the first session is an open state, and when it is determined that the state of the first session is an open state, store the first data in association with the first session in the blockchain.

[0125] In one embodiment, the device 300 further includes:

[0126] The fourth execution unit 34 is configured to execute a fourth transaction sent by the off-chain device, wherein the fourth transaction includes pre-closing information.

[0127] So as to determine whether all messages sent by the off-chain device are received;

[0128] In a case where it is determined that all messages sent by the off-chain device are received, a closing message of the first session is provided to the off-chain device.

[0129] In one implementation, the fourth execution unit 34 is further configured to modify the state of the first session to a closed state.

[0130] In one embodiment, the fourth execution unit 34 is also configured to, in the event that it is determined that at least part of the message sent by the off-chain device has not been received, store request information in association with the first session in the blockchain, wherein the request information is used to request the off-chain device to resend at least part of the message.

[0131] In one implementation, the fourth execution unit 34 is further configured to, in a case where it is determined that at least part of the message sent by the off-link device is not received, modify the state of the first session to a pre-closing state.

[0132] Figure 4 A communication device 400 for a blockchain and an off-chain device according to an embodiment of the present specification is shown, wherein the device is deployed on an off-chain device and includes:

[0133] A first acquisition unit 401 is configured to acquire a pre-opening message of a first session from a blockchain, where the first session is a session between the blockchain and the off-chain device;

[0134] A first sending unit 402 is configured to send a second transaction to the blockchain, wherein the second transaction includes confirmation information of the pre-opening message;

[0135] The starting unit 403 is configured to start the data retention period of the first session.

[0136] In one implementation, the first sending unit 402 is further configured to send a second transaction to the blockchain within a first predetermined time period.

[0137] In one embodiment, the device 400 also includes a determination unit 404 configured to determine whether to obtain a start message of the first session from the blockchain within a second predetermined period after the data retention period is started, and a first ending unit 405 configured to end the data retention period when the start message of the first session is not obtained from the blockchain within the second predetermined period.

[0138] In one implementation, the device 400 further includes a second sending unit 406 configured to send a fourth transaction to the blockchain when a preset condition is met, wherein the fourth transaction includes pre-closing information of the first session.

[0139] In one implementation, the device 400 further includes a second acquisition unit 407 configured to, after sending a fourth transaction to the blockchain, acquire a closing message of the first session from the blockchain; and a second ending unit 408 configured to end the data retention period.

[0140] In one embodiment, the device also includes a third acquisition unit 409, configured to, after sending a fourth transaction to the blockchain, obtain a request message for at least part of the messages in the first session that have been sent from the blockchain; a third sending unit 410, configured to send a fifth transaction to the blockchain, wherein the fifth transaction includes at least part of the message.

[0141] On the other hand, the present specification provides a computer-readable storage medium having a computer program stored thereon, which, when executed in a computer, causes the computer to execute any one of the above methods.

[0142] Another aspect of the present specification provides a computing device, including a memory and a processor, wherein the memory stores a computer program, and the processor implements any one of the above methods when executing the computer program.

[0143] Through the communication scheme between the blockchain and the off-chain device according to the embodiment of this specification, the session can be opened after the blockchain and the off-chain device have both confirmed the channel, and the off-chain device guarantees the session data retention period during the session opening, thereby ensuring secure two-way communication between the blockchain and the off-chain device. In addition, in the embodiment of this specification, by using the smart contract in the blockchain to conduct a session with the off-chain device, and recording the status of the session in the account status of the smart contract, it is convenient for each node to query the session status, and communication with the off-chain device can be carried out based on the session status.

[0144] Each embodiment in this specification is described in a progressive manner, and the same or similar parts between the embodiments can be referred to each other, and each embodiment focuses on the differences from other embodiments. In particular, for the system embodiment, since it is basically similar to the method embodiment, the description is relatively simple, and the relevant parts can be referred to the partial description of the method embodiment.

[0145] The above is a description of a specific embodiment of the specification. Other embodiments are within the scope of the appended claims. In some cases, the actions or steps recorded in the claims can be performed in an order different from that in the embodiments and still achieve the desired results. In addition, the processes depicted in the drawings do not necessarily require the specific order or continuous order shown to achieve the desired results. In some embodiments, multitasking and parallel processing are also possible or may be advantageous.

[0146] Those of ordinary skill in the art should also be further aware that the units and algorithm steps of each example described in conjunction with the embodiments disclosed herein can be implemented with electronic hardware, computer software or a combination of the two. In order to clearly illustrate the interchangeability of hardware and software, the composition and steps of each example have been generally described in the above description according to the function. Whether these functions are performed in hardware or software depends on the specific application and design constraints of the technical solution. Those of ordinary skill in the art can use different methods to implement the described functions for each specific application, but this implementation should not be considered to be beyond the scope of the present application. Among them, the software module can be placed in a random access memory (RAM), a memory, a read-only memory (ROM), an electrically programmable ROM, an electrically erasable programmable ROM, a register, a hard disk, a removable disk, a CD-ROM, or any other form of storage medium known in the technical field.

[0147] The specific implementation methods described above further illustrate the objectives, technical solutions and beneficial effects of the present invention in detail. It should be understood that the above description is only a specific implementation method of the present invention and is not intended to limit the scope of protection of the present invention. Any modifications, equivalent substitutions, improvements, etc. made within the spirit and principles of the present invention should be included in the scope of protection of the present invention.

Claims

1. A method for communication between a blockchain and an off-chain device, the method being executed by a blockchain node, comprising: Executing a first transaction to provide a pre-opening message of a first session to an off-chain device, wherein the first session is a session between the blockchain and the off-chain device; A second transaction sent by the off-chain device is executed to provide the off-chain device with a start message of the first session, wherein the second transaction includes confirmation information of the pre-start message.

2. The method according to claim 1, wherein: Executing the first transaction also includes executing the first transaction to store a pre-opening message of the first session in the blockchain.

3. The method according to claim 1 or 2, executing the first transaction also includes executing the first transaction to record the state of the first session as a pre-opening state in the blockchain.

4. The method according to claim 3, wherein: The first contract is called in the first transaction, wherein recording the state of the first session as a pre-opening state in the blockchain includes recording the state of the first session as a pre-opening state in the account state of the first contract.

5. The method according to claim 4, wherein: The first contract is called in the second transaction, and the execution of the second transaction sent by the off-chain device includes executing the first contract called in the second transaction, so as to determine whether the second transaction is executed within a first predetermined time period, and in the case where it is determined that the second transaction is executed within the first predetermined time period, providing a start message of the first session to the off-chain device.

6. The method according to claim 5, wherein: Executing the first contract invoked in the second transaction further causes, in an event that the second transaction is determined to be executed after a first predetermined period of time, the first session to be ended.

7. The method according to any one of claims 1-2, 4-6, wherein: Providing the start message of the first session to the off-chain device includes modifying the state of the first session to an open state.

8. The method according to claim 7, wherein: Providing the start message of the first session to the off-chain device includes storing the start message of the first session in the blockchain.

9. The method according to claim 8 further includes, after providing the start message of the first session to the off-chain device, executing a third transaction to call the first contract, wherein the third transaction includes the first data to be processed, so as to determine whether the state of the first session is an open state, and in the case where it is determined that the state of the first session is an open state, storing the first data in association with the first session in the blockchain.

10. The method according to claim 1, further comprising: executing a fourth transaction sent by the off-chain device, wherein the fourth transaction includes pre-closing information, So as to determine whether all messages sent by the off-chain device are received; In a case where it is determined that all messages sent by the off-chain device are received, a closing message of the first session is provided to the off-chain device.

11. The method according to claim 10, wherein: Providing a closing message of the first session to the off-chain device includes modifying a state of the first session to a closed state.

12. The method according to claim 10, wherein: Executing the fourth transaction also enables, in a case where it is determined that at least part of the message sent by the off-chain device has not been received, request information to be stored in association with the first session in the blockchain, the request information being used to request the off-chain device to resend at least part of the message.

13. The method according to claim 12, wherein: Executing the fourth transaction further causes, in a case where it is determined that a message at least partially sent by the off-chain device is not received, the state of the first session to be modified to a pre-closed state.

14. A communication device for a blockchain and an off-chain device, the device being deployed on a blockchain node, comprising: A first execution unit is configured to execute a first transaction to provide a pre-opening message of a first session to an off-chain device, wherein the first session is a session between the blockchain and the off-chain device; The second execution unit is configured to execute a second transaction sent by the off-chain device to provide the off-chain device with an opening message of the first session, wherein the second transaction includes confirmation information of the pre-opening message.

15. The device according to claim 14, wherein: The first execution unit is further configured to execute a first transaction to store a pre-opening message of the first session in the blockchain.

16. The device according to claim 14 or 15, wherein the first execution unit is further configured to execute a first transaction to record the state of the first session as a pre-opening state in the blockchain.

17. The device according to claim 16, wherein: The first contract is called in the first transaction, wherein the first execution unit is further configured to record the state of the first session as a pre-opening state in the account state of the first contract.

18. The device according to claim 17, wherein: The first contract is called in the second transaction, and the second execution unit is further configured to execute the first contract called in the second transaction, so as to determine whether the second transaction is executed within a first predetermined time period, and in the case where it is determined that the second transaction is executed within the first predetermined time period, provide a start message of the first session to the off-chain device.

19. The device according to claim 18, wherein: The first execution unit is further configured to, in a case where it is determined that the second transaction is performed after a first predetermined period of time, end the first session.

20. The device according to any one of claims 14-15, 17-18, wherein: The second execution unit is further configured to modify the state of the first session to an open state.

21. The device according to claim 20, wherein: The second execution unit is further configured to store a start message of the first session in the blockchain.

22. The apparatus according to claim 21 further comprises a third execution unit configured to, after providing a start message of the first session to the off-chain device, execute a third transaction that calls the first contract, wherein the third transaction comprises first data to be processed, so as to determine whether the state of the first session is an open state, and in the case where it is determined that the state of the first session is an open state, store the first data in association with the first session in the blockchain.

23. The apparatus of claim 14, further comprising: a fourth execution unit, configured to execute a fourth transaction sent by the off-chain device, wherein the fourth transaction includes pre-closing information; So as to determine whether all messages sent by the off-chain device are received; In a case where it is determined that all messages sent by the off-chain device are received, a closing message of the first session is provided to the off-chain device.

24. The device according to claim 23, wherein: The fourth execution unit is further configured to modify the state of the first session to a closed state.

25. The device according to claim 23, wherein: The fourth execution unit is also configured to, in the event that it is determined that at least part of the message sent by the off-chain device has not been received, store request information in association with the first session in the blockchain, wherein the request information is used to request the off-chain device to resend at least part of the message.

26. The device according to claim 25, wherein The fourth execution unit is further configured to, in a case where it is determined that at least part of the message sent by the off-chain device is not received, modify the state of the first session to a pre-closing state.

27. A computer-readable storage medium having a computer program or instruction stored thereon, which, when executed in a computer, causes the computer to execute the method according to any one of claims 1 to 13.

28. A computing device comprising a memory and a processor, wherein the memory stores a computer program or instructions, and the processor implements the method according to any one of claims 1 to 13 when executing the computer program or instructions.