|
| 1 | +--- |
| 2 | +title: "ChainExchange" |
| 3 | +weight: 5 |
| 4 | +dashboardWeight: 1 |
| 5 | +dashboardState: stable |
| 6 | +dashboardAudit: missing |
| 7 | +dashboardTests: 0 |
| 8 | +--- |
| 9 | + |
| 10 | +# ChainExchange |
| 11 | + |
| 12 | +{{< hint info >}} |
| 13 | +**Name**: Chain Exchange |
| 14 | +**Protocol ID**: `/fil/chain/xchg/0.0.1` |
| 15 | +{{< /hint >}} |
| 16 | + |
| 17 | +ChainExchange is a simple request/response protocol that allows Filecoin nodes to request ranges of Tipsets and/or Messages from each other. |
| 18 | + |
| 19 | +The `Request` message requests a chain segment of a given length by the hash of the highest Tipset in the segment (not necessarily heaviest Tipset of the current chain). For example, if the current height is at 5000, but a node is missing Tipsets between 4500-4700, then the `Request.Head` requested is 4700 and `Request.Length` is 200. |
| 20 | + |
| 21 | +The `Options` allow the requester to specify whether they want to receive block headers of the Tipsets only, the transaction messages included in every block, or both. |
| 22 | + |
| 23 | +The `Response` contains the requested chain segment in reverse iteration order. Each item in the `Chain` array contains either the block headers for that Tipset if the `Blocks` option bit in the request is set, or the messages across all blocks in that Tipset, if the `Messages` bit is set, or both, if both option bits are set. |
| 24 | + |
| 25 | +Each `CompactedMessages` structure contains the BLS and `secp256k1` messages for the corresponding Tipset in unified arrays that encode each message once regardless of how many times it appears in the Tipset's blocks (to reduce the `Response` size). The mapping between message and the blocks into which they are included is encoded in the `BlsIncludes` and `SecpkIncludes` arrays. These arrays are indexed by the block index in the Tipset and contain in that position an array of message indexes that belong to that block. The messages themselves are in the `Bls` and `Secpk` arrays. |
| 26 | + |
| 27 | +If not all Tipsets requested could be fetched, but the `Head` of the chain segment requested was successfully fetched (and potentially more contiguous Tipsets), then this is not considered an error and a `Partial` response code is returned. The node can continue extending the chain from the partial returned segment onwards. |
| 28 | + |
| 29 | +```go |
| 30 | +type Request struct { |
| 31 | + ## Head of the requested segment (block CIDs comprising the entire Tipset) |
| 32 | + Head [Cid] |
| 33 | + ## Length of the requested segment |
| 34 | + Length UInt |
| 35 | + ## Query options |
| 36 | + Options UInt |
| 37 | +} |
| 38 | +``` |
| 39 | + |
| 40 | +```go |
| 41 | +type Options enum { |
| 42 | + # Include block headers |
| 43 | + | Headers 1 |
| 44 | + # Include block messages |
| 45 | + | Messages 2 |
| 46 | +} |
| 47 | + |
| 48 | +type Response struct { |
| 49 | + ## Response Status |
| 50 | + Status status |
| 51 | + ## Optional error message |
| 52 | + ErrorMessage string |
| 53 | + ## Returned segment containing block messages and/or headers |
| 54 | + Chain []*BSTipSet |
| 55 | +} |
| 56 | + |
| 57 | +type Status enum { |
| 58 | + ## Success: the entire segment requested was fetched. |
| 59 | + | Ok 0 |
| 60 | + ## We could not fetch all Tipsets requested but at least we returned |
| 61 | + ## the `Head` requested (and potentially more contiguous Tipsets). |
| 62 | + ## Not considered an error. |
| 63 | + | Partial 101 |
| 64 | + ## `Request.Head` not found. |
| 65 | + | NotFound 201 |
| 66 | + ## Requester is making too many requests. |
| 67 | + | GoAway 202 |
| 68 | + ## Internal error occurred. |
| 69 | + | InternalError 203 |
| 70 | + ## Request was badly formed. |
| 71 | + | BadRequest 204 |
| 72 | +} |
| 73 | + |
| 74 | +type CompactedMessages struct { |
| 75 | + ## Array of BLS messages in this tipset. |
| 76 | + Bls [Message] |
| 77 | + ## Array of messages indexes present in each block: |
| 78 | + ## `BlsIncludes[BI] -> [MI]` |
| 79 | + ## * BI: block index in the tipset. |
| 80 | + ## * MI: message index in `Bls` list. |
| 81 | + BlsIncludes [[Uint]] |
| 82 | + |
| 83 | + ## Array of `secp256k1` messages. |
| 84 | + Secpk [SignedMessage] |
| 85 | + ## Inclusion array, see `BlsIncludes`. |
| 86 | + SecpkIncludes [[UInt]] |
| 87 | +} |
| 88 | +``` |
| 89 | + |
| 90 | +## Example |
| 91 | + |
| 92 | +For the set of arrays in the following `BSTipSet`, the corresponding messages per block are as shown below: |
| 93 | + |
| 94 | +**BSTipSet** |
| 95 | +```js |
| 96 | +Blocks: [b0, b1] |
| 97 | +CompactedMessages |
| 98 | +``` |
| 99 | + |
| 100 | +**CompactedMessages** |
| 101 | +```js |
| 102 | +Secpk: [mA, mB, mC, mD] |
| 103 | +SecpkIncludes: [[0, 1, 3], [1, 2, 0]] |
| 104 | +``` |
| 105 | + |
| 106 | +**Messages corresponding to each Block** |
| 107 | +```js |
| 108 | +Block 'b0': [mA, mB, mD] |
| 109 | +Block 'b1': [mB, mC, mA] |
| 110 | +``` |
| 111 | + |
| 112 | +In other words, the first element of the `SecpkIncludes` array: `[0, 1, 3]` points to the 1st, 2nd and 4th element of the `Secpk` array: `mA, mB, mD`, which correspond to the 1st element of the `Blocks` array `b0`. Hence, `Block 'b0': [mA, mB, mD]`. |
| 113 | + |
| 114 | +Similarly, the second element of the `SecpkIncludes` array: `[1, 2, 0]` points to the 2nd, 3rd and 1st element of the `Secpk` array: `mB, mC, mA`, which correspond to the 2nd element of the `Blocks` array `b1`. Hence, `Block 'b1': [mB, mC, mA]`. |
| 115 | + |
| 116 | +See [Lotus](https://github.com/filecoin-project/lotus/tree/master/chain/exchange) for an example implementation of the protocol. |
0 commit comments