Skip to content

Commit

Permalink
feat(malachine): set up context boilerplate
Browse files Browse the repository at this point in the history
  • Loading branch information
Trantorian1 committed Jan 24, 2025
1 parent 81ad1f2 commit 25ae91d
Show file tree
Hide file tree
Showing 11 changed files with 421 additions and 0 deletions.
31 changes: 31 additions & 0 deletions Cargo.lock

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

7 changes: 7 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ members = [
"crates/madara/tests",
"crates/madara/cairo-test-contracts",
"crates/madara/client/block_production",
"crates/madara/primitives/malachite",
]
resolver = "2"
# Everything except test-related packages, so that they are not compiled when doing `cargo build`.
Expand Down Expand Up @@ -266,6 +267,12 @@ flate2 = "1.0"
regex = "1.10.5"
sha3 = "0.10"

# Starknet consensus

[workspace.dependencies.malachite-core-types]
git = "https://github.com/informalsystems/malachite.git"
package = "informalsystems-malachitebft-core-types"

[patch.crates-io]
rocksdb = { git = "https://github.com/madara-alliance/rust-rocksdb", branch = "read-options-set-raw-snapshot" }
librocksdb-sys = { git = "https://github.com/madara-alliance/rust-rocksdb", branch = "read-options-set-raw-snapshot" }
Expand Down
5 changes: 5 additions & 0 deletions crates/madara/primitives/block/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ workspace = true
[package.metadata.docs.rs]
targets = ["x86_64-unknown-linux-gnu"]

[features]
default = []
testing = []

[dependencies]

# Madara
Expand All @@ -25,6 +29,7 @@ mp-transactions = { workspace = true }
blockifier = { workspace = true }
starknet-types-core = { workspace = true }
starknet-types-rpc = { workspace = true }
malachite-core-types.workspace = true

# Other
primitive-types.workspace = true
Expand Down
32 changes: 32 additions & 0 deletions crates/madara/primitives/block/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,7 @@ impl MadaraMaybePendingBlock {
}

/// Starknet block definition.
#[cfg_attr(any(test, feature = "testing"), derive(PartialEq))]
#[derive(Clone, Debug, serde::Serialize, serde::Deserialize)]
pub struct MadaraBlock {
pub info: MadaraBlockInfo,
Expand All @@ -219,6 +220,37 @@ impl MadaraBlock {
}
}

/// For testing we use a more rigorous impl of [PartialEq] which doesn't just
/// check the block hash.
#[cfg(not(any(test, feature = "testing")))]
impl PartialEq for MadaraBlock {
fn eq(&self, other: &Self) -> bool {
self.info.block_hash == other.info.block_hash
}
}

impl Eq for MadaraBlock {}

impl Ord for MadaraBlock {
fn cmp(&self, other: &Self) -> std::cmp::Ordering {
self.info.header.block_number.cmp(&other.info.header.block_number)
}
}

impl PartialOrd for MadaraBlock {
fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
Some(self.cmp(other))
}
}

impl malachite_core_types::Value for MadaraBlock {
type Id = Felt;

fn id(&self) -> Self::Id {
self.info.block_hash
}
}

/// Starknet block definition.
#[derive(Clone, Debug, serde::Serialize, serde::Deserialize)]
pub struct MadaraPendingBlock {
Expand Down
20 changes: 20 additions & 0 deletions crates/madara/primitives/malachite/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
[package]
name = "malachite"
authors.workspace = true
homepage.workspace = true
edition.workspace = true
repository.workspace = true
version.workspace = true
license.workspace = true

[dependencies]

# Madara
mp-block.workspace = true

# Starknet
malachite-core-types.workspace = true
starknet-types-core.workspace = true

[lints]
workspace = true
65 changes: 65 additions & 0 deletions crates/madara/primitives/malachite/src/context.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
use mp_block::MadaraBlock;

use crate::{
proposal::{ProposalPartStub, ProposalStub},
types::{Address, Height},
validators::{ValidatorSet, ValidatorStub},
vote::{SigningProviderStub, SigningSchemeStub, VoteStub},
};

#[derive(Clone, Debug)]
pub struct MadaraContext;

impl malachite_core_types::Context for MadaraContext {
type Address = Address;
type Height = Height;
type ProposalPart = ProposalPartStub;
type Proposal = ProposalStub;
type Validator = ValidatorStub;
type ValidatorSet = ValidatorSet;
type Value = MadaraBlock;
type Vote = VoteStub;
type SigningScheme = SigningSchemeStub;
type SigningProvider = SigningProviderStub;

fn select_proposer<'a>(
&self,
validator_set: &'a Self::ValidatorSet,
height: Self::Height,
round: malachite_core_types::Round,
) -> &'a Self::Validator {
todo!()
}

