Skip to content

Commit

Permalink
changes
Browse files Browse the repository at this point in the history
  • Loading branch information
malik672 committed Dec 2, 2024
1 parent 69def8c commit 64346df
Showing 1 changed file with 54 additions and 55 deletions.
109 changes: 54 additions & 55 deletions examples/transient_storage.rs
Original file line number Diff line number Diff line change
@@ -1,65 +1,64 @@
// //! Transient Storage example.
// #![cfg_attr(target_arch = "wasm32", no_std)]
// #![cfg_attr(target_arch = "wasm32", no_main)]
//! Transient Storage example.
#![cfg_attr(target_arch = "wasm32", no_std)]
#![cfg_attr(target_arch = "wasm32", no_main)]

// extern crate zink;
extern crate zink;

// use zink::storage::TransientStorage;
use zink::storage::TransientStorage;
/// Temporary counter with value type `i32` that resets after each transaction
#[zink::transient_storage(i32)]
pub struct TempCounter;

// /// Temporary counter with value type `i32` that resets after each transaction
// #[zink::transient_storage(i32)]
// pub struct TempCounter;
/// Set value to the transient storage.
#[zink::external]
pub fn set_temp(value: i32) {
TempCounter::set(value);
}

// /// Set value to the transient storage.
// #[zink::external]
// pub fn set_temp(value: i32) {
// TempCounter::set(value);
// }
/// Get value from the transient storage.
#[zink::external]
pub fn get_temp() -> i32 {
TempCounter::get()
}

// /// Get value from the transient storage.
// #[zink::external]
// pub fn get_temp() -> i32 {
// TempCounter::get()
// }
#[cfg(not(target_arch = "wasm32"))]
fn main() {}

// #[cfg(not(target_arch = "wasm32"))]
// fn main() {}
#[test]
fn transient_value() -> anyhow::Result<()> {
use zint::{Bytes32, Contract, U256};

// #[test]
// fn transient_value() -> anyhow::Result<()> {
// use zint::{Bytes32, Contract, U256};
let mut contract = Contract::search("transient_storage")?.compile()?;
let value: i32 = 42;

// let mut contract = Contract::search("transient_storage")?.compile()?;
// let value: i32 = 42;
// First transaction: set value
{
let info = contract.execute(&[b"set_temp(int32)".to_vec(), value.to_bytes32().to_vec()])?;
assert!(info.ret.is_empty());
assert_eq!(
info.transient_storage
.get(&U256::from_le_bytes(TempCounter::STORAGE_KEY)),
Some(&U256::from(value))
);
// Verify regular storage is untouched
assert_eq!(
info.storage
.get(&U256::from_le_bytes(TempCounter::STORAGE_KEY)),
None
);
}

// // First transaction: set value
// {
// let info = contract.execute(&[b"set_temp(int32)".to_vec(), value.to_bytes32().to_vec()])?;
// assert!(info.ret.is_empty());
// assert_eq!(
// info.transient_storage
// .get(&U256::from_le_bytes(TempCounter::STORAGE_KEY)),
// Some(&U256::from(value))
// );
// // Verify regular storage is untouched
// assert_eq!(
// info.storage
// .get(&U256::from_le_bytes(TempCounter::STORAGE_KEY)),
// None
// );
// }
// Second transaction: value should be cleared
{
let info = contract.execute(&[b"get_temp()".to_vec()])?;
assert_eq!(info.ret, 0.to_bytes32());
// Verify transient storage was cleared
assert_eq!(
info.transient_storage
.get(&U256::from_le_bytes(TempCounter::STORAGE_KEY)),
None
);
}

// // Second transaction: value should be cleared
// {
// let info = contract.execute(&[b"get_temp()".to_vec()])?;
// assert_eq!(info.ret, 0.to_bytes32());
// // Verify transient storage was cleared
// assert_eq!(
// info.transient_storage
// .get(&U256::from_le_bytes(TempCounter::STORAGE_KEY)),
// None
// );
// }

// Ok(())
// }
Ok(())
}

0 comments on commit 64346df

Please sign in to comment.