Skip to content

Commit

Permalink
use oz upgradeable
Browse files Browse the repository at this point in the history
  • Loading branch information
augustbleeds committed Dec 13, 2024
1 parent ff8e41d commit 313ec2a
Show file tree
Hide file tree
Showing 24 changed files with 353 additions and 135 deletions.
29 changes: 20 additions & 9 deletions contracts/src/access_control/access_controller.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,18 @@ mod AccessController {
use starknet::class_hash::ClassHash;

use openzeppelin::access::ownable::OwnableComponent;
use openzeppelin::upgrades::UpgradeableComponent;

use chainlink::libraries::access_control::{AccessControlComponent, IAccessController};
use chainlink::libraries::type_and_version::ITypeAndVersion;
use chainlink::libraries::upgradeable::{Upgradeable, IUpgradeable};
use chainlink::libraries::upgrades::v2::owner_upgradeable::OwnerUpgradeableComponent;

component!(path: OwnableComponent, storage: ownable, event: OwnableEvent);
component!(path: AccessControlComponent, storage: access_control, event: AccessControlEvent);
component!(path: UpgradeableComponent, storage: upgradeable, event: UpgradeableEvent);
component!(
path: OwnerUpgradeableComponent, storage: owner_upgradeable, event: OwnerUpgradeableEvent
);

#[abi(embed_v0)]
impl OwnableImpl = OwnableComponent::OwnableTwoStepImpl<ContractState>;
Expand All @@ -21,13 +26,23 @@ mod AccessController {
AccessControlComponent::AccessControlImpl<ContractState>;
impl AccessControlInternalImpl = AccessControlComponent::InternalImpl<ContractState>;

impl UpgradeableInternalImpl = UpgradeableComponent::InternalImpl<ContractState>;

#[abi(embed_v0)]
impl OwnerUpgradeableImpl =
OwnerUpgradeableComponent::OwnerUpgradeableImpl<ContractState>;

#[event]
#[derive(Drop, starknet::Event)]
enum Event {
#[flat]
OwnableEvent: OwnableComponent::Event,
#[flat]
AccessControlEvent: AccessControlComponent::Event,
#[flat]
UpgradeableEvent: UpgradeableComponent::Event,
#[flat]
OwnerUpgradeableEvent: OwnerUpgradeableComponent::Event
}

#[storage]
Expand All @@ -36,6 +51,10 @@ mod AccessController {
ownable: OwnableComponent::Storage,
#[substorage(v0)]
access_control: AccessControlComponent::Storage,
#[substorage(v0)]
upgradeable: UpgradeableComponent::Storage,
#[substorage(v0)]
owner_upgradeable: OwnerUpgradeableComponent::Storage
}

#[constructor]
Expand All @@ -50,12 +69,4 @@ mod AccessController {
'AccessController 1.0.0'
}
}

#[abi(embed_v0)]
impl UpgradeableImpl of IUpgradeable<ContractState> {
fn upgrade(ref self: ContractState, new_impl: ClassHash) {
self.ownable.assert_only_owner();
Upgradeable::upgrade(new_impl);
}
}
}
2 changes: 1 addition & 1 deletion contracts/src/emergency/sequencer_uptime_feed.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ mod SequencerUptimeFeed {
use chainlink::ocr2::aggregator::Round;
use chainlink::ocr2::aggregator::IAggregator;
use chainlink::ocr2::aggregator::{Transmission};
use chainlink::libraries::upgradeable::Upgradeable;
use chainlink::libraries::upgrades::v1::upgradeable::Upgradeable;

component!(path: OwnableComponent, storage: ownable, event: OwnableEvent);
component!(path: AccessControlComponent, storage: access_control, event: AccessControlEvent);
Expand Down
2 changes: 1 addition & 1 deletion contracts/src/libraries.cairo
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
mod access_control;
mod token;
mod upgradeable;
mod upgrades;
mod mocks;
mod type_and_version;
mod enumerable_set;
1 change: 1 addition & 0 deletions contracts/src/libraries/mocks.cairo
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
mod mock_upgradeable;
mod mock_owner_upgradeable;
mod mock_non_upgradeable;
mod mock_multisig_target;
mod mock_enumerable_set;
66 changes: 66 additions & 0 deletions contracts/src/libraries/mocks/mock_owner_upgradeable.cairo
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
use starknet::class_hash::ClassHash;

#[starknet::interface]
trait IFoo<TContractState> {
fn foo(self: @TContractState) -> bool;
}

#[starknet::contract]
mod MockOwnerUpgradeable {
use starknet::class_hash::ClassHash;
use starknet::ContractAddress;

use openzeppelin::access::ownable::OwnableComponent;
use openzeppelin::upgrades::UpgradeableComponent;

use chainlink::libraries::upgrades::v2::owner_upgradeable::OwnerUpgradeableComponent;

component!(path: OwnableComponent, storage: ownable, event: OwnableEvent);
component!(path: UpgradeableComponent, storage: upgradeable, event: UpgradeableEvent);
component!(
path: OwnerUpgradeableComponent, storage: owner_upgradeable, event: OwnerUpgradeableEvent
);

#[abi(embed_v0)]
impl OwnableImpl = OwnableComponent::OwnableTwoStepImpl<ContractState>;
impl OwnableInternalImpl = OwnableComponent::InternalImpl<ContractState>;

impl UpgradeableInternalImpl = UpgradeableComponent::InternalImpl<ContractState>;

#[abi(embed_v0)]
impl OwnerUpgradeableImpl =
OwnerUpgradeableComponent::OwnerUpgradeableImpl<ContractState>;

#[storage]
struct Storage {
#[substorage(v0)]
ownable: OwnableComponent::Storage,
#[substorage(v0)]
upgradeable: UpgradeableComponent::Storage,
#[substorage(v0)]
owner_upgradeable: OwnerUpgradeableComponent::Storage
}

#[event]
#[derive(Drop, starknet::Event)]
enum Event {
#[flat]
OwnableEvent: OwnableComponent::Event,
#[flat]
UpgradeableEvent: UpgradeableComponent::Event,
#[flat]
OwnerUpgradeableEvent: OwnerUpgradeableComponent::Event
}

#[constructor]
fn constructor(ref self: ContractState, owner: ContractAddress) {
self.ownable.initializer(owner);
}

#[abi(embed_v0)]
impl FooImpl of super::IFoo<ContractState> {
fn foo(self: @ContractState) -> bool {
true
}
}
}
2 changes: 1 addition & 1 deletion contracts/src/libraries/mocks/mock_upgradeable.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ trait IMockUpgradeable<TContractState> {
mod MockUpgradeable {
use starknet::class_hash::ClassHash;

use chainlink::libraries::upgradeable::Upgradeable;
use chainlink::libraries::upgrades::v1::upgradeable::Upgradeable;

#[storage]
struct Storage {}
Expand Down
3 changes: 3 additions & 0 deletions contracts/src/libraries/upgrades.cairo
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
mod v1;
mod v2;
mod utils;
Empty file.
1 change: 1 addition & 0 deletions contracts/src/libraries/upgrades/v1.cairo
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
mod upgradeable;
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
use starknet::class_hash::ClassHash;

// TODO: drop for OZ upgradeable
// DEPRECATED: Kept around for the starknet multisig.
// Use OZ for internal upgradeability and v2/owner_upgradeable.cairo for public owner
// upgradeablility

#[starknet::interface]
trait IUpgradeable<TContractState> {
Expand Down
1 change: 1 addition & 0 deletions contracts/src/libraries/upgrades/v2.cairo
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
mod owner_upgradeable;
41 changes: 41 additions & 0 deletions contracts/src/libraries/upgrades/v2/owner_upgradeable.cairo
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#[starknet::component]
mod OwnerUpgradeableComponent {
use openzeppelin::{
access::ownable::{
OwnableComponent, OwnableComponent::InternalTrait as OwnableInternalTrait
},
upgrades::{
upgradeable::{
UpgradeableComponent,
UpgradeableComponent::InternalTrait as UpgradeableInternalTrait
},
interface::IUpgradeable,
},
};
use starknet::class_hash::ClassHash;

#[storage]
struct Storage {}

#[event]
#[derive(Drop, starknet::Event)]
enum Event {}

#[embeddable_as(OwnerUpgradeableImpl)]
pub impl OwnerUpgradeable<
TContractState,
+HasComponent<TContractState>,
impl Ownable: OwnableComponent::HasComponent<TContractState>,
impl Upgradeable: UpgradeableComponent::HasComponent<TContractState>,
+Drop<TContractState>
> of IUpgradeable<ComponentState<TContractState>> {
fn upgrade(ref self: ComponentState<TContractState>, new_class_hash: ClassHash) {
let mut ownable_component = get_dep_component_mut!(ref self, Ownable);
ownable_component.assert_only_owner();

let mut upgradeable_component = get_dep_component_mut!(ref self, Upgradeable);
upgradeable_component.upgrade(new_class_hash);
}
}
}

2 changes: 1 addition & 1 deletion contracts/src/multisig.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ mod Multisig {
use starknet::storage::Map;

use chainlink::libraries::type_and_version::ITypeAndVersion;
use chainlink::libraries::upgradeable::{Upgradeable, IUpgradeable};
use chainlink::libraries::upgrades::v1::upgradeable::{Upgradeable, IUpgradeable};

#[event]
#[derive(Drop, starknet::Event)]
Expand Down
68 changes: 33 additions & 35 deletions contracts/src/ocr2/aggregator.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -170,49 +170,43 @@ impl SpanLegacyHash<T, impl THash: LegacyHash<T>, impl TCopy: Copy<T>> of Legacy

#[starknet::contract]
mod Aggregator {
use super::Round;
use super::{Transmission};
use super::SpanLegacyHash;
use super::pow;
use super::{Round, Transmission, SpanLegacyHash, pow};

use array::ArrayTrait;
use array::SpanTrait;
use array::{ArrayTrait, SpanTrait};
use box::BoxTrait;
use hash::LegacyHash;
use integer::U128IntoFelt252;
use integer::u128s_from_felt252;
use integer::U128sFromFelt252Result;
use integer::{U128IntoFelt252, u128s_from_felt252, U128sFromFelt252Result};
use zeroable::Zeroable;
use traits::Into;
use traits::TryInto;
use traits::{Into, TryInto};
use option::OptionTrait;

use starknet::ContractAddress;
use starknet::get_caller_address;
use starknet::contract_address_const;
use starknet::StorageBaseAddress;
use starknet::SyscallResult;
use starknet::storage_read_syscall;
use starknet::storage_write_syscall;
use starknet::storage_address_from_base_and_offset;
use starknet::class_hash::ClassHash;
use starknet::storage::Map;
use starknet::{
ContractAddress, get_caller_address, contract_address_const, StorageBaseAddress,
SyscallResult, storage_read_syscall, storage_write_syscall,
storage_address_from_base_and_offset, class_hash::ClassHash, storage::Map
};

use openzeppelin::access::ownable::OwnableComponent;
use openzeppelin::upgrades::UpgradeableComponent;
use openzeppelin::token::erc20::interface::{IERC20, IERC20Dispatcher, IERC20DispatcherTrait};

use chainlink::utils::split_felt;
use chainlink::libraries::access_control::{AccessControlComponent, IAccessController};
use chainlink::libraries::access_control::AccessControlComponent::InternalTrait as AccessControlInternalTrait;
use chainlink::libraries::upgradeable::{Upgradeable, IUpgradeable};

use chainlink::libraries::access_control::{
AccessControlComponent, IAccessController,
AccessControlComponent::InternalTrait as AccessControlInternalTrait
};
use chainlink::libraries::upgrades::v2::owner_upgradeable::OwnerUpgradeableComponent;
use chainlink::libraries::access_control::{
IAccessControllerDispatcher, IAccessControllerDispatcherTrait
};
use chainlink::libraries::type_and_version::ITypeAndVersion;

component!(path: OwnableComponent, storage: ownable, event: OwnableEvent);
component!(path: AccessControlComponent, storage: access_control, event: AccessControlEvent);
component!(path: UpgradeableComponent, storage: upgradeable, event: UpgradeableEvent);
component!(
path: OwnerUpgradeableComponent, storage: owner_upgradeable, event: OwnerUpgradeableEvent
);

#[abi(embed_v0)]
impl OwnableImpl = OwnableComponent::OwnableTwoStepImpl<ContractState>;
Expand All @@ -223,6 +217,12 @@ mod Aggregator {
AccessControlComponent::AccessControlImpl<ContractState>;
impl AccessControlInternalImpl = AccessControlComponent::InternalImpl<ContractState>;

impl UpgradeableInternalImpl = UpgradeableComponent::InternalImpl<ContractState>;

#[abi(embed_v0)]
impl OwnerUpgradeableImpl =
OwnerUpgradeableComponent::OwnerUpgradeableImpl<ContractState>;

const GIGA: u128 = 1000000000_u128;

const MAX_ORACLES: u32 = 31_u32;
Expand All @@ -234,6 +234,10 @@ mod Aggregator {
OwnableEvent: OwnableComponent::Event,
#[flat]
AccessControlEvent: AccessControlComponent::Event,
#[flat]
UpgradeableEvent: UpgradeableComponent::Event,
#[flat]
OwnerUpgradeableEvent: OwnerUpgradeableComponent::Event,
NewTransmission: NewTransmission,
ConfigSet: ConfigSet,
LinkTokenSet: LinkTokenSet,
Expand Down Expand Up @@ -294,6 +298,10 @@ mod Aggregator {
ownable: OwnableComponent::Storage,
#[substorage(v0)]
access_control: AccessControlComponent::Storage,
#[substorage(v0)]
upgradeable: UpgradeableComponent::Storage,
#[substorage(v0)]
owner_upgradeable: OwnerUpgradeableComponent::Storage,
/// Maximum number of faulty oracles
_f: u8,
_latest_epoch_and_round: u64, // (u32, u32)
Expand Down Expand Up @@ -409,16 +417,6 @@ mod Aggregator {
self._description.write(description);
}

// --- Upgradeable ---

#[abi(embed_v0)]
impl UpgradeableImpl of IUpgradeable<ContractState> {
fn upgrade(ref self: ContractState, new_impl: ClassHash) {
self.ownable.assert_only_owner();
Upgradeable::upgrade(new_impl)
}
}

// --- Validation ---

// NOTE: Currently unimplemented:
Expand Down
Loading

0 comments on commit 313ec2a

Please sign in to comment.