Skip to content

Commit 4058227

Browse files
Merge pull request #5387 from darthsiroftardis/auction-reward-split
Add rewards handling chainspec setting
2 parents 27ed209 + 211cec2 commit 4058227

File tree

34 files changed

+1472
-67
lines changed

34 files changed

+1472
-67
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ lint-smart-contracts:
131131

132132
.PHONY: audit-rs
133133
audit-rs:
134-
$(CARGO) audit --ignore RUSTSEC-2024-0437 --ignore RUSTSEC-2025-0022 --ignore RUSTSEC-2025-0055 --ignore RUSTSEC-2026-0001
134+
$(CARGO) audit --ignore RUSTSEC-2024-0437 --ignore RUSTSEC-2025-0022 --ignore RUSTSEC-2025-0055 --ignore RUSTSEC-2026-0001 --ignore RUSTSEC-2026-0007
135135

136136
.PHONY: audit
137137
audit: audit-rs

execution_engine/src/engine_state/engine_config.rs

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@ use num_rational::Ratio;
77
use num_traits::One;
88

99
use casper_types::{
10-
account::AccountHash, FeeHandling, ProtocolVersion, PublicKey, RefundHandling, StorageCosts,
11-
SystemConfig, TimeDiff, WasmConfig, DEFAULT_FEE_HANDLING, DEFAULT_MINIMUM_BID_AMOUNT,
12-
DEFAULT_REFUND_HANDLING,
10+
account::AccountHash, FeeHandling, ProtocolVersion, PublicKey, RefundHandling, RewardsHandling,
11+
StorageCosts, SystemConfig, TimeDiff, WasmConfig, DEFAULT_FEE_HANDLING,
12+
DEFAULT_MINIMUM_BID_AMOUNT, DEFAULT_REFUND_HANDLING,
1313
};
1414

1515
/// Default value for a maximum query depth configuration option.
@@ -93,6 +93,7 @@ pub struct EngineConfig {
9393
pub(crate) compute_rewards: bool,
9494
pub(crate) enable_entity: bool,
9595
pub(crate) trap_on_ambiguous_entity_version: bool,
96+
pub(crate) rewards_handling: RewardsHandling,
9697
storage_costs: StorageCosts,
9798
}
9899

@@ -118,6 +119,7 @@ impl Default for EngineConfig {
118119
protocol_version: DEFAULT_PROTOCOL_VERSION,
119120
enable_entity: DEFAULT_ENABLE_ENTITY,
120121
trap_on_ambiguous_entity_version: DEFAULT_TRAP_ON_AMBIGUOUS_ENTITY_VERSION,
122+
rewards_handling: RewardsHandling::Standard,
121123
storage_costs: Default::default(),
122124
}
123125
}
@@ -224,6 +226,11 @@ impl EngineConfig {
224226
self.trap_on_ambiguous_entity_version
225227
}
226228

229+
/// Returns the current configuration for rewards handling.
230+
pub fn rewards_handling(&self) -> RewardsHandling {
231+
self.rewards_handling.clone()
232+
}
233+
227234
/// Sets the protocol version of the config.
228235
///
229236
/// NOTE: This is only useful to the WasmTestBuilder for emulating a network upgrade, and hence
@@ -267,6 +274,7 @@ pub struct EngineConfigBuilder {
267274
balance_hold_interval: Option<TimeDiff>,
268275
enable_entity: Option<bool>,
269276
trap_on_ambiguous_entity_version: Option<bool>,
277+
rewards_handling: Option<RewardsHandling>,
270278
storage_costs: Option<StorageCosts>,
271279
}
272280

