Skip to content

Commit

Permalink
Deferred credits fix (#4629)
Browse files Browse the repository at this point in the history
* Initial fix

* Add UT for auto_sell and update quiche

* Update version
  • Loading branch information
Leo-Besancon authored Jan 17, 2024
1 parent 6b9505b commit e23cf05
Show file tree
Hide file tree
Showing 47 changed files with 193 additions and 52 deletions.
25 changes: 20 additions & 5 deletions Cargo.lock

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

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ massa_wallet = { path = "./massa-wallet" }
# Massa projects dependencies
massa-proto-rs = { git = "https://github.com/massalabs/massa-proto-rs", "rev" = "84678fb77cf6d06d85d55e2c20502908ff292e61" }
massa-sc-runtime = { git = "https://github.com/massalabs/massa-sc-runtime", "rev" = "bbab9f329eee08d1f54c595214634a03824e9343" }
peernet = { git = "https://github.com/massalabs/PeerNet", "branch" = "main" }
peernet = { git = "https://github.com/massalabs/PeerNet", "rev" = "04b05ddd320fbe76cc858115af7b5fc28bdb8310" }

# Common dependencies
transition = { git = "https://github.com/massalabs/transition.git", "rev" = "93fa3bf82f9f5ff421c78536879b7fd1b948ca75" }
Expand Down
2 changes: 1 addition & 1 deletion massa-api-exports/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "massa_api_exports"
version = "2.0.0"
version = "2.1.0"
authors = ["Massa Labs <[email protected]>"]
edition = "2021"

Expand Down
2 changes: 1 addition & 1 deletion massa-api/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "massa_api"
version = "2.0.0"
version = "2.1.0"
edition = "2021"

[features]
Expand Down
2 changes: 1 addition & 1 deletion massa-async-pool/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "massa_async_pool"
version = "2.0.0"
version = "2.1.0"
authors = ["Massa Labs <[email protected]>"]
edition = "2021"

Expand Down
2 changes: 1 addition & 1 deletion massa-bootstrap/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "massa_bootstrap"
version = "2.0.0"
version = "2.1.0"
authors = ["Massa Labs <[email protected]>"]
edition = "2021"

Expand Down
2 changes: 1 addition & 1 deletion massa-channel/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "massa_channel"
version = "2.0.0"
version = "2.1.0"
edition = "2021"

[features]
Expand Down
2 changes: 1 addition & 1 deletion massa-cipher/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "massa_cipher"
version = "2.0.0"
version = "2.1.0"
edition = "2021"

[dependencies]
Expand Down
2 changes: 1 addition & 1 deletion massa-client/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "massa-client"
version = "2.0.0"
version = "2.1.0"
edition = "2021"

[dependencies]
Expand Down
2 changes: 1 addition & 1 deletion massa-consensus-exports/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "massa_consensus_exports"
version = "2.0.0"
version = "2.1.0"
authors = ["Massa Labs <[email protected]>"]
edition = "2021"

Expand Down
2 changes: 1 addition & 1 deletion massa-consensus-worker/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "massa_consensus_worker"
version = "2.0.0"
version = "2.1.0"
authors = ["Massa Labs <[email protected]>"]
edition = "2021"

Expand Down
2 changes: 1 addition & 1 deletion massa-db-exports/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "massa_db_exports"
version = "2.0.0"
version = "2.1.0"
edition = "2021"

[features]
Expand Down
2 changes: 1 addition & 1 deletion massa-db-worker/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "massa_db_worker"
version = "2.0.0"
version = "2.1.0"
edition = "2021"

[features]
Expand Down
2 changes: 1 addition & 1 deletion massa-executed-ops/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "massa_executed_ops"
version = "2.0.0"
version = "2.1.0"
authors = ["Massa Labs <[email protected]>"]
edition = "2021"

Expand Down
2 changes: 1 addition & 1 deletion massa-execution-exports/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "massa_execution_exports"
version = "2.0.0"
version = "2.1.0"
authors = ["Massa Labs <[email protected]>"]
edition = "2021"

Expand Down
2 changes: 1 addition & 1 deletion massa-execution-worker/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ harness = false

[package]
name = "massa_execution_worker"
version = "2.0.0"
version = "2.1.0"
authors = ["Massa Labs <[email protected]>"]
edition = "2021"

Expand Down
6 changes: 5 additions & 1 deletion massa-execution-worker/src/speculative_roll_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,11 @@ impl SpeculativeRollState {
let owned_count = self.get_rolls(&addr);
if owned_count != 0 {
if let Some(amount) = roll_price.checked_mul_u64(owned_count) {
target_credits.insert(addr, amount);
let new_deferred_credits = self
.get_address_deferred_credit_for_slot(&addr, &target_slot)
.unwrap_or_default()
.saturating_add(amount);
target_credits.insert(addr, new_deferred_credits);
self.added_changes.roll_changes.insert(addr, 0);
}
}
Expand Down
124 changes: 123 additions & 1 deletion massa-execution-worker/src/tests/scenarios_mandatories.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ use massa_models::bytecode::Bytecode;
use massa_models::config::{
CHAINID, ENDORSEMENT_COUNT, LEDGER_ENTRY_DATASTORE_BASE_SIZE, THREAD_COUNT,
};
use massa_models::prehash::PreHashMap;
use massa_models::test_exports::gen_endorsements_for_denunciation;
use massa_models::{address::Address, amount::Amount, slot::Slot};
use massa_models::{
Expand All @@ -25,7 +26,9 @@ use massa_models::{
operation::{Operation, OperationSerializer, OperationType},
secure_share::SecureShareContent,
};
use massa_pos_exports::{MockSelectorControllerWrapper, PoSConfig, PoSFinalState, Selection};
use massa_pos_exports::{
CycleInfo, MockSelectorControllerWrapper, PoSConfig, PoSFinalState, ProductionStats, Selection,
};
use massa_signature::KeyPair;
use massa_test_framework::{TestUniverse, WaitPoint};
use mockall::predicate;
Expand Down Expand Up @@ -1472,6 +1475,125 @@ fn roll_sell() {
finalized_waitpoint.wait();
}

#[test]
fn auto_sell_on_missed_blocks() {
// setup
let exec_cfg = ExecutionConfig {
thread_count: 2,
periods_per_cycle: 2,
max_miss_ratio: Ratio::new(0, 1),
..Default::default()
};
let mut foreign_controllers = ExecutionForeignControllers::new_with_mocks();
selector_boilerplate(&mut foreign_controllers.selector_controller);
let finalized_waitpoint = WaitPoint::new();
let finalized_waitpoint_trigger_handle = finalized_waitpoint.get_trigger_handle();
let keypair = KeyPair::from_str(TEST_SK_1).unwrap();
let address = Address::from_public_key(&keypair.get_public_key());
let (rolls_path, _) = get_initials();
let mut batch = DBBatch::new();
let initial_deferred_credits = Amount::from_str("100").unwrap();
let mut pos_final_state = PoSFinalState::new(
PoSConfig {
thread_count: 2,
periods_per_cycle: 2,
..PoSConfig::default()
},
"",
&rolls_path.into_temp_path().to_path_buf(),
foreign_controllers.selector_controller.clone(),
foreign_controllers.db.clone(),
)
.unwrap();
pos_final_state.create_initial_cycle(&mut batch);
pos_final_state.put_deferred_credits_entry(
&Slot::new(1, 0),
&address,
&initial_deferred_credits,
&mut batch,
);

pos_final_state.put_deferred_credits_entry(
&Slot::new(7, 1),
&address,
&initial_deferred_credits,
&mut batch,
);

let mut prod_stats = PreHashMap::default();
prod_stats.insert(
address,
ProductionStats {
block_success_count: 0,
block_failure_count: 1,
},
);

let rolls_cycle_0 = pos_final_state.get_all_roll_counts(0);
let cycle_info_0 = CycleInfo::new(
0,
false,
rolls_cycle_0.clone(),
Default::default(),
prod_stats.clone(),
);
pos_final_state.put_new_cycle_info(&cycle_info_0, &mut batch);

foreign_controllers
.db
.write()
.write_batch(batch, Default::default(), None);
pos_final_state.recompute_pos_state_caches();

final_state_boilerplate(
&mut foreign_controllers.final_state,
foreign_controllers.db.clone(),
&foreign_controllers.selector_controller,
&mut foreign_controllers.ledger_controller,
None,
None,
Some(pos_final_state.clone()),
);

foreign_controllers
.final_state
.write()
.expect_finalize()
.times(1)
.with(predicate::eq(Slot::new(1, 0)), predicate::always())
.returning(move |_, _changes| {});

foreign_controllers
.final_state
.write()
.expect_finalize()
.with(predicate::eq(Slot::new(1, 1)), predicate::always())
.returning(move |_, changes| {
println!("changes: {:?}", changes);
let deferred_credits = changes
.pos_changes
.deferred_credits
.get_address_credits_for_slot(&address, &Slot::new(7, 1))
.unwrap();
assert_eq!(
deferred_credits,
Amount::from_mantissa_scale(10100, 0).unwrap()
);
finalized_waitpoint_trigger_handle.trigger();
});

let mut universe = ExecutionTestUniverse::new(foreign_controllers, exec_cfg.clone());

let block =
ExecutionTestUniverse::create_block(&keypair, Slot::new(1, 0), vec![], vec![], vec![]);
// set our block as a final block so the purchase is processed
universe.send_and_finalize(&keypair, block);
let block =
ExecutionTestUniverse::create_block(&keypair, Slot::new(1, 1), vec![], vec![], vec![]);
universe.send_and_finalize(&keypair, block);
finalized_waitpoint.wait();
}

#[test]
fn roll_slash() {
// Try to sell 97 rolls (operation 1) then process a Denunciation (with config set to slash
Expand Down
2 changes: 1 addition & 1 deletion massa-factory-exports/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "massa_factory_exports"
version = "2.0.0"
version = "2.1.0"
authors = ["Massa Labs <[email protected]>"]
edition = "2021"

Expand Down
2 changes: 1 addition & 1 deletion massa-factory-worker/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "massa_factory_worker"
version = "2.0.0"
version = "2.1.0"
authors = ["Massa Labs <[email protected]>"]
edition = "2021"

Expand Down
2 changes: 1 addition & 1 deletion massa-final-state/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "massa_final_state"
version = "2.0.0"
version = "2.1.0"
authors = ["Massa Labs <[email protected]>"]
edition = "2021"

Expand Down
2 changes: 1 addition & 1 deletion massa-grpc/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "massa_grpc"
version = "2.0.0"
version = "2.1.0"
edition = "2021"
description = "GRPC API for Massa Blockchain"
repository = "https://github.com/massalabs/massa/"
Expand Down
2 changes: 1 addition & 1 deletion massa-hash/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "massa_hash"
version = "2.0.0"
version = "2.1.0"
authors = ["Massa Labs <[email protected]>"]
edition = "2021"

Expand Down
2 changes: 1 addition & 1 deletion massa-ledger-exports/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "massa_ledger_exports"
version = "2.0.0"
version = "2.1.0"
authors = ["Massa Labs <[email protected]>"]
edition = "2021"

Expand Down
2 changes: 1 addition & 1 deletion massa-ledger-worker/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "massa_ledger_worker"
version = "2.0.0"
version = "2.1.0"
authors = ["Massa Labs <[email protected]>"]
edition = "2021"

Expand Down
Loading

0 comments on commit e23cf05

Please sign in to comment.