Skip to content

Commit c1250d2

Browse files
Merge pull request #5346 from EdHastingsCasperAssociation/feat-2.1-vm2-reservations
[VM2] Add / Cancel Reservation modification
2 parents 6e4a610 + c08b2ff commit c1250d2

File tree

3 files changed

+108
-47
lines changed

3 files changed

+108
-47
lines changed

executor/wasm/tests/integration.rs

Lines changed: 72 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -356,7 +356,7 @@ fn exec_system_call(system_menu: SystemMenu, initiator: Option<AccountHash>) {
356356
let initiator = initiator.unwrap_or(DEFAULT_STABLE_VALIDATOR_PUBLIC_KEY.to_account_hash());
357357

358358
let system_function_option: u32 = system_menu.into();
359-
let input_data = borsh::to_vec(&(system_function_option,))
359+
let input_data = borsh::to_vec(&(system_function_option, false))
360360
.map(Bytes::from)
361361
.unwrap();
362362
let execute_request = make_execution_request(
@@ -423,7 +423,7 @@ fn should_revert_invalid_system_option() {
423423
let block_time = Timestamp::now().into();
424424

425425
let account_hash = DEFAULT_STABLE_VALIDATOR_PUBLIC_KEY.to_account_hash();
426-
let input_data = borsh::to_vec(&(9999,)).map(Bytes::from).unwrap();
426+
let input_data = borsh::to_vec(&(9999, false)).map(Bytes::from).unwrap();
427427

428428
let execute_request = make_execution_request(
429429
&chainspec_config,
@@ -543,7 +543,7 @@ fn should_handle_reservations() {
543543
// need to bump the delegator reservation limit up to allow add_reservation to work
544544
let bid_request = {
545545
let opt: u32 = SystemMenu::Auction(AuctionMethods::Bid).into();
546-
let input_data = borsh::to_vec(&(opt,)).map(Bytes::from).unwrap();
546+
let input_data = borsh::to_vec(&(opt, false)).map(Bytes::from).unwrap();
547547
make_execution_request(
548548
&chainspec_config,
549549
Arc::clone(&address_generator),
@@ -579,9 +579,9 @@ fn should_handle_reservations() {
579579
}
580580

581581
// make a couple of reservations
582-
let add_res_request = {
582+
let add_res_pubk_request = {
583583
let opt: u32 = SystemMenu::Auction(AuctionMethods::AddReservation).into();
584-
let input_data = borsh::to_vec(&(opt,)).map(Bytes::from).unwrap();
584+
let input_data = borsh::to_vec(&(opt, false)).map(Bytes::from).unwrap();
585585
make_execution_request(
586586
&chainspec_config,
587587
Arc::clone(&address_generator),
@@ -594,16 +594,45 @@ fn should_handle_reservations() {
594594
)
595595
};
596596

597-
state_root_hash =
598-
match exec_and_commit(&executor, &global_state, &state_root_hash, add_res_request) {
599-
Ok(post_state) => post_state,
600-
Err(err_str) => panic!("{err_str}"),
601-
};
597+
state_root_hash = match exec_and_commit(
598+
&executor,
599+
&global_state,
600+
&state_root_hash,
601+
add_res_pubk_request,
602+
) {
603+
Ok(post_state) => post_state,
604+
Err(err_str) => panic!("{err_str}"),
605+
};
606+
607+
let add_res_purse_request = {
608+
let opt: u32 = SystemMenu::Auction(AuctionMethods::AddReservation).into();
609+
let input_data = borsh::to_vec(&(opt, true)).map(Bytes::from).unwrap();
610+
make_execution_request(
611+
&chainspec_config,
612+
Arc::clone(&address_generator),
613+
ExecutionKind::SessionBytes(read_wasm(VM2_SYSTEM_CALLER_WASM)),
614+
input_data,
615+
0,
616+
Some(account_hash),
617+
None,
618+
Some(block_time),
619+
)
620+
};
621+
622+
state_root_hash = match exec_and_commit(
623+
&executor,
624+
&global_state,
625+
&state_root_hash,
626+
add_res_purse_request,
627+
) {
628+
Ok(post_state) => post_state,
629+
Err(err_str) => panic!("{err_str}"),
630+
};
602631

603632
// cancel those reservations
604-
let cancel_request = {
633+
let cancel_pubk_request = {
605634
let opt: u32 = SystemMenu::Auction(AuctionMethods::CancelReservation).into();
606-
let input_data = borsh::to_vec(&(opt,)).map(Bytes::from).unwrap();
635+
let input_data = borsh::to_vec(&(opt, false)).map(Bytes::from).unwrap();
607636
make_execution_request(
608637
&chainspec_config,
609638
Arc::clone(&address_generator),
@@ -616,7 +645,37 @@ fn should_handle_reservations() {
616645
)
617646
};
618647

619-
match exec_and_commit(&executor, &global_state, &state_root_hash, cancel_request) {
648+
match exec_and_commit(
649+
&executor,
650+
&global_state,
651+
&state_root_hash,
652+
cancel_pubk_request,
653+
) {
654+
Ok(post_state) => post_state,
655+
Err(err_str) => panic!("{err_str}"),
656+
};
657+
658+
let cancel_purse_request = {
659+
let opt: u32 = SystemMenu::Auction(AuctionMethods::CancelReservation).into();
660+
let input_data = borsh::to_vec(&(opt, true)).map(Bytes::from).unwrap();
661+
make_execution_request(
662+
&chainspec_config,
663+
Arc::clone(&address_generator),
664+
ExecutionKind::SessionBytes(read_wasm(VM2_SYSTEM_CALLER_WASM)),
665+
input_data,
666+
0,
667+
Some(account_hash),
668+
None,
669+
Some(block_time),
670+
)
671+
};
672+
673+
match exec_and_commit(
674+
&executor,
675+
&global_state,
676+
&state_root_hash,
677+
cancel_purse_request,
678+
) {
620679
Ok(post_state) => post_state,
621680
Err(err_str) => panic!("{err_str}"),
622681
};

executor/wasm_host/src/system.rs

Lines changed: 18 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -534,34 +534,31 @@ pub fn native_exec<A, T: ToBytes, R: GlobalStateReader + 'static>(
534534
}
535535
}
536536
AuctionMethods::AddReservation => {
537-
let ret = bytesrepr::deserialize_from_slice::<&Bytes, (Vec<Reservation>,)>(&input);
537+
let ret = bytesrepr::deserialize_from_slice::<&Bytes, (Reservation,)>(&input);
538538
if let Err(err) = &ret {
539539
debug!(?err, "bytesrepr error in native_exec AddReservation");
540540
}
541541
let unpacked =
542542
ret.map_err(|_err| ExecuteError::Fatal(FatalHostError::TypeConversion))?;
543-
let reservations = unpacked.0;
544-
for reservation in &reservations {
545-
if reservation.validator_public_key().is_system() {
543+
let reservation = unpacked.0;
544+
if reservation.validator_public_key().is_system() {
545+
debug!(
546+
?method,
547+
"attempt to pass system public key from userland AddReservation validator"
548+
);
549+
return Err(ExecuteError::Fatal(FatalHostError::InvalidPublicKey));
550+
}
551+
if let DelegatorKind::PublicKey(delegator_public_key) = reservation.delegator_kind()
552+
{
553+
if delegator_public_key.is_system() {
546554
debug!(
547-
?method,
548-
"attempt to pass system public key from userland AddReservation validator"
549-
);
550-
return Err(ExecuteError::Fatal(FatalHostError::InvalidPublicKey));
551-
}
552-
if let DelegatorKind::PublicKey(delegator_public_key) =
553-
reservation.delegator_kind()
554-
{
555-
if delegator_public_key.is_system() {
556-
debug!(
557555
?method,
558556
"attempt to pass system public key from userland AddReservation delegator"
559557
);
560-
return Err(ExecuteError::Fatal(FatalHostError::InvalidPublicKey));
561-
}
558+
return Err(ExecuteError::Fatal(FatalHostError::InvalidPublicKey));
562559
}
563560
}
564-
let add_reservations_args = AddReservationsArgs::new(reservations);
561+
let add_reservations_args = AddReservationsArgs::new(vec![reservation]);
565562
system::add_reservations(
566563
&mut tracking_copy,
567564
runtime_native_config,
@@ -572,7 +569,7 @@ pub fn native_exec<A, T: ToBytes, R: GlobalStateReader + 'static>(
572569
.map(|_| None)
573570
}
574571
AuctionMethods::CancelReservation => {
575-
let unpacked: (PublicKey, Vec<DelegatorKind>) =
572+
let unpacked: (PublicKey, DelegatorKind) =
576573
bytesrepr::deserialize_from_slice(&input)
577574
.map_err(|_err| ExecuteError::Fatal(FatalHostError::TypeConversion))?;
578575
if unpacked.0.is_system() {
@@ -582,11 +579,12 @@ pub fn native_exec<A, T: ToBytes, R: GlobalStateReader + 'static>(
582579
);
583580
return Err(ExecuteError::Fatal(FatalHostError::InvalidPublicKey));
584581
}
582+
let reservations = vec![unpacked.1];
585583
let cancel_reservations_args = CancelReservationsArgs::new(
586584
// validator
587585
unpacked.0,
588-
// delegators
589-
unpacked.1,
586+
// delegator
587+
reservations,
590588
runtime_native_config.max_delegators_per_validator(),
591589
);
592590
system::cancel_reservations(

smart_contracts/contracts/vm2/vm2-system-caller/src/lib.rs

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ pub mod exports {
99
};
1010

1111
#[casper(export)]
12-
pub fn call(opt: u32) {
12+
pub fn call(opt: u32, purse_delegation: bool) {
1313
use borsh;
1414

1515
let option = match SystemContractOption::try_from(opt) {
@@ -92,23 +92,27 @@ pub mod exports {
9292
}
9393
SystemContractOption::AddReservation => {
9494
let pub_k = PublicKey::Ed25519([1; 32]);
95-
let res_pu = Reservation::new(DelegatorKind::Purse([254; 32]), pub_k, 1);
96-
let res_pk = Reservation::new(
97-
DelegatorKind::PublicKey(PublicKey::Ed25519([255; 32])),
98-
pub_k,
99-
1,
100-
);
101-
let reservations = vec![res_pu, res_pk];
102-
let args = (reservations,);
95+
let reservation = if purse_delegation {
96+
Reservation::new(DelegatorKind::Purse([254; 32]), pub_k, 1)
97+
} else {
98+
Reservation::new(
99+
DelegatorKind::PublicKey(PublicKey::Ed25519([255; 32])),
100+
pub_k,
101+
1,
102+
)
103+
};
104+
105+
let args = (reservation,);
103106
let input = borsh::to_vec(&args).expect("Serialization to succeed");
104107
Some(input)
105108
}
106109
SystemContractOption::CancelReservation => {
107-
let reservations = vec![
108-
DelegatorKind::Purse([254; 32]),
109-
DelegatorKind::PublicKey(PublicKey::Ed25519([255; 32])),
110-
];
111-
let args = (PublicKey::Ed25519([1; 32]), reservations);
110+
let delegator_kind = if purse_delegation {
111+
DelegatorKind::Purse([254; 32])
112+
} else {
113+
DelegatorKind::PublicKey(PublicKey::Ed25519([255; 32]))
114+
};
115+
let args = (PublicKey::Ed25519([1; 32]), delegator_kind);
112116
let input = borsh::to_vec(&args).expect("Serialization to succeed");
113117
Some(input)
114118
}

0 commit comments

Comments
 (0)