Skip to content

Commit

Permalink
Rollup branch stabilization (#693)
Browse files Browse the repository at this point in the history
  • Loading branch information
mateuszaaa authored Feb 16, 2024
2 parents 5c9f0eb + c28afa2 commit a03f217
Show file tree
Hide file tree
Showing 3 changed files with 106 additions and 1 deletion.
37 changes: 36 additions & 1 deletion pallets/rolldown/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use frame_support::{
StorageHasher,
};
use frame_system::{ensure_signed, pallet_prelude::*};
use messages::UpdateType;
use messages::{PendingRequestType, UpdateType};
use sp_runtime::traits::SaturatedConversion;

use alloy_sol_types::SolValue;
Expand Down Expand Up @@ -158,6 +158,8 @@ pub mod pallet {
EmptyUpdate,
AddressDeserializationFailure,
RequestDoesNotExist,
TooManyRequests,
InvalidUpdate,
}

#[pallet::config]
Expand Down Expand Up @@ -215,6 +217,39 @@ pub mod pallet {
let sequencer = ensure_signed(origin)?;

ensure!(!requests.order.is_empty(), Error::<T>::EmptyUpdate);
ensure!(requests.order.len() <= 10, Error::<T>::TooManyRequests);

let withdraws_count = requests.pendingWithdraws.len();
let deposits_count = requests.pendingDeposits.len();
let cancels_count = requests.pendingCancelResultions.len();
let l2_updates_count = requests.pendingL2UpdatesToRemove.len();

ensure!(
requests.order.iter().filter(|e| **e == PendingRequestType::DEPOSIT).count() ==
deposits_count,
Error::<T>::InvalidUpdate
);
ensure!(
requests.order.iter().filter(|e| **e == PendingRequestType::WITHDRAWAL).count() ==
withdraws_count,
Error::<T>::InvalidUpdate
);
ensure!(
requests
.order
.iter()
.filter(|e| **e == PendingRequestType::CANCEL_RESOLUTION)
.count() == cancels_count,
Error::<T>::InvalidUpdate
);
ensure!(
requests
.order
.iter()
.filter(|e| **e == PendingRequestType::L2_UPDATES_TO_REMOVE)
.count() == l2_updates_count,
Error::<T>::InvalidUpdate
);

// check json length to prevent big data spam, maybe not necessary as it will be checked later and slashed
let current_block_number =
Expand Down
1 change: 1 addition & 0 deletions pallets/rolldown/src/messages.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ pub struct L1Update {
pub pendingL2UpdatesToRemove: Vec<L2UpdatesToRemove>,
}

#[derive(Eq, PartialEq, RuntimeDebug, Clone, Encode, Decode, TypeInfo, Serialize)]
pub enum L1UpdateRequest {
Withdraw(Withdraw),
Deposit(Deposit),
Expand Down
69 changes: 69 additions & 0 deletions pallets/rolldown/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,35 @@ fn deposit_executed_after_dispute_period() {
});
}

#[test]
#[serial]
fn each_request_executed_only_once() {
ExtBuilder::new()
.issue(ALICE, ETH_TOKEN_ADDRESS_MGX, 0u128)
.execute_with_default_mocks(|| {
forward_to_block::<Test>(10);
let update = create_l1_update(vec![L1UpdateRequest::Deposit(messages::Deposit {
depositRecipient: DummyAddressConverter::convert_back(CHARLIE),
tokenAddress: ETH_TOKEN_ADDRESS,
amount: sp_core::U256::from(MILLION),
})]);
Rolldown::update_l2_from_l1(RuntimeOrigin::signed(ALICE), update.clone()).unwrap();

forward_to_block::<Test>(11);
Rolldown::update_l2_from_l1(RuntimeOrigin::signed(BOB), update).unwrap();

forward_to_block::<Test>(14);
assert!(!pending_updates::<Test>::contains_key(sp_core::U256::from(0u128)));
assert_eq!(TokensOf::<Test>::free_balance(ETH_TOKEN_ADDRESS_MGX, &CHARLIE), 0_u128);

forward_to_block::<Test>(15);
assert_eq!(TokensOf::<Test>::free_balance(ETH_TOKEN_ADDRESS_MGX, &CHARLIE), MILLION);

forward_to_block::<Test>(20);
assert_eq!(TokensOf::<Test>::free_balance(ETH_TOKEN_ADDRESS_MGX, &CHARLIE), MILLION);
});
}

#[test]
#[serial]
fn withdraw_executed_after_dispute_period() {
Expand Down Expand Up @@ -336,6 +365,46 @@ fn cancel_request() {
});
}

#[test]
#[serial]
fn reject_update_with_too_many_requests() {
ExtBuilder::new().execute_with_default_mocks(|| {
forward_to_block::<Test>(10);

let requests = vec![
L1UpdateRequest::Withdraw(messages::Withdraw {
depositRecipient: ETH_RECIPIENT_ACCOUNT,
tokenAddress: ETH_TOKEN_ADDRESS,
amount: sp_core::U256::from(MILLION),
});
11
];

let withdraw_update = create_l1_update(requests);

assert_err!(
Rolldown::update_l2_from_l1(RuntimeOrigin::signed(ALICE), withdraw_update),
Error::<Test>::TooManyRequests
);
});
}

#[test]
#[serial]
fn reject_update_with_missing_requests() {
ExtBuilder::new().execute_with_default_mocks(|| {
forward_to_block::<Test>(10);

let update =
L1Update { order: vec![messages::PendingRequestType::DEPOSIT], ..Default::default() };

assert_err!(
Rolldown::update_l2_from_l1(RuntimeOrigin::signed(ALICE), update),
Error::<Test>::InvalidUpdate
);
});
}

#[test]
fn test_conversion_u256() {
let val = sp_core::U256::from(1u8);
Expand Down

0 comments on commit a03f217

Please sign in to comment.