diff --git a/ledger/src/tests.rs b/ledger/src/tests.rs index 5f8f3182c0..0c82ef111b 100644 --- a/ledger/src/tests.rs +++ b/ledger/src/tests.rs @@ -23,6 +23,7 @@ use console::{ account::{Address, PrivateKey}, network::prelude::*, program::{Entry, Identifier, Literal, Plaintext, ProgramID, Value}, + types::U16, }; use ledger_block::{ConfirmedTransaction, Rejected, Transaction}; use ledger_committee::{Committee, MIN_VALIDATOR_STAKE}; @@ -1738,6 +1739,89 @@ finalize foo: assert_eq!(block.transactions().transaction_ids().collect::>(), vec![&transfer_id, &deployment_id]); } +#[test] +fn test_metadata() { + let rng = &mut TestRng::default(); + + // Initialize the test environment. + let crate::test_helpers::TestEnv { ledger, private_key, .. } = crate::test_helpers::sample_test_env(rng); + + // Deploy a test program to the ledger. + let program_id = ProgramID::::from_str("metadata.aleo").unwrap(); + let program = Program::::from_str(&format!( + " +program {program_id}; +function is_block: + input r0 as u32.public; + async is_block r0 into r1; + output r1 as {program_id}/is_block.future; + +finalize is_block: + input r0 as u32.public; + assert.eq r0 block.height; + +function is_id: + input r0 as u16.public; + async is_id r0 into r1; + output r1 as {program_id}/is_id.future; + +finalize is_id: + input r0 as u16.public; + assert.eq r0 network.id; + ", + )) + .unwrap(); + + // Deploy. + let transaction = ledger.vm.deploy(&private_key, &program, None, 0, None, rng).unwrap(); + // Verify. + ledger.vm().check_transaction(&transaction, None, rng).unwrap(); + + // Construct the next block. + let block = + ledger.prepare_advance_to_next_beacon_block(&private_key, vec![], vec![], vec![transaction], rng).unwrap(); + // Advance to the next block. + ledger.advance_to_next_block(&block).unwrap(); + assert_eq!(ledger.latest_height(), 1); + assert_eq!(ledger.latest_hash(), block.hash()); + assert_eq!(program, ledger.get_program(program_id).unwrap()); + + // Execute functions `is_block` and `is_id` to assert that the on-chain state is as expected. + let inputs_block: [Value; 1] = [Value::from_str("2u32").unwrap()]; + let tx_block = ledger + .vm + .execute(&private_key, (&program_id, "is_block"), inputs_block.iter(), None, 0, None, rng) + .unwrap(); + let inputs_id: [Value; 1] = [Value::from(Literal::U16(U16::new(CurrentNetwork::ID)))]; + let tx_id = ledger + .vm + .execute(&private_key, (&program_id, "is_id"), inputs_id.iter(), None, 0, None, rng) + .unwrap(); + + // Construct the next block. + let block_2 = + ledger.prepare_advance_to_next_beacon_block(&private_key, vec![], vec![], vec![tx_id, tx_block], rng).unwrap(); + // Advance to the next block. + ledger.advance_to_next_block(&block_2).unwrap(); + + // Execute the program. + let inputs_block_2: [Value; 1] = [Value::from_str("3u32").unwrap()]; + let tx_block_2= ledger + .vm + .execute(&private_key, (&program_id, "is_block"), inputs_block_2.iter(), None, 0, None, rng) + .unwrap(); + let tx_id_2 = ledger + .vm + .execute(&private_key, (&program_id, "is_id"), inputs_id.iter(), None, 0, None, rng) + .unwrap(); + + // Construct the next block. + let block_3 = + ledger.prepare_advance_to_next_beacon_block(&private_key, vec![], vec![], vec![tx_block_2, tx_id_2], rng).unwrap(); + // Advance to the next block. + ledger.advance_to_next_block(&block_3).unwrap(); +} + // These tests require the proof targets to be low enough to be able to generate **valid** solutions. // This requires the 'test' feature to be enabled for the `console` dependency. #[cfg(feature = "test")]