pub struct OracleReader { /* private fields */ }Implementations§
Source§impl OracleReader
impl OracleReader
Sourcepub fn set_key(&mut self, key: PreimageKey) -> Result<(), OracleError>
pub fn set_key(&mut self, key: PreimageKey) -> Result<(), OracleError>
Set the preimage key for the global oracle reader. This will overwrite any existing key
Internally this sends the 32 bytes of the key to the host by writing into the WritePreimage file descriptor. This may require several writes as the host may only accept a few bytes at a time. Once 32 bytes have been written successfully the key is considered set. If it fails to write 32 bytes it will return an error. Once it has written the key it will read the first 8 bytes of the ReadPreimage file descriptor which is the length encoded as a big endian u64. This is stored in the oracle reader along with the read cursor position.
§Examples
use cannon_io::prelude::*;
let mut oracle = oracle_reader();
oracle.set_key(PreimageKey::new_local(&[0xff;31]));Sourcepub fn key(&self) -> Option<PreimageKey>
pub fn key(&self) -> Option<PreimageKey>
Return the current key stored in the global oracle reader
Sourcepub fn get(&mut self, key: PreimageKey) -> Result<Vec<u8>, OracleError>
pub fn get(&mut self, key: PreimageKey) -> Result<Vec<u8>, OracleError>
Get the data corresponding to the currently set key from the host. Return the data in a new heap allocated Vec<u8>
Internally this reads self.length bytes from the ReadPreimage file descriptor into a new heap allocated Vec<u8> and returns it.
This is a high level way to interact with the preimage oracle but may not be the best way if heap allocations are not desirable.
§Examples
use cannon_io::prelude::*;
let mut oracle = oracle_reader();
let key = PreimageKey::new_local(&[0xff;31]);
let data = oracle.get(key).unwrap();Sourcepub fn get_exact(
&mut self,
key: PreimageKey,
buf: &mut [u8],
) -> Result<(), OracleError>
pub fn get_exact( &mut self, key: PreimageKey, buf: &mut [u8], ) -> Result<(), OracleError>
Get the data corresponding to the currently set key from the host. Write the data into the provided buffer
§Panics
This will panic if the size of the buffer is not equal to the size of the preimage as reported by the host
§Examples
use cannon_io::prelude::*;
let mut oracle = oracle_reader();
let key = PreimageKey::new_local(&[0xff;31]);
let mut buffer = [0_u8; 100];
oracle.get_exact(key, &mut buffer).unwrap();Trait Implementations§
Source§impl Read for OracleReader
impl Read for OracleReader
Source§fn read(&mut self, buf: &mut [u8]) -> Result<usize, Self::Error>
fn read(&mut self, buf: &mut [u8]) -> Result<usize, Self::Error>
Read up to buf.len() bytes from the ReadPreimage file descriptor into buf
This returns the number of bytes read. If the end of the data is reached it will return 0. Subsequent calls can be used to write the rest of the data as the host may only accept arbitrary small chunks at a time.
Source§fn read_to_end(&mut self, buf: &mut Vec<u8>) -> Result<usize, Self::Error>
fn read_to_end(&mut self, buf: &mut Vec<u8>) -> Result<usize, Self::Error>
Read all the data from the ReadPreimage file descriptor into buf
This will read all the data from the ReadPreimage file descriptor into buf. This is implemented by calling read() in a loop until it returns 0. This will read at most 32 bytes per call to read. New space will be allocated into the return buffer if it fills up.
Source§fn read_exact(&mut self, buf: &mut [u8]) -> Result<(), Self::Error>
fn read_exact(&mut self, buf: &mut [u8]) -> Result<(), Self::Error>
Read exactly buf.len() bytes from the ReadPreimage file descriptor into buf
This will read exactly buf.len() bytes from the ReadPreimage file descriptor into buf. This is implemented by calling read() in a loop until it returns 0. This will read at most 32 bytes per call to read. If the end of the data is reached before buf.len() bytes have been read it will return an error. After this function returns there may be more bytes in the stream to be read.