Check that hooks are called in order and the chain exits correctly We start two nodes, l2 will have two plugins registering the same hook (`htlc_accepted`) but handle different cases: - the `odd` plugin only handles the "AA"*32 preimage - the `even` plugin only handles the "BB"*32
(node_factory)
| 2043 | |
| 2044 | |
| 2045 | def test_hook_chaining(node_factory): |
| 2046 | """Check that hooks are called in order and the chain exits correctly |
| 2047 | |
| 2048 | We start two nodes, l2 will have two plugins registering the same hook |
| 2049 | (`htlc_accepted`) but handle different cases: |
| 2050 | |
| 2051 | - the `odd` plugin only handles the "AA"*32 preimage |
| 2052 | - the `even` plugin only handles the "BB"*32 preimage |
| 2053 | |
| 2054 | We check that plugins are called in the order they are registering the |
| 2055 | hook, and that they exit the call chain as soon as one plugin returns a |
| 2056 | result that isn't `continue`. On exiting the chain the remaining plugins |
| 2057 | are not called. If no plugin exits the chain we continue to handle |
| 2058 | internally as usual. |
| 2059 | |
| 2060 | """ |
| 2061 | l1, l2 = node_factory.line_graph(2) |
| 2062 | |
| 2063 | # Start the plugins manually instead of specifying them on the command |
| 2064 | # line, otherwise we cannot guarantee the order in which the hooks are |
| 2065 | # registered. |
| 2066 | p1 = os.path.join(os.path.dirname(__file__), "plugins/hook-chain-odd.py") |
| 2067 | p2 = os.path.join(os.path.dirname(__file__), "plugins/hook-chain-even.py") |
| 2068 | l2.rpc.plugin_start(p1) |
| 2069 | l2.rpc.plugin_start(p2) |
| 2070 | |
| 2071 | preimage1 = b'\xAA' * 32 |
| 2072 | preimage2 = b'\xBB' * 32 |
| 2073 | preimage3 = b'\xCC' * 32 |
| 2074 | hash1 = sha256(preimage1).hexdigest() |
| 2075 | hash2 = sha256(preimage2).hexdigest() |
| 2076 | hash3 = sha256(preimage3).hexdigest() |
| 2077 | |
| 2078 | inv = l2.rpc.invoice(123, 'odd', "Odd payment handled by the first plugin", |
| 2079 | preimage="AA" * 32)['bolt11'] |
| 2080 | l1.rpc.xpay(inv) |
| 2081 | |
| 2082 | # The first plugin will handle this, the second one should not be called. |
| 2083 | assert(l2.daemon.is_in_log( |
| 2084 | r'plugin-hook-chain-odd.py: htlc_accepted called for payment_hash {}'.format(hash1) |
| 2085 | )) |
| 2086 | assert(not l2.daemon.is_in_log( |
| 2087 | r'plugin-hook-chain-even.py: htlc_accepted called for payment_hash {}'.format(hash1) |
| 2088 | )) |
| 2089 | |
| 2090 | # The second run is with a payment_hash that `hook-chain-even.py` knows |
| 2091 | # about. `hook-chain-odd.py` is called, it returns a `continue`, and then |
| 2092 | # `hook-chain-even.py` resolves it. |
| 2093 | inv = l2.rpc.invoice( |
| 2094 | 123, 'even', "Even payment handled by the second plugin", preimage="BB" * 32 |
| 2095 | )['bolt11'] |
| 2096 | l1.rpc.xpay(inv) |
| 2097 | assert(l2.daemon.is_in_log( |
| 2098 | r'plugin-hook-chain-odd.py: htlc_accepted called for payment_hash {}'.format(hash2) |
| 2099 | )) |
| 2100 | assert(l2.daemon.is_in_log( |
| 2101 | r'plugin-hook-chain-even.py: htlc_accepted called for payment_hash {}'.format(hash2) |
| 2102 | )) |
nothing calls this directly
no test coverage detected