Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions Cargo.lock

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

37 changes: 32 additions & 5 deletions execution_engine/src/runtime/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,9 @@ use casper_types::{
},
system::{
self,
auction::{self, DelegatorKind, EraInfo},
handle_payment, mint,
mint::MINT_SUSTAIN_PURSE_KEY,
auction::{self, DelegatorKind, EraInfo, MINIMUM_DELEGATION_RATE_KEY},
handle_payment,
mint::{self, MINT_SUSTAIN_PURSE_KEY},
CallStackElement, Caller, CallerInfo, SystemEntityType, AUCTION, HANDLE_PAYMENT, MINT,
STANDARD_PAYMENT,
},
Expand Down Expand Up @@ -1084,7 +1084,7 @@ where

let max_delegators_per_validator =
self.context.engine_config().max_delegators_per_validator();

let minimum_delegation_rate = self.get_minimum_delegation_rate()?;
let minimum_bid_amount = self.context().engine_config().minimum_bid_amount();

let result = runtime
Expand All @@ -1099,6 +1099,7 @@ where
reserved_slots,
global_minimum_delegation_amount,
global_maximum_delegation_amount,
minimum_delegation_rate,
)
.map_err(Self::reverter)?;

Expand Down Expand Up @@ -1325,8 +1326,9 @@ where
let reservations =
Self::get_named_argument(runtime_args, auction::ARG_RESERVATIONS)?;

let minimum_delegation_rate = self.get_minimum_delegation_rate()?;
runtime
.add_reservations(reservations)
.add_reservations(reservations, minimum_delegation_rate)
.map_err(Self::reverter)?;

CLValue::from_t(()).map_err(Self::reverter)
Expand Down Expand Up @@ -4665,6 +4667,31 @@ where
)?;
Ok(Ok(()))
}

fn get_minimum_delegation_rate(&self) -> Result<u8, ExecError> {
let auction_contract_hash = self.context.get_system_contract(AUCTION)?;
let auction_named_keys = self
.context
.state()
.borrow_mut()
.get_named_keys(EntityAddr::System(auction_contract_hash.value()))?;
let minimum_delegation_rate_key =
auction_named_keys.get(MINIMUM_DELEGATION_RATE_KEY).ok_or(
ExecError::NamedKeyNotFound(MINIMUM_DELEGATION_RATE_KEY.to_string()),
)?;
let stored_value = self
.context
.state()
.borrow_mut()
.read(minimum_delegation_rate_key)?
.ok_or(ExecError::KeyNotFound(*minimum_delegation_rate_key))?;
if let StoredValue::CLValue(cl_value) = stored_value {
let minimum_delegation_rate: u8 = cl_value.into_t().map_err(ExecError::CLValue)?;
Ok(minimum_delegation_rate)
} else {
Err(ExecError::UnexpectedStoredValueVariant)
}
}
}

#[cfg(feature = "test-support")]
Expand Down
9 changes: 9 additions & 0 deletions execution_engine_testing/test_support/src/chainspec_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ impl ChainspecConfig {
locked_funds_period,
unbonding_delay,
round_seigniorage_rate,
minimum_delegation_rate,
..
} = core_config;

