Skip to content

Commit

Permalink
Merge branch 'main' into josef/bsync-tests
Browse files Browse the repository at this point in the history
  • Loading branch information
josef-widder committed Nov 20, 2024
2 parents 1f31f82 + 6a744fb commit 135a654
Show file tree
Hide file tree
Showing 12 changed files with 687 additions and 667 deletions.
22 changes: 18 additions & 4 deletions .github/workflows/coverage.yml
Original file line number Diff line number Diff line change
Expand Up @@ -43,28 +43,41 @@ jobs:
with:
toolchain: nightly
components: llvm-tools-preview
- name: Install cargo-nextest
uses: taiki-e/install-action@cargo-nextest
- name: Install cargo-llvm-cov
uses: taiki-e/install-action@cargo-llvm-cov
- name: Generate code coverage
run: |
cargo llvm-cov test \
cargo llvm-cov nextest \
--workspace \
--exclude malachite-test-mbt \
--ignore-filename-regex crates/cli \
--all-features \
--jobs 1 \
--no-capture \
--ignore-run-fail \
--lcov \
--output-path lcov.info
- name: Generate text report
run: cargo llvm-cov report
- name: Upload coverage to Codecov
uses: codecov/codecov-action@v3
uses: codecov/codecov-action@v4
if: ${{ !cancelled() }}
with:
token: ${{ secrets.CODECOV_TOKEN }}
files: code/lcov.info
flags: integration
fail_ci_if_error: false
url: https://codecov.informal.systems
- name: Upload test results to Codecov
if: ${{ !cancelled() }}
uses: codecov/test-results-action@v1
with:
token: ${{ secrets.CODECOV_TOKEN }}
file: code/target/nextest/default/junit.xml
flags: integration
fail_ci_if_error: false
url: https://codecov.informal.systems

mbt:
name: MBT
Expand Down Expand Up @@ -101,9 +114,10 @@ jobs:
- name: Generate text report
run: cargo llvm-cov report
- name: Upload coverage to Codecov
uses: codecov/codecov-action@v3
uses: codecov/codecov-action@v4
with:
token: ${{ secrets.CODECOV_TOKEN }}
files: code/lcov.info
flags: mbt
fail_ci_if_error: false
url: https://codecov.informal.systems
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ Unless required by applicable law or agreed to in writing, software distributed
[quint-link]: https://github.com/informalsystems/malachite/actions/workflows/quint.yml
[mbt-test-image]: https://github.com/informalsystems/malachite/actions/workflows/mbt.yml/badge.svg
[mbt-test-link]: https://github.com/informalsystems/malachite/actions/workflows/mbt.yml
[coverage-image]: https://codecov.io/gh/informalsystems/malachite/graph/badge.svg?token=B9KY7B6DJF
[coverage-link]: https://codecov.io/gh/informalsystems/malachite
[coverage-image]: https://codecov.informal.systems/gh/informalsystems/malachite/graph/badge.svg?token=LO0NSEJ9FC
[coverage-link]: https://codecov.informal.systems/gh/informalsystems/malachite
[license-image]: https://img.shields.io/badge/license-Apache_2.0-blue.svg
[license-link]: https://github.com/informalsystems/hermes/blob/master/LICENSE
[rustc-image]: https://img.shields.io/badge/Rust-stable-orange.svg
Expand Down
6 changes: 6 additions & 0 deletions code/.config/nextest.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[profile.default]
retries = 1
fail-fast = false

[profile.default.junit]
path = "junit.xml"
24 changes: 12 additions & 12 deletions code/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

76 changes: 41 additions & 35 deletions code/crates/starknet/host/src/block_store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,14 @@ use std::ops::RangeBounds;
use std::path::Path;
use std::sync::Arc;

use malachite_blocksync::SyncedBlock;
use malachite_common::CommitCertificate;
use malachite_proto::Protobuf;

use prost::Message;
use redb::ReadableTable;
use thiserror::Error;

use crate::codec::{decode_sync_block, encode_synced_block};
use crate::codec;
use crate::proto::{self as proto, Error as ProtoError};
use crate::types::MockContext;
use crate::types::{Block, Height, Transaction, Transactions};
Expand All @@ -21,27 +20,14 @@ pub struct DecidedBlock {
pub certificate: CommitCertificate<MockContext>,
}

