Skip to content

Commit

Permalink
feat(init-aptos): porting aptos files (#1)
Browse files Browse the repository at this point in the history
  • Loading branch information
tchataigner authored Mar 25, 2024
1 parent 3a07ab8 commit 05b8156
Show file tree
Hide file tree
Showing 27 changed files with 2,030 additions and 10 deletions.
18 changes: 18 additions & 0 deletions .github/dependabot.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
version: 2
updates:
- package-ecosystem: cargo
directory: /
pull-request-branch-name:
separator: "-"
schedule:
interval: weekly
groups:
rust-dependencies:
patterns:
- "*"
open-pull-requests-limit: 5

- package-ecosystem: "github-actions"
directory: "/"
schedule:
interval: "weekly"
87 changes: 87 additions & 0 deletions .github/workflows/rust.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
name: Rust

on:
merge_group:
push:
branches:
- "main"
pull_request:
types: [ opened, synchronize, reopened, ready_for_review ]
branches: [ main ]

env:
CARGO_TERM_COLOR: always
# Disable incremental compilation.
#
# Incremental compilation is useful as part of an edit-build-test-edit cycle,
# as it lets the compiler avoid recompiling code that hasn't changed. However,
# on CI, we're not making small edits; we're almost always building the entire
# project from scratch. Thus, incremental compilation on CI actually
# introduces *additional* overhead to support making future builds
# faster...but no future builds will ever occur in any given CI environment.
#
# See https://matklad.github.io/2021/09/04/fast-rust-builds.html#ci-workflow
# for details.
CARGO_INCREMENTAL: 0
# Allow more retries for network requests in cargo (downloading crates) and
# rustup (installing toolchains). This should help to reduce flaky CI failures
# from transient network timeouts or other issues.
CARGO_NET_RETRY: 10
RUSTUP_MAX_RETRIES: 10
# Don't emit giant backtraces in the CI logs.
RUST_BACKTRACE: short

concurrency:
group: ${{ github.workflow }}-${{ github.head_ref || github.run_id }}
cancel-in-progress: true

jobs:
test:
runs-on: ${{ matrix.os }}
strategy:
matrix:
os:
- ubuntu-latest
package:
- "aptos"
fail-fast: false
env:
RUSTFLAGS: -D warnings
steps:
- uses: actions/checkout@v4
- uses: dtolnay/rust-toolchain@stable
- uses: taiki-e/install-action@nextest
- uses: Swatinem/rust-cache@v2
# make sure benches don't bit-rot
- name: build benches
run: cargo build --benches --release
working-directory: ${{ github.workspace }}/${{ matrix.package }}
# TODO all-features
- name: cargo test
run: |
cargo nextest run --release --profile ci
working-directory: ${{ github.workspace }}/${{ matrix.package }}
- name: Doctests
run: |
cargo test --doc
working-directory: ${{ github.workspace }}/${{ matrix.package }}

clippy:
runs-on: ubuntu-latest
strategy:
matrix:
package:
- "aptos"
steps:
- uses: actions/checkout@v4
- uses: dtolnay/rust-toolchain@stable
with:
components: rustfmt, clippy
- uses: Swatinem/rust-cache@v2
# See '.cargo/config' for list of enabled/disabled clippy lints
- name: rustfmt
run: cargo fmt --all --check
working-directory: ${{ github.workspace }}/${{ matrix.package }}
- name: cargo clippy
run: cargo xclippy -D warnings
working-directory: ${{ github.workspace }}/${{ matrix.package }}
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
**/target
Cargo.lock
.idea
10 changes: 0 additions & 10 deletions .idea/.gitignore

This file was deleted.

15 changes: 15 additions & 0 deletions aptos/.cargo/config
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
[alias]
# Collection of project wide clippy lints. This is done via an alias because
# clippy doesn't currently allow for specifiying project-wide lints in a
# configuration file. This is a similar workaround to the ones presented here:
# <https://github.com/EmbarkStudios/rust-ecosystem/issues/59>
# TODO: add support for --all-features
xclippy = [
"clippy", "--all-targets", "--",
"-Wclippy::all",
"-Wclippy::disallowed_methods",
]

# This is necessary when building aptos-runtime: https://github.com/aptos-labs/aptos-core/pull/11889
[build]
rustflags = ["--cfg", "tokio_unstable"]
10 changes: 10 additions & 0 deletions aptos/.config/nextest.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
[profile.ci]
# Print out output for failing tests as soon as they fail, and also at the end
# of the run (for easy scrollability).
failure-output = "immediate-final"
# Show skipped tests in the CI output.
status-level = "skip"
# Do not cancel the test run on the first failure.
fail-fast = false
# Mark tests as slow after 5mins, kill them after 50
slow-timeout = { period = "300s", terminate-after = 10 }
78 changes: 78 additions & 0 deletions aptos/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
[package]
name = "aptos-lc"
version = "0.1.0"
edition = "2021"
license = "MIT OR Apache-2.0"
homepage = "https://github.com/wormhole-foundation/example-zk-light-clients"
repository = "https://github.com/wormhole-foundation/example-zk-light-clients"

[dependencies]
anyhow = "1.0.79"
bcs = { git = "https://github.com/aptos-labs/bcs.git", rev = "d31fab9d81748e2594be5cd5cdf845786a30562d" }
blst = "0.3.11"
bytes = { version = "1.5.0", features = ["serde"] }
getset = "0.1.2"
hex = "0.4.3"
itertools = "0.12.1"
proptest = "1.4.0"
serde = "1.0.193"
serde_bytes = "0.11.6"
serde_json = "1"
sha3 = "0.11.0-pre.3"
test-strategy = "0.3.1"
thiserror = "1.0.56"
tiny-keccak = { version = "2.0.2", features = ["sha3"] }

# shallow cloning would sure help here https://github.com/rust-lang/cargo/issues/1171
aptos-crypto = { git = "https://github.com/aptos-labs/aptos-core/", tag = "aptos-node-v1.10.0", optional = true }
aptos-executor = { git = "https://github.com/aptos-labs/aptos-core/", tag = "aptos-node-v1.10.0", optional = true }
aptos-executor-test-helpers = { git = "https://github.com/aptos-labs/aptos-core/", tag = "aptos-node-v1.10.0", optional = true }
aptos-executor-types = { git = "https://github.com/aptos-labs/aptos-core/", tag = "aptos-node-v1.10.0", optional = true }
aptos-sdk = { git = "https://github.com/aptos-labs/aptos-core/", tag = "aptos-node-v1.10.0", optional = true }
aptos-storage-interface = { git = "https://github.com/aptos-labs/aptos-core/", tag = "aptos-node-v1.10.0", optional = true }
aptos-temppath = { git = "https://github.com/aptos-labs/aptos-core/", tag = "aptos-node-v1.10.0", optional = true }
aptos-types = { git = "https://github.com/aptos-labs/aptos-core/", tag = "aptos-node-v1.10.0", optional = true }
aptos-vm = { git = "https://github.com/aptos-labs/aptos-core/", tag = "aptos-node-v1.10.0", optional = true }
aptos-vm-genesis = { git = "https://github.com/aptos-labs/aptos-core/", tag = "aptos-node-v1.10.0", optional = true }
move-core-types = { git = "https://github.com/aptos-labs/aptos-core/", tag = "aptos-node-v1.10.0", optional = true }

[dev-dependencies]
cfg-if = "1.0.0"
reqwest = { version = "0.11.23", features = ["blocking"] }

[target.'cfg(not(target_arch = "wasm32"))'.dev-dependencies]
criterion = { version = "0.5", features = ["html_reports"] }
pprof = { version = "0.13" }
# Versions to fit aptos crate
rand = "0.7.3"
rand_core = "0.5.1"

# Match the forks used by the aptos crates
[patch.crates-io]
merlin = { git = "https://github.com/aptos-labs/merlin" }
x25519-dalek = { git = "https://github.com/aptos-labs/x25519-dalek", branch = "zeroize_v1" }

[features]
default = []
aptos = [
"dep:aptos-crypto",
"dep:aptos-types",
"dep:aptos-executor-test-helpers",
"dep:aptos-temppath",
"dep:aptos-vm-genesis",
"dep:move-core-types",
"dep:aptos-sdk",
"dep:aptos-executor-types",
"dep:aptos-executor",
"dep:aptos-vm",
"dep:aptos-storage-interface",
]
flamegraph = ["pprof/flamegraph", "pprof/criterion"]

[profile.dev-ci]
inherits = "dev"
# By compiling dependencies with optimizations, performing tests gets much faster.
opt-level = 3
lto = "thin"
incremental = false
codegen-units = 16
1 change: 1 addition & 0 deletions aptos/rust-toolchain
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
stable
10 changes: 10 additions & 0 deletions aptos/src/crypto/error.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// SPDX-License-Identifier: Apache-2.0, MIT
use thiserror::Error;

#[derive(Debug, Error, PartialEq, Eq)]
pub enum CryptoError {
#[error("Failed to deserialize a valid BLS signature from received bytes")]
SignatureDeserializationError,
#[error("Failed to deserialize a valid public key from received bytes")]
PublicKeyDeserializationError,
}
142 changes: 142 additions & 0 deletions aptos/src/crypto/hash.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
// SPDX-License-Identifier: Apache-2.0, MIT
use anyhow::{anyhow, Result};
use getset::CopyGetters;
use serde::{Deserialize, Serialize};
use std::fmt;
use test_strategy::Arbitrary;
use tiny_keccak::{Hasher, Sha3};
pub const HASH_PREFIX: &[u8] = b"APTOS::";

pub trait CryptoHash {
/// Hashes the object and produces a `HashValue`.
fn hash(&self) -> HashValue;
}

pub fn prefixed_sha3(input: &[u8]) -> [u8; 32] {
let mut sha3 = Sha3::v256();
let salt: Vec<u8> = [HASH_PREFIX, input].concat();
sha3.update(&salt);
let mut output = [0u8; 32];
sha3.finalize(&mut output);
output
}

pub fn hash_data(tag: &[u8], data: Vec<&[u8]>) -> [u8; 32] {
let mut hasher = Sha3::v256();
if !tag.is_empty() {
hasher.update(tag);
}
for d in data {
hasher.update(d);
}
let mut output = [0u8; 32];
hasher.finalize(&mut output);
output
}

pub fn create_literal_hash(word: &str) -> HashValue {
let mut s = word.as_bytes().to_vec();
assert!(s.len() <= 32);
s.resize(32, 0);
HashValue::from_slice(&s).expect("Cannot fail")
}

#[derive(
Debug, Default, PartialEq, Eq, Deserialize, Serialize, Clone, Copy, Arbitrary, CopyGetters, Hash,
)]
pub struct HashValue {
#[getset(get_copy = "pub(crate)")]
hash: [u8; 32],
}

