From d2423c6f9a85d49efd2ec951672c2ee7b81d7c47 Mon Sep 17 00:00:00 2001 From: trueptolemy <823220586@qq.com> Date: Sun, 16 Jun 2019 02:23:03 +0800 Subject: [PATCH] pytest: Add a simple plugin to test 'forward_event' --- tests/plugins/forward_payment_status.py | 54 +++++++++++++++++++++++++ 1 file changed, 54 insertions(+) create mode 100755 tests/plugins/forward_payment_status.py diff --git a/tests/plugins/forward_payment_status.py b/tests/plugins/forward_payment_status.py new file mode 100755 index 000000000..040cb321a --- /dev/null +++ b/tests/plugins/forward_payment_status.py @@ -0,0 +1,54 @@ +#!/usr/bin/env python3 +"""This plugin is used to check that forward_event calls are working correctly. +""" +from lightning import Plugin + +plugin = Plugin() + + +def check(forward, dbforward): + # After finding the corresponding notification record, this function will + # make some changes on mutative fields of this record to make this record + # same as the ideal format with given status. + record = forward + if record['status'] == 'offered': + if dbforward['status'] == 'local_failed': + record['failcode'] = dbforward['failcode'] + record['failreason'] = dbforward['failreason'] + elif dbforward['status'] != 'offered': + record['resolved_time'] = dbforward['resolved_time'] + record['status'] = dbforward['status'] + if record == dbforward: + return True + else: + return False + + +@plugin.init() +def init(configuration, options, plugin): + plugin.forward_list = [] + + +@plugin.subscribe("forward_event") +def notify_warning(plugin, forward_event): + # One forward payment may have many notification records for different status, + # but one forward payment has only one record in 'listforwards' eventrually. + plugin.log("receive a forward recored, status: {}, payment_hash: {}".format(forward_event['status'], forward_event['payment_hash'])) + plugin.forward_list.append(forward_event) + + +@plugin.method('recordcheck') +def record_lookup(payment_hash, status, dbforward, plugin): + # Check if we received all notifications when forward changed. + # This check is based on the records of 'listforwards' + plugin.log("recordcheck: payment_hash: {}, status: {}".format(payment_hash, status)) + for forward in plugin.forward_list: + if forward['payment_hash'] == payment_hash and forward['status'] == status: + plugin.log("record exists") + check_result = check(forward, dbforward) + return check_result + plugin.log("no record") + return False + + +plugin.run()