-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
54 additions
and
55 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(()) | ||
} |