@@ -487,6 +495,7 @@ impl EngineConfigBuilder {
487495
.trap_on_ambiguous_entity_version
488496
.unwrap_or(DEFAULT_TRAP_ON_AMBIGUOUS_ENTITY_VERSION);
489497
let storage_costs = self.storage_costs.unwrap_or_default();
498+
let rewards_handling = self.rewards_handling.unwrap_or(RewardsHandling::Standard);
490499

491500
EngineConfig {
492501
max_associated_keys,
@@ -508,6 +517,7 @@ impl EngineConfigBuilder {
508517
compute_rewards,
509518
enable_entity,
510519
trap_on_ambiguous_entity_version,
520+
rewards_handling,
511521
storage_costs,
512522
}
513523
}

execution_engine/src/runtime/mod.rs

Lines changed: 33 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -53,15 +53,18 @@ use casper_types::{
5353
system::{
5454
self,
5555
auction::{self, DelegatorKind, EraInfo},
56-
handle_payment, mint, CallStackElement, Caller, CallerInfo, SystemEntityType, AUCTION,
57-
HANDLE_PAYMENT, MINT, STANDARD_PAYMENT,
56+
handle_payment, mint,
57+
mint::MINT_SUSTAIN_PURSE_KEY,
58+
CallStackElement, Caller, CallerInfo, SystemEntityType, AUCTION, HANDLE_PAYMENT, MINT,
59+
STANDARD_PAYMENT,
5860
},
5961
AccessRights, ApiError, BlockGlobalAddr, BlockTime, ByteCode, ByteCodeAddr, ByteCodeHash,
6062
ByteCodeKind, CLTyped, CLValue, ContextAccessRights, Contract, ContractWasm, EntityAddr,
6163
EntityKind, EntityVersion, EntityVersionKey, EntityVersions, Gas, GrantedAccess, Group, Groups,
6264
HashAddr, HostFunction, HostFunctionCost, InitiatorAddr, Key, NamedArg, Package, PackageHash,
63-
PackageStatus, Phase, PublicKey, RuntimeArgs, RuntimeFootprint, StoredValue, Transfer,
64-
TransferResult, TransferV2, TransferredTo, URef, DICTIONARY_ITEM_KEY_MAX_LENGTH, U512,
65+
PackageStatus, Phase, PublicKey, RewardsHandling, RuntimeArgs, RuntimeFootprint, StoredValue,
66+
Transfer, TransferResult, TransferV2, TransferredTo, URef, DICTIONARY_ITEM_KEY_MAX_LENGTH,
67+
U512,
6568
};
6669

6770
use crate::{
@@ -1254,8 +1257,33 @@ where
12541257
// ExecError>`
12551258
auction::METHOD_DISTRIBUTE => (|| {
12561259
runtime.charge_system_contract_call(auction_costs.distribute)?;
1260+
let rewards_handling = self.context().engine_config().rewards_handling();
12571261
let rewards = Self::get_named_argument(runtime_args, auction::ARG_REWARDS_MAP)?;
1258-
runtime.distribute(rewards).map_err(Self::reverter)?;
1262+
1263+
let sustain_purse = match rewards_handling {
1264+
RewardsHandling::Standard => None,
1265+
RewardsHandling::Sustain { .. } => {
1266+
let sustain_purse = {
1267+
let mint_hash = self.context.get_system_contract(AUCTION)?;
1268+
match self
1269+
.context
1270+
.state()
1271+
.borrow_mut()
1272+
.get_named_keys(EntityAddr::System(mint_hash.value()))?
1273+
.get(MINT_SUSTAIN_PURSE_KEY)
1274+
{
1275+
Some(Key::URef(uref)) => Some(*uref),
1276+
Some(_) | None => None,
1277+
}
1278+
};
1279+
1280+
sustain_purse
1281+
}
1282+
};
1283+
1284+
runtime
1285+
.distribute(rewards, sustain_purse, rewards_handling)
1286+
.map_err(Self::reverter)?;
12591287
CLValue::from_t(()).map_err(Self::reverter)
12601288
})(),
12611289

execution_engine_testing/test_support/src/genesis_config_builder.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ pub struct GenesisConfigBuilder {
2929
gas_hold_balance_handling: Option<HoldBalanceHandling>,
3030
gas_hold_interval_millis: Option<u64>,
3131
enable_addressable_entity: Option<bool>,
32+
rewards_ratio: Option<Ratio<u64>>,
3233
storage_costs: Option<StorageCosts>,
3334
}
3435

@@ -98,6 +99,12 @@ impl GenesisConfigBuilder {
9899
self
99100
}
100101

102+
/// Sets the rewards ratio.
103+
pub fn with_rewards_ratio(mut self, rewards_ratio: Ratio<u64>) -> Self {
104+
self.rewards_ratio = Some(rewards_ratio);
105+
self
106+
}
107+
101108
/// Sets the storage_costs handling.
102109
pub fn with_storage_costs(mut self, storage_costs: StorageCosts) -> Self {
103110
self.storage_costs = Some(storage_costs);
@@ -125,6 +132,7 @@ impl GenesisConfigBuilder {
125132
.unwrap_or(DEFAULT_GAS_HOLD_INTERVAL_MILLIS),
126133
self.enable_addressable_entity
127134
.unwrap_or(DEFAULT_ENABLE_ENTITY),
135+
self.rewards_ratio,
128136
self.storage_costs.unwrap_or_default(),
129137
)
130138
}

execution_engine_testing/test_support/src/lib.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,13 @@ pub static DEFAULT_PROPOSER_PUBLIC_KEY: Lazy<PublicKey> = Lazy::new(|| {
116116
/// Default proposer address.
117117
pub static DEFAULT_PROPOSER_ADDR: Lazy<AccountHash> =
118118
Lazy::new(|| AccountHash::from(&*DEFAULT_PROPOSER_PUBLIC_KEY));
119+
120+
/// Default public key to associate with the sustain purse.
121+
pub static DEFAULT_SUSTAIN_PUBLIC_KEY: Lazy<PublicKey> = Lazy::new(|| {
122+
let secret_key = SecretKey::ed25519_from_bytes([207; SecretKey::ED25519_LENGTH]).unwrap();
123+
PublicKey::from(&secret_key)
124+
});
125+
119126
/// Default accounts.
120127
pub static DEFAULT_ACCOUNTS: Lazy<Vec<GenesisAccount>> = Lazy::new(|| {
121128
let mut ret = Vec::new();

execution_engine_testing/test_support/src/transfer_request_builder.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ use casper_types::{
1919
bytesrepr::ToBytes,
2020
system::mint::{ARG_AMOUNT, ARG_ID, ARG_SOURCE, ARG_TARGET},
2121
BlockTime, CLValue, Digest, FeeHandling, Gas, InitiatorAddr, ProtocolVersion, RefundHandling,
22-
RuntimeArgs, TransactionHash, TransactionV1Hash, TransferTarget, URef,
22+
RewardsHandling, RuntimeArgs, TransactionHash, TransactionV1Hash, TransferTarget, URef,
2323
DEFAULT_GAS_HOLD_INTERVAL, U512,
2424
};
2525

@@ -61,6 +61,7 @@ impl TransferRequestBuilder {
6161
Ratio::new_raw(U512::zero(), U512::zero()),
6262
DEFAULT_ENABLE_ENTITY,
6363
2_500_000_000,
64+
RewardsHandling::Standard,
6465
);
6566
/// The default value used for `TransferRequest::state_hash`.
6667
pub const DEFAULT_STATE_HASH: Digest = Digest::from_raw([1; 32]);

execution_engine_testing/test_support/src/upgrade_request_builder.rs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use num_rational::Ratio;
44

55
use casper_types::{
66
ChainspecRegistry, Digest, EraId, FeeHandling, HoldBalanceHandling, Key, ProtocolUpgradeConfig,
7-
ProtocolVersion, StoredValue,
7+
ProtocolVersion, RewardsHandling, StoredValue,
88
};
99

1010
/// Builds an `UpgradeConfig`.
@@ -27,6 +27,7 @@ pub struct UpgradeRequestBuilder {
2727
maximum_delegation_amount: u64,
2828
minimum_delegation_amount: u64,
2929
enable_addressable_entity: bool,
30+
rewards_handling: RewardsHandling,
3031
}
3132

3233
impl UpgradeRequestBuilder {
@@ -149,6 +150,12 @@ impl UpgradeRequestBuilder {
149150
self
150151
}
151152

153+
/// Sets the rewards handling
154+
pub fn with_rewards_handling(mut self, rewards_handling: RewardsHandling) -> Self {
155+
self.rewards_handling = rewards_handling;
156+
self
157+
}
158+
152159
/// Consumes the `UpgradeRequestBuilder` and returns an [`ProtocolUpgradeConfig`].
153160
pub fn build(self) -> ProtocolUpgradeConfig {
154161
ProtocolUpgradeConfig::new(
@@ -170,6 +177,7 @@ impl UpgradeRequestBuilder {
170177
self.maximum_delegation_amount,
171178
self.minimum_delegation_amount,
172179
self.enable_addressable_entity,
180+
self.rewards_handling,
173181
)
174182
}
175183
}
@@ -195,6 +203,7 @@ impl Default for UpgradeRequestBuilder {
195203
maximum_delegation_amount: u64::MAX,
196204
minimum_delegation_amount: 0,
197205
enable_addressable_entity: false,
206+
rewards_handling: RewardsHandling::Standard,
198207
}
199208
}
200209
}

execution_engine_testing/test_support/src/wasm_test_builder.rs

Lines changed: 69 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,8 @@ use casper_types::{
6767
BlockTime, ByteCode, ByteCodeAddr, ByteCodeHash, CLTyped, CLValue, Contract, Digest,
6868
EntityAddr, EntryPoints, EraId, FeeHandling, Gas, HandlePaymentCosts, HoldBalanceHandling,
6969
InitiatorAddr, Key, KeyTag, MintCosts, Motes, Package, PackageHash, Phase,
70-
ProtocolUpgradeConfig, ProtocolVersion, PublicKey, RefundHandling, StoredValue,
71-
SystemHashRegistry, TransactionHash, TransactionV1Hash, URef, OS_PAGE_SIZE, U512,
70+
ProtocolUpgradeConfig, ProtocolVersion, PublicKey, RefundHandling, RewardsHandling,
71+
StoredValue, SystemHashRegistry, TransactionHash, TransactionV1Hash, URef, OS_PAGE_SIZE, U512,
7272
};
7373

7474
use crate::{
@@ -848,6 +848,7 @@ where
848848
credit_cap,
849849
enable_addressable_entity,
850850
config.system_costs_config.mint_costs().transfer,
851+
config.core_config.rewards_handling.clone(),
851852
);
852853

853854
let bidding_req = BiddingRequest::new(
@@ -1018,6 +1019,7 @@ where
10181019
credit_cap,
10191020
self.chainspec.core_config.enable_addressable_entity,
10201021
self.chainspec.system_costs_config.mint_costs().transfer,
1022+
self.chainspec.core_config.rewards_handling.clone(),
10211023
)
10221024
}
10231025

@@ -1080,6 +1082,71 @@ where
10801082
distribute_block_rewards_result
10811083
}
10821084

1085+
/// Distributes the rewards.
1086+
pub fn distribute_with_rewards_handling(
1087+
&mut self,
1088+
pre_state_hash: Option<Digest>,
1089+
protocol_version: ProtocolVersion,
1090+
rewards: BTreeMap<PublicKey, Vec<U512>>,
1091+
block_time: u64,
1092+
rewards_handling: RewardsHandling,
1093+
) -> BlockRewardsResult {
1094+
let pre_state_hash = pre_state_hash.or(self.post_state_hash).unwrap();
1095+
let administrators: BTreeSet<AccountHash> = self
1096+
.chainspec
1097+
.core_config
1098+
.administrators
1099+
.iter()
1100+
.map(|x| x.to_account_hash())
1101+
.collect();
1102+
let allow_unrestricted = self.chainspec.core_config.allow_unrestricted_transfers;
1103+
let transfer_config = TransferConfig::new(administrators, allow_unrestricted);
1104+
let include_credits = self.chainspec.core_config.fee_handling == FeeHandling::NoFee;
1105+
let credit_cap = Ratio::new_raw(
1106+
U512::from(*self.chainspec.core_config.validator_credit_cap.numer()),
1107+
U512::from(*self.chainspec.core_config.validator_credit_cap.denom()),
1108+
);
1109+
1110+
let native_runtime_config = NativeRuntimeConfig::new(
1111+
transfer_config,
1112+
self.chainspec.core_config.fee_handling,
1113+
self.chainspec.core_config.refund_handling,
1114+
self.chainspec.core_config.vesting_schedule_period.millis(),
1115+
self.chainspec.core_config.allow_auction_bids,
1116+
self.chainspec.core_config.compute_rewards,
1117+
self.chainspec.core_config.max_delegators_per_validator,
1118+
self.chainspec.core_config.minimum_bid_amount,
1119+
self.chainspec.core_config.minimum_delegation_amount,
1120+
self.chainspec.core_config.maximum_delegation_amount,
1121+
self.chainspec.core_config.gas_hold_interval.millis(),
1122+
include_credits,
1123+
credit_cap,
1124+
self.chainspec.core_config.enable_addressable_entity,
1125+
self.chainspec.system_costs_config.mint_costs().transfer,
1126+
rewards_handling,
1127+
);
1128+
1129+
let distribute_req = BlockRewardsRequest::new(
1130+
native_runtime_config,
1131+
pre_state_hash,
1132+
protocol_version,
1133+
BlockTime::new(block_time),
1134+
rewards,
1135+
);
1136+
let distribute_block_rewards_result = self
1137+
.data_access_layer
1138+
.distribute_block_rewards(distribute_req);
1139+
1140+
if let BlockRewardsResult::Success {
1141+
post_state_hash, ..
1142+
} = distribute_block_rewards_result
1143+
{
1144+
self.post_state_hash = Some(post_state_hash);
1145+
}
1146+
1147+
distribute_block_rewards_result
1148+
}
1149+
10831150
/// Finalizes payment for a transaction
10841151
pub fn handle_fee(
10851152
&mut self,

execution_engine_testing/tests/src/test/explorer/faucet.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -663,14 +663,14 @@ fn faucet_costs() {
663663
// This test will fail if execution costs vary. The expected costs should not be updated
664664
// without understanding why the cost has changed. If the costs do change, it should be
665665
// reflected in the "Costs by Entry Point" section of the faucet crate's README.md.
666-
const EXPECTED_FAUCET_INSTALL_COST: u64 = 160_503_972_212;
666+
const EXPECTED_FAUCET_INSTALL_COST: u64 = 149_263_295_166;
667667
const EXPECTED_FAUCET_INSTALL_COST_ALT: u64 = 149_230_872_143;
668668

669-
const EXPECTED_FAUCET_SET_VARIABLES_COST: u64 = 79_455_975;
669+
const EXPECTED_FAUCET_SET_VARIABLES_COST: u64 = 79_463_750;
670670

671-
const EXPECTED_FAUCET_CALL_BY_INSTALLER_COST: u64 = 2_652_626_533;
671+
const EXPECTED_FAUCET_CALL_BY_INSTALLER_COST: u64 = 2_652_633_308;
672672

673-
const EXPECTED_FAUCET_CALL_BY_USER_COST: u64 = 2_558_318_531;
673+
const EXPECTED_FAUCET_CALL_BY_USER_COST: u64 = 2_558_333_326;
674674

675675
let installer_account = AccountHash::new([1u8; 32]);
676676
let user_account: AccountHash = AccountHash::new([2u8; 32]);

0 commit comments

Comments
 (0)