Commit Graph

55 Commits

Author SHA1 Message Date
Kris Nuttycombe ad2c751633 Improve reporting for errors that can be generated in witness creation. 2022-08-04 13:00:58 -06:00
Kris Nuttycombe 7556c04472 Update bridgetree root module documentation. 2022-08-04 13:00:58 -06:00
Kris Nuttycombe 5e04301a4b Move documentation to top-level methods + minor cleanup. 2022-08-04 13:00:58 -06:00
Kris Nuttycombe adf9ba26e4 Move the BridgeTree type to the root module. 2022-08-04 13:00:58 -06:00
Kris Nuttycombe 6e97389d1b Move `Frontier` and `Tree` traits to testing module.
These traits are only used for comparison testing between
the CompleteTree and BridgeTree types; they don't represent
useful independent abstractions.
2022-08-04 13:00:58 -06:00
Kris Nuttycombe 55731de321 Move Hashable trait to hashing module 2022-08-04 13:00:58 -06:00
Kris Nuttycombe 2a19b40fc1 Move position types to position module. 2022-08-04 13:00:56 -06:00
Kris Nuttycombe d631b8278a Move common test code into the testing module. 2022-08-04 13:00:05 -06:00
Kris Nuttycombe e19446f8fd Make test utilities available under a test-dependencies feature. 2022-08-04 13:00:05 -06:00
Kris Nuttycombe 6b6548f02e Rename authentication_path -> witness 2022-08-04 13:00:05 -06:00
Kris Nuttycombe ee93cb8598 Rename witness->mark 2022-08-04 13:00:04 -06:00
Kris Nuttycombe 3b08071125 Remove the `bridgetree::Leaf` type & make ommer levels start at 0
This is a major refactoring that gets rid of a significant wart
in the design of the `bridgetree::Frontier` type that has existed
since the the project's inception.

The `Leaf` type incorrectly treated the siblings of leaves at
odd positions as special, whereas they should have been simply
treated as ordinary entries in the frontier's ommers list. Correcting
this allows for the code to be significantly simplified in a number
of places.
2022-07-21 18:32:58 -06:00
Kris Nuttycombe 4973b6682f Alter MerkleBridge to avoid storing duplicate ommers.
Prior to this change, each position being tracked would
require an entry in the `auth_fragments` map for each
bridge in a BridgeTree. However, very frequently, these
entries would duplicate storage of nodes higher up in the
tree, wherever a single node in the was an ancestor of
a leaf being tracked.

This commit restructures the tracking of auth data to
avoid this duplication of data, as well as the redundant
work that was required to produce each copy of the duplicated
nodes.
2022-07-21 18:27:09 -06:00
Kris Nuttycombe 06728b9499 Add an as_of_root argument to Tree::authentication_path
This requires a user of this API to specify a root of the tree
that identifies the state of the tree at which the user wishes
to construct an authentication path. This must be equal to either
the current root of the tree, or to the root of the tree at a
previous checkpoint.
2022-04-20 14:13:12 -06:00
Kris Nuttycombe a4aa2bf204 Fix an off-by-one error in BridgeTree garbage collection. 2022-04-05 16:16:41 -06:00
Kris Nuttycombe f9c12e37aa Add witnessed_positions and garbage_collect methods to the `Tree` trait.
* `witnessed_positions` returns the set of positions that have been
  witnessed in the tree.
* `garbage_collect` makes the garbage collection operation of
  `bridgetree::BridgeTree` publicly accessible. It is always safe to
  implement this method as a no-op.
2022-04-05 16:15:41 -06:00
Kris Nuttycombe 027a98e53a Remove redundant documentation on trait impls. 2022-03-31 08:21:33 -06:00
Kris Nuttycombe 244fa7b73c Apply suggestions from code review
Co-authored-by: Daira Hopwood <daira@jacaranda.org>
2022-03-31 07:10:02 -06:00
Kris Nuttycombe a858de06d8 Always query the tree by position, not hash.
This modifies the `Tree::authentication_path` and `Tree::remove_witness`
methods to only operate in terms of the tree position, rather than both
the position and the hash that is expected to be the leaf at that
position.

Positions in the tree are always unique, and the tree already contains
the leaf information corresponding to each position, so the existing API
previously required the storage of redundant information. This change
streamlines the API without loss of power.

