zcash/dev/rust.html

309 lines
16 KiB
HTML

<!DOCTYPE HTML>
<html lang="en" class="light sidebar-visible" dir="ltr">
<head>
<!-- Book generated using mdBook -->
<meta charset="UTF-8">
<title>Rust in zcashd - The zcashd Book</title>
<!-- Custom HTML head -->
<meta name="description" content="">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="theme-color" content="#ffffff">
<link rel="icon" href="../favicon.svg">
<link rel="shortcut icon" href="../favicon.png">
<link rel="stylesheet" href="../css/variables.css">
<link rel="stylesheet" href="../css/general.css">
<link rel="stylesheet" href="../css/chrome.css">
<link rel="stylesheet" href="../css/print.css" media="print">
<!-- Fonts -->
<link rel="stylesheet" href="../FontAwesome/css/font-awesome.css">
<link rel="stylesheet" href="../fonts/fonts.css">
<!-- Highlight.js Stylesheets -->
<link rel="stylesheet" id="highlight-css" href="../highlight.css">
<link rel="stylesheet" id="tomorrow-night-css" href="../tomorrow-night.css">
<link rel="stylesheet" id="ayu-highlight-css" href="../ayu-highlight.css">
<!-- Custom theme stylesheets -->
<!-- Provide site root and default themes to javascript -->
<script>
const path_to_root = "../";
const default_light_theme = "light";
const default_dark_theme = "navy";
</script>
<!-- Start loading toc.js asap -->
<script src="../toc.js"></script>
</head>
<body>
<div id="body-container">
<!-- Work around some values being stored in localStorage wrapped in quotes -->
<script>
try {
let theme = localStorage.getItem('mdbook-theme');
let sidebar = localStorage.getItem('mdbook-sidebar');
if (theme.startsWith('"') && theme.endsWith('"')) {
localStorage.setItem('mdbook-theme', theme.slice(1, theme.length - 1));
}
if (sidebar.startsWith('"') && sidebar.endsWith('"')) {
localStorage.setItem('mdbook-sidebar', sidebar.slice(1, sidebar.length - 1));
}
} catch (e) { }
</script>
<!-- Set the theme before any content is loaded, prevents flash -->
<script>
const default_theme = window.matchMedia("(prefers-color-scheme: dark)").matches ? default_dark_theme : default_light_theme;
let theme;
try { theme = localStorage.getItem('mdbook-theme'); } catch(e) { }
if (theme === null || theme === undefined) { theme = default_theme; }
const html = document.documentElement;
html.classList.remove('light')
html.classList.add(theme);
html.classList.add("js");
</script>
<input type="checkbox" id="sidebar-toggle-anchor" class="hidden">
<!-- Hide / unhide sidebar before it is displayed -->
<script>
let sidebar = null;
const sidebar_toggle = document.getElementById("sidebar-toggle-anchor");
if (document.body.clientWidth >= 1080) {
try { sidebar = localStorage.getItem('mdbook-sidebar'); } catch(e) { }
sidebar = sidebar || 'visible';
} else {
sidebar = 'hidden';
}
sidebar_toggle.checked = sidebar === 'visible';
html.classList.remove('sidebar-visible');
html.classList.add("sidebar-" + sidebar);
</script>
<nav id="sidebar" class="sidebar" aria-label="Table of contents">
<!-- populated by js -->
<mdbook-sidebar-scrollbox class="sidebar-scrollbox"></mdbook-sidebar-scrollbox>
<noscript>
<iframe class="sidebar-iframe-outer" src="../toc.html"></iframe>
</noscript>
<div id="sidebar-resize-handle" class="sidebar-resize-handle">
<div class="sidebar-resize-indicator"></div>
</div>
</nav>
<div id="page-wrapper" class="page-wrapper">
<div class="page">
<div id="menu-bar-hover-placeholder"></div>
<div id="menu-bar" class="menu-bar sticky">
<div class="left-buttons">
<label id="sidebar-toggle" class="icon-button" for="sidebar-toggle-anchor" title="Toggle Table of Contents" aria-label="Toggle Table of Contents" aria-controls="sidebar">
<i class="fa fa-bars"></i>
</label>
<button id="theme-toggle" class="icon-button" type="button" title="Change theme" aria-label="Change theme" aria-haspopup="true" aria-expanded="false" aria-controls="theme-list">
<i class="fa fa-paint-brush"></i>
</button>
<ul id="theme-list" class="theme-popup" aria-label="Themes" role="menu">
<li role="none"><button role="menuitem" class="theme" id="default_theme">Auto</button></li>
<li role="none"><button role="menuitem" class="theme" id="light">Light</button></li>
<li role="none"><button role="menuitem" class="theme" id="rust">Rust</button></li>
<li role="none"><button role="menuitem" class="theme" id="coal">Coal</button></li>
<li role="none"><button role="menuitem" class="theme" id="navy">Navy</button></li>
<li role="none"><button role="menuitem" class="theme" id="ayu">Ayu</button></li>
</ul>
<button id="search-toggle" class="icon-button" type="button" title="Search. (Shortkey: s)" aria-label="Toggle Searchbar" aria-expanded="false" aria-keyshortcuts="S" aria-controls="searchbar">
<i class="fa fa-search"></i>
</button>
</div>
<h1 class="menu-title">The zcashd Book</h1>
<div class="right-buttons">
<a href="../print.html" title="Print this book" aria-label="Print this book">
<i id="print-button" class="fa fa-print"></i>
</a>
</div>
</div>
<div id="search-wrapper" class="hidden">
<form id="searchbar-outer" class="searchbar-outer">
<input type="search" id="searchbar" name="searchbar" placeholder="Search this book ..." aria-controls="searchresults-outer" aria-describedby="searchresults-header">
</form>
<div id="searchresults-outer" class="searchresults-outer hidden">
<div id="searchresults-header" class="searchresults-header"></div>
<ul id="searchresults">
</ul>
</div>
</div>
<!-- Apply ARIA attributes after the sidebar and the sidebar toggle button are added to the DOM -->
<script>
document.getElementById('sidebar-toggle').setAttribute('aria-expanded', sidebar === 'visible');
document.getElementById('sidebar').setAttribute('aria-hidden', sidebar !== 'visible');
Array.from(document.querySelectorAll('#sidebar a')).forEach(function(link) {
link.setAttribute('tabIndex', sidebar === 'visible' ? 0 : -1);
});
</script>
<div id="content" class="content">
<main>
<h1 id="rust-in-zcashd"><a class="header" href="#rust-in-zcashd">Rust in <code>zcashd</code></a></h1>
<p><code>zcashd</code> is primarily a C++ codebase, but most new code is being written in Rust
where possible.</p>
<h2 id="auditing-rust-dependencies"><a class="header" href="#auditing-rust-dependencies">Auditing Rust dependencies</a></h2>
<p>We use <a href="https://github.com/mozilla/cargo-vet"><code>cargo-vet</code></a> to audit our Rust dependencies. This means that after
adding a new dependency, or updating existing dependencies with <code>cargo update</code>,
CI will fail until corresponding audits have been added.</p>
<p>We also have a significant number of pre-existing unaudited dependency versions
that are excluded from auditing checks. We aim to reduce this list over time.
New entries should not be added to the exclusion list without justification.</p>
<p>To audit a dependency, first <a href="https://mozilla.github.io/cargo-vet/install.html">install <code>cargo-vet</code></a> and then follow the
<a href="https://mozilla.github.io/cargo-vet/performing-audits.html">"Performing Audits" guide</a>. If you are updating a dependency then instead of
auditing the new version in its entirety, you can optionally just audit the
delta between the old and new versions - even if the old version is in the
"unaudited" exclusion list.</p>
<h2 id="adding-new-dependencies-in-online-rust-mode"><a class="header" href="#adding-new-dependencies-in-online-rust-mode">Adding new dependencies in online-Rust mode</a></h2>
<p>The <code>zcashd</code> build system pins all dependencies, and in order to facilitate
reproducible builds, <code>cargo</code> is configured to run in offline mode with vendored
crates. This means that if, for example, you add the <code>foobar</code> crate to
<code>Cargo.toml</code>, you will likely see an error similar to this:</p>
<pre><code>$ cargo check
error: no matching package named `foobar` found
location searched: registry `https://github.com/rust-lang/crates.io-index`
required by package `librustzcash v0.2.0 (/path/to/zcash)`
</code></pre>
<p>To add dependencies that are compatible with the reproducible build system, you need to follow these steps:</p>
<ol>
<li>First, if you've made changes to dependencies in <code>Cargo.toml</code>, these must be reverted before the next step:
<pre><code>git stash
</code></pre>
</li>
<li>Next, reconfigure the build system for "online" mode:
<pre><code>CONFIGURE_FLAGS=--enable-online-rust ./zcutil/build.sh
</code></pre>
</li>
<li>Now, introduce the dependency changes into <code>Cargo.toml</code>. If you saved changes in Step 1 with <code>git stash</code>, you can reapply them:
<pre><code>git stash pop
</code></pre>
</li>
<li>Update <code>Cargo.lock</code>:
<pre><code>cargo check
</code></pre>
</li>
<li>Commit the changes to <code>Cargo.toml</code> and <code>Cargo.lock</code> together:
<pre><code>git commit ./Cargo.{toml,lock}
</code></pre>
</li>
<li>Verify the reproducible build works in vendored/offline mode without the <code>--enable-online-rust</code> flag:
<pre><code>./zcutil/build.sh
</code></pre>
</li>
</ol>
<h2 id="using-an-unpublished-rust-dependency"><a class="header" href="#using-an-unpublished-rust-dependency">Using an unpublished Rust dependency</a></h2>
<p>Occasionally we may need to depend on an unpublished git revision of a crate.
We sometimes want to prove out API changes to the <code>zcash_*</code> Rust crates by
migrating <code>zcashd</code> to them first, before making a public crate release. Or we
might need to cut a <code>zcashd</code> release before some upstream dependency has
published a fix we need. In these cases, we use patch dependencies.</p>
<p>For example, to use an unpublished version of the <code>orchard</code> crate that includes
a new API, add the following patch to <code>Cargo.toml</code>:</p>
<pre><code>[dependencies]
# This dependency is listed with a version, meaning it comes from crates.io; the
# patch goes into a [patch.crates-io] section.
orchard = "0.4"
...
[patch.crates-io]
orchard = { git = "https://github.com/zcash/orchard.git", rev = "..." }
</code></pre>
<p>Note that if the git repository contains a workspace of interconnected crates
(for example, https://github.com/zcash/librustzcash), you will need to provide
patches for each of the dependencies that reference the same git revision.</p>
<p>You also need to update <code>.cargo/config.offline</code> to add a replacement definition
for each <code>(git, rev)</code> pair. Run <code>./test/lint/lint-cargo-patches.sh</code> to get the
lines that need to be present.</p>
<p>Finally, <code>./qa/supply-chain/config.toml</code> needs to be updated to ignore patched
dependencies. Run <code>cargo vet regenerate audit-as-crates-io</code>, and then ensure the
newly-added lines are of the form <code>audit-as-crates-io = false</code>.</p>
<h2 id="using-a-local-rust-dependency"><a class="header" href="#using-a-local-rust-dependency">Using a local Rust dependency</a></h2>
<p>During development, you can use a locally checked out version of a dependency
by applying a <a href="https://doc.rust-lang.org/cargo/reference/overriding-dependencies.html#the-patch-section"><code>cargo</code> patch</a>.</p>
<p>For example, to use a local version of the <code>orchard</code> crate that includes a new
API, add the following patch to <code>Cargo.toml</code>:</p>
<pre><code>[dependencies]
# This dependency is listed with a version, meaning it comes from crates.io; the
# patch goes into a [patch.crates-io] section.
orchard = "0.0"
...
[patch.crates-io]
# Comment out any existing patch, if present.
# orchard = { git = "https://github.com/zcash/orchard.git", rev = "..." }
# Add this patch (both relative and absolute paths work):
orchard = { path = "../relative/path/to/orchard" }
</code></pre>
<p>Usually you can apply a patch to use a locally checked out dependency without
needing to build <code>zcashd</code> in online-Rust mode. However, if your local changes
include a new dependency, you will need to ensure you are in online-Rust mode.</p>
</main>
<nav class="nav-wrapper" aria-label="Page navigation">
<!-- Mobile navigation buttons -->
<a rel="prev" href="../dev/dnsseed-policy.html" class="mobile-nav-chapters previous" title="Previous chapter" aria-label="Previous chapter" aria-keyshortcuts="Left">
<i class="fa fa-angle-left"></i>
</a>
<a rel="next prefetch" href="../dev/regtest.html" class="mobile-nav-chapters next" title="Next chapter" aria-label="Next chapter" aria-keyshortcuts="Right">
<i class="fa fa-angle-right"></i>
</a>
<div style="clear: both"></div>
</nav>
</div>
</div>
<nav class="nav-wide-wrapper" aria-label="Page navigation">
<a rel="prev" href="../dev/dnsseed-policy.html" class="nav-chapters previous" title="Previous chapter" aria-label="Previous chapter" aria-keyshortcuts="Left">
<i class="fa fa-angle-left"></i>
</a>
<a rel="next prefetch" href="../dev/regtest.html" class="nav-chapters next" title="Next chapter" aria-label="Next chapter" aria-keyshortcuts="Right">
<i class="fa fa-angle-right"></i>
</a>
</nav>
</div>
<script>
window.playground_copyable = true;
</script>
<script src="../elasticlunr.min.js"></script>
<script src="../mark.min.js"></script>
<script src="../searcher.js"></script>
<script src="../clipboard.min.js"></script>
<script src="../highlight.js"></script>
<script src="../book.js"></script>
<!-- Custom JS scripts -->
</div>
</body>
</html>