Skip to content

Commit

Permalink
Fixup
Browse files Browse the repository at this point in the history
  • Loading branch information
Eduardo Leegwater Simões committed Apr 12, 2024
1 parent e4b6c85 commit efa80e0
Show file tree
Hide file tree
Showing 7 changed files with 391 additions and 206 deletions.
1 change: 1 addition & 0 deletions rusk/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ serde_json = "1"
serde_with = { version = "3.1", features = ["hex"] }
humantime-serde = "1"
bs58 = "0.4"
base64 = "0.22"
hex = "0.4"
parking_lot = "0.12"
rkyv = { version = "0.7", default-features = false, features = ["size_32"] }
Expand Down
2 changes: 1 addition & 1 deletion rusk/src/bin/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,6 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
None => None,
};

// TODO: Pass event sender to the services that need it
let channel_cap = config.http.ws_event_channel_cap;
let (event_sender, event_receiver) = broadcast::channel(channel_cap);

Expand All @@ -107,6 +106,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
state_dir,
config.chain.generation_timeout(),
config.http.feeder_call_gas,
event_sender,
)?;

info!("Rusk VM loaded");
Expand Down
4 changes: 4 additions & 0 deletions rusk/src/lib/chain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,15 @@ use std::sync::Arc;
use std::time::Duration;

use parking_lot::RwLock;
use tokio::sync::broadcast;

use node::database::rocksdb::Backend;
use node::network::Kadcast;
use rusk_abi::dusk::{dusk, Dusk};
use rusk_abi::VM;

use crate::http::ContractEvent;

pub const MINIMUM_STAKE: Dusk = dusk(1000.0);

#[derive(Debug, Clone, Copy)]
Expand All @@ -33,6 +36,7 @@ pub struct Rusk {
dir: PathBuf,
pub(crate) generation_timeout: Option<Duration>,
pub(crate) feeder_gas_limit: u64,
pub(crate) event_sender: broadcast::Sender<ContractEvent>,
}

#[derive(Clone)]
Expand Down
29 changes: 25 additions & 4 deletions rusk/src/lib/chain/rusk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,10 @@ use rusk_abi::{
STAKE_CONTRACT, TRANSFER_CONTRACT, VM,
};
use rusk_profile::to_rusk_state_id_path;
use tokio::sync::broadcast;

use super::{coinbase_value, emission_amount, Rusk, RuskTip};
use crate::http::ContractEvent;
use crate::{Error, Result};

pub static DUSK_KEY: LazyLock<BlsPublicKey> = LazyLock::new(|| {
Expand All @@ -42,6 +44,7 @@ impl Rusk {
dir: P,
generation_timeout: Option<Duration>,
feeder_gas_limit: u64,
event_sender: broadcast::Sender<ContractEvent>,
) -> Result<Self> {
let dir = dir.as_ref();
let commit_id_path = to_rusk_state_id_path(dir);
Expand Down Expand Up @@ -73,6 +76,7 @@ impl Rusk {
dir: dir.into(),
generation_timeout,
feeder_gas_limit,
event_sender,
})
}

Expand Down Expand Up @@ -200,7 +204,7 @@ impl Rusk {
txs,
missed_generators,
)
.map(|(a, b, _)| (a, b))
.map(|(a, b, _, _)| (a, b))
}

/// Accept the given transactions.
Expand All @@ -220,7 +224,7 @@ impl Rusk {
) -> Result<(Vec<SpentTransaction>, VerificationOutput)> {
let session = self.session(block_height, None)?;

let (spent_txs, verification_output, session) = accept(
let (spent_txs, verification_output, session, events) = accept(
session,
block_height,
block_gas_limit,
Expand All @@ -239,6 +243,10 @@ impl Rusk {

self.set_current_commit(session.commit()?);

for event in events {
let _ = self.event_sender.send(event.into());
}

Ok((spent_txs, verification_output))
}

Expand All @@ -259,7 +267,7 @@ impl Rusk {
) -> Result<(Vec<SpentTransaction>, VerificationOutput)> {
let session = self.session(block_height, None)?;

let (spent_txs, verification_output, session) = accept(
let (spent_txs, verification_output, session, events) = accept(
session,
block_height,
block_gas_limit,
Expand All @@ -282,6 +290,10 @@ impl Rusk {
let commit_id_path = to_rusk_state_id_path(&self.dir);
fs::write(commit_id_path, commit)?;

for event in events {
let _ = self.event_sender.send(event.into());
}

Ok((spent_txs, verification_output))
}

Expand Down Expand Up @@ -435,21 +447,29 @@ fn accept(
generator: &BlsPublicKey,
txs: &[Transaction],
missed_generators: &[BlsPublicKey],
) -> Result<(Vec<SpentTransaction>, VerificationOutput, Session)> {
) -> Result<(
Vec<SpentTransaction>,
VerificationOutput,
Session,
Vec<Event>,
)> {
let mut session = session;

let mut block_gas_left = block_gas_limit;

let mut spent_txs = Vec::with_capacity(txs.len());
let mut dusk_spent = 0;

let mut events = Vec::new();
let mut event_hasher = Sha3_256::new();

for unspent_tx in txs {
let tx = &unspent_tx.inner;
let receipt = execute(&mut session, tx)?;

update_hasher(&mut event_hasher, &receipt.events);
events.extend(receipt.events);

let gas_spent = receipt.gas_spent;

dusk_spent += gas_spent * tx.fee.gas_price;
Expand Down Expand Up @@ -485,6 +505,7 @@ fn accept(
event_hash,
},
session,
events,
))
}

Expand Down
Loading

0 comments on commit efa80e0

Please sign in to comment.