Skip to content

Commit

Permalink
Merge branch 'main' into romac/store-proposed-value
Browse files Browse the repository at this point in the history
  • Loading branch information
romac committed Nov 21, 2024
2 parents ec67eca + fd05418 commit beb9a68
Show file tree
Hide file tree
Showing 18 changed files with 937 additions and 653 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.

78 changes: 42 additions & 36 deletions code/crates/starknet/host/src/block_store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,11 @@ use prost::Message;
use redb::ReadableTable;
use thiserror::Error;

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

use crate::codec::{decode_sync_block, encode_proposed_value, 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 @@ -25,27 +24,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::sync::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::sync::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 @@ -73,6 +59,8 @@ pub enum StoreError {
}

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

const PROPOSED_VALUE_TABLE: redb::TableDefinition<ProposedValueKey, Vec<u8>> =
redb::TableDefinition::new("proposed_values");
Expand All @@ -90,27 +78,44 @@ 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_decided_block(&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(())
}

fn insert_proposed_value(&self, value: ProposedValue<MockContext>) -> Result<(), StoreError> {
let key = (value.height, value.round, value.value);
let value = encode_proposed_value(&value)?;
let value = codec::encode_proposed_value(&value)?;
let tx = self.db.begin_write()?;
{
let mut table = tx.open_table(PROPOSED_VALUE_TABLE)?;
Expand Down Expand Up @@ -138,10 +143,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 @@ -166,8 +173,9 @@ impl Db {

fn create_tables(&self) -> Result<(), StoreError> {
let tx = self.db.begin_write()?;
// `open_table` implicitly creates the tables if needed
// Implicitly creates the tables if they do not exist yet
let _ = tx.open_table(BLOCK_TABLE)?;
let _ = tx.open_table(CERTIFICATE_TABLE)?;
let _ = tx.open_table(PROPOSED_VALUE_TABLE)?;
tx.commit()?;
Ok(())
Expand Down Expand Up @@ -205,12 +213,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
Loading

0 comments on commit beb9a68

Please sign in to comment.