Skip to content

Commit c5718bb

Browse files
authored
Merge pull request #1554 from hyperledger/docs_v1.3.1_awr
Additional info and clarification in listener docs
2 parents 5cf2669 + 0a9a64c commit c5718bb

File tree

1 file changed

+66
-22
lines changed

1 file changed

+66
-22
lines changed

doc-site/docs/reference/types/_includes/contractlistener_description.md

Lines changed: 66 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -7,51 +7,95 @@ a walk-through of how to set up listeners for the events from your smart contrac
77

88
See below for a deep dive into the format of contract listeners and important concepts to understand when managing them.
99

10-
### Multiple filters
10+
### Event filters
1111

12-
From v1.3.1 onwards, a contract listener can be created with multiple filters under a single topic when supported by the connector.
12+
#### Multiple filters
1313

14-
Before this change, each contract listener would only support listening to one specific event from an interface previously defined. Each listener would be comprised of:
14+
From v1.3.1 onwards, a contract listener can be created with multiple filters under a single topic, when supported by the connector. Each filter contains:
1515

1616
- a reference to a specific blockchain event to listen for
1717
- (optional) a specific location/address to listen from
18-
- a connector-specific signature (generated from the event), which allows you to easily identify and search for the contact listener for an event
19-
- a `topic` which determines the ordered stream that these events are part of
18+
- a connector-specific signature (generated from the event and the location)
2019

21-
This format is still supported by the API. However, it may not fully guarantee accurate ordering of events coming from multiple listeners, even if they share the same `topic` (such as during cases of blockchain catch-up, or when one listener is created much later than the others).
20+
In addition to this list of multiple filters, the listener specifies a single `topic` to identify the stream of events.
2221

23-
From v1.3.1, you can supply multiple filters on a single listener. Each filter contains:
22+
Creating a single listener that listens for multiple events will allow for the easiest management of listeners, and for strong ordering of the events that they process.
23+
24+
#### Single filter
25+
26+
Before v1.3.1, each contract listener would only support listening to one specific event from a contract interface. Each listener would be comprised of:
2427

2528
- a reference to a specific blockchain event to listen for
2629
- (optional) a specific location/address to listen from
27-
- a connector-specific signature (generated from the event and the location)
30+
- a connector-specific signature (generated from the event), which allows you to easily identify and search for the contact listener for an event
31+
- a `topic` which determines the ordered stream that these events are part of
32+
33+
For backwards compatibility, this format is still supported by the API.
34+
35+
### Signature strings
36+
37+
#### String format
38+
39+
Each filter is identified by a generated `signature` that matches a single event, and each contract listener is identified by a `signature` computed from its filters.
40+
41+
Ethereum provides a string standard for event signatures, of the form `EventName(uint256,bytes)`. Prior to v1.3.1, the signature of each Ethereum contract listener would exactly follow this Ethereum format.
42+
43+
As of v1.3.1, Ethereum signature strings have been changed, because this format does not fully describe the event - particularly because each top-level parameter can in the ABI definition be marked as `indexed`. For example, while the following two Solidity events have the same signature, they are serialized differently due to the different placement of `indexed` parameters, and thus a listener must define both individually to be able to process them:
44+
45+
- ERC-20 `Transfer`
46+
47+
```solidity
48+
event Transfer(address indexed _from, address indexed _to, uint256 _value)
49+
```
50+
51+
- ERC-721 `Transfer`
52+
53+
```solidity
54+
event Transfer(address indexed _from, address indexed _to, uint256 indexed _tokenId);
55+
```
56+
57+
The two above are now expressed in the following manner by the Ethereum blockchain connector:
58+
59+
```solidity
60+
Transfer(address,address,uint256) [i=0,1]
61+
Transfer(address,address,uint256) [i=0,1,2]
62+
```
63+
64+
The `[i=]` listing at the end of the signature indicates the position of all parameters that are marked as `indexed`.
65+
66+
Building on the blockchain-specific signature format for each event, FireFly will then compute the final signature for each filter and each contract listener as follows:
2867

29-
In addition to this list of multiple filters, the listener specifies a single `topic` to identify the stream of events. This new feature will allow better management of contract listeners and strong ordering of all of the events your application cares about.
68+
- Each filter signature is a combination of the location and the specific connector event signature, such as `0xa5ea5d0a6b2eaf194716f0cc73981939dca26da1:Changed(address,uint256) [i=0]`
69+
- Each contract listener signature is a concatenation of all the filter signatures, separated by `;`
3070

