Skip to content

Commit

Permalink
Merge remote-tracking branch 'ed/feat-2.0-contract-staking-reattempt'…
Browse files Browse the repository at this point in the history
… into feat-2.0-contract-staking-reattempt
  • Loading branch information
darthsiroftardis committed Nov 22, 2024
2 parents 02714cb + aaaebca commit 6b13978
Show file tree
Hide file tree
Showing 19 changed files with 480 additions and 134 deletions.
36 changes: 33 additions & 3 deletions execution_engine_testing/test_support/src/wasm_test_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,9 @@ use casper_types::{
runtime_args,
system::{
auction::{
BidKind, EraValidators, Unbond, UnbondKind, ValidatorWeights, WithdrawPurses,
ARG_ERA_END_TIMESTAMP_MILLIS, ARG_EVICTED_VALIDATORS, AUCTION_DELAY_KEY, ERA_ID_KEY,
METHOD_RUN_AUCTION, UNBONDING_DELAY_KEY,
BidKind, EraValidators, Unbond, UnbondKind, UnbondingPurse, ValidatorWeights,
WithdrawPurses, ARG_ERA_END_TIMESTAMP_MILLIS, ARG_EVICTED_VALIDATORS,
AUCTION_DELAY_KEY, ERA_ID_KEY, METHOD_RUN_AUCTION, UNBONDING_DELAY_KEY,
},
mint::{MINT_GAS_HOLD_HANDLING_KEY, MINT_GAS_HOLD_INTERVAL_KEY},
AUCTION, HANDLE_PAYMENT, MINT, STANDARD_PAYMENT,
Expand Down Expand Up @@ -1779,6 +1779,36 @@ where
ret
}

/// Gets [`BTreeMap<AccountHash, Vec<UnbondingPurse>>`].
pub fn get_unbonding_purses(&mut self) -> BTreeMap<AccountHash, Vec<UnbondingPurse>> {
let state_root_hash = self.get_post_state_hash();

let tracking_copy = self
.data_access_layer
.tracking_copy(state_root_hash)
.unwrap()
.unwrap();

let reader = tracking_copy.reader();

let unbond_keys = reader
.keys_with_prefix(&[KeyTag::Unbond as u8])
.unwrap_or_default();

let mut ret = BTreeMap::new();

for key in unbond_keys.into_iter() {
let read_result = reader.read(&key);
if let (Key::Unbond(account_hash), Ok(Some(StoredValue::Unbonding(unbonding_purses)))) =
(key, read_result)
{
ret.insert(account_hash, unbonding_purses);
}
}

ret
}

/// Gets [`WithdrawPurses`].
pub fn get_withdraw_purses(&mut self) -> WithdrawPurses {
let state_root_hash = self.get_post_state_hash();
Expand Down
49 changes: 42 additions & 7 deletions resources/test/sse_data_schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -2603,7 +2603,7 @@
]
},
"delegators": {
"description": "This validator's delegators, indexed by their kind.",
"description": "This validator's delegators, indexed by their public keys.",
"allOf": [
{
"$ref": "#/definitions/Array_of_PublicKeyAndDelegator"
Expand Down Expand Up @@ -2660,32 +2660,32 @@
"description": "The public key of the delegator.",
"allOf": [
{
"$ref": "#/definitions/DelegatorKind"
"$ref": "#/definitions/PublicKey"
}
]
},
"delegator": {
"description": "The delegator details.",
"allOf": [
{
"$ref": "#/definitions/DelegatorBid"
"$ref": "#/definitions/Delegator"
}
]
}
}
},
"DelegatorBid": {
"Delegator": {
"description": "Represents a party delegating their stake to a validator (or \"delegatee\")",
"type": "object",
"required": [
"bonding_purse",
"delegator_kind",
"delegator_public_key",
"staked_amount",
"validator_public_key"
],
"properties": {
"delegator_kind": {
"$ref": "#/definitions/DelegatorKind"
"delegator_public_key": {
"$ref": "#/definitions/PublicKey"
},
"staked_amount": {
"$ref": "#/definitions/U512"
Expand Down Expand Up @@ -3039,6 +3039,41 @@
},
"additionalProperties": false
},
"DelegatorBid": {
"description": "Represents a party delegating their stake to a validator (or \"delegatee\")",
"type": "object",
"required": [
"bonding_purse",
"delegator_kind",
"staked_amount",
"validator_public_key"
],
"properties": {
"delegator_kind": {
"$ref": "#/definitions/DelegatorKind"
},
"staked_amount": {
"$ref": "#/definitions/U512"
},
"bonding_purse": {
"$ref": "#/definitions/URef"
},
"validator_public_key": {
"$ref": "#/definitions/PublicKey"
},
"vesting_schedule": {
"anyOf": [
{
"$ref": "#/definitions/VestingSchedule"
},
{
"type": "null"
}
]
}
},
"additionalProperties": false
},
"Bridge": {
"description": "A bridge record pointing to a new `ValidatorBid` after the public key was changed.",
"type": "object",
Expand Down
52 changes: 40 additions & 12 deletions storage/src/system/auction/detail.rs
Original file line number Diff line number Diff line change
Expand Up @@ -633,11 +633,11 @@ where
.iter_mut()
.max_by(|x, y| x.era_of_creation().cmp(&y.era_of_creation()))
{
Some(era) => {
let purse = *era.bonding_purse();
Some(unbond_era) => {
let purse = *unbond_era.bonding_purse();
let new_amount =
era.amount().saturating_add(delegator_reward_trunc);
era.with_amount(new_amount);
unbond_era.amount().saturating_add(delegator_reward_trunc);
unbond_era.with_amount(new_amount);
provider.write_unbond(unbond_bid_addr, Some(unbond.clone()))?;
purse
}
Expand Down Expand Up @@ -705,10 +705,10 @@ where
.iter_mut()
.max_by(|x, y| x.era_of_creation().cmp(&y.era_of_creation()))
{
Some(era) => {
let purse = *era.bonding_purse();
let new_amount = era.amount().saturating_add(amount);
era.with_amount(new_amount);
Some(unbond_era) => {
let purse = *unbond_era.bonding_purse();
let new_amount = unbond_era.amount().saturating_add(amount);
unbond_era.with_amount(new_amount);
provider.write_unbond(unbond_addr, Some(unbond.clone()))?;
purse
}
Expand Down Expand Up @@ -1084,11 +1084,18 @@ where
{
let mut ret = vec![];
let bid_addr = BidAddr::from(validator_public_key.clone());
let delegator_bid_keys = provider.get_keys_by_prefix(
let mut delegator_bid_keys = provider.get_keys_by_prefix(
&bid_addr
.delegated_account_prefix()
.map_err(|_| Error::Serialization)?,
)?;
delegator_bid_keys.extend(
provider.get_keys_by_prefix(
&bid_addr
.delegated_purse_prefix()
.map_err(|_| Error::Serialization)?,
)?,
);
for delegator_bid_key in delegator_bid_keys {
let delegator_bid = read_delegator_bid(provider, &delegator_bid_key)?;
ret.push(*delegator_bid);
Expand Down Expand Up @@ -1122,11 +1129,18 @@ where
{
let mut ret = vec![];
let bid_addr = BidAddr::from(validator_public_key.clone());
let reservation_bid_keys = provider.get_keys_by_prefix(
let mut reservation_bid_keys = provider.get_keys_by_prefix(
&bid_addr
.reserved_account_prefix()
.map_err(|_| Error::Serialization)?,
)?;
reservation_bid_keys.extend(
provider.get_keys_by_prefix(
&bid_addr
.reserved_purse_prefix()
.map_err(|_| Error::Serialization)?,
)?,
);
for reservation_bid_key in reservation_bid_keys {
let reservation_bid = read_reservation_bid(provider, &reservation_bid_key)?;
ret.push(*reservation_bid);
Expand Down Expand Up @@ -1294,11 +1308,18 @@ where
{
let mut ret = vec![];
let bid_addr = BidAddr::from(validator_public_key.clone());
let delegator_bid_keys = provider.get_keys_by_prefix(
let mut delegator_bid_keys = provider.get_keys_by_prefix(
&bid_addr
.delegated_account_prefix()
.map_err(|_| Error::Serialization)?,
)?;
delegator_bid_keys.extend(
provider.get_keys_by_prefix(
&bid_addr
.delegated_purse_prefix()
.map_err(|_| Error::Serialization)?,
)?,
);

for delegator_bid_key in delegator_bid_keys {
let delegator = read_delegator_bid(provider, &delegator_bid_key)?;
Expand All @@ -1318,11 +1339,18 @@ where
{
let mut ret = vec![];
let bid_addr = BidAddr::from(validator_public_key.clone());
let reservation_bid_keys = provider.get_keys_by_prefix(
let mut reservation_bid_keys = provider.get_keys_by_prefix(
&bid_addr
.reserved_account_prefix()
.map_err(|_| Error::Serialization)?,
)?;
reservation_bid_keys.extend(
provider.get_keys_by_prefix(
&bid_addr
.reserved_purse_prefix()
.map_err(|_| Error::Serialization)?,
)?,
);

for reservation_bid_key in reservation_bid_keys {
let reservation = read_reservation_bid(provider, &reservation_bid_key)?;
Expand Down
16 changes: 9 additions & 7 deletions storage/src/system/protocol_upgrade.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@ use casper_types::{
contracts::{ContractHash, NamedKeys},
system::{
auction::{
BidAddr, BidKind, SeigniorageRecipientsSnapshotV1, SeigniorageRecipientsSnapshotV2,
SeigniorageRecipientsV2, ValidatorBid, 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,
BidAddr, BidKind, DelegatorBid, DelegatorKind, SeigniorageRecipientsSnapshotV1,
SeigniorageRecipientsSnapshotV2, SeigniorageRecipientsV2, ValidatorBid,
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,
},
handle_payment::ACCUMULATION_PURSE_KEY,
mint::{
Expand Down Expand Up @@ -1182,7 +1182,7 @@ where
for (_, delegator) in delegators {
let delegator_bid_addr = BidAddr::new_delegator_kind(
validator_public_key,
delegator.delegator_kind(),
&DelegatorKind::PublicKey(delegator.delegator_public_key().clone()),
);
// the previous code was removing a delegator bid from the embedded
// collection within their validator's bid when the delegator fully
Expand All @@ -1191,7 +1191,9 @@ where
if !delegator.staked_amount().is_zero() {
tc.write(
delegator_bid_addr.into(),
StoredValue::BidKind(BidKind::Delegator(Box::new(delegator))),
StoredValue::BidKind(BidKind::Delegator(Box::new(DelegatorBid::from(
delegator,
)))),
);
}
}
Expand Down
55 changes: 52 additions & 3 deletions types/benches/bytesrepr_bench.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@ use casper_types::{
account::AccountHash,
addressable_entity::{ActionThresholds, AddressableEntity, AssociatedKeys, EntityKind},
bytesrepr::{self, Bytes, FromBytes, ToBytes},
system::auction::{Bid, DelegatorBid, DelegatorKind, EraInfo, SeigniorageAllocation},
system::auction::{
Bid, BidKind, Delegator, DelegatorBid, DelegatorKind, EraInfo, SeigniorageAllocation,
ValidatorBid,
},
AccessRights, AddressableEntityHash, ByteCodeHash, CLTyped, CLValue, DeployHash, DeployInfo,
EntityVersionKey, EntityVersions, Gas, Group, Groups, InitiatorAddr, Key, Package, PackageHash,
PackageStatus, ProtocolVersion, PublicKey, SecretKey, TransactionHash, TransactionRuntime,
Expand Down Expand Up @@ -544,7 +547,19 @@ fn u32_to_pk(i: u32) -> PublicKey {
PublicKey::from(&sk)
}

fn sample_delegators(delegators_len: u32) -> Vec<DelegatorBid> {
fn sample_delegators(delegators_len: u32) -> Vec<Delegator> {
(0..delegators_len)
.map(|i| {
let delegator_pk = u32_to_pk(i);
let staked_amount = U512::from_dec_str("123123123123123").unwrap();
let bonding_purse = URef::default();
let validator_pk = u32_to_pk(i);
Delegator::unlocked(delegator_pk, staked_amount, bonding_purse, validator_pk)
})
.collect()
}

fn sample_delegator_bids(delegators_len: u32) -> Vec<DelegatorBid> {
(0..delegators_len)
.map(|i| {
let delegator_pk = u32_to_pk(i);
Expand Down Expand Up @@ -577,7 +592,7 @@ fn sample_bid(delegators_len: u32) -> Bid {
let curr_delegators = bid.delegators_mut();
for delegator in new_delegators.into_iter() {
assert!(curr_delegators
.insert(delegator.delegator_kind().clone(), delegator)
.insert(delegator.delegator_public_key().clone(), delegator)
.is_none());
}
bid
Expand All @@ -587,6 +602,33 @@ fn serialize_bid(delegators_len: u32, b: &mut Bencher) {
let bid = sample_bid(delegators_len);
b.iter(|| Bid::to_bytes(black_box(&bid)));
}
fn serialize_delegation_bid(delegators_len: u32, b: &mut Bencher) {
let bids = sample_delegator_bids(delegators_len);
for bid in bids {
b.iter(|| BidKind::to_bytes(black_box(&BidKind::Delegator(Box::new(bid.clone())))));
}
}

fn sample_validator_bid() -> BidKind {
let validator_public_key = PublicKey::System;
let bonding_purse = URef::default();
let staked_amount = U512::from_dec_str("123123123123123").unwrap();
let delegation_rate = 10u8;
BidKind::Validator(Box::new(ValidatorBid::unlocked(
validator_public_key,
bonding_purse,
staked_amount,
delegation_rate,
0,
0,
0,
)))
}

fn serialize_validator_bid(b: &mut Bencher) {
let bid = sample_validator_bid();
b.iter(|| BidKind::to_bytes(black_box(&bid)));
}

fn deserialize_bid(delegators_len: u32, b: &mut Bencher) {
let bid = sample_bid(delegators_len);
Expand Down Expand Up @@ -799,6 +841,13 @@ fn bytesrepr_bench(c: &mut Criterion) {
"bytesrepr::deserialize_contract_package",
deserialize_contract_package,
);
c.bench_function(
"bytesrepr::serialize_validator_bid",
serialize_validator_bid,
);
c.bench_function("bytesrepr::serialize_delegation_bid", |b| {
serialize_delegation_bid(10, b)
});
c.bench_function("bytesrepr::serialize_bid_small", |b| serialize_bid(10, b));
c.bench_function("bytesrepr::serialize_bid_medium", |b| serialize_bid(100, b));
c.bench_function("bytesrepr::serialize_bid_big", |b| serialize_bid(1000, b));
Expand Down
7 changes: 5 additions & 2 deletions types/src/auction_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -142,8 +142,11 @@ impl AuctionState {
0,
);
let mut delegators: BTreeMap<DelegatorKind, DelegatorBid> = BTreeMap::new();
for (delegator_kind, delegator) in bid.delegators() {
delegators.insert(delegator_kind.clone(), delegator.clone());
for (delegator_public_key, delegator) in bid.delegators() {
delegators.insert(
DelegatorKind::PublicKey(delegator_public_key.clone()),
DelegatorBid::from(delegator.clone()),
);
}
staking.insert(public_key, (validator_bid, delegators));
}
Expand Down
Loading

0 comments on commit 6b13978

Please sign in to comment.