Skip to content

Commit

Permalink
improve tests for convertL1 and disableL1
Browse files Browse the repository at this point in the history
  • Loading branch information
chcmedeiros committed Nov 21, 2024
1 parent 9dd2260 commit cbc6678
Show file tree
Hide file tree
Showing 8 changed files with 316 additions and 62 deletions.
4 changes: 2 additions & 2 deletions app/rust/src/parser/constants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@ pub const PVM_ADD_PERMISSIONLESS_DELEGATOR: u32 = 0x0000001A;
pub const PVM_CONVERT_SUBNET_L1: u32 = 0x00000023;
pub const PVM_REGISTER_L1_VALIDATOR: u32 = 0x00000024;
pub const PVM_SET_L1_VALIDATOR_WEIGHT: u32 = 0x00000025;
pub const PVM_DISABLE_L1_VALIDATOR: u32 = 0x00000026;
pub const PVM_INCREASE_L1_VALIDATOR_BALANCE: u32 = 0x00000027;
pub const PVM_DISABLE_L1_VALIDATOR: u32 = 0x00000027;
pub const PVM_INCREASE_L1_VALIDATOR_BALANCE: u32 = 0x00000026;

// base tx for p-chain which collides with evm import tx.
// we should check chain-id
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,7 @@ mod tests {
assert_eq!(tx.chain_id, manager_chain_id);
assert_eq!(tx.address, manager_address);
}

#[test]
#[cfg_attr(miri, ignore)]
fn ui_convert_subnet_to_l1() {
Expand Down
67 changes: 63 additions & 4 deletions app/rust/src/parser/transactions/pvm/banff/disable_l1_validator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,10 @@
* limitations under the License.
********************************************************************************/

use crate::utils::hex_encode;
use core::{mem::MaybeUninit, ptr::addr_of_mut};

use nom::bytes::complete::{tag, take};

use zemu_sys::ViewError;

use crate::{
Expand Down Expand Up @@ -100,24 +101,34 @@ impl<'b> DisplayableItem for DisableL1ValidatorTx<'b> {
) -> Result<u8, zemu_sys::ViewError> {
use bolos::{pic_str, PIC};
use lexical_core::Number;
let mut buffer = [0; u64::FORMATTED_SIZE_DECIMAL + 2];

match item_n {
0 => {
let label = pic_str!(b"DisableValidator");
let label = pic_str!(b"DisableL1Val");
title[..label.len()].copy_from_slice(label);
let content = pic_str!(b"transaction");
handle_ui_message(content, message, page)
}
1 => {
let prefix = pic_str!(b"0x"!);
let label = pic_str!(b"Validation ID");
title[..label.len()].copy_from_slice(label);
handle_ui_message(self.validation_id, message, page)

// prefix
let mut out = [0; VALIDATION_ID_LEN * 2 + 2];
let mut sz = prefix.len();
out[..prefix.len()].copy_from_slice(&prefix[..]);

sz += hex_encode(self.validation_id, &mut out[prefix.len()..])
.map_err(|_| ViewError::Unknown)?;

handle_ui_message(&out[..sz], message, page)
}
2 => {
let label = pic_str!(b"Fee(AVAX)");
title[..label.len()].copy_from_slice(label);

let mut buffer = [0; u64::FORMATTED_SIZE_DECIMAL + 2];
let fee = self.fee().map_err(|_| ViewError::Unknown)?;
let fee_buff =
nano_avax_to_fp_str(fee, &mut buffer[..]).map_err(|_| ViewError::Unknown)?;
Expand All @@ -128,3 +139,51 @@ impl<'b> DisplayableItem for DisableL1ValidatorTx<'b> {
}
}
}

#[cfg(test)]
mod tests {
use super::*;
use std::prelude::v1::*;

use crate::parser::snapshots_common::ReducedPage;
use zuit::Page;

const DATA: &[u8] = &[];

include!("testvectors/disable_l1_validator.rs");
#[test]
fn parse_disable_l1_validator() {
let validation_id = &[
0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef, 0x01, 0x23, 0x45, 0x67, 0x89, 0xab,
0xcd, 0xef, 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef, 0x01, 0x23, 0x45, 0x67,
0x89, 0xab, 0xcd, 0xef,
];

let (_, tx) = DisableL1ValidatorTx::from_bytes(DISABLE_L1_VALIDATOR_DATA).unwrap();
assert_eq!(tx.validation_id, validation_id);
assert_eq!(tx.disable_auth.sig_indices.len(), 1);
assert_eq!(tx.disable_auth.sig_indices[0][3], 9);
}

#[test]
#[cfg_attr(miri, ignore)]
fn ui_disable_l1_validator() {
println!("-------------------- Disable L1 Validator TX ------------------------");
let (_, tx) = DisableL1ValidatorTx::from_bytes(DISABLE_L1_VALIDATOR_DATA).unwrap();

let items = tx.num_items().expect("Overflow?");

let mut pages = Vec::<Page<18, 1024>>::with_capacity(items as usize);
for i in 0..items {
let mut page = Page::default();

tx.render_item(i as _, &mut page.title, &mut page.message, 0)
.unwrap();

pages.push(page);
}

let reduced = pages.iter().map(ReducedPage::from).collect::<Vec<_>>();
insta::assert_debug_snapshot!(reduced);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -112,8 +112,10 @@ impl<'b> DisplayableItem for IncreaseL1ValidatorBalanceTx<'b> {
}
1 => {
let label = pic_str!(b"Validation ID");
let mut data = [0; 32];
data[..32].copy_from_slice(&self.validation_id[..]);
title[..label.len()].copy_from_slice(label);
handle_ui_message(self.validation_id, message, page)
handle_ui_message(&data, message, page)
}
2 => {
let label = pic_str!(b"Amount(AVAX)");
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
---
source: src/parser/transactions/pvm/banff/convert_subnet_to_l1.rs
assertion_line: 230
expression: reduced
---
[
"ConvertSubnetToL1": "Transaction",
"SubnetID": "SkB92YpWm4UpburLz9tEKZw2i67H3FF6YkjaU4BkFUDTG9Xm",
"Chain ID": "0x3132333435363738212223242526272811121314151617180102030405060708",
"Subnet Manager": "0x000000000000000000000000000000000000dead",
"Fee(AVAX)": "1152921505.606846975",
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
---
source: src/parser/transactions/pvm/banff/disable_l1_validator.rs
assertion_line: 190
expression: reduced
---
[
"DisableL1Val": "transaction",
"Validation ID": "0x0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef",
"Fee(AVAX)": "1152921506.606846974",
]
Loading

0 comments on commit cbc6678

Please sign in to comment.