31-
Note: For backwards compatibility, the response from the API will populate top-level `event` and `location` fields with the contents of the first event filter in the array.
71+
#### Duplicate filters
3272

33-
### Duplicate filters
73+
FireFly restricts the creation of a contract listener containing duplicate filters.
3474

35-
FireFly will restrict the creation of a contract listener with duplicate filters or superset filters. For example, if two filters are listening to the same event, but one has specified a location and the other hasn't, then the latter will be a superset, and already be listening to all the events matching the first filter. Creation of duplicate or superset filters will be blocked.
75+
This includes the special case where one filter is a superset of another filter, due to a wildcard location.
3676

37-
### Duplicate listeners
77+
For example, if two filters are listening to the same event, but one has specified a location and the other hasn't, then the latter will be a superset, and already be listening to all the events matching the first filter. Creation of duplicate or superset filters within a single listener will be blocked.
3878

39-
As of v1.3.1, each filter on a listener includes a signature generated from the filter location + event, and the listener concatenates all of these signatures to build the overall contract listener signature. This contract listener signature - containing all the locations and event signatures combined with the listener topic - will guarantee uniqueness of the contract listener. If you tried to create the same listener again, you would receive HTTP 409. This combination can allow a developer to assert that their listener exists, without the risk of creating duplicates.
79+
#### Duplicate listeners
4080

41-
Note: Prior to v1.3.1, FireFly would detect duplicates simply by requiring a unique combination of signature + topic + location for each listener. When using muliple filters, we cannot rely on the signature of one event and the location of one filter to calculate uniqueness of a contract listener, hence the need for the more sophisticated uniqueness checks described here.
81+
As noted above, each listener has a generated signature. This signature - containing all the locations and event signatures combined with the listener topic - will guarantee uniqueness of the contract listener. If you tried to create the same listener again, you would receive HTTP 409. This combination can allow a developer to assert that their listener exists, without the risk of creating duplicates.
4282

43-
### Signature enhancements
83+
**Note:** Prior to v1.3.1, FireFly would detect duplicates simply by requiring a unique combination of signature + topic + location for each listener. The updated behavior for the listener signature is intended to preserve similar functionality, even when dealing with listeners that contain many event filters.
4484

45-
As mentioned above, we have introduced a new format for signatures of contract listener and filter signature:
85+
### Backwards compatibility
4686

47-
- Each filter signature will be a combination of the location and the specific connector event signature
48-
- Each contract listener signature will be a concatenation of all the filter signatures
87+
As noted throughout this document, the behavior of listeners is changed in v1.3.1. However, the following behaviors are retained for backwards-compatibility, to ensure that code written prior to v1.3.1 should continue to function.
4988

50-
Furthermore, because Ethereum ABI does not include a differentiation between indexed and non-indexed fields, we have included a new section in the Ethereum-specific event signature to add the index of the indexed fields. As such, a filter listening to the event `Changed(address indexed from, uint256 value)` at address `0xa5ea5d0a6b2eaf194716f0cc73981939dca26da1` will result in `0xa5ea5d0a6b2eaf194716f0cc73981939dca26da1:Changed(address,uint256) [i=0]` where `[i=0]` specifies that the first field is indexed. If there were more indexed fields, it will be a comma-separated list of the index of those indexed fields such as `[i=0,2,3]` (specifies that fields at index 0, 2, and 3 are indexed fields).
89+
- The response from all query APIs of `listeners` will continue to populate top-level `event` and `location` fields
90+
- The first entry from the `filters` array is duplicated to these fields
91+
- On input to create a new `listener`, the `event` and `location` fields are still supported
92+
- They function identically to supplying a `filters` array with a single entry
93+
- The `signature` field is preserved at the listener level
94+
- The format has been changed as described above
5195

52-
### Formats supported
96+
### Input formats
5397

54-
As described above, there are two input formats supported by the API for backwards compatibility with previous releases.
98+
The two input formats supported when creating a contract listener are shown below.
5599

56100
**Muliple Filters**
57101

0 commit comments

Comments
 (0)