Commit Graph

828 Commits

Author SHA1 Message Date
Conrado Gouvea c9e93a75f7
Replace primitives_types with uint (#2350) 2021-06-18 15:35:05 -03:00
teor 2396950641
Add property test strategies for V5 transactions (#2347)
Add proptest strategies that:
- set the initial block height
- set the transaction version
- make all V5 transaction network upgrade fields valid
2021-06-18 14:40:08 -03:00
teor 4d22a0bae9
Security: Limit reconnection rate to individual peers (#2275)
* Security: Limit reconnection rate to individual peers

Reconnection Rate

Limit the reconnection rate to each individual peer by applying the
liveness cutoff to the attempt, responded, and failure time fields.
If any field is recent, the peer is skipped.

The new liveness cutoff skips any peers that have recently been attempted
or failed. (Previously, the liveness check was only applied if the peer
was in the `Responded` state, which could lead to repeated retries of
`Failed` peers, particularly in small address books.)

Reconnection Order

Zebra prefers more useful peer states, then the earliest attempted,
failed, and responded times, then the most recent gossiped last seen
times.

Before this change, Zebra took the most recent time in all the peer time
fields, and used that time for liveness and ordering. This led to
confusion between trusted and untrusted data, and success and failure
times.

Unlike the previous order, the new order:
- tries all peers in each state, before re-trying any peer in that state,
  and
- only checks the the gossiped untrusted last seen time
  if all other times are equal.

* Preserve the later time if changes arrive out of order

* Update CandidateSet::next documentation

* Update CandidateSet state diagram

* Fix variant names in comments

* Explain why timestamps can be left out of MetaAddrChanges

* Add a simple test for the individual peer retry limit

* Only generate valid Arbitrary PeerServices values

* Add an individual peer retry limit AddressBook and CandidateSet test

* Stop deleting recently live addresses from the address book

If we delete recently live addresses from the address book, we can get a
new entry for them, and reconnect too rapidly.

* Rename functions to match similar tokio API

* Fix docs for service sorting

* Clarify a comment

* Cleanup a variable and comments

* Remove blank lines in the CandidateSet state diagram

* Add a multi-peer proptest that checks outbound attempt fairness

* Fix a comment typo

Co-authored-by: Janito Vaqueiro Ferreira Filho <janito.vff@gmail.com>

* Simplify time maths in MetaAddr

* Create a Duration32 type to simplify calculations and comparisons

* Rename variables for clarity

* Split a string constant into multiple lines

* Make constants match rustdoc order

Co-authored-by: Janito Vaqueiro Ferreira Filho <janito.vff@gmail.com>
2021-06-18 09:30:44 -03:00
Pili Guerra 6396ac27d8
Update versions for zebra v1.0.0-alpha.11 release (#2334)
* Update versions for zebra v1.0.0-alpha.11 release

* Update Cargo.lock
2021-06-18 10:37:58 +01:00
teor ff7c1c4523
Depend on a specific git commit for zcash_history (#2345) 2021-06-18 09:32:21 +00:00
Alfredo Garcia 544d182d25
Add and use a function for mandatory checkpoint (#2314)
* add `mandatory_checkpoint_height()` function

* use mandatory checkpoint instead of canopy in acceptance tests
2021-06-18 10:05:28 +10:00
Conrado P. L. Gouvea 1cb10ec9e7 update librustzcash, change zcash_history to work with it 2021-06-17 14:26:31 -04:00
Alfredo Garcia 2291abc150
Stop panicking on invalid orchard nullifiers (#2267)
* stop panicking on invalid orchard nullifiers

* add context to error

* use `from_bytes_wide` for nullifiers in arbitrary

* orchard::Nullifier vec to array conversion is a bit clearer and simpler

Co-authored-by: teor <teor@riseup.net>
2021-06-15 02:29:19 +00:00
Janito Vaqueiro Ferreira Filho 0e89236405
Reject V5 transactions before NU5 activation (#2285)
* Add a `Transaction::version` getter

Returns the version of the transaction as a `u32`.

* Add `Transaction::is_overwintered` helper method

Returns if the `fOverwintered` flag should be set for the transaction's
version.

* Use new helpers to serialize transaction version

Reduce the repeated code and make it less error-prone with future
changes.

* Add getter methods to `transaction::Request` type

Refactor to move the type deconstruction code into the `Request` type.
The main objective is to make it easier to split the call handler into
methods that receive the request directly.

* Refactor to create `verify_v4_transaction` helper

Split the code specific to V4 transactions into a separate helper
method.

* Create `verify_v5_transaction` helper method

Prepare a separate method to have the validation code.

* Add `UnsupportedByNetworkUpgrade` error variant

An error for when a transaction's version isn't supported by the network
upgrade of the block it's included or for the current network upgrade if
the transaction is for the mempool.

* Verify a V5 transaction's network upgrade

For now, only NU5 supports V5 transactions.

* Test that V5 transaction is rejected on Canopy

Create a fake V5 transaction and try to verify it using a block height
from Canopy's activation. The verifier should reject the transaction
with an error saying that the network upgrade does not support that
transaction version.

* Test if V5 tx. is accepted after NU5 activation

Create a fake V5 transaction and pretend it is placed in a block that
has a height after the NU5 activation. The test should succeed, but
since the NU5 activation height has not been specified yet (neither for
the testnet nor the mainnet), for now this test is marked as
`should_panic`.

* Add `TODO` comment to the code

Add more detail to what's left to do, and link to the appropriate PRs.

* Use `u32` to store transaction version

Use a type consistent with how the version is specified.

Co-authored-by: teor <teor@riseup.net>

Co-authored-by: teor <teor@riseup.net>
2021-06-15 10:15:59 +10:00
Alfredo Garcia 3dcd407d66
Security: Stop panicking on invalid reserved orchard::Flags bits (#2284)
* stop panicking in deserialize orchard flags

* make the test simpler

Co-authored-by: Janito Vaqueiro Ferreira Filho <janito.vff@gmail.com>
2021-06-15 10:04:18 +10:00
Conrado Gouvea 5c0880810b
ZIP-221: integrate MMR tree from librustcash (without Orchard) (#2227)
* add zcash_history.rs with librustzcash Tree wrapper

* Apply suggestions from code review

Co-authored-by: teor <teor@riseup.net>

* Apply changes from code review

* Update zebra-chain/src/primitives/zcash_history.rs

Co-authored-by: teor <teor@riseup.net>

* Apply changes from code review

* Add Entry struct; return Result where needed; add test

* Apply suggestions from code review

Co-authored-by: teor <teor@riseup.net>

* zcash_history: improve naming style with `inner`

* zcash_history: check if block has the correct network upgrade when adding to tree

* zcash_history: test improvements

* zcash_history: split Tree::new into new_from_block and new_from_cache

* zcash_history: move tests to their own file

* remove unneeded empty line in Cargo.toml

Co-authored-by: teor <teor@riseup.net>
2021-06-11 11:25:48 -03:00
dependabot[bot] fc0edb5c44
build(deps): bump itertools from 0.10.0 to 0.10.1 (#2279)
Bumps [itertools](https://github.com/rust-itertools/itertools) from 0.10.0 to 0.10.1.
- [Release notes](https://github.com/rust-itertools/itertools/releases)
- [Changelog](https://github.com/rust-itertools/itertools/blob/master/CHANGELOG.md)
- [Commits](https://github.com/rust-itertools/itertools/compare/v0.10.0...v0.10.1)

---
updated-dependencies:
- dependency-name: itertools
  dependency-type: direct:production
  update-type: version-update:semver-patch
...

Signed-off-by: dependabot[bot] <support@github.com>

Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2021-06-11 12:26:46 +10:00
Pili Guerra 9aafa79fa3
Update versions for zebra v1.0.0-alpha.10 release (#2245)
* Update versions for zebra v1.0.0-alpha.10 release

* Update Cargo.lock
2021-06-09 12:56:36 +02:00
teor 8ebb415e7c Clippy: remove needless borrows 2021-06-07 18:33:58 -04:00
Janito Vaqueiro Ferreira Filho 2e0318878a
Further test new transaction consensus rules (#2246)
* Add a `at_least_one!` macro for testing

Similar to the `vec!` macro, but doesn't allow creating an empty list.

* Test if `has_inputs_and_outputs` considers actions

Create a dummy transaction with no inputs and no outputs, and add a
dummy Orchard action to it. The `check::has_inputs_and_outputs`
should succeed, because the consensus rule considers having Orchard
actions as having inputs and/or outputs.

* Refactor to create helper function

Move the code to create a fake Orchard shielded data instance to a
helper function in `zebra_chain::transaction::arbitrary`, so that other
tests can also use it.

* Test coinbase V5 transaction with enable spends

A V5 coinbase transaction that has Orchard shielded data MUST NOT have
the enable spends flag set.

* Test if coinbase without enable spends is valid

A coinbase transaction with Orchard shielded data and without the enable
spends flag set should be valid.

* Add a security comment about the `at_least_one!` macro

This macro must not be used outside tests, because it allows memory denial
of service.

Co-authored-by: teor <teor@riseup.net>
2021-06-07 12:02:18 +10:00
teor c453fbf6f6
Add final sapling root test vectors (#2243)
* Add sapling final root test vectors

Also tidy some formatting and imports

* Doc: final sapling roots can be duplicated

* Reverse the byte order of final sapling root test vectors

This makes the test vectors match the byte order in the block header,
rather than the zcashd RPC responses.

* Ignore pre-sapling block header commitments

Previously, Zebra expected this reserved field to be all zeroes,
but some mainnet and testnet blocks had other values.

* Test structural and semantic validation of the block commitment field

History roots are excluded from these tests, because they require
contextual validation.
2021-06-04 10:31:47 -03:00
Janito Vaqueiro Ferreira Filho b44d81669f
Move the check in `transaction::check::sapling_balances_match` to `V4` deserialization (#2234)
* Implement `PartialEq<i64>` for `Amount`

Allows to compare an `Amount` instance directly to an integer.

* Add `SerializationError::BadTransactionBalance`

Error variant representing deserialization of a transaction that doesn't
conform to the Sapling consensus rule where the balance MUST be zero if
there aren't any shielded spends and outputs.

* Validate consensus rule when deserializing

Return an error if the deserialized V4 transaction has a non-zero value
balance but doesn't have any Sapling shielded spends nor outputs.

* Add consensus rule link to field documentation

Describe how the consensus rule is validated structurally by
`ShieldedData`.

* Clarify that `value_balance` is zero

Make the description more concise and objective.

Co-authored-by: Alfredo Garcia <oxarbitrage@gmail.com>

* Update field documentation

Include information about how the consensus rule is guaranteed during
serialization.

Co-authored-by: teor <teor@riseup.net>

* Remove `check::sapling_balances_match` function

The check is redundant because the respective consensus rule is
validated structurally by `ShieldedData`.

* Test deserialization of invalid V4 transaction

A transaction with no Sapling shielded spends and no outputs but with a
non-zero balance value should fail to deserialize.

* Change least-significant byte of the value balance

State how the byte index is calculated, and change the least
significant-byte to be non-zero.

Co-authored-by: teor <teor@riseup.net>
2021-06-04 08:53:00 +10:00
teor 2f0f379a9e
Standardise clippy lints and require docs (#2238)
* Standardise lints across Zebra crates, and add missing docs

The only remaining module with missing docs is `zebra_test::command`

* Todo -> TODO

* Clarify what a transcript ErrorChecker does

Also change `Error` -> `BoxError`

* TransError -> ExpectedTranscriptError

* Output Descriptions -> Output descriptions
2021-06-04 08:48:40 +10:00
Janito Vaqueiro Ferreira Filho 9416b5d5cd
Update `transaction::check::coinbase_tx_no_joinsplit_or_spend` to validate V5 coinbase transactions with Orchard shielded data (#2236)
* Add a `Transaction::orchard_shielded_data` getter

Allows accessing the Orchard shielded data if it is present in the
transaction, regardless of the transaction version.

* Refactor `orchard_nullifiers` to use new getter

Allows making the method more concise.

* Add `CoinbaseHasEnableSpendsOrchard` error variant

Used when the validation rule is not met.

* Implement `enableSpendsOrchard` in coinbase check

The flag must not be set for the coinbase transaction.

* Refactor `Transaction::orchard_*` getters

Use the fact that `Option<T>` implements `Iterator<T>` to simplify the
code and remove the need for boxing the iterators.

Co-authored-by: teor <teor@riseup.net>
2021-06-03 01:54:08 +00:00
Alfredo Garcia a9fe0d9d3e
Make sure the mandatory checkpoint includes Canopy activation (#2235)
* Make sure the Canopy activation block is a finalized checkpoint block

This enables ZIP-221 chain history from Canopy activation onwards.

* Clarify that the mandatory checkpoint test includes Canopy activation

The test was correct, but the docs and assertion message did not include activation.

* Document that the mandatory checkpoint includes Canopy activation

Co-authored-by: teor <teor@riseup.net>
2021-06-03 10:24:08 +10:00
teor 81f2df9f36
Adjust the benchmark sample size so all benchmarks finish successfully (#2237)
This works on my machine.
2021-06-03 09:35:42 +10:00
teor 35f097995b
Make debugging easier on proptests with large vectors (#2232)
* Restore SummaryDebug on arbitrary chains

And also add it to some more proptest vectors.

* Reduce most arbitrary vectors from 10 to 4

This makes debugging easier

* Make SummaryDebug generic over collections and exact size iterators

* Document DisplayToDebug
2021-06-02 10:18:04 -03:00
Janito Vaqueiro Ferreira Filho db0cdb74ff
Update `has_inputs_and_outputs` to check V5 transactions (#2229)
* Fix documentation comment

Was missing a slash to become documentation.

* Add documentation link to type reference

Just to help navigation a bit.

* Implement `Transaction::orchard_actions()` getter

Returns an iterator to iterator over the actions in the Orchard shielded
data (if there is one, otherwise it returns an empty iterator).

* Add V5 support for `has_inputs_and_outputs`

Checks if the transaction has Orchard actions. If it does, it is
considered to have inputs and outputs.

* Refactor transaction test vectors

Make it easier to reuse the fake V5 transaction converter in other test
vectors.

* Move helper function to `zebra-chain` crate

Place it together with some other helper functions, including the one
that actually creates the fake V5 transaction.

* Test transaction with no inputs

`check::has_inputs_and_outputs` should return an error indicating that
the transaction has no inputs.

* Test transaction with no outputs

`check::has_inputs_and_outputs` should return an error indicating that
the transaction has no outputs.

* Note that transaction is fake in `expect` message

Should make the message easier to find, and also gives emphasis to the
fact that the transaction is a fake conversion to V5.

Co-authored-by: teor <teor@riseup.net>

Co-authored-by: teor <teor@riseup.net>
2021-06-02 11:32:52 +10:00
Alfredo Garcia 1685611592
Store orchard nullifiers into the state (#2185)
* add nullifier methods to orchard
* store orchard nullifiers
* bump database version
* update `IntoDisk`
* support V5 in `UpdateWith`
* add a test for finalized state
* Use the latest network upgrade in state proptests
2021-06-01 17:53:13 +10:00
Deirdre Connolly 4e8efd0cec
Split out parsing & validation of test VerificationKey from checking of the signature (#2228)
Now that we always generate an extra proper VerificationKey for each
Tweak::ChangePubkey case, this /should/ never fail: it also helps split out the
actual verification of the signature from the parsing and validation of the key
itself.
2021-06-01 15:59:34 +10:00
teor ebe1c9f88e
Add a DateTime32 type for 32-bit serialized times (#2210)
* Add a DateTime32 type for 32-bit serialized times
* Use DateTime32 for MetaAddr.last_seen
* Create and use a `DateTime32::now` method
2021-05-31 12:52:34 +10:00
teor 4c276dae64
Cleanup a few arbitrary impls (#2222) 2021-05-28 09:49:28 -03:00
teor 0b611eb770
Generate test chains that pass basic chain consistency tests (#2221)
* Set the tip height and previous hash for arbitrary genesis blocks

And cleanup the ledger strategy interface.

* Generate partial chains with correct previous block hashes

* Provide the network value from the PreparedChain strategy
2021-05-28 09:48:27 -03:00
Deirdre Connolly a5f5913d5f
Get redpallas tweak proptests working again (#2219)
Instead of creating an invalid verification key for a particular signature by tweaking its bytes,
create another verification key and when the ChangePubkey tweak is applied, just swap out the correct
SignatureCase::pk_bytes for SignatureCase::invalid_pk_bytes and check that trying to verify the signature
using that wrong key fails, as expected.

Resolves #2170
2021-05-27 21:59:14 +00:00
teor f94033df08
Make arbitrary block chains pass some genesis checks (#2208)
* Clarify the finalized state assertion that checks the genesis block

* Make arbitrary block chains pass some genesis checks

Use the genesis previous block hash for
- the first arbitrary block in each chain, and
- individual arbitrary blocks.

This setting can be adjusted by individual proptests as needed.
2021-05-27 12:41:20 -03:00
teor f0c271bcfe
Doc: shielded data always contains at least one action (#2218)
Remove an incorrect part of a comment
2021-05-27 12:06:08 -03:00
Conrado Gouvea f77441d49c
Fix scriptCode serialization and sighash test vectors (#2198)
* Fix scriptCode serialization and sighash test vectors

The scriptCode was being serialized without the compact size prefix, and the test vectors included the prefix in the script, which cancelled each other
2021-05-27 10:04:10 -03:00
teor 078385ae00 Canonicalise arbitrary IP addresses in proptests
This makes round-trip serialization tests work.
2021-05-26 18:13:35 -04:00
teor 6fb94baeb9 Stop converting IPv6-compatible IPv4 addresses to IPv4
Zcash only uses IPv6-mapped IPv4 addresses in its network protocol.
2021-05-26 18:13:35 -04:00
Deirdre Connolly 7894cec814
Test Eq/PartialEq for orchard keys (#2187)
* Add ConstantTimeEq's for Orchard FullViewingKey and DiversifierKey and affirmatively test

* Fix orchard::keys doc comments with links to make them automatic links

* Exercise ConstantTimeEq for FullViewingKey with a cheap clone

* Allow some clippy lints to pass for somewhat contrived tests

Co-authored-by: teor <teor@riseup.net>
2021-05-27 07:46:05 +10:00
Pili Guerra e3d2ae0a8a
Update versions for zebra v1.0.0-alpha.9 release (#2196)
* Update versions for zebra v1.0.0-alpha.9 release

* Update Cargo.lock
2021-05-26 13:01:39 +02:00
Alfredo Garcia c3820b44eb
Add action methods (#2199)
* add action methods

Co-authored-by: teor <teor@riseup.net>
Co-authored-by: Janito Vaqueiro Ferreira Filho <janito.vff@gmail.com>
2021-05-26 02:02:42 +00:00
Deirdre Connolly 896565377a Fix hyperlinks in doc 2021-05-25 14:12:36 -04:00
teor be7d69c3cb Try arbitrary leap seconds 2021-05-25 14:12:36 -04:00
teor f0549b2f7c
Derive Arbitrary impls for a bunch of chain and network types (#2179)
Enable proptests for internal and external network protocol messages,
using times with the correct protocol-specific ranges. (4 or 8 bytes.)
2021-05-24 11:10:07 -04:00
teor 6797f4167d Add another regression test case 2021-05-24 11:08:05 -04:00
teor 5e80d3ffd0 Add proptest regressions for redpallas::prop::tweak_signature
These regressions make `spendauth.check()` fail.
2021-05-24 11:08:05 -04:00
teor 5ac72f1184 Log instead of failing the redpallas::prop::tweak_signature test 2021-05-24 11:08:05 -04:00
teor d4f3ecd755
Remove redundant cmp_owned and comments (#2186)
* Remove redundant cmp_owned and comments

This is a cleanup after PR #2184.
2021-05-23 21:40:47 +00:00
Deirdre Connolly ad7a29517c
Fix clippy::cmp_owned for (sapling, orchard)::keys with `ConstantTimeEq` (#2184)
* Impl subtle::ConstantTimeEq for orchard SpendingKey, use that in Eq/PartialEq

* Use constant time comparisons for secret key data where applicable

This also makes Clippy happier so that we aren't creating types just to compare.

* Fix clippy::cmp_owned for orchard::keys Eq/PartialEq

By impl'ing ConstantTimeEq for those types where leaks of the value
would compromise access or privacy.

* Make clippy::cmp_owned happy for some sapling::keys
2021-05-22 09:40:49 -03:00
teor 57fb5c028c
Fix up some doc links (#2180) 2021-05-21 12:06:31 -03:00
Alfredo Garcia a57c09a3b8
Orchard data in V5 parsing (#2116)
* initialize the work on parsing orchard data in V5
* add the rest of orchard serialization
* fix serialization according to spec
* fix arbitrary for Signature<SpendAuth>
* move deserialization of AuthorizedAction to shielded_data module
* use `from_bits_truncate` to generate valid arbitrary flags
* change panic message
* fix serialization/deserialization when nActionsOrchard is empty
* fix Halo2Proof deserialization
* implement ZcashSerialize and ZcashDeserialize for flags
* implement ZcashSerialize and ZcashDeserialize for orchard::tree::Root
* use ZcashSerialize and ZcashDeserialize for binding_sig
* implement from_parts()
* implement Arbitrary for  Signature<Binding>
* add trusted preallocate with tests
* fix Arbitrary for orchard Nullifier
* Use zcash_serialize_bytes instead of write_compactsize

Co-authored-by: teor <teor@riseup.net>
2021-05-21 00:42:06 +00:00
teor eb2e58ba53
Security: reject compact sizes greater than the protocol message limit (#2155)
These sizes should be impossible in valid messages.
So they likely represent a memory preallocation attack.
2021-05-17 18:23:06 -04:00
teor b600e82d6e
Security: Avoid silently corrupting invalid times during serialization (#2149)
* Security: panic if an internally generated time is out of range

If Zebra has a bug where it generates blocks, transactions, or meta
addresses with bad times, panic. This avoids sending bad data onto the
network.

(Previously, Zebra would truncate some of these times, silently
corrupting the underlying data.)

Make it clear that deserialization of these objects is infalliable.
2021-05-17 16:53:10 -04:00
Alfredo Garcia 1f25d84273
correct some docs (#2157) 2021-05-14 16:35:10 -03:00