Skip to content

Commit

Permalink
Merge pull request #8 from hakymulla/constructor
Browse files Browse the repository at this point in the history
Constructor
  • Loading branch information
ametel01 authored Jun 10, 2024
2 parents a3a0ef9 + d01b324 commit 476b752
Show file tree
Hide file tree
Showing 14 changed files with 215 additions and 202 deletions.
4 changes: 2 additions & 2 deletions starknet/katana/declare.sh
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,6 @@ declare_contract() {
rm -f katana/declared-classes.txt

# Declare each contract and save the class hash
declare_contract "fact-registry" "target/dev/fossil_FactRegistry.contract_class.json"
declare_contract "headers-store" "target/dev/fossil_L1HeaderStore.contract_class.json"
declare_contract "messages-proxy" "target/dev/fossil_L1MessagesProxy.contract_class.json"
declare_contract "headers-store" "target/dev/fossil_L1HeaderStore.contract_class.json"
declare_contract "fact-registry" "target/dev/fossil_FactRegistry.contract_class.json"
83 changes: 65 additions & 18 deletions starknet/katana/deploy.sh
Original file line number Diff line number Diff line change
@@ -1,25 +1,72 @@
#!/bin/bash

# Deploy the contracts
deploy_contract() {
local contract_name=$1
local class_hash=$2
# Load the addresses from the deployed-contracts.txt file
declare -A class_hashes
declare -A addresses

echo "Deploying contract for $contract_name with class hash $class_hash..."

# Run the deployment command and capture the output
output=$(starkli deploy "$class_hash" --salt 0x1 -w)

echo "$contract_name: $output" >> katana/deployed-contracts.txt
echo "Deployment address for $contract_name saved to deployed-contracts.txt"
}

# Remove existing deployed-contracts.txt file if it exists
rm -f katana/deployed-contracts.txt

# Read each line from declared-classes.txt and deploy the contract
while IFS= read -r line; do
contract_name=$(echo "$line" | cut -d ':' -f 1 | xargs)
class_hash=$(echo "$line" | cut -d ' ' -f 2 | xargs)
deploy_contract "$contract_name" "$class_hash"

class_hashes["$contract_name"]=$class_hash
done < katana/declared-classes.txt

messages_proxy_class_hash=${class_hashes["messages-proxy"]}
headers_store_class_hash=${class_hashes["headers-store"]}
fact_registry_class_hash=${class_hashes["fact-registry"]}

# Debug: Print the class_hashes
echo "Fact Registry Class hash: $fact_registry_class_hash"
echo "Headers Store Class hash: $headers_store_class_hash"
echo "Messages Proxy Class hash: $messages_proxy_class_hash"

# Retrieve the L1_MESSAGE_SENDER_ADDRESS from the environment variables
l1_message_sender_address=${L1_MESSAGE_SENDER_ADDRESS}
owner_address=${OWNER_ADDRESS} # 0x6162896d1d7ab204c7ccac6dd5f8e9e7c25ecd5ae4fcb4ad32e57786bb46e03 //katana-0
admin_address=${ADMIN_ADDRESS}

# Check if the L1_MESSAGE_SENDER_ADDRESS environment variable is set
if [ -z "$l1_message_sender_address" ]; then
echo "Error: L1_MESSAGE_SENDER_ADDRESS environment variable is not set."
exit 1
fi

# Remove existing declared-classes.txt file if it exists
rm -f katana/deployed-contracts.txt

# Perform Deployment
# Message Proxy Deployment
echo "Deploying messages-proxy with L1_MESSAGE_SENDER_ADDRESS and owner address..."
output=$(starkli deploy "$messages_proxy_class_hash" "$l1_message_sender_address" "$owner_address" --salt 0x1 -w)
# output=$(starkli deploy "$messages_proxy_class_hash" "$l1_message_sender_address" katana-1 --salt 0x1 -w)
echo "messages-proxy: $output" >> katana/deployed-contracts.txt
messages_proxy=$output
echo "Messages Proxy address: $messages_proxy"
echo "Deployment address for messages-proxy saved to deployed-contracts.txt"
echo " "

# Header Store Deployment
echo "Deploying headers-store with messages-proxy address..."
output=$(starkli deploy "$headers_store_class_hash" "$messages_proxy" "$admin_address" --salt 0x1 -w)
echo "headers-store: $output" >> katana/deployed-contracts.txt
headers_store=$output
echo "Header Store address: $headers_store"
echo "Deployment address for headers-store saved to deployed-contracts.txt"
echo " "

# Fact Registry Deployment
echo "Deploying fact-registry with headers-store address and owner address..."
output=$(starkli deploy "$fact_registry_class_hash" "$headers_store" "$owner_address" --salt 0x1 -w)
echo "fact-registry: $output" >> katana/deployed-contracts.txt
echo "Fact Registry address: $headers_store"
echo "Deployment address for fact-registry saved to deployed-contracts.txt"

echo "Deployment complete."

echo " "

#Set l1_headers_store_address for messages-proxy
echo "Setting headers-store for messages-proxy contract..."
output=$(starkli invoke "$messages_proxy" set_l1_headers_store "$headers_store" --account katana-0 -w)

echo "l1_headers_store_address set complete."
48 changes: 0 additions & 48 deletions starknet/katana/initialize_contracts.sh

This file was deleted.

3 changes: 2 additions & 1 deletion starknet/katana/katana.env
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export STARKNET_ACCOUNT=katana-0
export STARKNET_RPC=http://0.0.0.0:5050
export OWNER_ADDRESS=0xb3ff441a68610b30fd5e2abbf3a1548eb6ba6f3559f2862bf2dc757e5828ca
export OWNER_ADDRESS=0x6162896d1d7ab204c7ccac6dd5f8e9e7c25ecd5ae4fcb4ad32e57786bb46e03
export ADMIN_ADDRESS=0xe29882a1fcba1e7e10cad46212257fea5c752a4f9b1b1ec683c503a2cf5c8a
export L1_MESSAGE_SENDER_ADDRESS=0x9fE46736679d2D9a65F0992F2272dE9f3c7fa6e0
57 changes: 29 additions & 28 deletions starknet/src/L1_headers_store/contract.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@ pub mod L1HeaderStore {
ownable: OwnableComponent::Storage,
#[substorage(v0)]
upgradeable: UpgradeableComponent::Storage,
initialized: bool,
l1_messages_origin: ContractAddress,
latest_l1_block: u64,
block_parent_hash: LegacyMap::<u64, u256>,
Expand All @@ -75,27 +74,30 @@ pub mod L1HeaderStore {
upgradeableEvent: UpgradeableComponent::Event
}

// *************************************************************************
// CONSTRUCTOR
// *************************************************************************
/// Contract Constructor.
///
/// # Arguments
/// * `l1_messages_origin` - The address of L1 Message Proxy.
/// * `admin` - .
#[constructor]
fn constructor(
ref self: ContractState,
l1_messages_origin: starknet::ContractAddress,
admin: starknet::ContractAddress
) {
self.l1_messages_origin.write(l1_messages_origin);
self.ownable.initializer(admin);
}

// *************************************************************************
// EXTERNAL FUNCTIONS
// *************************************************************************
// Implementation of IL1HeadersStore interface
#[abi(embed_v0)]
impl L1HeaderStoreImpl of IL1HeadersStore<ContractState> {
/// Initialize the contract.
///
/// # Arguments
/// * `l1_messages_origin` - The address of L1 Message Proxy.
fn initialize(
ref self: ContractState,
l1_messages_origin: starknet::ContractAddress,
admin: starknet::ContractAddress
) {
assert!(self.initialized.read() == false, "L1HeaderStore: already initialized");
self.initialized.write(true);
self.l1_messages_origin.write(l1_messages_origin);
self.ownable.initializer(admin);
}

/// Receives `block_number` and `parent_hash` from L1 Message Proxy for processing.
///
/// # Arguments
Expand All @@ -117,6 +119,17 @@ pub mod L1HeaderStore {
}
}

/// Change L1 Message Proxy address. (Only Owner)
///
/// # Arguments
/// * `l1_messages_origin` - The address of L1 Message Proxy.
fn change_l1_messages_origin(
ref self: ContractState, l1_messages_origin: starknet::ContractAddress
) {
self.ownable.assert_only_owner();
self.l1_messages_origin.write(l1_messages_origin);
}

fn store_state_root(ref self: ContractState, block_number: u64, state_root: u256) {
self.ownable.assert_only_owner();

Expand Down Expand Up @@ -147,18 +160,6 @@ pub mod L1HeaderStore {
};
}


/// Checks if the contract state has been initialized for a specific block number.
///
/// # Arguments
/// * `block_number` - The block number to check.
///
/// # Returns
/// * A boolean indicating whether the contract state has been initialized.
fn get_initialized(self: @ContractState, block_number: u64) -> bool {
self.initialized.read()
}

/// Retrieves the parent hash of the specified block number.
///
/// # Arguments
Expand Down
3 changes: 1 addition & 2 deletions starknet/src/L1_headers_store/interface.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,12 @@ use starknet::{EthAddress, ContractAddress};

#[starknet::interface]
pub trait IL1HeadersStore<TState> {
fn initialize(ref self: TState, l1_messages_origin: ContractAddress, admin: ContractAddress);
fn receive_from_l1(ref self: TState, parent_hash: u256, block_number: u64);
fn change_l1_messages_origin(ref self: TState, l1_messages_origin: starknet::ContractAddress);
fn store_state_root(ref self: TState, block_number: u64, state_root: u256);
fn store_many_state_roots(
ref self: TState, start_block: u64, end_block: u64, state_roots: Array<u256>
);
fn get_initialized(self: @TState, block_number: u64) -> bool;
fn get_parent_hash(self: @TState, block_number: u64) -> u256;
fn get_latest_l1_block(self: @TState) -> u64;
fn get_state_root(self: @TState, block_number: u64) -> u256;
Expand Down
46 changes: 23 additions & 23 deletions starknet/src/L1_messages_proxy/contract.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ pub mod L1MessagesProxy {
ownable: OwnableComponent::Storage,
#[substorage(v0)]
upgradeable: UpgradeableComponent::Storage,
initialized: bool,
l1_messages_sender: EthAddress,
l1_headers_store: IL1HeadersStoreDispatcher,
}
Expand All @@ -64,6 +63,23 @@ pub mod L1MessagesProxy {
upgradeableEvent: UpgradeableComponent::Event
}

// *************************************************************************
// CONSTRUCTOR
// *************************************************************************
/// Contract Constructor.
///
/// # Arguments
/// * `l1_messages_sender` - The address of the L1 solidity contract.
/// * `owner` - The owner address.
#[constructor]
fn constructor(
ref self: ContractState, l1_messages_sender: EthAddress, owner: starknet::ContractAddress
) {
self.l1_messages_sender.write(l1_messages_sender);
self.ownable.initializer(owner);
}


// *************************************************************************
// L1 HANDLER FUNCTION
// *************************************************************************
Expand Down Expand Up @@ -97,28 +113,20 @@ pub mod L1MessagesProxy {
// Implementation of IL1MessagesProxy interface
#[abi(embed_v0)]
impl L1MessagesProxyImpl of IL1MessagesProxy<ContractState> {
/// Initialize the contract.
/// Set the Header Store Address. (Only Owner)
///
/// # Arguments
/// * `l1_messages_sender` - The address of the L1 solidity contract.
/// * `l1_headers_store_address` - The address of the Header Store cairo contract.
/// * `owner` - The owner address.
fn initialize(
ref self: ContractState,
l1_messages_sender: EthAddress,
l1_headers_store_address: starknet::ContractAddress,
owner: starknet::ContractAddress
/// * `l1_headers_store_address` - The address of the header store cairo contract.
fn set_l1_headers_store(
ref self: ContractState, l1_headers_store_address: starknet::ContractAddress
) {
assert!(!self.get_initialized(), "L1MessagesProxy: already initialized");
self.initialized.write(true);
self.l1_messages_sender.write(l1_messages_sender);
self.ownable.assert_only_owner();
self
.l1_headers_store
.write(IL1HeadersStoreDispatcher { contract_address: l1_headers_store_address });
self.ownable.initializer(owner);
}

/// Change contract address.
/// Change contract address. (Only Owner)
///
/// # Arguments
/// * `l1_messages_sender` - The address of the L1 solidity contract.
Expand All @@ -135,14 +143,6 @@ pub mod L1MessagesProxy {
.write(IL1HeadersStoreDispatcher { contract_address: l1_headers_store_address });
}

/// Checks if the contract has been initialized
///
/// # Returns
/// * A boolean indicating whether the contract state has been initialized.
fn get_initialized(self: @ContractState) -> bool {
self.initialized.read()
}

/// Retrieves the sender address of L1 messages stored in the contract state.
///
/// # Returns
Expand Down
8 changes: 1 addition & 7 deletions starknet/src/L1_messages_proxy/interface.cairo
Original file line number Diff line number Diff line change
@@ -1,17 +1,11 @@
#[starknet::interface]
pub trait IL1MessagesProxy<TState> {
fn initialize(
ref self: TState,
l1_messages_sender: starknet::EthAddress,
l1_headers_store_address: starknet::ContractAddress,
owner: starknet::ContractAddress
);
fn set_l1_headers_store(ref self: TState, l1_headers_store_address: starknet::ContractAddress);
fn change_contract_addresses(
ref self: TState,
l1_messages_sender: starknet::EthAddress,
l1_headers_store_address: starknet::ContractAddress
);
fn get_initialized(self: @TState) -> bool;
fn get_l1_messages_sender(self: @TState) -> starknet::EthAddress;
fn get_l1_headers_store_address(self: @TState) -> starknet::ContractAddress;
}
Loading

0 comments on commit 476b752

Please sign in to comment.