Skip to content

Commit

Permalink
Support component packing
Browse files Browse the repository at this point in the history
  • Loading branch information
tarrencev committed Sep 8, 2023
1 parent 933646f commit 1330c7c
Show file tree
Hide file tree
Showing 13 changed files with 550 additions and 268 deletions.
12 changes: 10 additions & 2 deletions crates/dojo-core/src/component.cairo
Original file line number Diff line number Diff line change
@@ -1,10 +1,18 @@
trait Component<T> {
fn name(self: @T) -> felt252;
fn keys(self: @T) -> Span<felt252>;
fn values(self: @T) -> Span<felt252>;
fn pack(self: @T) -> Span<felt252>;
fn unpack(ref packed: Span<felt252>) -> Option<T>;
}

#[starknet::interface]
trait INamed<T> {
trait IComponent<T> {
fn name(self: @T) -> felt252;
fn layout(self: @T) -> Span<felt252>;
fn schema(self: @T) -> Array<(felt252, felt252, bool)>;
}

#[starknet::interface]
trait ISystem<T> {
fn name(self: @T) -> felt252;
}
87 changes: 44 additions & 43 deletions crates/dojo-core/src/database/storage.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -62,127 +62,128 @@ fn set_many(address_domain: u32, keys: Span<felt252>, offset: u8, mut value: Spa
}


trait StorageSize<T> {
fn unpacked_size() -> usize;
fn packed_size() -> usize;
trait StorageLayout<T> {
fn size() -> usize;
fn layout(ref layout: Array<u8>);
}

impl StorageSizeFelt252 of StorageSize<felt252> {
impl StorageLayoutFelt252 of StorageLayout<felt252> {
#[inline(always)]
fn unpacked_size() -> usize {
fn size() -> usize {
1
}

#[inline(always)]
fn packed_size() -> usize {
252
fn layout(ref layout: Array<u8>) {
layout.append(252);
}
}

impl StorageSizeBool of StorageSize<bool> {
impl StorageLayoutBool of StorageLayout<bool> {
#[inline(always)]
fn unpacked_size() -> usize {
fn size() -> usize {
1
}

#[inline(always)]
fn packed_size() -> usize {
1
fn layout(ref layout: Array<u8>) {
layout.append(1);
}
}

impl StorageSizeU8 of StorageSize<u8> {
impl StorageLayoutU8 of StorageLayout<u8> {
#[inline(always)]
fn unpacked_size() -> usize {
fn size() -> usize {
1
}

#[inline(always)]
fn packed_size() -> usize {
8
fn layout(ref layout: Array<u8>) {
layout.append(8);
}
}

impl StorageSizeU16 of StorageSize<u16> {
impl StorageLayoutU16 of StorageLayout<u16> {
#[inline(always)]
fn unpacked_size() -> usize {
fn size() -> usize {
1
}

#[inline(always)]
fn packed_size() -> usize {
16
fn layout(ref layout: Array<u8>) {
layout.append(16);
}
}

impl StorageSizeU32 of StorageSize<u32> {
impl StorageLayoutU32 of StorageLayout<u32> {
#[inline(always)]
fn unpacked_size() -> usize {
fn size() -> usize {
1
}

#[inline(always)]
fn packed_size() -> usize {
32
fn layout(ref layout: Array<u8>) {
layout.append(32);
}
}

impl StorageSizeU64 of StorageSize<u64> {
impl StorageLayoutU64 of StorageLayout<u64> {
#[inline(always)]
fn unpacked_size() -> usize {
fn size() -> usize {
1
}

#[inline(always)]
fn packed_size() -> usize {
64
fn layout(ref layout: Array<u8>) {
layout.append(64);
}
}

impl StorageSizeU128 of StorageSize<u128> {
impl StorageLayoutU128 of StorageLayout<u128> {
#[inline(always)]
fn unpacked_size() -> usize {
fn size() -> usize {
1
}

#[inline(always)]
fn packed_size() -> usize {
128
fn layout(ref layout: Array<u8>) {
layout.append(128);
}
}

impl StorageSizeU256 of StorageSize<u256> {
impl StorageLayoutU256 of StorageLayout<u256> {
#[inline(always)]
fn unpacked_size() -> usize {
fn size() -> usize {
2
}

#[inline(always)]
fn packed_size() -> usize {
256
fn layout(ref layout: Array<u8>) {
layout.append(128);
layout.append(128);
}
}

impl StorageSizeContractAddress of StorageSize<starknet::ContractAddress> {
impl StorageLayoutContractAddress of StorageLayout<starknet::ContractAddress> {
#[inline(always)]
fn unpacked_size() -> usize {
fn size() -> usize {
1
}

#[inline(always)]
fn packed_size() -> usize {
256
fn layout(ref layout: Array<u8>) {
layout.append(251);
}
}

impl StorageSizeClassHash of StorageSize<starknet::ClassHash> {
impl StorageLayoutClassHash of StorageLayout<starknet::ClassHash> {
#[inline(always)]
fn unpacked_size() -> usize {
fn size() -> usize {
1
}

#[inline(always)]
fn packed_size() -> usize {
256
fn layout(ref layout: Array<u8>) {
layout.append(251);
}
}
2 changes: 1 addition & 1 deletion crates/dojo-core/src/lib.cairo
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
mod database;
use database::storage::StorageSize;
use database::storage::StorageLayout;
#[cfg(test)]
mod database_test;
mod executor;
Expand Down
19 changes: 8 additions & 11 deletions crates/dojo-core/src/packing.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,23 @@ use traits::{Into, TryInto};
use integer::{U256BitAnd, U256BitOr, U256BitXor, upcast, downcast, BoundedInt};
use option::OptionTrait;

#[derive(Copy, Drop)]
struct LayoutItem {
value: felt252,
size: u8
}

fn pack(ref unpacked: Array<LayoutItem>) -> Span<felt252> {
fn pack(ref unpacked: Span<felt252>, ref layout: Span<u8>) -> Span<felt252> {
// assert(unpacked.len() == layout.len(), "mismatched input lens");
let mut packed: Array<felt252> = ArrayTrait::new();
let mut packing: felt252 = 0x0;
let mut offset: u8 = 0x0;
loop {
match unpacked.pop_front() {
Option::Some(s) => {
pack_inner(@s.value, s.size, ref packing, ref offset, ref packed);
Option::Some(item) => {
pack_inner(item, *layout.pop_front().unwrap(), ref packing, ref offset, ref packed);
},
Option::None(_) => {
break packed.span();
break;
}
};
}
};
packed.append(packing);
packed.span()
}

fn unpack(ref packed: Span<felt252>, ref layout: Span<u8>) -> Option<Span<felt252>> {
Expand Down
Loading

0 comments on commit 1330c7c

Please sign in to comment.