use crate::{
traits::{FinalizeStoreTrait, RegistersLoad, RegistersStore, StackMatches, StackProgram},
Opcode,
Operand,
};
use console::{
network::prelude::*,
program::{Identifier, Locator, Register, Value},
};
use std::io::{BufRead, BufReader};
#[derive(Clone, PartialEq, Eq, Hash)]
pub enum MappingLocator<N: Network> {
Locator(Locator<N>),
Resource(Identifier<N>),
}
impl<N: Network> Parser for MappingLocator<N> {
#[inline]
fn parse(string: &str) -> ParserResult<Self> {
alt((
map(Locator::parse, |locator| MappingLocator::Locator(locator)),
map(Identifier::parse, |identifier| MappingLocator::Resource(identifier)),
))(string)
}
}
impl<N: Network> FromStr for MappingLocator<N> {
type Err = Error;
#[inline]
fn from_str(string: &str) -> Result<Self> {
match Self::parse(string) {
Ok((remainder, object)) => {
ensure!(remainder.is_empty(), "Failed to parse string. Found invalid character in: \"{remainder}\"");
Ok(object)
}
Err(error) => bail!("Failed to parse string. {error}"),
}
}
}
impl<N: Network> Debug for MappingLocator<N> {
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
Display::fmt(self, f)
}
}
impl<N: Network> Display for MappingLocator<N> {
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
match self {
MappingLocator::Locator(locator) => Display::fmt(locator, f),
MappingLocator::Resource(resource) => Display::fmt(resource, f),
}
}
}
impl<N: Network> FromBytes for MappingLocator<N> {
fn read_le<R: Read>(mut reader: R) -> IoResult<Self> {
let version = u8::read_le(&mut reader)?;
if version != 0 {
return Err(error("Failed to read MappingLocator. Invalid version."));
}
let variant = u8::read_le(&mut reader)?;
match variant {
0 => Ok(MappingLocator::Locator(Locator::read_le(&mut reader)?)),
1 => Ok(MappingLocator::Resource(Identifier::read_le(&mut reader)?)),
_ => Err(error("Failed to read MappingLocator. Invalid variant.")),
}
}
}
impl<N: Network> ToBytes for MappingLocator<N> {
fn write_le<W: Write>(&self, mut writer: W) -> IoResult<()> {
match self {
MappingLocator::Locator(locator) => {
0u8.write_le(&mut writer)?;
0u8.write_le(&mut writer)?;
locator.write_le(&mut writer)
}
MappingLocator::Resource(resource) => {
0u8.write_le(&mut writer)?;
1u8.write_le(&mut writer)?;
resource.write_le(&mut writer)
}
}
}
}
#[derive(Clone)]
pub struct Get<N: Network> {
mapping: MappingLocator<N>,
key: Operand<N>,
destination: Register<N>,
}
impl<N: Network> PartialEq for Get<N> {
#[inline]
fn eq(&self, other: &Self) -> bool {
self.mapping == other.mapping && self.key == other.key && self.destination == other.destination
}
}
impl<N: Network> Eq for Get<N> {}
impl<N: Network> std::hash::Hash for Get<N> {
#[inline]
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
self.mapping.hash(state);
self.key.hash(state);
self.destination.hash(state);
}
}
impl<N: Network> Get<N> {
#[inline]
pub const fn opcode() -> Opcode {
Opcode::Command("get")
}
#[inline]
pub fn operands(&self) -> Vec<Operand<N>> {
vec![self.key.clone()]
}
#[inline]
pub const fn mapping(&self) -> &MappingLocator<N> {
&self.mapping
}
#[inline]
pub const fn key(&self) -> &Operand<N> {
&self.key
}
#[inline]
pub const fn destination(&self) -> &Register<N> {
&self.destination
}
}
impl<N: Network> Get<N> {
#[inline]
pub fn finalize(
&self,
stack: &(impl StackMatches<N> + StackProgram<N>),
store: &impl FinalizeStoreTrait<N>,
registers: &mut (impl RegistersLoad<N> + RegistersStore<N>),
) -> Result<()> {
let (program_id, mapping_name) = match self.mapping {
MappingLocator::Locator(locator) => (*locator.program_id(), *locator.resource()),
MappingLocator::Resource(mapping_name) => (*stack.program_id(), mapping_name),
};
if !store.contains_mapping_confirmed(&program_id, &mapping_name)? {
bail!("Mapping '{program_id}/{mapping_name}' does not exist in storage");
}
let key = registers.load_plaintext(stack, &self.key)?;
let value = match store.get_value_speculative(program_id, mapping_name, &key)? {
Some(Value::Plaintext(plaintext)) => Value::Plaintext(plaintext),
Some(Value::Record(..)) => bail!("Cannot 'get' a 'record'"),
Some(Value::Future(..)) => bail!("Cannot 'get' a 'future'",),
None => bail!("Key '{key}' does not exist in mapping '{program_id}/{mapping_name}'"),
};
registers.store(stack, &self.destination, value)?;
Ok(())
}
}
impl<N: Network> Parser for Get<N> {
#[inline]
fn parse(string: &str) -> ParserResult<Self> {
let (string, _) = Sanitizer::parse(string)?;
let (string, _) = tag(*Self::opcode())(string)?;
let (string, _) = Sanitizer::parse_whitespaces(string)?;
let (string, mapping) = MappingLocator::parse(string)?;
let (string, _) = tag("[")(string)?;
let (string, _) = Sanitizer::parse_whitespaces(string)?;
let (string, key) = Operand::parse(string)?;
let (string, _) = Sanitizer::parse_whitespaces(string)?;
let (string, _) = tag("]")(string)?;
let (string, _) = Sanitizer::parse_whitespaces(string)?;
let (string, _) = tag("into")(string)?;
let (string, _) = Sanitizer::parse_whitespaces(string)?;
let (string, destination) = Register::parse(string)?;
let (string, _) = Sanitizer::parse_whitespaces(string)?;
let (string, _) = tag(";")(string)?;
Ok((string, Self { mapping, key, destination }))
}
}
impl<N: Network> FromStr for Get<N> {
type Err = Error;
#[inline]
fn from_str(string: &str) -> Result<Self> {
match Self::parse(string) {
Ok((remainder, object)) => {
ensure!(remainder.is_empty(), "Failed to parse string. Found invalid character in: \"{remainder}\"");
Ok(object)
}
Err(error) => bail!("Failed to parse string. {error}"),
}
}
}
impl<N: Network> Debug for Get<N> {
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
Display::fmt(self, f)
}
}
impl<N: Network> Display for Get<N> {
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
write!(f, "{} ", Self::opcode())?;
write!(f, "{}[{}] into ", self.mapping, self.key)?;
write!(f, "{};", self.destination)
}
}
impl<N: Network> FromBytes for Get<N> {
fn read_le<R: Read>(reader: R) -> IoResult<Self> {
let mut reader = BufReader::with_capacity(1, reader);
let first_byte = {
let buffer = reader.fill_buf()?;
match buffer.first() {
Some(byte) => *byte,
None => return Err(error("Failed to read `get`. Expected byte.")),
}
};
let mapping = match first_byte {
0u8 => MappingLocator::read_le(&mut reader)?,
_ => MappingLocator::Resource(Identifier::read_le(&mut reader)?),
};
let key = Operand::read_le(&mut reader)?;
let destination = Register::read_le(&mut reader)?;
Ok(Self { mapping, key, destination })
}
}
impl<N: Network> ToBytes for Get<N> {
fn write_le<W: Write>(&self, mut writer: W) -> IoResult<()> {
match &self.mapping {
MappingLocator::Locator(_) => self.mapping.write_le(&mut writer)?,
MappingLocator::Resource(identifier) => identifier.write_le(&mut writer)?,
}
self.key.write_le(&mut writer)?;
self.destination.write_le(&mut writer)
}
}
#[cfg(test)]
mod tests {
use super::*;
use console::{network::Testnet3, program::Register};
type CurrentNetwork = Testnet3;
struct OldGet<N: Network> {
mapping: Identifier<N>,
key: Operand<N>,
destination: Register<N>,
}
impl<N: Network> ToBytes for OldGet<N> {
fn write_le<W: Write>(&self, mut writer: W) -> IoResult<()>
where
Self: Sized,
{
self.mapping.write_le(&mut writer)?;
self.key.write_le(&mut writer)?;
self.destination.write_le(&mut writer)
}
}
#[test]
fn test_parse() {
let (string, get) = Get::<CurrentNetwork>::parse("get account[r0] into r1;").unwrap();
assert!(string.is_empty(), "Parser did not consume all of the string: '{string}'");
assert_eq!(get.mapping, MappingLocator::from_str("account").unwrap());
assert_eq!(get.operands().len(), 1, "The number of operands is incorrect");
assert_eq!(get.key, Operand::Register(Register::Locator(0)), "The first operand is incorrect");
assert_eq!(get.destination, Register::Locator(1), "The second operand is incorrect");
let (string, get) = Get::<CurrentNetwork>::parse("get token.aleo/balances[r0] into r1;").unwrap();
assert!(string.is_empty(), "Parser did not consume all of the string: '{string}'");
assert_eq!(get.mapping, MappingLocator::from_str("token.aleo/balances").unwrap());
assert_eq!(get.operands().len(), 1, "The number of operands is incorrect");
assert_eq!(get.key, Operand::Register(Register::Locator(0)), "The first operand is incorrect");
assert_eq!(get.destination, Register::Locator(1), "The second operand is incorrect");
}
#[test]
fn test_from_bytes() {
let (string, get) = Get::<CurrentNetwork>::parse("get account[r0] into r1;").unwrap();
assert!(string.is_empty());
let old_get = OldGet::<CurrentNetwork> {
mapping: Identifier::from_str("account").unwrap(),
key: Operand::Register(Register::Locator(0)),
destination: Register::Locator(1),
};
let get_bytes = get.to_bytes_le().unwrap();
let old_get_bytes = old_get.to_bytes_le().unwrap();
let first = Get::<CurrentNetwork>::from_bytes_le(&get_bytes[..]).unwrap();
let second = Get::<CurrentNetwork>::from_bytes_le(&old_get_bytes[..]).unwrap();
assert_eq!(first, second);
}
}