-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add config and messaging base of contracts
- Loading branch information
Showing
18 changed files
with
817 additions
and
69 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,2 @@ | ||
scarb 2.5.0 | ||
scarb 2.4.4 | ||
starknet-foundry 0.16.0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
//! SPDX-License-Identifier: MIT | ||
//! | ||
//! | ||
|
||
mod Errors { | ||
const INVALID_ADDRESS: felt252 = 'Config: invalid address'; | ||
} | ||
|
||
/// Appchain settlement contract on starknet. | ||
#[starknet::contract] | ||
mod appchain { | ||
use starknet::ContractAddress; | ||
use openzeppelin::access::ownable::{OwnableComponent as ownable_cpt, interface::IOwnable}; | ||
|
||
use piltover::config::{config_cpt, config_cpt::InternalTrait as ConfigInternal, IConfig}; | ||
|
||
component!(path: ownable_cpt, storage: ownable, event: OwnableEvent); | ||
component!(path: config_cpt, storage: config, event: ConfigEvent); | ||
|
||
#[abi(embed_v0)] | ||
impl ConfigImpl = config_cpt::ConfigImpl<ContractState>; | ||
|
||
#[storage] | ||
struct Storage { | ||
#[substorage(v0)] | ||
ownable: ownable_cpt::Storage, | ||
#[substorage(v0)] | ||
config: config_cpt::Storage, | ||
} | ||
|
||
#[event] | ||
#[derive(Drop, starknet::Event)] | ||
enum Event { | ||
#[flat] | ||
OwnableEvent: ownable_cpt::Event, | ||
#[flat] | ||
ConfigEvent: config_cpt::Event, | ||
} | ||
|
||
/// Initializes the contract. | ||
/// | ||
/// # Arguments | ||
/// | ||
/// * `address` - The contract address of the owner. | ||
#[constructor] | ||
fn constructor(ref self: ContractState, owner: ContractAddress) { | ||
self.ownable.transfer_ownership(owner); | ||
|
||
assert(self.config.is_owner_or_operator(owner), 'bad'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
//! SPDX-License-Identifier: MIT | ||
//! | ||
//! Base configuration for appchain contract. | ||
|
||
/// Errors. | ||
mod errors { | ||
const INVALID_CALLER: felt252 = 'Config: not owner or operator'; | ||
} | ||
|
||
/// Configuration component. | ||
/// | ||
/// Depends on `ownable` to ensure the configuration is | ||
/// only editable by contract's owner. | ||
#[starknet::component] | ||
mod config_cpt { | ||
use starknet::ContractAddress; | ||
|
||
use openzeppelin::access::ownable::{ | ||
OwnableComponent as ownable_cpt, OwnableComponent::InternalTrait as OwnableInternal, | ||
interface::IOwnable, | ||
}; | ||
|
||
use piltover::config::interface::IConfig; | ||
|
||
use super::errors; | ||
|
||
#[storage] | ||
struct Storage { | ||
/// Appchain operator that is allowed to update the state. | ||
operator: ContractAddress, | ||
/// Program info (StarknetOS), with program hash and config hash. | ||
program_info: (felt252, felt252), | ||
/// Facts registry contract address. | ||
facts_registry: ContractAddress, | ||
} | ||
|
||
#[embeddable_as(ConfigImpl)] | ||
impl Config< | ||
TContractState, | ||
+HasComponent<TContractState>, | ||
impl Ownable: ownable_cpt::HasComponent<TContractState>, | ||
+Drop<TContractState> | ||
> of IConfig<ComponentState<TContractState>> { | ||
fn set_operator(ref self: ComponentState<TContractState>, address: ContractAddress) { | ||
get_dep_component!(self, Ownable).assert_only_owner(); | ||
self.operator.write(address) | ||
} | ||
|
||
fn get_operator(self: @ComponentState<TContractState>) -> ContractAddress { | ||
self.operator.read() | ||
} | ||
|
||
fn set_program_info( | ||
ref self: ComponentState<TContractState>, program_hash: felt252, config_hash: felt252 | ||
) { | ||
self.assert_only_owner_or_operator(); | ||
self.program_info.write((program_hash, config_hash)) | ||
} | ||
|
||
fn get_program_info(self: @ComponentState<TContractState>) -> (felt252, felt252) { | ||
self.program_info.read() | ||
} | ||
|
||
fn set_facts_registry(ref self: ComponentState<TContractState>, address: ContractAddress) { | ||
self.assert_only_owner_or_operator(); | ||
self.facts_registry.write(address) | ||
} | ||
|
||
fn get_facts_registry(self: @ComponentState<TContractState>) -> ContractAddress { | ||
self.facts_registry.read() | ||
} | ||
} | ||
|
||
#[generate_trait] | ||
impl InternalImpl< | ||
TContractState, | ||
+HasComponent<TContractState>, | ||
impl Ownable: ownable_cpt::HasComponent<TContractState>, | ||
+Drop<TContractState> | ||
> of InternalTrait<TContractState> { | ||
/// Asserts if the caller is the owner of the contract or | ||
/// the authorized operator. Reverts otherwise. | ||
fn assert_only_owner_or_operator(ref self: ComponentState<TContractState>) { | ||
assert( | ||
self.is_owner_or_operator(starknet::get_caller_address()), errors::INVALID_CALLER | ||
); | ||
} | ||
|
||
/// Verifies if the given address is the owner of the contract | ||
/// or the authorized operator. | ||
/// | ||
/// # Arguments | ||
/// | ||
/// * `address` - The contrat address to verify. | ||
fn is_owner_or_operator( | ||
ref self: ComponentState<TContractState>, address: ContractAddress | ||
) -> bool { | ||
let owner = get_dep_component!(self, Ownable).owner(); | ||
address == owner || address == self.operator.read() | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
//! SPDX-License-Identifier: MIT | ||
//! | ||
//! Interface for appchain settlement contract configuration. | ||
use starknet::ContractAddress; | ||
|
||
#[starknet::interface] | ||
trait IConfig<T> { | ||
/// Sets the operator that is in charge to push state updates. | ||
/// | ||
/// # Arguments | ||
/// | ||
/// * `address` - The operator account address. | ||
fn set_operator(ref self: T, address: ContractAddress); | ||
|
||
/// Gets the operator address. | ||
/// | ||
/// # Returns | ||
/// | ||
/// The operator's address. | ||
fn get_operator(self: @T) -> ContractAddress; | ||
|
||
/// Sets the information of the program that generates the | ||
/// state transition trace (namely StarknetOS). | ||
/// | ||
/// # Arguments | ||
/// | ||
/// * `program_hash` - The program hash. | ||
/// * `config_hash` - The program's config hash. | ||
fn set_program_info(ref self: T, program_hash: felt252, config_hash: felt252); | ||
|
||
/// Gets the information of the program that generates the | ||
/// state transition trace (namely StarknetOS). | ||
/// | ||
/// # Returns | ||
/// | ||
/// The program hash and it's configuration hash. | ||
fn get_program_info(self: @T) -> (felt252, felt252); | ||
|
||
/// Sets the facts registry contract address, which is already | ||
/// initialized with the verifier information. | ||
/// | ||
/// # Arguments | ||
/// | ||
/// * `address` - The facts registry contract's address. | ||
fn set_facts_registry(ref self: T, address: ContractAddress); | ||
|
||
/// Gets the facts registry contract address. | ||
/// | ||
/// # Returns | ||
/// | ||
/// The contract address of the facts registry. | ||
fn get_facts_registry(self: @T) -> ContractAddress; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
#[starknet::contract] | ||
mod config_mock { | ||
use starknet::ContractAddress; | ||
|
||
use openzeppelin::access::ownable::{ | ||
OwnableComponent as ownable_cpt, OwnableComponent::InternalTrait as OwnableInternal | ||
}; | ||
|
||
use piltover::config::{config_cpt, config_cpt::InternalTrait as ConfigInternal, IConfig}; | ||
|
||
component!(path: ownable_cpt, storage: ownable, event: OwnableEvent); | ||
component!(path: config_cpt, storage: config, event: ConfigEvent); | ||
|
||
#[abi(embed_v0)] | ||
impl ConfigImpl = config_cpt::ConfigImpl<ContractState>; | ||
|
||
#[storage] | ||
struct Storage { | ||
#[substorage(v0)] | ||
ownable: ownable_cpt::Storage, | ||
#[substorage(v0)] | ||
config: config_cpt::Storage | ||
} | ||
|
||
#[event] | ||
#[derive(Drop, starknet::Event)] | ||
enum Event { | ||
#[flat] | ||
OwnableEvent: ownable_cpt::Event, | ||
#[flat] | ||
ConfigEvent: config_cpt::Event | ||
} | ||
|
||
#[constructor] | ||
fn constructor(ref self: ContractState, owner: ContractAddress) { | ||
self.ownable.initializer(owner); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
//! SPDX-License-Identifier: MIT | ||
//! | ||
//! Interface for appchain settlement contract. | ||
use starknet::ContractAddress; | ||
|
||
#[starknet::interface] | ||
trait IAppchain<T> { | ||
/// Sets the operator that is in charge to push state updates. | ||
/// | ||
/// # Arguments | ||
/// | ||
/// * `address` - The operator account address. | ||
fn set_operator(ref self: T, address: ContractAddress); | ||
|
||
/// Gets the operator address. | ||
/// | ||
/// # Returns | ||
/// | ||
/// The operator's address. | ||
fn get_operator(self: @T) -> ContractAddress; | ||
|
||
/// Sets the information of the program that generates the | ||
/// state transition trace (namely StarknetOS). | ||
/// | ||
/// # Arguments | ||
/// | ||
/// * `program_hash` - The program hash. | ||
/// * `config_hash` - The program's config hash. | ||
fn set_program_info(ref self: T, program_hash: felt252, config_hash: felt252); | ||
|
||
/// Gets the information of the program that generates the | ||
/// state transition trace (namely StarknetOS). | ||
/// | ||
/// # Returns | ||
/// | ||
/// The program hash and it's configuration hash. | ||
fn get_program_info(self: @T) -> (felt252, felt252); | ||
|
||
/// Sets the facts registry contract address, which is already | ||
/// initialized with the verifier information. | ||
/// | ||
/// # Arguments | ||
/// | ||
/// * `address` - The facts registry contract's address. | ||
fn set_facts_registry(ref self: T, address: ContractAddress); | ||
|
||
/// Gets the facts registry contract address. | ||
/// | ||
/// # Returns | ||
/// | ||
/// The contract address of the facts registry. | ||
fn get_facts_registry(self: @T) -> ContractAddress; | ||
} |
Oops, something went wrong.