Skip to content

Commit

Permalink
Changes to enable light-client wasm compilation (#1256)
Browse files Browse the repository at this point in the history
- Moved around the different network Topics to their respective crates
- Consensus no longer depends upon mempool
- Remove a dependency of account from blockchain interface

The idea of these changes is to allow the light client to compile by:
* Not requiring accounts (which depends upon the db)
* Not requiring  mempool (which is not needed for the light client)
  • Loading branch information
viquezclaudio authored Jan 16, 2023
1 parent 418d78f commit d33a014
Show file tree
Hide file tree
Showing 16 changed files with 85 additions and 78 deletions.
3 changes: 1 addition & 2 deletions Cargo.lock

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

1 change: 0 additions & 1 deletion blockchain-interface/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ parking_lot = { git = "https://github.com/styppo/parking_lot.git" }
thiserror = "1.0"
tokio-stream = { version = "0.1", features = ["sync"] }

nimiq-account = { path = "../primitives/account" }
nimiq-block = { path = "../primitives/block" }
nimiq-database-value = { path = "../database/database-value" }
nimiq-hash = { path = "../hash" }
Expand Down
5 changes: 2 additions & 3 deletions blockchain-interface/src/error.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use thiserror::Error;

use nimiq_account::AccountError;
use nimiq_block::{Block, BlockError, ForkProof};
use nimiq_hash::Blake2bHash;
use nimiq_primitives::networks::NetworkId;
Expand Down Expand Up @@ -84,8 +83,8 @@ pub enum PushError {
InvalidPredecessor,
#[error("Duplicate transaction")]
DuplicateTransaction,
#[error("Account error: {0}")]
AccountsError(#[from] AccountError),
#[error("Account error")]
AccountsError,
#[error("Invalid fork")]
InvalidFork,
#[error("Blockchain error: {0}")]
Expand Down
8 changes: 6 additions & 2 deletions blockchain/src/blockchain/accounts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,9 @@ impl Blockchain {

// Check if the receipts contain an error.
if let Err(e) = batch_info {
return Err(PushError::AccountsError(e));
// TODO: This is due to a dependency between blockchain and accounts that is not wasm friendly
log::error!("Received accounts error while committing accounts: {}", e);
return Err(PushError::AccountsError);
}

// Macro blocks are final and receipts for the previous batch are no longer necessary
Expand Down Expand Up @@ -99,7 +101,9 @@ impl Blockchain {
Ok(batch_info) => batch_info,
Err(e) => {
// Check if the receipts contain an error.
return Err(PushError::AccountsError(e));
// TODO: This is due to a dependency between blockchain and accounts that is not wasm friendly
log::error!("Received accounts error while committing accounts: {}", e);
return Err(PushError::AccountsError);
}
};

Expand Down
7 changes: 6 additions & 1 deletion blockchain/src/blockchain/history_sync.rs
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,12 @@ impl Blockchain {
txn.abort();
#[cfg(feature = "metrics")]
this.metrics.note_invalid_block();
return Err(PushError::AccountsError(e));
// TODO: This is due to a dependency between blockchain and accounts that is not wasm friendly
log::error!(
"Received accounts error while extending history sync: {}",
e,
);
return Err(PushError::AccountsError);
}
}
this.state.accounts.finalize_batch(&mut txn);
Expand Down
1 change: 0 additions & 1 deletion consensus/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ nimiq-hash = { path = "../hash" }
nimiq-keys = { path = "../keys" }
nimiq-light-blockchain = { path = "../light-blockchain" }
nimiq-macros = { path = "../macros" }
nimiq-mempool = { path = "../mempool" }
nimiq-nano-primitives = { path = "../nano-primitives" }
nimiq-network-interface = { path = "../network-interface" }
nimiq-primitives = { path = "../primitives", features = ["policy"] }
Expand Down
3 changes: 1 addition & 2 deletions consensus/src/consensus/consensus_proxy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,9 @@ use tokio::sync::broadcast::Sender as BroadcastSender;
use tokio_stream::wrappers::BroadcastStream;

use nimiq_blockchain_proxy::BlockchainProxy;
use nimiq_mempool::mempool::{ControlTransactionTopic, TransactionTopic};
use nimiq_network_interface::network::Network;
use nimiq_primitives::account::AccountType;
use nimiq_transaction::Transaction;
use nimiq_transaction::{ControlTransactionTopic, Transaction, TransactionTopic};

use crate::ConsensusEvent;

Expand Down
4 changes: 2 additions & 2 deletions consensus/src/sync/live/block_live_sync.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use parking_lot::Mutex;
use pin_project::pin_project;
use tokio::task::spawn_blocking;

use nimiq_block::Block;
use nimiq_block::{Block, BlockHeaderTopic, BlockTopic};
#[cfg(feature = "full")]
use nimiq_blockchain::Blockchain;
use nimiq_blockchain_interface::{PushError, PushResult};
Expand All @@ -20,7 +20,7 @@ use nimiq_network_interface::network::{MsgAcceptance, Network};

use crate::sync::{
live::{
block_queue::{BlockHeaderTopic, BlockQueue, BlockTopic, QueuedBlock},
block_queue::{BlockQueue, QueuedBlock},
request_component::RequestComponent,
},
syncer::{LiveSync, LiveSyncEvent, LiveSyncPeerEvent, LiveSyncPushEvent},
Expand Down
26 changes: 2 additions & 24 deletions consensus/src/sync/live/block_queue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,37 +8,15 @@ use std::{
use futures::stream::BoxStream;
use futures::{Stream, StreamExt};

use nimiq_block::Block;
use nimiq_block::{Block, BlockHeaderTopic, BlockTopic};
use nimiq_blockchain_interface::{AbstractBlockchain, Direction};
use nimiq_blockchain_proxy::BlockchainProxy;
use nimiq_hash::Blake2bHash;
use nimiq_network_interface::network::{MsgAcceptance, Network, PubsubId, Topic};
use nimiq_network_interface::network::{MsgAcceptance, Network, PubsubId};
use nimiq_primitives::policy::Policy;

use crate::sync::live::request_component::{RequestComponent, RequestComponentEvent};

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

impl Topic for BlockTopic {
type Item = Block;

const BUFFER_SIZE: usize = 16;
const NAME: &'static str = "blocks";
const VALIDATE: bool = true;
}

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

impl Topic for BlockHeaderTopic {
type Item = Block;

const BUFFER_SIZE: usize = 16;
const NAME: &'static str = "block-headers";
const VALIDATE: bool = true;
}

pub type BlockStream<N> = BoxStream<'static, (Block, <N as Network>::PubsubId)>;

type BlockAndId<N> = (Block, Option<<N as Network>::PubsubId>);
Expand Down
26 changes: 1 addition & 25 deletions mempool/src/mempool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ use nimiq_primitives::coin::Coin;
use nimiq_transaction::account::staking_contract::{
IncomingStakingTransactionData, OutgoingStakingTransactionProof,
};
use nimiq_transaction::Transaction;
use nimiq_transaction::{ControlTransactionTopic, Transaction, TransactionTopic};

use crate::config::MempoolConfig;
use crate::executor::MempoolExecutor;
Expand All @@ -29,30 +29,6 @@ use crate::mempool_state::{EvictionReason, MempoolState};
use crate::mempool_transactions::TxPriority;
use crate::verify::{verify_tx, VerifyErr};

/// Transaction topic for the Mempool to request transactions from the network
#[derive(Clone, Debug, Default)]
pub struct TransactionTopic;

impl Topic for TransactionTopic {
type Item = Transaction;

const BUFFER_SIZE: usize = 1024;
const NAME: &'static str = "transactions";
const VALIDATE: bool = true;
}

/// Control Transaction topic for the Mempool to request transactions from the network
#[derive(Clone, Debug, Default)]
pub struct ControlTransactionTopic;

impl Topic for ControlTransactionTopic {
type Item = Transaction;

const BUFFER_SIZE: usize = 1024;
const NAME: &'static str = "Controltransactions";
const VALIDATE: bool = true;
}

/// Struct defining the Mempool
pub struct Mempool {
/// Blockchain reference
Expand Down
24 changes: 24 additions & 0 deletions primitives/block/src/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use nimiq_database_value::{FromDatabaseValue, IntoDatabaseValue};
use nimiq_hash::{Blake2bHash, Blake2sHash, Hash, SerializeContent};
use nimiq_hash_derive::SerializeContent;
use nimiq_keys::PublicKey;
use nimiq_network_interface::network::Topic;
use nimiq_primitives::coin::Coin;
use nimiq_primitives::policy::Policy;
use nimiq_primitives::slots::Validators;
Expand All @@ -19,6 +20,29 @@ use crate::macro_block::{MacroBlock, MacroHeader};
use crate::micro_block::{MicroBlock, MicroHeader};
use crate::{BlockError, MacroBody, MicroBody, MicroJustification, TendermintProof};

/// These network topics are used to subscribe and request Blocks and Block Headers respectively
#[derive(Clone, Debug, Default)]
pub struct BlockTopic;

impl Topic for BlockTopic {
type Item = Block;

const BUFFER_SIZE: usize = 16;
const NAME: &'static str = "blocks";
const VALIDATE: bool = true;
}

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

impl Topic for BlockHeaderTopic {
type Item = Block;

const BUFFER_SIZE: usize = 16;
const NAME: &'static str = "block-headers";
const VALIDATE: bool = true;
}

/// Defines the type of the block, either Micro or Macro (which includes both checkpoint and
/// election blocks).
#[derive(Clone, Copy, Debug, Eq, PartialEq, Ord, PartialOrd, Hash, Serialize, Deserialize)]
Expand Down
12 changes: 12 additions & 0 deletions primitives/block/src/tendermint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use beserial::{Deserialize, Serialize};
use nimiq_bls::AggregatePublicKey;
use nimiq_hash::{Blake2sHash, Hash, SerializeContent};
use nimiq_hash_derive::SerializeContent;
use nimiq_network_interface::network::Topic;
use nimiq_primitives::policy::Policy;
use nimiq_primitives::slots::Validators;

Expand All @@ -14,6 +15,17 @@ use crate::signed::{
};
use crate::{MacroBlock, MacroHeader, MultiSignature};

/// This topic is used to obtain tendermint proposals through the network
pub struct ProposalTopic;

impl Topic for ProposalTopic {
type Item = SignedTendermintProposal;

const BUFFER_SIZE: usize = 8;
const NAME: &'static str = "tendermint-proposal";
const VALIDATE: bool = true;
}

/// The proposal message sent by the Tendermint leader.
#[derive(Clone, Debug, Serialize, Deserialize, SerializeContent, PartialEq, Eq)]
pub struct TendermintProposal {
Expand Down
1 change: 1 addition & 0 deletions primitives/transaction/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ nimiq-bls = { path = "../../bls", features = ["serde-derive"] }
nimiq-hash = { path = "../../hash", features = ["serde-derive"] }
nimiq-keys = { path = "../../keys", features = ["serde-derive"] }
nimiq-macros = { path = "../../macros" }
nimiq-network-interface = { path = "../../network-interface" }
nimiq-primitives = { path = "..", features = ["account", "coin", "networks", "policy", "serde-derive"] }
nimiq-utils = { path = "../../utils", features = ["merkle"] }

Expand Down
24 changes: 24 additions & 0 deletions primitives/transaction/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ use beserial::{
use nimiq_hash::{Blake2bHash, Hash, SerializeContent};
use nimiq_keys::Address;
use nimiq_keys::{PublicKey, Signature};
use nimiq_network_interface::network::Topic;
use nimiq_primitives::account::AccountType;
use nimiq_primitives::coin::Coin;
use nimiq_primitives::networks::NetworkId;
Expand All @@ -27,6 +28,29 @@ use crate::account::AccountTransactionVerification;

pub mod account;

/// Transaction topic for the Mempool to request transactions from the network
#[derive(Clone, Debug, Default)]
pub struct TransactionTopic;

impl Topic for TransactionTopic {
type Item = Transaction;

const BUFFER_SIZE: usize = 1024;
const NAME: &'static str = "transactions";
const VALIDATE: bool = true;
}

/// Control Transaction topic for the Mempool to request control transactions from the network
#[derive(Clone, Debug, Default)]
pub struct ControlTransactionTopic;

impl Topic for ControlTransactionTopic {
type Item = Transaction;

const BUFFER_SIZE: usize = 1024;
const NAME: &'static str = "Controltransactions";
const VALIDATE: bool = true;
}
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct TransactionsProof {
#[beserial(len_type(u16))]
Expand Down
5 changes: 2 additions & 3 deletions validator/src/tendermint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ use futures::{future::BoxFuture, stream::BoxStream, FutureExt, StreamExt};
use parking_lot::RwLock;

use nimiq_block::{
Block, MacroBlock, MacroBody, MacroHeader, MultiSignature, SignedTendermintProposal,
TendermintProof, TendermintProposal,
Block, MacroBlock, MacroBody, MacroHeader, MultiSignature, ProposalTopic,
SignedTendermintProposal, TendermintProof, TendermintProposal,
};
use nimiq_block_production::BlockProducer;
use nimiq_blockchain::Blockchain;
Expand All @@ -26,7 +26,6 @@ use nimiq_validator_network::ValidatorNetwork;
use nimiq_vrf::VrfSeed;

use crate::aggregation::tendermint::HandelTendermintAdapter;
use crate::validator::ProposalTopic;

/// The struct that interfaces with the Tendermint crate. It only has to implement the
/// TendermintOutsideDeps trait in order to do this.
Expand Down
13 changes: 1 addition & 12 deletions validator/src/validator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,11 @@ use crate::micro::{ProduceMicroBlock, ProduceMicroBlockEvent};
use crate::r#macro::{PersistedMacroState, ProduceMacroBlock};
use crate::slash::ForkProofPool;
use nimiq_account::StakingContract;
use nimiq_block::{Block, BlockType, SignedTendermintProposal};
use nimiq_block::{Block, BlockHeaderTopic, BlockTopic, BlockType, ProposalTopic};
use nimiq_block_production::BlockProducer;
use nimiq_blockchain::Blockchain;
use nimiq_blockchain_interface::{AbstractBlockchain, BlockchainEvent, ForkEvent, PushResult};
use nimiq_bls::KeyPair as BlsKeyPair;
use nimiq_consensus::sync::live::block_queue::{BlockHeaderTopic, BlockTopic};
use nimiq_consensus::{Consensus, ConsensusEvent, ConsensusProxy};
use nimiq_database::{Database, Environment, ReadTransaction, WriteTransaction};
use nimiq_hash::{Blake2bHash, Hash};
Expand All @@ -37,16 +36,6 @@ use nimiq_tendermint::TendermintReturn;
use nimiq_transaction_builder::TransactionBuilder;
use nimiq_validator_network::ValidatorNetwork;

pub struct ProposalTopic;

impl Topic for ProposalTopic {
type Item = SignedTendermintProposal;

const BUFFER_SIZE: usize = 8;
const NAME: &'static str = "tendermint-proposal";
const VALIDATE: bool = true;
}

#[derive(PartialEq)]
enum ValidatorStakingState {
Active,
Expand Down

0 comments on commit d33a014

Please sign in to comment.