impl HashValue {
pub fn new(hash: [u8; 32]) -> Self {
HashValue { hash }
}

/// Create from a slice (e.g. retrieved from storage).
pub fn from_slice<T: AsRef<[u8]>>(bytes: T) -> Result<Self> {
<[u8; 32]>::try_from(bytes.as_ref())
.map_err(|e| anyhow!("Invalid length: {}", e))
.map(Self::new)
}

pub fn from_human_readable(hex: &str) -> Result<Self> {
let hex = hex.strip_prefix("0x").unwrap_or(hex);
let bytes = hex::decode(hex).unwrap();
Ok(HashValue::new(bytes.try_into().unwrap()))
}

/// Returns a `HashValueBitIterator` over all the bits that represent this `HashValue`.
pub fn iter_bits(&self) -> HashValueBitIterator<'_> {
HashValueBitIterator::new(self)
}

/// Dumps into a vector.
pub fn to_vec(&self) -> Vec<u8> {
self.hash.to_vec()
}
}

impl AsRef<[u8; 32]> for HashValue {
fn as_ref(&self) -> &[u8; 32] {
&self.hash
}
}

impl fmt::LowerHex for HashValue {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
if f.alternate() {
write!(f, "0x")?;
}
for byte in &self.hash {
write!(f, "{:02x}", byte)?;
}
Ok(())
}
}

