Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rb tree cleanup #88

Merged
merged 3 commits into from
Jul 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions src/contracts/option_round.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -182,20 +182,20 @@ mod OptionRound {
use pitch_lake_starknet::contracts::{
market_aggregator::{IMarketAggregatorDispatcher, IMarketAggregatorDispatcherTrait},
utils::{
red_black_tree::{rb_tree_component, ClearingPriceReturn, rb_tree_component::Node},
red_black_tree::{RBTreeComponent, ClearingPriceReturn, RBTreeComponent::Node},
utils::{min, max}
},
vault::{Vault::VaultType, IVaultDispatcher, IVaultDispatcherTrait},
option_round::IOptionRound
};


component!(path: rb_tree_component, storage: bids_tree, event: BidTreeEvent);
component!(path: RBTreeComponent, storage: bids_tree, event: BidTreeEvent);

#[abi(embed_v0)]
impl RBTreeImpl = rb_tree_component::RBTree<ContractState>;
impl RBTreeImpl = RBTreeComponent::RBTree<ContractState>;

impl RBTreeInternalImpl = rb_tree_component::InternalImpl<ContractState>;
impl RBTreeInternalImpl = RBTreeComponent::InternalImpl<ContractState>;

impl BidPartialOrdTrait of PartialOrd<Bid> {
// @return if lhs < rhs
Expand Down Expand Up @@ -283,7 +283,7 @@ mod OptionRound {
bids_head: felt252,
bids_tail: felt252,
#[substorage(v0)]
bids_tree: rb_tree_component::Storage,
bids_tree: RBTreeComponent::Storage,
}

// The parameters needed to construct an option round
Expand Down Expand Up @@ -332,7 +332,7 @@ mod OptionRound {
OptionSettle: OptionSettle,
UnusedBidsRefunded: UnusedBidsRefunded,
OptionsExercised: OptionsExercised,
BidTreeEvent: rb_tree_component::Event,
BidTreeEvent: RBTreeComponent::Event,
}

// Emitted when the auction starts
Expand Down
78 changes: 11 additions & 67 deletions src/contracts/utils/red_black_tree.cairo
Original file line number Diff line number Diff line change
@@ -1,36 +1,31 @@
use pitch_lake_starknet::contracts::{
utils::red_black_tree::rb_tree_component::ClearingPriceReturn, option_round::OptionRound::Bid
utils::red_black_tree::RBTreeComponent::ClearingPriceReturn, option_round::OptionRound::Bid
};
use starknet::ContractAddress;
#[starknet::interface]
trait IRBTree<TContractState> {
fn insert(ref self: TContractState, value: Bid);
fn find(ref self: TContractState, value: Bid) -> felt252;
fn delete(ref self: TContractState, value: Bid);
fn find_clearing_price(
ref self: TContractState, total_options_available: u256
) -> Option<ClearingPriceReturn>;
fn find_options_for(
ref self: TContractState, bidder: ContractAddress, clearing_bid: felt252
) -> u256;
fn delete(ref self: TContractState, value: Bid);
fn get_root(self: @TContractState) -> felt252;
fn traverse_postorder(ref self: TContractState);
fn get_height(ref self: TContractState) -> u256;
fn get_tree_structure(ref self: TContractState) -> Array<Array<(Bid, bool, u256)>>;
fn is_tree_valid(ref self: TContractState) -> bool;
}

const BLACK: bool = false;
const RED: bool = true;


#[starknet::component]
pub mod rb_tree_component {
pub mod RBTreeComponent {
use pitch_lake_starknet::contracts::utils::red_black_tree::IRBTree;
use super::{BLACK, RED, Bid, ContractAddress};
use core::{array::ArrayTrait, option::OptionTrait, traits::{IndexView, TryInto}};


#[storage]
struct Storage {
root: felt252,
Expand All @@ -40,7 +35,6 @@ pub mod rb_tree_component {
clearing_bid_amount_sold: u256,
}


#[derive(Copy, Drop, Serde, starknet::Store, PartialEq)]
struct Node {
value: Bid,
Expand Down Expand Up @@ -73,7 +67,6 @@ pub mod rb_tree_component {
node: Node,
}


#[embeddable_as(RBTree)]
impl RBTreeImpl<
TContractState, +HasComponent<TContractState>
Expand All @@ -88,13 +81,20 @@ pub mod rb_tree_component {

self.insert_node_recursively(self.root.read(), new_node_id, value);
self.balance_after_insertion(new_node_id);

}

fn find(ref self: ComponentState<TContractState>, value: Bid) -> felt252 {
self.find_node(self.root.read(), value)
}

fn delete(ref self: ComponentState<TContractState>, value: Bid) {
let node_to_delete_id = self.find_node(self.root.read(), value);
if node_to_delete_id == 0 {
return;
}
self.delete_node(node_to_delete_id);
}

fn find_clearing_price(
ref self: ComponentState<TContractState>, total_options_available: u256
) -> Option<ClearingPriceReturn> {
Expand Down Expand Up @@ -129,27 +129,6 @@ pub mod rb_tree_component {
)
}


fn delete(ref self: ComponentState<TContractState>, value: Bid) {
let node_to_delete_id = self.find_node(self.root.read(), value);
if node_to_delete_id == 0 {
return;
}
self.delete_node(node_to_delete_id);
}

fn get_root(self: @ComponentState<TContractState>) -> felt252 {
self.root.read()
}

fn traverse_postorder(ref self: ComponentState<TContractState>) {
self.traverse_postorder_from_node(self.root.read());
}

fn get_height(ref self: ComponentState<TContractState>) -> u256 {
return self.get_sub_tree_height(self.root.read());
}

fn get_tree_structure(
ref self: ComponentState<TContractState>
) -> Array<Array<(Bid, bool, u256)>> {
Expand All @@ -165,19 +144,6 @@ pub mod rb_tree_component {
pub impl InternalImpl<
TContractState, +HasComponent<TContractState>
> of InternalTrait<TContractState> {
fn traverse_postorder_from_node(
ref self: ComponentState<TContractState>, current_id: felt252
) {
if (current_id == 0) {
return;
}
let current_node = self.tree.read(current_id);

self.traverse_postorder_from_node(current_node.right);
println!("{}", current_node.value);
self.traverse_postorder_from_node(current_node.left);
}

fn traverse_postorder_calculate_options_from_node(
ref self: ComponentState<TContractState>,
current_id: felt252,
Expand Down Expand Up @@ -234,17 +200,12 @@ pub mod rb_tree_component {
TraverseReturn::RemainingOptions((
_clearing_price, remaining
)) => {
//println!("PENDING{} {} ",_clearing_price,remaining);
remaining_options = remaining;
clearing_price = _clearing_price;
}
}

// println!("current_node\n{}\n",current_node.value);

//Check for self
if (current_node.value.amount >= remaining_options) {
// println!("IN HERE");
self.clearing_bid_amount_sold.write(remaining_options);

return Option::Some(TraverseReturn::ClearingPriceFelt(current_id));
Expand Down Expand Up @@ -356,23 +317,6 @@ pub mod rb_tree_component {
self.tree.write(node_id, node);
}

fn get_sub_tree_height(ref self: ComponentState<TContractState>, node_id: felt252) -> u256 {
let node = self.tree.read(node_id);

if (node_id == 0) {
return 0;
} else {
let left_height = self.get_sub_tree_height(node.left);
let right_height = self.get_sub_tree_height(node.right);

if (left_height > right_height) {
return left_height + 1;
} else {
return right_height + 1;
}
}
}

fn get_parent(ref self: ComponentState<TContractState>, node_id: felt252) -> felt252 {
if node_id == 0 {
0
Expand Down
Loading