1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162
|
# Copyright 2014-2022 Vincent Texier <vit@free.fr>
#
# DuniterPy is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# DuniterPy is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import unittest
import pypeg2
from duniterpy.grammars.output import CLTV, CSV, SIG, XHX, Condition, Operator
pubkey = "DNann1Lh55eZMEDXeYt59bzHbA3NJR46DeQYCS2qQdLV"
class TestOutputgrammar(unittest.TestCase):
def test_sig(self):
condition = "SIG(HgTTJLAQ5sqfknMq7yLPZbehtuLSsKj9CxWN7k8QvYJd)"
result = pypeg2.parse(condition, SIG)
self.assertEqual(result.pubkey, "HgTTJLAQ5sqfknMq7yLPZbehtuLSsKj9CxWN7k8QvYJd")
self.assertEqual(pypeg2.compose(result, Condition), condition)
def test_xhx(self):
condition = "XHX(309BC5E644F797F53E5A2065EAF38A173437F2E6)"
result = pypeg2.parse(condition, XHX)
self.assertEqual(result.sha_hash, "309BC5E644F797F53E5A2065EAF38A173437F2E6")
def test_sig_condition(self):
condition = "SIG(HgTTJLAQ5sqfknMq7yLPZbehtuLSsKj9CxWN7k8QvYJd)"
result = pypeg2.parse(condition, Condition)
self.assertEqual(
result.left.pubkey, "HgTTJLAQ5sqfknMq7yLPZbehtuLSsKj9CxWN7k8QvYJd"
)
self.assertEqual(pypeg2.compose(result, Condition), condition)
def test_xhr_condition(self):
condition = "SIG(HgTTJLAQ5sqfknMq7yLPZbehtuLSsKj9CxWN7k8QvYJd)"
result = pypeg2.parse(condition, Condition)
self.assertEqual(
result.left.pubkey, "HgTTJLAQ5sqfknMq7yLPZbehtuLSsKj9CxWN7k8QvYJd"
)
self.assertEqual(pypeg2.compose(result, Condition), condition)
def test_simple_condition(self):
condition = "(SIG(HgTTJLAQ5sqfknMq7yLPZbehtuLSsKj9CxWN7k8QvYJd))"
result = pypeg2.parse(condition, Condition)
self.assertEqual(
result.left.left.pubkey, "HgTTJLAQ5sqfknMq7yLPZbehtuLSsKj9CxWN7k8QvYJd"
)
self.assertEqual(pypeg2.compose(result, Condition), condition)
def test_simple_and_condition(self):
condition = (
"(SIG(HgTTJLAQ5sqfknMq7yLPZbehtuLSsKj9CxWN7k8QvYJd) && XHX("
"309BC5E644F797F53E5A2065EAF38A173437F2E6))"
)
result = pypeg2.parse(condition, Condition)
self.assertEqual(
result.left.left.pubkey, "HgTTJLAQ5sqfknMq7yLPZbehtuLSsKj9CxWN7k8QvYJd"
)
self.assertEqual(result.left.op.name, "&&")
self.assertEqual(
result.left.right.sha_hash, "309BC5E644F797F53E5A2065EAF38A173437F2E6"
)
self.assertEqual(pypeg2.compose(result, Condition), condition)
def test_simple_or_condition(self):
condition = (
"(SIG(HgTTJLAQ5sqfknMq7yLPZbehtuLSsKj9CxWN7k8QvYJd) || XHX("
"309BC5E644F797F53E5A2065EAF38A173437F2E6))"
)
result = pypeg2.parse(condition, Condition)
self.assertEqual(
result.left.left.pubkey, "HgTTJLAQ5sqfknMq7yLPZbehtuLSsKj9CxWN7k8QvYJd"
)
self.assertEqual(result.left.op.name, "||")
self.assertEqual(
result.left.right.sha_hash, "309BC5E644F797F53E5A2065EAF38A173437F2E6"
)
self.assertEqual(pypeg2.compose(result, Condition), condition)
def test_complex_condition(self):
condition = (
"(SIG(HgTTJLAQ5sqfknMq7yLPZbehtuLSsKj9CxWN7k8QvYJd) || (SIG("
"DNann1Lh55eZMEDXeYt59bzHbA3NJR46DeQYCS2qQdLV) && XHX(309BC5E644F797F53E5A2065EAF38A173437F2E6)))"
)
result = pypeg2.parse(condition, Condition)
self.assertEqual(
result.left.left.pubkey, "HgTTJLAQ5sqfknMq7yLPZbehtuLSsKj9CxWN7k8QvYJd"
)
self.assertEqual(result.left.op.name, "||")
self.assertEqual(
result.left.right.left.pubkey,
"DNann1Lh55eZMEDXeYt59bzHbA3NJR46DeQYCS2qQdLV",
)
self.assertEqual(result.left.right.op.name, "&&")
self.assertEqual(
result.left.right.right.sha_hash, "309BC5E644F797F53E5A2065EAF38A173437F2E6"
)
self.assertEqual(pypeg2.compose(result, Condition), condition)
def test_csv_cltv_condition(self):
condition = "(CSV(1654300) || (SIG(DNann1Lh55eZMEDXeYt59bzHbA3NJR46DeQYCS2qQdLV) && CLTV(2594024)))"
result = pypeg2.parse(condition, Condition)
self.assertEqual(result.left.left.time, "1654300")
self.assertEqual(result.left.op.name, "||")
self.assertEqual(
result.left.right.left.pubkey,
"DNann1Lh55eZMEDXeYt59bzHbA3NJR46DeQYCS2qQdLV",
)
self.assertEqual(result.left.right.op.name, "&&")
self.assertEqual(result.left.right.right.timestamp, "2594024")
self.assertEqual(pypeg2.compose(result, Condition), condition)
def test_instanciate_condition(self):
Condition.token(
SIG.token("HgTTJLAQ5sqfknMq7yLPZbehtuLSsKj9CxWN7k8QvYJd"),
Operator.token("||"),
Condition.token(
SIG.token("DNann1Lh55eZMEDXeYt59bzHbA3NJR46DeQYCS2qQdLV"),
Operator.token("&&"),
XHX.token("309BC5E644F797F53E5A2065EAF38A173437F2E6"),
),
)
condition = (
"(SIG(HgTTJLAQ5sqfknMq7yLPZbehtuLSsKj9CxWN7k8QvYJd) || (SIG("
"DNann1Lh55eZMEDXeYt59bzHbA3NJR46DeQYCS2qQdLV) && XHX(309BC5E644F797F53E5A2065EAF38A173437F2E6)))"
)
inst = pypeg2.parse(condition, Condition)
self.assertEqual(
inst.left.left.pubkey, "HgTTJLAQ5sqfknMq7yLPZbehtuLSsKj9CxWN7k8QvYJd"
)
self.assertEqual(inst.left.op.name, "||")
self.assertEqual(
inst.left.right.left.pubkey, "DNann1Lh55eZMEDXeYt59bzHbA3NJR46DeQYCS2qQdLV"
)
self.assertEqual(inst.left.right.op.name, "&&")
self.assertEqual(
inst.left.right.right.sha_hash, "309BC5E644F797F53E5A2065EAF38A173437F2E6"
)
self.assertEqual(pypeg2.compose(inst, Condition), condition)
def test_SIG_token_and_compose(self):
self.assertEqual(SIG.token(pubkey).compose(), "SIG(" + pubkey + ")")
def test_CSV_token_and_compose(self):
self.assertEqual(CSV.token(pubkey).compose(), "CSV(" + pubkey + ")")
def test_CLTV_token_and_compose(self):
self.assertEqual(CLTV.token(pubkey).compose(), "CLTV(" + pubkey + ")")
def test_HXH_token_and_compose(self):
self.assertEqual(XHX.token(pubkey).compose(), "XHX(" + pubkey + ")")
|