Skip to content

Commit

Permalink
Merge pull request #2281 from AleoHQ/feat/fee-based-on-operand-size
Browse files Browse the repository at this point in the history
Price Finalize Opcodes Based On Operand Types
  • Loading branch information
howardwu authored Jan 28, 2024
2 parents 285fa0e + 21c9ca1 commit 90b3515
Show file tree
Hide file tree
Showing 19 changed files with 418 additions and 144 deletions.
1 change: 1 addition & 0 deletions console/program/src/data/literal/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ mod parse;
mod sample;
mod serialize;
mod size_in_bits;
mod size_in_bytes;
mod to_bits;
mod to_type;
mod variant;
Expand Down
26 changes: 26 additions & 0 deletions console/program/src/data/literal/size_in_bytes.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// Copyright (C) 2019-2023 Aleo Systems Inc.
// This file is part of the snarkVM library.

// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at:
// http://www.apache.org/licenses/LICENSE-2.0

// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

use super::*;

impl<N: Network> Literal<N> {
/// Returns the size in bytes of this literal.
#[allow(clippy::cast_possible_truncation)]
pub fn size_in_bytes(&self) -> u16 {
// Note: This upcast to u32 and downcast to u16 is safe because the size of a literal is
// always less than or equal to u16::MAX bits, and we are dividing by 8, so the result will
// always fit in a u16.
(((self.size_in_bits() as u32) + 7) / 8) as u16
}
}
4 changes: 4 additions & 0 deletions console/program/src/data_types/literal_type/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,12 @@
mod bytes;
mod parse;
mod serialize;
mod size_in_bits;
mod size_in_bytes;

use snarkvm_console_account::Signature;
use snarkvm_console_network::prelude::*;
use snarkvm_console_types::{prelude::*, Boolean};

use core::fmt::{self, Debug, Display};
use num_derive::FromPrimitive;
Expand Down
43 changes: 43 additions & 0 deletions console/program/src/data_types/literal_type/size_in_bits.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// Copyright (C) 2019-2023 Aleo Systems Inc.
// This file is part of the snarkVM library.

// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at:
// http://www.apache.org/licenses/LICENSE-2.0

// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

use super::*;

impl LiteralType {
/// Returns the number of bits of this literal type.
///
/// For string literals, this method returns the maximum number of bits that can be stored in the string.
pub fn size_in_bits<N: Network>(&self) -> u16 {
let size = match self {
Self::Address => Address::<N>::size_in_bits(),
Self::Boolean => Boolean::<N>::size_in_bits(),
Self::Field => Field::<N>::size_in_bits(),
Self::Group => Group::<N>::size_in_bits(),
Self::I8 => I8::<N>::size_in_bits(),
Self::I16 => I16::<N>::size_in_bits(),
Self::I32 => I32::<N>::size_in_bits(),
Self::I64 => I64::<N>::size_in_bits(),
Self::I128 => I128::<N>::size_in_bits(),
Self::U8 => U8::<N>::size_in_bits(),
Self::U16 => U16::<N>::size_in_bits(),
Self::U32 => U32::<N>::size_in_bits(),
Self::U64 => U64::<N>::size_in_bits(),
Self::U128 => U128::<N>::size_in_bits(),
Self::Scalar => Scalar::<N>::size_in_bits(),
Self::Signature => Signature::<N>::size_in_bits(),
Self::String => N::MAX_STRING_BYTES.saturating_mul(8) as usize,
};
u16::try_from(size).or_halt_with::<N>("Literal exceeds u16::MAX bits.")
}
}
28 changes: 28 additions & 0 deletions console/program/src/data_types/literal_type/size_in_bytes.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// Copyright (C) 2019-2023 Aleo Systems Inc.
// This file is part of the snarkVM library.

// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at:
// http://www.apache.org/licenses/LICENSE-2.0

// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

use super::*;

impl LiteralType {
/// Returns the number of bytes of this literal.
///
/// For string literals, this method returns the maximum number of bytes that can be stored in the string.
#[allow(clippy::cast_possible_truncation)]
pub fn size_in_bytes<N: Network>(&self) -> u16 {
// Note: This upcast to u32 and downcast to u16 is safe because the size of a literal is
// always less than or equal to u16::MAX bits, and we are dividing by 8, so the result will
// always fit in a u16.
(((self.size_in_bits::<N>() as u32) + 7) / 8) as u16
}
}
Binary file modified parameters/src/testnet3/resources/block.genesis
Binary file not shown.
2 changes: 1 addition & 1 deletion synthesizer/program/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,7 @@ impl<N: Network, Instruction: InstructionTrait<N>, Command: CommandTrait<N>> Pro
/// Returns a reference to the function with the given name.
pub fn get_function_ref(&self, name: &Identifier<N>) -> Result<&FunctionCore<N, Instruction, Command>> {
// Attempt to retrieve the function.
let function = self.functions.get(name).ok_or_else(|| anyhow!("Function '{name}' is not defined."))?;
let function = self.functions.get(name).ok_or(anyhow!("Function '{}/{name}' is not defined.", self.id))?;
// Ensure the function name matches.
ensure!(function.name() == name, "Expected function '{name}', but found function '{}'", function.name());
// Ensure the number of inputs is within the allowed range.
Expand Down
Loading

0 comments on commit 90b3515

Please sign in to comment.