Skip to content

Commit

Permalink
Merge branch 'v0.1.0' into 336-audit-n-03-inconsistent-error-types
Browse files Browse the repository at this point in the history
  • Loading branch information
bidzyyys committed Oct 15, 2024
2 parents bed6b5e + 3b560b3 commit ee62177
Show file tree
Hide file tree
Showing 4 changed files with 195 additions and 83 deletions.
6 changes: 2 additions & 4 deletions .github/workflows/check-links.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,14 @@ jobs:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4

- name: Link Checker
uses: lycheeverse/lychee-action@v1
with:
args: --no-progress './**/*.md'
fail: true

- name: Run linkspector
uses: umbrelladocs/action-linkspector@v1
with:
fail_on_error: true


23 changes: 11 additions & 12 deletions contracts/src/token/erc721/extensions/consecutive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,18 +43,20 @@ use crate::{
structs::{
bitmap::{BitMap, IBitMap},
checkpoints,
checkpoints::{ITrace160, Trace160, U96},
checkpoints::{ITrace, Size, Trace, S160},
},
},
};

type U96 = <S160 as Size>::Key;

sol_storage! {
/// State of an [`Erc721Consecutive`] token.
pub struct Erc721Consecutive {
/// Erc721 contract storage.
Erc721 erc721;
/// Checkpoint library contract for sequential ownership.
Trace160 _sequential_ownership;
Trace<S160> _sequential_ownership;
/// BitMap library contract for sequential burn of tokens.
BitMap _sequential_burn;
/// Used to offset the first token id in
Expand Down Expand Up @@ -803,18 +805,15 @@ mod tests {
use alloy_primitives::{address, uint, Address, U256};
use stylus_sdk::msg;

use crate::{
token::{
erc721,
erc721::{
extensions::consecutive::{
ERC721ExceededMaxBatchMint, Erc721Consecutive, Error,
},
tests::random_token_id,
ERC721InvalidReceiver, ERC721NonexistentToken, IErc721,
use crate::token::{
erc721,
erc721::{
extensions::consecutive::{
ERC721ExceededMaxBatchMint, Erc721Consecutive, Error, U96,
},
tests::random_token_id,
ERC721InvalidReceiver, ERC721NonexistentToken, IErc721,
},
utils::structs::checkpoints::U96,
};

const BOB: Address = address!("F4EaCDAbEf3c8f1EdE91b6f2A6840bc2E4DD3526");
Expand Down
100 changes: 100 additions & 0 deletions contracts/src/utils/structs/checkpoints/generic_size.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
//! Contains generic size utilities for checkpoint storage contract.

use core::ops::{Add, Div, Mul, Sub};

use stylus_sdk::{alloy_primitives::Uint, prelude::*};

/// Trait that associates types of specific size for checkpoints key and value.
pub trait Size {
/// Type of the key in abi.
type Key: Num;

/// Type of the key in storage.
type KeyStorage: for<'a> StorageType<Wraps<'a> = Self::Key>
+ Accessor<Wraps = Self::Key>;

/// Type of the value in abi.
type Value: Num;

/// Type of the value in storage.
type ValueStorage: for<'a> StorageType<Wraps<'a> = Self::Value>
+ Accessor<Wraps = Self::Value>;
}

/// Defines size of checkpoint storage contract with specific key and value
/// bits.
///
/// # Arguments
///
/// * `$name` - Identifier of the typed size.
/// * `$key_bits` - Number of bits in checkpoint's key.
/// * `$value_bits` - Number of bits in checkpoint's value.
macro_rules! define_checkpoint_size {
($name:ident, $key_bits:expr, $value_bits:expr) => {
#[doc = "Size of checkpoint storage contract with"]
#[doc = stringify!($key_bits)]
#[doc = "bit key and "]
#[doc = stringify!($value_bits)]
#[doc = "bit value."]
pub struct $name;

impl Size for $name {
type Key = stylus_sdk::alloy_primitives::Uint<
$key_bits,
{ usize::div_ceil($key_bits, 64) },
>;
type KeyStorage = stylus_sdk::storage::StorageUint<
$key_bits,
{ usize::div_ceil($key_bits, 64) },
>;
type Value = stylus_sdk::alloy_primitives::Uint<
$value_bits,
{ usize::div_ceil($value_bits, 64) },
>;
type ValueStorage = stylus_sdk::storage::StorageUint<
$value_bits,
{ usize::div_ceil($value_bits, 64) },
>;
}
};
}

define_checkpoint_size!(S160, 96, 160);
define_checkpoint_size!(S224, 32, 224);
define_checkpoint_size!(S208, 48, 208);

/// Abstracts number inside the checkpoint contract.
pub trait Num: Add + Sub + Mul + Div + Ord + Sized + Copy {
/// Zero value of the number.
const ZERO: Self;
}

impl<const B: usize, const L: usize> Num for Uint<B, L> {
const ZERO: Self = Self::ZERO;
}

/// Abstracts accessor inside the checkpoint contract.
pub trait Accessor {
/// Type of the number associated with the storage type.
type Wraps: Num;

/// Gets underlying element [`Self::Wraps`] from persistent storage.
fn get(&self) -> Self::Wraps;

/// Sets underlying element [`Self::Wraps`] in persistent storage.
fn set(&mut self, value: Self::Wraps);
}

impl<const B: usize, const L: usize> Accessor
for stylus_sdk::storage::StorageUint<B, L>
{
type Wraps = Uint<B, L>;

fn get(&self) -> Self::Wraps {
self.get()
}

fn set(&mut self, value: Self::Wraps) {
self.set(value);
}
}
Loading

0 comments on commit ee62177

Please sign in to comment.