impl DecidedBlock {
fn into_bytes(self) -> Result<Vec<u8>, ProtoError> {
let synced_block = SyncedBlock {
certificate: self.certificate.clone(),
block_bytes: self.block.to_bytes().unwrap(),
};

let proto = encode_synced_block(synced_block)?;
Ok(proto.encode_to_vec())
}

fn from_bytes(bytes: &[u8]) -> Option<Self> {
let synced_block = proto::blocksync::SyncedBlock::decode(bytes).ok()?;
let synced_block = decode_sync_block(synced_block).ok()?;
let block = Block::from_bytes(synced_block.block_bytes.as_ref()).ok()?;
fn decode_certificate(bytes: &[u8]) -> Result<CommitCertificate<MockContext>, ProtoError> {
let proto = proto::CommitCertificate::decode(bytes)?;
codec::decode_certificate(proto)
}

Some(Self {
block,
certificate: synced_block.certificate,
})
}
fn encode_certificate(certificate: CommitCertificate<MockContext>) -> Result<Vec<u8>, ProtoError> {
let proto = codec::encode_certificate(certificate)?;
Ok(proto.encode_to_vec())
}

#[derive(Debug, Error)]
Expand Down Expand Up @@ -112,6 +98,8 @@ impl redb::Key for HeightKey {
}

const BLOCK_TABLE: redb::TableDefinition<HeightKey, Vec<u8>> = redb::TableDefinition::new("blocks");
const CERTIFICATE_TABLE: redb::TableDefinition<HeightKey, Vec<u8>> =
redb::TableDefinition::new("certificates");

struct Db {
db: redb::Database,
Expand All @@ -126,21 +114,38 @@ impl Db {

fn get(&self, height: Height) -> Result<Option<DecidedBlock>, StoreError> {
let tx = self.db.begin_read()?;
let table = tx.open_table(BLOCK_TABLE)?;
let value = table.get(&height)?;
let block = value.and_then(|value| DecidedBlock::from_bytes(&value.value()));
Ok(block)
let block = {
let table = tx.open_table(BLOCK_TABLE)?;
let value = table.get(&height)?;
value.and_then(|value| Block::from_bytes(&value.value()).ok())
};
let certificate = {
let table = tx.open_table(CERTIFICATE_TABLE)?;
let value = table.get(&height)?;
value.and_then(|value| decode_certificate(&value.value()).ok())
};

let decided_block = block
.zip(certificate)
.map(|(block, certificate)| DecidedBlock { block, certificate });

Ok(decided_block)
}

fn insert(&self, decided_block: DecidedBlock) -> Result<(), StoreError> {
let height = decided_block.block.height;

let tx = self.db.begin_write()?;
{
let mut table = tx.open_table(BLOCK_TABLE)?;
table.insert(height, decided_block.into_bytes()?)?;
let mut blocks = tx.open_table(BLOCK_TABLE)?;
blocks.insert(height, decided_block.block.to_bytes()?.to_vec())?;
}
{
let mut certificates = tx.open_table(CERTIFICATE_TABLE)?;
certificates.insert(height, encode_certificate(decided_block.certificate)?)?;
}
tx.commit()?;

Ok(())
}

Expand All @@ -162,10 +167,12 @@ impl Db {
fn prune(&self, retain_height: Height) -> Result<Vec<Height>, StoreError> {
let tx = self.db.begin_write().unwrap();
let pruned = {
let mut table = tx.open_table(BLOCK_TABLE)?;
let keys = self.range(&table, ..retain_height)?;
let mut blocks = tx.open_table(BLOCK_TABLE)?;
let mut certificates = tx.open_table(CERTIFICATE_TABLE)?;
let keys = self.range(&blocks, ..retain_height)?;
for key in &keys {
table.remove(key)?;
blocks.remove(key)?;
certificates.remove(key)?;
}
keys
};
Expand All @@ -190,8 +197,9 @@ impl Db {

fn create_tables(&self) -> Result<(), StoreError> {
let tx = self.db.begin_write()?;
// Implicitly creates the "blocks" table if it does not exists
// Implicitly creates the tables if they do not exist yet
let _ = tx.open_table(BLOCK_TABLE)?;
let _ = tx.open_table(CERTIFICATE_TABLE)?;
tx.commit()?;
Ok(())
}
Expand Down Expand Up @@ -228,12 +236,10 @@ impl BlockStore {
certificate: &CommitCertificate<MockContext>,
txes: &[Transaction],
) -> Result<(), StoreError> {
let block_id = certificate.value_id;

let decided_block = DecidedBlock {
block: Block {
height: certificate.height,
block_hash: block_id,
block_hash: certificate.value_id,
transactions: Transactions::new(txes.to_vec()),
},
certificate: certificate.clone(),
Expand Down
4 changes: 2 additions & 2 deletions code/crates/starknet/host/src/codec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ impl NetworkCodec<blocksync::Response<MockContext>> for ProtobufCodec {

Ok(blocksync::Response {
height: Height::new(response.block_number, response.fork_id),
block: response.block.map(decode_sync_block).transpose()?,
block: response.block.map(decode_synced_block).transpose()?,
})
}

Expand Down Expand Up @@ -309,7 +309,7 @@ pub(crate) fn decode_certificate(
Ok(certificate)
}

pub(crate) fn decode_sync_block(
pub(crate) fn decode_synced_block(
synced_block: proto::blocksync::SyncedBlock,
) -> Result<blocksync::SyncedBlock<MockContext>, ProtoError> {
let certificate = if let Some(certificate) = synced_block.certificate {
Expand Down
Loading

0 comments on commit 135a654

Please sign in to comment.