Beispiel #1
0
    def test_single_entry(self) -> None:
        mktree_file = join(MERKLE_TREE_TEST_DIR, "single")
        data = TEST_VALUES[0]

        mktree = PersistentMerkleTree.open(mktree_file,
                                           MERKLE_TREE_TEST_NUM_LEAVES,
                                           MERKLE_TREE_HASH)
        mktree.insert(data)
        self.assertEqual(1, mktree.get_num_entries())
        self.assertEqual(data, mktree.get_leaf(0))
        self.assertEqual(ZERO_ENTRY, mktree.get_leaf(1))
        root_1 = mktree.recompute_root()
        self.assertEqual(MERKLE_TREE_HASH.hash(data, ZERO_ENTRY),
                         mktree.get_node(1, 0))
        self.assertNotEqual(self._expected_empty(), root_1)
        mktree.save()

        mktree = PersistentMerkleTree.open(mktree_file,
                                           MERKLE_TREE_TEST_NUM_LEAVES,
                                           MERKLE_TREE_HASH)
        self.assertEqual(1, mktree.get_num_entries())
        self.assertEqual(data, mktree.get_leaf(0))
        self.assertEqual(ZERO_ENTRY, mktree.get_leaf(1))
        root_2 = mktree.recompute_root()
        self.assertEqual(root_1, root_2)
    def test_empty_save_load(self) -> None:
        mktree_file = join(MERKLE_TREE_TEST_DIR, "empty_save_load")
        mktree = PersistentMerkleTree.open(mktree_file,
                                           MERKLE_TREE_TEST_NUM_LEAVES)
        mktree.save()

        mktree = PersistentMerkleTree.open(mktree_file,
                                           MERKLE_TREE_TEST_NUM_LEAVES)
        root = mktree.recompute_root()
        mktree.save()

        self.assertEqual(self._expected_empty(), root)
Beispiel #3
0
 def __init__(self, mixer_instance: Any, username: str, wallet_dir: str,
              secret_address: ZklayAddressPriv, tree_hash: ITreeHash):
     # k_sk_receiver: EncryptionSecretKey):
     assert "_" not in username
     self.mixer_instance = mixer_instance
     self.username = username
     self.wallet_dir = wallet_dir
     self.a_sk = secret_address.addr_sk
     self.state_file = join(wallet_dir, f"state_{username}")
     self.state = _load_state_or_default(self.state_file)
     _ensure_dir(join(self.wallet_dir, SPENT_SUBDIRECTORY))
     self.merkle_tree = PersistentMerkleTree.open(
         join(wallet_dir, MERKLE_TREE_FILE),
         int(math.pow(2, ZETH_MERKLE_TREE_DEPTH)), tree_hash)
     self.merkle_tree_changed = False
     self.next_addr = self.merkle_tree.get_num_entries()