fn signing_provider(&self) -> &Self::SigningProvider {
todo!()
}

fn new_proposal(
height: Self::Height,
round: malachite_core_types::Round,
value: Self::Value,
pol_round: malachite_core_types::Round,
address: Self::Address,
) -> Self::Proposal {
todo!()
}

fn new_prevote(
height: Self::Height,
round: malachite_core_types::Round,
value_id: malachite_core_types::NilOrVal<malachite_core_types::ValueId<Self>>,
address: Self::Address,
) -> Self::Vote {
todo!()
}

fn new_precommit(
height: Self::Height,
round: malachite_core_types::Round,
value_id: malachite_core_types::NilOrVal<malachite_core_types::ValueId<Self>>,
address: Self::Address,
) -> Self::Vote {
todo!()
}
}
5 changes: 5 additions & 0 deletions crates/madara/primitives/malachite/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
mod context;
mod proposal;
mod types;
mod validators;
mod vote;
48 changes: 48 additions & 0 deletions crates/madara/primitives/malachite/src/proposal.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
use mp_block::MadaraBlock;

use crate::{
context::MadaraContext,
types::{Address, Height},
};

#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord)]
pub struct ProposalPartStub;

impl malachite_core_types::ProposalPart<MadaraContext> for ProposalPartStub {
fn is_first(&self) -> bool {
unimplemented!()
}

fn is_last(&self) -> bool {
unimplemented!()
}
}

#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord)]
pub struct ProposalStub;

impl malachite_core_types::Proposal<MadaraContext> for ProposalStub {
fn height(&self) -> Height {
todo!()
}

fn round(&self) -> malachite_core_types::Round {
todo!()
}

fn value(&self) -> &MadaraBlock {
todo!()
}

fn take_value(self) -> MadaraBlock {
todo!()
}

fn pol_round(&self) -> malachite_core_types::Round {
todo!()
}

fn validator_address(&self) -> &Address {
todo!()
}
}
37 changes: 37 additions & 0 deletions crates/madara/primitives/malachite/src/types.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
use starknet_types_core::felt::Felt;

#[repr(transparent)]
#[derive(Clone, Copy, Default, Debug, Eq, PartialEq, Ord, PartialOrd)]
pub struct Height(u64);

impl std::fmt::Display for Height {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("Height").field("at", &self.0).finish()
}
}

impl malachite_core_types::Height for Height {
fn increment_by(&self, n: u64) -> Self {
Self(self.0.saturating_add(n))
}

fn decrement_by(&self, n: u64) -> Option<Self> {
self.0.checked_sub(n).map(Self)
}

fn as_u64(&self) -> u64 {
self.0
}
}

#[repr(transparent)]
#[derive(Clone, Copy, Default, Debug, Eq, PartialEq, Ord, PartialOrd)]
pub struct Address(Felt);

impl std::fmt::Display for Address {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("Address").field("at", &self.0).finish()
}
}

impl malachite_core_types::Address for Address {}
44 changes: 44 additions & 0 deletions crates/madara/primitives/malachite/src/validators.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
use malachite_core_types::Validator as _;
use std::collections::BTreeMap;

use crate::{context::MadaraContext, types::Address};

#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord)]
pub struct ValidatorStub;

impl malachite_core_types::Validator<MadaraContext> for ValidatorStub {
fn address(&self) -> &Address {
todo!()
}

fn public_key(&self) -> &malachite_core_types::PublicKey<MadaraContext> {
todo!()
}

fn voting_power(&self) -> malachite_core_types::VotingPower {
todo!()
}
}

#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord)]
pub struct ValidatorSet {
validators: BTreeMap<Address, ValidatorStub>,
}

impl malachite_core_types::ValidatorSet<MadaraContext> for ValidatorSet {
fn count(&self) -> usize {
self.validators.len()
}

fn total_voting_power(&self) -> malachite_core_types::VotingPower {
self.validators.values().fold(0, |acc, v| acc + v.voting_power())
}

fn get_by_address(&self, address: &Address) -> Option<&ValidatorStub> {
self.validators.get(address)
}

fn get_by_index(&self, index: usize) -> Option<&ValidatorStub> {
self.validators.values().take(index).last()
}
}
Loading

0 comments on commit 25ae91d

Please sign in to comment.