|
| 1 | +# Predicate |
| 2 | + |
| 3 | +This package contains the predicate data structure and its encoding and helper functions to unpack/pack the data structure. |
| 4 | + |
| 5 | +## Encoding |
| 6 | + |
| 7 | +A byte slice of size N is encoded as: |
| 8 | + |
| 9 | +1. Slice of N bytes |
| 10 | +2. Delimiter byte `0xff` |
| 11 | +3. Appended 0s to the nearest multiple of 32 bytes |
| 12 | + |
| 13 | + |
| 14 | +## Results |
| 15 | + |
| 16 | +This defines how to encode `PredicateResults` within the block header's `Extra` data field. |
| 17 | + |
| 18 | +For more information on the motivation for encoding the results of predicate verification within a block, see [here](../../../vms/platformvm/warp/README.md#processing-historical-avalanche-interchain-messages). |
| 19 | + |
| 20 | +### Serialization |
| 21 | + |
| 22 | +Results have a maximum size of 1MB enforced by the codec. The actual size depends on how much data the Precompile predicates may put into the results, the gas cost they charge, and the block gas limit. PredicateResults are encoded using the AvalancheGo codec, which serializes a map by serializing the length of the map as a `uint32` and then serializes each key-value pair sequentially. |
| 23 | + |
| 24 | +PredicateResults: |
| 25 | + |
| 26 | +| Field | Type | Size | |
| 27 | +|-------|------|------| |
| 28 | +| codecID | uint16 | 2 bytes | |
| 29 | +| results | map[[32]byte]TxPredicateResults | 4 + size(results) bytes | |
| 30 | +| **Total** | | **6 + size(results)** | |
| 31 | + |
| 32 | +- `codecID` is the codec version used to serialize the payload and is hardcoded to `0x0000` |
| 33 | +- `results` is a map of transaction hashes to the corresponding `TxPredicateResults` |
| 34 | + |
| 35 | +TxPredicateResults |
| 36 | + |
| 37 | +| Field | Type | Size | |
| 38 | +|-------|------|------| |
| 39 | +| txPredicateResults | map[[20]byte][]byte | 4 + size(txPredicateResults) bytes | |
| 40 | + |
| 41 | +- `txPredicateResults` is a map of precompile addresses to the corresponding byte array returned by the predicate |
| 42 | + |
| 43 | +#### Examples |
| 44 | + |
| 45 | +##### Empty Predicate Results Map |
| 46 | + |
| 47 | +``` |
| 48 | +// codecID |
| 49 | +0x00, 0x00, |
| 50 | +// results length |
| 51 | +0x00, 0x00, 0x00, 0x00 |
| 52 | +``` |
| 53 | + |
| 54 | +##### Predicate Map with a Single Transaction Result |
| 55 | + |
| 56 | +``` |
| 57 | +// codecID |
| 58 | +0x00, 0x00, |
| 59 | +// Results length |
| 60 | +0x00, 0x00, 0x00, 0x01, |
| 61 | +// txHash (key in results map) |
| 62 | +0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 63 | +0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 64 | +0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 65 | +0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 66 | +// TxPredicateResults (value in results map) |
| 67 | +// TxPredicateResults length |
| 68 | +0x00, 0x00, 0x00, 0x01, |
| 69 | +// precompile address (key in TxPredicateResults map) |
| 70 | +0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 71 | +0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 72 | +0x00, 0x00, 0x00, 0x00, |
| 73 | +// Byte array results (value in TxPredicateResults map) |
| 74 | +// Length of bytes result |
| 75 | +0x00, 0x00, 0x00, 0x03, |
| 76 | +// bytes |
| 77 | +0x01, 0x02, 0x03 |
| 78 | +``` |
| 79 | + |
| 80 | +##### Predicate Map with Two Transaction Results |
| 81 | + |
| 82 | +``` |
| 83 | +// codecID |
| 84 | +0x00, 0x00, |
| 85 | +// Results length |
| 86 | +0x00, 0x00, 0x00, 0x02, |
| 87 | +// txHash (key in results map) |
| 88 | +0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 89 | +0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 90 | +0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 91 | +0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 92 | +// TxPredicateResults (value in results map) |
| 93 | +// TxPredicateResults length |
| 94 | +0x00, 0x00, 0x00, 0x01, |
| 95 | +// precompile address (key in TxPredicateResults map) |
| 96 | +0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 97 | +0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 98 | +0x00, 0x00, 0x00, 0x00, |
| 99 | +// Byte array results (value in TxPredicateResults map) |
| 100 | +// Length of bytes result |
| 101 | +0x00, 0x00, 0x00, 0x03, |
| 102 | +// bytes |
| 103 | +0x01, 0x02, 0x03 |
| 104 | +// txHash2 (key in results map) |
| 105 | +0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 106 | +0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 107 | +0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 108 | +0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 109 | +// TxPredicateResults (value in results map) |
| 110 | +// TxPredicateResults length |
| 111 | +0x00, 0x00, 0x00, 0x01, |
| 112 | +// precompile address (key in TxPredicateResults map) |
| 113 | +0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 114 | +0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 115 | +0x00, 0x00, 0x00, 0x00, |
| 116 | +// Byte array results (value in TxPredicateResults map) |
| 117 | +// Length of bytes result |
| 118 | +0x00, 0x00, 0x00, 0x03, |
| 119 | +// bytes |
| 120 | +0x01, 0x02, 0x03 |
| 121 | +``` |
0 commit comments