From 20434aa835c0ac4cdd7c4807a2349d830c9afd11 Mon Sep 17 00:00:00 2001
From: timotheyca <tim@ongoteam.yaconnect.com>
Date: Sun, 11 Sep 2022 16:26:58 +0300
Subject: [PATCH] metadata verification

---
 .../trees/binary/activebinarytree.py          |  2 +-
 rainbowadn/collection/trees/binary/avl.py     |  5 +++--
 .../trees/binary/core/balancedcreation.py     |  6 ++++++
 .../trees/binary/core/binarybalancing.py      | 20 ++++++++++++++-----
 .../trees/binary/core/binarycreation.py       |  5 +++++
 .../binary/core/protocolizedbinarysplit.py    |  9 +++++++++
 rainbowadn/flow13/_binaryflow.py              |  1 +
 trace_flow.py                                 |  2 +-
 8 files changed, 41 insertions(+), 9 deletions(-)

diff --git a/rainbowadn/collection/trees/binary/activebinarytree.py b/rainbowadn/collection/trees/binary/activebinarytree.py
index 4da53a9..1580ecb 100644
--- a/rainbowadn/collection/trees/binary/activebinarytree.py
+++ b/rainbowadn/collection/trees/binary/activebinarytree.py
@@ -46,7 +46,7 @@ class ActiveBinaryTree(
             protocol,
             NullableReference(
                 Null(),
-                BinaryTreeFactory(KeyMetadataFactory(factory, protocol.empty_metadata().factory))
+                BinaryTreeFactory(KeyMetadataFactory(factory, protocol.empty_metadata.factory))
             )
         )
 
diff --git a/rainbowadn/collection/trees/binary/avl.py b/rainbowadn/collection/trees/binary/avl.py
index 842884e..9fe53d4 100644
--- a/rainbowadn/collection/trees/binary/avl.py
+++ b/rainbowadn/collection/trees/binary/avl.py
@@ -1,6 +1,7 @@
 from typing import Generic, Optional, TypeVar
 
 from rainbowadn.atomic import *
+from rainbowadn.collection.comparison import *
 from rainbowadn.core import *
 from .actions import *
 from .core import *
@@ -12,8 +13,8 @@ TreeType = TypeVar('TreeType')
 
 
 class AVL(BinaryBalancing[ActiveKeyType, Integer, TreeType]):
-    def empty_metadata(self) -> HashPoint[Integer]:
-        return HashPoint.of(Integer(0))
+    def __init__(self, comparator_: Comparator[ActiveKeyType]):
+        super().__init__(comparator_, HashPoint.of(Integer(0)))
 
     async def metadata(
             self,
diff --git a/rainbowadn/collection/trees/binary/core/balancedcreation.py b/rainbowadn/collection/trees/binary/core/balancedcreation.py
index 37dade3..342a7df 100644
--- a/rainbowadn/collection/trees/binary/core/balancedcreation.py
+++ b/rainbowadn/collection/trees/binary/core/balancedcreation.py
@@ -45,3 +45,9 @@ class BalancedCreation(
                 )
             )
         )
+
+    async def verify_metadata(
+            self, treel: TreeType, treer: TreeType, key: HashPoint[ActiveKeyType], metadata: HashPoint[MetaDataType]
+    ) -> bool:
+        assert_true(await self.protocol.verify_metadata(treel, treer, key, metadata, self))
+        return True
diff --git a/rainbowadn/collection/trees/binary/core/binarybalancing.py b/rainbowadn/collection/trees/binary/core/binarybalancing.py
index c586036..4203800 100644
--- a/rainbowadn/collection/trees/binary/core/binarybalancing.py
+++ b/rainbowadn/collection/trees/binary/core/binarybalancing.py
@@ -13,22 +13,32 @@ MetaDataType = TypeVar('MetaDataType')
 
 
 class BinaryBalancing(Generic[ActiveKeyType, MetaDataType, TreeType]):
-    def __init__(self, comparator_: Comparator[ActiveKeyType]):
+    def __init__(self, comparator_: Comparator[ActiveKeyType], empty_metadata: HashPoint[MetaDataType]):
         assert isinstance(comparator_, Comparator)