pub struct HashValueBitIterator<'a> {
/// The reference to the bytes that represent the `HashValue`.
hash_bytes: &'a [u8],
pos: std::ops::Range<usize>,
// invariant hash_bytes.len() == HashValue::LENGTH;
// invariant pos.end == hash_bytes.len() * 8;
}

impl<'a> HashValueBitIterator<'a> {
/// Constructs a new `HashValueBitIterator` using given `HashValue`.
fn new(hash_value: &'a HashValue) -> Self {
HashValueBitIterator {
hash_bytes: hash_value.as_ref(),
pos: (0..256),
}
}

/// Returns the `index`-th bit in the bytes.
fn get_bit(&self, index: usize) -> bool {
debug_assert_eq!(self.hash_bytes.len(), 32); // invariant
debug_assert!(index < 256); // assumed precondition
let pos = index / 8;
let bit = 7 - index % 8;
(self.hash_bytes[pos] >> bit) & 1 != 0
}
}

impl<'a> Iterator for HashValueBitIterator<'a> {
type Item = bool;

fn next(&mut self) -> Option<Self::Item> {
self.pos.next().map(|x| self.get_bit(x))
}

fn size_hint(&self) -> (usize, Option<usize>) {
self.pos.size_hint()
}
}

impl<'a> DoubleEndedIterator for HashValueBitIterator<'a> {
fn next_back(&mut self) -> Option<Self::Item> {
self.pos.next_back().map(|x| self.get_bit(x))
}
}
4 changes: 4 additions & 0 deletions aptos/src/crypto/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
// SPDX-License-Identifier: Apache-2.0, MIT
mod error;
pub mod hash;
pub mod sig;
Loading

0 comments on commit 05b8156

Please sign in to comment.