This also adds additional consistency checks to the property tests.
2022-03-30 22:16:31 -06:00
Kris Nuttycombe 34cc29dc4b Remove the unused recording types & logic 2022-03-30 15:20:11 -06:00
Kris Nuttycombe 250052489d Add a test demonstrating incorrect consistency check. 2022-03-10 18:39:36 -07:00
Kris Nuttycombe e01885f6e2 Rewinds are no longer blocked by existing witnesses.
In practice, when performing a rewind we don't want to have to
do the additional bookkeeping keeping the leaf position and
value around just to be able to remove the witnesses. After
this change, `rewind` will always succeed unless there are
no checkpoints.
2022-03-02 11:32:31 -07:00
Kris Nuttycombe f112568d73 Do not force Hashable values to also be Ord and Clone
While these additional bounds are often used with hashable
types, they are not necessary for the construction of `Frontier`
values, and so should not be required everywhere.
2022-02-24 09:01:23 -07:00
Kris Nuttycombe 42b9608e43 Fix linting errors. 2022-02-23 21:44:36 -07:00
Kris Nuttycombe 61a06de241 Fix formatting. 2022-02-23 21:30:52 -07:00
Kris Nuttycombe b4339d94db Return the current position & leaf from the witness operation.
Co-authored-by: str4d <jack@electriccoin.co>
2022-02-23 19:30:10 -07:00
Kris Nuttycombe 1dbb0d057d Update the property tests to generate positioned unwitness and auth_path operations. 2022-02-23 13:35:17 -07:00
Kris Nuttycombe b23b6ff9ec Record position with hash when constructing witnesses. 2022-02-23 11:33:08 -07:00
Kris Nuttycombe d5b7d90e13 Add bridgetree garbage collection.
This adds a garbage collection method to BridgeTree that
can be used to eliminate checkpoints and removed witnesses
that are no longer reachable for possible rewinds. It also
prunes unnecessary auth fragments from the tree.
2022-02-23 08:59:12 -07:00
Kris Nuttycombe c284081a0a Merge remote-tracking branch 'upstream/master' into fix_bridgetree_checkpoint_rewind 2022-02-22 21:05:11 -07:00
Kris Nuttycombe 993dd64650 Fix bridgetree checkpoint/rewind logic to fully restore the tree state.
Removal of witnesses is now recorded on a per-checkpoint
level, so that rewinding the tree state now restores any
witnesses that were removed since the checkpoint.

A number of internal consistency checks previously enforced
by the property tests have been removed as they did not
correctly implement this semantic, and reimplementing these
consistency checks would be equivalent to reimplementing the
sample tree, so these checks are now performed simply by
comparison against the reference implentation.
2022-02-22 09:44:32 -07:00
Kris Nuttycombe aedd865d42 Make example tests reusable across tree implementations. 2022-02-20 13:09:49 -07:00
Kris Nuttycombe efffa8a442 Key auth fragments by note position rather than bridge index.
This substantially simplifies reasoning about authentication
path fragments in garbage collection.
2022-02-20 10:21:58 -07:00
Kris Nuttycombe da420277a5 Use BTreeMap instead of HashMap when keyed by leaf hashes 2022-02-20 10:21:58 -07:00
Kris Nuttycombe 6327364524 Add current_leaf and is_witnessed to the tree API
Rename `current_position` to `current_offset` in the
sample API to avoid confusion with the meaning of
the `Position` type.
2022-02-20 10:21:21 -07:00
ghassmo 24e1980e8a use Self instead of the type's name 2022-01-23 23:16:30 +04:00
Kris Nuttycombe 67a5d6ff29 Fix documentation of tree methods. 2021-12-06 13:37:48 -07:00
Kris Nuttycombe 4e12c24807 Move `Position` into the base lib module and ensure wraps u64 explicitly. 2021-07-30 11:52:52 -06:00
Kris Nuttycombe 2f65f19fa6 Fix formatting. 2021-07-22 12:10:42 -06:00
Kris Nuttycombe 4d44fb56f5
Apply suggestions from code review
Co-authored-by: str4d <jack@electriccoin.co>
Co-authored-by: ying tong <yingtong@z.cash>
2021-07-20 09:45:36 -06:00
Kris Nuttycombe d188121cbe Improve bridgetree documentation. 2021-07-08 10:16:55 -06:00
Kris Nuttycombe e499a27f04 Revert change to string combine function for tests. 2021-06-24 14:12:52 -06:00
Kris Nuttycombe cf68d49d3f Trivial fixup. 2021-06-23 15:39:24 -06:00
Kris Nuttycombe 3decd0bf48 Use const generic parameter for tree depth. 2021-06-23 14:14:57 -06:00
Kris Nuttycombe aed785e896 Rename "Level" -> "Altitude"
The FlyClient spec uses "Altitude" for this concept in
Merkle mountain ranges, so I'm renaming for consistency.
2021-06-19 11:24:33 -06:00
Kris Nuttycombe 06e0a5772e Add `Frontier` trait as a subset of `Tree` operations.
For some applications, it is not necessary to include the
full range of witnessing and checkpointing operations. The
Frontier trait captures the minimal operations required:
appending leaves to the tree and constructing the root
of the tree by hashing against empty nodes.
2021-06-18 14:21:03 -06:00
Kris Nuttycombe c1fcd83329 Implement efficient incremental merkle tree. 2021-06-17 19:32:50 -06:00
Kris Nuttycombe 1267c8ec07 Rename "efficient" module to "bridgetree". 2021-06-17 19:31:26 -06:00
Kris Nuttycombe 37d1f920e4 Refactor hashing api & contracts of witness & checkpoint methods.
In this commit:

* TreeHasher is replaced by the Hashable trait. Hashable is
  modified to use level information in the construction of
  the hash, and a convenience method is provided for obtaining
  the empty node at a given level.

* The pop_checkpoint method is removed from the `Tree` interface.
  Instead, a maximum number of checkpoints is established at tree
  construction time and then the oldest checkpoints are automatically
  dropped when adding new checkpoints once that number is reached.

* The `witness` method implementation in the sample tree
  is modified to ignore duplicate calls.

* Property testing functiona are modified to abstract over
  the type of hashable data.
2021-06-17 19:24:18 -06:00
Kris Nuttycombe 68c8d88bd3 Move sample tree & efficient tree to independent modules. 2021-06-17 19:17:47 -06:00