+        assert isinstance(empty_metadata, HashPoint)
         self.comparator = comparator_
-
-    def empty_metadata(self) -> HashPoint[MetaDataType]:
-        raise NotImplementedError
+        self.empty_metadata = empty_metadata
 
     async def metadata(
             self,
             treel: TreeType,
             treer: TreeType,
             key: HashPoint[ActiveKeyType],
-            creation: BinaryCreation[ActiveKeyType, MetaDataType, TreeType]
+            creation: BinaryCreation[ActiveKeyType, MetaDataType, TreeType],
     ) -> HashPoint[MetaDataType]:
         raise NotImplementedError
 
+    async def verify_metadata(
+            self,
+            treel: TreeType,
+            treer: TreeType,
+            key: HashPoint[ActiveKeyType],
+            metadata: HashPoint[MetaDataType],
+            creation: BinaryCreation[ActiveKeyType, MetaDataType, TreeType],
+    ) -> bool:
+        assert_eq(await self.metadata(treel, treer, key, creation), metadata)
+        return True
+
     async def balance(
             self,
             protocolized: BinaryProtocolized[ActiveKeyType, MetaDataType, TreeType],
diff --git a/rainbowadn/collection/trees/binary/core/binarycreation.py b/rainbowadn/collection/trees/binary/core/binarycreation.py
index b54da67..02f4498 100644
--- a/rainbowadn/collection/trees/binary/core/binarycreation.py
+++ b/rainbowadn/collection/trees/binary/core/binarycreation.py
@@ -24,3 +24,8 @@ class BinaryCreation(Generic[ActiveKeyType, MetaDataType, TreeType]):
 
     async def tree(self, treel: TreeType, treer: TreeType, key: HashPoint[ActiveKeyType]) -> TreeType:
         raise NotImplementedError
+
+    async def verify_metadata(
+            self, treel: TreeType, treer: TreeType, key: HashPoint[ActiveKeyType], metadata: HashPoint[MetaDataType]
+    ) -> bool:
+        raise NotImplementedError
diff --git a/rainbowadn/collection/trees/binary/core/protocolizedbinarysplit.py b/rainbowadn/collection/trees/binary/core/protocolizedbinarysplit.py
index 864c088..86cab57 100644
--- a/rainbowadn/collection/trees/binary/core/protocolizedbinarysplit.py
+++ b/rainbowadn/collection/trees/binary/core/protocolizedbinarysplit.py
@@ -1,5 +1,6 @@
 from typing import Generic, Optional, TypeVar
 
+from rainbowadn.core import *
 from .binarycreation import *
 from .binaryprotocolized import *
 from .binarysplit import *
@@ -50,3 +51,11 @@ class ProtocolizedBinarySplit(
             return None
         else:
             return ProtocolizedBinarySplit(protocolized.creation, split, protocolized.tree)
+
+    async def verify_metadata(self) -> bool:
+        assert_true(
+            await self.protocol.verify_metadata(
+                self.split.treel, self.split.treer, self.split.key, self.split.metadata
+            )
+        )
+        return True
diff --git a/rainbowadn/flow13/_binaryflow.py b/rainbowadn/flow13/_binaryflow.py
index 665f6e7..18505f6 100644
--- a/rainbowadn/flow13/_binaryflow.py
+++ b/rainbowadn/flow13/_binaryflow.py
@@ -176,6 +176,7 @@ class VerifySubsetAction(
                 assert_trues(
                     await gather(
                         self.on(case.protocolizedl()),
+                        case.verify_metadata(),
                         self.on(case.protocolizedr()),
                     )
                 )
diff --git a/trace_flow.py b/trace_flow.py
index 1f44f0e..f97684e 100644
--- a/trace_flow.py
+++ b/trace_flow.py
@@ -167,7 +167,7 @@ if __name__ == '__main__':
     try:
         asyncio.run(
             trace(
-                preset_short
+                preset_long
             )
         )
     except KeyboardInterrupt: