Skip to content

Commit

Permalink
change entry point name
Browse files Browse the repository at this point in the history
  • Loading branch information
Maciej Wójcik committed Feb 27, 2024
1 parent dae2bf6 commit 1ea90ac
Show file tree
Hide file tree
Showing 16 changed files with 113 additions and 114 deletions.
16 changes: 8 additions & 8 deletions Cargo.lock

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

13 changes: 6 additions & 7 deletions execution_engine/src/runtime/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -994,16 +994,15 @@ where
CLValue::from_t(()).map_err(Self::reverter)
})(),

auction::METHOD_TRANSFER_VALIDATOR => (|| {
runtime.charge_system_contract_call(auction_costs.transfer_validator)?;
auction::METHOD_CHANGE_BID_PUBLIC_KEY => (|| {
runtime.charge_system_contract_call(auction_costs.change_bid_public_key)?;

let validator_public_key =
Self::get_named_argument(runtime_args, auction::ARG_VALIDATOR)?;
let new_validator_public_key =
Self::get_named_argument(runtime_args, auction::ARG_NEW_VALIDATOR)?;
let public_key = Self::get_named_argument(runtime_args, auction::ARG_PUBLIC_KEY)?;
let new_public_key =
Self::get_named_argument(runtime_args, auction::ARG_NEW_PUBLIC_KEY)?;

runtime
.transfer_validator(validator_public_key, new_validator_public_key)
.change_bid_public_key(public_key, new_public_key)
.map_err(Self::reverter)?;

CLValue::from_t(()).map_err(Self::reverter)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ use casper_execution_engine::{
};
use casper_storage::data_access_layer::GenesisRequest;

use casper_types::system::auction::ARG_NEW_PUBLIC_KEY;
use casper_types::{
self,
account::AccountHash,
Expand Down Expand Up @@ -48,7 +49,7 @@ const CONTRACT_WITHDRAW_BID: &str = "withdraw_bid.wasm";
const CONTRACT_DELEGATE: &str = "delegate.wasm";
const CONTRACT_UNDELEGATE: &str = "undelegate.wasm";
const CONTRACT_REDELEGATE: &str = "redelegate.wasm";
const CONTRACT_TRANSFER_VALIDATOR: &str = "transfer_validator.wasm";
const CONTRACT_CHANGE_BID_PUBLIC_KEY: &str = "change_bid_public_key.wasm";

const TRANSFER_AMOUNT: u64 = MINIMUM_ACCOUNT_CREATION_BALANCE + 1000;

Expand Down Expand Up @@ -4354,7 +4355,7 @@ fn should_increase_existing_delegation_when_limit_exceeded() {

#[ignore]
#[test]
fn should_fail_transfer_if_conflicting_validator_bid_exists() {
fn should_fail_bid_public_key_change_if_conflicting_validator_bid_exists() {
let system_fund_request = ExecuteRequestBuilder::standard(
*DEFAULT_ACCOUNT_ADDR,
CONTRACT_TRANSFER_TO_ACCOUNT,
Expand Down Expand Up @@ -4450,10 +4451,10 @@ fn should_fail_transfer_if_conflicting_validator_bid_exists() {

let validator_1_transfer_request = ExecuteRequestBuilder::standard(
*NON_FOUNDER_VALIDATOR_1_ADDR,
CONTRACT_TRANSFER_VALIDATOR,
CONTRACT_CHANGE_BID_PUBLIC_KEY,
runtime_args! {
ARG_VALIDATOR => NON_FOUNDER_VALIDATOR_1_PK.clone(),
ARG_NEW_VALIDATOR => NON_FOUNDER_VALIDATOR_2_PK.clone()
ARG_PUBLIC_KEY => NON_FOUNDER_VALIDATOR_1_PK.clone(),
ARG_NEW_PUBLIC_KEY => NON_FOUNDER_VALIDATOR_2_PK.clone()
},
)
.build();
Expand All @@ -4464,12 +4465,12 @@ fn should_fail_transfer_if_conflicting_validator_bid_exists() {
assert!(matches!(
error,
Error::Exec(execution::Error::Revert(ApiError::AuctionError(auction_error)))
if auction_error == AuctionError::TransferValidatorBid as u8));
if auction_error == AuctionError::ChangeBidPublicKey as u8));
}

#[ignore]
#[test]
fn should_transfer_validator_bid() {
fn should_change_validator_bid_public_key() {
let system_fund_request = ExecuteRequestBuilder::standard(
*DEFAULT_ACCOUNT_ADDR,
CONTRACT_TRANSFER_TO_ACCOUNT,
Expand Down Expand Up @@ -4582,10 +4583,10 @@ fn should_transfer_validator_bid() {

let validator_1_transfer_request = ExecuteRequestBuilder::standard(
*NON_FOUNDER_VALIDATOR_1_ADDR,
CONTRACT_TRANSFER_VALIDATOR,
CONTRACT_CHANGE_BID_PUBLIC_KEY,
runtime_args! {
ARG_VALIDATOR => NON_FOUNDER_VALIDATOR_1_PK.clone(),
ARG_NEW_VALIDATOR => NON_FOUNDER_VALIDATOR_2_PK.clone()
ARG_PUBLIC_KEY => NON_FOUNDER_VALIDATOR_1_PK.clone(),
ARG_NEW_PUBLIC_KEY => NON_FOUNDER_VALIDATOR_2_PK.clone()
},
)
.build();
Expand Down
2 changes: 1 addition & 1 deletion resources/local/chainspec.toml.in
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,7 @@ withdraw_validator_reward = 10_000
read_era_id = 10_000
activate_bid = 10_000
redelegate = 2_500_000_000
transfer_validator = 2_500_000_000
change_bid_public_key = 2_500_000_000

[system_costs.mint_costs]
mint = 2_500_000_000
Expand Down
2 changes: 1 addition & 1 deletion resources/production/chainspec.toml
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,7 @@ withdraw_validator_reward = 10_000
read_era_id = 10_000
activate_bid = 10_000
redelegate = 2_500_000_000
transfer_validator = 2_500_000_000
change_bid_public_key = 2_500_000_000

[system_costs.mint_costs]
mint = 2_500_000_000
Expand Down
2 changes: 1 addition & 1 deletion resources/test/valid/0_9_0/chainspec.toml
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ withdraw_validator_reward = 10_000
read_era_id = 10_000
activate_bid = 10_000
redelegate = 10_000
transfer_validator = 10_000
change_bid_public_key = 10_000

[system_costs.mint_costs]
mint = 2_500_000_000
Expand Down
2 changes: 1 addition & 1 deletion resources/test/valid/0_9_0_unordered/chainspec.toml
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ withdraw_validator_reward = 10_000
read_era_id = 10_000
activate_bid = 10_000
redelegate = 10_000
transfer_validator = 10_000
change_bid_public_key = 10_000

[system_costs.mint_costs]
mint = 2_500_000_000
Expand Down
2 changes: 1 addition & 1 deletion resources/test/valid/1_0_0/chainspec.toml
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ withdraw_validator_reward = 10_000
read_era_id = 10_000
activate_bid = 10_000
redelegate = 10_000
transfer_validator = 10_000
change_bid_public_key = 10_000

[system_costs.mint_costs]
mint = 2_500_000_000
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
[package]
name = "transfer_validator"
name = "change_bid_public_key"
version = "0.1.0"
authors = ["Maciej Wójcik <[email protected]>"]
edition = "2018"

[[bin]]
name = "transfer_validator"
name = "change_bid_public_key"
bench = false
doctest = false
test = false
Expand Down
32 changes: 32 additions & 0 deletions smart_contracts/contracts/client/change_bid_public_key/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#![no_std]
#![no_main]

extern crate alloc;

use casper_contract::contract_api::{runtime, system};
use casper_types::{
runtime_args,
system::auction::{ARG_NEW_PUBLIC_KEY, ARG_PUBLIC_KEY, METHOD_CHANGE_BID_PUBLIC_KEY},
PublicKey,
};

fn change_bid_public_key(public_key: PublicKey, new_public_key: PublicKey) {
let contract_hash = system::get_auction();
let args = runtime_args! {
ARG_PUBLIC_KEY => public_key,
ARG_NEW_PUBLIC_KEY => new_public_key
};
runtime::call_contract::<()>(contract_hash, METHOD_CHANGE_BID_PUBLIC_KEY, args);
}

// Change validator bid public key.
//
// Accepts current bid's public key and new public key.
// Updates existing validator bid and all related delegator bids with
// the new public key.
#[no_mangle]
pub extern "C" fn call() {
let public_key = runtime::get_named_arg(ARG_PUBLIC_KEY);
let new_public_key = runtime::get_named_arg(ARG_NEW_PUBLIC_KEY);
change_bid_public_key(public_key, new_public_key);
}
32 changes: 0 additions & 32 deletions smart_contracts/contracts/client/transfer_validator/src/main.rs

This file was deleted.

45 changes: 21 additions & 24 deletions storage/src/system/auction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -700,39 +700,38 @@ pub trait Auction:
}
}

/// Updates a `ValidatorBid` and all related delegator bids to use a new validator public key.
/// Updates a `ValidatorBid` and all related delegator bids to use a new public key.
///
/// This in effect "transfers" a validator along with its stake and all delegators
/// This in effect "transfers" a validator bid along with its stake and all delegators
/// from one public key to another.
/// This method can only be called by the existing validator.
/// This method can only be called by the account associated with the current `ValidatorBid`.
///
/// The arguments are the existing validator's key and the new validator's key.
fn transfer_validator(
/// The arguments are the existing bids's 'validator_public_key' and the new public key.
fn change_bid_public_key(
&mut self,
validator_public_key: PublicKey,
new_validator_public_key: PublicKey,
public_key: PublicKey,
new_public_key: PublicKey,
) -> Result<(), Error> {
let validator_account_hash =
AccountHash::from_public_key(&validator_public_key, |x| self.blake2b(x));
let validator_account_hash = AccountHash::from_public_key(&public_key, |x| self.blake2b(x));

// check that the caller is the existing validator
// check that the caller is the current bid's owner
if !self.is_allowed_session_caller(&validator_account_hash) {
return Err(Error::InvalidContext);
}

// verify that a bid for existing validator exists
let validator_bid_addr = BidAddr::from(validator_public_key.clone());
// verify that a bid for given public key exists
let validator_bid_addr = BidAddr::from(public_key.clone());
let mut validator_bid = read_validator_bid(self, &validator_bid_addr.into())?;

// verify that a bid for new validator does not exist yet
let new_validator_bid_addr = BidAddr::from(new_validator_public_key.clone());
// verify that a bid for the new key does not exist yet
let new_validator_bid_addr = BidAddr::from(new_public_key.clone());
if self.read_bid(&new_validator_bid_addr.into())?.is_some() {
return Err(Error::TransferValidatorBid);
return Err(Error::ChangeBidPublicKey);
}

debug!("transferring validator bid from {validator_bid_addr} to {new_validator_bid_addr}");

validator_bid.with_validator_public_key(new_validator_public_key.clone());
validator_bid.with_validator_public_key(new_public_key.clone());
self.write_bid(
new_validator_bid_addr.into(),
BidKind::Validator(validator_bid),
Expand All @@ -741,18 +740,16 @@ pub trait Auction:
debug!("pruning validator bid {}", validator_bid_addr);
self.prune_bid(validator_bid_addr);

debug!("transferring delegator bids from validator {validator_bid_addr} to {new_validator_bid_addr}");
let delegators = read_delegator_bids(self, &validator_public_key)?;
debug!("transferring delegator bids from validator bid {validator_bid_addr} to {new_validator_bid_addr}");
let delegators = read_delegator_bids(self, &public_key)?;
for mut delegator in delegators {
let delegator_public_key = delegator.delegator_public_key().clone();
let delegator_bid_addr =
BidAddr::new_from_public_keys(&validator_public_key, Some(&delegator_public_key));
BidAddr::new_from_public_keys(&public_key, Some(&delegator_public_key));

delegator.with_validator_public_key(new_validator_public_key.clone());
let new_delegator_bid_addr = BidAddr::new_from_public_keys(
&new_validator_public_key,
Some(&delegator_public_key),
);
delegator.with_validator_public_key(new_public_key.clone());
let new_delegator_bid_addr =
BidAddr::new_from_public_keys(&new_public_key, Some(&delegator_public_key));

self.write_bid(
new_delegator_bid_addr.into(),
Expand Down
Loading

0 comments on commit 1ea90ac

Please sign in to comment.