-
Notifications
You must be signed in to change notification settings - Fork 1
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
22 additions
and
8 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,37 +1,51 @@ | ||
use starknet::storage_access::{Store, StorageBaseAddress}; | ||
use utils::hash::Digest; | ||
use utils::hash::{Digest, U256IntoDigest, DigestIntoU256}; | ||
|
||
// todo: pack the felts to save on storage | ||
pub impl DigestStore of starknet::Store<Digest> { | ||
fn read(address_domain: u32, base: StorageBaseAddress) -> starknet::SyscallResult<Digest> { | ||
match Store::<[u32; 8]>::read(address_domain, base) { | ||
Result::Ok(value) => Result::Ok(Digest { value }), | ||
match Store::<felt252>::read(address_domain, base) { | ||
Result::Ok(value) => { | ||
let value_as_u256: u256 = value.into(); | ||
Result::Ok(value_as_u256.into()) | ||
}, | ||
Result::Err(err) => Result::Err(err), | ||
} | ||
} | ||
|
||
fn write( | ||
address_domain: u32, base: StorageBaseAddress, value: Digest | ||
) -> starknet::SyscallResult<()> { | ||
Store::<[u32; 8]>::write(address_domain, base, value.value) | ||
let value_as_u256: u256 = value.into(); | ||
Store::<felt252>::write(address_domain, base, value_as_u256.try_into().unwrap()) | ||
} | ||
|
||
fn read_at_offset( | ||
address_domain: u32, base: StorageBaseAddress, offset: u8 | ||
) -> starknet::SyscallResult<Digest> { | ||
match Store::<[u32; 8]>::read_at_offset(address_domain, base, offset) { | ||
Result::Ok(value) => Result::Ok(Digest { value }), | ||
match Store::<felt252>::read_at_offset(address_domain, base, offset) { | ||
Result::Ok(value) => { | ||
let value_as_u256: u256 = value.into(); | ||
Result::Ok(value_as_u256.into()) | ||
}, | ||
Result::Err(err) => Result::Err(err), | ||
} | ||
} | ||
|
||
fn write_at_offset( | ||
address_domain: u32, base: StorageBaseAddress, offset: u8, value: Digest | ||
) -> starknet::SyscallResult<()> { | ||
Store::<[u32; 8]>::write_at_offset(address_domain, base, offset, value.value) | ||
let value_as_u256: u256 = value.into(); | ||
Store::< | ||
felt252 | ||
>::write_at_offset(address_domain, base, offset, value_as_u256.try_into().unwrap()) | ||
} | ||
|
||
// Returns 1 since we store the Bitcoin hash (256 bits) as a single felt (251 bits). | ||
// This is possible because in Bitcoin mainnet, due to minimum difficulty requirements, | ||
// the first 32 bits of the hash are always zeros. | ||
fn size() -> u8 { | ||
Store::<[u32; 8]>::size() | ||
// Store::<[u32; 8]>::size() | ||
1 | ||
} | ||
} |