Skip to content

Commit

Permalink
rusk: change block processing to slash failed iterations provisioner
Browse files Browse the repository at this point in the history
  • Loading branch information
herr-seppia committed Jan 17, 2024
1 parent f984096 commit d1eebb0
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 4 deletions.
40 changes: 36 additions & 4 deletions rusk/src/lib/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ impl Rusk {
block_gas_limit: u64,
generator: &BlsPublicKey,
txs: I,
missed_generators: &[BlsPublicKey],
) -> Result<(Vec<SpentTransaction>, Vec<Transaction>, VerificationOutput)>
{
let inner = self.inner.lock();
Expand Down Expand Up @@ -179,11 +180,12 @@ impl Rusk {
}
}

reward_and_update_root(
reward_slash_and_update_root(
&mut session,
block_height,
dusk_spent,
generator,
missed_generators,
)?;

let state_root = session.root();
Expand All @@ -206,14 +208,22 @@ impl Rusk {
block_gas_limit: u64,
generator: &BlsPublicKey,
txs: &[Transaction],
missed_generators: &[BlsPublicKey],
) -> Result<(Vec<SpentTransaction>, VerificationOutput)> {
let inner = self.inner.lock();

let current_commit = inner.current_commit;
let mut session =
rusk_abi::new_session(&inner.vm, current_commit, block_height)?;

accept(&mut session, block_height, block_gas_limit, generator, txs)
accept(
&mut session,
block_height,
block_gas_limit,
generator,
txs,
missed_generators,
)
}

/// Accept the given transactions.
Expand All @@ -228,6 +238,7 @@ impl Rusk {
generator: BlsPublicKey,
txs: Vec<Transaction>,
consistency_check: Option<VerificationOutput>,
missed_generators: &[BlsPublicKey],
) -> Result<(Vec<SpentTransaction>, VerificationOutput)> {
let mut inner = self.inner.lock();

Expand All @@ -241,6 +252,7 @@ impl Rusk {
block_gas_limit,
&generator,
&txs[..],
missed_generators,
)?;

if let Some(expected_verification) = consistency_check {
Expand Down Expand Up @@ -269,6 +281,7 @@ impl Rusk {
generator: BlsPublicKey,
txs: Vec<Transaction>,
consistency_check: Option<VerificationOutput>,
missed_generators: &[BlsPublicKey],
) -> Result<(Vec<SpentTransaction>, VerificationOutput)> {
let mut inner = self.inner.lock();

Expand All @@ -282,6 +295,7 @@ impl Rusk {
block_gas_limit,
&generator,
&txs[..],
missed_generators,
)?;

if let Some(expected_verification) = consistency_check {
Expand Down Expand Up @@ -610,6 +624,7 @@ fn accept(
block_gas_limit: u64,
generator: &BlsPublicKey,
txs: &[Transaction],
missed_generators: &[BlsPublicKey],
) -> Result<(Vec<SpentTransaction>, VerificationOutput)> {
let mut block_gas_left = block_gas_limit;

Expand Down Expand Up @@ -641,7 +656,13 @@ fn accept(
});
}

reward_and_update_root(session, block_height, dusk_spent, generator)?;
reward_slash_and_update_root(
session,
block_height,
dusk_spent,
generator,
missed_generators,
)?;

let state_root = session.root();
let event_hash = event_hasher.finalize().into();
Expand Down Expand Up @@ -707,11 +728,12 @@ fn update_hasher(hasher: &mut Sha3_256, event: Event) {
hasher.update(event.data);
}

fn reward_and_update_root(
fn reward_slash_and_update_root(
session: &mut Session,
block_height: u64,
dusk_spent: Dusk,
generator: &BlsPublicKey,
slashing: &[BlsPublicKey],
) -> Result<()> {
let (dusk_value, generator_value) =
coinbase_value(block_height, dusk_spent);
Expand All @@ -728,6 +750,16 @@ fn reward_and_update_root(
&(*generator, generator_value),
u64::MAX,
)?;
let slash_amount = emission_amount(block_height) / 255;

for to_slash in slashing {
session.call::<_, ()>(
STAKE_CONTRACT,
"slash",
&(*to_slash, slash_amount),
u64::MAX,
)?;
}

session.call::<_, ()>(TRANSFER_CONTRACT, "update_root", &(), u64::MAX)?;

Expand Down
4 changes: 4 additions & 0 deletions rusk/src/lib/vm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ impl VMExecution for Rusk {
params.block_gas_limit,
params.generator_pubkey.inner(),
txs,
&params.missed_generators[..],
)
.map_err(|inner| {
anyhow::anyhow!("Cannot execute txs: {inner}!!")
Expand All @@ -57,6 +58,7 @@ impl VMExecution for Rusk {
blk.header().gas_limit,
&generator,
blk.txs(),
&blk.header().failed_iterations.to_missed_generators()?,
)
.map_err(|inner| anyhow::anyhow!("Cannot verify txs: {inner}!!"))?;

Expand All @@ -83,6 +85,7 @@ impl VMExecution for Rusk {
state_root: blk.header().state_hash,
event_hash: blk.header().event_hash,
}),
&blk.header().failed_iterations.to_missed_generators()?,
)
.map_err(|inner| anyhow::anyhow!("Cannot accept txs: {inner}!!"))?;

Expand All @@ -109,6 +112,7 @@ impl VMExecution for Rusk {
state_root: blk.header().state_hash,
event_hash: blk.header().event_hash,
}),
&blk.header().failed_iterations.to_missed_generators()?,
)
.map_err(|inner| {
anyhow::anyhow!("Cannot finalize txs: {inner}!!")
Expand Down
1 change: 1 addition & 0 deletions rusk/tests/common/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ pub fn generator_procedure(
round,
block_gas_limit,
generator_pubkey,
missed_generators: vec![],
};

let (transfer_txs, discarded, execute_output) = rusk
Expand Down

0 comments on commit d1eebb0

Please sign in to comment.