Expand All @@ -145,6 +146,7 @@ impl ChainspecConfig {
.with_unbonding_delay(*unbonding_delay)
.with_genesis_timestamp_millis(DEFAULT_GENESIS_TIMESTAMP_MILLIS)
.with_storage_costs(*storage_costs)
.with_minimum_delegation_rate(*minimum_delegation_rate)
.build();

Ok(GenesisRequest::new(
Expand All @@ -167,6 +169,12 @@ impl ChainspecConfig {
)
}

/// Sets the minimum delegation rate config option.
pub fn with_minimum_delegation_rate(mut self, minimum_delegation_rate: u8) -> Self {
self.core_config.minimum_delegation_rate = minimum_delegation_rate;
self
}

/// Sets the vesting schedule period millis config option.
pub fn with_max_associated_keys(&mut self, value: u32) -> &mut Self {
self.core_config.max_associated_keys = value;
Expand Down Expand Up @@ -311,6 +319,7 @@ impl TryFrom<ChainspecConfig> for GenesisConfig {
.with_genesis_timestamp_millis(DEFAULT_GENESIS_TIMESTAMP_MILLIS)
.with_storage_costs(chainspec_config.storage_costs)
.with_enable_addressable_entity(chainspec_config.core_config.enable_addressable_entity)
.with_minimum_delegation_rate(chainspec_config.core_config.minimum_delegation_rate)
.build())
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
//! A builder for an [`GenesisConfig`].
use casper_execution_engine::engine_state::engine_config::DEFAULT_ENABLE_ENTITY;
use casper_types::{
GenesisAccount, GenesisConfig, HoldBalanceHandling, StorageCosts, SystemConfig, WasmConfig,
system::auction::DelegationRate, GenesisAccount, GenesisConfig, HoldBalanceHandling,
StorageCosts, SystemConfig, WasmConfig,
};
use num_rational::Ratio;

Expand Down Expand Up @@ -31,6 +32,7 @@ pub struct GenesisConfigBuilder {
enable_addressable_entity: Option<bool>,
rewards_ratio: Option<Ratio<u64>>,
storage_costs: Option<StorageCosts>,
minimum_delegation_rate: DelegationRate,
}

impl GenesisConfigBuilder {
Expand Down Expand Up @@ -111,6 +113,12 @@ impl GenesisConfigBuilder {
self
}

/// Sets the minimum delegation rate config option.
pub fn with_minimum_delegation_rate(mut self, minimum_delegation_rate: DelegationRate) -> Self {
self.minimum_delegation_rate = minimum_delegation_rate;
self
}

/// Builds a new [`GenesisConfig`] object.
pub fn build(self) -> GenesisConfig {
GenesisConfig::new(
Expand All @@ -134,6 +142,7 @@ impl GenesisConfigBuilder {
.unwrap_or(DEFAULT_ENABLE_ENTITY),
self.rewards_ratio,
self.storage_costs.unwrap_or_default(),
self.minimum_delegation_rate,
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ use std::collections::BTreeMap;
use num_rational::Ratio;

use casper_types::{
ChainspecRegistry, Digest, EraId, FeeHandling, HoldBalanceHandling, Key, ProtocolUpgradeConfig,
ProtocolVersion, RewardsHandling, StoredValue,
system::auction::DelegationRate, ChainspecRegistry, Digest, EraId, FeeHandling,
HoldBalanceHandling, Key, ProtocolUpgradeConfig, ProtocolVersion, RewardsHandling, StoredValue,
};

/// Builds an `UpgradeConfig`.
Expand All @@ -28,6 +28,7 @@ pub struct UpgradeRequestBuilder {
minimum_delegation_amount: u64,
enable_addressable_entity: bool,
rewards_handling: RewardsHandling,
new_minimum_delegation_rate: Option<DelegationRate>,
}

impl UpgradeRequestBuilder {
Expand Down Expand Up @@ -156,6 +157,15 @@ impl UpgradeRequestBuilder {
self
}

/// Sets the minimum delegation rate for validator bids and reservations.
pub fn with_new_minimum_delegation_rate(
mut self,
new_minimum_delegation_rate: DelegationRate,
) -> Self {
self.new_minimum_delegation_rate = Some(new_minimum_delegation_rate);
self
}

/// Consumes the `UpgradeRequestBuilder` and returns an [`ProtocolUpgradeConfig`].
pub fn build(self) -> ProtocolUpgradeConfig {
ProtocolUpgradeConfig::new(
Expand All @@ -178,6 +188,7 @@ impl UpgradeRequestBuilder {
self.minimum_delegation_amount,
self.enable_addressable_entity,
self.rewards_handling,
self.new_minimum_delegation_rate,
)
}
}
Expand All @@ -204,6 +215,7 @@ impl Default for UpgradeRequestBuilder {
minimum_delegation_amount: 0,
enable_addressable_entity: false,
rewards_handling: RewardsHandling::Standard,
new_minimum_delegation_rate: None,
}
}
}
55 changes: 55 additions & 0 deletions execution_engine_testing/tests/src/test/contract_api/auction.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
use casper_engine_test_support::{
ChainspecConfig, ExecuteRequestBuilder, LmdbWasmTestBuilder, DEFAULT_ACCOUNTS,
DEFAULT_ACCOUNT_ADDR, DEFAULT_ACCOUNT_PUBLIC_KEY, DEFAULT_PROTOCOL_VERSION,
};
use casper_execution_engine::{engine_state::Error, execution::ExecError};
use casper_types::{
runtime_args,
system::auction::{ARG_AMOUNT, ARG_DELEGATION_RATE, ARG_PUBLIC_KEY},
ApiError, U512,
};
use once_cell::sync::Lazy;
use std::path::PathBuf;

const ADD_BIDS_WASM: &str = "auction_bids.wasm";
const ARG_ENTRY_POINT: &str = "entry_point";
/// The name of the chainspec file on disk.
pub const CHAINSPEC_NAME: &str = "chainspec.toml";
pub static LOCAL_PATH: Lazy<PathBuf> =
Lazy::new(|| PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("../../resources/local/"));

#[ignore]
#[test]
fn add_auction_should_fail_when_delegation_rate_not_met() {
let path = LOCAL_PATH.join(CHAINSPEC_NAME);
let mut chainspec =
ChainspecConfig::from_chainspec_path(path).expect("must build chainspec configuration");
chainspec = chainspec.with_minimum_delegation_rate(20);
let mut builder = LmdbWasmTestBuilder::new_temporary_with_config(chainspec.clone());
let genesis_request = chainspec
.create_genesis_request(DEFAULT_ACCOUNTS.clone(), DEFAULT_PROTOCOL_VERSION)
.unwrap();
builder.run_genesis(genesis_request);

let exec_request = ExecuteRequestBuilder::standard(
*DEFAULT_ACCOUNT_ADDR,
ADD_BIDS_WASM,
runtime_args! {
ARG_ENTRY_POINT => "add_bid",
ARG_PUBLIC_KEY => DEFAULT_ACCOUNT_PUBLIC_KEY.clone(),
ARG_AMOUNT => U512::from(10_000_000_000_000u64),
ARG_DELEGATION_RATE => 19u8,
},
)
.build();

let commit = builder.exec(exec_request).commit();
commit.expect_failure();
let last_exec_result = commit
.get_last_exec_result()
.expect("Expected to be called after exec()");
assert!(matches!(
last_exec_result.error().cloned(),
Some(Error::Exec(ExecError::Revert(ApiError::AuctionError(64))))
));
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
mod account;
mod add_contract_version;
mod auction;
mod create_purse;
mod dictionary;
mod generic_hash;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,9 @@ use casper_types::{
auction::{
DelegatorKind, SeigniorageRecipientsSnapshotV1, SeigniorageRecipientsSnapshotV2,
AUCTION_DELAY_KEY, DEFAULT_SEIGNIORAGE_RECIPIENTS_SNAPSHOT_VERSION,
LOCKED_FUNDS_PERIOD_KEY, SEIGNIORAGE_RECIPIENTS_SNAPSHOT_KEY,
SEIGNIORAGE_RECIPIENTS_SNAPSHOT_VERSION_KEY, UNBONDING_DELAY_KEY, VALIDATOR_SLOTS_KEY,
LOCKED_FUNDS_PERIOD_KEY, MINIMUM_DELEGATION_RATE_KEY,
SEIGNIORAGE_RECIPIENTS_SNAPSHOT_KEY, SEIGNIORAGE_RECIPIENTS_SNAPSHOT_VERSION_KEY,
UNBONDING_DELAY_KEY, VALIDATOR_SLOTS_KEY,
},
mint::ROUND_SEIGNIORAGE_RATE_KEY,
},
Expand Down Expand Up @@ -889,3 +890,67 @@ fn should_migrate_seigniorage_snapshot_to_new_version() {
}
}
}

#[test]
fn should_store_and_upgrade_minimum_delegation_rate_named_key() {
const UPGRADED_MINIMUM_DELEGATION_RATE: u8 = 20;

let mut builder = LmdbWasmTestBuilder::default();
builder.run_genesis(LOCAL_GENESIS_REQUEST.clone());

let auction_contract_hash = builder.get_auction_contract_hash();
let auction_named_keys =
builder.get_named_keys(EntityAddr::System(auction_contract_hash.value()));
let minimum_delegation_rate_key = *auction_named_keys
.get(MINIMUM_DELEGATION_RATE_KEY)
.expect("minimum delegation rate key should exist at genesis");

let minimum_delegation_rate: u8 = builder
.query(None, minimum_delegation_rate_key, &[])
.expect("should have minimum delegation rate")
.as_cl_value()
.expect("minimum delegation rate should be a CLValue")
.clone()
.into_t()
.expect("minimum delegation rate should be u8");

assert_eq!(
minimum_delegation_rate, 0,
"genesis should have set minimum delegation rate to 0!"
);

let sem_ver = PROTOCOL_VERSION.value();
let new_protocol_version =
ProtocolVersion::from_parts(sem_ver.major, sem_ver.minor, sem_ver.patch + 1);

let mut upgrade_request = UpgradeRequestBuilder::new()
.with_current_protocol_version(PROTOCOL_VERSION)
.with_new_protocol_version(new_protocol_version)
.with_activation_point(DEFAULT_ACTIVATION_POINT)
.with_new_minimum_delegation_rate(UPGRADED_MINIMUM_DELEGATION_RATE)
.build();

builder
.upgrade(&mut upgrade_request)
.expect_upgrade_success();

let upgraded_auction_contract_hash = builder.get_auction_contract_hash();
let upgraded_named_keys =
builder.get_named_keys(EntityAddr::System(upgraded_auction_contract_hash.value()));
let upgraded_minimum_delegation_rate_key = *upgraded_named_keys
.get(MINIMUM_DELEGATION_RATE_KEY)
.expect("minimum delegation rate key should exist after upgrade");
let upgraded_minimum_delegation_rate: u8 = builder
.query(None, upgraded_minimum_delegation_rate_key, &[])
.expect("should have upgraded minimum delegation rate")
.as_cl_value()
.expect("minimum delegation rate should be a CLValue")
.clone()
.into_t()
.expect("minimum delegation rate should be u8");

assert_eq!(
upgraded_minimum_delegation_rate,
UPGRADED_MINIMUM_DELEGATION_RATE
);
}
Loading